OSDN Git Service

eda64df691f78aa2875fdf73c14bdfc6073c10aa
[bytom/bytom.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/pseudohsm"
9         "github.com/bytom/blockchain/txbuilder"
10         "github.com/bytom/crypto/ed25519/chainkd"
11 )
12
13 type createKeyResp struct {
14         Xpub     *pseudohsm.XPub `json:"xpub"`
15         Mnemonic *string         `json:"mnemonic"`
16 }
17
18 func (a *API) pseudohsmCreateKey(ctx context.Context, in struct {
19         Alias    string `json:"alias"`
20         Password string `json:"password"`
21         Mnemonic string `json:"nnemonic"`
22         Language string `json:"language"`
23 }) Response {
24         if in.Language == "" {
25                 in.Language = "en"
26         }
27         if len(in.Mnemonic) > 0 {
28                 xpub, err := a.wallet.Hsm.ImportKeyFromMnemonic(in.Alias, in.Password, in.Mnemonic, in.Language)
29                 if err != nil {
30                         return NewErrorResponse(err)
31                 }
32                 return NewSuccessResponse(&createKeyResp{Xpub: xpub})
33         }
34         xpub, mnemonic, err := a.wallet.Hsm.XCreate(in.Alias, in.Password, in.Language)
35         if err != nil {
36                 return NewErrorResponse(err)
37         }
38         return NewSuccessResponse(&createKeyResp{Xpub: xpub, Mnemonic: mnemonic})
39 }
40
41 type importKeyResp struct {
42         Xpub *pseudohsm.XPub `json:"xpub"`
43 }
44
45 func (a *API) pseudohsmListKeys(ctx context.Context) Response {
46         return NewSuccessResponse(a.wallet.Hsm.ListKeys())
47 }
48
49 func (a *API) pseudohsmDeleteKey(ctx context.Context, x struct {
50         Password string       `json:"password"`
51         XPub     chainkd.XPub `json:"xpub"`
52 }) Response {
53         if err := a.wallet.Hsm.XDelete(x.XPub, x.Password); err != nil {
54                 return NewErrorResponse(err)
55         }
56         return NewSuccessResponse(nil)
57 }
58
59 type signResp struct {
60         Tx           *txbuilder.Template `json:"transaction"`
61         SignComplete bool                `json:"sign_complete"`
62 }
63
64 func (a *API) pseudohsmSignTemplates(ctx context.Context, x struct {
65         Password string             `json:"password"`
66         Txs      txbuilder.Template `json:"transaction"`
67 }) Response {
68         if err := txbuilder.Sign(ctx, &x.Txs, x.Password, a.pseudohsmSignTemplate); err != nil {
69                 log.WithField("build err", err).Error("fail on sign transaction.")
70                 return NewErrorResponse(err)
71         }
72         log.Info("Sign Transaction complete.")
73         return NewSuccessResponse(&signResp{Tx: &x.Txs, SignComplete: txbuilder.SignProgress(&x.Txs)})
74 }
75
76 func (a *API) pseudohsmSignTemplate(ctx context.Context, xpub chainkd.XPub, path [][]byte, data [32]byte, password string) ([]byte, error) {
77         return a.wallet.Hsm.XSign(xpub, path, data[:], password)
78 }
79
80 // ResetPasswordResp is response for reset key password
81 type ResetPasswordResp struct {
82         Changed bool `json:"changed"`
83 }
84
85 func (a *API) pseudohsmResetPassword(ctx context.Context, ins struct {
86         XPub        chainkd.XPub `json:"xpub"`
87         OldPassword string       `json:"old_password"`
88         NewPassword string       `json:"new_password"`
89 }) Response {
90         resp := &ResetPasswordResp{Changed: false}
91         if err := a.wallet.Hsm.ResetPassword(ins.XPub, ins.OldPassword, ins.NewPassword); err != nil {
92                 return NewSuccessResponse(resp)
93         }
94         resp.Changed = true
95         return NewSuccessResponse(resp)
96 }
97
98 // CheckPasswordResp is response for check key password
99 type CheckPasswordResp struct {
100         CheckResult bool `json:"check_result"`
101 }
102
103 func (a *API) pseudohsmCheckPassword(ctx context.Context, ins struct {
104         XPub     chainkd.XPub `json:"xpub"`
105         Password string       `json:"password"`
106 }) Response {
107         resp := &CheckPasswordResp{CheckResult: false}
108         if _, err := a.wallet.Hsm.LoadChainKDKey(ins.XPub, ins.Password); err != nil {
109                 return NewSuccessResponse(resp)
110         }
111         resp.CheckResult = true
112         return NewSuccessResponse(resp)
113 }