@cryptotaxi247 / kubo / commits / cf74d3916

Partial Key API, ApiAddr funcion

This commit was moved from ipfs/go-ipfs-http-client@0ffdef159261e19cfd76edabb394179aa9295003

Łukasz Magiera committed Jan 8, 2019 at 02:07 UTC cf74d391603457ad626b7f6b27ff0655bc946ee4
2 files changed +97 -2
client/httpapi/api.go
+11 -2
@@ -29,6 +29,7 @@ type HttpApi struct {
29 httpcli *gohttp.Client
30 }
31
32 +//TODO: Return errors here
33 func NewLocalApi() iface.CoreAPI {
34 baseDir := os.Getenv(EnvDir)
35 if baseDir == "" {
@@ -39,6 +40,14 @@ func NewLocalApi() iface.CoreAPI {
40 }
41
42 func NewPathApi(p string) iface.CoreAPI {
43 + a := ApiAddr(p)
44 + if a == nil {
45 + return nil
46 + }
47 + return NewApi(a)
48 +}
49 +
50 +func ApiAddr(p string) ma.Multiaddr {
51 baseDir, err := homedir.Expand(p)
52 if err != nil {
53 return nil
@@ -60,7 +69,7 @@ func NewPathApi(p string) iface.CoreAPI {
69 return nil
70 }
71
63 - return NewApi(maddr)
72 + return maddr
73 }
74
75 func NewApi(a ma.Multiaddr) *HttpApi { // TODO: should be MAddr?
@@ -122,7 +131,7 @@ func (api *HttpApi) Name() iface.NameAPI {
131 }
132
133 func (api *HttpApi) Key() iface.KeyAPI {
125 - return nil
134 + return (*KeyAPI)(api)
135 }
136
137 func (api *HttpApi) Pin() iface.PinAPI {
client/httpapi/key.go new
+86
@@ -0,0 +1,86 @@
1 +package httpapi
2 +
3 +import (
4 + "context"
5 +
6 + "github.com/ipfs/go-ipfs/core/coreapi/interface"
7 + caopts "github.com/ipfs/go-ipfs/core/coreapi/interface/options"
8 +
9 + "github.com/libp2p/go-libp2p-peer"
10 +)
11 +
12 +type KeyAPI HttpApi
13 +
14 +type keyOutput struct {
15 + JName string `json:"Name"`
16 + Id string
17 +}
18 +
19 +func (k *keyOutput) Name() string {
20 + return k.JName
21 +}
22 +
23 +func (k *keyOutput) Path() iface.Path {
24 + p, _ := iface.ParsePath("/ipns/" + k.Id)
25 + return p
26 +}
27 +
28 +func (k *keyOutput) ID() peer.ID {
29 + p, _ := peer.IDB58Decode(k.Id)
30 + return p
31 +}
32 +
33 +func (k *keyOutput) valid() error {
34 + _, err := peer.IDB58Decode(k.Id)
35 + return err
36 +}
37 +
38 +
39 +func (api *KeyAPI) Generate(ctx context.Context, name string, opts ...caopts.KeyGenerateOption) (iface.Key, error) {
40 + options, err := caopts.KeyGenerateOptions(opts...)
41 + if err != nil {
42 + return nil, err
43 + }
44 +
45 + var out keyOutput
46 + err = api.core().request("key/gen", name).
47 + Option("type", options.Algorithm).
48 + Option("size", options.Size).
49 + Exec(ctx, &out)
50 + if err != nil {
51 + return nil, err
52 + }
53 + if err := out.valid(); err != nil {
54 + return nil, err
55 + }
56 + return &out, nil
57 +}
58 +
59 +func (api *KeyAPI) Rename(ctx context.Context, oldName string, newName string, opts ...caopts.KeyRenameOption) (iface.Key, bool, error) {
60 + panic("implement me")
61 +}
62 +
63 +func (api *KeyAPI) List(ctx context.Context) ([]iface.Key, error) {
64 + panic("implement me")
65 +}
66 +
67 +func (api *KeyAPI) Self(ctx context.Context) (iface.Key, error) {
68 + var id struct{ID string}
69 + if err := api.core().request("id").Exec(ctx, &id); err != nil {
70 + return nil, err
71 + }
72 +
73 + out := keyOutput{JName: "self", Id: id.ID}
74 + if err := out.valid(); err != nil {
75 + return nil, err
76 + }
77 + return &out, nil
78 +}
79 +
80 +func (api *KeyAPI) Remove(ctx context.Context, name string) (iface.Key, error) {
81 + panic("implement me")
82 +}
83 +
84 +func (api *KeyAPI) core() *HttpApi {
85 + return (*HttpApi)(api)
86 +}