This repository was archived by the owner on Oct 5, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 41
/
Copy pathkey.go
86 lines (68 loc) · 1.76 KB
/
key.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
package httpapi
import (
"context"
"github.com/ipfs/go-ipfs/core/coreapi/interface"
caopts "github.com/ipfs/go-ipfs/core/coreapi/interface/options"
"github.com/libp2p/go-libp2p-peer"
)
type KeyAPI HttpApi
type keyOutput struct {
JName string `json:"Name"`
Id string
}
func (k *keyOutput) Name() string {
return k.JName
}
func (k *keyOutput) Path() iface.Path {
p, _ := iface.ParsePath("/ipns/" + k.Id)
return p
}
func (k *keyOutput) ID() peer.ID {
p, _ := peer.IDB58Decode(k.Id)
return p
}
func (k *keyOutput) valid() error {
_, err := peer.IDB58Decode(k.Id)
return err
}
func (api *KeyAPI) Generate(ctx context.Context, name string, opts ...caopts.KeyGenerateOption) (iface.Key, error) {
options, err := caopts.KeyGenerateOptions(opts...)
if err != nil {
return nil, err
}
var out keyOutput
err = api.core().request("key/gen", name).
Option("type", options.Algorithm).
Option("size", options.Size).
Exec(ctx, &out)
if err != nil {
return nil, err
}
if err := out.valid(); err != nil {
return nil, err
}
return &out, nil
}
func (api *KeyAPI) Rename(ctx context.Context, oldName string, newName string, opts ...caopts.KeyRenameOption) (iface.Key, bool, error) {
panic("implement me")
}
func (api *KeyAPI) List(ctx context.Context) ([]iface.Key, error) {
panic("implement me")
}
func (api *KeyAPI) Self(ctx context.Context) (iface.Key, error) {
var id struct{ID string}
if err := api.core().request("id").Exec(ctx, &id); err != nil {
return nil, err
}
out := keyOutput{JName: "self", Id: id.ID}
if err := out.valid(); err != nil {
return nil, err
}
return &out, nil
}
func (api *KeyAPI) Remove(ctx context.Context, name string) (iface.Key, error) {
panic("implement me")
}
func (api *KeyAPI) core() *HttpApi {
return (*HttpApi)(api)
}