forked from FactomProject/FactomCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbtc.go
More file actions
640 lines (504 loc) · 18.2 KB
/
btc.go
File metadata and controls
640 lines (504 loc) · 18.2 KB
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
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
package main
import (
"bytes"
"encoding/hex"
"encoding/json"
"strconv"
"time"
// "sort"
"fmt"
"log"
"io/ioutil"
"path/filepath"
"github.com/conformal/btcjson"
"github.com/conformal/btcnet"
"github.com/conformal/btcscript"
"github.com/conformal/btcutil"
"github.com/conformal/btcwire"
"github.com/conformal/btcws"
"github.com/conformal/btcrpcclient"
"github.com/FactomProject/FactomCode/notaryapi"
)
// fee is paid to miner for tx written into btc
var fee btcutil.Amount
// balances store unspent balance & address & its WIF
var balances []balance
type balance struct {
unspentResult btcjson.ListUnspentResult
address btcutil.Address
wif *btcutil.WIF
}
// failedMerkles stores to-be-written-to-btc FactomBlock Hash
// after it failed for maxTrials attempt
var failedMerkles []*notaryapi.Hash
// maxTrials is the max attempts to writeToBTC
const maxTrials = 3
// the spentResult is the one showing up in ListSpent()
// but is already spent in blockexploer
// it's a bug in btcwallet
var spentResult = btcjson.ListUnspentResult {
TxId:"1a3450d99659d5b704d89c26d56082a0f13ba2a275fdd9ffc0ec4f42c88fe857",
Vout:0xb,
Address:"mvwnVraAK1VKRcbPgrSAVZ6E5hkqbwRxCy",
Account:"",
ScriptPubKey:"76a914a93c1baaaeae1b30688edab5e927fb2bfc794cae88ac",
RedeemScript:"",
Amount:1,
Confirmations:28247,
}
// ByAmount defines the methods needed to satisify sort.Interface to
// sort a slice of Utxos by their amount.
type ByAmount []btcjson.ListUnspentResult
func (u ByAmount) Len() int { return len(u) }
func (u ByAmount) Less(i, j int) bool { return u[i].Amount < u[j].Amount }
func (u ByAmount) Swap(i, j int) { u[i], u[j] = u[j], u[i] }
func writeToBTC(bytes []byte) (*btcwire.ShaHash, error) {
for attempts := 0; attempts < maxTrials; attempts++ {
txHash, err := SendRawTransactionToBTC(bytes)
if err != nil {
log.Printf("Attempt %d to send raw tx to BTC failed: %s\n", attempts, err)
time.Sleep(time.Duration(attempts*20) * time.Second)
continue
}
return txHash, nil
}
return nil, fmt.Errorf("Fail to write hash %s to BTC: %s", bytes)
}
func SendRawTransactionToBTC(hash []byte) (*btcwire.ShaHash, error) {
b := balances[0]
i := copy(balances, balances[1:])
balances[i] = b
//balances[0:] = balances[1:]
//balances[len(balances) - 1] = b
msgtx, err := createRawTransaction(b, hash)
if err != nil {
return nil, fmt.Errorf("cannot create Raw Transaction: %s", err)
}
shaHash, err := sendRawTransaction(msgtx)
if err != nil {
return nil, fmt.Errorf("cannot send Raw Transaction: %s", err)
}
return shaHash, nil
}
func unlockWallet(timeoutSecs int64) error {
err := wclient.WalletPassphrase(walletPassphrase, int64(timeoutSecs))
if err != nil {
return fmt.Errorf("cannot unlock wallet with passphrase: %s", err)
}
return nil
}
func initWallet() error {
fee, _ = btcutil.NewAmount(btcTransFee)
failedMerkles = make([]*notaryapi.Hash, 0, 100)
err := unlockWallet(int64(1))
if err != nil {
return fmt.Errorf("%s", err)
}
unspentResults, err := wclient.ListUnspent() //minConf=1
if err != nil {
return fmt.Errorf("cannot list unspent. %s", err)
}
//fmt.Println("unspentResults.len=", len(unspentResults))
balances = make([]balance, 0, 100)
if len(unspentResults) > 0 {
var i int
for _, b := range unspentResults {
//bypass the bug in btcwallet where one unconfirmed spent
//is listed in unspent result
if b.Amount > float64(0.1) && !compareUnspentResult(spentResult, b) {
//fmt.Println(i, " ", b.Amount)
balances = append(balances, balance{unspentResult : b})
i++
}
}
}
// fmt.Println("balances.len=", len(balances))
for i, b := range balances {
addr, err := btcutil.DecodeAddress(b.unspentResult.Address, &btcnet.TestNet3Params)
if err != nil {
return fmt.Errorf("cannot decode address: %s", err)
}
balances[i].address = addr
wif, err := wclient.DumpPrivKey(addr)
if err != nil {
return fmt.Errorf("cannot get WIF: %s", err)
}
balances[i].wif = wif
// fmt.Println(balances[i])
}
// registerNotifications()
time.Sleep(1 * time.Second)
return nil
}
func registerNotifications() {
// OnBlockConnected or OnBlockDisconnected
err := dclient.NotifyBlocks()
if err != nil {
fmt.Println("NotifyBlocks err: ", err.Error())
}
// OnTxAccepted: not useful since it covers all addresses
err = dclient.NotifyNewTransactions(false) //verbose is false
if err != nil {
fmt.Println("NotifyNewTransactions err: ", err.Error())
}
// OnRecvTx
addresses := make([]btcutil.Address, 0, 30)
for _, a := range balances {
addresses = append(addresses, a.address)
}
err = dclient.NotifyReceived(addresses)
if err != nil {
fmt.Println("NotifyReceived err: ", err.Error())
}
// OnRedeemingTx
//err := dclient.NotifySpent(outpoints)
}
func compareUnspentResult(a, b btcjson.ListUnspentResult) bool {
if a.TxId == b.TxId && a.Vout == b.Vout && a.Amount == b.Amount && a.Address == b.Address {
return true
} else {
return false
}
}
func createRawTransaction(b balance, hash []byte) (*btcwire.MsgTx, error) {
msgtx := btcwire.NewMsgTx()
if err := addTxOuts(msgtx, b, hash); err != nil {
return nil, fmt.Errorf("cannot addTxOuts: %s", err)
}
if err := addTxIn(msgtx, b); err != nil {
return nil, fmt.Errorf("cannot addTxIn: %s", err)
}
if err := validateMsgTx(msgtx, []btcjson.ListUnspentResult{b.unspentResult}); err != nil {
return nil, fmt.Errorf("cannot validateMsgTx: %s", err)
}
return msgtx, nil
}
func addTxIn(msgtx *btcwire.MsgTx, b balance) error {
output := b.unspentResult
// fmt.Printf("unspentResult: %#v\n", output)
prevTxHash, err := btcwire.NewShaHashFromStr(output.TxId)
if err != nil {
return fmt.Errorf("cannot get sha hash from str: %s", err)
}
outPoint := btcwire.NewOutPoint(prevTxHash, output.Vout)
msgtx.AddTxIn(btcwire.NewTxIn(outPoint, nil))
// OnRedeemingTx
err = dclient.NotifySpent([]*btcwire.OutPoint {outPoint})
if err != nil {
fmt.Println("NotifySpent err: ", err.Error())
}
subscript, err := hex.DecodeString(output.ScriptPubKey)
if err != nil {
return fmt.Errorf("cannot decode scriptPubKey: %s", err)
}
sigScript, err := btcscript.SignatureScript(msgtx, 0, subscript,
btcscript.SigHashAll, b.wif.PrivKey, true) //.ToECDSA(), true)
if err != nil {
return fmt.Errorf("cannot create scriptSig: %s", err)
}
msgtx.TxIn[0].SignatureScript = sigScript
return nil
}
func addTxOuts(msgtx *btcwire.MsgTx, b balance, hash []byte) error {
header := []byte{0x46, 0x61, 0x63, 0x74, 0x6f, 0x6d, 0x21, 0x21} // Factom!!
hash = append(header, hash...)
builder := btcscript.NewScriptBuilder()
builder.AddOp(btcscript.OP_RETURN)
builder.AddData(hash)
opReturn := builder.Script()
msgtx.AddTxOut(btcwire.NewTxOut(0, opReturn))
amount, _ := btcutil.NewAmount(b.unspentResult.Amount)
change := amount - fee
// Check if there are leftover unspent outputs, and return coins back to
// a new address we own.
if change > 0 {
// Spend change.
pkScript, err := btcscript.PayToAddrScript(b.address)
if err != nil {
return fmt.Errorf("cannot create txout script: %s", err)
}
msgtx.AddTxOut(btcwire.NewTxOut(int64(change), pkScript))
}
return nil
}
func selectInputs(eligible []btcjson.ListUnspentResult, minconf int) (selected []btcjson.ListUnspentResult, out btcutil.Amount, err error) {
// Iterate throguh eligible transactions, appending to outputs and
// increasing out. This is finished when out is greater than the
// requested amt to spend.
selected = make([]btcjson.ListUnspentResult, 0, len(eligible))
for _, e := range eligible {
amount, err := btcutil.NewAmount(e.Amount)
if err != nil {
fmt.Println("err in creating NewAmount")
continue
}
selected = append(selected, e)
out += amount
if out >= fee {
return selected, out, nil
}
}
if out < fee {
return nil, 0, fmt.Errorf("insufficient funds: transaction requires %v fee, but only %v spendable", fee, out)
}
return selected, out, nil
}
func validateMsgTx(msgtx *btcwire.MsgTx, inputs []btcjson.ListUnspentResult) error {
flags := btcscript.ScriptCanonicalSignatures | btcscript.ScriptStrictMultiSig
bip16 := time.Now().After(btcscript.Bip16Activation)
if bip16 {
flags |= btcscript.ScriptBip16
}
for i, txin := range msgtx.TxIn {
subscript, err := hex.DecodeString(inputs[i].ScriptPubKey)
if err != nil {
return fmt.Errorf("cannot decode scriptPubKey: %s", err)
}
engine, err := btcscript.NewScript(
txin.SignatureScript, subscript, i, msgtx, flags)
if err != nil {
return fmt.Errorf("cannot create script engine: %s", err)
}
if err = engine.Execute(); err != nil {
return fmt.Errorf("cannot validate transaction: %s", err)
}
}
return nil
}
func sendRawTransaction(msgtx *btcwire.MsgTx) (*btcwire.ShaHash, error) {
buf := bytes.Buffer{}
buf.Grow(msgtx.SerializeSize())
if err := msgtx.BtcEncode(&buf, btcwire.ProtocolVersion); err != nil {
// Hitting OOM by growing or writing to a bytes.Buffer already
// panics, and all returned errors are unexpected.
//panic(err) //?? should we have retry logic?
return nil, err
}
// use rpc client for btcd here for better callback info
// this should not require wallet to be unlocked
shaHash, err := dclient.SendRawTransaction(msgtx, false)
if err != nil {
return nil, fmt.Errorf("failed in rpcclient.SendRawTransaction: %s", err)
}
fmt.Println("btc txHash returned: ", shaHash) // new tx hash
return shaHash, nil
}
func createBtcwalletNotificationHandlers() btcrpcclient.NotificationHandlers {
ntfnHandlers := btcrpcclient.NotificationHandlers{
OnAccountBalance: func(account string, balance btcutil.Amount, confirmed bool) {
//go newBalance(account, balance, confirmed)
//fmt.Println("wclient: OnAccountBalance, account=", account, ", balance=",
// balance.ToUnit(btcutil.AmountBTC), ", confirmed=", confirmed)
},
OnWalletLockState: func(locked bool) {
fmt.Println("wclient: OnWalletLockState, locked=", locked)
},
OnUnknownNotification: func(method string, params []json.RawMessage) {
//fmt.Println("wclient: OnUnknownNotification: method=", method, "\nparams[0]=",
// string(params[0]), "\nparam[1]=", string(params[1]))
},
}
return ntfnHandlers
}
func createBtcdNotificationHandlers() btcrpcclient.NotificationHandlers {
ntfnHandlers := btcrpcclient.NotificationHandlers{
OnBlockConnected: func(hash *btcwire.ShaHash, height int32) {
//fmt.Println("dclient: OnBlockConnected: hash=", hash, ", height=", height)
//go newBlock(hash, height) // no need
},
OnRecvTx: func(transaction *btcutil.Tx, details *btcws.BlockDetails) {
//fmt.Printf("dclient: OnRecvTx: details=%#v\n", details)
//fmt.Printf("dclient: OnRecvTx: tx=%#v, tx.Sha=%#v, tx.index=%d\n",
//transaction, transaction.Sha().String(), transaction.Index())
},
OnRedeemingTx: func(transaction *btcutil.Tx, details *btcws.BlockDetails) {
fmt.Printf("dclient: OnRedeemingTx: details=%#v\n", details)
fmt.Printf("dclient: OnRedeemingTx: tx.Sha=%#v, tx.index=%d\n",
transaction.Sha().String(), transaction.Index())
if details != nil {
// do not block OnRedeemingTx callback
go saveDBBatch(transaction, details)
}
},
}
return ntfnHandlers
}
func saveDBBatch(transaction *btcutil.Tx, details *btcws.BlockDetails) {
fmt.Println("In saveDBBatch, len(dbBatches.batches)=", len(dbBatches.batches))
var i int
var found bool
for i = 0; i < len(dbBatches.batches); i++ {
fmt.Printf("i=%d, dbBatch=%#v\n", i, dbBatches.batches[i])
if dbBatches.batches[i].BTCTxHash != nil &&
bytes.Compare(dbBatches.batches[i].BTCTxHash.Bytes, transaction.Sha().Bytes()) == 0 {
dbBatches.batches[i].BTCTxOffset = details.Index
dbBatches.batches[i].BTCBlockHeight = details.Height
txHash, _ := btcwire.NewShaHashFromStr(details.Hash)
dbBatches.batches[i].BTCBlockHash = toHash(txHash)
found = true
break
}
}
fmt.Println("In saveFBBatch, found=", found)
if found {
fmt.Printf("found in dbBatches: i=%d, len=%d, DELETE dbBatch%#v\n",i , len(dbBatches.batches), dbBatches.batches[i])
//delete dbBatches.batches[i]
dbBatch := dbBatches.batches[i]
dbBatches.batchMutex.Lock()
dbBatches.batches = append(dbBatches.batches[:i], dbBatches.batches[i+1:] ...)
/*
copy(dbBatches.batches[i:], dbBatches.batches[i+1:])
dbBatches.batches[len(dbBatches.batches) - 1] = nil
dbBatches.batches = dbBatches.batches[:len(dbBatches.batches) - 1]
*/
dbBatches.batchMutex.Unlock()
// Update db with DBBatch
db.InsertDBBatch(dbBatch)
ExportDataFromDbToFile()
fmt.Println("found in dbBatches: after deletion, len=", len(dbBatches.batches))
}
}
func initRPCClient() error {
// Connect to local btcwallet RPC server using websockets.
ntfnHandlers := createBtcwalletNotificationHandlers()
certHomeDir := btcutil.AppDataDir(certHomePath, false)
certs, err := ioutil.ReadFile(filepath.Join(certHomeDir, "rpc.cert"))
if err != nil {
return fmt.Errorf("cannot read rpc.cert file: %s", err)
}
connCfg := &btcrpcclient.ConnConfig{
Host: rpcClientHost,
Endpoint: rpcClientEndpoint,
User: rpcClientUser,
Pass: rpcClientPass,
Certificates: certs,
}
wclient, err = btcrpcclient.New(connCfg, &ntfnHandlers)
if err != nil {
return fmt.Errorf("cannot create rpc client for btcwallet: %s", err)
}
// Connect to local btcd RPC server using websockets.
dntfnHandlers := createBtcdNotificationHandlers()
certHomeDir = btcutil.AppDataDir(certHomePathBtcd, false)
certs, err = ioutil.ReadFile(filepath.Join(certHomeDir, "rpc.cert"))
if err != nil {
return fmt.Errorf("cannot read rpc.cert file for btcd rpc server: %s", err)
}
dconnCfg := &btcrpcclient.ConnConfig{
Host: rpcBtcdHost,
Endpoint: rpcClientEndpoint,
User: rpcClientUser,
Pass: rpcClientPass,
Certificates: certs,
}
dclient, err = btcrpcclient.New(dconnCfg, &dntfnHandlers)
if err != nil {
return fmt.Errorf("cannot create rpc client for btcd: %s", err)
}
return nil
}
func shutdown() {
// For this example gracefully shutdown the client after 10 seconds.
// Ordinarily when to shutdown the client is highly application
// specific.
log.Println("Client shutdown in 2 seconds...")
time.AfterFunc(time.Second*2, func() {
log.Println("Going down...")
wclient.Shutdown()
dclient.Shutdown()
})
defer log.Println("Shutdown done!")
// Wait until the client either shuts down gracefully (or the user
// terminates the process with Ctrl+C).
wclient.WaitForShutdown()
dclient.WaitForShutdown()
}
func newEntryBlock(chain *notaryapi.EChain) (*notaryapi.EBlock){
// acquire the last block
block := chain.Blocks[len(chain.Blocks)-1]
if len(block.EBEntries) < 1{
//log.Println("No new entry found. No block created for chain: " + notaryapi.EncodeChainID(chain.ChainID))
return nil
}
// Create the block and add a new block for new coming entries
chain.BlockMutex.Lock()
blkhash, _ := notaryapi.CreateHash(block)
log.Println("blkhash:%v", blkhash.Bytes)
block.IsSealed = true
chain.NextBlockID++
newblock, _ := notaryapi.CreateBlock(chain, block, 10)
chain.Blocks = append(chain.Blocks, newblock)
chain.BlockMutex.Unlock()
block.EBHash = blkhash
// Create the Entry Block Merkle Root for FB Entry
hashes := make([]*notaryapi.Hash, 0, len(block.EBEntries)+1)
for _, entry := range block.EBEntries {
data, _ := entry.MarshalBinary()
hashes = append (hashes, notaryapi.Sha(data))
}
binaryEBHeader, _ := block.Header.MarshalBinary()
hashes = append (hashes, notaryapi.Sha(binaryEBHeader))
merkle := notaryapi.BuildMerkleTreeStore(hashes)
block.MerkleRoot = merkle[len(merkle) - 1] // MerkleRoot is not marshalized in Entry Block
fmt.Println("block.MerkleRoot:%v", block.MerkleRoot.String())
//Store the block in db
db.ProcessEBlockBatch(block)
log.Println("EntryBlock: block" + strconv.FormatUint(block.Header.BlockID, 10) +" created for chain: " + chain.ChainID.String())
return block
}
func newDirectoryBlock(chain *notaryapi.DChain) *notaryapi.DBlock {
// acquire the last block
block := chain.Blocks[len(chain.Blocks)-1]
if len(block.DBEntries) < 1{
//log.Println("No Directory block created for chain ... because no new entry is found.")
return nil
}
// Create the block add a new block for new coming entries
chain.BlockMutex.Lock()
block.Header.EntryCount = uint32(len(block.DBEntries))
// Calculate Merkle Root for FBlock and store it in header
if block.Header.MerkleRoot == nil {
block.Header.MerkleRoot = block.CalculateMerkleRoot()
fmt.Println("block.Header.MerkleRoot:%v", block.Header.MerkleRoot.String())
}
blkhash, _ := notaryapi.CreateHash(block)
block.IsSealed = true
chain.NextBlockID++
newblock, _ := notaryapi.CreateFBlock(chain, block, 10)
chain.Blocks = append(chain.Blocks, newblock)
chain.BlockMutex.Unlock()
//Store the block in db
block.DBHash = blkhash
db.ProcessDBlockBatch(block)
log.Println("DirectoryBlock: block" + strconv.FormatUint(block.Header.BlockID, 10) +" created for directory block chain: " + notaryapi.EncodeBinary(chain.ChainID))
//update FBBlock with FBHash & FBlockID
//block.FBlockID = block.Header.BlockID
//Export all db records associated w/ this new factom block
ExportDbToFile(blkhash)
return block
}
func saveDBBatchMerkleRoottoBTC(dbBatch *notaryapi.DBBatch) {
fmt.Println("in saveFBBatchMerkleRoottoBTC: len(dbBatch.DBlocks)=", len(dbBatch.DBlocks))
//calculate batch merkle root
hashes := make([]*notaryapi.Hash, 0, len(dbBatch.DBlocks))
for i:=0; i<len(dbBatch.DBlocks); i++ {
fmt.Printf("i=%d, merkle root: %s\n", i, dbBatch.DBlocks[i].Header.MerkleRoot.String())
hashes = append(hashes, dbBatch.DBlocks[i].Header.MerkleRoot)
}
merkle := notaryapi.BuildMerkleTreeStore(hashes)
merkleRoot := merkle[len(merkle) - 1]
dbBatch.FBBatchMerkleRoot = merkleRoot
txHash, err := writeToBTC(merkleRoot.Bytes)
if err != nil {
failedMerkles = append(failedMerkles, merkleRoot)
fmt.Println("failed to record ", merkleRoot.Bytes, " to BTC: ", err.Error())
}
//convert btc tx hash to factom hash, and update dbBatch
dbBatch.BTCTxHash = toHash(txHash)
fmt.Print("Recorded FBBatch merkle root in BTC tx hash:\n",txHash, "\nconverted hash: ", dbBatch.BTCTxHash.String(), "\n")
}
func toHash(txHash *btcwire.ShaHash) *notaryapi.Hash {
h := new (notaryapi.Hash)
h.SetBytes(txHash.Bytes())
return h
}