Skip to content

Commit

Permalink
Wallet add unit test testing SignContext expectations
Browse files Browse the repository at this point in the history
  • Loading branch information
marino39 authored and attente committed Aug 8, 2023
1 parent de8a111 commit 62007cb
Show file tree
Hide file tree
Showing 2 changed files with 153 additions and 3 deletions.
25 changes: 22 additions & 3 deletions relayer/rpc_relayer.go
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ func (r *RpcRelayer) Wait(ctx context.Context, metaTxnID sequence.MetaTxnID, opt
}

func (r *RpcRelayer) protoConfig(ctx context.Context, config core.WalletConfig, walletAddress common.Address) (*proto.WalletConfig, error) {
if walletConfigV1 := config.(*v1.WalletConfig); walletConfigV1 != nil {
if walletConfigV1, ok := config.(*v1.WalletConfig); ok {
var signers []*proto.WalletSigner
for _, signer := range walletConfigV1.Signers_ {
signers = append(signers, &proto.WalletSigner{
Expand All @@ -207,8 +207,27 @@ func (r *RpcRelayer) protoConfig(ctx context.Context, config core.WalletConfig,
Threshold: walletConfigV1.Threshold_,
ChainId: &chainID,
}, nil
} else if walletConfigV2 := config.(*v2.WalletConfig); walletConfigV2 != nil {
// todo: implement v2
} else if walletConfigV2, ok := config.(*v2.WalletConfig); ok {
var signers []*proto.WalletSigner
for address, weight := range walletConfigV2.Signers() {
signers = append(signers, &proto.WalletSigner{
Address: address.Hex(),
Weight: uint8(weight),
})
}

result, err := r.provider.ChainID(ctx)
if err != nil {
return nil, err
}
chainID := result.Uint64()

return &proto.WalletConfig{
Address: walletAddress.Hex(),
Signers: signers,
Threshold: walletConfigV2.Threshold_,
ChainId: &chainID,
}, nil
}

return nil, fmt.Errorf("relayer: unknown wallet config version")
Expand Down
131 changes: 131 additions & 0 deletions wallet_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"github.com/0xsequence/go-sequence/relayer"
"github.com/0xsequence/go-sequence/signing_service"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
"github.com/stretchr/testify/require"
)

Expand Down Expand Up @@ -516,3 +517,133 @@ func TestWalletSignDigestWithNotFirstSigner(t *testing.T) {
require.NoError(t, err)
require.NotNil(t, sig)
}

type MockSignerDigestSigner struct {
mock.Mock
}

func (m *MockSignerDigestSigner) Address() common.Address {
args := m.Called()
return args.Get(0).(common.Address)
}

func (m *MockSignerDigestSigner) SignDigest(ctx context.Context, digest common.Hash, optChainID ...*big.Int) ([]byte, core.Signature[core.WalletConfig], error) {
args := m.Called(ctx, digest, optChainID)

var sig []byte
if args.Get(0) != nil {
sig = args.Get(0).([]byte)
}

var sigTyped core.Signature[core.WalletConfig]
if args.Get(1) != nil {
sigTyped = args.Get(1).(core.Signature[core.WalletConfig])
}

var err error
if args.Get(2) != nil {
err = args.Get(2).(error)
}

return sig, sigTyped, err
}

var _ sequence.SignerDigestSigner = &MockSignerDigestSigner{}

func TestWalletSignMessageCheckSignContext(t *testing.T) {
eoa1, err := ethwallet.NewWalletFromRandomEntropy()
assert.NoError(t, err)

mockSigner := &MockSignerDigestSigner{}

walletConfig := &v2.WalletConfig{
Threshold_: 3,
Tree: &v2.WalletConfigTreeAddressLeaf{
Weight: 3, Address: eoa1.Address(),
},
}

wallet, err := sequence.NewWallet(sequence.WalletOptions[*v2.WalletConfig]{
Config: walletConfig,
}, mockSigner)
require.NoError(t, err)

wallet.SetChainID(big.NewInt(1))

message := []byte("hello world")

mockSigner.On("Address").Return(eoa1.Address()).Maybe()
mockSigner.On("SignDigest", mock.Anything, mock.Anything, mock.Anything).Run(func(args mock.Arguments) {
ctx := args.Get(0).(context.Context)
signContext := signing_service.SignContextFromContext(ctx)
assert.NotNil(t, signContext)
assert.Equal(t, signContext.ChainId, uint64(1))
assert.Equal(t, *signContext.WalletAddress, wallet.Address().Hex())
assert.Equal(t, signContext.SigneeWalletAddress, wallet.Address().Hex())
assert.Equal(t, *signContext.Message, string(message))
}).Return([]byte{0x01}, nil, nil).Once()

_, _, _ = wallet.SignMessage(context.Background(), message)

mockSigner.AssertExpectations(t)
}

func TestWalletSignTransactionCheckSignContext(t *testing.T) {
eoa1, err := ethwallet.NewWalletFromRandomEntropy()
assert.NoError(t, err)

mockSigner := &MockSignerDigestSigner{}

walletConfig := &v2.WalletConfig{
Threshold_: 3,
Tree: &v2.WalletConfigTreeAddressLeaf{
Weight: 3, Address: eoa1.Address(),
},
}

wallet, err := sequence.NewWallet(sequence.WalletOptions[*v2.WalletConfig]{
Config: walletConfig,
}, mockSigner)
require.NoError(t, err)

wallet.SetChainID(testChain.ChainID())

rel, err := relayer.NewLocalRelayer(testChain.MustWallet(1), nil)
require.NoError(t, err)

err = wallet.SetRelayer(rel)
require.NoError(t, err)

txn := sequence.Transactions{
&sequence.Transaction{
DelegateCall: false,
RevertOnError: true,
GasLimit: big.NewInt(100000000),
To: common.Address{},
Value: big.NewInt(1),
Data: nil,
Transactions: nil,
Nonce: big.NewInt(1),
Signature: nil,
},
}

rawTx, err := txn.EncodeRaw()
require.NoError(t, err)

mockSigner.On("Address").Return(eoa1.Address()).Maybe()
mockSigner.On("SignDigest", mock.Anything, mock.Anything, mock.Anything).Run(func(args mock.Arguments) {
ctx := args.Get(0).(context.Context)
signContext := signing_service.SignContextFromContext(ctx)
assert.NotNil(t, signContext)
assert.Equal(t, signContext.ChainId, uint64(1337))
assert.Equal(t, *signContext.WalletAddress, wallet.Address().Hex())
assert.Equal(t, signContext.SigneeWalletAddress, wallet.Address().Hex())
assert.Equal(t, *signContext.Transactions, ethcoder.HexEncode(rawTx))
assert.Equal(t, *signContext.Nonce, uint64(1))
}).Return([]byte{0x01}, nil, nil).Once()

_, _ = wallet.SignTransactions(context.Background(), txn)

mockSigner.AssertExpectations(t)
}

0 comments on commit 62007cb

Please sign in to comment.