github.com/onflow/flow-go@v0.35.7-crescendo-preview.23-atree-inlining/model/encodable/keys_test.go (about) 1 package encodable 2 3 import ( 4 "crypto/rand" 5 "encoding/hex" 6 "encoding/json" 7 "fmt" 8 "testing" 9 10 "github.com/stretchr/testify/require" 11 12 "github.com/onflow/crypto" 13 ) 14 15 func isHexString(enc []byte) error { 16 str := string(enc) 17 if len(str) <= 2 { 18 if str == "\"\"" { 19 return nil 20 } 21 return fmt.Errorf("invalid hex: %v", str) 22 } 23 24 if str == "null" { 25 return nil 26 } 27 28 // remove the " 29 str = str[1 : len(str)-1] 30 _, err := hex.DecodeString(str) 31 return err 32 } 33 34 func TestIsHexString(t *testing.T) { 35 require.NoError(t, isHexString([]byte("abcd"))) 36 require.NoError(t, isHexString([]byte("\"\""))) 37 require.NoError(t, isHexString([]byte("null"))) 38 require.Error(t, isHexString([]byte("\""))) 39 require.Error(t, isHexString([]byte("QEVX="))) 40 } 41 42 func TestEncodableNetworkPubKey(t *testing.T) { 43 netw, err := crypto.GeneratePrivateKey(crypto.ECDSAP256, generateRandomSeed(t)) 44 require.NoError(t, err) 45 key := NetworkPubKey{netw.PublicKey()} 46 47 enc, err := json.Marshal(key) 48 require.NoError(t, err) 49 require.NotEmpty(t, enc) 50 51 var dec NetworkPubKey 52 err = json.Unmarshal(enc, &dec) 53 require.NoError(t, err) 54 require.True(t, key.Equals(dec.PublicKey)) 55 56 require.NoError(t, isHexString(enc)) 57 } 58 59 func TestEncodableNetworkPubKeyNil(t *testing.T) { 60 key := NetworkPubKey{} 61 62 enc, err := json.Marshal(key) 63 require.NoError(t, err) 64 require.Equal(t, "null", string(enc)) 65 66 var dec NetworkPubKey 67 err = json.Unmarshal(enc, &dec) 68 require.NoError(t, err) 69 require.Equal(t, key, dec) 70 71 require.NoError(t, isHexString(enc)) 72 } 73 74 func TestEncodableNetworkPrivKey(t *testing.T) { 75 netw, err := crypto.GeneratePrivateKey(crypto.ECDSAP256, generateRandomSeed(t)) 76 require.NoError(t, err) 77 key := NetworkPrivKey{netw} 78 79 enc, err := json.Marshal(key) 80 require.NoError(t, err) 81 require.NotEmpty(t, enc) 82 83 var dec NetworkPrivKey 84 err = json.Unmarshal(enc, &dec) 85 require.NoError(t, err) 86 require.True(t, key.Equals(dec.PrivateKey)) 87 88 require.NoError(t, isHexString(enc)) 89 } 90 91 func TestEncodableNetworkPrivKeyNil(t *testing.T) { 92 key := NetworkPrivKey{} 93 94 enc, err := json.Marshal(key) 95 require.NoError(t, err) 96 require.Equal(t, "null", string(enc)) 97 98 var dec NetworkPrivKey 99 err = json.Unmarshal(enc, &dec) 100 require.NoError(t, err) 101 require.Equal(t, key, dec) 102 103 require.NoError(t, isHexString(enc)) 104 } 105 106 func TestEncodableStakingPubKey(t *testing.T) { 107 stak, err := crypto.GeneratePrivateKey(crypto.BLSBLS12381, generateRandomSeed(t)) 108 require.NoError(t, err) 109 key := StakingPubKey{stak.PublicKey()} 110 111 enc, err := json.Marshal(key) 112 require.NoError(t, err) 113 require.NotEmpty(t, enc) 114 115 var dec StakingPubKey 116 err = json.Unmarshal(enc, &dec) 117 require.NoError(t, err) 118 require.True(t, key.Equals(dec.PublicKey)) 119 120 require.NoError(t, isHexString(enc)) 121 } 122 123 func TestEncodableStakingPubKeyNil(t *testing.T) { 124 key := StakingPubKey{} 125 126 enc, err := json.Marshal(key) 127 require.NoError(t, err) 128 require.Equal(t, "null", string(enc)) 129 130 var dec StakingPubKey 131 err = json.Unmarshal(enc, &dec) 132 require.NoError(t, err) 133 require.Equal(t, key, dec) 134 135 require.NoError(t, isHexString(enc)) 136 } 137 138 func TestEncodableStakingPrivKey(t *testing.T) { 139 stak, err := crypto.GeneratePrivateKey(crypto.BLSBLS12381, generateRandomSeed(t)) 140 require.NoError(t, err) 141 key := StakingPrivKey{stak} 142 143 enc, err := json.Marshal(key) 144 require.NoError(t, err) 145 require.NotEmpty(t, enc) 146 147 var dec StakingPrivKey 148 err = json.Unmarshal(enc, &dec) 149 require.NoError(t, err) 150 151 require.True(t, key.Equals(dec.PrivateKey), "encoded/decoded key equality check failed") 152 153 require.NoError(t, isHexString(enc)) 154 } 155 156 func TestEncodableStakingPrivKeyNil(t *testing.T) { 157 key := StakingPrivKey{} 158 159 enc, err := json.Marshal(key) 160 require.NoError(t, err) 161 require.Equal(t, "null", string(enc)) 162 163 var dec StakingPrivKey 164 err = json.Unmarshal(enc, &dec) 165 require.NoError(t, err) 166 require.Equal(t, key, dec) 167 168 require.NoError(t, isHexString(enc)) 169 } 170 171 func TestEncodableRandomBeaconPubKey(t *testing.T) { 172 randbeac, err := crypto.GeneratePrivateKey(crypto.BLSBLS12381, generateRandomSeed(t)) 173 require.NoError(t, err) 174 key := RandomBeaconPubKey{randbeac.PublicKey()} 175 176 enc, err := json.Marshal(key) 177 require.NoError(t, err) 178 require.NotEmpty(t, enc) 179 180 var dec RandomBeaconPubKey 181 err = json.Unmarshal(enc, &dec) 182 require.NoError(t, err) 183 require.True(t, key.Equals(dec.PublicKey)) 184 185 require.NoError(t, isHexString(enc)) 186 } 187 188 func TestEncodableRandomBeaconPubKeyNil(t *testing.T) { 189 key := RandomBeaconPubKey{} 190 191 enc, err := json.Marshal(key) 192 require.NoError(t, err) 193 require.Equal(t, "null", string(enc)) 194 195 var dec RandomBeaconPubKey 196 err = json.Unmarshal(enc, &dec) 197 require.NoError(t, err) 198 require.Equal(t, key, dec) 199 200 require.NoError(t, isHexString(enc)) 201 } 202 203 func TestEncodableRandomBeaconPrivKey(t *testing.T) { 204 randbeac, err := crypto.GeneratePrivateKey(crypto.BLSBLS12381, generateRandomSeed(t)) 205 require.NoError(t, err) 206 key := RandomBeaconPrivKey{randbeac} 207 208 enc, err := json.Marshal(key) 209 require.NoError(t, err) 210 require.NotEmpty(t, enc) 211 212 var dec RandomBeaconPrivKey 213 err = json.Unmarshal(enc, &dec) 214 require.NoError(t, err) 215 216 require.True(t, key.Equals(dec.PrivateKey), "encoded/decoded key equality check failed") 217 218 require.NoError(t, isHexString(enc)) 219 } 220 221 func TestEncodableRandomBeaconPrivKeyNil(t *testing.T) { 222 key := RandomBeaconPrivKey{} 223 224 enc, err := json.Marshal(key) 225 require.NoError(t, err) 226 require.Equal(t, "null", string(enc)) 227 228 var dec RandomBeaconPrivKey 229 err = json.Unmarshal(enc, &dec) 230 require.NoError(t, err) 231 require.Equal(t, key, dec) 232 233 require.NoError(t, isHexString(enc)) 234 } 235 236 func TestEncodableRandomBeaconPrivKeyMsgPack(t *testing.T) { 237 randbeac, err := crypto.GeneratePrivateKey(crypto.BLSBLS12381, generateRandomSeed(t)) 238 require.NoError(t, err) 239 key := RandomBeaconPubKey{randbeac.PublicKey()} 240 oldPubKey := key.PublicKey 241 242 b, err := key.MarshalMsgpack() 243 require.NoError(t, err) 244 245 t.Logf("bytes: %d\n", len(b)) 246 247 err = key.UnmarshalMsgpack(b) 248 require.NoError(t, err) 249 250 require.True(t, oldPubKey.Equals(key.PublicKey)) 251 } 252 253 func generateRandomSeed(t *testing.T) []byte { 254 seed := make([]byte, 48) 255 n, err := rand.Read(seed) 256 require.Nil(t, err) 257 require.Equal(t, n, 48) 258 return seed 259 }