@gobob/bob-sdk 1.0.1 → 1.0.3
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/electrs.d.ts +24 -0
- package/dist/electrs.js +59 -0
- package/dist/electrs.js.map +1 -0
- package/dist/index.d.ts +3 -0
- package/dist/index.js +20 -0
- package/dist/index.js.map +1 -0
- package/dist/relay.d.ts +14 -0
- package/dist/relay.js +36 -0
- package/dist/relay.js.map +1 -0
- package/dist/utils.d.ts +11 -0
- package/dist/utils.js +109 -0
- package/dist/utils.js.map +1 -0
- package/package.json +7 -2
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
export declare const MAINNET_ESPLORA_BASE_PATH = "https://btc-mainnet.interlay.io";
|
|
2
|
+
export declare const TESTNET_ESPLORA_BASE_PATH = "https://btc-testnet.interlay.io";
|
|
3
|
+
export declare const REGTEST_ESPLORA_BASE_PATH = "http://localhost:3002";
|
|
4
|
+
export interface MerkleProof {
|
|
5
|
+
blockHeight: number;
|
|
6
|
+
merkle: string;
|
|
7
|
+
pos: number;
|
|
8
|
+
}
|
|
9
|
+
export interface ElectrsClient {
|
|
10
|
+
getBlockHash(height: number): Promise<string>;
|
|
11
|
+
getBlockHeader(hash: string): Promise<string>;
|
|
12
|
+
getTransactionHex(txId: string): Promise<string>;
|
|
13
|
+
getMerkleProof(txId: string): Promise<MerkleProof>;
|
|
14
|
+
}
|
|
15
|
+
export declare class DefaultElectrsClient implements ElectrsClient {
|
|
16
|
+
private basePath;
|
|
17
|
+
constructor(networkOrUrl?: string);
|
|
18
|
+
getBlockHash(height: number): Promise<string>;
|
|
19
|
+
getBlockHeader(hash: string): Promise<string>;
|
|
20
|
+
getTransactionHex(txId: string): Promise<string>;
|
|
21
|
+
getMerkleProof(txId: string): Promise<MerkleProof>;
|
|
22
|
+
getJson<T>(url: string): Promise<T>;
|
|
23
|
+
getText(url: string): Promise<string>;
|
|
24
|
+
}
|
package/dist/electrs.js
ADDED
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.DefaultElectrsClient = exports.REGTEST_ESPLORA_BASE_PATH = exports.TESTNET_ESPLORA_BASE_PATH = exports.MAINNET_ESPLORA_BASE_PATH = void 0;
|
|
4
|
+
exports.MAINNET_ESPLORA_BASE_PATH = "https://btc-mainnet.interlay.io";
|
|
5
|
+
exports.TESTNET_ESPLORA_BASE_PATH = "https://btc-testnet.interlay.io";
|
|
6
|
+
exports.REGTEST_ESPLORA_BASE_PATH = "http://localhost:3002";
|
|
7
|
+
function encodeElectrsMerkleProof(merkle) {
|
|
8
|
+
return merkle.map(item => Buffer.from(item, "hex").reverse().toString("hex")).join('');
|
|
9
|
+
}
|
|
10
|
+
class DefaultElectrsClient {
|
|
11
|
+
constructor(networkOrUrl = "mainnet") {
|
|
12
|
+
switch (networkOrUrl) {
|
|
13
|
+
case "mainnet":
|
|
14
|
+
this.basePath = exports.MAINNET_ESPLORA_BASE_PATH;
|
|
15
|
+
break;
|
|
16
|
+
case "testnet":
|
|
17
|
+
this.basePath = exports.TESTNET_ESPLORA_BASE_PATH;
|
|
18
|
+
break;
|
|
19
|
+
case "regtest":
|
|
20
|
+
this.basePath = exports.REGTEST_ESPLORA_BASE_PATH;
|
|
21
|
+
break;
|
|
22
|
+
default:
|
|
23
|
+
this.basePath = networkOrUrl;
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
async getBlockHash(height) {
|
|
27
|
+
return this.getText(`${this.basePath}/block-height/${height}`);
|
|
28
|
+
}
|
|
29
|
+
async getBlockHeader(hash) {
|
|
30
|
+
return this.getText(`${this.basePath}/block/${hash}/header`);
|
|
31
|
+
}
|
|
32
|
+
async getTransactionHex(txId) {
|
|
33
|
+
return this.getText(`${this.basePath}/tx/${txId}/hex`);
|
|
34
|
+
}
|
|
35
|
+
async getMerkleProof(txId) {
|
|
36
|
+
const response = await this.getJson(`${this.basePath}/tx/${txId}/merkle-proof`);
|
|
37
|
+
return {
|
|
38
|
+
blockHeight: response.block_height,
|
|
39
|
+
merkle: encodeElectrsMerkleProof(response.merkle),
|
|
40
|
+
pos: response.pos,
|
|
41
|
+
};
|
|
42
|
+
}
|
|
43
|
+
async getJson(url) {
|
|
44
|
+
const response = await fetch(url);
|
|
45
|
+
if (!response.ok) {
|
|
46
|
+
throw new Error(response.statusText);
|
|
47
|
+
}
|
|
48
|
+
return await response.json();
|
|
49
|
+
}
|
|
50
|
+
async getText(url) {
|
|
51
|
+
const response = await fetch(url);
|
|
52
|
+
if (!response.ok) {
|
|
53
|
+
throw new Error(response.statusText);
|
|
54
|
+
}
|
|
55
|
+
return await response.text();
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
exports.DefaultElectrsClient = DefaultElectrsClient;
|
|
59
|
+
//# sourceMappingURL=electrs.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"electrs.js","sourceRoot":"","sources":["../src/electrs.ts"],"names":[],"mappings":";;;AAAa,QAAA,yBAAyB,GAAG,iCAAiC,CAAC;AAC9D,QAAA,yBAAyB,GAAG,iCAAiC,CAAC;AAC9D,QAAA,yBAAyB,GAAG,uBAAuB,CAAC;AA+BjE,SAAS,wBAAwB,CAAC,MAAgB;IAE9C,OAAO,MAAM,CAAC,GAAG,CAAC,IAAI,CAAC,EAAE,CAAC,MAAM,CAAC,IAAI,CAAC,IAAI,EAAE,KAAK,CAAC,CAAC,OAAO,EAAE,CAAC,QAAQ,CAAC,KAAK,CAAC,CAAC,CAAC,IAAI,CAAC,EAAE,CAAC,CAAC;AAC3F,CAAC;AAED,MAAa,oBAAoB;IAG7B,YAAY,eAAuB,SAAS;QACxC,QAAQ,YAAY,EAAE;YAClB,KAAK,SAAS;gBACV,IAAI,CAAC,QAAQ,GAAG,iCAAyB,CAAC;gBAC1C,MAAM;YACV,KAAK,SAAS;gBACV,IAAI,CAAC,QAAQ,GAAG,iCAAy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MAAM,KAAK,CAAC,GAAG,CAAC,CAAC;QAClC,IAAI,CAAC,QAAQ,CAAC,EAAE,EAAE;YACd,MAAM,IAAI,KAAK,CAAC,QAAQ,CAAC,UAAU,CAAC,CAAC;SACxC;QACD,OAAO,MAAM,QAAQ,CAAC,IAAI,EAAE,CAAC;IACjC,CAAC;CACJ;AA3DD,oDA2DC"}
|
package/dist/index.d.ts
ADDED
package/dist/index.js
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
+
};
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
__exportStar(require("./electrs"), exports);
|
|
18
|
+
__exportStar(require("./relay"), exports);
|
|
19
|
+
__exportStar(require("./utils"), exports);
|
|
20
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,4CAA0B;AAC1B,0CAAwB;AACxB,0CAAwB"}
|
package/dist/relay.d.ts
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { ElectrsClient } from "./electrs";
|
|
2
|
+
export interface BitcoinTxInfo {
|
|
3
|
+
version: string;
|
|
4
|
+
inputVector: string;
|
|
5
|
+
outputVector: string;
|
|
6
|
+
locktime: string;
|
|
7
|
+
}
|
|
8
|
+
export declare function getBitcoinTxInfo(electrsClient: ElectrsClient, txId: string): Promise<BitcoinTxInfo>;
|
|
9
|
+
export interface BitcoinTxProof {
|
|
10
|
+
merkleProof: string;
|
|
11
|
+
txIndexInBlock: number;
|
|
12
|
+
bitcoinHeaders: string;
|
|
13
|
+
}
|
|
14
|
+
export declare function getBitcoinTxProof(electrsClient: ElectrsClient, txId: string, txProofDifficultyFactor: number): Promise<BitcoinTxProof>;
|
package/dist/relay.js
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.getBitcoinTxProof = exports.getBitcoinTxInfo = void 0;
|
|
4
|
+
const bitcoinjs_lib_1 = require("bitcoinjs-lib");
|
|
5
|
+
const utils_1 = require("./utils");
|
|
6
|
+
async function getBitcoinTxInfo(electrsClient, txId) {
|
|
7
|
+
const txHex = await electrsClient.getTransactionHex(txId);
|
|
8
|
+
const tx = bitcoinjs_lib_1.Transaction.fromHex(txHex);
|
|
9
|
+
const versionBuffer = Buffer.allocUnsafe(4);
|
|
10
|
+
versionBuffer.writeInt32LE(tx.version);
|
|
11
|
+
const locktimeBuffer = Buffer.allocUnsafe(4);
|
|
12
|
+
locktimeBuffer.writeInt32LE(tx.locktime);
|
|
13
|
+
return {
|
|
14
|
+
version: versionBuffer.toString("hex"),
|
|
15
|
+
inputVector: (0, utils_1.encodeRawInput)(tx).toString("hex"),
|
|
16
|
+
outputVector: (0, utils_1.encodeRawOutput)(tx).toString("hex"),
|
|
17
|
+
locktime: locktimeBuffer.toString("hex")
|
|
18
|
+
};
|
|
19
|
+
}
|
|
20
|
+
exports.getBitcoinTxInfo = getBitcoinTxInfo;
|
|
21
|
+
async function getBitcoinTxProof(electrsClient, txId, txProofDifficultyFactor) {
|
|
22
|
+
const merkleProof = await electrsClient.getMerkleProof(txId);
|
|
23
|
+
const range = (start, end) => Array.from({ length: end - start }, (_element, index) => index + start);
|
|
24
|
+
const blockHeights = range(merkleProof.blockHeight, merkleProof.blockHeight + txProofDifficultyFactor);
|
|
25
|
+
const bitcoinHeaders = await Promise.all(blockHeights.map(async (height) => {
|
|
26
|
+
const hash = await electrsClient.getBlockHash(height);
|
|
27
|
+
return electrsClient.getBlockHeader(hash);
|
|
28
|
+
}));
|
|
29
|
+
return {
|
|
30
|
+
merkleProof: merkleProof.merkle,
|
|
31
|
+
txIndexInBlock: merkleProof.pos,
|
|
32
|
+
bitcoinHeaders: bitcoinHeaders.join(''),
|
|
33
|
+
};
|
|
34
|
+
}
|
|
35
|
+
exports.getBitcoinTxProof = getBitcoinTxProof;
|
|
36
|
+
//# sourceMappingURL=relay.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"relay.js","sourceRoot":"","sources":["../src/relay.ts"],"names":[],"mappings":";;;AAAA,iDAA4C;AAE5C,mCAA0D;AASnD,KAAK,UAAU,gBAAgB,CAClC,aAA4B,EAC5B,IAAY;IAEZ,MAAM,KAAK,GAAG,MAAM,aAAa,CAAC,iBAAiB,CAAC,IAAI,CAAC,CAAC;IAC1D,MAAM,EAAE,GAAG,2BAAW,CAAC,OAAO,CAAC,KAAK,CAAC,CAAC;IAEtC,MAAM,aAAa,GAAG,MAAM,CAAC,WAAW,CAAC,CAAC,CAAC,CAAC;IAC5C,aAAa,CAAC,YAAY,CAAC,EAAE,CAAC,OAAO,CAAC,CAAC;IAEvC,MAAM,cAAc,GAAG,MAAM,CAAC,WAAW,CAAC,CAAC,CAAC,CAAC;IAC7C,cAAc,CAAC,YAAY,CAAC,EAAE,CAAC,QAAQ,CAAC,CAAC;IAEzC,OAAO;QACH,OAAO,EAAE,aAAa,CAAC,QAAQ,CAAC,KAAK,CAA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reference types="node" />
|
|
2
|
+
import { Block } from "bitcoinjs-lib";
|
|
3
|
+
import { Transaction } from "bitcoinjs-lib/src/transaction";
|
|
4
|
+
export declare function encodeRawInput(tx: Transaction): Buffer;
|
|
5
|
+
export declare function encodeRawOutput(tx: Transaction): Buffer;
|
|
6
|
+
export declare function encodeRawWitness(tx: Transaction): Buffer;
|
|
7
|
+
export declare function getMerkleProof(block: Block, txHash: string, forWitness?: boolean): {
|
|
8
|
+
pos: number;
|
|
9
|
+
proof: string;
|
|
10
|
+
root: string;
|
|
11
|
+
};
|
package/dist/utils.js
ADDED
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.getMerkleProof = exports.encodeRawWitness = exports.encodeRawOutput = exports.encodeRawInput = void 0;
|
|
4
|
+
const bufferutils_1 = require("bitcoinjs-lib/src/bufferutils");
|
|
5
|
+
const crypto_1 = require("bitcoinjs-lib/src/crypto");
|
|
6
|
+
function varSliceSize(someScript) {
|
|
7
|
+
const length = someScript.length;
|
|
8
|
+
return bufferutils_1.varuint.encodingLength(length) + length;
|
|
9
|
+
}
|
|
10
|
+
function encodeRawInput(tx) {
|
|
11
|
+
const inputSize = bufferutils_1.varuint.encodingLength(tx.ins.length) + tx.ins.reduce((sum, input) => {
|
|
12
|
+
return sum + 40 + varSliceSize(input.script);
|
|
13
|
+
}, 0);
|
|
14
|
+
const inputBuffer = Buffer.allocUnsafe(inputSize);
|
|
15
|
+
const inputBufferWriter = new bufferutils_1.BufferWriter(inputBuffer, 0);
|
|
16
|
+
inputBufferWriter.writeVarInt(tx.ins.length);
|
|
17
|
+
tx.ins.forEach(txIn => {
|
|
18
|
+
inputBufferWriter.writeSlice(txIn.hash);
|
|
19
|
+
inputBufferWriter.writeUInt32(txIn.index);
|
|
20
|
+
inputBufferWriter.writeVarSlice(txIn.script);
|
|
21
|
+
inputBufferWriter.writeUInt32(txIn.sequence);
|
|
22
|
+
});
|
|
23
|
+
return inputBuffer;
|
|
24
|
+
}
|
|
25
|
+
exports.encodeRawInput = encodeRawInput;
|
|
26
|
+
function isOutput(out) {
|
|
27
|
+
return out.value !== undefined;
|
|
28
|
+
}
|
|
29
|
+
function encodeRawOutput(tx) {
|
|
30
|
+
const outputSize = bufferutils_1.varuint.encodingLength(tx.outs.length) + tx.outs.reduce((sum, output) => {
|
|
31
|
+
return sum + 8 + varSliceSize(output.script);
|
|
32
|
+
}, 0);
|
|
33
|
+
const outputBuffer = Buffer.allocUnsafe(outputSize);
|
|
34
|
+
const outputBufferWriter = new bufferutils_1.BufferWriter(outputBuffer, 0);
|
|
35
|
+
outputBufferWriter.writeVarInt(tx.outs.length);
|
|
36
|
+
tx.outs.forEach(txOut => {
|
|
37
|
+
if (isOutput(txOut)) {
|
|
38
|
+
outputBufferWriter.writeUInt64(txOut.value);
|
|
39
|
+
}
|
|
40
|
+
else {
|
|
41
|
+
outputBufferWriter.writeSlice(txOut.valueBuffer);
|
|
42
|
+
}
|
|
43
|
+
outputBufferWriter.writeVarSlice(txOut.script);
|
|
44
|
+
});
|
|
45
|
+
return outputBuffer;
|
|
46
|
+
}
|
|
47
|
+
exports.encodeRawOutput = encodeRawOutput;
|
|
48
|
+
function vectorSize(someVector) {
|
|
49
|
+
const length = someVector.length;
|
|
50
|
+
return (bufferutils_1.varuint.encodingLength(length) +
|
|
51
|
+
someVector.reduce((sum, witness) => {
|
|
52
|
+
return sum + varSliceSize(witness);
|
|
53
|
+
}, 0));
|
|
54
|
+
}
|
|
55
|
+
function encodeRawWitness(tx) {
|
|
56
|
+
const witnessSize = tx.ins.reduce((sum, input) => {
|
|
57
|
+
return sum + vectorSize(input.witness);
|
|
58
|
+
}, 0);
|
|
59
|
+
const witnessBuffer = Buffer.allocUnsafe(witnessSize);
|
|
60
|
+
const witnessBufferWriter = new bufferutils_1.BufferWriter(witnessBuffer, 0);
|
|
61
|
+
tx.ins.forEach(input => {
|
|
62
|
+
witnessBufferWriter.writeVector(input.witness);
|
|
63
|
+
});
|
|
64
|
+
return witnessBuffer;
|
|
65
|
+
}
|
|
66
|
+
exports.encodeRawWitness = encodeRawWitness;
|
|
67
|
+
function chunkArray(array, chunkSize) {
|
|
68
|
+
const chunkedArray = [];
|
|
69
|
+
let index = 0;
|
|
70
|
+
while (index < array.length) {
|
|
71
|
+
chunkedArray.push(array.slice(index, index + chunkSize));
|
|
72
|
+
index += chunkSize;
|
|
73
|
+
}
|
|
74
|
+
return chunkedArray;
|
|
75
|
+
}
|
|
76
|
+
function createMerkleBranchAndRoot(hashes, index) {
|
|
77
|
+
let merkle = [];
|
|
78
|
+
while (hashes.length > 1) {
|
|
79
|
+
if (hashes.length % 2 != 0) {
|
|
80
|
+
let last = hashes[hashes.length - 1];
|
|
81
|
+
hashes.push(last);
|
|
82
|
+
}
|
|
83
|
+
if (index % 2 == 0) {
|
|
84
|
+
index++;
|
|
85
|
+
}
|
|
86
|
+
else {
|
|
87
|
+
index--;
|
|
88
|
+
}
|
|
89
|
+
merkle.push(hashes[index]);
|
|
90
|
+
index = Math.floor(index / 2);
|
|
91
|
+
hashes = chunkArray(hashes, 2).map(pair => (0, crypto_1.hash256)(Buffer.concat([pair[0], pair[1]])));
|
|
92
|
+
}
|
|
93
|
+
return {
|
|
94
|
+
merkle,
|
|
95
|
+
root: hashes[0],
|
|
96
|
+
};
|
|
97
|
+
}
|
|
98
|
+
function getMerkleProof(block, txHash, forWitness) {
|
|
99
|
+
const txIds = block.transactions.map(tx => tx.getHash(forWitness));
|
|
100
|
+
const pos = txIds.map(value => value.toString("hex")).indexOf(txHash);
|
|
101
|
+
const merkleAndRoot = createMerkleBranchAndRoot(txIds, pos);
|
|
102
|
+
return {
|
|
103
|
+
pos: pos,
|
|
104
|
+
proof: merkleAndRoot.merkle.map(value => value.toString("hex")).join(''),
|
|
105
|
+
root: merkleAndRoot.root.toString("hex"),
|
|
106
|
+
};
|
|
107
|
+
}
|
|
108
|
+
exports.getMerkleProof = getM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AAC,CAAC,IAAI,CAAC,EAAE,CAAC;QACxE,IAAI,EAAE,aAAa,CAAC,IAAI,CAAC,QAAQ,CAAC,KAAK,CAAC;KAC3C,CAAC;AACN,CAAC;AAXD,wCAWC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@gobob/bob-sdk",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.3",
|
|
4
4
|
"main": "dist/index.js",
|
|
5
5
|
"types": "dist/index.d.ts",
|
|
6
6
|
"scripts": {
|
|
@@ -8,7 +8,12 @@
|
|
|
8
8
|
"build": "tsc -p tsconfig.json"
|
|
9
9
|
},
|
|
10
10
|
"files": [
|
|
11
|
-
"dist"
|
|
11
|
+
"dist/**/*",
|
|
12
|
+
"!**/*.spec.*",
|
|
13
|
+
"!**/*.json",
|
|
14
|
+
"CHANGELOG.md",
|
|
15
|
+
"LICENSE",
|
|
16
|
+
"README.md"
|
|
12
17
|
],
|
|
13
18
|
"devDependencies": {
|
|
14
19
|
"@types/chai": "^4.3.6",
|