Skip to content
This repository has been archived by the owner on Jan 25, 2021. It is now read-only.

Commit

Permalink
added find scripthashes
Browse files Browse the repository at this point in the history
  • Loading branch information
apisit committed Apr 27, 2018
1 parent 1ed0611 commit 273eed0
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 5 deletions.
2 changes: 2 additions & 0 deletions neoutils/smartcontract/const.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ type NetworkFeeAmount float64

type Operation string

const scripthashLength = 20

const (
NEO NativeAsset = "c56f33fc6ecfcd0c225c4ab356fee59390af8560be0e930faebe74a6daff7c9b"
GAS NativeAsset = "602c79718b16e442de58778e148d0b1084e3b2dffd5de6b7b16cee7969282de7"
Expand Down
23 changes: 23 additions & 0 deletions neoutils/smartcontract/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,26 @@ type appcall struct {
scriptHash []byte
}

func (p *Parser) FindScriptHashes() ([]string, error) {
list := []string{}
b, err := hex.DecodeString(p.Script)
if err != nil {
return list, err
}
reversed := reverseBytes(b)
splittedByAppCall := bytes.Split(reversed, []byte{byte(APPCALL)})
for _, v := range splittedByAppCall {
if len(v) >= 20 {
startIndex := len(v) - scripthashLength
endIndex := len(v)
s := reversed[startIndex:endIndex]
hashString := fmt.Sprintf("%x", s)
list = append(list, hashString)
}
}
return list, nil
}

func (p *Parser) splitScriptWithAPPCALL() ([]appcall, error) {

list := []appcall{}
Expand Down Expand Up @@ -201,6 +221,9 @@ func (p *Parser) splitScriptWithAPPCALL() ([]appcall, error) {
tempOperationAndArgs := []byte{}
tempScriptHash := []byte{}
if index == 0 {
// if len(splitted[index+1]) < 20 {
// continue
// }
tempOperationAndArgs = splitted[index]
tempScriptHash = reverseBytes(splitted[index+1][:20])
} else {
Expand Down
14 changes: 13 additions & 1 deletion neoutils/smartcontract/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func TestParserGetListOfOperations(t *testing.T) {

func TestGetScripthashFromScript(t *testing.T) {
expectedResult := "b7c1f850a025e34455e7e98c588c784385077fb1"
p := smartcontract.NewParserWithScript("0830f4e2644b020000140c17e908b4014177e01d1a7fc3e6b5ed1ea83905141ffb723601fe7bf5e78b9ec6f6c79d69e317b9c753c1087472616e7366657267cf9472821400ceb06ca780c2a937fec5bbec51b9661a50b4e3696743e8")
p := smartcontract.NewParserWithScript("0800b90eef16000000141ffb723601fe7bf5e78b9ec6f6c79d69e317b9c71456f2328b6ecb7e42e7875eed2f6f8d674dab383b53c1087472616e7366657267fb1c540417067c270dee32f21023aa8b9b71abce66c4485313df54e64f")
result, err := p.GetListOfScriptHashes()
if err != nil {
log.Printf("Expected: %v but got error : %v", expectedResult, err)
Expand All @@ -50,6 +50,18 @@ func TestGetScripthashFromScript(t *testing.T) {
log.Printf("result = %v", result)
}

func TestFindScriptHash(t *testing.T) {
s := "0800b90eef16000000141ffb723601fe7bf5e78b9ec6f6c79d69e317b9c71456f2328b6ecb7e42e7875eed2f6f8d674dab383b53c1087472616e7366657267fb1c540417067c270dee32f21023aa8b9b71abce66c4485313df54e64f"

p := smartcontract.NewParserWithScript(s)
list, err := p.FindScriptHashes()
if err != nil {
t.Fail()
return
}
log.Printf("%+v", list)
}

func TestParserSingleAPPCALL(t *testing.T) {
// expectedToAddress := "AM8pnu1yK7ViMt7Sw2nPpbtPQXTwjjkykn"

Expand Down
5 changes: 1 addition & 4 deletions neoutils/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,10 @@ func TestConvertScripthashFromParamToNEOAddress(t *testing.T) {
}

func TestScriptHashToNEOAddress(t *testing.T) {
hash := "cc1bf80ceb9db91792c84feb8353921d9df3b4e8"

hash := "ceab719b8baa2310f232ee0d277c061704541cfb"
address := ScriptHashToNEOAddress(hash)

scripthash := NEOAddressToScriptHash(address)
log.Printf("address = %v result = %s", address, scripthash)

if scripthash != hash {
t.Fail()
}
Expand Down

0 comments on commit 273eed0

Please sign in to comment.