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
|
package main
import (
"math/big"
"fmt"
"github.com/obscuren/secp256k1-go"
_"encoding/hex"
_"crypto/sha256"
_ "bytes"
)
/*
Transaction Contract Size
-------------------------------------------
sender sender 20 bytes
recipient 0x0 20 bytes
value endowment 4 bytes (uint32)
fee fee 4 bytes (uint32)
d_size o_size 4 bytes (uint32)
data ops *
signature signature 64 bytes
*/
var StepFee *big.Int = new(big.Int)
var TxFee *big.Int = new(big.Int)
var ContractFee *big.Int = new(big.Int)
var MemFee *big.Int = new(big.Int)
var DataFee *big.Int = new(big.Int)
var CryptoFee *big.Int = new(big.Int)
var ExtroFee *big.Int = new(big.Int)
var Period1Reward *big.Int = new(big.Int)
var Period2Reward *big.Int = new(big.Int)
var Period3Reward *big.Int = new(big.Int)
var Period4Reward *big.Int = new(big.Int)
type Transaction struct {
nonce string
sender string
recipient string
value uint64
fee uint32
data []string
memory []int
lastTx string
v uint32
r, s []byte
}
func NewTransaction(to string, value uint64, data []string) *Transaction {
tx := Transaction{sender: "1234567890", recipient: to, value: value}
tx.nonce = "0"
tx.fee = 0//uint32((ContractFee + MemoryFee * float32(len(tx.data))) * 1e8)
tx.lastTx = "0"
// Serialize the data
tx.data = make([]string, len(data))
for i, val := range data {
instr, err := CompileInstr(val)
if err != nil {
//fmt.Printf("compile error:%d %v\n", i+1, err)
}
tx.data[i] = instr
}
tx.Sign([]byte("privkey"))
tx.Sender()
return &tx
}
func (tx *Transaction) Hash() []byte {
preEnc := []interface{}{
tx.nonce,
tx.recipient,
tx.value,
tx.fee,
tx.data,
}
return Sha256Bin(Encode(preEnc))
}
func (tx *Transaction) IsContract() bool {
return tx.recipient == ""
}
func (tx *Transaction) Signature(key []byte) []byte {
hash := tx.Hash()
sec := Sha256Bin(key)
sig, _ := secp256k1.Sign(hash, sec)
return sig
}
func (tx *Transaction) PublicKey() []byte {
hash := Sha256Bin(tx.Hash())
sig := append(tx.r, tx.s...)
pubkey, _ := secp256k1.RecoverPubkey(hash, sig)
return pubkey
}
func (tx *Transaction) Sender() []byte {
pubkey := tx.PublicKey()
// Validate the returned key.
// Return nil if public key isn't in full format (04 = full, 03 = compact)
if pubkey[0] != 4 {
return nil
}
return Sha256Bin(pubkey[1:65])[12:]
}
func (tx *Transaction) Sign(privk []byte) {
sig := tx.Signature(privk)
// Add 27 so we get either 27 or 28 (for positive and negative)
tx.v = uint32(sig[64]) + 27
tx.r = sig[:32]
tx.s = sig[32:65]
}
func (tx *Transaction) MarshalRlp() []byte {
// Prepare the transaction for serialization
preEnc := []interface{}{
tx.nonce,
tx.recipient,
tx.value,
tx.fee,
tx.data,
tx.v,
tx.r,
tx.s,
}
return Encode(preEnc)
}
func (tx *Transaction) UnmarshalRlp(data []byte) {
decoder := NewRlpDecoder(data)
tx.nonce = decoder.Get(0).AsString()
tx.recipient = decoder.Get(0).AsString()
tx.value = decoder.Get(2).AsUint()
tx.fee = uint32(decoder.Get(3).AsUint())
d := decoder.Get(4)
tx.data = make([]string, d.Length())
fmt.Println(d.Get(0))
for i := 0; i < d.Length(); i++ {
tx.data[i] = d.Get(i).AsString()
}
tx.v = uint32(decoder.Get(5).AsUint())
tx.r = decoder.Get(6).AsBytes()
tx.s = decoder.Get(7).AsBytes()
}
func InitFees() {
// Base for 2**60
b60 := new(big.Int)
b60.Exp(big.NewInt(2), big.NewInt(64), big.NewInt(0))
// Base for 2**80
b80 := new(big.Int)
b80.Exp(big.NewInt(2), big.NewInt(80), big.NewInt(0))
StepFee.Exp(big.NewInt(10), big.NewInt(16), big.NewInt(0))
//StepFee.Div(b60, big.NewInt(64))
//fmt.Println("StepFee:", StepFee)
TxFee.Exp(big.NewInt(2), big.NewInt(64), big.NewInt(0))
//fmt.Println("TxFee:", TxFee)
ContractFee.Exp(big.NewInt(2), big.NewInt(64), big.NewInt(0))
//fmt.Println("ContractFee:", ContractFee)
MemFee.Div(b60, big.NewInt(4))
//fmt.Println("MemFee:", MemFee)
DataFee.Div(b60, big.NewInt(16))
//fmt.Println("DataFee:", DataFee)
CryptoFee.Div(b60, big.NewInt(16))
//fmt.Println("CrytoFee:", CryptoFee)
ExtroFee.Div(b60, big.NewInt(16))
//fmt.Println("ExtroFee:", ExtroFee)
Period1Reward.Mul(b80, big.NewInt(1024))
//fmt.Println("Period1Reward:", Period1Reward)
Period2Reward.Mul(b80, big.NewInt(512))
//fmt.Println("Period2Reward:", Period2Reward)
Period3Reward.Mul(b80, big.NewInt(256))
//fmt.Println("Period3Reward:", Period3Reward)
Period4Reward.Mul(b80, big.NewInt(128))
//fmt.Println("Period4Reward:", Period4Reward)
}
|