/
check.go
272 lines (224 loc) 路 6.5 KB
/
check.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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
package transaction
import (
"crypto/sha256"
"encoding/base64"
"encoding/hex"
"errors"
"math/big"
"strings"
"github.com/MinterTeam/minter-go-sdk/v2/wallet"
"github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/crypto/secp256k1"
"github.com/ethereum/go-ethereum/rlp"
)
// CheckData is like an ordinary bank check.
// Each user of network can issue check with any amount of coins and pass it to another person.
// Receiver will be able to cash a check from arbitrary account.
type CheckData struct {
Nonce []byte // Unique ID of the check
ChainID ChainID // ID of the network
DueBlock uint64 // Defines last block height in which the check can be used
Coin CoinID // ID of coin
Value *big.Int // Amount of coins
GasCoin CoinID // ID of a coin to pay fee
Lock *big.Int // Secret to prevent hijacking
V *big.Int // V signature of issuer
R *big.Int // R signature of issuer
S *big.Int // S signature of issuer
}
// MustSender tries to return sender of transaction and panics on error.
func (check *CheckData) MustSender() string {
sender, err := check.Sender()
if err != nil {
panic(err)
}
return sender
}
// Sender returns sender of CheckDat from transaction signature.
func (check *CheckData) Sender() (string, error) {
pub, err := check.PublicKey()
if err != nil {
return "", err
}
return wallet.AddressByPublicKey(pub)
}
// PublicKey returns public key from transaction signature.
func (check *CheckData) PublicKey() (string, error) {
if check.V.BitLen() > 8 {
return "", errors.New("invalid transaction v, r, s values")
}
v := byte(check.V.Uint64() - 27)
if !crypto.ValidateSignatureValues(v, check.R, check.S, true) {
return "", errors.New("invalid transaction v, r, s values")
}
r := check.R.Bytes()
s := check.S.Bytes()
sig := make([]byte, 65)
copy(sig[32-len(r):32], r)
copy(sig[64-len(s):64], s)
sig[64] = v
hash, err := rlpHash([]interface{}{
check.Nonce,
check.ChainID,
check.DueBlock,
check.Coin,
check.Value,
check.GasCoin,
check.Lock,
})
if err != nil {
return "", err
}
pub, err := secp256k1.RecoverPubkey(hash[:], sig)
if err != nil {
return "", err
}
if len(pub) == 0 || pub[0] != 4 {
return "", errors.New("invalid public key")
}
return wallet.PubPrefix04ToMp(hex.EncodeToString(pub)), nil
}
// Check is like an ordinary bank check.
// Each user of network can issue check with any amount of coins and pass it to another person.
// Receiver will be able to cash a check from arbitrary account.
type Check struct {
*CheckData
passphrase string
}
// NewCheck issues a check that will later be redeemed by the person of your choice.
func NewCheck(nonce string, chainID ChainID, dueBlock uint64, coin CoinID, value *big.Int, gasCoin CoinID) *Check {
check := &Check{
CheckData: &CheckData{
Nonce: []byte(nonce),
ChainID: chainID,
DueBlock: dueBlock,
Coin: coin,
Value: value,
GasCoin: gasCoin,
},
}
return check
}
// DecodeCheckBase64 returns CheckData from RLP-encoded structure in base64 format.
func DecodeCheckBase64(rawCheck string) (*CheckData, error) {
decode, err := base64.StdEncoding.DecodeString(rawCheck)
if err != nil {
panic(err)
}
res := new(CheckData)
if err := rlp.DecodeBytes(decode, res); err != nil {
return nil, err
}
return res, nil
}
// DecodeCheck returns CheckData from RLP-encoded structure in hex format.
func DecodeCheck(check string) (*CheckData, error) {
check = strings.Title(strings.ToLower(check))
if !strings.HasPrefix(check, "Mc") {
return nil, errors.New("check don't has prefix 'Mc'")
}
decode, err := hex.DecodeString(check[2:])
if err != nil {
panic(err)
}
res := new(CheckData)
if err := rlp.DecodeBytes(decode, res); err != nil {
return nil, err
}
return res, nil
}
// SetPassphrase sets secret phrase which you will pass to receiver of the check
func (check *Check) SetPassphrase(passphrase string) *Check {
check.passphrase = passphrase
return check
}
// Encode returns string representation of Check. Checks are prefixed with "Mc". RLP-encoded structure in hex format.
func (check *Check) Encode() (string, error) {
src, err := rlp.EncodeToBytes(check.CheckData)
if err != nil {
return "", err
}
return "Mc" + hex.EncodeToString(src), nil
}
// EncodeBase64 returns string representation of Check. RLP-encoded structure in base64 format.
func (check *Check) EncodeBase64() (string, error) {
src, err := rlp.EncodeToBytes(check.CheckData)
if err != nil {
return "", err
}
return base64.StdEncoding.EncodeToString(src), nil
}
// Sign signs Check with private key
func (check *Check) Sign(prKey string) (encodeInterface, error) {
msgHash, err := rlpHash([]interface{}{
check.Nonce,
check.ChainID,
check.DueBlock,
check.Coin,
check.Value,
check.GasCoin,
})
if err != nil {
return nil, err
}
passphraseSum256 := sha256.Sum256([]byte(check.passphrase))
lock, err := secp256k1.Sign(msgHash[:], passphraseSum256[:])
if err != nil {
return nil, err
}
check.Lock = big.NewInt(0).SetBytes(lock)
msgHashWithLock, err := rlpHash([]interface{}{
check.Nonce,
check.ChainID,
check.DueBlock,
check.Coin,
check.Value,
check.GasCoin,
check.Lock,
})
if err != nil {
return nil, err
}
privateKey, err := crypto.HexToECDSA(prKey)
if err != nil {
return nil, err
}
sig, err := crypto.Sign(msgHashWithLock[:], privateKey)
if err != nil {
return nil, err
}
check.R = new(big.Int).SetBytes(sig[:32])
check.S = new(big.Int).SetBytes(sig[32:64])
check.V = new(big.Int).SetBytes([]byte{sig[64] + 27})
return check, nil
}
// CheckAddress is hijacking protection
type CheckAddress struct {
address Address
passphrase string
}
// NewCheckAddress sets special passphrase to protect checks from hijacking by another person in the moment of activation
func NewCheckAddress(address string, passphrase string) (*CheckAddress, error) {
toHex, err := wallet.AddressToHex(address)
if err != nil {
return nil, err
}
check := &CheckAddress{passphrase: passphrase}
copy(check.address[:], toHex)
return check, nil
}
// Proof returns hash of this passphrase is used as private key in ECDSA to prove that sender is the one who owns the check.
func (check *CheckAddress) Proof() (string, error) {
passphraseSum256 := sha256.Sum256([]byte(check.passphrase))
addressHash, err := rlpHash([]interface{}{
check.address[:],
})
if err != nil {
return "", err
}
lock, err := secp256k1.Sign(addressHash[:], passphraseSum256[:])
if err != nil {
return "", err
}
return hex.EncodeToString(lock), nil
}