kubo/client/httpapi/key.go
Łukasz Magiera c213e26542 Unixfs.Add progress events
This commit was moved from ipfs/go-ipfs-http-client@2f3a77b686
2019-01-09 21:55:45 +01:00

86 lines
1.8 KiB
Go

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)
}