github.com/iotexproject/iotex-core@v1.14.1-rc1/action/consignment_transfer_test.go (about)

     1  // Copyright (c) 2020 IoTeX Foundation
     2  // This source code is provided 'as is' and no warranties are given as to title or non-infringement, merchantability
     3  // or fitness for purpose and, to the extent permitted by law, all liability for your use of the code is disclaimed.
     4  // This source code is governed by Apache License 2.0 that can be found in the LICENSE file.
     5  
     6  package action
     7  
     8  import (
     9  	"encoding/hex"
    10  	"encoding/json"
    11  	"math/rand"
    12  	"testing"
    13  
    14  	"github.com/stretchr/testify/require"
    15  )
    16  
    17  var (
    18  	_sigTests = []struct {
    19  		signer    string
    20  		recipient string
    21  		msg       string
    22  		hash      string
    23  		sig       string
    24  	}{
    25  		// sample sig generated by keystore file at https://etherscan.io/verifySig/2059
    26  		{
    27  			"53fbc28faf9a52dfe5f591948a23189e900381b5",
    28  			"io120au9ra0nffdle04jx2g5gccn6gq8qd4fy03l4",
    29  			"{\"nonce\":\"136\",\"address\":\"io14s0vgnj0pjnazu4hsqlksdk7slah9vcfscn9ks\"}",
    30  			"f93a97fae37fdadab6d49b74e3f3e4bee707ea2f007e08007bcc356cb283665b",
    31  			"5595906a47dfc107a78cc48b500f89ab2dec545ba86578295aed4a260ce9a98b335924e86f683832e313f1a5dda7826d9b59caf40dd22ce92716420a367dfaec1c",
    32  		},
    33  		// sample sig created by Ledger Nano S at https://etherscan.io/verifySig/2060
    34  		{
    35  			"35cb41ec685a30bf5ebc3f935aebb1a8391e5fd6",
    36  			"io1xh95rmrgtgct7h4u87f446a34qu3uh7kzqkx2a",
    37  			"{\"nonce\":\"136\",\"address\":\"io14s0vgnj0pjnazu4hsqlksdk7slah9vcfscn9ks\"}",
    38  			"f93a97fae37fdadab6d49b74e3f3e4bee707ea2f007e08007bcc356cb283665b",
    39  			"e28fb729d1f8b841d36c2688571886a077e1f1529b239aa02d4ae1da84cdf5a37e7656e13d374d4fdf3010e75bcd3629a36765e941f2e427c8c4529c1f713caf01",
    40  		},
    41  		// sample sig https://etherscan.io/verifySig/2080
    42  		{
    43  			"ac1ec44e4f0ca7d172b7803f6836de87fb72b309",
    44  			"io14s0vgnj0pjnazu4hsqlksdk7slah9vcfscn9ks",
    45  			"{\"bucket\":47,\"nonce\":136,\"recipient\":\"io14s0vgnj0pjnazu4hsqlksdk7slah9vcfscn9ks\",\"reclaim\":\"This is to certify I am transferring the ownership of said bucket to said recipient on IoTeX blockchain\"}",
    46  			"baf289f37b913e736ff4e31c6f4b021c242b88416b7c528738766206e42735cc",
    47  			"0de7d21eb2fe2a81529e0927803a1ed918d002f4502f68aed913d941d88b3d454917a462755d241baad44199fb53c9126028ccbeeab0bd74eee30849aa0e1b391b",
    48  		},
    49  	}
    50  )
    51  
    52  func TestVerifyEccSig(t *testing.T) {
    53  	r := require.New(t)
    54  
    55  	for _, v := range _sigTests {
    56  		sig, _ := hex.DecodeString(v.sig)
    57  		pk, err := RecoverPubkeyFromEccSig("Ethereum", []byte(v.msg), sig)
    58  		r.NoError(err)
    59  		r.Equal(v.signer, hex.EncodeToString(pk.Hash()))
    60  		h, _ := hex.DecodeString(v.hash)
    61  		r.True(pk.Verify(h, sig))
    62  	}
    63  
    64  	// test with modified signature
    65  	for _, v := range _sigTests {
    66  		sig, _ := hex.DecodeString(v.sig)
    67  		sig[rand.Intn(len(sig))]++
    68  		pk, err := RecoverPubkeyFromEccSig("Ethereum", []byte(v.msg), sig)
    69  		if err == nil {
    70  			r.NotEqual(v.signer, hex.EncodeToString(pk.Hash()))
    71  		}
    72  	}
    73  }
    74  
    75  func TestConsignmentTransfer(t *testing.T) {
    76  	r := require.New(t)
    77  
    78  	// generate payload from tests
    79  	v := _sigTests[2]
    80  	_, err := NewConsignJSON("Trezor", v.recipient, v.sig, 47, 136)
    81  	r.Equal(ErrNotSupported, err)
    82  	b, err := NewConsignJSON("Ethereum", v.recipient, v.sig, 47, 136)
    83  	r.NoError(err)
    84  
    85  	// process the payload as a consignment transfer
    86  	con, err := NewConsignment(b)
    87  	r.NoError(err)
    88  	r.Equal(v.signer, hex.EncodeToString(con.Transferor().Bytes()))
    89  	r.Equal(v.recipient, con.Transferee().String())
    90  	r.EqualValues(47, con.AssetID())
    91  	r.EqualValues(136, con.TransfereeNonce())
    92  
    93  	// test unsupported signature type
    94  	c := &ConsignJSON{
    95  		Type: "Trezor",
    96  		Msg:  v.msg,
    97  		Sig:  v.sig,
    98  	}
    99  	b, err = json.Marshal(c)
   100  	r.NoError(err)
   101  	con, err = NewConsignment(b)
   102  	r.Equal(ErrNotSupported, err)
   103  	r.Nil(con)
   104  }