-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexternal_sign.go
227 lines (192 loc) · 5.22 KB
/
external_sign.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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
package externalsign
import (
"crypto"
"crypto/rsa"
"crypto/sha256"
"encoding/hex"
"encoding/json"
"errors"
"fmt"
"net/http"
"strconv"
"time"
"github.com/go-resty/resty/v2"
"github.com/google/uuid"
"github.com/morph-l2/go-ethereum/common"
"github.com/morph-l2/go-ethereum/common/hexutil"
"github.com/morph-l2/go-ethereum/core/types"
"github.com/morph-l2/go-ethereum/log"
)
type ExternalSign struct {
Appid string
Address string
Privkey *rsa.PrivateKey
Chain string
// http client
Client *resty.Client
Signer types.Signer
}
type BusinessData struct {
Appid string `json:"appid"`
Data string `json:"data"`
Noncestr string `json:"noncestr"`
Timestamp string `json:"timestamp"`
}
type ReqData struct {
BusinessData
BizSignature string `json:"bizSignature"`
Pubkey string `json:"publicKey"`
TxData string `json:"txData"` // hex string of marshaled tx
}
type Data struct {
Address string `json:"address"`
Chain string `json:"chain"`
Sha3 string `json:"sha3"`
}
type GenAddrData struct {
CoinId string `json:"coinId"`
ChainCoinId string `json:"chainCoinId"`
EncryptKey string `json:"encryptKey"`
KeyMd5 string `json:"keyMd5"`
UniqId string `json:"uniqId"`
Chain string `json:"chain"`
}
func NewExternalSign(appid string, priv *rsa.PrivateKey, addr, chain string, signer types.Signer) *ExternalSign {
// new resty.client
client := resty.New()
return &ExternalSign{
Appid: appid,
Privkey: priv,
Client: client,
Address: addr,
Chain: chain,
Signer: signer,
}
}
func (e *ExternalSign) newData(hash string) (*Data, error) {
return &Data{
Address: e.Address,
Chain: e.Chain,
Sha3: hash,
}, nil
}
func (e *ExternalSign) NewGenAddrData() *GenAddrData {
return &GenAddrData{
Chain: e.Chain,
}
}
func (e *ExternalSign) craftReqData(data interface{}, txinfo string) (*ReqData, error) {
nonceStr := uuid.NewString()
dataBs, err := json.Marshal(data)
if err != nil {
return nil, fmt.Errorf("marshal data failed: %w", err)
}
businessData := BusinessData{
Appid: e.Appid,
Data: string(dataBs),
Noncestr: nonceStr,
Timestamp: strconv.FormatInt(time.Now().UnixMilli(), 10),
}
businessDataBs, err := json.Marshal(businessData)
if err != nil {
return nil, fmt.Errorf("marshal data failed: %w", err)
}
hashed := sha256.Sum256([]byte(businessDataBs))
signature, err := rsa.SignPKCS1v15(nil, e.Privkey, crypto.SHA256, hashed[:])
if err != nil {
return nil, fmt.Errorf("sign data failed: %w", err)
}
hexSig := hex.EncodeToString(signature)
pubkey, err := GetPubKeyStr(e.Privkey)
if err != nil {
return nil, fmt.Errorf("GetPubKeyStr err:%w", err)
}
return &ReqData{
BusinessData: businessData,
BizSignature: hexSig,
Pubkey: pubkey,
TxData: txinfo,
}, nil
}
// request external sign
// params: unsigned tx
// return: signed tx
func (e *ExternalSign) RequestSign(url string, tx *types.Transaction) (*types.Transaction, error) {
hashHex := e.Signer.Hash(tx).Hex()
data, err := e.newData(hashHex)
if err != nil {
return nil, fmt.Errorf("new data error:%s", err)
}
// txinfo
txBs, err := tx.MarshalBinary()
if err != nil {
return nil, fmt.Errorf("marshal tx failed:%w", err)
}
reqdata, err := e.craftReqData(*data, hex.EncodeToString(txBs))
if err != nil {
return nil, fmt.Errorf("craft req data error:%s", err)
}
resp, err := e.doRequest(url, reqdata)
if err != nil {
return nil, fmt.Errorf("doRequest err: %w", err)
}
// decode resp
response := new(Response)
err = json.Unmarshal(resp.Body(), response)
if err != nil {
return nil, fmt.Errorf("unmarshal resp err:%w", err)
}
if len(response.Result.SignDatas) == 0 {
return nil, errors.New("respones sha3 empty")
}
sig, err := hexutil.Decode(response.Result.SignDatas[0].Sign)
if err != nil {
return nil, fmt.Errorf("decode sig failed:%w", err)
}
signedTx, err := tx.WithSignature(e.Signer, sig)
if err != nil {
return nil, fmt.Errorf("with signature err:%w", err)
}
return signedTx, nil
}
func (e *ExternalSign) RequestWalletAddr(url string) (*common.Address, error) {
data := e.NewGenAddrData()
reqData, err := e.craftReqData(data, "")
if err != nil {
return nil, fmt.Errorf("craftReqData err:%w", err)
}
resp, err := e.doRequest(url, reqData)
if err != nil {
return nil, fmt.Errorf("doRequest err: %w", err)
}
// decode resp
response := new(Response)
err = json.Unmarshal(resp.Body(), response)
if err != nil {
return nil, fmt.Errorf("unmarshal resp err:%w", err)
}
if len(response.Result.Address) == 0 {
return nil, errors.New("response address empty")
}
addr := common.HexToAddress(response.Result.Address)
return &addr, nil
}
func (e *ExternalSign) doRequest(url string, payload interface{}) (*resty.Response, error) {
log.Trace("req info", "payload", payload)
resp, err := e.Client.R().
SetHeader("Content-Type", "application/json").
SetBody(payload).
Post(url)
if err != nil {
return nil, fmt.Errorf("request sign error: %v", err)
}
// log resp info
log.Trace("response info",
"status", resp.StatusCode(),
"body", resp.String(),
)
if resp.StatusCode() != http.StatusOK {
return nil, fmt.Errorf("response status not ok: %v, resp body:%v", resp.StatusCode(), string(resp.Body()))
}
return resp, nil
}