-
Notifications
You must be signed in to change notification settings - Fork 3
/
abc.go
406 lines (342 loc) · 10.2 KB
/
abc.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
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
package main
import (
"bytes"
"crypto/sha256"
"encoding/hex"
"errors"
"flag"
"fmt"
"math/rand"
"os"
"strconv"
"strings"
"time"
"github.com/bcext/cashutil"
"github.com/bcext/gcash/chaincfg"
"github.com/bcext/gcash/chaincfg/chainhash"
"github.com/bcext/gcash/rpcclient"
"github.com/bcext/gcash/txscript"
"github.com/bcext/gcash/wire"
"github.com/qshuai/tcolor"
"github.com/shopspring/decimal"
)
var (
param = &chaincfg.MainNetParams
client *rpcclient.Client
wif *cashutil.WIF
sender cashutil.Address
feerateDecimal decimal.Decimal
wait int
utxos chan utxo
)
const (
defaultSignatureSize = 107
defaultSequence = 0xffffffff
// in fact, 540 satoshi is enough.
defaultP2SHoutputValue = 546
// to ensure the transaction will be relayed
addfee = 10
)
type utxo struct {
hash *chainhash.Hash
vout int
value int64
script []byte
}
func main() {
for u := range utxos {
fmt.Println("available utxo:", len(utxos)+1)
// assemble transaction with necessary elements.
tx, err := createAssembleTx(u, sender, wif, feerateDecimal)
if err != nil {
fmt.Println(tcolor.WithColor(tcolor.Red, "assemble transaction failed: "+err.Error()))
os.Exit(1)
}
fmt.Println("first transaction: ")
fmt.Println("\thash:", tx.TxHash())
buf := bytes.NewBuffer(nil)
err = tx.Serialize(buf)
if err != nil {
fmt.Println(tcolor.WithColor(tcolor.Red, "transaction serialize failed: "+err.Error()))
os.Exit(1)
}
fmt.Println("rawtx:", hex.EncodeToString(buf.Bytes()))
// broadcast the first transaction
_, err = client.SendRawTransaction(tx, false)
if err != nil {
panic(tcolor.WithColor(tcolor.Red, "broadcast transaction failed: "+err.Error()))
}
// =======================
// shoud add a utxo to channel
shash := tx.TxHash()
spendableutxo := utxo{
hash: &shash,
value: tx.TxOut[1].Value,
vout: 1,
script: tx.TxOut[1].PkScript,
}
utxos <- spendableutxo
// -------------------------
// so now we get a utxo.
hash := tx.TxHash()
u := utxo{
hash: &hash,
value: defaultP2SHoutputValue,
vout: 0,
script: tx.TxOut[0].PkScript,
}
// =======================
spendTx, err := spendAssembleTx(u, wif)
if err != nil {
fmt.Println(tcolor.WithColor(tcolor.Red, "assemble transaction failed: "+err.Error()))
os.Exit(1)
}
// broadcast the second transaction
_, err = client.SendRawTransaction(spendTx, false)
if err != nil {
panic(tcolor.WithColor(tcolor.Red, "broadcast transaction failed: "+err.Error()))
}
fmt.Println("second transction:")
fmt.Println("\thash:", tcolor.WithColor(tcolor.Green, spendTx.TxHash().String()))
buf2 := bytes.NewBuffer(nil)
err = tx.Serialize(buf2)
if err != nil {
fmt.Println(tcolor.WithColor(tcolor.Red, "transaction serialize failed: "+err.Error()))
os.Exit(1)
}
fmt.Println("\trawtx:", hex.EncodeToString(buf2.Bytes()))
// leave a blank line
fmt.Println()
time.Sleep(time.Duration(wait) * time.Second)
}
}
// the output with special opcode locate on the first output.
func createAssembleTx(u utxo, sender cashutil.Address,
wif *cashutil.WIF, feerate decimal.Decimal) (*wire.MsgTx, error) {
var tx wire.MsgTx
tx.Version = 1
tx.LockTime = 0
// reference a spendable utxo
outpoint := wire.NewOutPoint(u.hash, uint32(u.vout))
tx.TxIn = append(tx.TxIn, wire.NewTxIn(outpoint, nil))
tx.TxIn[0].Sequence = defaultSequence
// p2sh lock script with opcode OP_CHECKDATASIG
script, err := txscript.NewScriptBuilder().AddData(wif.SerializePubKey()).
AddOp(txscript.OP_CHECKDATASIG).Script()
if err != nil {
return nil, err
}
scriptHash := cashutil.Hash160(script)
// create a output with hash(payload: public key and OP_CHECKDATASIG)
tx.TxOut = make([]*wire.TxOut, 2)
hashScript, err := txscript.NewScriptBuilder().AddOp(txscript.OP_HASH160).
AddData(scriptHash).AddOp(txscript.OP_EQUAL).Script()
if err != nil {
return nil, err
}
tx.TxOut[0] = &wire.TxOut{PkScript: hashScript, Value: defaultP2SHoutputValue}
// add a change output, the offset in output is 1.
changeScript, err := txscript.PayToAddrScript(sender)
if err != nil {
return nil, err
}
tx.TxOut[1] = &wire.TxOut{PkScript: changeScript}
// calculate the chang amount
txsize := tx.SerializeSize() + defaultSignatureSize
fee := feerate.Mul(decimal.New(int64(txsize*1e5), 0)).Truncate(0).IntPart()
outValue := u.value - fee - addfee - defaultP2SHoutputValue
tx.TxOut[1].Value = outValue
sourcePkScript, err := txscript.PayToAddrScript(sender)
if err != nil {
return nil, err
}
// sign the transaction
return createSign(&tx, []int64{u.value}, sourcePkScript, wif)
}
func createSign(tx *wire.MsgTx, inputValueSlice []int64, pkScript []byte, wif *cashutil.WIF) (*wire.MsgTx, error) {
for idx, _ := range tx.TxIn {
sig, err := txscript.RawTxInSignature(tx, idx, pkScript, cashutil.Amount(inputValueSlice[idx]),
txscript.SigHashAll|txscript.SigHashForkID, wif.PrivKey)
if err != nil {
return nil, err
}
sig, err = txscript.NewScriptBuilder().AddData(sig).Script()
if err != nil {
return nil, err
}
pk, err := txscript.NewScriptBuilder().AddData(wif.PrivKey.PubKey().SerializeCompressed()).Script()
if err != nil {
return nil, err
}
sig = append(sig, pk...)
tx.TxIn[0].SignatureScript = sig
// check whether signature is ok or not.
engine, err := txscript.NewEngine(pkScript, tx, idx, txscript.StandardVerifyFlags,
nil, nil, inputValueSlice[idx])
if err != nil {
return nil, err
}
// execution the script in stack
err = engine.Execute()
if err != nil {
return nil, err
}
}
return tx, nil
}
func spendAssembleTx(u utxo, wif *cashutil.WIF) (*wire.MsgTx, error) {
var tx wire.MsgTx
tx.Version = 1
tx.LockTime = 0
// add a OP_RETURN output
tx.TxOut = make([]*wire.TxOut, 1)
script, err := txscript.NewScriptBuilder().AddOp(txscript.OP_RETURN).
AddData([]byte("the transaction is valid on chain of bitcoin-abc")).
Script()
if err != nil {
return nil, err
}
tx.TxOut[0] = wire.NewTxOut(0, script)
outpoint := wire.NewOutPoint(u.hash, uint32(u.vout))
tx.TxIn = append(tx.TxIn, wire.NewTxIn(outpoint, nil))
tx.TxIn[0].Sequence = defaultSequence
// sign the transaction
return spendSign(&tx, []int64{u.value}, u.script, wif)
}
func spendSign(tx *wire.MsgTx, inputValueSlice []int64, pkScript []byte, wif *cashutil.WIF) (*wire.MsgTx, error) {
for idx, _ := range tx.TxIn {
// get random bytes
rand.Seed(time.Now().UnixNano())
data := make([]byte, 32)
rand.Read(data)
hash := sha256.Sum256(data)
sig, err := wif.PrivKey.Sign(hash[:])
if err != nil {
return nil, err
}
script, err := txscript.NewScriptBuilder().AddData(wif.PrivKey.PubKey().SerializeCompressed()).
AddOp(txscript.OP_CHECKDATASIG).Script()
if err != nil {
return nil, err
}
pk, err := txscript.NewScriptBuilder().AddData(sig.Serialize()).AddData(data).
AddData(script).Script()
if err != nil {
return nil, err
}
tx.TxIn[0].SignatureScript = pk
engine, err := txscript.NewEngine(pkScript, tx, idx, txscript.StandardVerifyFlags|txscript.ScriptEnableCheckDataSig,
nil, nil, inputValueSlice[idx])
if err != nil {
return nil, err
}
// verify the signature
err = engine.Execute()
if err != nil {
return nil, err
}
}
return tx, nil
}
func paseUtxo(data []string, owner cashutil.Address) ([]utxo, error) {
set := make([]utxo, 0, len(data))
for _, item := range data {
origin := strings.Split(item, ":")
if len(origin) != 3 {
return nil, errors.New("element not enough")
}
hash, err := chainhash.NewHashFromStr(origin[0])
if err != nil {
return nil, err
}
vout, err := strconv.Atoi(origin[1])
if err != nil {
return nil, err
}
amount, err := strconv.Atoi(origin[2])
if err != nil {
return nil, err
}
pkscript, err := txscript.PayToAddrScript(owner)
if err != nil {
return nil, err
}
set = append(set, utxo{
hash: hash,
vout: vout,
value: int64(amount),
script: pkscript,
})
}
return set, nil
}
func GetRPC(host, user, passwd string) *rpcclient.Client {
if client != nil {
return client
}
// rpc client instance
connCfg := &rpcclient.ConnConfig{
Host: host,
User: user,
Pass: passwd,
HTTPPostMode: true, // Bitcoin core only supports HTTP POST mode
DisableTLS: true, // Bitcoin core does not provide TLS by default
}
// Notice the notification parameter is nil since notifications are
// not supported in HTTP POST mode.
c, err := rpcclient.New(connCfg, nil)
if err != nil {
panic(err)
}
client = c
return c
}
func init() {
privKey := flag.String("privkey", "", "private key of the sender")
feerate := flag.String("feerate", "0.00001", "the specified feerate for bitcoin cash network")
second := flag.Int("wait", 600, "the interval for creating transaction")
host := flag.String("rpchost", "127.0.0.1:8332", "Please input rpc host(ip:port)")
user := flag.String("rpcuser", "", "please input your rpc username")
passwd := flag.String("rpcpassword", "", "please input your rpc password")
u := flag.String("utxo", "", "please input a spendable utxo, format[hash:index:value]")
flag.Parse()
wait = *second
if *privKey == "" || *u == "" {
fmt.Println(tcolor.WithColor(tcolor.Red, "empty private key and receiver not allowed"))
os.Exit(1)
}
client = GetRPC(*host, *user, *passwd)
// parse privkey
var err error
wif, err = cashutil.DecodeWIF(*privKey)
if err != nil {
fmt.Println(tcolor.WithColor(tcolor.Red, "private key format error: "+err.Error()))
os.Exit(1)
}
// get bitcoin address for sender
pubKey := wif.PrivKey.PubKey()
pubKeyBytes := pubKey.SerializeCompressed()
pkHash := cashutil.Hash160(pubKeyBytes)
sender, err = cashutil.NewAddressPubKeyHash(pkHash, param)
if err != nil {
fmt.Println(tcolor.WithColor(tcolor.Red, "address encode failed, please check your privkey: "+err.Error()))
os.Exit(1)
}
// parse feerate
feerateDecimal, err = decimal.NewFromString(*feerate)
if err != nil {
fmt.Println(tcolor.WithColor(tcolor.Red, "incorrect feerate: "+err.Error()))
os.Exit(1)
}
// init utxo container
utxos = make(chan utxo, 100)
ret, err := paseUtxo([]string{*u}, sender)
if err != nil {
fmt.Println(tcolor.WithColor(tcolor.Red, "init utxo failed: "+err.Error()))
os.Exit(1)
}
// import utxo element to channel
for _, item := range ret {
utxos <- item
}
}