@did-btcr2/bitcoin 0.3.4 → 0.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/cjs/bitcoin.js +83 -84
- package/dist/cjs/bitcoin.js.map +1 -1
- package/dist/cjs/client/http.js +9 -0
- package/dist/cjs/client/http.js.map +1 -0
- package/dist/cjs/client/rest/address.js +20 -28
- package/dist/cjs/client/rest/address.js.map +1 -1
- package/dist/cjs/client/rest/block.js +23 -22
- package/dist/cjs/client/rest/block.js.map +1 -1
- package/dist/cjs/client/rest/index.js +31 -84
- package/dist/cjs/client/rest/index.js.map +1 -1
- package/dist/cjs/client/rest/protocol.js +129 -0
- package/dist/cjs/client/rest/protocol.js.map +1 -0
- package/dist/cjs/client/rest/transaction.js +19 -21
- package/dist/cjs/client/rest/transaction.js.map +1 -1
- package/dist/cjs/client/rpc/index.js +75 -337
- package/dist/cjs/client/rpc/index.js.map +1 -1
- package/dist/cjs/client/rpc/interface.js +1 -6
- package/dist/cjs/client/rpc/interface.js.map +1 -1
- package/dist/cjs/client/rpc/json-rpc.js +40 -63
- package/dist/cjs/client/rpc/json-rpc.js.map +1 -1
- package/dist/cjs/client/rpc/protocol.js +134 -0
- package/dist/cjs/client/rpc/protocol.js.map +1 -0
- package/dist/cjs/constants.js +16 -5
- package/dist/cjs/constants.js.map +1 -1
- package/dist/cjs/errors.js.map +1 -1
- package/dist/cjs/index.js +21 -13
- package/dist/cjs/index.js.map +1 -1
- package/dist/cjs/types.js +5 -55
- package/dist/cjs/types.js.map +1 -1
- package/dist/esm/bitcoin.js +83 -84
- package/dist/esm/bitcoin.js.map +1 -1
- package/dist/esm/client/http.js +9 -0
- package/dist/esm/client/http.js.map +1 -0
- package/dist/esm/client/rest/address.js +20 -28
- package/dist/esm/client/rest/address.js.map +1 -1
- package/dist/esm/client/rest/block.js +23 -22
- package/dist/esm/client/rest/block.js.map +1 -1
- package/dist/esm/client/rest/index.js +31 -84
- package/dist/esm/client/rest/index.js.map +1 -1
- package/dist/esm/client/rest/protocol.js +129 -0
- package/dist/esm/client/rest/protocol.js.map +1 -0
- package/dist/esm/client/rest/transaction.js +19 -21
- package/dist/esm/client/rest/transaction.js.map +1 -1
- package/dist/esm/client/rpc/index.js +75 -337
- package/dist/esm/client/rpc/index.js.map +1 -1
- package/dist/esm/client/rpc/interface.js +1 -6
- package/dist/esm/client/rpc/interface.js.map +1 -1
- package/dist/esm/client/rpc/json-rpc.js +40 -63
- package/dist/esm/client/rpc/json-rpc.js.map +1 -1
- package/dist/esm/client/rpc/protocol.js +134 -0
- package/dist/esm/client/rpc/protocol.js.map +1 -0
- package/dist/esm/constants.js +16 -5
- package/dist/esm/constants.js.map +1 -1
- package/dist/esm/errors.js.map +1 -1
- package/dist/esm/index.js +21 -13
- package/dist/esm/index.js.map +1 -1
- package/dist/esm/types.js +5 -55
- package/dist/esm/types.js.map +1 -1
- package/dist/types/bitcoin.d.ts +66 -45
- package/dist/types/bitcoin.d.ts.map +1 -1
- package/dist/types/client/http.d.ts +21 -0
- package/dist/types/client/http.d.ts.map +1 -0
- package/dist/types/client/rest/address.d.ts +16 -19
- package/dist/types/client/rest/address.d.ts.map +1 -1
- package/dist/types/client/rest/block.d.ts +24 -16
- package/dist/types/client/rest/block.d.ts.map +1 -1
- package/dist/types/client/rest/index.d.ts +20 -51
- package/dist/types/client/rest/index.d.ts.map +1 -1
- package/dist/types/client/rest/protocol.d.ts +58 -0
- package/dist/types/client/rest/protocol.d.ts.map +1 -0
- package/dist/types/client/rest/transaction.d.ts +16 -17
- package/dist/types/client/rest/transaction.d.ts.map +1 -1
- package/dist/types/client/rpc/index.d.ts +105 -253
- package/dist/types/client/rpc/index.d.ts.map +1 -1
- package/dist/types/client/rpc/interface.d.ts +1 -4
- package/dist/types/client/rpc/interface.d.ts.map +1 -1
- package/dist/types/client/rpc/json-rpc.d.ts +20 -18
- package/dist/types/client/rpc/json-rpc.d.ts.map +1 -1
- package/dist/types/client/rpc/protocol.d.ts +82 -0
- package/dist/types/client/rpc/protocol.d.ts.map +1 -0
- package/dist/types/constants.d.ts +16 -5
- package/dist/types/constants.d.ts.map +1 -1
- package/dist/types/errors.d.ts +4 -3
- package/dist/types/errors.d.ts.map +1 -1
- package/dist/types/index.d.ts +20 -13
- package/dist/types/index.d.ts.map +1 -1
- package/dist/types/types.d.ts +98 -696
- package/dist/types/types.d.ts.map +1 -1
- package/package.json +3 -3
- package/src/bitcoin.ts +105 -118
- package/src/client/http.ts +28 -0
- package/src/client/rest/address.ts +24 -30
- package/src/client/rest/block.ts +29 -26
- package/src/client/rest/index.ts +38 -94
- package/src/client/rest/protocol.ts +153 -0
- package/src/client/rest/transaction.ts +23 -23
- package/src/client/rpc/index.ts +140 -379
- package/src/client/rpc/interface.ts +42 -51
- package/src/client/rpc/json-rpc.ts +53 -60
- package/src/client/rpc/protocol.ts +160 -0
- package/src/constants.ts +17 -5
- package/src/errors.ts +10 -3
- package/src/index.ts +81 -13
- package/src/types.ts +332 -972
package/src/client/rest/index.ts
CHANGED
|
@@ -1,129 +1,73 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { DEFAULT_BITCOIN_NETWORK_CONFIG } from '../../constants.js';
|
|
1
|
+
import { MethodError } from '@did-btcr2/common';
|
|
3
2
|
import { BitcoinAddress } from './address.js';
|
|
4
3
|
import { BitcoinBlock } from './block.js';
|
|
5
4
|
import { BitcoinTransaction } from './transaction.js';
|
|
6
|
-
import {
|
|
7
|
-
|
|
5
|
+
import { EsploraProtocol } from './protocol.js';
|
|
6
|
+
import { RestConfig } from '../../types.js';
|
|
7
|
+
import { HttpExecutor, HttpRequest, defaultHttpExecutor } from '../http.js';
|
|
8
8
|
|
|
9
9
|
/**
|
|
10
|
-
*
|
|
11
|
-
*
|
|
12
|
-
* @
|
|
10
|
+
* Esplora REST API client for Bitcoin.
|
|
11
|
+
*
|
|
12
|
+
* Wraps the sans-I/O {@link EsploraProtocol} with an {@link HttpExecutor}
|
|
13
|
+
* for convenience. Users who want full control over I/O can access the
|
|
14
|
+
* protocol layer directly via the {@link protocol} property.
|
|
13
15
|
*/
|
|
14
16
|
export class BitcoinRestClient {
|
|
15
|
-
|
|
16
|
-
* The encapsulated {@link RestClientConfig} object.
|
|
17
|
-
* @private
|
|
18
|
-
* @type {RestClientConfig}
|
|
19
|
-
*/
|
|
20
|
-
private _config: RestClientConfig;
|
|
17
|
+
private _config: RestConfig;
|
|
21
18
|
|
|
22
19
|
/**
|
|
23
|
-
* The
|
|
24
|
-
*
|
|
20
|
+
* The sans-I/O protocol layer. Use this to build {@link HttpRequest}
|
|
21
|
+
* descriptors without performing any I/O.
|
|
25
22
|
*/
|
|
23
|
+
readonly protocol: EsploraProtocol;
|
|
24
|
+
|
|
25
|
+
private readonly executor: HttpExecutor;
|
|
26
|
+
|
|
27
|
+
/** Transaction-related API calls. */
|
|
26
28
|
public transaction: BitcoinTransaction;
|
|
27
29
|
|
|
28
|
-
/**
|
|
29
|
-
* The api calls related to bitcoin blocks.
|
|
30
|
-
* @type {BitcoinBlock}
|
|
31
|
-
*/
|
|
30
|
+
/** Block-related API calls. */
|
|
32
31
|
public block: BitcoinBlock;
|
|
33
32
|
|
|
34
|
-
/**
|
|
35
|
-
* The api calls related to bitcoin addresses.
|
|
36
|
-
* @type {BitcoinAddress}
|
|
37
|
-
*/
|
|
33
|
+
/** Address-related API calls. */
|
|
38
34
|
public address: BitcoinAddress;
|
|
39
35
|
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
public api: (params: RestApiCallParams) => Promise<any> = this.call;
|
|
45
|
-
|
|
46
|
-
constructor(config: RestClientConfig){
|
|
47
|
-
this._config = new RestClientConfig(config);
|
|
48
|
-
this.api = this.call.bind(this);
|
|
49
|
-
this.transaction = new BitcoinTransaction(this.api);
|
|
50
|
-
this.block = new BitcoinBlock(this.api);
|
|
51
|
-
this.address = new BitcoinAddress(this.api);
|
|
52
|
-
}
|
|
36
|
+
constructor(config: RestConfig, executor?: HttpExecutor) {
|
|
37
|
+
this._config = config;
|
|
38
|
+
this.protocol = new EsploraProtocol(config);
|
|
39
|
+
this.executor = executor ?? defaultHttpExecutor;
|
|
53
40
|
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
get config(): RestClientConfig {
|
|
59
|
-
const config = this._config;
|
|
60
|
-
return config;
|
|
41
|
+
const exec = this.executeRequest.bind(this);
|
|
42
|
+
this.transaction = new BitcoinTransaction(this.protocol, exec);
|
|
43
|
+
this.block = new BitcoinBlock(this.protocol, exec);
|
|
44
|
+
this.address = new BitcoinAddress(this.protocol, exec);
|
|
61
45
|
}
|
|
62
46
|
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
*
|
|
66
|
-
* @param {RestClientConfig} config The configuration object for the client (optional).
|
|
67
|
-
* @returns {BitcoinRestClient} A new {@link BitcoinRestClient} instance.
|
|
68
|
-
* @example
|
|
69
|
-
* ```
|
|
70
|
-
* const rest = BitcoinRestClient.connect();
|
|
71
|
-
* ```
|
|
72
|
-
*/
|
|
73
|
-
public static connect(config?: RestClientConfig): BitcoinRestClient {
|
|
74
|
-
return new BitcoinRestClient(config ?? DEFAULT_BITCOIN_NETWORK_CONFIG.regtest.rest);
|
|
47
|
+
get config(): RestConfig {
|
|
48
|
+
return this._config;
|
|
75
49
|
}
|
|
76
50
|
|
|
77
51
|
/**
|
|
78
|
-
*
|
|
79
|
-
*
|
|
80
|
-
* @param {RestApiCallParams} params The parameters for the API call. See {@link RestApiCallParams} for details.
|
|
81
|
-
* @param {string} [params.path] The path to the API endpoint (required).
|
|
82
|
-
* @param {string} [params.url] The full URL to the API endpoint (optional).
|
|
83
|
-
* @param {string} [params.method] The HTTP method to use (default is 'GET').
|
|
84
|
-
* @param {any} [params.body] The body of the request (optional).
|
|
85
|
-
* @param {any} [params.headers] Additional headers to include in the request (optional).
|
|
86
|
-
* @returns {Promise<any>} A promise resolving to the response data.
|
|
52
|
+
* Execute an {@link HttpRequest} built by the protocol layer,
|
|
53
|
+
* parse the response, and throw on HTTP errors.
|
|
87
54
|
*/
|
|
88
|
-
private async
|
|
89
|
-
|
|
90
|
-
url ??= `${StringUtils.replaceEnd(this.config.host, '/')}${path}`;
|
|
91
|
-
|
|
92
|
-
// Set the method to GET if not provided
|
|
93
|
-
method ??= 'GET';
|
|
94
|
-
|
|
95
|
-
// Construct the request options
|
|
96
|
-
const requestInit = {
|
|
97
|
-
method,
|
|
98
|
-
headers : headers ?? {
|
|
99
|
-
'Content-Type' : 'application/json',
|
|
100
|
-
...this.config.headers,
|
|
101
|
-
}
|
|
102
|
-
} as any;
|
|
103
|
-
|
|
104
|
-
// If the method is POST or PUT, add the body to the request
|
|
105
|
-
if(body) {
|
|
106
|
-
requestInit.body = JSONUtils.isObject(body) ? JSON.stringify(body) : body;
|
|
107
|
-
requestInit.method = 'POST';
|
|
108
|
-
}
|
|
109
|
-
|
|
110
|
-
// Make the request
|
|
111
|
-
const response = await fetch(url, requestInit) as RestResponse;
|
|
55
|
+
private async executeRequest(request: HttpRequest): Promise<any> {
|
|
56
|
+
const response = await this.executor(request);
|
|
112
57
|
|
|
113
|
-
|
|
114
|
-
const data =
|
|
58
|
+
const contentType = response.headers.get('Content-Type') ?? '';
|
|
59
|
+
const data = contentType.includes('text/plain')
|
|
115
60
|
? await response.text()
|
|
116
61
|
: await response.json();
|
|
117
62
|
|
|
118
|
-
// Check if the response is ok (status in the range 200-299)
|
|
119
63
|
if (!response.ok) {
|
|
120
64
|
throw new MethodError(
|
|
121
|
-
`Request to ${url} failed: ${response.status} - ${response.statusText}`,
|
|
65
|
+
`Request to ${request.url} failed: ${response.status} - ${response.statusText}`,
|
|
122
66
|
'FAILED_HTTP_REQUEST',
|
|
123
|
-
{ data
|
|
67
|
+
{ data }
|
|
124
68
|
);
|
|
125
69
|
}
|
|
126
70
|
|
|
127
71
|
return data;
|
|
128
72
|
}
|
|
129
|
-
}
|
|
73
|
+
}
|
|
@@ -0,0 +1,153 @@
|
|
|
1
|
+
import { StringUtils } from '@did-btcr2/common';
|
|
2
|
+
import { RestConfig } from '../../types.js';
|
|
3
|
+
import { HttpRequest } from '../http.js';
|
|
4
|
+
|
|
5
|
+
const HEX64_RE = /^[0-9a-f]{64}$/i;
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Sans-I/O Esplora REST API protocol.
|
|
9
|
+
*
|
|
10
|
+
* Every method returns an {@link HttpRequest} descriptor — a plain object
|
|
11
|
+
* describing *what* to request — without performing any I/O. The caller
|
|
12
|
+
* is responsible for executing the request with an HTTP client of their
|
|
13
|
+
* choice and deserializing the response.
|
|
14
|
+
*
|
|
15
|
+
* This mirrors the pattern used by the Rust `esploda` crate where
|
|
16
|
+
* `Esplora` methods return `http::Request<()>` objects.
|
|
17
|
+
*
|
|
18
|
+
* @example
|
|
19
|
+
* ```ts
|
|
20
|
+
* const protocol = new EsploraProtocol({ host: 'https://mempool.space/api' });
|
|
21
|
+
*
|
|
22
|
+
* // Build a request descriptor (no I/O)
|
|
23
|
+
* const req = protocol.getTx('abc123...');
|
|
24
|
+
*
|
|
25
|
+
* // Execute with any HTTP client
|
|
26
|
+
* const res = await fetch(req.url, req);
|
|
27
|
+
* const tx: RawTransactionRest = await res.json();
|
|
28
|
+
* ```
|
|
29
|
+
*/
|
|
30
|
+
export class EsploraProtocol {
|
|
31
|
+
private readonly baseUrl: string;
|
|
32
|
+
private readonly defaultHeaders: Record<string, string>;
|
|
33
|
+
|
|
34
|
+
constructor(config: RestConfig) {
|
|
35
|
+
this.baseUrl = StringUtils.replaceEnd(config.host, '/');
|
|
36
|
+
this.defaultHeaders = {
|
|
37
|
+
'Content-Type' : 'application/json',
|
|
38
|
+
...config.headers,
|
|
39
|
+
};
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
// ── validation ─────────────────────────────────────────────────────
|
|
43
|
+
|
|
44
|
+
private static assertHex64(value: string, label: string): void {
|
|
45
|
+
if (!HEX64_RE.test(value)) {
|
|
46
|
+
throw new Error(`Invalid ${label}: expected 64-char hex string`);
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
private static assertAddress(value: string): void {
|
|
51
|
+
if (!value || /[/\\.?#]/.test(value)) {
|
|
52
|
+
throw new Error('Invalid address: contains illegal characters');
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
// ── helpers ──────────────────────────────────────────────────────────
|
|
57
|
+
|
|
58
|
+
private get(path: string): HttpRequest {
|
|
59
|
+
return {
|
|
60
|
+
url : `${this.baseUrl}${path}`,
|
|
61
|
+
method : 'GET',
|
|
62
|
+
headers : { ...this.defaultHeaders },
|
|
63
|
+
};
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
private post(path: string, body: string, headers?: Record<string, string>): HttpRequest {
|
|
67
|
+
return {
|
|
68
|
+
url : `${this.baseUrl}${path}`,
|
|
69
|
+
method : 'POST',
|
|
70
|
+
headers : headers ?? { ...this.defaultHeaders },
|
|
71
|
+
body,
|
|
72
|
+
};
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
// ── Transaction ──────────────────────────────────────────────────────
|
|
76
|
+
|
|
77
|
+
/** GET /tx/:txid */
|
|
78
|
+
getTx(txid: string): HttpRequest {
|
|
79
|
+
EsploraProtocol.assertHex64(txid, 'txid');
|
|
80
|
+
return this.get(`/tx/${txid}`);
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
/** GET /tx/:txid/hex */
|
|
84
|
+
getTxHex(txid: string): HttpRequest {
|
|
85
|
+
EsploraProtocol.assertHex64(txid, 'txid');
|
|
86
|
+
return this.get(`/tx/${txid}/hex`);
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
/** GET /tx/:txid/raw */
|
|
90
|
+
getTxRaw(txid: string): HttpRequest {
|
|
91
|
+
EsploraProtocol.assertHex64(txid, 'txid');
|
|
92
|
+
return this.get(`/tx/${txid}/raw`);
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
/** POST /tx */
|
|
96
|
+
postTx(hex: string): HttpRequest {
|
|
97
|
+
return this.post('/tx', hex, { 'Content-Type': 'text/plain' });
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
// ── Block ────────────────────────────────────────────────────────────
|
|
101
|
+
|
|
102
|
+
/** GET /blocks/tip/height */
|
|
103
|
+
getBlockTipHeight(): HttpRequest {
|
|
104
|
+
return this.get('/blocks/tip/height');
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
/** GET /block/:blockhash */
|
|
108
|
+
getBlock(blockhash: string): HttpRequest {
|
|
109
|
+
EsploraProtocol.assertHex64(blockhash, 'blockhash');
|
|
110
|
+
return this.get(`/block/${blockhash}`);
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
/** GET /block-height/:height */
|
|
114
|
+
getBlockHeight(height: number): HttpRequest {
|
|
115
|
+
return this.get(`/block-height/${height}`);
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
// ── Address ──────────────────────────────────────────────────────────
|
|
119
|
+
|
|
120
|
+
/** GET /address/:address/txs */
|
|
121
|
+
getAddressTxs(address: string): HttpRequest {
|
|
122
|
+
EsploraProtocol.assertAddress(address);
|
|
123
|
+
return this.get(`/address/${address}/txs`);
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
/** GET /address/:address/txs/mempool */
|
|
127
|
+
getAddressTxsMempool(address: string): HttpRequest {
|
|
128
|
+
EsploraProtocol.assertAddress(address);
|
|
129
|
+
return this.get(`/address/${address}/txs/mempool`);
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
/** GET /address/:address/txs/chain[/:last_seen_txid] */
|
|
133
|
+
getAddressTxsChain(address: string, lastSeenTxId?: string): HttpRequest {
|
|
134
|
+
EsploraProtocol.assertAddress(address);
|
|
135
|
+
if (lastSeenTxId) EsploraProtocol.assertHex64(lastSeenTxId, 'lastSeenTxId');
|
|
136
|
+
const path = lastSeenTxId
|
|
137
|
+
? `/address/${address}/txs/chain/${lastSeenTxId}`
|
|
138
|
+
: `/address/${address}/txs/chain`;
|
|
139
|
+
return this.get(path);
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
/** GET /address/:address */
|
|
143
|
+
getAddressInfo(address: string): HttpRequest {
|
|
144
|
+
EsploraProtocol.assertAddress(address);
|
|
145
|
+
return this.get(`/address/${address}`);
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
/** GET /address/:address/utxo */
|
|
149
|
+
getAddressUtxos(address: string): HttpRequest {
|
|
150
|
+
EsploraProtocol.assertAddress(address);
|
|
151
|
+
return this.get(`/address/${address}/utxo`);
|
|
152
|
+
}
|
|
153
|
+
}
|
|
@@ -1,28 +1,31 @@
|
|
|
1
1
|
import { Bytes } from '@did-btcr2/common';
|
|
2
|
-
import { RawTransactionRest
|
|
2
|
+
import { RawTransactionRest } from '../../types.js';
|
|
3
|
+
import { HttpRequest } from '../http.js';
|
|
4
|
+
import { EsploraProtocol } from './protocol.js';
|
|
3
5
|
|
|
4
6
|
export class BitcoinTransaction {
|
|
5
|
-
private
|
|
7
|
+
private readonly protocol: EsploraProtocol;
|
|
8
|
+
private readonly exec: (req: HttpRequest) => Promise<any>;
|
|
6
9
|
|
|
7
|
-
constructor(
|
|
8
|
-
this.
|
|
10
|
+
constructor(protocol: EsploraProtocol, exec: (req: HttpRequest) => Promise<any>) {
|
|
11
|
+
this.protocol = protocol;
|
|
12
|
+
this.exec = exec;
|
|
9
13
|
}
|
|
10
14
|
|
|
11
15
|
/**
|
|
12
16
|
* Returns the transaction in JSON format.
|
|
13
17
|
* See {@link https://github.com/blockstream/esplora/blob/master/API.md#get-txtxid | Esplora GET /tx/:txid } for details.
|
|
14
18
|
* @param {string} txid The transaction id (required).
|
|
15
|
-
* @returns {
|
|
19
|
+
* @returns {Promise<RawTransactionRest>} A promise resolving to data about a transaction.
|
|
16
20
|
*/
|
|
17
21
|
public async get(txid: string): Promise<RawTransactionRest> {
|
|
18
|
-
return await this.
|
|
22
|
+
return await this.exec(this.protocol.getTx(txid));
|
|
19
23
|
}
|
|
20
24
|
|
|
21
25
|
/**
|
|
22
|
-
*
|
|
23
|
-
* See {@link https://github.com/blockstream/esplora/blob/master/API.md#get-txtxid | Esplora GET /tx/:txid } for details.
|
|
26
|
+
* Checks if a transaction is confirmed.
|
|
24
27
|
* @param {string} txid The transaction id (required).
|
|
25
|
-
* @returns {
|
|
28
|
+
* @returns {Promise<boolean>} True if the transaction is confirmed.
|
|
26
29
|
*/
|
|
27
30
|
public async isConfirmed(txid: string): Promise<boolean> {
|
|
28
31
|
const tx = await this.get(txid);
|
|
@@ -30,35 +33,32 @@ export class BitcoinTransaction {
|
|
|
30
33
|
}
|
|
31
34
|
|
|
32
35
|
/**
|
|
33
|
-
* Returns the raw transaction
|
|
34
|
-
* See {@link https://github.com/blockstream/esplora/blob/master/API.md#get-txtxidhex | Esplora GET /tx/:txid/hex }
|
|
35
|
-
* {@link https://github.com/blockstream/esplora/blob/master/API.md#get-txtxidraw | Esplora GET /tx/:txid/raw } for details.
|
|
36
|
+
* Returns the raw transaction as a hex string.
|
|
37
|
+
* See {@link https://github.com/blockstream/esplora/blob/master/API.md#get-txtxidhex | Esplora GET /tx/:txid/hex } for details.
|
|
36
38
|
* @param {string} txid The transaction id (required).
|
|
37
|
-
* @returns {Promise<
|
|
39
|
+
* @returns {Promise<string>} A promise resolving to the raw transaction hex.
|
|
38
40
|
*/
|
|
39
41
|
public async getHex(txid: string): Promise<string> {
|
|
40
|
-
return await this.
|
|
42
|
+
return await this.exec(this.protocol.getTxHex(txid));
|
|
41
43
|
}
|
|
42
44
|
|
|
43
45
|
/**
|
|
44
|
-
* Returns the raw transaction
|
|
45
|
-
* See {@link https://github.com/blockstream/esplora/blob/master/API.md#get-
|
|
46
|
-
* {@link https://github.com/blockstream/esplora/blob/master/API.md#get-txtxidraw | Esplora GET /tx/:txid/raw } for details.
|
|
46
|
+
* Returns the raw transaction as binary data.
|
|
47
|
+
* See {@link https://github.com/blockstream/esplora/blob/master/API.md#get-txtxidraw | Esplora GET /tx/:txid/raw } for details.
|
|
47
48
|
* @param {string} txid The transaction id (required).
|
|
48
|
-
* @returns {Promise<
|
|
49
|
+
* @returns {Promise<Bytes>} A promise resolving to the raw transaction bytes.
|
|
49
50
|
*/
|
|
50
51
|
public async getRaw(txid: string): Promise<Bytes> {
|
|
51
|
-
return await this.
|
|
52
|
+
return await this.exec(this.protocol.getTxRaw(txid));
|
|
52
53
|
}
|
|
53
54
|
|
|
54
55
|
/**
|
|
55
|
-
* Broadcast a raw transaction to the network.
|
|
56
|
-
* will be returned on success.
|
|
56
|
+
* Broadcast a raw transaction to the network.
|
|
57
57
|
* See {@link https://github.com/blockstream/esplora/blob/master/API.md#post-tx | Esplora POST /tx } for details.
|
|
58
58
|
* @param {string} tx The raw transaction in hex format (required).
|
|
59
59
|
* @returns {Promise<string>} The transaction id of the broadcasted transaction.
|
|
60
60
|
*/
|
|
61
61
|
public async send(tx: string): Promise<string> {
|
|
62
|
-
return await this.
|
|
62
|
+
return await this.exec(this.protocol.postTx(tx));
|
|
63
63
|
}
|
|
64
|
-
}
|
|
64
|
+
}
|