-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathsign_request.go
152 lines (113 loc) · 3.66 KB
/
sign_request.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
package block_io_go
import (
"encoding/json"
"encoding/hex"
"errors"
)
// Sign any hex string
func SignInputs(ecKey *ECKey, hexData string) (string, error) {
data, err := hex.DecodeString(hexData)
if (err != nil) {
return "", err
}
return ecKey.SignHex(data)
}
func signRequest(ecKey *ECKey, reqData *SignatureData) error {
pubKey := ecKey.PublicKeyHex()
for i := 0; i < len(reqData.Inputs); i++ {
for j := 0; j < len(reqData.Inputs[i].Signers); j++ {
if (reqData.Inputs[i].Signers[j].SignerPublicKey != pubKey) {
continue
}
var err error = nil
reqData.Inputs[i].Signers[j].SignedData, err = SignInputs(ecKey, reqData.Inputs[i].DataToSign)
if (err != nil) {
return err
}
}
}
return nil
}
// Sign a withdrawal request with a pin
func SignWithdrawRequest(pin string, withdrawData *SignatureData) (string, error) {
if (withdrawData.EncryptedPassphrase == EncryptedPassphrase{} ||
withdrawData.EncryptedPassphrase.Passphrase == "") {
return "", errors.New("Withdrawal sign request is missing encrypted passphrase")
}
aesKey := PinToAesKey(pin)
var encryptedPassphrase = withdrawData.EncryptedPassphrase.Passphrase
ecKey, err := ExtractKeyFromEncryptedPassphrase(encryptedPassphrase, aesKey)
if (err != nil) {
return "", err
}
pubKeyHex := ecKey.PublicKeyHex()
if pubKeyHex != withdrawData.EncryptedPassphrase.SignerPublicKey {
return "", errors.New("Public key mismatch")
}
withdrawData.EncryptedPassphrase = EncryptedPassphrase{}
return SignRequestWithKey(ecKey, withdrawData)
}
// Convenience withdrawal request function that takes a JSON string
func SignWithdrawRequestJson(pin string, withdrawData string) (string, error) {
withdrawObj, err := ParseSignatureResponse(withdrawData)
if err != nil {
return "", err
}
return SignWithdrawRequest(pin, withdrawObj)
}
// Sign a withdrawal request with a custom ECKey
func SignRequestWithKey(eckey *ECKey, sigRequest *SignatureData) (string, error) {
if sigRequest.ReferenceID == "" {
return "", errors.New("Signing request is missing referenceId")
}
signErr := signRequest(eckey, sigRequest)
if signErr != nil {
return "", signErr
}
jsonData, err := json.Marshal(sigRequest)
if err != nil {
return "", err
}
return string(jsonData), nil
}
// Sign a JSON string withdrawal request with a custom ECKey
func SignRequestJsonWithKey(ecKey *ECKey, data string) (string, error) {
sigRequest, err := ParseSignatureResponse(data)
if (err != nil) {
return "", err
}
return SignRequestWithKey(ecKey, sigRequest)
}
// Sign a withdrawal request with a set of custom ECKeys
func SignRequestWithKeys(ecKeys []*ECKey, sigRequest *SignatureData) (string, error) {
if sigRequest.ReferenceID == "" {
return "", errors.New("Signing request is missing referenceId")
}
for i := 0; i < len(ecKeys); i++ {
signErr := signRequest(ecKeys[i], sigRequest)
if (signErr != nil) {
return "", nil
}
}
output, err := json.Marshal(sigRequest)
if err != nil {
return "", err
}
return string(output), nil
}
// Sign a JSON string withdrawal request with a set of custom ECKeys
func SignRequestJsonWithKeys(ecKeys []*ECKey, data string) (string, error) {
sigObj, err := ParseSignatureResponse(data)
if (err != nil) {
return "", err
}
return SignRequestWithKeys(ecKeys, sigObj)
}
//DEPRECIATED. Use SignRequestWithKeys or SignRequestJsonWithKeys
func SignDtrustRequest(ecKeys []*ECKey, dtrustReqData SignatureData) (string, error) {
return SignRequestWithKeys(ecKeys, &dtrustReqData)
}
//DEPRECIATED. Use SignRequestWithKey or SignRequestJsonWithKey
func SignSweepRequest(ecKey *ECKey, sweepReqData SignatureData) (string, error) {
return SignRequestWithKey(ecKey, &sweepReqData)
}