6 log "github.com/sirupsen/logrus"
8 "github.com/vapor/blockchain/txbuilder"
9 "github.com/vapor/common"
10 "github.com/vapor/consensus"
11 "github.com/vapor/crypto"
12 "github.com/vapor/crypto/ed25519/chainkd"
15 type createKeyResp struct {
16 Alias string `json:"alias"`
17 XPub chainkd.XPub `json:"xpub"`
18 File string `json:"file"`
19 Mnemonic string `json:"mnemonic,omitempty"`
22 func (a *API) pseudohsmCreateKey(ctx context.Context, in struct {
23 Alias string `json:"alias"`
24 Password string `json:"password"`
25 Mnemonic string `json:"mnemonic"`
26 Language string `json:"language"`
28 if in.Language == "" {
31 if len(in.Mnemonic) > 0 {
32 xpub, err := a.wallet.Hsm.ImportKeyFromMnemonic(in.Alias, in.Password, in.Mnemonic, in.Language)
34 return NewErrorResponse(err)
36 return NewSuccessResponse(&createKeyResp{Alias: xpub.Alias, XPub: xpub.XPub, File: xpub.File})
38 xpub, mnemonic, err := a.wallet.Hsm.XCreate(in.Alias, in.Password, in.Language)
40 return NewErrorResponse(err)
42 return NewSuccessResponse(&createKeyResp{Alias: xpub.Alias, XPub: xpub.XPub, File: xpub.File, Mnemonic: *mnemonic})
45 func (a *API) pseudohsmUpdateKeyAlias(ctx context.Context, in struct {
46 XPub chainkd.XPub `json:"xpub"`
47 NewAlias string `json:"new_alias"`
49 if err := a.wallet.Hsm.UpdateKeyAlias(in.XPub, in.NewAlias); err != nil {
50 return NewErrorResponse(err)
52 return NewSuccessResponse(nil)
55 func (a *API) pseudohsmListKeys(ctx context.Context) Response {
56 return NewSuccessResponse(a.wallet.Hsm.ListKeys())
59 func (a *API) pseudohsmDeleteKey(ctx context.Context, x struct {
60 Password string `json:"password"`
61 XPub chainkd.XPub `json:"xpub"`
63 if err := a.wallet.Hsm.XDelete(x.XPub, x.Password); err != nil {
64 return NewErrorResponse(err)
66 return NewSuccessResponse(nil)
69 type signTemplateResp struct {
70 Tx *txbuilder.Template `json:"transaction"`
71 SignComplete bool `json:"sign_complete"`
74 func (a *API) signTemplate(ctx context.Context, x struct {
75 Password string `json:"password"`
76 Txs txbuilder.Template `json:"transaction"`
78 if err := txbuilder.Sign(ctx, &x.Txs, x.Password, a.PseudohsmSignTemplate); err != nil {
79 log.WithField("build err", err).Error("fail on sign transaction.")
80 return NewErrorResponse(err)
82 log.Info("Sign Transaction complete.")
83 return NewSuccessResponse(&signTemplateResp{Tx: &x.Txs, SignComplete: txbuilder.SignProgress(&x.Txs)})
86 type signTemplatesResp struct {
87 Tx []*txbuilder.Template `json:"transaction"`
88 SignComplete bool `json:"sign_complete"`
91 func (a *API) signTemplates(ctx context.Context, x struct {
92 Password string `json:"password"`
93 Txs []*txbuilder.Template `json:"transactions"`
96 for _, tx := range x.Txs {
97 if err := txbuilder.Sign(ctx, tx, x.Password, a.PseudohsmSignTemplate); err != nil {
98 log.WithField("build err", err).Error("fail on sign transaction.")
99 return NewErrorResponse(err)
101 signComplete = signComplete && txbuilder.SignProgress(tx)
104 log.Info("Sign Chain Tx complete.")
105 return NewSuccessResponse(&signTemplatesResp{Tx: x.Txs, SignComplete: signComplete})
108 func (a *API) PseudohsmSignTemplate(ctx context.Context, xpub chainkd.XPub, path [][]byte, data [32]byte, password string) ([]byte, error) {
109 return a.wallet.Hsm.XSign(xpub, path, data[:], password)
112 // ResetPasswordResp is response for reset key password
113 type ResetPasswordResp struct {
114 Changed bool `json:"changed"`
117 func (a *API) pseudohsmResetPassword(ctx context.Context, ins struct {
118 XPub chainkd.XPub `json:"xpub"`
119 OldPassword string `json:"old_password"`
120 NewPassword string `json:"new_password"`
122 resp := &ResetPasswordResp{Changed: false}
123 if err := a.wallet.Hsm.ResetPassword(ins.XPub, ins.OldPassword, ins.NewPassword); err != nil {
124 return NewSuccessResponse(resp)
127 return NewSuccessResponse(resp)
130 // CheckPasswordResp is response for check key password
131 type CheckPasswordResp struct {
132 CheckResult bool `json:"check_result"`
135 func (a *API) pseudohsmCheckPassword(ctx context.Context, ins struct {
136 XPub chainkd.XPub `json:"xpub"`
137 Password string `json:"password"`
139 resp := &CheckPasswordResp{CheckResult: false}
140 if _, err := a.wallet.Hsm.LoadChainKDKey(ins.XPub, ins.Password); err != nil {
141 return NewSuccessResponse(resp)
143 resp.CheckResult = true
144 return NewSuccessResponse(resp)
147 type keyPair struct {
148 Xpub chainkd.XPub `json:"xpub"`
149 Xprv chainkd.XPrv `json:"xprv"`
150 Address string `json:"address,omitempty"`
153 func (a *API) createXKeys(ctx context.Context) Response {
154 xprv, xpub, err := chainkd.NewXKeys(nil)
156 return NewErrorResponse(err)
159 pubHash := crypto.Ripemd160(xpub.PublicKey())
161 address, err := common.NewAddressWitnessPubKeyHash(pubHash, &consensus.ActiveNetParams)
163 return NewErrorResponse(err)
166 return NewSuccessResponse(&keyPair{Xprv: xprv, Xpub: xpub, Address: address.EncodeAddress()})