github.com/stafiprotocol/go-substrate-rpc-client@v1.4.7/sr25519/sr25519_test.go (about) 1 package sr25519 2 3 import ( 4 "testing" 5 6 "github.com/stafiprotocol/go-substrate-rpc-client/subkey" 7 "github.com/stretchr/testify/assert" 8 ) 9 10 func fromHex(t *testing.T, hex string) []byte { 11 bytes, success := subkey.DecodeHex(hex) 12 assert.True(t, success) 13 return bytes 14 } 15 16 func TestFromPublicKeyVerifyGood(t *testing.T) { 17 addr := "5GrwvaEF5zXb26Fz9rcQpDWS57CtERHpNehXCPcNoHGKutQY" 18 msg := fromHex(t, "0xDEADBEEF") 19 sig := fromHex(t, "dc7cf771e1989a5c3cddca30ec5efaeff9a5c14a36c3c032510019e7144e0375f9207ef6745390ca3dc76b307b26f60125c942e2b7fb23100cc79402a12dde8b") 20 21 network, pubkeyBytes, err := subkey.SS58Decode(addr) 22 assert.NoError(t, err) 23 pubkey, err := Scheme{}.FromPublicKey(pubkeyBytes) 24 assert.NoError(t, err) 25 assert.Equal(t, pubkey.SS58Address(network), addr) 26 assert.True(t, pubkey.Verify(msg, sig)) 27 } 28 29 func TestVerifyBad(t *testing.T) { 30 addr := "5GrwvaEF5zXb26Fz9rcQpDWS57CtERHpNehXCPcNoHGKutQY" 31 badAddr := "5FHneW46xGXgs5mUiveU4sbTyGBzmstUspZC92UhjJM694ty" 32 msg := fromHex(t, "0xDEADBEEF") 33 badMsg := fromHex(t, "0xBADDBEEF") 34 sig := fromHex(t, "dc7cf771e1989a5c3cddca30ec5efaeff9a5c14a36c3c032510019e7144e0375f9207ef6745390ca3dc76b307b26f60125c942e2b7fb23100cc79402a12dde8b") 35 badSig := fromHex(t, "0c7cf771e1989a5c3cddca30ec5efaeff9a5c14a36c3c032510019e7144e0375f9207ef6745390ca3dc76b307b26f60125c942e2b7fb23100cc79402a12dde8b") 36 37 _, pubkeyBytes, err := subkey.SS58Decode(addr) 38 assert.NoError(t, err) 39 pubkey, err := Scheme{}.FromPublicKey(pubkeyBytes) 40 assert.NoError(t, err) 41 assert.True(t, pubkey.Verify(msg, sig)) 42 assert.False(t, pubkey.Verify(badMsg, sig)) 43 assert.False(t, pubkey.Verify(msg, badSig)) 44 assert.False(t, pubkey.Verify(badMsg, badSig)) 45 46 _, badPubkeyBytes, err := subkey.SS58Decode(badAddr) 47 assert.NoError(t, err) 48 badPubkey, err := Scheme{}.FromPublicKey(badPubkeyBytes) 49 assert.NoError(t, err) 50 assert.False(t, badPubkey.Verify(msg, sig)) 51 }