-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add methods signature parser, decouple ABI parser
- Loading branch information
1 parent
bf867f4
commit d8b99a9
Showing
6 changed files
with
176 additions
and
61 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package celoutils | ||
|
||
import ( | ||
"io" | ||
|
||
"github.com/celo-org/celo-blockchain/accounts/abi" | ||
) | ||
|
||
type ( | ||
ParsedABI struct { | ||
EventSignatures []EventSignature | ||
MethodSignatures []MethodSignature | ||
} | ||
) | ||
|
||
func ParseABI(abiReader io.Reader) (*ParsedABI, error) { | ||
abi, err := abi.JSON(abiReader) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
var ( | ||
eventSignatures []EventSignature | ||
methodsSignatures []MethodSignature | ||
) | ||
|
||
for _, v := range abi.Events { | ||
eventSignature, err := EventSignatureFromString(v.Sig) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
eventSignatures = append(eventSignatures, eventSignature) | ||
} | ||
|
||
for _, v := range abi.Methods { | ||
methodSignature, err := MethodSignatureFromString(v.Sig) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
methodsSignatures = append(methodsSignatures, methodSignature) | ||
} | ||
|
||
return &ParsedABI{ | ||
EventSignatures: eventSignatures, | ||
MethodSignatures: methodsSignatures, | ||
}, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package celoutils | ||
|
||
import ( | ||
"reflect" | ||
"strings" | ||
"testing" | ||
) | ||
|
||
func TestParseABI(t *testing.T) { | ||
// contract T { | ||
// event received(address sender, uint amount, bytes memo); | ||
// event receivedAddr(address sender); | ||
// function receive(bytes memo) external payable { | ||
// received(msg.sender, msg.value, memo); | ||
// receivedAddr(msg.sender); | ||
// } | ||
// } | ||
// https://www.4byte.directory/event-signatures | ||
// 253837 received(address,uint256,bytes) 0x75fd880d39c1daf53b6547ab6cb59451fc6452d27caa90e5b6649dd8293b9eed | ||
// 253838 receivedAddr(address) 0x46923992397eac56cf13058aced2a1871933622717e27b24eabc13bf9dd329c8 | ||
eventSignatures := []EventSignature{ | ||
{ | ||
Signature: "received(address,uint256,bytes)", | ||
Hash: "0x75fd880d39c1daf53b6547ab6cb59451fc6452d27caa90e5b6649dd8293b9eed", | ||
}, | ||
{ | ||
Signature: "receivedAddr(address)", | ||
Hash: "0x46923992397eac56cf13058aced2a1871933622717e27b24eabc13bf9dd329c8", | ||
}, | ||
} | ||
|
||
methodSignatures := []MethodSignature{ | ||
{ | ||
Signature: "receive(bytes)", | ||
Hash: "a69b6ed0", | ||
}, | ||
} | ||
|
||
want := &ParsedABI{ | ||
EventSignatures: eventSignatures, | ||
MethodSignatures: methodSignatures, | ||
} | ||
|
||
json := `[{"constant":false,"inputs":[{"name":"memo","type":"bytes"}],"name":"receive","outputs":[],"payable":true,"stateMutability":"payable","type":"function"},{"anonymous":false,"inputs":[{"indexed":false,"name":"sender","type":"address"},{"indexed":false,"name":"amount","type":"uint256"},{"indexed":false,"name":"memo","type":"bytes"}],"name":"received","type":"event"},{"anonymous":false,"inputs":[{"indexed":false,"name":"sender","type":"address"}],"name":"receivedAddr","type":"event"}]` | ||
got, err := ParseABI(strings.NewReader(json)) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
if !reflect.DeepEqual(got.EventSignatures, want.EventSignatures) { | ||
t.Errorf("Event signatures slices should be equal, got %v and %v", got, want) | ||
} | ||
|
||
if !reflect.DeepEqual(got.MethodSignatures, want.MethodSignatures) { | ||
t.Errorf("Method signatures slices should be equal, got %v and %v", got, want) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package celoutils | ||
|
||
import ( | ||
"encoding/hex" | ||
|
||
"github.com/grassrootseconomics/w3-celo" | ||
) | ||
|
||
type ( | ||
MethodSignature struct { | ||
Signature string | ||
Hash string | ||
} | ||
) | ||
|
||
func MethodSignatureFromString(methodSignature string) (MethodSignature, error) { | ||
method, err := w3.NewFunc(methodSignature, "") | ||
if err != nil { | ||
return MethodSignature{}, err | ||
} | ||
|
||
return MethodSignature{ | ||
Signature: method.Signature, | ||
Hash: hex.EncodeToString(method.Selector[:]), | ||
}, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package celoutils | ||
|
||
import ( | ||
"reflect" | ||
"testing" | ||
) | ||
|
||
func TestMethodSignatureFromString(t *testing.T) { | ||
type args struct { | ||
methodSignature string | ||
} | ||
tests := []struct { | ||
name string | ||
args args | ||
want MethodSignature | ||
wantErr bool | ||
}{ | ||
{ | ||
name: "Transfer Method", | ||
args: args{ | ||
methodSignature: "transfer(address, uint256)", | ||
}, | ||
want: MethodSignature{ | ||
Signature: "transfer(address,uint256)", | ||
Hash: "a9059cbb", | ||
}, | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
got, err := MethodSignatureFromString(tt.args.methodSignature) | ||
if (err != nil) != tt.wantErr { | ||
t.Errorf("MethodSignatureFromString() error = %v, wantErr %v", err, tt.wantErr) | ||
return | ||
} | ||
if !reflect.DeepEqual(got, tt.want) { | ||
t.Errorf("MethodSignatureFromString() = %v, want %v", got, tt.want) | ||
} | ||
}) | ||
} | ||
} |