6 log "github.com/sirupsen/logrus"
8 "github.com/bytom/blockchain/txbuilder"
9 "github.com/bytom/crypto/ed25519/chainkd"
12 type createKeyResp struct {
13 Alias string `json:"alias"`
14 XPub chainkd.XPub `json:"xpub"`
15 File string `json:"file"`
16 Mnemonic string `json:"mnemonic,omitempty"`
19 func (a *API) pseudohsmCreateKey(ctx context.Context, in struct {
20 Alias string `json:"alias"`
21 Password string `json:"password"`
22 Mnemonic string `json:"mnemonic"`
23 Language string `json:"language"`
25 if in.Language == "" {
28 if len(in.Mnemonic) > 0 {
29 xpub, err := a.wallet.Hsm.ImportKeyFromMnemonic(in.Alias, in.Password, in.Mnemonic, in.Language)
31 return NewErrorResponse(err)
33 return NewSuccessResponse(&createKeyResp{Alias: xpub.Alias, XPub: xpub.XPub, File: xpub.File})
35 xpub, mnemonic, err := a.wallet.Hsm.XCreate(in.Alias, in.Password, in.Language)
37 return NewErrorResponse(err)
39 return NewSuccessResponse(&createKeyResp{Alias: xpub.Alias, XPub: xpub.XPub, File: xpub.File, Mnemonic: *mnemonic})
42 func (a *API) pseudohsmUpdateKeyAlias(ctx context.Context, in struct {
43 XPub chainkd.XPub `json:"xpub"`
44 NewAlias string `json:"new_alias"`
46 if err := a.wallet.Hsm.UpdateKeyAlias(in.XPub, in.NewAlias); err != nil {
47 return NewErrorResponse(err)
49 return NewSuccessResponse(nil)
52 func (a *API) pseudohsmListKeys(ctx context.Context) Response {
53 return NewSuccessResponse(a.wallet.Hsm.ListKeys())
56 func (a *API) pseudohsmDeleteKey(ctx context.Context, x struct {
57 Password string `json:"password"`
58 XPub chainkd.XPub `json:"xpub"`
60 if err := a.wallet.Hsm.XDelete(x.XPub, x.Password); err != nil {
61 return NewErrorResponse(err)
63 return NewSuccessResponse(nil)
66 type signTemplateResp struct {
67 Tx *txbuilder.Template `json:"transaction"`
68 SignComplete bool `json:"sign_complete"`
71 func (a *API) signTemplate(ctx context.Context, x struct {
72 Password string `json:"password"`
73 Txs txbuilder.Template `json:"transaction"`
75 if err := txbuilder.Sign(ctx, &x.Txs, x.Password, a.pseudohsmSignTemplate); err != nil {
76 log.WithField("build err", err).Error("fail on sign transaction.")
77 return NewErrorResponse(err)
79 log.Info("Sign Transaction complete.")
80 return NewSuccessResponse(&signTemplateResp{Tx: &x.Txs, SignComplete: txbuilder.SignProgress(&x.Txs)})
83 type signTemplatesResp struct {
84 Tx []*txbuilder.Template `json:"transaction"`
85 SignComplete bool `json:"sign_complete"`
88 func (a *API) signTemplates(ctx context.Context, x struct {
89 Password string `json:"password"`
90 Txs []*txbuilder.Template `json:"transactions"`
93 for _, tx := range x.Txs {
94 if err := txbuilder.Sign(ctx, tx, x.Password, a.pseudohsmSignTemplate); err != nil {
95 log.WithField("build err", err).Error("fail on sign transaction.")
96 return NewErrorResponse(err)
98 signComplete = signComplete && txbuilder.SignProgress(tx)
101 log.Info("Sign Chain Tx complete.")
102 return NewSuccessResponse(&signTemplatesResp{Tx: x.Txs, SignComplete: signComplete})
105 func (a *API) pseudohsmSignTemplate(ctx context.Context, xpub chainkd.XPub, path [][]byte, data [32]byte, password string) ([]byte, error) {
106 return a.wallet.Hsm.XSign(xpub, path, data[:], password)
109 // ResetPasswordResp is response for reset key password
110 type ResetPasswordResp struct {
111 Changed bool `json:"changed"`
114 func (a *API) pseudohsmResetPassword(ctx context.Context, ins struct {
115 XPub chainkd.XPub `json:"xpub"`
116 OldPassword string `json:"old_password"`
117 NewPassword string `json:"new_password"`
119 resp := &ResetPasswordResp{Changed: false}
120 if err := a.wallet.Hsm.ResetPassword(ins.XPub, ins.OldPassword, ins.NewPassword); err != nil {
121 return NewSuccessResponse(resp)
124 return NewSuccessResponse(resp)
127 // CheckPasswordResp is response for check key password
128 type CheckPasswordResp struct {
129 CheckResult bool `json:"check_result"`
132 func (a *API) pseudohsmCheckPassword(ctx context.Context, ins struct {
133 XPub chainkd.XPub `json:"xpub"`
134 Password string `json:"password"`
136 resp := &CheckPasswordResp{CheckResult: false}
137 if _, err := a.wallet.Hsm.LoadChainKDKey(ins.XPub, ins.Password); err != nil {
138 return NewSuccessResponse(resp)
140 resp.CheckResult = true
141 return NewSuccessResponse(resp)