github.com/stafiprotocol/go-substrate-rpc-client@v1.4.7/types/address_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  	"encoding/binary"
    21  	"testing"
    22  
    23  	. "github.com/stafiprotocol/go-substrate-rpc-client/types"
    24  )
    25  
    26  func TestAddress_EncodeDecode(t *testing.T) {
    27  	assertRoundtrip(t, NewAddressFromAccountID([]byte{128, 42}))
    28  	assertRoundtrip(t, NewAddressFromAccountIndex(421))
    29  }
    30  
    31  func TestAddress_Encode(t *testing.T) {
    32  	assertEncode(t, []encodingAssert{
    33  		{NewAddressFromAccountID([]byte{
    34  			1, 2, 3, 4, 5, 6, 7, 8, 1, 2, 3, 4, 5, 6, 7, 8,
    35  			1, 2, 3, 4, 5, 6, 7, 8, 1, 2, 3, 4, 5, 6, 7, 8,
    36  		}), []byte{
    37  			255,
    38  			1, 2, 3, 4, 5, 6, 7, 8, 1, 2, 3, 4, 5, 6, 7, 8,
    39  			1, 2, 3, 4, 5, 6, 7, 8, 1, 2, 3, 4, 5, 6, 7, 8,
    40  		}},
    41  		{NewAddressFromAccountIndex(binary.BigEndian.Uint32([]byte{
    42  			17, 18, 19, 20,
    43  		})), []byte{
    44  			253, 20, 19, 18, 17, // order is reversed because scale uses little endian
    45  		}},
    46  		{NewAddressFromAccountIndex(uint32(binary.BigEndian.Uint16([]byte{
    47  			21, 22,
    48  		}))), []byte{
    49  			252, 22, 21, // order is reversed because scale uses little endian
    50  		}},
    51  		{NewAddressFromAccountIndex(uint32(23)), []byte{23}},
    52  	})
    53  }
    54  
    55  func TestAddress_EncodeWithOptions(t *testing.T) {
    56  	SetSerDeOptions(SerDeOptions{NoPalletIndices: true})
    57  	defer SetSerDeOptions(SerDeOptions{NoPalletIndices: false})
    58  	assertEncode(t, []encodingAssert{
    59  		{NewAddressFromAccountID([]byte{
    60  			1, 2, 3, 4, 5, 6, 7, 8, 1, 2, 3, 4, 5, 6, 7, 8,
    61  			1, 2, 3, 4, 5, 6, 7, 8, 1, 2, 3, 4, 5, 6, 7, 8,
    62  		}), []byte{
    63  			1, 2, 3, 4, 5, 6, 7, 8, 1, 2, 3, 4, 5, 6, 7, 8,
    64  			1, 2, 3, 4, 5, 6, 7, 8, 1, 2, 3, 4, 5, 6, 7, 8,
    65  		}},
    66  		{NewAddressFromAccountIndex(binary.BigEndian.Uint32([]byte{
    67  			17, 18, 19, 20,
    68  		})), []byte{
    69  			253, 20, 19, 18, 17, // order is reversed because scale uses little endian
    70  		}},
    71  	})
    72  }
    73  
    74  func TestAddress_Decode(t *testing.T) {
    75  	assertDecode(t, []decodingAssert{
    76  		{[]byte{
    77  			255,
    78  			1, 2, 3, 4, 5, 6, 7, 8, 1, 2, 3, 4, 5, 6, 7, 8,
    79  			1, 2, 3, 4, 5, 6, 7, 8, 1, 2, 3, 4, 5, 6, 7, 8,
    80  		}, NewAddressFromAccountID([]byte{
    81  			1, 2, 3, 4, 5, 6, 7, 8, 1, 2, 3, 4, 5, 6, 7, 8,
    82  			1, 2, 3, 4, 5, 6, 7, 8, 1, 2, 3, 4, 5, 6, 7, 8,
    83  		})},
    84  		{[]byte{
    85  			253, 20, 19, 18, 17, // order is reversed because scale uses little endian
    86  		}, NewAddressFromAccountIndex(binary.BigEndian.Uint32([]byte{
    87  			17, 18, 19, 20,
    88  		}))},
    89  		{[]byte{
    90  			252, 22, 21, // order is reversed because scale uses little endian
    91  		}, NewAddressFromAccountIndex(uint32(binary.BigEndian.Uint16([]byte{
    92  			21, 22,
    93  		})))},
    94  		{[]byte{23}, NewAddressFromAccountIndex(uint32(23))},
    95  	})
    96  }
    97  
    98  func TestAddress_DecodeWithOptions(t *testing.T) {
    99  	SetSerDeOptions(SerDeOptions{NoPalletIndices: true})
   100  	defer SetSerDeOptions(SerDeOptions{NoPalletIndices: false})
   101  	assertDecode(t, []decodingAssert{
   102  		{[]byte{
   103  			1, 2, 3, 4, 5, 6, 7, 8, 1, 2, 3, 4, 5, 6, 7, 8,
   104  			1, 2, 3, 4, 5, 6, 7, 8, 1, 2, 3, 4, 5, 6, 7, 8,
   105  		}, NewAddressFromAccountID([]byte{
   106  			1, 2, 3, 4, 5, 6, 7, 8, 1, 2, 3, 4, 5, 6, 7, 8,
   107  			1, 2, 3, 4, 5, 6, 7, 8, 1, 2, 3, 4, 5, 6, 7, 8,
   108  		})},
   109  		{[]byte{
   110  			254, 2, 3, 4, 5, 6, 7, 8, 1, 2, 3, 4, 5, 6, 7, 8,
   111  			1, 2, 3, 4, 5, 6, 7, 8, 1, 2, 3, 4, 5, 6, 7, 8,
   112  		}, NewAddressFromAccountID([]byte{
   113  			254, 2, 3, 4, 5, 6, 7, 8, 1, 2, 3, 4, 5, 6, 7, 8,
   114  			1, 2, 3, 4, 5, 6, 7, 8, 1, 2, 3, 4, 5, 6, 7, 8,
   115  		})},
   116  	})
   117  }