-
Notifications
You must be signed in to change notification settings - Fork 41
/
extool.go
151 lines (134 loc) · 3.98 KB
/
extool.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
// Copyright (c) 2024 Hemi Labs, Inc.
// Use of this source code is governed by the MIT License,
// which can be found in the LICENSE file.
package main
import (
"context"
"encoding/hex"
"flag"
"fmt"
"log"
"os"
"strconv"
"time"
btcchainhash "github.com/btcsuite/btcd/chaincfg/chainhash"
"github.com/hemilabs/heminetwork/hemi/electrs"
"github.com/hemilabs/heminetwork/version"
)
func init() {
version.Component = "extool"
}
func main() {
ver := flag.Bool("v", false, "version")
flag.Parse()
if *ver {
fmt.Printf("v%v\n", version.String())
os.Exit(0)
}
address := flag.Arg(0)
if address == "" {
log.Fatal("No address specified")
}
c, err := electrs.NewClient(address, &electrs.ClientOptions{
InitialConnections: 1,
MaxConnections: 1,
})
if err != nil {
log.Fatalf("Failed to create electrs client: %v", err)
}
ctx, ctxCancel := context.WithTimeout(context.Background(), 10*time.Second)
defer ctxCancel()
switch cmd := flag.Arg(1); cmd {
case "balance":
scriptHash, err := btcchainhash.NewHashFromStr(flag.Arg(2))
if err != nil {
log.Fatalf("Invalid script hash %q: %v", flag.Arg(2), err)
}
balance, err := c.Balance(ctx, scriptHash[:])
if err != nil {
log.Fatalf("Failed to get balance: %v", err)
}
log.Printf("Balance for %v - confirmed %v, unconfirmed %v", scriptHash, balance.Confirmed, balance.Unconfirmed)
case "broadcast":
rtx, err := hex.DecodeString(flag.Arg(2))
if err != nil {
log.Fatalf("Invalid transaction %q: %v", flag.Arg(2), err)
}
txid, err := c.Broadcast(ctx, rtx)
if err != nil {
log.Fatalf("Failed to broadcast transaction: %v", err)
}
log.Printf("Transaction broadcast with hash %x", txid)
case "raw-block-header":
height, err := strconv.Atoi(flag.Arg(2))
if err != nil {
log.Fatalf("Invalid height %q: %v", flag.Arg(2), err)
}
rbh, err := c.RawBlockHeader(ctx, uint64(height))
if err != nil {
log.Fatalf("Failed to get raw block header: %v", err)
}
log.Printf("%x", rbh)
case "raw-transaction":
txHash, err := btcchainhash.NewHashFromStr(flag.Arg(2))
if err != nil {
log.Fatalf("Invalid transaction hash %q: %v", flag.Arg(2), err)
}
rtx, err := c.RawTransaction(ctx, txHash[:])
if err != nil {
log.Fatalf("Failed to get raw transaction: %v", err)
}
log.Printf("%x", rtx)
case "transaction":
txHash, err := btcchainhash.NewHashFromStr(flag.Arg(2))
if err != nil {
log.Fatalf("Invalid transaction hash %q: %v", flag.Arg(2), err)
}
txJSON, err := c.Transaction(ctx, txHash[:])
if err != nil {
log.Fatalf("Failed to get transaction: %v", err)
}
log.Printf("%v", string(txJSON))
case "transaction-at-position":
height, err := strconv.Atoi(flag.Arg(2))
if err != nil {
log.Fatalf("Invalid height %q: %v", flag.Arg(2), err)
}
index, err := strconv.Atoi(flag.Arg(3))
if err != nil {
log.Fatalf("Invalid index %q: %v", flag.Arg(3), err)
}
txh, merkleHashes, err := c.TransactionAtPosition(ctx, uint64(height), uint64(index))
if err != nil {
log.Fatalf("Failed to get transaction at position: %v (%T)", err, err)
}
txHash, err := btcchainhash.NewHash(txh)
if err != nil {
log.Fatalf("Failed to create BTC hash from TX hash: %v", err)
}
log.Printf("TX hash at height %v, index %v: %v", height, index, txHash)
log.Printf("Merkle hashes:")
for _, merkleHash := range merkleHashes {
log.Printf("%x", merkleHash)
}
case "utxos":
scriptHash, err := btcchainhash.NewHashFromStr(flag.Arg(2))
if err != nil {
log.Fatalf("Invalid script hash %q: %v", flag.Arg(2), err)
}
utxos, err := c.UTXOs(ctx, scriptHash[:])
if err != nil {
log.Fatalf("Failed to get utxos: %v", err)
}
log.Printf("Got %d utxos for %v", len(utxos), scriptHash)
for i, utxo := range utxos {
utxoHash, err := btcchainhash.NewHash(utxo.Hash)
if err != nil {
log.Fatalf("Failed to create BTC hash from UTXO hash: %v", err)
}
log.Printf("UTXO %d - %v:%v %v", i, utxoHash, utxo.Index, utxo.Value)
}
default:
log.Fatalf("Unknown command %q", cmd)
}
}