Skip to content

Commit 67e1a17

Browse files
imthe-1Jorropo
authored andcommitted
feat: adds secp256k1 keypair type to key gen command, adds test cases
1 parent 99fdaa1 commit 67e1a17

File tree

5 files changed

+65
-3
lines changed

5 files changed

+65
-3
lines changed

core/commands/keystore.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ var keyGenCmd = &cmds.Command{
8383
Tagline: "Create a new keypair",
8484
},
8585
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),
8787
cmds.IntOption(keyStoreSizeOptionName, "s", "size of the key to generate"),
8888
ke.OptionIPNSBase,
8989
},
@@ -398,7 +398,7 @@ The PEM format allows for key generation outside of the IPFS node:
398398
allowAnyKeyType, _ := req.Options[keyAllowAnyTypeOptionName].(bool)
399399
if !allowAnyKeyType {
400400
switch t := sk.(type) {
401-
case *crypto.RsaPrivateKey, *crypto.Ed25519PrivateKey:
401+
case *crypto.RsaPrivateKey, *crypto.Ed25519PrivateKey, *crypto.Secp256k1PrivateKey:
402402
default:
403403
return fmt.Errorf("key type %T is not allowed to be imported, only RSA or Ed25519;"+
404404
" use flag --%s if you are sure of what you're doing",
@@ -604,7 +604,7 @@ environment variable:
604604
Arguments: []cmds.Argument{},
605605
Options: []cmds.Option{
606606
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),
608608
cmds.IntOption(keyStoreSizeOptionName, "s", "size of the key to generate"),
609609
},
610610
NoRemote: true,

core/coreapi/key.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,14 @@ func (api *KeyAPI) Generate(ctx context.Context, name string, opts ...caopts.Key
8282
return nil, err
8383
}
8484

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+
8593
sk = priv
8694
pk = pub
8795
default:

test/sharness/lib/test-lib.sh

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -486,6 +486,14 @@ test_check_ed25519_b58mh_peerid() {
486486
}
487487
}
488488

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+
489497
test_check_rsa2048_base36_peerid() {
490498
peeridlen=$(echo "$1" | tr -dC "[:alnum:]" | wc -c | tr -d " ") &&
491499
test "$peeridlen" = "56" || {
@@ -502,6 +510,14 @@ test_check_ed25519_base36_peerid() {
502510
}
503511
}
504512

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+
505521
convert_tcp_maddr() {
506522
echo $1 | awk -F'/' '{ printf "%s:%s", $3, $5 }'
507523
}

test/sharness/t0027-rotate.sh

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,12 +87,19 @@ test_rotate() {
8787
}
8888
test_rotate 'rsa' ''
8989
test_rotate 'ed25519' ''
90+
test_rotate 'secp256k1' ''
9091
test_rotate '' ''
9192
test_rotate 'rsa' 'rsa'
9293
test_rotate 'ed25519' 'rsa'
94+
test_rotate 'secp256k1' 'rsa'
9395
test_rotate '' 'rsa'
9496
test_rotate 'rsa' 'ed25519'
9597
test_rotate 'ed25519' 'ed25519'
98+
test_rotate 'secp256k1' 'ed25519'
9699
test_rotate '' 'ed25519'
100+
test_rotate 'rsa' 'secp256k1'
101+
test_rotate 'ed25519' 'secp256k1'
102+
test_rotate 'secp256k1' 'secp256k1'
103+
test_rotate '' 'secp256k1'
97104

98105
test_done

test/sharness/t0165-keystore.sh

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,29 @@ PEERID=$(ipfs key list --ipns-base=base36 -l | grep key_ed25519 | head -n 1 | cu
5555
test_check_ed25519_base36_peerid $PEERID &&
5656
ipfs key rm key_ed25519
5757
'
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+
5881
# end of format test
5982

6083

@@ -72,6 +95,11 @@ ipfs key rm key_ed25519
7295

7396
test_key_import_export_all_formats ed25519_key
7497

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+
75103
test_openssl_compatibility_all_types
76104

77105
INVALID_KEY=../t0165-keystore-data/openssl_secp384r1.pem
@@ -116,6 +144,7 @@ ipfs key rm key_ed25519
116144
test_expect_success "all keys show up in list output" '
117145
echo generated_ed25519_key > list_exp &&
118146
echo generated_rsa_key >> list_exp &&
147+
echo generated_secp256k1_key >> list_exp &&
119148
echo quxel >> list_exp &&
120149
echo self >> list_exp
121150
ipfs key list > list_out &&
@@ -135,6 +164,7 @@ ipfs key rm key_ed25519
135164
test_expect_success "key rm remove a key" '
136165
ipfs key rm generated_rsa_key
137166
echo generated_ed25519_key > list_exp &&
167+
echo generated_secp256k1_key >> list_exp &&
138168
echo quxel >> list_exp &&
139169
echo self >> list_exp
140170
ipfs key list > list_out &&
@@ -149,6 +179,7 @@ ipfs key rm key_ed25519
149179
test_expect_success "key rename rename a key" '
150180
ipfs key rename generated_ed25519_key fooed
151181
echo fooed > list_exp &&
182+
echo generated_secp256k1_key >> list_exp &&
152183
echo quxel >> list_exp &&
153184
echo self >> list_exp
154185
ipfs key list > list_out &&

0 commit comments

Comments
 (0)