@teleportdao/bitcoin 1.4.3 → 1.4.6
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 +66 -53
- package/dist/bitcoin-base.d.ts.map +1 -1
- package/dist/bitcoin-base.js +47 -38
- package/dist/bitcoin-base.js.map +1 -1
- package/dist/bitcoin-interface-utils.d.ts +12 -10
- package/dist/bitcoin-interface-utils.d.ts.map +1 -1
- package/dist/bitcoin-interface-utils.js +16 -10
- package/dist/bitcoin-interface-utils.js.map +1 -1
- package/dist/bitcoin-interface.d.ts +206 -50
- package/dist/bitcoin-interface.d.ts.map +1 -1
- package/dist/bitcoin-interface.js +42 -27
- package/dist/bitcoin-interface.js.map +1 -1
- package/dist/bitcoin-utils.d.ts +111 -41
- package/dist/bitcoin-utils.d.ts.map +1 -1
- package/dist/bitcoin-utils.js +215 -156
- package/dist/bitcoin-utils.js.map +1 -1
- package/dist/bundle.js +13 -0
- package/dist/index.d.ts +5 -6
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +32 -12
- package/dist/index.js.map +1 -1
- package/dist/sign/sign-transaction.d.ts +9 -5
- package/dist/sign/sign-transaction.d.ts.map +1 -1
- package/dist/sign/sign-transaction.js +14 -11
- package/dist/sign/sign-transaction.js.map +1 -1
- package/dist/teleport-dao-payments.d.ts +68 -89
- package/dist/teleport-dao-payments.d.ts.map +1 -1
- package/dist/teleport-dao-payments.js +16 -4
- package/dist/teleport-dao-payments.js.map +1 -1
- package/dist/transaction-builder/bitcoin-transaction-builder.d.ts +30 -11
- package/dist/transaction-builder/bitcoin-transaction-builder.d.ts.map +1 -1
- package/dist/transaction-builder/bitcoin-transaction-builder.js +37 -9
- package/dist/transaction-builder/bitcoin-transaction-builder.js.map +1 -1
- package/dist/transaction-builder/transaction-builder.d.ts +198 -9
- package/dist/transaction-builder/transaction-builder.d.ts.map +1 -1
- package/dist/transaction-builder/transaction-builder.js +291 -38
- package/dist/transaction-builder/transaction-builder.js.map +1 -1
- package/dist/utils/networks.d.ts +5 -35
- package/dist/utils/networks.d.ts.map +1 -1
- package/dist/utils/networks.js +26 -2
- package/dist/utils/networks.js.map +1 -1
- package/dist/utils/tools.d.ts +15 -9
- package/dist/utils/tools.d.ts.map +1 -1
- package/dist/utils/tools.js +14 -11
- package/dist/utils/tools.js.map +1 -1
- package/package.json +8 -6
- package/src/{bitcoin-base.js → bitcoin-base.ts} +248 -219
- package/src/{bitcoin-interface-utils.js → bitcoin-interface-utils.ts} +59 -53
- package/src/{bitcoin-interface.js → bitcoin-interface.ts} +420 -350
- package/src/{bitcoin-utils.js → bitcoin-utils.ts} +608 -483
- package/src/helper/teleport-request-helper.js +179 -179
- package/src/index.ts +5 -0
- package/src/sign/sign-transaction.ts +96 -0
- package/src/{teleport-dao-payments.js → teleport-dao-payments.ts} +341 -280
- package/src/transaction-builder/bitcoin-transaction-builder.ts +61 -0
- package/src/transaction-builder/transaction-builder.ts +567 -0
- package/src/utils/{networks.js → networks.ts} +33 -31
- package/src/utils/{tools.js → tools.ts} +80 -72
- package/tsconfig.json +10 -9
- package/webpack.config.js +16 -0
- package/dist/transaction-builder/transaction-builder-common.d.ts +0 -57
- package/dist/transaction-builder/transaction-builder-common.d.ts.map +0 -1
- package/dist/transaction-builder/transaction-builder-common.js +0 -183
- package/dist/transaction-builder/transaction-builder-common.js.map +0 -1
- package/src/index.js +0 -15
- package/src/sign/sign-transaction.js +0 -36
- package/src/transaction-builder/bitcoin-transaction-builder.js +0 -37
- package/src/transaction-builder/transaction-builder-common.js +0 -236
- package/src/transaction-builder/transaction-builder.js +0 -159
|
@@ -1,72 +1,80 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
const sleep =
|
|
5
|
-
|
|
6
|
-
async function runWithRetries(
|
|
7
|
-
action,
|
|
8
|
-
config = {
|
|
9
|
-
maxTries: 2,
|
|
10
|
-
retrySleep: 1000,
|
|
11
|
-
},
|
|
12
|
-
) {
|
|
13
|
-
const maxTries = config.maxTries || 2
|
|
14
|
-
const retrySleep = config.retrySleep || 1000 // milliseconds
|
|
15
|
-
let lastError
|
|
16
|
-
for (let count = 0; count < maxTries; count += 1) {
|
|
17
|
-
try {
|
|
18
|
-
return await action()
|
|
19
|
-
} catch (error) {
|
|
20
|
-
lastError = error
|
|
21
|
-
}
|
|
22
|
-
await sleep(retrySleep)
|
|
23
|
-
}
|
|
24
|
-
throw lastError || new Error("function failed after retries")
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
function getRandomInteger(min, max) {
|
|
28
|
-
return Math.floor(Math.random() * (max - min)) + min
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
function getAxiosInstance({
|
|
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
|
-
}
|
|
1
|
+
import { promisify } from "util"
|
|
2
|
+
import axios from "axios"
|
|
3
|
+
|
|
4
|
+
const sleep = promisify(setTimeout)
|
|
5
|
+
|
|
6
|
+
async function runWithRetries(
|
|
7
|
+
action: () => any,
|
|
8
|
+
config = {
|
|
9
|
+
maxTries: 2,
|
|
10
|
+
retrySleep: 1000,
|
|
11
|
+
},
|
|
12
|
+
) {
|
|
13
|
+
const maxTries = config.maxTries || 2
|
|
14
|
+
const retrySleep = config.retrySleep || 1000 // milliseconds
|
|
15
|
+
let lastError
|
|
16
|
+
for (let count = 0; count < maxTries; count += 1) {
|
|
17
|
+
try {
|
|
18
|
+
return await action()
|
|
19
|
+
} catch (error: any) {
|
|
20
|
+
lastError = error
|
|
21
|
+
}
|
|
22
|
+
await sleep(retrySleep)
|
|
23
|
+
}
|
|
24
|
+
throw lastError || new Error("function failed after retries")
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
function getRandomInteger(min: number, max: number) {
|
|
28
|
+
return Math.floor(Math.random() * (max - min)) + min
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
function getAxiosInstance({
|
|
32
|
+
baseUrl,
|
|
33
|
+
timeout = 10000,
|
|
34
|
+
headers = {},
|
|
35
|
+
auth,
|
|
36
|
+
}: {
|
|
37
|
+
baseUrl: string
|
|
38
|
+
timeout?: number
|
|
39
|
+
headers?: { [key: string]: string }
|
|
40
|
+
auth?: {
|
|
41
|
+
username: string
|
|
42
|
+
password: string
|
|
43
|
+
}
|
|
44
|
+
}) {
|
|
45
|
+
let host = baseUrl
|
|
46
|
+
let instance
|
|
47
|
+
|
|
48
|
+
instance = axios.create({
|
|
49
|
+
baseURL: host,
|
|
50
|
+
timeout,
|
|
51
|
+
auth,
|
|
52
|
+
headers: {
|
|
53
|
+
...headers,
|
|
54
|
+
},
|
|
55
|
+
})
|
|
56
|
+
|
|
57
|
+
// Add a response interceptor
|
|
58
|
+
instance.interceptors.response.use(
|
|
59
|
+
(response) => response,
|
|
60
|
+
(error) => {
|
|
61
|
+
// todo : fix this part
|
|
62
|
+
if (error.response) {
|
|
63
|
+
const serviceError = new Error(
|
|
64
|
+
JSON.stringify({ data: error.response.data, message: error.message }),
|
|
65
|
+
)
|
|
66
|
+
return Promise.reject(serviceError)
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
if (error.request) {
|
|
70
|
+
const serviceError = new Error(error.message)
|
|
71
|
+
return Promise.reject(serviceError)
|
|
72
|
+
}
|
|
73
|
+
return Promise.reject(error)
|
|
74
|
+
},
|
|
75
|
+
)
|
|
76
|
+
|
|
77
|
+
return instance
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
export { sleep, runWithRetries, getRandomInteger, getAxiosInstance }
|
package/tsconfig.json
CHANGED
|
@@ -1,9 +1,10 @@
|
|
|
1
|
-
{
|
|
2
|
-
"extends": "../../tsconfig.json",
|
|
3
|
-
"compilerOptions": {
|
|
4
|
-
"rootDir": "src" /* Specify the root folder within your source files. */,
|
|
5
|
-
"outDir": "dist" /* Specify an output folder for all emitted files. */,
|
|
6
|
-
"baseUrl": "./src" /* Specify the base directory to resolve non-relative module names. */
|
|
7
|
-
},
|
|
8
|
-
"
|
|
9
|
-
|
|
1
|
+
{
|
|
2
|
+
"extends": "../../tsconfig.json",
|
|
3
|
+
"compilerOptions": {
|
|
4
|
+
"rootDir": "src" /* Specify the root folder within your source files. */,
|
|
5
|
+
"outDir": "dist" /* Specify an output folder for all emitted files. */,
|
|
6
|
+
"baseUrl": "./src" /* Specify the base directory to resolve non-relative module names. */
|
|
7
|
+
},
|
|
8
|
+
"include": ["src"],
|
|
9
|
+
"exclude": ["node_modules", "dist"]
|
|
10
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
const path = require("path")
|
|
2
|
+
// const webpack = require("webpack")
|
|
3
|
+
|
|
4
|
+
module.exports = {
|
|
5
|
+
mode: "production",
|
|
6
|
+
entry: "./dist/index.js",
|
|
7
|
+
output: {
|
|
8
|
+
filename: "bundle.js",
|
|
9
|
+
path: path.resolve(__dirname, "dist"),
|
|
10
|
+
libraryTarget: "umd", // Module type
|
|
11
|
+
globalObject: "this", // Fix for universal module definition
|
|
12
|
+
},
|
|
13
|
+
performance: {
|
|
14
|
+
hints: false,
|
|
15
|
+
},
|
|
16
|
+
}
|
|
@@ -1,57 +0,0 @@
|
|
|
1
|
-
export = BaseBitcoinLikeTransaction;
|
|
2
|
-
declare class BaseBitcoinLikeTransaction {
|
|
3
|
-
static helperHandleInputsAndOutputs({ targets, extendedUtxo, feeRate, changeAddress, selectType, }: {
|
|
4
|
-
targets: any;
|
|
5
|
-
extendedUtxo: any;
|
|
6
|
-
feeRate: any;
|
|
7
|
-
changeAddress: any;
|
|
8
|
-
selectType?: string | undefined;
|
|
9
|
-
}): {
|
|
10
|
-
inputs: any;
|
|
11
|
-
fee: any;
|
|
12
|
-
outputs: any;
|
|
13
|
-
change: {
|
|
14
|
-
address: any;
|
|
15
|
-
value: any;
|
|
16
|
-
} | undefined;
|
|
17
|
-
};
|
|
18
|
-
constructor({ network, testnet, feeMin, dustLimit, maximumNumberOfOutputsInTransaction, }: {
|
|
19
|
-
network: any;
|
|
20
|
-
testnet: any;
|
|
21
|
-
feeMin?: number | undefined;
|
|
22
|
-
dustLimit: any;
|
|
23
|
-
maximumNumberOfOutputsInTransaction?: number | undefined;
|
|
24
|
-
});
|
|
25
|
-
testnet: any;
|
|
26
|
-
network: any;
|
|
27
|
-
maximumNumberOfOutputsInTransaction: number;
|
|
28
|
-
feeMin: number;
|
|
29
|
-
dustLimit: any;
|
|
30
|
-
_getUtxo(userAddress: any): Promise<void>;
|
|
31
|
-
_getTransactionHex(transactionId: any): Promise<void>;
|
|
32
|
-
convertBaseInputsToInputs(baseInputs: any): Promise<void>;
|
|
33
|
-
createUnsignedTransaction(baseInputs: any): Promise<void>;
|
|
34
|
-
createAddressObject({ addressType, publicKey }: {
|
|
35
|
-
addressType: any;
|
|
36
|
-
publicKey: any;
|
|
37
|
-
}): bitcoin.payments.Payment;
|
|
38
|
-
validateAddress(address: any): boolean;
|
|
39
|
-
getExtendedUtxo(signerInfo: any): Promise<any>;
|
|
40
|
-
convertUtxoToInput({ extendedUtxo, targets, changeAddress, feeRate, selectType }: any): Promise<{
|
|
41
|
-
inputs: void;
|
|
42
|
-
outputs: any;
|
|
43
|
-
change: {
|
|
44
|
-
address: any;
|
|
45
|
-
value: any;
|
|
46
|
-
} | undefined;
|
|
47
|
-
fee: any;
|
|
48
|
-
feeRate: any;
|
|
49
|
-
}>;
|
|
50
|
-
processUnsignedTransaction({ extendedUtxo, targets, changeAddress, fullAmount, feeRate, selfTransaction, selectType, }: any): Promise<void>;
|
|
51
|
-
getOpReturnTarget(dataHex: any): {
|
|
52
|
-
script: Buffer | undefined;
|
|
53
|
-
value: number;
|
|
54
|
-
};
|
|
55
|
-
}
|
|
56
|
-
import bitcoin = require("bitcoinjs-lib");
|
|
57
|
-
//# sourceMappingURL=transaction-builder-common.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"transaction-builder-common.d.ts","sourceRoot":"","sources":["../../src/transaction-builder/transaction-builder-common.js"],"names":[],"mappings":";AA2BA;IAqEE;;;;;;;;;;;;;;MAgDC;IAnHD;;;;;;OAYC;IALC,aAAsB;IACtB,aAAsB;IACtB,4CAA8E;IAC9E,eAAoB;IACpB,eAAuE;IAIzE,0CAIC;IAGD,sDAIC;IAGD,0DAIC;IAGD,0DAIC;IAGD;;;iCAEC;IAED,uCAkBC;IAoDD,+CAUC;IAOD;;;;;;;;;OAuBC;IAOD,4IA2BC;IAED;;;MAUC;CACF"}
|
|
@@ -1,183 +0,0 @@
|
|
|
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 bitcoin = require("bitcoinjs-lib");
|
|
12
|
-
const coinselect = require("coinselect");
|
|
13
|
-
const coinselectSplit = require("coinselect/split");
|
|
14
|
-
const coinselectAccumulative = require("coinselect/accumulative");
|
|
15
|
-
const { createAddressObjectByPublicKey } = require("../bitcoin-utils");
|
|
16
|
-
const TX_EMPTY_SIZE = 4 + 1 + 1 + 4;
|
|
17
|
-
const TX_INPUT_BASE = 32 + 4 + 1 + 4;
|
|
18
|
-
const TX_INPUT_P2PKH = 107;
|
|
19
|
-
const TX_INPUT_P2SH_P2PKH = 50;
|
|
20
|
-
const TX_INPUT_P2WPKH = 47;
|
|
21
|
-
const TX_OUTPUT_BASE = 8 + 1;
|
|
22
|
-
const TX_OUTPUT_P2PKH = 25;
|
|
23
|
-
const componentBytes = {
|
|
24
|
-
bytePerInput: {
|
|
25
|
-
p2pkh: TX_INPUT_BASE + TX_INPUT_P2PKH,
|
|
26
|
-
p2wpkh: TX_INPUT_BASE + TX_INPUT_P2WPKH,
|
|
27
|
-
p2shp2wpkh: TX_INPUT_BASE + TX_INPUT_P2SH_P2PKH,
|
|
28
|
-
},
|
|
29
|
-
baseTxBytes: TX_EMPTY_SIZE,
|
|
30
|
-
bytePerOutput: TX_OUTPUT_BASE + TX_OUTPUT_P2PKH,
|
|
31
|
-
};
|
|
32
|
-
class BaseBitcoinLikeTransaction {
|
|
33
|
-
constructor({ network, testnet, feeMin = 0, dustLimit, maximumNumberOfOutputsInTransaction = 50, }) {
|
|
34
|
-
this.testnet = testnet;
|
|
35
|
-
this.network = network;
|
|
36
|
-
this.maximumNumberOfOutputsInTransaction = maximumNumberOfOutputsInTransaction;
|
|
37
|
-
this.feeMin = feeMin;
|
|
38
|
-
this.dustLimit = dustLimit || 1 * 2 * componentBytes.bytePerInput.p2pkh;
|
|
39
|
-
}
|
|
40
|
-
_getUtxo(userAddress) {
|
|
41
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
42
|
-
throw new Error("Do not call abstract method directly");
|
|
43
|
-
});
|
|
44
|
-
}
|
|
45
|
-
_getTransactionHex(transactionId) {
|
|
46
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
47
|
-
throw new Error("Do not call abstract method directly");
|
|
48
|
-
});
|
|
49
|
-
}
|
|
50
|
-
convertBaseInputsToInputs(baseInputs) {
|
|
51
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
52
|
-
throw new Error("Do not call abstract method directly");
|
|
53
|
-
});
|
|
54
|
-
}
|
|
55
|
-
createUnsignedTransaction(baseInputs) {
|
|
56
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
57
|
-
throw new Error("Do not call abstract method directly");
|
|
58
|
-
});
|
|
59
|
-
}
|
|
60
|
-
createAddressObject({ addressType, publicKey }) {
|
|
61
|
-
return createAddressObjectByPublicKey({ addressType, publicKey }, this.network);
|
|
62
|
-
}
|
|
63
|
-
validateAddress(address) {
|
|
64
|
-
try {
|
|
65
|
-
let isValid = false;
|
|
66
|
-
let network = this.network;
|
|
67
|
-
let isAddressSegwit = address.startsWith(network.bech32);
|
|
68
|
-
if (isAddressSegwit) {
|
|
69
|
-
bitcoin.address.fromBech32(address);
|
|
70
|
-
isValid = true;
|
|
71
|
-
}
|
|
72
|
-
else {
|
|
73
|
-
let base58Data = bitcoin.address.fromBase58Check(address);
|
|
74
|
-
isValid =
|
|
75
|
-
base58Data.version === Number(network.scriptHash) ||
|
|
76
|
-
base58Data.version === Number(network.pubKeyHash);
|
|
77
|
-
}
|
|
78
|
-
return isValid;
|
|
79
|
-
}
|
|
80
|
-
catch (error) {
|
|
81
|
-
return false;
|
|
82
|
-
}
|
|
83
|
-
}
|
|
84
|
-
static helperHandleInputsAndOutputs({ targets, extendedUtxo, feeRate, changeAddress, selectType = "normal", }) {
|
|
85
|
-
let selectResponse;
|
|
86
|
-
switch (selectType) {
|
|
87
|
-
case "normal":
|
|
88
|
-
selectResponse = coinselect(extendedUtxo, targets, Math.round(feeRate));
|
|
89
|
-
break;
|
|
90
|
-
case "accumulative":
|
|
91
|
-
selectResponse = coinselectAccumulative(extendedUtxo, targets, Math.round(feeRate));
|
|
92
|
-
break;
|
|
93
|
-
case "full":
|
|
94
|
-
selectResponse = coinselectSplit(extendedUtxo, [{ address: targets[0].address }], Math.round(feeRate));
|
|
95
|
-
break;
|
|
96
|
-
default:
|
|
97
|
-
break;
|
|
98
|
-
}
|
|
99
|
-
let { inputs, outputs, fee } = selectResponse;
|
|
100
|
-
if (!inputs || !outputs) {
|
|
101
|
-
throw new Error("not enough balance");
|
|
102
|
-
}
|
|
103
|
-
let changeIndex = outputs.findIndex((x) => !x.address && !x.script && x.value > 0);
|
|
104
|
-
let change;
|
|
105
|
-
if (changeIndex >= 0) {
|
|
106
|
-
change = {
|
|
107
|
-
address: changeAddress,
|
|
108
|
-
value: outputs[changeIndex].value,
|
|
109
|
-
};
|
|
110
|
-
outputs.splice(changeIndex, 1);
|
|
111
|
-
}
|
|
112
|
-
return {
|
|
113
|
-
inputs,
|
|
114
|
-
fee,
|
|
115
|
-
outputs,
|
|
116
|
-
change,
|
|
117
|
-
};
|
|
118
|
-
}
|
|
119
|
-
getExtendedUtxo(signerInfo) {
|
|
120
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
121
|
-
let utxo = yield this._getUtxo(signerInfo.address);
|
|
122
|
-
const extendedUtxo = utxo.map((input) => (Object.assign(Object.assign({}, input), { signerInfo })));
|
|
123
|
-
if (!extendedUtxo || extendedUtxo.length === 0) {
|
|
124
|
-
throw new Error("no utxo found");
|
|
125
|
-
}
|
|
126
|
-
return extendedUtxo;
|
|
127
|
-
});
|
|
128
|
-
}
|
|
129
|
-
convertUtxoToInput({ extendedUtxo, targets, changeAddress, feeRate, selectType }) {
|
|
130
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
131
|
-
let { inputs: filteredInputs, outputs, change, fee, } = BaseBitcoinLikeTransaction.helperHandleInputsAndOutputs({
|
|
132
|
-
targets,
|
|
133
|
-
extendedUtxo,
|
|
134
|
-
feeRate,
|
|
135
|
-
changeAddress,
|
|
136
|
-
selectType,
|
|
137
|
-
});
|
|
138
|
-
let inputs = yield this.convertBaseInputsToInputs(filteredInputs);
|
|
139
|
-
return {
|
|
140
|
-
inputs,
|
|
141
|
-
outputs,
|
|
142
|
-
change,
|
|
143
|
-
fee,
|
|
144
|
-
feeRate,
|
|
145
|
-
};
|
|
146
|
-
});
|
|
147
|
-
}
|
|
148
|
-
processUnsignedTransaction({ extendedUtxo, targets = [], changeAddress = undefined, fullAmount = false, feeRate, selfTransaction = false, selectType = "normal", }) {
|
|
149
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
150
|
-
if (!selfTransaction && targets.length === 0)
|
|
151
|
-
throw new Error("no target");
|
|
152
|
-
const { inputs, outputs, change, fee } = yield this.convertUtxoToInput({
|
|
153
|
-
extendedUtxo,
|
|
154
|
-
targets,
|
|
155
|
-
changeAddress,
|
|
156
|
-
feeRate,
|
|
157
|
-
selectType: fullAmount ? "full" : selectType,
|
|
158
|
-
});
|
|
159
|
-
let unsignedTransaction = yield this.createUnsignedTransaction({
|
|
160
|
-
inputs,
|
|
161
|
-
outputs,
|
|
162
|
-
change,
|
|
163
|
-
fee,
|
|
164
|
-
feeRate,
|
|
165
|
-
});
|
|
166
|
-
return unsignedTransaction;
|
|
167
|
-
});
|
|
168
|
-
}
|
|
169
|
-
getOpReturnTarget(dataHex) {
|
|
170
|
-
if (!dataHex.length > 0)
|
|
171
|
-
throw new Error("invalid data in hex");
|
|
172
|
-
const embed = bitcoin.payments.embed({
|
|
173
|
-
data: [Buffer.from(dataHex, "hex")],
|
|
174
|
-
network: this.network,
|
|
175
|
-
});
|
|
176
|
-
return {
|
|
177
|
-
script: embed.output,
|
|
178
|
-
value: 0,
|
|
179
|
-
};
|
|
180
|
-
}
|
|
181
|
-
}
|
|
182
|
-
module.exports = BaseBitcoinLikeTransaction;
|
|
183
|
-
//# sourceMappingURL=transaction-builder-common.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"transaction-builder-common.js","sourceRoot":"","sources":["../../src/transaction-buil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IAAI,KAAK,CAAC,WAAW,CAAC,CAAA;YAE1E,MAAM,EAAE,MAAM,EAAE,OAAO,EAAE,MAAM,EAAE,GAAG,EAAE,GAAG,MAAM,IAAI,CAAC,kBAAkB,CAAC;gBACrE,YAAY;gBACZ,OAAO;gBACP,aAAa;gBACb,OAAO;gBACP,UAAU,EAAE,UAAU,CAAC,CAAC,CAAC,MAAM,CAAC,CAAC,CAAC,UAAU;aAC7C,CAAC,CAAA;YACF,IAAI,mBAAmB,GAAG,MAAM,IAAI,CAAC,yBAAyB,CAAC;gBAC7D,MAAM;gBACN,OAAO;gBACP,MAAM;gBACN,GAAG;gBACH,OAAO;aACR,CAAC,CAAA;YAEF,OAAO,mBAAmB,CAAA;QAC5B,CAAC;KAAA;IAED,iBAAiB,CAAC,OAAO;QACvB,IAAI,CAAC,OAAO,CAAC,MAAM,GAAG,CAAC;YAAE,MAAM,IAAI,KAAK,CAAC,qBAAqB,CAAC,CAAA;QAC/D,MAAM,KAAK,GAAG,OAAO,CAAC,QAAQ,CAAC,KAAK,CAAC;YACnC,IAAI,EAAE,CAAC,MAAM,CAAC,IAAI,CAAC,OAAO,EAAE,KAAK,CAAC,CAAC;YACnC,OAAO,EAAE,IAAI,CAAC,OAAO;SACtB,CAAC,CAAA;QACF,OAAO;YACL,MAAM,EAAE,KAAK,CAAC,MAAM;YACpB,KAAK,EAAE,CAAC;SACT,CAAA;IACH,CAAC;CACF;AAED,MAAM,CAAC,OAAO,GAAG,0BAA0B,CAAA"}
|
package/src/index.js
DELETED
|
@@ -1,15 +0,0 @@
|
|
|
1
|
-
// eslint-disable-next-line global-require
|
|
2
|
-
global.Buffer = global.Buffer || require("buffer").Buffer
|
|
3
|
-
const TeleportDaoPayment = require("./teleport-dao-payments")
|
|
4
|
-
const bitcoinUtils = require("./bitcoin-utils")
|
|
5
|
-
const BitcoinInterface = require("./bitcoin-interface")
|
|
6
|
-
const BitcoinInterfaceUtils = require("./bitcoin-interface-utils")
|
|
7
|
-
const BitcoinBase = require("./bitcoin-base")
|
|
8
|
-
|
|
9
|
-
module.exports = {
|
|
10
|
-
TeleportDaoPayment,
|
|
11
|
-
BitcoinInterface,
|
|
12
|
-
BitcoinInterfaceUtils,
|
|
13
|
-
bitcoinUtils,
|
|
14
|
-
BitcoinBase,
|
|
15
|
-
}
|
|
@@ -1,36 +0,0 @@
|
|
|
1
|
-
const bitcoin = require("bitcoinjs-lib")
|
|
2
|
-
|
|
3
|
-
class BitcoinLikeSignTransaction {
|
|
4
|
-
constructor(network) {
|
|
5
|
-
this.network = network
|
|
6
|
-
}
|
|
7
|
-
|
|
8
|
-
async signPsbt(extendedUnsignedTransaction, privateKey) {
|
|
9
|
-
const { network } = this
|
|
10
|
-
const keyPair = bitcoin.ECPair.fromPrivateKey(privateKey, {
|
|
11
|
-
network,
|
|
12
|
-
compressed: true,
|
|
13
|
-
})
|
|
14
|
-
const psbt = bitcoin.Psbt.fromBase64(extendedUnsignedTransaction.unsignedTransaction, {
|
|
15
|
-
network,
|
|
16
|
-
})
|
|
17
|
-
psbt.signAllInputs(keyPair)
|
|
18
|
-
|
|
19
|
-
const partialSigendPsbt = psbt.toBase64()
|
|
20
|
-
return partialSigendPsbt
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
finalizePsbts(psbtsBase64 = []) {
|
|
24
|
-
const finals = psbtsBase64.map((psbtBase64) =>
|
|
25
|
-
bitcoin.Psbt.fromBase64(psbtBase64, { network: this.network }),
|
|
26
|
-
)
|
|
27
|
-
const psbt =
|
|
28
|
-
finals.length === 1
|
|
29
|
-
? finals[0]
|
|
30
|
-
: new bitcoin.Psbt({ network: this.network }).combine(...finals)
|
|
31
|
-
psbt.finalizeAllInputs()
|
|
32
|
-
return psbt.extractTransaction().toHex()
|
|
33
|
-
}
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
module.exports = BitcoinLikeSignTransaction
|
|
@@ -1,37 +0,0 @@
|
|
|
1
|
-
const BaseTransactionBuilder = require("./transaction-builder")
|
|
2
|
-
const BitcoinInterface = require("../bitcoin-interface")
|
|
3
|
-
|
|
4
|
-
class BitcoinTransactionBuilder extends BaseTransactionBuilder {
|
|
5
|
-
constructor(connectionInfo, networkName, network) {
|
|
6
|
-
super({
|
|
7
|
-
network,
|
|
8
|
-
testnet: networkName?.includes("_testnet"),
|
|
9
|
-
dustLimit: 1000,
|
|
10
|
-
})
|
|
11
|
-
this.btcInterface = new BitcoinInterface(connectionInfo, networkName)
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
async _getUtxo(userAddress) {
|
|
15
|
-
let utxos = await this.btcInterface.getAddressesUtxo([userAddress])
|
|
16
|
-
return utxos.map((tx) => ({
|
|
17
|
-
hash: tx.txId,
|
|
18
|
-
value: tx.value,
|
|
19
|
-
index: tx.index,
|
|
20
|
-
}))
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
async _getFeeRate(speed) {
|
|
24
|
-
return this.btcInterface.getFeeRate(speed)
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
async _getTransactionHex(transactionId) {
|
|
28
|
-
return this.btcInterface.provider.getRawTransaction(transactionId)
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
async sendTx(txHex) {
|
|
32
|
-
let txId = await this.btcInterface.provider.sendRawTransaction(txHex)
|
|
33
|
-
return txId
|
|
34
|
-
}
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
module.exports = BitcoinTransactionBuilder
|