MCPcopy
hub / github.com/canopy-network/canopy / TestNewPublicKeyFromBytes

Function TestNewPublicKeyFromBytes

lib/crypto/key_test.go:65–130  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

63}
64
65func TestNewPublicKeyFromBytes(t *testing.T) {
66 // pre-generate a secp256k1
67 secp256k1Pk, err := NewSECP256K1PrivateKey()
68 require.NoError(t, err)
69 // pre-generate a secp256k1
70 ethSecp256k1, err := NewETHSECP256K1PrivateKey()
71 require.NoError(t, err)
72 // pre-generate a ED25519
73 ed25519Pk, err := NewEd25519PrivateKey()
74 require.NoError(t, err)
75 // pre-generate a BLS12381
76 blsPrivateKey, err := NewBLS12381PrivateKey()
77 require.NoError(t, err)
78 tests := []struct {
79 name string
80 bytes []byte
81 expected PublicKeyI
82 error string
83 }{
84 {
85 name: "not a recognized key",
86 bytes: []byte("abcd"),
87 error: "unrecognized public key format",
88 },
89 {
90 name: "eth_secp256k1 public key",
91 bytes: ethSecp256k1.PublicKey().Bytes(),
92 expected: ethSecp256k1.PublicKey(),
93 },
94 {
95 name: "eth_secp256k1 public key with a SEC1 prefix",
96 bytes: ethSecp256k1.PublicKey().(*ETHSECP256K1PublicKey).BytesWithPrefix(),
97 expected: ethSecp256k1.PublicKey(),
98 },
99 {
100 name: "secp256k1 public key",
101 bytes: secp256k1Pk.PublicKey().Bytes(),
102 expected: secp256k1Pk.PublicKey(),
103 },
104 {
105 name: "ed25519 public key",
106 bytes: ed25519Pk.PublicKey().Bytes(),
107 expected: ed25519Pk.PublicKey(),
108 },
109 {
110 name: "bls12381 public key",
111 bytes: blsPrivateKey.PublicKey().Bytes(),
112 expected: blsPrivateKey.PublicKey(),
113 },
114 }
115 for _, test := range tests {
116 t.Run(test.name, func(t *testing.T) {
117 // execute the function call
118 got, e := NewPublicKeyFromBytes(test.bytes)
119 // check if an error is expected or not
120 require.Equal(t, test.error != "", e != nil)
121 // check the error
122 if e != nil {

Callers

nothing calls this directly

Calls 13

PublicKeyMethod · 0.95
PublicKeyMethod · 0.95
PublicKeyMethod · 0.95
NewSECP256K1PrivateKeyFunction · 0.85
NewEd25519PrivateKeyFunction · 0.85
NewBLS12381PrivateKeyFunction · 0.85
NewPublicKeyFromBytesFunction · 0.85
BytesWithPrefixMethod · 0.80
RunMethod · 0.80
EqualMethod · 0.80
BytesMethod · 0.65

Tested by

no test coverage detected