This repository has been archived by the owner on May 7, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
client.ts
451 lines (419 loc) · 17.7 KB
/
client.ts
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
import * as grpcWeb from "grpc-web";
import * as bchrpc from "../pb/bchrpc_pb";
import * as bchrpc_pb_service from "../pb/BchrpcServiceClientPb";
import * as util from "./util";
export default class GrpcClient {
public client: bchrpc_pb_service.bchrpcClient;
/**
* Create a client.
* @param url - The bchd server expressed as host:port.
* @param testnet - Whether testnet is being used, default:false.
* @param options - grpc client options.
*/
constructor({ url, testnet = false, options }:
{ url?: string; testnet?: boolean; options: null | { [index: string]: string; } }) {
if (typeof url === 'string') {
url = url
} else if (!url && !testnet) {
url = "https://bchd.fountainhead.cash:443";
// url = "https://bchd.greyh.at:8335";
} else if (!url) {
url = "https://bchd-testnet.greyh.at:18335";
}
if (!options) {
options = {
"grpc.max_receive_message_length": "-1", // unlimited
};
}
this.client = new bchrpc_pb_service.bchrpcClient(url, null, options);
}
/**
* Get information about transactions in mempool
*/
public getMempoolInfo(): Promise<bchrpc.GetMempoolInfoResponse> {
return new Promise((resolve, reject) => {
this.client.getMempoolInfo(
new bchrpc.GetMempoolInfoRequest(),
null,
(err: grpcWeb.Error, response: bchrpc.GetMempoolInfoResponse) => {
if (err !== null) { reject(err); } else {
resolve(response!);
}
}
);
});
}
/**
* Get transactions from mempool
* @param fullTransactions - A flag to return full transaction data. Default is `false`, only transaction hashes are returned.
*/
public getMempool(
{ fullTransactions }: { fullTransactions?: boolean }
): Promise<bchrpc.GetMempoolResponse> {
const req = new bchrpc.GetMempoolRequest();
if (fullTransactions) {
req.setFullTransactions(fullTransactions);
}
return new Promise((resolve, reject) => {
this.client.getMempool(
req,
null,
(err: grpcWeb.Error, response: bchrpc.GetMempoolResponse) => {
if (err !== null) { reject(err); } else { resolve(response!); }
}
);
});
}
/**
* Get a raw transaction
* @param hash - the hash, in either a base64 encoded string or byte array, little-endian.
* @param hashHex - the hash as a big-endian hexadecimal encoded string, sill be overridden by hash if provided.
*/
public getRawTransaction(
{ hash, hashHex }:
{ hash?: string | Uint8Array; hashHex?: string }
): Promise<bchrpc.GetRawTransactionResponse> {
const req = new bchrpc.GetRawTransactionRequest();
if (hashHex) {
req.setHash(util.hexToU8(hashHex).reverse());
} else if (hash) {
req.setHash(hash);
} else {
throw Error("No hash provided for transaction");
}
return new Promise((resolve, reject) => {
this.client.getRawTransaction(req, null, (err: grpcWeb.Error, response: bchrpc.GetRawTransactionResponse) => {
if (err !== null) { reject(err); } else { resolve(response!); }
});
});
}
/**
* Get a transaction
* @param hash - the hash, expressed in little-endian in either a base64 encoded string or byte array.
* @param hashHex - the hash as a big-endian hexadecimal encoded string, will be overridden by hash, if provided.
*/
public getTransaction(
{ hash, hashHex }:
{ hash?: string | Uint8Array; hashHex?: string }
): Promise<bchrpc.GetTransactionResponse> {
const req = new bchrpc.GetTransactionRequest();
if (hashHex) {
req.setHash(util.hexToU8(hashHex).reverse());
} else if (hash) {
req.setHash(hash);
} else {
throw Error("No hash provided for transaction");
}
return new Promise((resolve, reject) => {
this.client.getTransaction(req, null, (err: grpcWeb.Error, response: bchrpc.GetTransactionResponse) => {
if (err !== null) { reject(err); } else { resolve(response!); }
});
});
}
/**
* Get block header information
* @param blockLocatorHashes - Sparse list of hashes known to the client.
* @param stopHash -Last block hash to return.
*/
public getHeaders({ blockLocatorHashes, stopHash }:
{
blockLocatorHashes?: (string | Uint8Array)[],
stopHash?: string
}
): Promise<bchrpc.GetHeadersResponse> {
const req = new bchrpc.GetHeadersRequest();
if (blockLocatorHashes) {
req.setBlockLocatorHashesList(blockLocatorHashes);
}
if (stopHash) {
req.setStopHash(stopHash);
}
return new Promise((resolve, reject) => {
this.client.getHeaders(req, null, (err: grpcWeb.Error, response: bchrpc.GetHeadersResponse) => {
if (err !== null) { reject(err); } else { resolve(response!); }
});
});
}
/**
* Get transactions related to a particular address
* @param address - Bitcoin cash address in casharr format.
* @param nbSkip - Number of transactions to skip, in chronological order.
* @param nbFetch - Number of transactions return.
* @param height - Filter to only return transactions after this block number.
* @param hash - the hash, expressed in little-endian in either a base64 encoded string or byte array.
* @param hashHex - the hash as a big-endian hexadecimal encoded string, will be overridden by `hash`, if provided.
*/
public getAddressTransactions({ address, nbSkip, nbFetch, height, hashHex }:
{
address: string,
nbSkip?: number,
nbFetch?: number,
height?: number,
hashHex?: string
}
): Promise<bchrpc.GetAddressTransactionsResponse> {
const req = new bchrpc.GetAddressTransactionsRequest();
if (nbSkip) {
req.setNbSkip(nbSkip);
}
if (nbFetch) {
req.setNbFetch(nbFetch);
}
if (height) {
req.setHeight(height);
}
if (hashHex) {
req.setHash(util.hexToU8(hashHex).reverse());
}
req.setAddress(address);
return new Promise((resolve, reject) => {
this.client.getAddressTransactions(req, null, (err: grpcWeb.Error, response: bchrpc.GetAddressTransactionsResponse) => {
if (err !== null) { reject(err); } else { resolve(response!); }
});
});
}
public getUnspentOutput(
{ hash, hashHex, vout, includeMempool }:
{
hash?: string | Uint8Array, hashHex?: string, vout: number,
includeMempool?: boolean
}
): Promise<bchrpc.GetUnspentOutputResponse> {
const req = new bchrpc.GetUnspentOutputRequest();
if (includeMempool) {
req.setIncludeMempool(true);
}
if (hashHex) {
req.setHash(util.hexToU8(hashHex).reverse());
} else if (hash) {
req.setHash(hash);
}
req.setIndex(vout);
return new Promise((resolve, reject) => {
this.client.getUnspentOutput(req, null, (err, data) => {
if (err !== null) { reject(err); } else { resolve(data!); }
});
});
}
/**
* Retrieve merkle (SPV) proof that the given transaction is in the provided block.
* @param hash - the tx hash, in either a 'base64' encoded string or byte array, little-endian.
* @param hashHex - the tx hash as a big-endian 'hex' encoded string, will be overridden by hash if also provided.
*/
public getMerkleProof(
{ hash, hashHex }:
{
hash?: string | Uint8Array, hashHex?: string
}
): Promise<bchrpc.GetMerkleProofResponse> {
const req = new bchrpc.GetMerkleProofRequest();
if (hashHex) {
req.setTransactionHash(util.hexToU8(hashHex).reverse());
} else if (hash) {
req.setTransactionHash(hash);
}
return new Promise((resolve, reject) => {
this.client.getMerkleProof(req, null, (err, data) => {
if (err !== null) { reject(err); } else { resolve(data!); }
});
});
}
public getAddressUtxos(
{ address, includeMempool }:
{ address: string, includeMempool: boolean }
): Promise<bchrpc.GetAddressUnspentOutputsResponse> {
const req = new bchrpc.GetAddressUnspentOutputsRequest();
req.setAddress(address);
if (includeMempool) {
req.setIncludeMempool(true);
}
return new Promise((resolve, reject) => {
this.client.getAddressUnspentOutputs(req, null, (err, response) => {
if (err !== null) { reject(err); } else { resolve(response!); }
});
});
}
/**
* Retrieve raw block from a hash
* @param hash - the hash, in either a 'base64' encoded string or byte array, little-endian.
* @param hashHex - the hash as a big-endian 'hex' encoded string, will be overridden by hash if also provided.
*/
public getRawBlock(
{ hash, hashHex }:
{ hash?: string | Uint8Array, hashHex?: string },
): Promise<bchrpc.GetRawBlockResponse> {
const req = new bchrpc.GetRawBlockRequest();
if (hashHex) {
req.setHash(util.hexToU8(hashHex).reverse());
} else if (hash) {
req.setHash(hash);
} else {
throw Error("No hash provided for raw block request");
}
return new Promise((resolve, reject) => {
this.client.getRawBlock(req, null, (err, response) => {
if (err !== null) { reject(err); } else { resolve(response!); }
});
});
}
/**
* Retrieve block info given a block number or hash
* @param index - the block number to be retrieved.
* @param hash - the hash, in either a 'base64' encoded string or byte array, little-endian.
* @param hashHex - the hash as a big-endian 'hex' encoded string, will be overridden by hash if also provided.
* @param fullTransactions - a flag to return full transaction data, by defult `false` only transaction hashes are returned.
*/
public getBlock(
{ index, hash, hashHex, fullTransactions }:
{ index?: number, hash?: string | Uint8Array, hashHex?: string, fullTransactions?: boolean }
): Promise<bchrpc.GetBlockResponse> {
const req = new bchrpc.GetBlockRequest();
if (index !== null && index !== undefined) {
req.setHeight(index);
} else if (hashHex) {
req.setHash(util.hexToU8(hashHex).reverse());
} else if (hash) {
req.setHash(hash);
} else {
throw Error("No index or hash provided for block");
}
if (fullTransactions) {
req.setFullTransactions(true);
}
return new Promise((resolve, reject) => {
this.client.getBlock(req, null, (err, response) => {
if (err !== null) { reject(err); } else { resolve(response!); }
});
});
}
/**
* Retrieve block filter given a block number or hash
* @param height - the block number index to be retrieved.
* @param hash - the hash, expressed in little-endian in either a base64 encoded string or byte array.
* @param hashHex - the hash as a big-endian 'hex' encoded string, will be overridden a hash if provided.
*/
public getBlockFilter(
{ height, hash, hashHex }:
{ height?: number, hash?: string | Uint8Array, hashHex?: string }
): Promise<bchrpc.GetBlockFilterResponse> {
const req = new bchrpc.GetBlockInfoRequest();
if (height !== null && height !== undefined) { req.setHeight(height); } else if (hashHex) {
req.setHash(util.hexToU8(hashHex).reverse());
} else if (hash) {
req.setHash(hash);
}
return new Promise((resolve, reject) => {
this.client.getBlockFilter(req, null, (err, response) => {
if (err !== null) { reject(err); } else { resolve(response!); }
});
});
}
/**
* Retrieve block info given a block number or hash
* @param height - the block number index to be retrieved.
* @param hash - the hash, expressed in little-endian in either a base64 encoded string or byte array.
* @param hashHex - the hash as a big-endian 'hex' encoded string, will be overridden a hash if provided.
*/
public getBlockInfo(
{ height, hash, hashHex }:
{ height?: number, hash?: string | Uint8Array, hashHex?: string }
): Promise<bchrpc.GetBlockInfoResponse> {
const req = new bchrpc.GetBlockInfoRequest();
if (height !== null && height !== undefined) { req.setHeight(height); } else if (hashHex) {
req.setHash(util.hexToU8(hashHex).reverse());
} else if (hash) {
req.setHash(hash);
}
return new Promise((resolve, reject) => {
this.client.getBlockInfo(req, null, (err, response) => {
if (err !== null) { reject(err); } else { resolve(response!); }
});
});
}
/**
* Retrieve block info for the network, network state and host node.
*/
public getBlockchainInfo(
): Promise<bchrpc.GetBlockchainInfoResponse> {
return new Promise((resolve, reject) => {
this.client.getBlockchainInfo(new bchrpc.GetBlockchainInfoRequest(), null, (err, response) => {
if (err !== null) { reject(err); } else { resolve(response!); }
});
});
}
/**
* @param includeMempoolAcceptance - If true, new unconfirmed transactions from mempool are included apart from the ones confirmed in a block.
* @param includeBlockAcceptance - If true, transactions are included when they are confirmed. This notification is sent in addition to any requested mempool notifications.
* @param includeSerializedTxn - If true, transactions are serialized using bitcoin protocol encoding. Default is false, transaction will be Marshaled.
* @param filter - Transaction filter
* @param subscribeAllTransactions - If true, a filter will be constructed that captures all transactions
* @param unsubscribe - NOT IMPLEMENTED, see ClientReadableStream.cancel()
*/
public subscribeTransactions({ includeMempoolAcceptance,
includeBlockAcceptance,
includeSerializedTxn,
transactionFilter,
unsubscribe }:
{
includeMempoolAcceptance?: boolean, includeBlockAcceptance?: boolean, includeSerializedTxn?: boolean,
transactionFilter?: bchrpc.TransactionFilter, unsubscribe?: boolean
},
): Promise<grpcWeb.ClientReadableStream<bchrpc.TransactionNotification>> {
return new Promise((resolve, reject) => {
const req = new bchrpc.SubscribeTransactionsRequest();
includeMempoolAcceptance ? req.setIncludeMempool(true) : req.setIncludeMempool(false);
includeBlockAcceptance ? req.setIncludeInBlock(true) : req.setIncludeInBlock(false);
includeSerializedTxn ? req.setSerializeTx(true) : req.setSerializeTx(false);
if(transactionFilter){
transactionFilter.setAllTransactions(false)
req.setSubscribe(transactionFilter)
}else{
const defaultFilter = new bchrpc.TransactionFilter();
defaultFilter.setAllTransactions(true)
req.setSubscribe(defaultFilter)
}
if(unsubscribe){
throw new Error('Unsubscribing is not currently (2020) possible on grpc-web, see grpc-web ClientReadableStream.cancel()');
}
try {
resolve(this.client.subscribeTransactions(req));
} catch (err) {
reject(err);
}
});
}
public subscribeBlocks({ includeSerializedBlock, includeTxnHashes, includeTxnData }:
{ includeSerializedBlock?: boolean, includeTxnHashes?: boolean, includeTxnData?: boolean },
): Promise<grpcWeb.ClientReadableStream<bchrpc.BlockNotification>> {
return new Promise((resolve, reject) => {
const req = new bchrpc.SubscribeBlocksRequest();
includeTxnHashes ? req.setFullBlock(true) : req.setFullBlock(false);
includeTxnData ? req.setFullTransactions(true) : req.setFullTransactions(false);
includeSerializedBlock ? req.setSerializeBlock(true) : req.setSerializeBlock(false);
try {
resolve(this.client.subscribeBlocks(req));
} catch (err) {
reject(err);
}
});
}
public submitTransaction(
{ txnHex, txn }: { txnHex?: string, txn?: Uint8Array }
): Promise<bchrpc.SubmitTransactionResponse> {
let tx: string | Uint8Array;
const req = new bchrpc.SubmitTransactionRequest();
if (txnHex) {
tx = util.hexToU8(txnHex);
} else if (txn) {
tx = txn;
} else {
throw Error("Most provide either Hex string or Uint8Array");
}
req.setTransaction(tx);
return new Promise((resolve, reject) => {
this.client.submitTransaction(req, null, (err, response) => {
if (err !== null) { reject(err); } else { resolve(response!); }
});
});
}
}