/
private_transaction.go
156 lines (141 loc) · 4.54 KB
/
private_transaction.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
package types
import (
"crypto/ecdsa"
"fmt"
"math/big"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/rlp"
"golang.org/x/crypto/sha3"
)
// PrivateTransaction .
type PrivateTransaction struct {
Data txdata
}
type txdata struct {
AccountNonce uint64 `json:"nonce" gencodec:"required"`
Price *big.Int `json:"gasPrice" gencodec:"required"`
GasLimit uint64 `json:"gas" gencodec:"required"`
Recipient *common.Address `json:"to" rlp:"nil"` // nil means contract creation
Amount *big.Int `json:"value" gencodec:"required"`
Payload []byte `json:"input" gencodec:"required"`
V *big.Int `json:"v" gencodec:"required"`
R *big.Int `json:"r" gencodec:"required"`
S *big.Int `json:"s" gencodec:"required"`
PrivateFrom []byte `json:"private_from" gencodec:"required"`
PrivateFor [][]byte `json:"private_for" gencodec:"required"`
Restriction string
}
// NewContractCreation .
func NewContractCreation(nonce uint64, amount *big.Int, gasLimit uint64, gasPrice *big.Int, data []byte, privateFrom []byte, privateFor [][]byte) *PrivateTransaction {
return newTransaction(nonce, nil, amount, gasLimit, gasPrice, data, privateFrom, privateFor)
}
// NewTransaction .
func NewTransaction(nonce uint64, to *common.Address, amount *big.Int, gasLimit uint64, gasPrice *big.Int, data []byte, privateFrom []byte, privateFor [][]byte) *PrivateTransaction {
return newTransaction(nonce, to, amount, gasLimit, gasPrice, data, privateFrom, privateFor)
}
// SignTx .
func (tx *PrivateTransaction) SignTx(chainID *big.Int, prv *ecdsa.PrivateKey) (*PrivateTransaction, error) {
h := hash(tx, chainID)
sig, err := crypto.Sign(h[:], prv)
if err != nil {
return nil, err
}
return withSignature(tx, sig, chainID)
}
// MarshalPrivateTransaction .
func MarshalPrivateTransaction(r map[string]interface{}) (*PrivateTransaction, error) {
// AccountNonce: can not get private nonce from r now
// Price , GasLimit Amount dont care
// recipient required
var recipient *common.Address
if _, ok := r["to"]; !ok {
*recipient = common.HexToAddress(r["to"].(string))
}
// payload required
if _, ok := r["input"]; !ok {
return nil, fmt.Errorf("input data not found")
}
payload, err := hexutil.Decode(r["input"].(string))
if err != nil {
return nil, fmt.Errorf("payload can not decode")
}
// KEVIN TODO: load other args
ptx := txdata{
Recipient: recipient,
Payload: payload,
}
return &PrivateTransaction{
Data: ptx,
}, nil
}
func newTransaction(nonce uint64, to *common.Address, amount *big.Int, gasLimit uint64, gasPrice *big.Int, data []byte, privateFrom []byte, privateFor [][]byte) *PrivateTransaction {
if len(data) > 0 {
data = common.CopyBytes(data)
}
d := txdata{
AccountNonce: nonce,
Recipient: to,
Payload: data,
Amount: new(big.Int),
GasLimit: gasLimit,
Price: new(big.Int),
PrivateFrom: privateFrom,
PrivateFor: privateFor,
Restriction: "restricted",
V: new(big.Int),
R: new(big.Int),
S: new(big.Int),
}
if amount != nil {
d.Amount.Set(amount)
}
if gasPrice != nil {
d.Price.Set(gasPrice)
}
return &PrivateTransaction{Data: d}
}
func hash(tx *PrivateTransaction, chainID *big.Int) common.Hash {
h := rlpHash([]interface{}{
tx.Data.AccountNonce,
tx.Data.Price,
tx.Data.GasLimit,
tx.Data.Recipient,
tx.Data.Amount,
tx.Data.Payload,
chainID, uint(0), uint(0),
tx.Data.PrivateFrom,
tx.Data.PrivateFor,
tx.Data.Restriction,
})
return h
}
func rlpHash(x interface{}) (h common.Hash) {
hw := sha3.NewLegacyKeccak256()
err := rlp.Encode(hw, x)
if err != nil {
return common.Hash{}
}
hw.Sum(h[:0])
return h
}
func withSignature(tx *PrivateTransaction, sig []byte, chainID *big.Int) (*PrivateTransaction, error) {
r, s, v, err := signatureValues(tx, sig)
if err != nil {
return nil, err
}
newV := v.Uint64() + chainID.Uint64()*2 + 8 // KEVIN hack from web3js-eea
cpy := &PrivateTransaction{Data: tx.Data}
cpy.Data.R, cpy.Data.S, cpy.Data.V = r, s, new(big.Int).SetUint64(newV)
return cpy, nil
}
func signatureValues(tx *PrivateTransaction, sig []byte) (r, s, v *big.Int, err error) {
if len(sig) != crypto.SignatureLength {
panic(fmt.Sprintf("wrong size for signature: got %d, want %d", len(sig), crypto.SignatureLength))
}
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
}