-
Notifications
You must be signed in to change notification settings - Fork 0
/
hsm.go
142 lines (124 loc) · 4.49 KB
/
hsm.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
package api
import (
"context"
log "github.com/sirupsen/logrus"
"github.com/bytom/bystack/blockchain/txbuilder"
"github.com/bytom/bystack/crypto/sm2/chainkd"
)
type createKeyResp struct {
Alias string `json:"alias"`
XPub chainkd.XPub `json:"xpub"`
File string `json:"file"`
Mnemonic string `json:"mnemonic,omitempty"`
}
func (a *API) pseudohsmCreateKey(ctx context.Context, in struct {
Alias string `json:"alias"`
Password string `json:"password"`
Mnemonic string `json:"mnemonic"`
Language string `json:"language"`
}) Response {
if in.Language == "" {
in.Language = "en"
}
if len(in.Mnemonic) > 0 {
xpub, err := a.wallet.Hsm.ImportKeyFromMnemonic(in.Alias, in.Password, in.Mnemonic, in.Language)
if err != nil {
return NewErrorResponse(err)
}
return NewSuccessResponse(&createKeyResp{Alias: xpub.Alias, XPub: xpub.XPub, File: xpub.File})
}
xpub, mnemonic, err := a.wallet.Hsm.XCreate(in.Alias, in.Password, in.Language)
if err != nil {
return NewErrorResponse(err)
}
return NewSuccessResponse(&createKeyResp{Alias: xpub.Alias, XPub: xpub.XPub, File: xpub.File, Mnemonic: *mnemonic})
}
func (a *API) pseudohsmUpdateKeyAlias(ctx context.Context, in struct {
XPub chainkd.XPub `json:"xpub"`
NewAlias string `json:"new_alias"`
}) Response {
if err := a.wallet.Hsm.UpdateKeyAlias(in.XPub, in.NewAlias); err != nil {
return NewErrorResponse(err)
}
return NewSuccessResponse(nil)
}
func (a *API) pseudohsmListKeys(ctx context.Context) Response {
return NewSuccessResponse(a.wallet.Hsm.ListKeys())
}
func (a *API) pseudohsmDeleteKey(ctx context.Context, x struct {
Password string `json:"password"`
XPub chainkd.XPub `json:"xpub"`
}) Response {
if err := a.wallet.Hsm.XDelete(x.XPub, x.Password); err != nil {
return NewErrorResponse(err)
}
return NewSuccessResponse(nil)
}
type signTemplateResp struct {
Tx *txbuilder.Template `json:"transaction"`
SignComplete bool `json:"sign_complete"`
}
func (a *API) signTemplate(ctx context.Context, x struct {
Password string `json:"password"`
Txs txbuilder.Template `json:"transaction"`
}) Response {
if err := txbuilder.Sign(ctx, &x.Txs, x.Password, a.pseudohsmSignTemplate); err != nil {
log.WithField("build err", err).Error("fail on sign transaction.")
return NewErrorResponse(err)
}
log.Info("Sign Transaction complete.")
return NewSuccessResponse(&signTemplateResp{Tx: &x.Txs, SignComplete: txbuilder.SignProgress(&x.Txs)})
}
type signTemplatesResp struct {
Tx []*txbuilder.Template `json:"transaction"`
SignComplete bool `json:"sign_complete"`
}
func (a *API) signTemplates(ctx context.Context, x struct {
Password string `json:"password"`
Txs []*txbuilder.Template `json:"transactions"`
}) Response {
signComplete := true
for _, tx := range x.Txs {
if err := txbuilder.Sign(ctx, tx, x.Password, a.pseudohsmSignTemplate); err != nil {
log.WithField("build err", err).Error("fail on sign transaction.")
return NewErrorResponse(err)
}
signComplete = signComplete && txbuilder.SignProgress(tx)
}
log.Info("Sign Chain Tx complete.")
return NewSuccessResponse(&signTemplatesResp{Tx: x.Txs, SignComplete: signComplete})
}
func (a *API) pseudohsmSignTemplate(ctx context.Context, xpub chainkd.XPub, path [][]byte, data [32]byte, password string) ([]byte, error) {
return a.wallet.Hsm.XSign(xpub, path, data[:], password)
}
// ResetPasswordResp is response for reset key password
type ResetPasswordResp struct {
Changed bool `json:"changed"`
}
func (a *API) pseudohsmResetPassword(ctx context.Context, ins struct {
XPub chainkd.XPub `json:"xpub"`
OldPassword string `json:"old_password"`
NewPassword string `json:"new_password"`
}) Response {
resp := &ResetPasswordResp{Changed: false}
if err := a.wallet.Hsm.ResetPassword(ins.XPub, ins.OldPassword, ins.NewPassword); err != nil {
return NewSuccessResponse(resp)
}
resp.Changed = true
return NewSuccessResponse(resp)
}
// CheckPasswordResp is response for check key password
type CheckPasswordResp struct {
CheckResult bool `json:"check_result"`
}
func (a *API) pseudohsmCheckPassword(ctx context.Context, ins struct {
XPub chainkd.XPub `json:"xpub"`
Password string `json:"password"`
}) Response {
resp := &CheckPasswordResp{CheckResult: false}
if _, err := a.wallet.Hsm.LoadChainKDKey(ins.XPub, ins.Password); err != nil {
return NewSuccessResponse(resp)
}
resp.CheckResult = true
return NewSuccessResponse(resp)
}