Skip to content
This repository was archived by the owner on Feb 7, 2024. It is now read-only.

Commit 32c19d8

Browse files
committed
implement: ipfs key gen
Signed-off-by: Max Peng <[email protected]> (cherry picked from commit cb0f9c6)
1 parent 981fa08 commit 32c19d8

File tree

2 files changed

+46
-0
lines changed

2 files changed

+46
-0
lines changed

key.go

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,41 @@ type keyListOutput struct {
1111
Keys []*Key
1212
}
1313

14+
type KeyOpt func(*RequestBuilder) error
15+
type keyGen struct{}
16+
17+
var KeyGen keyGen
18+
19+
func (keyGen) Type(alg string) KeyOpt {
20+
return func(rb *RequestBuilder) error {
21+
rb.Option("type", alg)
22+
return nil
23+
}
24+
}
25+
26+
func (keyGen) Size(size int) KeyOpt {
27+
return func(rb *RequestBuilder) error {
28+
rb.Option("size", size)
29+
return nil
30+
}
31+
}
32+
33+
// KeyGen Create a new keypair
34+
func (s *Shell) KeyGen(ctx context.Context, name string, options ...KeyOpt) (*Key, error) {
35+
rb := s.Request("key/gen", name)
36+
for _, opt := range options {
37+
if err := opt(rb); err != nil {
38+
return nil, err
39+
}
40+
}
41+
42+
var out Key
43+
if err := rb.Exec(ctx, &out); err != nil {
44+
return nil, err
45+
}
46+
return &out, nil
47+
}
48+
1449
// KeyList List all local keypairs
1550
func (s *Shell) KeyList(ctx context.Context) ([]*Key, error) {
1651
var out keyListOutput

key_test.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,17 @@ import (
77
"github.com/cheekybits/is"
88
)
99

10+
func TestKeyGen(t *testing.T) {
11+
is := is.New(t)
12+
s := NewShell(shellUrl)
13+
14+
key, err := s.KeyGen(context.Background(), "testKey", KeyGen.Type("ed25519"), KeyGen.Size(2048))
15+
is.Nil(err)
16+
17+
is.Equal(key.Name, "testKey")
18+
is.NotNil(key.Id)
19+
}
20+
1021
func TestKeyList(t *testing.T) {
1122
is := is.New(t)
1223
s := NewShell(shellUrl)

0 commit comments

Comments
 (0)