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

     1  package subkey
     2  
     3  import (
     4  	"reflect"
     5  	"strings"
     6  	"testing"
     7  )
     8  
     9  func TestAddressInfo(t *testing.T) {
    10  	tests := []struct {
    11  		name    string
    12  		address string
    13  		prefix  uint16
    14  		pub     string
    15  	}{
    16  		{"TestAddressInfo_Alice_Substrate", "5GrwvaEF5zXb26Fz9rcQpDWS57CtERHpNehXCPcNoHGKutQY", 42, "0xd43593c715fdd31c61141abd04a99fd6822c8558854ccde39a5684e7a56da27d"},
    17  		{"TestAddressInfo_Alice_Heiko", "hJKzPoi3MQnSLvbShxeDmzbtHncrMXe5zwS3Wa36P6kXeNpcv", 110, "0xd43593c715fdd31c61141abd04a99fd6822c8558854ccde39a5684e7a56da27d"},
    18  		{"TestAddressInfo_Alice_Contextfree", "a7SvTrjvshEMePMEZpEkYMekuZMPpDwMNqfUx8N8ScEEQYfM8", 11820, "0xd43593c715fdd31c61141abd04a99fd6822c8558854ccde39a5684e7a56da27d"},
    19  	}
    20  	for _, tt := range tests {
    21  		t.Run(tt.name, func(t *testing.T) {
    22  			prefix, pubbz, err := SS58Decode(tt.address)
    23  			if err != nil {
    24  				t.Errorf("Decoding %v failed with %v", tt.address, err)
    25  			}
    26  			if !reflect.DeepEqual(prefix, tt.prefix) {
    27  				t.Errorf("ss58.Decode() prefix = %v, want %v", prefix, tt.prefix)
    28  			}
    29  			reencoded := SS58Encode(pubbz, prefix)
    30  			if !reflect.DeepEqual(reencoded, tt.address) {
    31  				t.Errorf("Address did not roundtrip: Started with %v and ended with %v", tt.address, reencoded)
    32  			}
    33  			hexpub := EncodeHex(pubbz)
    34  			if !reflect.DeepEqual(hexpub, tt.pub) {
    35  				t.Errorf("ss58.Decode() pubkey = %v, want %v", hexpub, tt.pub)
    36  			}
    37  
    38  			decoded, _ := DecodeHex(tt.pub)
    39  			if !reflect.DeepEqual(pubbz, decoded) {
    40  				t.Errorf("DecodeHexPubKey()= %v, want %v", decoded, pubbz)
    41  			}
    42  		})
    43  	}
    44  
    45  	// Test bad checksum
    46  	_, _, err := SS58Decode("a8SvTrjvshEMePMEZpEkYMekuZMPpDwMNqfUx8N8ScEEQYfM8")
    47  	if err == nil || !strings.Contains(err.Error(), "checksum mismatch") {
    48  		t.Errorf("Expected checksum mismatch but got '%v'", err)
    49  	}
    50  }