@@ -3,13 +3,16 @@ package keystore_test
33import (
44 "context"
55 "fmt"
6+ "math/big"
67 "sort"
78 "sync"
89 "testing"
910
1011 "github.com/stretchr/testify/assert"
1112 "github.com/stretchr/testify/require"
1213
14+ gethcrypto "github.com/ethereum/go-ethereum/crypto"
15+
1316 "github.com/smartcontractkit/chainlink-common/keystore"
1417)
1518
@@ -258,7 +261,16 @@ func TestKeystore_ExportImport(t *testing.T) {
258261 key1ks1 , err := ks1 .GetKeys (t .Context (), keystore.GetKeysRequest {KeyNames : []string {"key1" }})
259262 require .NoError (t , err )
260263 key1ks2 , err := ks2 .GetKeys (t .Context (), keystore.GetKeysRequest {KeyNames : []string {"key1" }})
261- require .Equal (t , key1ks1 , key1ks2 )
264+ require .NoError (t , err )
265+ // Test equality of the keys except of the CreatedAt field.
266+ require .Len (t , key1ks1 .Keys , 1 )
267+ require .Len (t , key1ks2 .Keys , 1 )
268+ key1ks1Info := key1ks1 .Keys [0 ].KeyInfo
269+ key1ks2Info := key1ks2 .Keys [0 ].KeyInfo
270+ require .Equal (t , key1ks1Info .Name , key1ks2Info .Name )
271+ require .Equal (t , key1ks1Info .PublicKey , key1ks2Info .PublicKey )
272+ require .Equal (t , key1ks1Info .KeyType , key1ks2Info .KeyType )
273+ require .Equal (t , key1ks1Info .Metadata , key1ks2Info .Metadata )
262274
263275 testData := []byte ("hello world" )
264276 signature , err := ks2 .Sign (t .Context (), keystore.SignRequest {
@@ -411,3 +423,20 @@ func TestKeystore_RenameKey(t *testing.T) {
411423 require .EqualError (t , err , "key not found: key1" )
412424 })
413425}
426+
427+ func TestECDSA_Serialization_WithPadding (t * testing.T ) {
428+ // This test ensures that ECDSA private keys that serialize to less than 32 bytes
429+ // are correctly padded with leading zeros during serialization and deserialization.
430+ // This is important for compatibility with Ethereum's crypto library which expects
431+ // 32-byte private keys.
432+
433+ // The example key has been found randomly such that it has 2 leading zero bytes when serialized.
434+ key , ok := big .NewInt (0 ).SetString ("57269542458293433845411819226400606954116463824740942170224417652371448" , 10 )
435+ require .True (t , ok )
436+ privateKeyBytes := make ([]byte , 32 )
437+ key .FillBytes (privateKeyBytes )
438+ require .Equal (t , []byte {0 , 0 , 8 , 76 , 62 , 209 , 247 , 104 , 97 , 108 , 141 , 217 , 255 , 150 , 114 , 196 , 223 , 66 , 254 , 101 , 209 , 14 , 233 , 174 , 149 , 89 , 207 , 141 , 2 , 188 , 111 , 248 }, privateKeyBytes )
439+ deserializedKey , err := gethcrypto .ToECDSA (privateKeyBytes )
440+ require .NoError (t , err )
441+ require .Equal (t , key , deserializedKey .D )
442+ }
0 commit comments