Skip to content

Commit 17381ec

Browse files
holimangballet
authored andcommitted
core/signer, clef: improve ui-test flow, fix errors in uint handling (#19584)
* core/signer, clef: improve ui-test flow, fix errors in uint handling for eip-712 * core/signer: add fuzzer testcases + crashfixes * signer: address review concerns, check sign in integer parsing
1 parent 7a22da9 commit 17381ec

27 files changed

Lines changed: 1326 additions & 557 deletions

‎cmd/clef/main.go‎

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ import (
3333
"path/filepath"
3434
"runtime"
3535
"strings"
36+
"time"
3637

3738
"github.com/ethereum/go-ethereum/accounts"
3839
"github.com/ethereum/go-ethereum/accounts/keystore"
@@ -638,43 +639,50 @@ func testExternalUI(api *core.SignerAPI) {
638639
errs := make([]string, 0)
639640

640641
a := common.HexToAddress("0xdeadbeef000000000000000000000000deadbeef")
642+
addErr := func(errStr string) {
643+
log.Info("Test error", "error", errStr)
644+
errs = append(errs, errStr)
645+
}
641646

642647
queryUser := func(q string) string {
643648
resp, err := api.UI.OnInputRequired(core.UserInputRequest{
644649
Title: "Testing",
645650
Prompt: q,
646651
})
647652
if err != nil {
648-
errs = append(errs, err.Error())
653+
addErr(err.Error())
649654
}
650655
return resp.Text
651656
}
652657
expectResponse := func(testcase, question, expect string) {
653658
if got := queryUser(question); got != expect {
654-
errs = append(errs, fmt.Sprintf("%s: got %v, expected %v", testcase, got, expect))
659+
addErr(fmt.Sprintf("%s: got %v, expected %v", testcase, got, expect))
655660
}
656661
}
657662
expectApprove := func(testcase string, err error) {
658663
if err == nil || err == accounts.ErrUnknownAccount {
659664
return
660665
}
661-
errs = append(errs, fmt.Sprintf("%v: expected no error, got %v", testcase, err.Error()))
666+
addErr(fmt.Sprintf("%v: expected no error, got %v", testcase, err.Error()))
662667
}
663668
expectDeny := func(testcase string, err error) {
664669
if err == nil || err != core.ErrRequestDenied {
665-
errs = append(errs, fmt.Sprintf("%v: expected ErrRequestDenied, got %v", testcase, err))
670+
addErr(fmt.Sprintf("%v: expected ErrRequestDenied, got %v", testcase, err))
666671
}
667672
}
668-
673+
var delay = 1 * time.Second
669674
// Test display of info and error
670675
{
671676
api.UI.ShowInfo("If you see this message, enter 'yes' to next question")
677+
time.Sleep(delay)
672678
expectResponse("showinfo", "Did you see the message? [yes/no]", "yes")
673679
api.UI.ShowError("If you see this message, enter 'yes' to the next question")
680+
time.Sleep(delay)
674681
expectResponse("showerror", "Did you see the message? [yes/no]", "yes")
675682
}
676683
{ // Sign data test - clique header
677684
api.UI.ShowInfo("Please approve the next request for signing a clique header")
685+
time.Sleep(delay)
678686
cliqueHeader := types.Header{
679687
common.HexToHash("0000H45H"),
680688
common.HexToHash("0000H45H"),
@@ -700,21 +708,35 @@ func testExternalUI(api *core.SignerAPI) {
700708
_, err = api.SignData(ctx, accounts.MimetypeClique, *addr, hexutil.Encode(cliqueRlp))
701709
expectApprove("signdata - clique header", err)
702710
}
711+
{ // Sign data test - typed data
712+
api.UI.ShowInfo("Please approve the next request for signing EIP-712 typed data")
713+
time.Sleep(delay)
714+
addr, _ := common.NewMixedcaseAddressFromString("0x0011223344556677889900112233445566778899")
715+
data := `{"types":{"EIP712Domain":[{"name":"name","type":"string"},{"name":"version","type":"string"},{"name":"chainId","type":"uint256"},{"name":"verifyingContract","type":"address"}],"Person":[{"name":"name","type":"string"},{"name":"test","type":"uint8"},{"name":"wallet","type":"address"}],"Mail":[{"name":"from","type":"Person"},{"name":"to","type":"Person"},{"name":"contents","type":"string"}]},"primaryType":"Mail","domain":{"name":"Ether Mail","version":"1","chainId":"1","verifyingContract":"0xCCCcccccCCCCcCCCCCCcCcCccCcCCCcCcccccccC"},"message":{"from":{"name":"Cow","test":"3","wallet":"0xcD2a3d9F938E13CD947Ec05AbC7FE734Df8DD826"},"to":{"name":"Bob","wallet":"0xbBbBBBBbbBBBbbbBbbBbbbbBBbBbbbbBbBbbBBbB","test":"2"},"contents":"Hello, Bob!"}}`
716+
//_, err := api.SignData(ctx, accounts.MimetypeTypedData, *addr, hexutil.Encode([]byte(data)))
717+
var typedData core.TypedData
718+
err := json.Unmarshal([]byte(data), &typedData)
719+
_, err = api.SignTypedData(ctx, *addr, typedData)
720+
expectApprove("sign 712 typed data", err)
721+
}
703722
{ // Sign data test - plain text
704723
api.UI.ShowInfo("Please approve the next request for signing text")
724+
time.Sleep(delay)
705725
addr, _ := common.NewMixedcaseAddressFromString("0x0011223344556677889900112233445566778899")
706726
_, err := api.SignData(ctx, accounts.MimetypeTextPlain, *addr, hexutil.Encode([]byte("hello world")))
707727
expectApprove("signdata - text", err)
708728
}
709729
{ // Sign data test - plain text reject
710730
api.UI.ShowInfo("Please deny the next request for signing text")
731+
time.Sleep(delay)
711732
addr, _ := common.NewMixedcaseAddressFromString("0x0011223344556677889900112233445566778899")
712733
_, err := api.SignData(ctx, accounts.MimetypeTextPlain, *addr, hexutil.Encode([]byte("hello world")))
713734
expectDeny("signdata - text", err)
714735
}
715736
{ // Sign transaction
716737

717738
api.UI.ShowInfo("Please reject next transaction")
739+
time.Sleep(delay)
718740
data := hexutil.Bytes([]byte{})
719741
to := common.NewMixedcaseAddress(a)
720742
tx := core.SendTxArgs{
@@ -733,16 +755,19 @@ func testExternalUI(api *core.SignerAPI) {
733755
}
734756
{ // Listing
735757
api.UI.ShowInfo("Please reject listing-request")
758+
time.Sleep(delay)
736759
_, err := api.List(ctx)
737760
expectDeny("list", err)
738761
}
739762
{ // Import
740763
api.UI.ShowInfo("Please reject new account-request")
764+
time.Sleep(delay)
741765
_, err := api.New(ctx)
742766
expectDeny("newaccount", err)
743767
}
744768
{ // Metadata
745769
api.UI.ShowInfo("Please check if you see the Origin in next listing (approve or deny)")
770+
time.Sleep(delay)
746771
api.List(context.WithValue(ctx, "Origin", "origin.com"))
747772
expectResponse("metadata - origin", "Did you see origin (origin.com)? [yes/no] ", "yes")
748773
}

‎common/math/big.go‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,13 @@ const (
4242
// HexOrDecimal256 marshals big.Int as hex or decimal.
4343
type HexOrDecimal256 big.Int
4444

45+
// NewHexOrDecimal256 creates a new HexOrDecimal256
46+
func NewHexOrDecimal256(x int64) *HexOrDecimal256 {
47+
b := big.NewInt(x)
48+
h := HexOrDecimal256(*b)
49+
return &h
50+
}
51+
4552
// UnmarshalText implements encoding.TextUnmarshaler.
4653
func (i *HexOrDecimal256) UnmarshalText(input []byte) error {
4754
bigint, ok := ParseBig256(string(input))

‎signer/core/api.go‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -482,7 +482,6 @@ func (api *SignerAPI) SignTransaction(ctx context.Context, args SendTxArgs, meth
482482
return nil, err
483483
}
484484
}
485-
486485
req := SignTxRequest{
487486
Transaction: args,
488487
Meta: MetadataFromContext(ctx),

0 commit comments

Comments
 (0)