forked from checksum0/go-electrum
-
Notifications
You must be signed in to change notification settings - Fork 0
/
transaction.go
157 lines (129 loc) · 5.45 KB
/
transaction.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
package electrum
import "context"
// BroadcastTransaction sends a raw transaction to the remote server to
// be broadcasted on the server network.
// https://electrumx.readthedocs.io/en/latest/protocol-methods.html#blockchain-transaction-broadcast
func (s *Client) BroadcastTransaction(ctx context.Context, rawTx string) (string, error) {
resp := &basicResp{}
err := s.request(ctx, "blockchain.transaction.broadcast", []interface{}{rawTx}, &resp)
if err != nil {
return "", err
}
return resp.Result, nil
}
// GetTransactionResp represents the response to GetTransaction().
type GetTransactionResp struct {
Result *GetTransactionResult `json:"result"`
}
// GetTransactionResult represents the content of the result field in the response to GetTransaction().
type GetTransactionResult struct {
Blockhash string `json:"blockhash"`
Blocktime uint64 `json:"blocktime"`
Confirmations int32 `json:"confirmations"`
Hash string `json:"hash"`
Hex string `json:"hex"`
Locktime uint32 `json:"locktime"`
Size uint32 `json:"size"`
Time uint64 `json:"time"`
Version uint32 `json:"version"`
Vin []Vin `json:"vin"`
Vout []Vout `json:"vout"`
Merkle GetMerkleProofResult `json:"merkle,omitempty"` // For protocol v1.5 and up.
}
// Vin represents the input side of a transaction.
type Vin struct {
Coinbase string `json:"coinbase"`
ScriptSig *ScriptSig `json:"scriptsig"`
Sequence uint32 `json:"sequence"`
TxID string `json:"txid"`
Vout uint32 `json:"vout"`
}
// ScriptSig represents the signature script for that transaction input.
type ScriptSig struct {
Asm string `json:"asm"`
Hex string `json:"hex"`
}
// Vout represents the output side of a transaction.
type Vout struct {
N uint32 `json:"n"`
ScriptPubkey ScriptPubkey `json:"scriptpubkey"`
Value float64 `json:"value"`
}
// ScriptPubkey represents the script of that transaction output.
type ScriptPubkey struct {
Addresses []string `json:"addresses,omitempty"`
Asm string `json:"asm"`
Hex string `json:"hex,omitempty"`
ReqSigs uint32 `json:"reqsigs,omitempty"`
Type string `json:"type"`
}
// GetTransaction gets the detailed information for a transaction.
// https://electrumx.readthedocs.io/en/latest/protocol-methods.html#blockchain-transaction-get
func (s *Client) GetTransaction(ctx context.Context, txHash string) (*GetTransactionResult, error) {
var resp GetTransactionResp
err := s.request(ctx, "blockchain.transaction.get", []interface{}{txHash, true}, &resp)
if err != nil {
return nil, err
}
return resp.Result, nil
}
// GetRawTransaction gets a raw encoded transaction.
// https://electrumx.readthedocs.io/en/latest/protocol-methods.html#blockchain-transaction-get
func (s *Client) GetRawTransaction(ctx context.Context, txHash string) (string, error) {
var resp basicResp
err := s.request(ctx, "blockchain.transaction.get", []interface{}{txHash, false}, &resp)
if err != nil {
return "", err
}
return resp.Result, nil
}
// GetMerkleProofResp represents the response to GetMerkleProof().
type GetMerkleProofResp struct {
Result *GetMerkleProofResult `json:"result"`
}
// GetMerkleProofResult represents the content of the result field in the response to GetMerkleProof().
type GetMerkleProofResult struct {
Merkle []string `json:"merkle"`
Height uint32 `json:"block_height"`
Position uint32 `json:"pos"`
}
// GetMerkleProof returns the merkle proof for a confirmed transaction.
// https://electrumx.readthedocs.io/en/latest/protocol-methods.html#blockchain-transaction-get-merkle
func (s *Client) GetMerkleProof(ctx context.Context, txHash string, height uint32) (*GetMerkleProofResult, error) {
var resp GetMerkleProofResp
err := s.request(ctx, "blockchain.transaction.get_merkle", []interface{}{txHash, height}, &resp)
if err != nil {
return nil, err
}
return resp.Result, err
}
// GetHashFromPosition returns the transaction hash for a specific position in a block.
// https://electrumx.readthedocs.io/en/latest/protocol-methods.html#blockchain-transaction-id-from-pos
func (s *Client) GetHashFromPosition(ctx context.Context, height, position uint32) (string, error) {
var resp basicResp
err := s.request(ctx, "blockchain.transaction.id_from_pos", []interface{}{height, position, false}, &resp)
if err != nil {
return "", err
}
return resp.Result, err
}
// GetMerkleProofFromPosResp represents the response to GetMerkleProofFromPosition().
type GetMerkleProofFromPosResp struct {
Result *GetMerkleProofFromPosResult `json:"result"`
}
// GetMerkleProofFromPosResult represents the content of the result field in the response
// to GetMerkleProofFromPosition().
type GetMerkleProofFromPosResult struct {
Hash string `json:"tx_hash"`
Merkle []string `json:"merkle"`
}
// GetMerkleProofFromPosition returns the merkle proof for a specific position in a block.
// https://electrumx.readthedocs.io/en/latest/protocol-methods.html#blockchain-transaction-id-from-pos
func (s *Client) GetMerkleProofFromPosition(ctx context.Context, height, position uint32) (*GetMerkleProofFromPosResult, error) {
var resp GetMerkleProofFromPosResp
err := s.request(ctx, "blockchain.transaction.id_from_pos", []interface{}{height, position, true}, &resp)
if err != nil {
return nil, err
}
return resp.Result, err
}