6 log "github.com/sirupsen/logrus"
8 "github.com/bytom/blockchain/pseudohsm"
9 "github.com/bytom/blockchain/txbuilder"
10 "github.com/bytom/crypto/ed25519/chainkd"
13 type createKeyResp struct {
14 Xpub *pseudohsm.XPub `json:"xpub"`
15 Mnemonic *string `json:"mnemonic"`
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"`
24 if in.Language == "" {
27 if len(in.Mnemonic) > 0 {
28 xpub, err := a.wallet.Hsm.ImportKeyFromMnemonic(in.Alias, in.Password, in.Mnemonic, in.Language)
30 return NewErrorResponse(err)
32 return NewSuccessResponse(&createKeyResp{Xpub: xpub})
34 xpub, mnemonic, err := a.wallet.Hsm.XCreate(in.Alias, in.Password, in.Language)
36 return NewErrorResponse(err)
38 return NewSuccessResponse(&createKeyResp{Xpub: xpub, Mnemonic: mnemonic})
41 type importKeyResp struct {
42 Xpub *pseudohsm.XPub `json:"xpub"`
45 func (a *API) pseudohsmListKeys(ctx context.Context) Response {
46 return NewSuccessResponse(a.wallet.Hsm.ListKeys())
49 func (a *API) pseudohsmDeleteKey(ctx context.Context, x struct {
50 Password string `json:"password"`
51 XPub chainkd.XPub `json:"xpub"`
53 if err := a.wallet.Hsm.XDelete(x.XPub, x.Password); err != nil {
54 return NewErrorResponse(err)
56 return NewSuccessResponse(nil)
59 type signResp struct {
60 Tx *txbuilder.Template `json:"transaction"`
61 SignComplete bool `json:"sign_complete"`
64 func (a *API) pseudohsmSignTemplates(ctx context.Context, x struct {
65 Password string `json:"password"`
66 Txs txbuilder.Template `json:"transaction"`
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)
72 log.Info("Sign Transaction complete.")
73 return NewSuccessResponse(&signResp{Tx: &x.Txs, SignComplete: txbuilder.SignProgress(&x.Txs)})
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)
80 // ResetPasswordResp is response for reset key password
81 type ResetPasswordResp struct {
82 Changed bool `json:"changed"`
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"`
90 resp := &ResetPasswordResp{Changed: false}
91 if err := a.wallet.Hsm.ResetPassword(ins.XPub, ins.OldPassword, ins.NewPassword); err != nil {
92 return NewSuccessResponse(resp)
95 return NewSuccessResponse(resp)
98 // CheckPasswordResp is response for check key password
99 type CheckPasswordResp struct {
100 CheckResult bool `json:"check_result"`
103 func (a *API) pseudohsmCheckPassword(ctx context.Context, ins struct {
104 XPub chainkd.XPub `json:"xpub"`
105 Password string `json:"password"`
107 resp := &CheckPasswordResp{CheckResult: false}
108 if _, err := a.wallet.Hsm.LoadChainKDKey(ins.XPub, ins.Password); err != nil {
109 return NewSuccessResponse(resp)
111 resp.CheckResult = true
112 return NewSuccessResponse(resp)