github.com/stafiprotocol/go-substrate-rpc-client@v1.4.7/signature/signature_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 signature_test
    18  
    19  import (
    20  	"crypto/rand"
    21  	"testing"
    22  
    23  	"github.com/stafiprotocol/go-substrate-rpc-client/signature"
    24  	"github.com/stafiprotocol/go-substrate-rpc-client/types"
    25  	"github.com/stretchr/testify/assert"
    26  )
    27  
    28  var testSecretPhrase = "little orbit comfort eyebrow talk pink flame ridge bring milk equip blood"
    29  var testSecretSeed = "0x167d9a020688544ea246b056799d6a771e97c9da057e4d0b87024537f99177bc"
    30  var testPubKey = "0xdc64bef918ddda3126a39a11113767741ddfdf91399f055e1d963f2ae1ec2535"
    31  var testAddressSS58 = "5H3gKVQU7DfNFfNGkgTrD7p715jjg7QXtat8X3UxiSyw7APW"
    32  var testKusamaAddressSS58 = "HZHyokLjagJ1KBiXPGu75B79g1yUnDiLxisuhkvCFCRrWBk"
    33  var defaultNetWork = uint16(42)
    34  
    35  func TestKeyRingPairFromSecretPhrase(t *testing.T) {
    36  	p, err := signature.KeyringPairFromSecret(testSecretPhrase, defaultNetWork)
    37  	assert.NoError(t, err)
    38  
    39  	assert.Equal(t, signature.KeyringPair{
    40  		URI:       testSecretPhrase,
    41  		Address:   testAddressSS58,
    42  		PublicKey: types.MustHexDecodeString(testPubKey),
    43  	}, p)
    44  }
    45  
    46  func TestKeyringPairFromSecretSeed(t *testing.T) {
    47  	p, err := signature.KeyringPairFromSecret(testSecretSeed, defaultNetWork)
    48  	assert.NoError(t, err)
    49  
    50  	assert.Equal(t, signature.KeyringPair{
    51  		URI:       testSecretSeed,
    52  		Address:   testAddressSS58,
    53  		PublicKey: types.MustHexDecodeString(testPubKey),
    54  	}, p)
    55  }
    56  
    57  func TestKeyringPairFromSecretSeedAndNetwork(t *testing.T) {
    58  	p, err := signature.KeyringPairFromSecret(testSecretSeed, 2)
    59  	assert.NoError(t, err)
    60  
    61  	assert.Equal(t, signature.KeyringPair{
    62  		URI:       testSecretSeed,
    63  		Address:   testKusamaAddressSS58,
    64  		PublicKey: types.MustHexDecodeString(testPubKey),
    65  	}, p)
    66  }
    67  
    68  func TestSignAndVerify(t *testing.T) {
    69  	data := []byte("hello!")
    70  
    71  	sig, err := signature.Sign(data, signature.TestKeyringPairAlice.URI)
    72  	assert.NoError(t, err)
    73  
    74  	ok, err := signature.Verify(data, sig, signature.TestKeyringPairAlice.URI)
    75  	assert.NoError(t, err)
    76  
    77  	assert.True(t, ok)
    78  }
    79  
    80  func TestSignAndVerifyLong(t *testing.T) {
    81  	data := make([]byte, 258)
    82  	_, err := rand.Read(data)
    83  	assert.NoError(t, err)
    84  
    85  	sig, err := signature.Sign(data, signature.TestKeyringPairAlice.URI)
    86  	assert.NoError(t, err)
    87  
    88  	ok, err := signature.Verify(data, sig, signature.TestKeyringPairAlice.URI)
    89  	assert.NoError(t, err)
    90  
    91  	assert.True(t, ok)
    92  }