-
Notifications
You must be signed in to change notification settings - Fork 38
/
addresses.go
52 lines (46 loc) · 1.28 KB
/
addresses.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
package mempool
import (
"encoding/json"
"fmt"
"github.com/btcsuite/btcd/btcutil"
"github.com/btcsuite/btcd/chaincfg/chainhash"
"github.com/btcsuite/btcd/wire"
"github.com/vincentdebug/go-ord-tx/pkg/btcapi"
"net/http"
)
type UTXO struct {
Txid string `json:"txid"`
Vout int `json:"vout"`
Status struct {
Confirmed bool `json:"confirmed"`
BlockHeight int `json:"block_height"`
BlockHash string `json:"block_hash"`
BlockTime int64 `json:"block_time"`
} `json:"status"`
Value int64 `json:"value"`
}
// UTXOs is a slice of UTXO
type UTXOs []UTXO
func (c *MempoolClient) ListUnspent(address btcutil.Address) ([]*btcapi.UnspentOutput, error) {
res, err := c.request(http.MethodGet, fmt.Sprintf("/address/%s/utxo", address.EncodeAddress()), nil)
if err != nil {
return nil, err
}
var utxos UTXOs
err = json.Unmarshal(res, &utxos)
if err != nil {
return nil, err
}
unspentOutputs := make([]*btcapi.UnspentOutput, 0)
for _, utxo := range utxos {
txHash, err := chainhash.NewHashFromStr(utxo.Txid)
if err != nil {
return nil, err
}
unspentOutputs = append(unspentOutputs, &btcapi.UnspentOutput{
Outpoint: wire.NewOutPoint(txHash, uint32(utxo.Vout)),
Output: wire.NewTxOut(utxo.Value, address.ScriptAddress()),
})
}
return unspentOutputs, nil
}