@cryptotaxi247 / kubo / commits / 67e1a173f

feat: adds secp256k1 keypair type to key gen command, adds test cases

imthe1 committed Apr 20, 2023 at 20:54 UTC 67e1a173fcde1b7c4b09464184aea8ef86bedab2
5 files changed +65 -3
core/commands/keystore.go
+3 -3
@@ -83,7 +83,7 @@ var keyGenCmd = &cmds.Command{
83 Tagline: "Create a new keypair",
84 },
85 Options: []cmds.Option{
86 - cmds.StringOption(keyStoreTypeOptionName, "t", "type of the key to create: rsa, ed25519").WithDefault(keyStoreAlgorithmDefault),
86 + cmds.StringOption(keyStoreTypeOptionName, "t", "type of the key to create: rsa, ed25519, secp256k1").WithDefault(keyStoreAlgorithmDefault),
87 cmds.IntOption(keyStoreSizeOptionName, "s", "size of the key to generate"),
88 ke.OptionIPNSBase,
89 },
@@ -398,7 +398,7 @@ The PEM format allows for key generation outside of the IPFS node:
398 allowAnyKeyType, _ := req.Options[keyAllowAnyTypeOptionName].(bool)
399 if !allowAnyKeyType {
400 switch t := sk.(type) {
401 - case *crypto.RsaPrivateKey, *crypto.Ed25519PrivateKey:
401 + case *crypto.RsaPrivateKey, *crypto.Ed25519PrivateKey, *crypto.Secp256k1PrivateKey:
402 default:
403 return fmt.Errorf("key type %T is not allowed to be imported, only RSA or Ed25519;"+
404 " use flag --%s if you are sure of what you're doing",
@@ -604,7 +604,7 @@ environment variable:
604 Arguments: []cmds.Argument{},
605 Options: []cmds.Option{
606 cmds.StringOption(oldKeyOptionName, "o", "Keystore name to use for backing up your existing identity"),
607 - cmds.StringOption(keyStoreTypeOptionName, "t", "type of the key to create: rsa, ed25519").WithDefault(keyStoreAlgorithmDefault),
607 + cmds.StringOption(keyStoreTypeOptionName, "t", "type of the key to create: rsa, ed25519, secp256k1").WithDefault(keyStoreAlgorithmDefault),
608 cmds.IntOption(keyStoreSizeOptionName, "s", "size of the key to generate"),
609 },
610 NoRemote: true,
core/coreapi/key.go
+8
@@ -82,6 +82,14 @@ func (api *KeyAPI) Generate(ctx context.Context, name string, opts ...caopts.Key
82 return nil, err
83 }
84
85 + sk = priv
86 + pk = pub
87 + case "secp256k1":
88 + priv, pub, err := crypto.GenerateSecp256k1Key(rand.Reader)
89 + if err != nil {
90 + return nil, err
91 + }
92 +
93 sk = priv
94 pk = pub
95 default:
test/sharness/lib/test-lib.sh
+16
@@ -486,6 +486,14 @@ test_check_ed25519_b58mh_peerid() {
486 }
487 }
488
489 +test_check_secp256k1_b58mh_peerid() {
490 + peeridlen=$(echo "$1" | tr -dC "[:alnum:]" | wc -c | tr -d " ") &&
491 + test "$peeridlen" = "53" || {
492 + echo "Bad SECP256K1 B58MH peerid '$1' with len '$peeridlen'"
493 + return 1
494 + }
495 +}
496 +
497 test_check_rsa2048_base36_peerid() {
498 peeridlen=$(echo "$1" | tr -dC "[:alnum:]" | wc -c | tr -d " ") &&
499 test "$peeridlen" = "56" || {
@@ -502,6 +510,14 @@ test_check_ed25519_base36_peerid() {
510 }
511 }
512
513 +test_check_secp256k1_base36_peerid() {
514 + peeridlen=$(echo "$1" | tr -dC "[:alnum:]" | wc -c | tr -d " ") &&
515 + test "$peeridlen" = "63" || {
516 + echo "Bad SECP256K1 B36CID peerid '$1' with len '$peeridlen'"
517 + return 1
518 + }
519 +}
520 +
521 convert_tcp_maddr() {
522 echo $1 | awk -F'/' '{ printf "%s:%s", $3, $5 }'
523 }
test/sharness/t0027-rotate.sh
+7
@@ -87,12 +87,19 @@ test_rotate() {
87 }
88 test_rotate 'rsa' ''
89 test_rotate 'ed25519' ''
90 +test_rotate 'secp256k1' ''
91 test_rotate '' ''
92 test_rotate 'rsa' 'rsa'
93 test_rotate 'ed25519' 'rsa'
94 +test_rotate 'secp256k1' 'rsa'
95 test_rotate '' 'rsa'
96 test_rotate 'rsa' 'ed25519'
97 test_rotate 'ed25519' 'ed25519'
98 +test_rotate 'secp256k1' 'ed25519'
99 test_rotate '' 'ed25519'
100 +test_rotate 'rsa' 'secp256k1'
101 +test_rotate 'ed25519' 'secp256k1'
102 +test_rotate 'secp256k1' 'secp256k1'
103 +test_rotate '' 'secp256k1'
104
105 test_done
test/sharness/t0165-keystore.sh
+31
@@ -55,6 +55,29 @@ PEERID=$(ipfs key list --ipns-base=base36 -l | grep key_ed25519 | head -n 1 | cu
55 test_check_ed25519_base36_peerid $PEERID &&
56 ipfs key rm key_ed25519
57 '
58 +
59 +test_expect_success "create an SECP256k1 key and test B58MH/B36CID output formats" '
60 +PEERID=$(ipfs key gen --ipns-base=b58mh --type=secp256k1 key_secp256k1) &&
61 +test_check_secp256k1_b58mh_peerid $PEERID &&
62 +ipfs key rm key_secp256k1 &&
63 +PEERID=$(ipfs key gen --ipns-base=base36 --type=secp256k1 key_secp256k1) &&
64 +test_check_secp256k1_base36_peerid $PEERID
65 +'
66 +
67 +test_expect_success "test SECP256k1 key sk export format" '
68 +ipfs key export key_secp256k1 &&
69 +test_check_ed25519_sk key_secp256k1.key &&
70 +rm key_secp256k1.key
71 +'
72 +
73 +test_expect_success "test SECP256k1 key B58MH/B36CID multihash format" '
74 +PEERID=$(ipfs key list --ipns-base=b58mh -l | grep key_secp256k1 | head -n 1 | cut -d " " -f1) &&
75 +test_check_secp256k1_b58mh_peerid $PEERID &&
76 +PEERID=$(ipfs key list --ipns-base=base36 -l | grep key_secp256k1 | head -n 1 | cut -d " " -f1) &&
77 +test_check_secp256k1_base36_peerid $PEERID &&
78 +ipfs key rm key_secp256k1
79 +'
80 +
81 # end of format test
82
83
@@ -72,6 +95,11 @@ ipfs key rm key_ed25519
95
96 test_key_import_export_all_formats ed25519_key
97
98 + test_expect_success "create a new secp256k1 key" '
99 + k1hash=$(ipfs key gen generated_secp256k1_key --type=secp256k1)
100 + echo $k1hash > secp256k1_key_id
101 + '
102 +
103 test_openssl_compatibility_all_types
104
105 INVALID_KEY=../t0165-keystore-data/openssl_secp384r1.pem
@@ -116,6 +144,7 @@ ipfs key rm key_ed25519
144 test_expect_success "all keys show up in list output" '
145 echo generated_ed25519_key > list_exp &&
146 echo generated_rsa_key >> list_exp &&
147 + echo generated_secp256k1_key >> list_exp &&
148 echo quxel >> list_exp &&
149 echo self >> list_exp
150 ipfs key list > list_out &&
@@ -135,6 +164,7 @@ ipfs key rm key_ed25519
164 test_expect_success "key rm remove a key" '
165 ipfs key rm generated_rsa_key
166 echo generated_ed25519_key > list_exp &&
167 + echo generated_secp256k1_key >> list_exp &&
168 echo quxel >> list_exp &&
169 echo self >> list_exp
170 ipfs key list > list_out &&
@@ -149,6 +179,7 @@ ipfs key rm key_ed25519
179 test_expect_success "key rename rename a key" '
180 ipfs key rename generated_ed25519_key fooed
181 echo fooed > list_exp &&
182 + echo generated_secp256k1_key >> list_exp &&
183 echo quxel >> list_exp &&
184 echo self >> list_exp
185 ipfs key list > list_out &&