-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
76 lines (63 loc) · 1.76 KB
/
main.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
package main
import (
"github.com/niktrix/bitcoin-lib/account"
"github.com/niktrix/bitcoin-lib/utils"
"log"
btcchain "github.com/btcsuite/btcd/chaincfg"
)
func main() {
var ut []utils.UTXO
//compressedKey := "cPmTTa8ctUckw7KYppLd1Vkx7jxjjRcpMqZ6Dm4n2FVfXRBRyirL" // compressed key
unCompressedKey := "5JsjKubviP3TDfNfbE3qdxKuNqqSVCctEF3jzyw26qYzonGEgsE" //uncompressed private key
isCompressed := false
chain := "testnet" // testnet || mainnet
chainConfig := &btcchain.TestNet3Params
switch chain {
case "testnet":
chainConfig = &btcchain.TestNet3Params
break
case "mainnet":
chainConfig = &btcchain.MainNetParams
break
}
destination := "<TO BTC ADDRESS>"
amount := int64(32000000000)
txFee := int64(1000)
acc, err := account.NewAccount(unCompressedKey, chainConfig, isCompressed)
if err != nil {
log.Fatalln(err)
}
log.Println("fromAddress", acc.Address)
log.Println("toAddress", destination)
log.Println("txFee", txFee)
log.Println("amount", amount)
btchelper := utils.NewBitPay(chain)
ut, err = btchelper.GetUnspentTxs(acc.Address.String())
if err != nil {
log.Fatalln("Error Getting unspent Tx", err)
return
}
if len(ut) == 0 {
log.Println(" No unspent Tx available")
return
}
transaction := utils.NewTx()
transaction.SetUnspentTxs(ut)
transaction.SetAmount(amount)
transaction.SetFee(txFee)
transaction.SetFrom(acc)
transaction.SetTo(destination)
transaction.SetConfig(chainConfig)
transaction.SetIsCompress(isCompressed)
err = transaction.Execute()
if err != nil {
log.Fatalln("Error Executing Tx", err)
}
rawtx := transaction.GetRaw()
log.Println("rawTx: ", rawtx)
response, err := btchelper.BroadCastTX(rawtx)
if err != nil {
log.Fatalln("Error Broadcasting Tx", err)
}
log.Println("response: ", response)
}