github.com/stafiprotocol/go-substrate-rpc-client@v1.4.7/types/extrinsic_payload_test.go (about)

     1  // Go Substrate RPC Client (GSRPC) provides APIs and types around Polkadot and any Substrate-based chain RPC calls
     2  //
     3  // Copyright 2020 Stafi Protocol
     4  //
     5  // Licensed under the Apache License, Version 2.0 (the "License");
     6  // you may not use this file except in compliance with the License.
     7  // You may obtain a copy of the License at
     8  //
     9  //     http://www.apache.org/licenses/LICENSE-2.0
    10  //
    11  // Unless required by applicable law or agreed to in writing, software
    12  // distributed under the License is distributed on an "AS IS" BASIS,
    13  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    14  // See the License for the specific language governing permissions and
    15  // limitations under the License.
    16  
    17  package types_test
    18  
    19  import (
    20  	"testing"
    21  
    22  	"github.com/stafiprotocol/go-substrate-rpc-client/signature"
    23  	. "github.com/stafiprotocol/go-substrate-rpc-client/types"
    24  	"github.com/stretchr/testify/assert"
    25  )
    26  
    27  var examplaryExtrinsicPayload = ExtrinsicPayloadV4{ExtrinsicPayloadV3: ExtrinsicPayloadV3{Method: BytesBare{0x6, 0x0, 0xff, 0xd7, 0x56, 0x8e, 0x5f, 0xa, 0x7e, 0xda, 0x67, 0xa8, 0x26, 0x91, 0xff, 0x37, 0x9a, 0xc4, 0xbb, 0xa4, 0xf9, 0xc9, 0xb8, 0x59, 0xfe, 0x77, 0x9b, 0x5d, 0x46, 0x36, 0x3b, 0x61, 0xad, 0x2d, 0xb9, 0xe5, 0x6c}, Era: ExtrinsicEra{IsImmortalEra: false, IsMortalEra: true, AsMortalEra: MortalEra{First: 0x7, Second: 0x3}}, Nonce: NewUCompactFromUInt(0x1234), Tip: NewUCompactFromUInt(0x5678), SpecVersion: 0x7b, GenesisHash: Hash{0xdc, 0xd1, 0x34, 0x67, 0x1, 0xca, 0x83, 0x96, 0x49, 0x6e, 0x52, 0xaa, 0x27, 0x85, 0xb1, 0x74, 0x8d, 0xeb, 0x6d, 0xb0, 0x95, 0x51, 0xb7, 0x21, 0x59, 0xdc, 0xb3, 0xe0, 0x89, 0x91, 0x2, 0x5b}, BlockHash: Hash{0xde, 0x8f, 0x69, 0xee, 0xb5, 0xe0, 0x65, 0xe1, 0x8c, 0x69, 0x50, 0xff, 0x70, 0x8d, 0x7e, 0x55, 0x1f, 0x68, 0xdc, 0x9b, 0xf5, 0x9a, 0x7, 0xc5, 0x23, 0x67, 0xc0, 0x28, 0xf, 0x80, 0x5e, 0xc7}}, TransactionVersion: 1} //nolint:lll
    28  
    29  func TestExtrinsicPayload(t *testing.T) {
    30  	var era ExtrinsicEra
    31  	err := DecodeFromHexString("0x0703", &era)
    32  	assert.NoError(t, err)
    33  
    34  	p := ExtrinsicPayloadV4{
    35  		ExtrinsicPayloadV3: ExtrinsicPayloadV3{
    36  			Method: MustHexDecodeString(
    37  				"0x0600ffd7568e5f0a7eda67a82691ff379ac4bba4f9c9b859fe779b5d46363b61ad2db9e56c"),
    38  			Era:         era,
    39  			Nonce:       NewUCompactFromUInt(4660),
    40  			Tip:         NewUCompactFromUInt(22136),
    41  			SpecVersion: 123,
    42  			GenesisHash: NewHash(MustHexDecodeString("0xdcd1346701ca8396496e52aa2785b1748deb6db09551b72159dcb3e08991025b")),
    43  			BlockHash:   NewHash(MustHexDecodeString("0xde8f69eeb5e065e18c6950ff708d7e551f68dc9bf59a07c52367c0280f805ec7")),
    44  		},
    45  		TransactionVersion: 1,
    46  	}
    47  
    48  	assert.Equal(t, examplaryExtrinsicPayload, p)
    49  
    50  	enc, err := EncodeToHexString(examplaryExtrinsicPayload)
    51  	assert.NoError(t, err)
    52  
    53  	assert.Equal(t, "0x"+
    54  		"0600ffd7568e5f0a7eda67a82691ff379ac4bba4f9c9b859fe779b5d46363b61ad2db9e56c"+ // Method
    55  		"0703"+ // Era
    56  		"d148"+ // Nonce
    57  		"e2590100"+ // Tip
    58  		"7b000000"+ // Spec version
    59  		"01000000"+ // Tx version
    60  		"dcd1346701ca8396496e52aa2785b1748deb6db09551b72159dcb3e08991025b"+ // Genesis Hash
    61  		"de8f69eeb5e065e18c6950ff708d7e551f68dc9bf59a07c52367c0280f805ec7", // BlockHash
    62  		enc)
    63  
    64  	// b := bytes.NewBuffer(MustHexDecodeString())
    65  
    66  	var dec ExtrinsicPayloadV4
    67  	err = DecodeFromHexString(enc, &dec)
    68  	assert.Error(t, err)
    69  }
    70  
    71  func TestExtrinsicPayload_Sign(t *testing.T) {
    72  	sig, err := examplaryExtrinsicPayload.Sign(signature.TestKeyringPairAlice)
    73  	assert.NoError(t, err)
    74  
    75  	// verify sig
    76  	b, err := EncodeToBytes(examplaryExtrinsicPayload)
    77  	assert.NoError(t, err)
    78  	ok, err := signature.Verify(b, sig[:], HexEncodeToString(signature.TestKeyringPairAlice.PublicKey))
    79  	assert.NoError(t, err)
    80  	assert.True(t, ok)
    81  }