/
transaction_signing.go
391 lines (363 loc) · 11 KB
/
transaction_signing.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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
// Copyright (c) 2018 The MATRIX Authors
// Distributed under the MIT software license, see the accompanying
// file COPYING or or http://www.opensource.org/licenses/mit-license.php
package types
import (
"crypto/ecdsa"
"errors"
"fmt"
"math/big"
"github.com/MatrixAINetwork/go-matrix/common"
"github.com/MatrixAINetwork/go-matrix/crypto"
"github.com/MatrixAINetwork/go-matrix/params"
"runtime"
"sync"
)
var (
ErrInvalidChainId = errors.New("invalid chain id for signer")
)
// sigCache is used to cache the derived sender and contains
// the signer used to derive it.
type sigCache struct {
signer Signer
from common.Address
}
//批量解签名
func BatchSender(txser SelfTransactions) {
var waitG = &sync.WaitGroup{}
maxProcs := runtime.NumCPU() //获取cpu个数
if maxProcs >= 2 {
runtime.GOMAXPROCS(maxProcs - 1) //限制同时运行的goroutines数量
}
for _, tx := range txser {
if tx.GetMatrixType() == common.ExtraUnGasMinerTxType || tx.GetMatrixType() == common.ExtraUnGasValidatorTxType ||
tx.GetMatrixType() == common.ExtraUnGasInterestTxType || tx.GetMatrixType() == common.ExtraUnGasTxsType || tx.GetMatrixType() == common.ExtraUnGasLotteryTxType {
continue
}
sig := NewEIP155Signer(tx.ChainId())
waitG.Add(1)
go Sender_self(sig, tx, waitG)
}
waitG.Wait()
}
// MakeSigner returns a Signer based on the given chain config and block number.
func MakeSigner(config *params.ChainConfig, blockNumber *big.Int) Signer {
var signer Signer
switch {
case config.IsEIP155(blockNumber):
signer = NewEIP155Signer(config.ChainId)
case config.IsHomestead(blockNumber):
//signer = HomesteadSigner{}
default:
//signer = FrontierSigner{}
}
return signer
}
// SignTx signs the transaction using the given signer and private key
func SignTx(tx SelfTransaction, s Signer, prv *ecdsa.PrivateKey) (SelfTransaction, error) {
h := s.Hash(tx)
sig, err := crypto.Sign(h[:], prv)
if err != nil {
return nil, err
}
return tx.WithSignature(s, sig)
}
// Sender returns the address derived from the signature (V, R, S) using secp256k1
// elliptic curve and an error if it failed deriving or upon an incorrect
// signature.
//
// Sender may cache the address, allowing it to be used regardless of
// signing method. The cache is invalidated if the cached signer does
// not match the signer used in the current call.
func Sender(signer Signer, tx SelfTransaction) (common.Address, error) {
if sc := tx.GetFromLoad(); sc != nil {
sigCache, ok := sc.(sigCache)
if ok {
// If the signer used to derive from in a previous
// call is not the same as used current, invalidate
// the cache.
if sigCache.signer.Equal(signer) {
return sigCache.from, nil
}
}
}
addr, err := signer.Sender(tx)
if err != nil {
return common.Address{}, err
}
tx.SetFromLoad(sigCache{signer: signer, from: addr})
return addr, nil
//if sc := tx.from.Load(); sc != nil {
// sigCache := sc.(sigCache)
// // If the signer used to derive from in a previous
// // call is not the same as used current, invalidate
// // the cache.
// if sigCache.signer.Equal(signer) {
// return sigCache.from, nil
// }
//}
//
//addr, err := signer.Sender(tx)
//if err != nil {
// return common.Address{}, err
//}
//tx.from.Store(sigCache{signer: signer, from: addr})
//return addr, nil
}
//
func Sender_self(signer Signer, tx SelfTransaction, waitg *sync.WaitGroup) (common.Address, error) {
defer waitg.Done()
if sc := tx.GetFromLoad(); sc != nil {
sigCache := sc.(sigCache)
// If the signer used to derive from in a previous
// call is not the same as used current, invalidate
// the cache.
if sigCache.signer.Equal(signer) {
return sigCache.from, nil
}
}
addr, err := signer.Sender(tx)
if err != nil {
return common.Address{}, err
}
tx.SetFromLoad(sigCache{signer: signer, from: addr})
return addr, nil
//if sc := tx.from.Load(); sc != nil {
// sigCache := sc.(sigCache)
// // If the signer used to derive from in a previous
// // call is not the same as used current, invalidate
// // the cache.
// if sigCache.signer.Equal(signer) {
// return sigCache.from, nil
// }
//}
//
//addr, err := signer.Sender(tx)
//if err != nil {
// return common.Address{}, err
//}
//tx.from.Store(sigCache{signer: signer, from: addr})
//return addr, nil
}
// Signer encapsulates transaction signature handling. Note that this interface is not a
// stable API and may change at any time to accommodate new protocol rules.
type Signer interface {
// Sender returns the sender address of the transaction.
Sender(tx SelfTransaction) (common.Address, error)
// SignatureValues returns the raw R, S, V values corresponding to the
// given signature.
SignatureValues(tx SelfTransaction, sig []byte) (r, s, v *big.Int, err error)
// Hash returns the hash to be signed.
Hash(tx SelfTransaction) common.Hash
// Equal returns true if the given signer is the same as the receiver.
Equal(Signer) bool
}
// EIP155Transaction implements Signer using the EIP155 rules.
type EIP155Signer struct {
chainId, chainIdMul *big.Int
}
func NewEIP155Signer(chainId *big.Int) EIP155Signer {
if chainId == nil {
chainId = new(big.Int)
}
return EIP155Signer{
chainId: chainId,
chainIdMul: new(big.Int).Mul(chainId, big.NewInt(2)),
}
}
func (s EIP155Signer) Equal(s2 Signer) bool {
eip155, ok := s2.(EIP155Signer)
return ok && eip155.chainId.Cmp(s.chainId) == 0
}
var big8 = big.NewInt(8)
func (s EIP155Signer) Sender(tx SelfTransaction) (common.Address, error) {
if tx.ChainId().Cmp(s.chainId) != 0 {
return common.Address{}, ErrInvalidChainId
}
//=====begin======
V := new(big.Int).Set(tx.GetTxV())
//if V.Cmp(big.NewInt(128)) > 0 {
// V.Sub(V, big.NewInt(128))
//}
V.Sub(V, s.chainIdMul)
//=======end========
V.Sub(V, big8)
return recoverPlain(s.Hash(tx), tx.GetTxR(), tx.GetTxS(), V, true)
//if !tx.Protected() {
// return HomesteadSigner{}.Sender(tx)
//}
//if tx.ChainId().Cmp(s.chainId) != 0 {
// return common.Address{}, ErrInvalidChainId
//}
////=====begin======
//V := new(big.Int).Set(tx.data.V)
//if V.Cmp(big.NewInt(128)) > 0 {
// V.Sub(V, big.NewInt(128))
//}
//V.Sub(V, s.chainIdMul)
////V := new(big.Int).Sub(tx.data.V, s.chainIdMul) 注释原来的方式
////=======end========
//V.Sub(V, big8)
//return recoverPlain(s.Hash(tx), tx.data.R, tx.data.S, V, true)
}
// WithSignature returns a new transaction with the given signature. This signature
// needs to be in the [R || S || V] format where V is 0 or 1.
func (s EIP155Signer) SignatureValues(tx SelfTransaction, sig []byte) (R, S, V *big.Int, err error) {
if len(sig) != 65 {
panic(fmt.Sprintf("wrong size for signature: got %d, want 65", len(sig)))
}
R = new(big.Int).SetBytes(sig[:32])
S = new(big.Int).SetBytes(sig[32:64])
V = new(big.Int).SetBytes([]byte{sig[64] + 27})
//if err != nil {
// return nil, nil, nil, err
//}
if s.chainId.Sign() != 0 {
V = big.NewInt(int64(sig[64] + 35))
V.Add(V, s.chainIdMul)
}
return R, S, V, nil
}
// Hash returns the hash to be signed by the sender.
// It does not uniquely identify the transaction.
func (s EIP155Signer) Hash(txer SelfTransaction) common.Hash {
switch txer.TxType() {
case NormalTxIndex:
tx, ok := txer.(*Transaction)
if !ok {
return common.Hash{}
}
var data1 txdata1
TxdataAddresToString(tx.Currency, &tx.data, &data1)
return rlpHash([]interface{}{
data1.AccountNonce,
data1.Price,
data1.GasLimit,
data1.Recipient,
data1.Amount,
data1.Payload,
s.chainId, uint(0), uint(0),
data1.TxEnterType,
data1.IsEntrustTx,
data1.CommitTime,
data1.Extra,
})
case BroadCastTxIndex:
tx, ok := txer.(*TransactionBroad)
if !ok {
return common.Hash{}
}
return rlpHash([]interface{}{
tx.data.AccountNonce,
tx.data.Price,
tx.data.GasLimit,
tx.data.Recipient,
tx.data.Amount,
tx.data.Payload,
tx.data.Extra,
s.chainId, uint(0), uint(0),
})
default:
return common.Hash{}
}
}
// HomesteadTransaction implements TransactionInterface using the
// homestead rules.
//type HomesteadSigner struct{ FrontierSigner }
//
//func (s HomesteadSigner) Equal(s2 Signer) bool {
// _, ok := s2.(HomesteadSigner)
// return ok
//}
//
//// SignatureValues returns signature values. This signature
//// needs to be in the [R || S || V] format where V is 0 or 1.
//func (hs HomesteadSigner) SignatureValues(tx *Transaction, sig []byte) (r, s, v *big.Int, err error) {
// return hs.FrontierSigner.SignatureValues(tx, sig)
//}
//
//func (hs HomesteadSigner) Sender(tx *Transaction) (common.Address, error) {
// return recoverPlain(hs.Hash(tx), tx.data.R, tx.data.S, tx.data.V, true)
//}
//
//type FrontierSigner struct{}
//
//func (s FrontierSigner) Equal(s2 Signer) bool {
// _, ok := s2.(FrontierSigner)
// return ok
//}
// SignatureValues returns signature values. This signature
// needs to be in the [R || S || V] format where V is 0 or 1.
//func (fs FrontierSigner) SignatureValues(tx *Transaction, sig []byte) (r, s, v *big.Int, err error) {
// if len(sig) != 65 {
// panic(fmt.Sprintf("wrong size for signature: got %d, want 65", len(sig)))
// }
// r = new(big.Int).SetBytes(sig[:32])
// s = new(big.Int).SetBytes(sig[32:64])
// v = new(big.Int).SetBytes([]byte{sig[64] + 27})
// return r, s, v, nil
//}
//
//// Hash returns the hash to be signed by the sender.
//// It does not uniquely identify the transaction.
//func (fs FrontierSigner) Hash(tx *Transaction) common.Hash {
// return rlpHash([]interface{}{
// tx.data.AccountNonce,
// tx.data.Price,
// tx.data.GasLimit,
// tx.data.Recipient,
// tx.data.Amount,
// tx.data.Payload,
// })
//}
//
//func (fs FrontierSigner) Sender(tx *Transaction) (common.Address, error) {
// return recoverPlain(fs.Hash(tx), tx.data.R, tx.data.S, tx.data.V, false)
//}
func recoverPlain(sighash common.Hash, R, S, Vb *big.Int, homestead bool) (common.Address, error) {
if Vb.BitLen() > 8 {
return common.Address{}, ErrInvalidSig
}
V := byte(Vb.Uint64() - 27)
if !crypto.ValidateSignatureValues(V, R, S, homestead) {
return common.Address{}, ErrInvalidSig
}
// encode the snature in uncompressed format
r, s := R.Bytes(), S.Bytes()
sig := make([]byte, 65)
copy(sig[32-len(r):32], r)
copy(sig[64-len(s):64], s)
sig[64] = V
// recover the public key from the snature
pub, err := crypto.Ecrecover(sighash[:], sig)
if err != nil {
return common.Address{}, err
}
if len(pub) == 0 || pub[0] != 4 {
return common.Address{}, errors.New("invalid public key")
}
var addr common.Address
copy(addr[:], crypto.Keccak256(pub[1:])[12:])
return addr, nil
}
// 将原来的deriveChainId方法改为deriveChainId1,然后重写deriveChainId方法
func deriveChainId(v *big.Int) *big.Int {
v1 := new(big.Int).Set(v)
//tmp := big.NewInt(128)
//if v1.Cmp(tmp) > 0 {
// v1.Sub(v1, tmp)
//}
return deriveChainId1(v1)
}
// deriveChainId derives the chain id from the given v parameter
func deriveChainId1(v *big.Int) *big.Int {
if v.BitLen() <= 64 {
v := v.Uint64()
if v == 27 || v == 28 {
return new(big.Int)
}
return new(big.Int).SetUint64((v - 35) / 2)
}
v = new(big.Int).Sub(v, big.NewInt(35))
return v.Div(v, big.NewInt(2))
}