OSDN Git Service

fix rescan wallet (#1108)
[bytom/bytom-spv.git] / api / hsm.go
1 package api
2
3 import (
4         "context"
5
6         log "github.com/sirupsen/logrus"
7
8         "github.com/bytom/blockchain/txbuilder"
9         "github.com/bytom/crypto/ed25519/chainkd"
10 )
11
12 func (a *API) pseudohsmCreateKey(ctx context.Context, in struct {
13         Alias    string `json:"alias"`
14         Password string `json:"password"`
15 }) Response {
16         xpub, err := a.wallet.Hsm.XCreate(in.Alias, in.Password)
17         if err != nil {
18                 return NewErrorResponse(err)
19         }
20         return NewSuccessResponse(xpub)
21 }
22
23 func (a *API) pseudohsmListKeys(ctx context.Context) Response {
24         return NewSuccessResponse(a.wallet.Hsm.ListKeys())
25 }
26
27 func (a *API) pseudohsmDeleteKey(ctx context.Context, x struct {
28         Password string       `json:"password"`
29         XPub     chainkd.XPub `json:"xpub"`
30 }) Response {
31         if err := a.wallet.Hsm.XDelete(x.XPub, x.Password); err != nil {
32                 return NewErrorResponse(err)
33         }
34         return NewSuccessResponse(nil)
35 }
36
37 type signResp struct {
38         Tx           *txbuilder.Template `json:"transaction"`
39         SignComplete bool                `json:"sign_complete"`
40 }
41
42 func (a *API) pseudohsmSignTemplates(ctx context.Context, x struct {
43         Password string             `json:"password"`
44         Txs      txbuilder.Template `json:"transaction"`
45 }) Response {
46         if err := txbuilder.Sign(ctx, &x.Txs, x.Password, a.pseudohsmSignTemplate); err != nil {
47                 log.WithField("build err", err).Error("fail on sign transaction.")
48                 return NewErrorResponse(err)
49         }
50         log.Info("Sign Transaction complete.")
51         return NewSuccessResponse(&signResp{Tx: &x.Txs, SignComplete: txbuilder.SignProgress(&x.Txs)})
52 }
53
54 func (a *API) pseudohsmSignTemplate(ctx context.Context, xpub chainkd.XPub, path [][]byte, data [32]byte, password string) ([]byte, error) {
55         return a.wallet.Hsm.XSign(xpub, path, data[:], password)
56 }
57
58 // ResetPasswordResp is response for reset password password
59 type ResetPasswordResp struct {
60         Changed bool `json:"changed"`
61 }
62
63 func (a *API) pseudohsmResetPassword(ctx context.Context, ins struct {
64         XPub        chainkd.XPub `json:"xpub"`
65         OldPassword string       `json:"old_password"`
66         NewPassword string       `json:"new_password"`
67 }) Response {
68         resp := &ResetPasswordResp{Changed: false}
69         if err := a.wallet.Hsm.ResetPassword(ins.XPub, ins.OldPassword, ins.NewPassword); err != nil {
70                 return NewSuccessResponse(resp)
71         }
72         resp.Changed = true
73         return NewSuccessResponse(resp)
74 }