@teleportdao/bitcoin 1.0.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/bitcoin-base.d.ts +56 -0
- package/dist/bitcoin-base.d.ts.map +1 -0
- package/dist/bitcoin-base.js +138 -0
- package/dist/bitcoin-base.js.map +1 -0
- package/dist/bitcoin-interface-utils.d.ts +18 -0
- package/dist/bitcoin-interface-utils.d.ts.map +1 -0
- package/dist/bitcoin-interface-utils.js +31 -0
- package/dist/bitcoin-interface-utils.js.map +1 -0
- package/dist/bitcoin-interface.d.ts +154 -0
- package/dist/bitcoin-interface.d.ts.map +1 -0
- package/dist/bitcoin-interface.js +248 -0
- package/dist/bitcoin-interface.js.map +1 -0
- package/dist/bitcoin-utils.d.ts +70 -0
- package/dist/bitcoin-utils.d.ts.map +1 -0
- package/dist/bitcoin-utils.js +388 -0
- package/dist/bitcoin-utils.js.map +1 -0
- package/dist/helper/burn-request-helper.d.ts +7 -0
- package/dist/helper/burn-request-helper.d.ts.map +1 -0
- package/dist/helper/burn-request-helper.js +26 -0
- package/dist/helper/burn-request-helper.js.map +1 -0
- package/dist/helper/teleport-request-helper.d.ts +45 -0
- package/dist/helper/teleport-request-helper.d.ts.map +1 -0
- package/dist/helper/teleport-request-helper.js +141 -0
- package/dist/helper/teleport-request-helper.js.map +1 -0
- package/dist/index.d.ts +7 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +15 -0
- package/dist/index.js.map +1 -0
- package/dist/sign/sign-transaction.d.ts +8 -0
- package/dist/sign/sign-transaction.d.ts.map +1 -0
- package/dist/sign/sign-transaction.js +41 -0
- package/dist/sign/sign-transaction.js.map +1 -0
- package/dist/teleport-dao-payments.d.ts +92 -0
- package/dist/teleport-dao-payments.d.ts.map +1 -0
- package/dist/teleport-dao-payments.js +203 -0
- package/dist/teleport-dao-payments.js.map +1 -0
- package/dist/transaction-builder/bitcoin-transaction-builder.d.ts +12 -0
- package/dist/transaction-builder/bitcoin-transaction-builder.d.ts.map +1 -0
- package/dist/transaction-builder/bitcoin-transaction-builder.js +50 -0
- package/dist/transaction-builder/bitcoin-transaction-builder.js.map +1 -0
- package/dist/transaction-builder/transaction-builder-common.d.ts +80 -0
- package/dist/transaction-builder/transaction-builder-common.d.ts.map +1 -0
- package/dist/transaction-builder/transaction-builder-common.js +170 -0
- package/dist/transaction-builder/transaction-builder-common.js.map +1 -0
- package/dist/transaction-builder/transaction-builder.d.ts +19 -0
- package/dist/transaction-builder/transaction-builder.d.ts.map +1 -0
- package/dist/transaction-builder/transaction-builder.js +130 -0
- package/dist/transaction-builder/transaction-builder.js.map +1 -0
- package/dist/utils/networks.d.ts +36 -0
- package/dist/utils/networks.d.ts.map +1 -0
- package/dist/utils/networks.js +30 -0
- package/dist/utils/networks.js.map +1 -0
- package/dist/utils/tools.d.ts +13 -0
- package/dist/utils/tools.d.ts.map +1 -0
- package/dist/utils/tools.js +65 -0
- package/dist/utils/tools.js.map +1 -0
- package/package.json +34 -0
- package/src/bitcoin-base.js +174 -0
- package/src/bitcoin-interface-utils.js +42 -0
- package/src/bitcoin-interface.js +267 -0
- package/src/bitcoin-utils.js +443 -0
- package/src/helper/burn-request-helper.js +27 -0
- package/src/helper/teleport-request-helper.js +162 -0
- package/src/index.js +15 -0
- package/src/sign/sign-transaction.js +36 -0
- package/src/teleport-dao-payments.js +276 -0
- package/src/transaction-builder/bitcoin-transaction-builder.js +37 -0
- package/src/transaction-builder/transaction-builder-common.js +228 -0
- package/src/transaction-builder/transaction-builder.js +135 -0
- package/src/utils/networks.js +31 -0
- package/src/utils/tools.js +72 -0
- package/tsconfig.json +9 -0
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
export = BitcoinBase;
|
|
2
|
+
declare class BitcoinBase {
|
|
3
|
+
static checkBalanceIsSufficient({ targets, extendedUtxo, changeAddress, feeRate, fullAmount, }: {
|
|
4
|
+
targets: any;
|
|
5
|
+
extendedUtxo: any;
|
|
6
|
+
changeAddress: any;
|
|
7
|
+
feeRate: any;
|
|
8
|
+
fullAmount?: boolean | undefined;
|
|
9
|
+
}): boolean;
|
|
10
|
+
constructor(networkName: any, connectionInfo?: {
|
|
11
|
+
api: {
|
|
12
|
+
enabled: boolean;
|
|
13
|
+
provider: string;
|
|
14
|
+
};
|
|
15
|
+
});
|
|
16
|
+
network: any;
|
|
17
|
+
hdWalletPath: any;
|
|
18
|
+
transactionBuilder: TransactionBuilder;
|
|
19
|
+
btcInterface: import("./bitcoin-interface");
|
|
20
|
+
signer: BitcoinSign;
|
|
21
|
+
currentAccount: string | null | undefined;
|
|
22
|
+
currentAccountType: string | null;
|
|
23
|
+
privateKey: Buffer | null;
|
|
24
|
+
publicKey: Buffer | null;
|
|
25
|
+
publicKeys: any[];
|
|
26
|
+
setMultiSigAccount(accountType?: string): void;
|
|
27
|
+
setAccountPrivateKey(privateKeyHex: any): void;
|
|
28
|
+
setAccountPublicKey(publicKeyHex: any): void;
|
|
29
|
+
setAccountPrivateKeyByMnemonic({ mnemonic, mnemonicPassword, index, walletNumber, addressType, }: {
|
|
30
|
+
mnemonic: any;
|
|
31
|
+
mnemonicPassword?: string | undefined;
|
|
32
|
+
index?: number | undefined;
|
|
33
|
+
walletNumber?: number | undefined;
|
|
34
|
+
addressType?: string | undefined;
|
|
35
|
+
}): void;
|
|
36
|
+
setAccount(accountType?: string): string | undefined;
|
|
37
|
+
addressObj: import("bitcoinjs-lib").Payment | undefined;
|
|
38
|
+
getExtendedUtxo({ address, addressType, publicKey, derivationPath }: {
|
|
39
|
+
address: string;
|
|
40
|
+
addressType: string;
|
|
41
|
+
publicKey: string | null;
|
|
42
|
+
derivationPath: string | null;
|
|
43
|
+
privateKeyId: string | null;
|
|
44
|
+
}): Promise<any>;
|
|
45
|
+
send({ receiverAddress, amount, fullAmount, speed }: {
|
|
46
|
+
receiverAddress: any;
|
|
47
|
+
amount: any;
|
|
48
|
+
fullAmount?: boolean | undefined;
|
|
49
|
+
speed?: string | undefined;
|
|
50
|
+
}): Promise<any>;
|
|
51
|
+
sendSignedPsbt(signedPsbt: any): Promise<any>;
|
|
52
|
+
sendMultiSignedPsbt(signedPsbts?: any[]): Promise<any>;
|
|
53
|
+
}
|
|
54
|
+
import TransactionBuilder = require("./transaction-builder/bitcoin-transaction-builder");
|
|
55
|
+
import BitcoinSign = require("./sign/sign-transaction");
|
|
56
|
+
//# sourceMappingURL=bitcoin-base.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"bitcoin-base.d.ts","sourceRoot":"","sources":["../src/bitcoin-base.js"],"names":[],"mappings":";AASA;IAyGE;;;;;;gBAmBC;IA3HD;;;;;OAwBC;IAfC,aAAoC;IACpC,kBAA6E;IAE7E,uCAA2F;IAC3F,4CAAwD;IAExD,oBAA2C;IAE3C,0CAA0B;IAC1B,kCAA8B;IAE9B,0BAAsB;IACtB,yBAAqB;IAErB,kBAAoB;IAGtB,+CAiBC;IAED,+CAEC;IAED,6CAEC;IAED;;;;;;aAkBC;IAED,qDASC;IAFC,wDAA4B;IAe9B;QAR8B,OAAO;QACP,WAAW;QACV,SAAS;QAET,cAAc;QACd,YAAY;qBAU1C;IAuBD;;;;;qBAuBC;IAED,8CAIC;IAED,uDAIC;CACF"}
|
|
@@ -0,0 +1,138 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
+
});
|
|
10
|
+
};
|
|
11
|
+
const bip39 = require("bip39");
|
|
12
|
+
const bip32 = require("bip32");
|
|
13
|
+
const hdWalletNetworksPath = require("@teleportdao/configs").hdWalletPath;
|
|
14
|
+
const networks = require("./utils/networks");
|
|
15
|
+
const TransactionBuilder = require("./transaction-builder/bitcoin-transaction-builder");
|
|
16
|
+
const BitcoinSign = require("./sign/sign-transaction");
|
|
17
|
+
class BitcoinBase {
|
|
18
|
+
constructor(networkName, connectionInfo = {
|
|
19
|
+
api: {
|
|
20
|
+
enabled: true,
|
|
21
|
+
provider: "BlockStream",
|
|
22
|
+
},
|
|
23
|
+
}) {
|
|
24
|
+
this.network = networks[networkName];
|
|
25
|
+
this.hdWalletPath = hdWalletNetworksPath[networkName.replace("_testnet", "")];
|
|
26
|
+
this.transactionBuilder = new TransactionBuilder(connectionInfo, networkName, this.network);
|
|
27
|
+
this.btcInterface = this.transactionBuilder.btcInterface;
|
|
28
|
+
this.signer = new BitcoinSign(this.network);
|
|
29
|
+
this.currentAccount = null;
|
|
30
|
+
this.currentAccountType = null;
|
|
31
|
+
this.privateKey = null;
|
|
32
|
+
this.publicKey = null;
|
|
33
|
+
this.publicKeys = [];
|
|
34
|
+
}
|
|
35
|
+
setMultiSigAccount(accountType = "p2sh") {
|
|
36
|
+
switch (accountType) {
|
|
37
|
+
default:
|
|
38
|
+
throw new Error("accountType is incorrect");
|
|
39
|
+
}
|
|
40
|
+
this.currentAccountType = accountType;
|
|
41
|
+
}
|
|
42
|
+
setAccountPrivateKey(privateKeyHex) {
|
|
43
|
+
this.privateKey = Buffer.from(privateKeyHex, "hex");
|
|
44
|
+
}
|
|
45
|
+
setAccountPublicKey(publicKeyHex) {
|
|
46
|
+
this.publicKey = Buffer.from(publicKeyHex, "hex");
|
|
47
|
+
}
|
|
48
|
+
setAccountPrivateKeyByMnemonic({ mnemonic, mnemonicPassword = "", index = 0, walletNumber = 0, addressType = "p2sh-p2wpkh", }) {
|
|
49
|
+
if (!bip39.validateMnemonic(mnemonic))
|
|
50
|
+
throw new Error("invalid mnemonic");
|
|
51
|
+
const seed = bip39.mnemonicToSeedSync(mnemonic, mnemonicPassword);
|
|
52
|
+
const node = bip32.fromSeed(seed);
|
|
53
|
+
if (!this.hdWalletPath[addressType])
|
|
54
|
+
throw new Error("incorrect path or addressType");
|
|
55
|
+
const path = `${this.hdWalletPath[addressType]}/${walletNumber}`;
|
|
56
|
+
const account = node.derivePath(path);
|
|
57
|
+
const userKeyPair = account.derive(index);
|
|
58
|
+
this.setAccountPrivateKey(userKeyPair.privateKey.toString("hex"));
|
|
59
|
+
this.setAccountPublicKey(userKeyPair.publicKey.toString("hex"));
|
|
60
|
+
}
|
|
61
|
+
setAccount(accountType = "p2pkh") {
|
|
62
|
+
let addressObj = this.transactionBuilder.createAddressObject({
|
|
63
|
+
addressType: accountType,
|
|
64
|
+
publicKey: this.publicKey,
|
|
65
|
+
});
|
|
66
|
+
this.currentAccount = addressObj.address;
|
|
67
|
+
this.currentAccountType = accountType;
|
|
68
|
+
this.addressObj = addressObj;
|
|
69
|
+
return addressObj.address;
|
|
70
|
+
}
|
|
71
|
+
getExtendedUtxo({ address, addressType, publicKey, derivationPath }) {
|
|
72
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
73
|
+
return this.transactionBuilder.getExtendedUtxo({
|
|
74
|
+
address,
|
|
75
|
+
addressType,
|
|
76
|
+
publicKey,
|
|
77
|
+
derivationPath,
|
|
78
|
+
});
|
|
79
|
+
});
|
|
80
|
+
}
|
|
81
|
+
static checkBalanceIsSufficient({ targets, extendedUtxo, changeAddress, feeRate, fullAmount = false, }) {
|
|
82
|
+
try {
|
|
83
|
+
TransactionBuilder.helperHandleInputsAndOutputs({
|
|
84
|
+
targets,
|
|
85
|
+
extendedUtxo,
|
|
86
|
+
changeAddress,
|
|
87
|
+
feeRate,
|
|
88
|
+
fullAmount,
|
|
89
|
+
});
|
|
90
|
+
return true;
|
|
91
|
+
}
|
|
92
|
+
catch (err) {
|
|
93
|
+
return false;
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
send({ receiverAddress, amount, fullAmount = false, speed = "normal" }) {
|
|
97
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
98
|
+
let extendedUtxo = yield this.getExtendedUtxo({
|
|
99
|
+
address: this.currentAccount,
|
|
100
|
+
addressType: this.currentAccountType,
|
|
101
|
+
publicKey: this.publicKey.toString("hex"),
|
|
102
|
+
});
|
|
103
|
+
let feeRate = yield this.transactionBuilder._getFeeRate(speed);
|
|
104
|
+
let unsignedTx = yield this.transactionBuilder.processUnsignedTransaction({
|
|
105
|
+
extendedUtxo,
|
|
106
|
+
targets: [
|
|
107
|
+
{
|
|
108
|
+
address: receiverAddress,
|
|
109
|
+
value: amount,
|
|
110
|
+
},
|
|
111
|
+
],
|
|
112
|
+
changeAddress: this.currentAccount,
|
|
113
|
+
feeRate,
|
|
114
|
+
fullAmount,
|
|
115
|
+
});
|
|
116
|
+
let signedPsbt = yield this.signer.signPsbt(unsignedTx, this.privateKey);
|
|
117
|
+
let signedTx = this.signer.finalizePsbts([signedPsbt]);
|
|
118
|
+
let txId = yield this.transactionBuilder.sendTx(signedTx);
|
|
119
|
+
return txId;
|
|
120
|
+
});
|
|
121
|
+
}
|
|
122
|
+
sendSignedPsbt(signedPsbt) {
|
|
123
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
124
|
+
let signedTx = this.signer.finalizePsbts([signedPsbt]);
|
|
125
|
+
let txId = yield this.transactionBuilder.sendTx(signedTx);
|
|
126
|
+
return txId;
|
|
127
|
+
});
|
|
128
|
+
}
|
|
129
|
+
sendMultiSignedPsbt(signedPsbts = []) {
|
|
130
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
131
|
+
let signedTx = this.signer.finalizePsbts(signedPsbts);
|
|
132
|
+
let txId = yield this.transactionBuilder.sendTx(signedTx);
|
|
133
|
+
return txId;
|
|
134
|
+
});
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
module.exports = BitcoinBase;
|
|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AAI,CAAC,kBAAkB,CAAC,MAAM,CAAC,QAAQ,CAAC,CAAA;YACzD,OAAO,IAAI,CAAA;QACb,CAAC;KAAA;CACF;AAED,MAAM,CAAC,OAAO,GAAG,WAAW,CAAA"}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
export = BitcoinInterfaceUtils;
|
|
2
|
+
declare class BitcoinInterfaceUtils {
|
|
3
|
+
constructor(networkName: any);
|
|
4
|
+
testnet: any;
|
|
5
|
+
network: any;
|
|
6
|
+
convertHashToAddress(hashHex: any, addressType: any): string | undefined;
|
|
7
|
+
convertAddressToScript(address: any): {
|
|
8
|
+
script: Buffer | undefined;
|
|
9
|
+
hash: Buffer | undefined;
|
|
10
|
+
addressType: "p2wpkh" | "p2sh" | "p2pkh";
|
|
11
|
+
};
|
|
12
|
+
convertAddressToObject(address: any): {
|
|
13
|
+
addressObject: import("bitcoinjs-lib").Payment;
|
|
14
|
+
addressType: "p2wpkh" | "p2sh" | "p2pkh";
|
|
15
|
+
};
|
|
16
|
+
createAddressObjectByPublicKey(address: any, addressType: any): import("bitcoinjs-lib").Payment;
|
|
17
|
+
}
|
|
18
|
+
//# sourceMappingURL=bitcoin-interface-utils.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"bitcoin-interface-utils.d.ts","sourceRoot":"","sources":["../src/bitcoin-interface-utils.js"],"names":[],"mappings":";AAOA;IACE,8BAGC;IAFC,aAA+C;IAC/C,aAAoC;IAGtC,yEAMC;IAED;;;;MAOC;IAED;;;MAGC;IAED,gGAGC;CACF"}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
const networks = require("./utils/networks");
|
|
3
|
+
const { createAddressObjectByHash, createAddressObjectByAddress, createAddressObjectByPublicKey, } = require("./bitcoin-utils");
|
|
4
|
+
class BitcoinInterfaceUtils {
|
|
5
|
+
constructor(networkName) {
|
|
6
|
+
this.testnet = networkName.includes("_testnet");
|
|
7
|
+
this.network = networks[networkName];
|
|
8
|
+
}
|
|
9
|
+
convertHashToAddress(hashHex, addressType) {
|
|
10
|
+
let addressObj = createAddressObjectByHash({ addressType, hash: Buffer.from(hashHex, "hex") }, this.network);
|
|
11
|
+
return addressObj.address;
|
|
12
|
+
}
|
|
13
|
+
convertAddressToScript(address) {
|
|
14
|
+
let { addressObject, addressType } = createAddressObjectByAddress(address, this.network);
|
|
15
|
+
return {
|
|
16
|
+
script: addressObject.output,
|
|
17
|
+
hash: addressObject.hash,
|
|
18
|
+
addressType,
|
|
19
|
+
};
|
|
20
|
+
}
|
|
21
|
+
convertAddressToObject(address) {
|
|
22
|
+
let addObj = createAddressObjectByAddress(address, this.network);
|
|
23
|
+
return addObj;
|
|
24
|
+
}
|
|
25
|
+
createAddressObjectByPublicKey(address, addressType) {
|
|
26
|
+
let addObj = createAddressObjectByPublicKey({ address, addressType }, this.network);
|
|
27
|
+
return addObj;
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
module.exports = BitcoinInterfaceUtils;
|
|
31
|
+
//# sourceMappingURL=bitcoin-interface-utils.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"bitcoin-interface-utils.js","sourceRoot":"","sources":["../src/bitcoin-interface-utils.js"],"names":[],"mappings":";AAAA,MAAM,QAAQ,GAAG,OAAO,CAAC,kBAAkB,CAAC,CAAA;AAC5C,MAAM,EACJ,yBAAyB,EACzB,4BAA4B,EAC5B,8BAA8B,GAC/B,GAAG,OAAO,CAAC,iBAAiB,CAAC,CAAA;AAE9B,MAAM,qBAAqB;IACzB,YAAY,WAAW;QACrB,IAAI,CAAC,OAAO,GAAG,WAAW,CAAC,QAAQ,CAAC,UAAU,CAAC,CAAA;QAC/C,IAAI,CAAC,OAAO,GAAG,QAAQ,CAAC,WAAW,CAAC,CAAA;IACtC,CAAC;IAED,oBAAoB,CAAC,OAAO,EAAE,WAAW;QACvC,IAAI,UAAU,GAAG,yBAAyB,CACxC,EAAE,WAAW,EAAE,IAAI,EAAE,MAAM,CAAC,IAAI,CAAC,OAAO,EAAE,KAAK,CAAC,EAAE,EAClD,IAAI,CAAC,OAAO,CACb,CAAA;QACD,OAAO,UAAU,CAAC,OAAO,CAAA;IAC3B,CAAC;IAED,sBAAsB,CAAC,OAAO;QAC5B,IAAI,EAAE,aAAa,EAAE,WAAW,EAAE,GAAG,4BAA4B,CAAC,OAAO,EAAE,IAAI,CAAC,OAAO,CAAC,CAAA;QACxF,OAAO;YACL,MAAM,EAAE,aAAa,CAAC,MAAM;YAC5B,IAAI,EAAE,aAAa,CAAC,IAAI;YACxB,WAAW;SACZ,CAAA;IACH,CAAC;IAED,sBAAsB,CAAC,OAAO;QAC5B,IAAI,MAAM,GAAG,4BAA4B,CAAC,OAAO,EAAE,IAAI,CAAC,OAAO,CAAC,CAAA;QAChE,OAAO,MAAM,CAAA;IACf,CAAC;IAED,8BAA8B,CAAC,OAAO,EAAE,WAAW;QACjD,IAAI,MAAM,GAAG,8BAA8B,CAAC,EAAE,OAAO,EAAE,WAAW,EAAE,EAAE,IAAI,CAAC,OAAO,CAAC,CAAA;QACnF,OAAO,MAAM,CAAA;IACf,CAAC;CACF;AAED,MAAM,CAAC,OAAO,GAAG,qBAAqB,CAAA"}
|
|
@@ -0,0 +1,154 @@
|
|
|
1
|
+
export = BitcoinInterface;
|
|
2
|
+
declare class BitcoinInterface extends BitcoinInterfaceUtils {
|
|
3
|
+
constructor(connectionInfo: any, networkName: any, config?: {
|
|
4
|
+
minTeleporterFeeAmount: number;
|
|
5
|
+
});
|
|
6
|
+
rpcProvider: import("@teleportdao/providers/dist/bitcoin/rpc") | undefined;
|
|
7
|
+
apiProviderName: any;
|
|
8
|
+
apiProvider: any;
|
|
9
|
+
minTeleporterFeeAmount: number;
|
|
10
|
+
provider: any;
|
|
11
|
+
getLatestBlockNumber(): Promise<any>;
|
|
12
|
+
getBlockHash(blockNumber: any): Promise<any>;
|
|
13
|
+
getBlockHeaderHex(blockNumber: any): Promise<any>;
|
|
14
|
+
getTransaction(txId: any): Promise<any>;
|
|
15
|
+
getFeeRate(speed?: string): Promise<any>;
|
|
16
|
+
getHexBlockHeaders(startBlockNumber: any, endBlockNumber: any): Promise<{
|
|
17
|
+
hexBlockHeaders: string;
|
|
18
|
+
fromBlockNumber: any;
|
|
19
|
+
toBlockNumber: any;
|
|
20
|
+
difficulty: any;
|
|
21
|
+
}[]>;
|
|
22
|
+
getRequestProof(transaction: any): Promise<{
|
|
23
|
+
parsedTx: {
|
|
24
|
+
version: any;
|
|
25
|
+
flag: any;
|
|
26
|
+
vin: string;
|
|
27
|
+
vout: string;
|
|
28
|
+
witness: string;
|
|
29
|
+
locktime: string;
|
|
30
|
+
};
|
|
31
|
+
merkleProof: any;
|
|
32
|
+
blockNumber: any;
|
|
33
|
+
blockHash: any;
|
|
34
|
+
}>;
|
|
35
|
+
getMerkleProof(txId: any, blockHash: any): Promise<{
|
|
36
|
+
intermediateNodes: any;
|
|
37
|
+
transactionIndex: any;
|
|
38
|
+
}>;
|
|
39
|
+
getAddressesUtxo(addresses: any): Promise<any>;
|
|
40
|
+
getBalance(address: any): Promise<any>;
|
|
41
|
+
getBlockTransactions(addresses: any, blockNumber: any): Promise<any[]>;
|
|
42
|
+
getMultipleBlocksTransactions(addresses: any, startBlockNumber: any, endBlockNumber: any): Promise<any>;
|
|
43
|
+
getTransactionHistory(addresses: any, startBlockNumber: any, endBlockNumber: any): Promise<any>;
|
|
44
|
+
getMempoolTransactionHistory(addresses: any): Promise<any>;
|
|
45
|
+
getTeleporterRequests(addresses: any, startblockNumber: any, endBlockNumber: any, mempool?: boolean): Promise<{
|
|
46
|
+
requests: {
|
|
47
|
+
transaction: any;
|
|
48
|
+
request: {
|
|
49
|
+
status: boolean;
|
|
50
|
+
data: {
|
|
51
|
+
requestType: string;
|
|
52
|
+
chainId: number;
|
|
53
|
+
appId: number;
|
|
54
|
+
recipientAddress: string;
|
|
55
|
+
percentageFee: number;
|
|
56
|
+
speed: boolean;
|
|
57
|
+
exchangeTokenAddress: string;
|
|
58
|
+
outputAmount: number;
|
|
59
|
+
deadline: number;
|
|
60
|
+
isFixedToken: boolean;
|
|
61
|
+
};
|
|
62
|
+
message?: undefined;
|
|
63
|
+
code?: undefined;
|
|
64
|
+
} | {
|
|
65
|
+
status: boolean;
|
|
66
|
+
message: string;
|
|
67
|
+
code: string;
|
|
68
|
+
data?: undefined;
|
|
69
|
+
} | {
|
|
70
|
+
status: boolean;
|
|
71
|
+
data: {
|
|
72
|
+
requestType: string;
|
|
73
|
+
chainId: number;
|
|
74
|
+
appId: number;
|
|
75
|
+
recipientAddress: string;
|
|
76
|
+
percentageFee: number;
|
|
77
|
+
mode: number;
|
|
78
|
+
tokenAddress: string;
|
|
79
|
+
borrowAmount: number;
|
|
80
|
+
};
|
|
81
|
+
message?: undefined;
|
|
82
|
+
code?: undefined;
|
|
83
|
+
} | {
|
|
84
|
+
status: boolean;
|
|
85
|
+
data: any;
|
|
86
|
+
value: any;
|
|
87
|
+
message?: undefined;
|
|
88
|
+
code?: undefined;
|
|
89
|
+
};
|
|
90
|
+
lockerAddress: any;
|
|
91
|
+
lockerLockingScript: any;
|
|
92
|
+
}[];
|
|
93
|
+
invalidRequests: {
|
|
94
|
+
transaction: any;
|
|
95
|
+
request: {
|
|
96
|
+
status: boolean;
|
|
97
|
+
data: {
|
|
98
|
+
requestType: string;
|
|
99
|
+
chainId: number;
|
|
100
|
+
appId: number;
|
|
101
|
+
recipientAddress: string;
|
|
102
|
+
percentageFee: number;
|
|
103
|
+
speed: boolean;
|
|
104
|
+
exchangeTokenAddress: string;
|
|
105
|
+
outputAmount: number;
|
|
106
|
+
deadline: number;
|
|
107
|
+
isFixedToken: boolean;
|
|
108
|
+
};
|
|
109
|
+
message?: undefined;
|
|
110
|
+
code?: undefined;
|
|
111
|
+
} | {
|
|
112
|
+
status: boolean;
|
|
113
|
+
message: string;
|
|
114
|
+
code: string;
|
|
115
|
+
data?: undefined;
|
|
116
|
+
} | {
|
|
117
|
+
status: boolean;
|
|
118
|
+
data: {
|
|
119
|
+
requestType: string;
|
|
120
|
+
chainId: number;
|
|
121
|
+
appId: number;
|
|
122
|
+
recipientAddress: string;
|
|
123
|
+
percentageFee: number;
|
|
124
|
+
mode: number;
|
|
125
|
+
tokenAddress: string;
|
|
126
|
+
borrowAmount: number;
|
|
127
|
+
};
|
|
128
|
+
message?: undefined;
|
|
129
|
+
code?: undefined;
|
|
130
|
+
} | {
|
|
131
|
+
status: boolean;
|
|
132
|
+
data: any;
|
|
133
|
+
value: any;
|
|
134
|
+
message?: undefined;
|
|
135
|
+
code?: undefined;
|
|
136
|
+
};
|
|
137
|
+
lockerAddress: any;
|
|
138
|
+
lockerLockingScript: any;
|
|
139
|
+
}[];
|
|
140
|
+
}>;
|
|
141
|
+
getLockersBurnTransactions(addresses: any, startBlockNumber: any, endBlockNumber: any, mempool?: boolean): Promise<{
|
|
142
|
+
transaction: any;
|
|
143
|
+
burnInfo: {
|
|
144
|
+
receivers: any[];
|
|
145
|
+
changes: any[];
|
|
146
|
+
totalInputValue: any;
|
|
147
|
+
lockerVin: any;
|
|
148
|
+
};
|
|
149
|
+
lockerAddress: any;
|
|
150
|
+
lockerLockingScript: any;
|
|
151
|
+
}[]>;
|
|
152
|
+
}
|
|
153
|
+
import BitcoinInterfaceUtils = require("./bitcoin-interface-utils");
|
|
154
|
+
//# sourceMappingURL=bitcoin-interface.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"bitcoin-interface.d.ts","sourceRoot":"","sources":["../src/bitcoin-interface.js"],"names":[],"mappings":";AAYA;IACE;;OAeC;IAZG,2EAAqD;IAMrD,qBAAkD;IAClD,iBAAkE;IAGpE,+BAA2D;IAC3D,cAAoD;IAMtD,qCAGC;IAED,6CAGC;IAED,kDAGC;IAED,wCAEC;IAGD,yCAMC;IAKD;;;;;SAkCC;IAED;;;;;;;;;;;;OAoBC;IAED;;;OAMC;IAKD,+CAWC;IAED,uCAMC;IAID,uEAYC;IAGD,wGAaC;IAGD,gGAcC;IAGD,2DAMC;IAID;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;OAwBC;IAED;;;;;;;;;;SAuBC;CACF"}
|
|
@@ -0,0 +1,248 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
+
});
|
|
10
|
+
};
|
|
11
|
+
const { getRpcProvider, getApiProvider } = require("@teleportdao/providers").bitcoin;
|
|
12
|
+
const { runWithRetries, sleep } = require("./utils/tools");
|
|
13
|
+
const { parseRawTransaction, calculateMerkleProof, parseBlockHeader, extractTransactionsAndBlockInfoFromRawBlock, } = require("./bitcoin-utils");
|
|
14
|
+
const { checkAndParseProtocolRequest } = require("./helper/teleport-request-helper");
|
|
15
|
+
const { getBurnTransactionInfo } = require("./helper/burn-request-helper");
|
|
16
|
+
const BitcoinInterfaceUtils = require("./bitcoin-interface-utils");
|
|
17
|
+
class BitcoinInterface extends BitcoinInterfaceUtils {
|
|
18
|
+
constructor(connectionInfo, networkName, config = { minTeleporterFeeAmount: 0 }) {
|
|
19
|
+
var _a;
|
|
20
|
+
super(networkName);
|
|
21
|
+
if ((_a = connectionInfo.rpc) === null || _a === void 0 ? void 0 : _a.enabled) {
|
|
22
|
+
this.rpcProvider = getRpcProvider(connectionInfo.rpc);
|
|
23
|
+
}
|
|
24
|
+
else if (connectionInfo.api.provider !== "BlockStream") {
|
|
25
|
+
throw new Error("if rpc is disabled, we just support BlockStream as api provider");
|
|
26
|
+
}
|
|
27
|
+
if (connectionInfo.api.enabled) {
|
|
28
|
+
this.apiProviderName = connectionInfo.api.provider;
|
|
29
|
+
this.apiProvider = getApiProvider(connectionInfo.api, networkName);
|
|
30
|
+
}
|
|
31
|
+
this.minTeleporterFeeAmount = config.minTeleporterFeeAmount;
|
|
32
|
+
this.provider = this.rpcProvider || this.apiProvider;
|
|
33
|
+
}
|
|
34
|
+
getLatestBlockNumber() {
|
|
35
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
36
|
+
let latestHeight = yield this.provider.getLatestBlockNumber();
|
|
37
|
+
return latestHeight;
|
|
38
|
+
});
|
|
39
|
+
}
|
|
40
|
+
getBlockHash(blockNumber) {
|
|
41
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
42
|
+
let headerHash = yield runWithRetries(() => this.provider.getBlockHash(blockNumber));
|
|
43
|
+
return headerHash;
|
|
44
|
+
});
|
|
45
|
+
}
|
|
46
|
+
getBlockHeaderHex(blockNumber) {
|
|
47
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
48
|
+
let headerHex = yield runWithRetries(() => this.provider.getBlockHeaderHex(blockNumber));
|
|
49
|
+
return headerHex;
|
|
50
|
+
});
|
|
51
|
+
}
|
|
52
|
+
getTransaction(txId) {
|
|
53
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
54
|
+
return this.provider.getTransaction(txId);
|
|
55
|
+
});
|
|
56
|
+
}
|
|
57
|
+
getFeeRate(speed = "normal") {
|
|
58
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
59
|
+
if (!(speed === "normal" || speed === "slow" || speed === "fast")) {
|
|
60
|
+
throw new Error("incorrect speed");
|
|
61
|
+
}
|
|
62
|
+
let fee = yield this.provider.getFeeRate(speed);
|
|
63
|
+
return fee;
|
|
64
|
+
});
|
|
65
|
+
}
|
|
66
|
+
getHexBlockHeaders(startBlockNumber, endBlockNumber) {
|
|
67
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
68
|
+
const blockHeaders = [];
|
|
69
|
+
let difficulty = null;
|
|
70
|
+
let hexBlockHeaders = "";
|
|
71
|
+
let fromBlockNumber = startBlockNumber;
|
|
72
|
+
for (let blockNumber = startBlockNumber; blockNumber <= endBlockNumber; blockNumber += 1) {
|
|
73
|
+
let blockHeader = yield this.getBlockHeaderHex(blockNumber);
|
|
74
|
+
console.log("block", blockNumber);
|
|
75
|
+
let parsedBlockHeader = parseBlockHeader(blockHeader);
|
|
76
|
+
if (difficulty && parsedBlockHeader.difficulty !== difficulty) {
|
|
77
|
+
blockHeaders.push({
|
|
78
|
+
hexBlockHeaders,
|
|
79
|
+
fromBlockNumber,
|
|
80
|
+
toBlockNumber: blockNumber - 1,
|
|
81
|
+
difficulty,
|
|
82
|
+
});
|
|
83
|
+
hexBlockHeaders = blockHeader;
|
|
84
|
+
fromBlockNumber = blockNumber;
|
|
85
|
+
}
|
|
86
|
+
else {
|
|
87
|
+
hexBlockHeaders += blockHeader;
|
|
88
|
+
}
|
|
89
|
+
difficulty = parsedBlockHeader.difficulty;
|
|
90
|
+
}
|
|
91
|
+
if (hexBlockHeaders) {
|
|
92
|
+
blockHeaders.push({
|
|
93
|
+
hexBlockHeaders,
|
|
94
|
+
fromBlockNumber,
|
|
95
|
+
toBlockNumber: endBlockNumber,
|
|
96
|
+
difficulty,
|
|
97
|
+
});
|
|
98
|
+
}
|
|
99
|
+
return blockHeaders;
|
|
100
|
+
});
|
|
101
|
+
}
|
|
102
|
+
getRequestProof(transaction) {
|
|
103
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
104
|
+
let transactionHex = transaction.hex || (yield this.provider.getRawTransaction(transaction.txId));
|
|
105
|
+
let txInfo;
|
|
106
|
+
if (!(transaction.blockHash && transaction.blockNumber)) {
|
|
107
|
+
txInfo = yield this.provider.getTransaction(transaction.txId);
|
|
108
|
+
}
|
|
109
|
+
let blockHash = transaction.blockHash || txInfo.blockHash;
|
|
110
|
+
let blockNumber = transaction.blockNumber || txInfo.blockNumber;
|
|
111
|
+
let parsedTx = parseRawTransaction(transactionHex);
|
|
112
|
+
let merkleProof = transaction.merkleProof || (yield this.getMerkleProof(transaction.txId, blockHash));
|
|
113
|
+
return {
|
|
114
|
+
parsedTx,
|
|
115
|
+
merkleProof,
|
|
116
|
+
blockNumber,
|
|
117
|
+
blockHash,
|
|
118
|
+
};
|
|
119
|
+
});
|
|
120
|
+
}
|
|
121
|
+
getMerkleProof(txId, blockHash) {
|
|
122
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
123
|
+
let txIds = yield this.provider.getBlockTransactionIds(blockHash);
|
|
124
|
+
let proof = calculateMerkleProof(txIds, txId);
|
|
125
|
+
return proof;
|
|
126
|
+
});
|
|
127
|
+
}
|
|
128
|
+
getAddressesUtxo(addresses) {
|
|
129
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
130
|
+
if (!this.apiProvider) {
|
|
131
|
+
throw new Error("this function need an api provider");
|
|
132
|
+
}
|
|
133
|
+
const allPromises = [];
|
|
134
|
+
for (let address of addresses) {
|
|
135
|
+
let promise = yield this.apiProvider.getUtxos(address);
|
|
136
|
+
allPromises.push(promise);
|
|
137
|
+
}
|
|
138
|
+
let result = yield Promise.all(allPromises);
|
|
139
|
+
return result.flat(1);
|
|
140
|
+
});
|
|
141
|
+
}
|
|
142
|
+
getBalance(address) {
|
|
143
|
+
var _a;
|
|
144
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
145
|
+
if (!this.apiProvider) {
|
|
146
|
+
throw new Error("this function need an api provider");
|
|
147
|
+
}
|
|
148
|
+
let utxos = (_a = (yield this.apiProvider.getUtxos(address))) !== null && _a !== void 0 ? _a : [];
|
|
149
|
+
return utxos.reduce((a, tx) => a + Number(tx.value), 0);
|
|
150
|
+
});
|
|
151
|
+
}
|
|
152
|
+
getBlockTransactions(addresses, blockNumber) {
|
|
153
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
154
|
+
let utxos = yield this.getAddressesUtxo(addresses);
|
|
155
|
+
let rawBlockHex = yield this.rpcProvider.getBlockByBlockNumber(blockNumber, 0);
|
|
156
|
+
let { withdrawTxs, depositTxs } = extractTransactionsAndBlockInfoFromRawBlock(rawBlockHex, blockNumber, addresses, utxos, this.network);
|
|
157
|
+
return depositTxs.concat(withdrawTxs);
|
|
158
|
+
});
|
|
159
|
+
}
|
|
160
|
+
getMultipleBlocksTransactions(addresses, startBlockNumber, endBlockNumber) {
|
|
161
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
162
|
+
if (endBlockNumber - startBlockNumber > 20) {
|
|
163
|
+
throw new Error("cant get more than 20 block per function call");
|
|
164
|
+
}
|
|
165
|
+
let blockTxs = [];
|
|
166
|
+
for (let blockNumber = +startBlockNumber + 1; blockNumber <= endBlockNumber; blockNumber += 1) {
|
|
167
|
+
console.log("get block transactions. blockNumber: ", blockNumber);
|
|
168
|
+
const response = this.getBlockTransactions(addresses, blockNumber);
|
|
169
|
+
blockTxs.push(response);
|
|
170
|
+
yield sleep(200);
|
|
171
|
+
}
|
|
172
|
+
blockTxs = yield Promise.all(blockTxs);
|
|
173
|
+
return blockTxs.flat(1);
|
|
174
|
+
});
|
|
175
|
+
}
|
|
176
|
+
getTransactionHistory(addresses, startBlockNumber, endBlockNumber) {
|
|
177
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
178
|
+
if (this.rpcProvider) {
|
|
179
|
+
let endBlock = endBlockNumber || (yield this.getLatestBlockNumber());
|
|
180
|
+
let startBlock = Math.max(+startBlockNumber, +endBlock - 20);
|
|
181
|
+
return this.getMultipleBlocksTransactions(addresses, startBlock, endBlock);
|
|
182
|
+
}
|
|
183
|
+
if (this.apiProviderName !== "BlockStream") {
|
|
184
|
+
throw new Error("just support BlockStream as api provider for this function");
|
|
185
|
+
}
|
|
186
|
+
let txs = yield this.apiProvider.getTransactionHistoryForMultipleAddresses(addresses, startBlockNumber);
|
|
187
|
+
return txs.flat(1);
|
|
188
|
+
});
|
|
189
|
+
}
|
|
190
|
+
getMempoolTransactionHistory(addresses) {
|
|
191
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
192
|
+
if (this.apiProviderName !== "BlockStream") {
|
|
193
|
+
throw new Error("teleporter just support BlockStream as api provider");
|
|
194
|
+
}
|
|
195
|
+
let txs = yield this.apiProvider.getMempoolTransactionHistoryForMultipleAddresses(addresses);
|
|
196
|
+
return txs.flat(1);
|
|
197
|
+
});
|
|
198
|
+
}
|
|
199
|
+
getTeleporterRequests(addresses, startblockNumber, endBlockNumber, mempool = false) {
|
|
200
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
201
|
+
let transactions = mempool
|
|
202
|
+
? yield this.getMempoolTransactionHistory(addresses)
|
|
203
|
+
: yield this.getTransactionHistory(addresses, startblockNumber, endBlockNumber);
|
|
204
|
+
let requests = [];
|
|
205
|
+
let invalidRequests = [];
|
|
206
|
+
for (let transaction of transactions) {
|
|
207
|
+
console.log(`received tx to check for teleport: ${transaction.txId}`);
|
|
208
|
+
let address = transaction.address;
|
|
209
|
+
let request = checkAndParseProtocolRequest(transaction.vout, address, {
|
|
210
|
+
minTeleporterFeeAmount: this.minTeleporterFeeAmount,
|
|
211
|
+
});
|
|
212
|
+
let lockerLockingScript = transaction.addressScript || this.convertAddressToScript(address).script.toString("hex");
|
|
213
|
+
if (request.status) {
|
|
214
|
+
requests.push({ transaction, request, lockerAddress: address, lockerLockingScript });
|
|
215
|
+
}
|
|
216
|
+
else if (request.code !== "NO_OP_RETURN") {
|
|
217
|
+
invalidRequests.push({ transaction, request, lockerAddress: address, lockerLockingScript });
|
|
218
|
+
}
|
|
219
|
+
}
|
|
220
|
+
return { requests, invalidRequests };
|
|
221
|
+
});
|
|
222
|
+
}
|
|
223
|
+
getLockersBurnTransactions(addresses, startBlockNumber, endBlockNumber, mempool = false) {
|
|
224
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
225
|
+
let transactions = mempool
|
|
226
|
+
? yield this.getMempoolTransactionHistory(addresses)
|
|
227
|
+
: yield this.getTransactionHistory(addresses, startBlockNumber, endBlockNumber);
|
|
228
|
+
let validTxs = [];
|
|
229
|
+
for (let transaction of transactions) {
|
|
230
|
+
let address = transaction.address;
|
|
231
|
+
console.log(`received tx to check burn: ${transaction.txId}`);
|
|
232
|
+
let burnInfo = getBurnTransactionInfo(address, transaction.vin, transaction.vout);
|
|
233
|
+
if (burnInfo) {
|
|
234
|
+
validTxs.push({
|
|
235
|
+
transaction,
|
|
236
|
+
burnInfo,
|
|
237
|
+
lockerAddress: address,
|
|
238
|
+
lockerLockingScript: transaction.addressScript ||
|
|
239
|
+
this.convertAddressToScript(address).script.toString("hex"),
|
|
240
|
+
});
|
|
241
|
+
}
|
|
242
|
+
}
|
|
243
|
+
return validTxs;
|
|
244
|
+
});
|
|
245
|
+
}
|
|
246
|
+
}
|
|
247
|
+
module.exports = BitcoinInterface;
|
|
248
|
+
//# sourceMappingURL=bitcoin-interface.js.map
|