@yaswap/bitcoin 2.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/CHANGELOG.md +6 -0
- package/dist/lib/chain/BitcoinBaseChainProvider.d.ts +11 -0
- package/dist/lib/chain/BitcoinBaseChainProvider.js +7 -0
- package/dist/lib/chain/BitcoinBaseChainProvider.js.map +1 -0
- package/dist/lib/chain/esplora/BitcoinEsploraApiProvider.d.ts +20 -0
- package/dist/lib/chain/esplora/BitcoinEsploraApiProvider.js +151 -0
- package/dist/lib/chain/esplora/BitcoinEsploraApiProvider.js.map +1 -0
- package/dist/lib/chain/esplora/BitcoinEsploraBaseProvider.d.ts +19 -0
- package/dist/lib/chain/esplora/BitcoinEsploraBaseProvider.js +96 -0
- package/dist/lib/chain/esplora/BitcoinEsploraBaseProvider.js.map +1 -0
- package/dist/lib/chain/esplora/BitcoinEsploraBatchBaseProvider.d.ts +12 -0
- package/dist/lib/chain/esplora/BitcoinEsploraBatchBaseProvider.js +48 -0
- package/dist/lib/chain/esplora/BitcoinEsploraBatchBaseProvider.js.map +1 -0
- package/dist/lib/chain/esplora/types.d.ts +93 -0
- package/dist/lib/chain/esplora/types.js +3 -0
- package/dist/lib/chain/esplora/types.js.map +1 -0
- package/dist/lib/chain/jsonRpc/BitcoinJsonRpcBaseProvider.d.ts +17 -0
- package/dist/lib/chain/jsonRpc/BitcoinJsonRpcBaseProvider.js +83 -0
- package/dist/lib/chain/jsonRpc/BitcoinJsonRpcBaseProvider.js.map +1 -0
- package/dist/lib/chain/jsonRpc/BitcoinJsonRpcProvider.d.ts +20 -0
- package/dist/lib/chain/jsonRpc/BitcoinJsonRpcProvider.js +181 -0
- package/dist/lib/chain/jsonRpc/BitcoinJsonRpcProvider.js.map +1 -0
- package/dist/lib/chain/jsonRpc/types.d.ts +85 -0
- package/dist/lib/chain/jsonRpc/types.js +3 -0
- package/dist/lib/chain/jsonRpc/types.js.map +1 -0
- package/dist/lib/fee/BitcoinFeeApiProvider.d.ts +7 -0
- package/dist/lib/fee/BitcoinFeeApiProvider.js +40 -0
- package/dist/lib/fee/BitcoinFeeApiProvider.js.map +1 -0
- package/dist/lib/index.d.ts +16 -0
- package/dist/lib/index.js +57 -0
- package/dist/lib/index.js.map +1 -0
- package/dist/lib/networks.d.ts +7 -0
- package/dist/lib/networks.js +14 -0
- package/dist/lib/networks.js.map +1 -0
- package/dist/lib/swap/BitcoinSwapBaseProvider.d.ts +50 -0
- package/dist/lib/swap/BitcoinSwapBaseProvider.js +365 -0
- package/dist/lib/swap/BitcoinSwapBaseProvider.js.map +1 -0
- package/dist/lib/swap/BitcoinSwapEsploraProvider.d.ts +11 -0
- package/dist/lib/swap/BitcoinSwapEsploraProvider.js +50 -0
- package/dist/lib/swap/BitcoinSwapEsploraProvider.js.map +1 -0
- package/dist/lib/swap/BitcoinSwapRpcProvider.d.ts +10 -0
- package/dist/lib/swap/BitcoinSwapRpcProvider.js +28 -0
- package/dist/lib/swap/BitcoinSwapRpcProvider.js.map +1 -0
- package/dist/lib/swap/types.d.ts +14 -0
- package/dist/lib/swap/types.js +4 -0
- package/dist/lib/swap/types.js.map +1 -0
- package/dist/lib/types.d.ts +89 -0
- package/dist/lib/types.js +45 -0
- package/dist/lib/types.js.map +1 -0
- package/dist/lib/utils.d.ts +31 -0
- package/dist/lib/utils.js +214 -0
- package/dist/lib/utils.js.map +1 -0
- package/dist/lib/wallet/BitcoinBaseWallet.d.ts +67 -0
- package/dist/lib/wallet/BitcoinBaseWallet.js +408 -0
- package/dist/lib/wallet/BitcoinBaseWallet.js.map +1 -0
- package/dist/lib/wallet/BitcoinHDWallet.d.ts +42 -0
- package/dist/lib/wallet/BitcoinHDWallet.js +215 -0
- package/dist/lib/wallet/BitcoinHDWallet.js.map +1 -0
- package/dist/lib/wallet/BitcoinNodeWallet.d.ts +41 -0
- package/dist/lib/wallet/BitcoinNodeWallet.js +237 -0
- package/dist/lib/wallet/BitcoinNodeWallet.js.map +1 -0
- package/dist/lib/wallet/IBitcoinWallet.d.ts +10 -0
- package/dist/lib/wallet/IBitcoinWallet.js +3 -0
- package/dist/lib/wallet/IBitcoinWallet.js.map +1 -0
- package/package.json +36 -0
|
@@ -0,0 +1,45 @@
|
|
|
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 __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __importStar = (this && this.__importStar) || function (mod) {
|
|
19
|
+
if (mod && mod.__esModule) return mod;
|
|
20
|
+
var result = {};
|
|
21
|
+
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
|
22
|
+
__setModuleDefault(result, mod);
|
|
23
|
+
return result;
|
|
24
|
+
};
|
|
25
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
26
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
27
|
+
};
|
|
28
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
29
|
+
exports.SwapMode = exports.AddressType = exports.BitcoinJsonRpcTypes = exports.BitcoinEsploraTypes = void 0;
|
|
30
|
+
exports.BitcoinEsploraTypes = __importStar(require("./chain/esplora/types"));
|
|
31
|
+
exports.BitcoinJsonRpcTypes = __importStar(require("./chain/jsonRpc/types"));
|
|
32
|
+
__exportStar(require("./swap/types"), exports);
|
|
33
|
+
var AddressType;
|
|
34
|
+
(function (AddressType) {
|
|
35
|
+
AddressType["LEGACY"] = "legacy";
|
|
36
|
+
AddressType["P2SH_SEGWIT"] = "p2sh-segwit";
|
|
37
|
+
AddressType["BECH32"] = "bech32";
|
|
38
|
+
})(AddressType = exports.AddressType || (exports.AddressType = {}));
|
|
39
|
+
var SwapMode;
|
|
40
|
+
(function (SwapMode) {
|
|
41
|
+
SwapMode["P2SH"] = "p2sh";
|
|
42
|
+
SwapMode["P2SH_SEGWIT"] = "p2shSegwit";
|
|
43
|
+
SwapMode["P2WSH"] = "p2wsh";
|
|
44
|
+
})(SwapMode = exports.SwapMode || (exports.SwapMode = {}));
|
|
45
|
+
//# sourceMappingURL=types.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.js","sourceRoot":"","sources":["../../lib/types.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAGA,6EAA6D;AAC7D,6EAA6D;AAC7D,+CAA6B;AAsE7B,IAAY,WAIX;AAJD,WAAY,WAAW;IACnB,gCAAiB,CAAA;IACjB,0CAA2B,CAAA;IAC3B,gCAAiB,CAAA;AACrB,CAAC,EAJW,WAAW,GAAX,mBAAW,KAAX,mBAAW,QAItB;AAED,IAAY,QAIX;AAJD,WAAY,QAAQ;IAChB,yBAAa,CAAA;IACb,sCAA0B,CAAA;IAC1B,2BAAe,CAAA;AACnB,CAAC,EAJW,QAAQ,GAAR,gBAAQ,KAAR,gBAAQ,QAInB"}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
/// <reference types="node" />
|
|
2
|
+
import { AddressType, Transaction } from '@yaswap/types';
|
|
3
|
+
import { BitcoinNetwork, Transaction as BitcoinTransaction, UTXO } from './types';
|
|
4
|
+
declare const AddressTypes: string[];
|
|
5
|
+
declare function calculateFee(numInputs: number, numOutputs: number, feePerByte: number): number;
|
|
6
|
+
/**
|
|
7
|
+
* Get compressed pubKey from pubKey.
|
|
8
|
+
* @param pubKey - 65 byte string with prefix, x, y.
|
|
9
|
+
* @returns the compressed pubKey of uncompressed pubKey.
|
|
10
|
+
*/
|
|
11
|
+
declare function compressPubKey(pubKey: string): string;
|
|
12
|
+
declare type CoinSelectTarget = {
|
|
13
|
+
value: number;
|
|
14
|
+
script?: Buffer;
|
|
15
|
+
id?: string;
|
|
16
|
+
};
|
|
17
|
+
declare function selectCoins(utxos: UTXO[], targets: CoinSelectTarget[], feePerByte: number, fixedInputs?: UTXO[]): {
|
|
18
|
+
inputs: UTXO[];
|
|
19
|
+
outputs: CoinSelectTarget[];
|
|
20
|
+
fee: number;
|
|
21
|
+
change: CoinSelectTarget;
|
|
22
|
+
};
|
|
23
|
+
declare function decodeRawTransaction(hex: string, network: BitcoinNetwork): BitcoinTransaction;
|
|
24
|
+
declare function normalizeTransactionObject(tx: BitcoinTransaction, fee: number, block?: {
|
|
25
|
+
number: number;
|
|
26
|
+
hash: string;
|
|
27
|
+
}): Transaction<BitcoinTransaction>;
|
|
28
|
+
declare function witnessStackToScriptWitness(witness: Buffer[]): Buffer;
|
|
29
|
+
declare function getPubKeyHash(address: string, network: BitcoinNetwork): Buffer;
|
|
30
|
+
declare function validateAddress(_address: AddressType, network: BitcoinNetwork): void;
|
|
31
|
+
export { calculateFee, compressPubKey, CoinSelectTarget, selectCoins, decodeRawTransaction, normalizeTransactionObject, witnessStackToScriptWitness, AddressTypes, getPubKeyHash, validateAddress, };
|
|
@@ -0,0 +1,214 @@
|
|
|
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 __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __importStar = (this && this.__importStar) || function (mod) {
|
|
19
|
+
if (mod && mod.__esModule) return mod;
|
|
20
|
+
var result = {};
|
|
21
|
+
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
|
22
|
+
__setModuleDefault(result, mod);
|
|
23
|
+
return result;
|
|
24
|
+
};
|
|
25
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
26
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
27
|
+
};
|
|
28
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
29
|
+
exports.validateAddress = exports.getPubKeyHash = exports.AddressTypes = exports.witnessStackToScriptWitness = exports.normalizeTransactionObject = exports.decodeRawTransaction = exports.selectCoins = exports.compressPubKey = exports.calculateFee = void 0;
|
|
30
|
+
const errors_1 = require("@yaswap/errors");
|
|
31
|
+
const types_1 = require("@yaswap/types");
|
|
32
|
+
const varuint = __importStar(require("bip174/src/lib/converter/varint"));
|
|
33
|
+
const bitcoin = __importStar(require("bitcoinjs-lib"));
|
|
34
|
+
const classify = __importStar(require("bitcoinjs-lib/src/classify"));
|
|
35
|
+
const coinselect_1 = __importDefault(require("coinselect"));
|
|
36
|
+
const accumulative_1 = __importDefault(require("coinselect/accumulative"));
|
|
37
|
+
const AddressTypes = ['legacy', 'p2sh-segwit', 'bech32'];
|
|
38
|
+
exports.AddressTypes = AddressTypes;
|
|
39
|
+
function calculateFee(numInputs, numOutputs, feePerByte) {
|
|
40
|
+
return (numInputs * 148 + numOutputs * 34 + 10) * feePerByte;
|
|
41
|
+
}
|
|
42
|
+
exports.calculateFee = calculateFee;
|
|
43
|
+
/**
|
|
44
|
+
* Get compressed pubKey from pubKey.
|
|
45
|
+
* @param pubKey - 65 byte string with prefix, x, y.
|
|
46
|
+
* @returns the compressed pubKey of uncompressed pubKey.
|
|
47
|
+
*/
|
|
48
|
+
function compressPubKey(pubKey) {
|
|
49
|
+
const x = pubKey.substring(2, 66);
|
|
50
|
+
const y = pubKey.substring(66, 130);
|
|
51
|
+
const even = parseInt(y.substring(62, 64), 16) % 2 === 0;
|
|
52
|
+
const prefix = even ? '02' : '03';
|
|
53
|
+
return prefix + x;
|
|
54
|
+
}
|
|
55
|
+
exports.compressPubKey = compressPubKey;
|
|
56
|
+
function selectCoins(utxos, targets, feePerByte, fixedInputs = []) {
|
|
57
|
+
let selectUtxos = utxos;
|
|
58
|
+
// Default coinselect won't accumulate some inputs
|
|
59
|
+
// TODO: does coinselect need to be modified to ABSOLUTELY not skip an input?
|
|
60
|
+
const coinselectStrat = fixedInputs.length ? accumulative_1.default : coinselect_1.default;
|
|
61
|
+
if (fixedInputs.length) {
|
|
62
|
+
selectUtxos = [
|
|
63
|
+
// Order fixed inputs to the start of the list so they are used
|
|
64
|
+
...fixedInputs,
|
|
65
|
+
...utxos.filter((utxo) => !fixedInputs.find((input) => input.vout === utxo.vout && input.txid === utxo.txid)),
|
|
66
|
+
];
|
|
67
|
+
}
|
|
68
|
+
const { inputs, outputs, fee } = coinselectStrat(selectUtxos, targets, Math.ceil(feePerByte));
|
|
69
|
+
let change;
|
|
70
|
+
if (inputs && outputs) {
|
|
71
|
+
change = outputs.find((output) => output.id !== 'main');
|
|
72
|
+
}
|
|
73
|
+
return { inputs, outputs, fee, change };
|
|
74
|
+
}
|
|
75
|
+
exports.selectCoins = selectCoins;
|
|
76
|
+
const OUTPUT_TYPES_MAP = {
|
|
77
|
+
[classify.types.P2WPKH]: 'witness_v0_keyhash',
|
|
78
|
+
[classify.types.P2WSH]: 'witness_v0_scripthash',
|
|
79
|
+
};
|
|
80
|
+
function decodeRawTransaction(hex, network) {
|
|
81
|
+
const bjsTx = bitcoin.Transaction.fromHex(hex);
|
|
82
|
+
const vin = bjsTx.ins.map((input) => {
|
|
83
|
+
return {
|
|
84
|
+
txid: Buffer.from(input.hash).reverse().toString('hex'),
|
|
85
|
+
vout: input.index,
|
|
86
|
+
scriptSig: {
|
|
87
|
+
asm: bitcoin.script.toASM(input.script),
|
|
88
|
+
hex: input.script.toString('hex'),
|
|
89
|
+
},
|
|
90
|
+
txinwitness: input.witness.map((w) => w.toString('hex')),
|
|
91
|
+
sequence: input.sequence,
|
|
92
|
+
};
|
|
93
|
+
});
|
|
94
|
+
const vout = bjsTx.outs.map((output, n) => {
|
|
95
|
+
const type = classify.output(output.script);
|
|
96
|
+
const vout = {
|
|
97
|
+
value: output.value / 1e8,
|
|
98
|
+
n,
|
|
99
|
+
scriptPubKey: {
|
|
100
|
+
asm: bitcoin.script.toASM(output.script),
|
|
101
|
+
hex: output.script.toString('hex'),
|
|
102
|
+
reqSigs: 1,
|
|
103
|
+
type: OUTPUT_TYPES_MAP[type] || type,
|
|
104
|
+
addresses: [],
|
|
105
|
+
},
|
|
106
|
+
};
|
|
107
|
+
try {
|
|
108
|
+
const address = bitcoin.address.fromOutputScript(output.script, network);
|
|
109
|
+
vout.scriptPubKey.addresses.push(address);
|
|
110
|
+
}
|
|
111
|
+
catch (e) {
|
|
112
|
+
/** If output script is not parasable, we just skip it */
|
|
113
|
+
}
|
|
114
|
+
return vout;
|
|
115
|
+
});
|
|
116
|
+
return {
|
|
117
|
+
txid: bjsTx.getHash(false).reverse().toString('hex'),
|
|
118
|
+
hash: bjsTx.getHash(true).reverse().toString('hex'),
|
|
119
|
+
version: bjsTx.version,
|
|
120
|
+
locktime: bjsTx.locktime,
|
|
121
|
+
size: bjsTx.byteLength(),
|
|
122
|
+
vsize: bjsTx.virtualSize(),
|
|
123
|
+
weight: bjsTx.weight(),
|
|
124
|
+
vin,
|
|
125
|
+
vout,
|
|
126
|
+
hex,
|
|
127
|
+
};
|
|
128
|
+
}
|
|
129
|
+
exports.decodeRawTransaction = decodeRawTransaction;
|
|
130
|
+
function normalizeTransactionObject(tx, fee, block) {
|
|
131
|
+
const value = tx.vout.reduce((p, n) => p.plus(new types_1.BigNumber(n.value).times(1e8)), new types_1.BigNumber(0));
|
|
132
|
+
const result = {
|
|
133
|
+
hash: tx.txid,
|
|
134
|
+
value: value.toNumber(),
|
|
135
|
+
_raw: tx,
|
|
136
|
+
confirmations: 0,
|
|
137
|
+
status: tx.confirmations > 0 ? types_1.TxStatus.Success : types_1.TxStatus.Pending,
|
|
138
|
+
};
|
|
139
|
+
if (fee) {
|
|
140
|
+
const feePrice = Math.round(fee / tx.vsize);
|
|
141
|
+
Object.assign(result, {
|
|
142
|
+
fee,
|
|
143
|
+
feePrice,
|
|
144
|
+
});
|
|
145
|
+
}
|
|
146
|
+
if (block) {
|
|
147
|
+
Object.assign(result, {
|
|
148
|
+
blockHash: block.hash,
|
|
149
|
+
blockNumber: block.number,
|
|
150
|
+
confirmations: tx.confirmations,
|
|
151
|
+
});
|
|
152
|
+
}
|
|
153
|
+
return result;
|
|
154
|
+
}
|
|
155
|
+
exports.normalizeTransactionObject = normalizeTransactionObject;
|
|
156
|
+
// TODO: This is copy pasta because it's not exported from bitcoinjs-lib
|
|
157
|
+
// https://github.com/bitcoinjs/bitcoinjs-lib/blob/master/test/integration/csv.spec.ts#L477
|
|
158
|
+
function witnessStackToScriptWitness(witness) {
|
|
159
|
+
let buffer = Buffer.allocUnsafe(0);
|
|
160
|
+
function writeSlice(slice) {
|
|
161
|
+
buffer = Buffer.concat([buffer, Buffer.from(slice)]);
|
|
162
|
+
}
|
|
163
|
+
function writeVarInt(i) {
|
|
164
|
+
const currentLen = buffer.length;
|
|
165
|
+
const varintLen = varuint.encodingLength(i);
|
|
166
|
+
buffer = Buffer.concat([buffer, Buffer.allocUnsafe(varintLen)]);
|
|
167
|
+
varuint.encode(i, buffer, currentLen);
|
|
168
|
+
}
|
|
169
|
+
function writeVarSlice(slice) {
|
|
170
|
+
writeVarInt(slice.length);
|
|
171
|
+
writeSlice(slice);
|
|
172
|
+
}
|
|
173
|
+
function writeVector(vector) {
|
|
174
|
+
writeVarInt(vector.length);
|
|
175
|
+
vector.forEach(writeVarSlice);
|
|
176
|
+
}
|
|
177
|
+
writeVector(witness);
|
|
178
|
+
return buffer;
|
|
179
|
+
}
|
|
180
|
+
exports.witnessStackToScriptWitness = witnessStackToScriptWitness;
|
|
181
|
+
function getPubKeyHash(address, network) {
|
|
182
|
+
const outputScript = bitcoin.address.toOutputScript(address, network);
|
|
183
|
+
const type = classify.output(outputScript);
|
|
184
|
+
if (![classify.types.P2PKH, classify.types.P2WPKH].includes(type)) {
|
|
185
|
+
throw new Error(`Bitcoin swap doesn't support the address ${address} type of ${type}. Not possible to derive public key hash.`);
|
|
186
|
+
}
|
|
187
|
+
try {
|
|
188
|
+
const bech32 = bitcoin.address.fromBech32(address);
|
|
189
|
+
return bech32.data;
|
|
190
|
+
}
|
|
191
|
+
catch (e) {
|
|
192
|
+
const base58 = bitcoin.address.fromBase58Check(address);
|
|
193
|
+
return base58.hash;
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
exports.getPubKeyHash = getPubKeyHash;
|
|
197
|
+
function validateAddress(_address, network) {
|
|
198
|
+
const address = _address.toString();
|
|
199
|
+
if (typeof address !== 'string') {
|
|
200
|
+
throw new errors_1.InvalidAddressError(`Invalid address: ${address}`);
|
|
201
|
+
}
|
|
202
|
+
let pubKeyHash;
|
|
203
|
+
try {
|
|
204
|
+
pubKeyHash = getPubKeyHash(address, network);
|
|
205
|
+
}
|
|
206
|
+
catch (e) {
|
|
207
|
+
throw new errors_1.InvalidAddressError(`Invalid Address. Failed to parse: ${address}`);
|
|
208
|
+
}
|
|
209
|
+
if (!pubKeyHash) {
|
|
210
|
+
throw new errors_1.InvalidAddressError(`Invalid Address: ${address}`);
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
exports.validateAddress = validateAddress;
|
|
214
|
+
//# sourceMappingURL=utils.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"utils.js","sourceRoot":"","sources":["../../lib/utils.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,2CAAqD;AACrD,yCAA8E;AAC9E,yEAA2D;AAC3D,uDAAyC;AACzC,qEAAuD;AACvD,4DAAoC;AACpC,2EAA6D;AAG7D,MAAM,YAAY,GAAG,CAAC,QAAQ,EAAE,aAAa,EAAE,QAAQ,CAAC,CAAC;AAmOrD,oCAAY;AAjOhB,SAAS,YAAY,CAAC,SAAiB,EAAE,UAAkB,EAAE,UAAkB;IAC3E,OAAO,CAAC,SAAS,GAAG,GAAG,GAAG,UAAU,GAAG,EAAE,GAAG,EAAE,CAAC,GAAG,UAAU,CAAC;AACjE,CAAC;AAwNG,oCAAY;AAtNhB;;;;GAIG;AACH,SAAS,cAAc,CAAC,MAAc;IAClC,MAAM,CAAC,GAAG,MAAM,CAAC,SAAS,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC;IAClC,MAAM,CAAC,GAAG,MAAM,CAAC,SAAS,CAAC,EAAE,EAAE,GAAG,CAAC,CAAC;IACpC,MAAM,IAAI,GAAG,QAAQ,CAAC,CAAC,CAAC,SAAS,CAAC,EAAE,EAAE,EAAE,CAAC,EAAE,EAAE,CAAC,GAAG,CAAC,KAAK,CAAC,CAAC;IACzD,MAAM,MAAM,GAAG,IAAI,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,IAAI,CAAC;IAElC,OAAO,MAAM,GAAG,CAAC,CAAC;A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AK,QAAQ,EAAE;QAC7B,MAAM,IAAI,4BAAmB,CAAC,oBAAoB,OAAO,EAAE,CAAC,CAAC;KAChE;IAED,IAAI,UAAU,CAAC;IACf,IAAI;QACA,UAAU,GAAG,aAAa,CAAC,OAAO,EAAE,OAAO,CAAC,CAAC;KAChD;IAAC,OAAO,CAAC,EAAE;QACR,MAAM,IAAI,4BAAmB,CAAC,qCAAqC,OAAO,EAAE,CAAC,CAAC;KACjF;IAED,IAAI,CAAC,UAAU,EAAE;QACb,MAAM,IAAI,4BAAmB,CAAC,oBAAoB,OAAO,EAAE,CAAC,CAAC;KAChE;AACL,CAAC;AAYG,0CAAe"}
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
/// <reference types="node" />
|
|
2
|
+
import { Chain, Wallet } from '@yaswap/client';
|
|
3
|
+
import { Address, AddressType, Asset, Transaction, TransactionRequest } from '@yaswap/types';
|
|
4
|
+
import { BIP32Interface } from 'bip32';
|
|
5
|
+
import { payments } from 'bitcoinjs-lib';
|
|
6
|
+
import { BitcoinBaseChainProvider } from '../chain/BitcoinBaseChainProvider';
|
|
7
|
+
import { AddressType as BtcAddressType, BitcoinNetwork, BitcoinWalletProviderOptions, Input, OutputTarget, P2SHInput, PsbtInputTarget, Transaction as BtcTransaction, UTXO } from '../types';
|
|
8
|
+
import { CoinSelectTarget } from '../utils';
|
|
9
|
+
export declare enum AddressSearchType {
|
|
10
|
+
EXTERNAL = 0,
|
|
11
|
+
CHANGE = 1,
|
|
12
|
+
EXTERNAL_OR_CHANGE = 2
|
|
13
|
+
}
|
|
14
|
+
declare type DerivationCache = {
|
|
15
|
+
[index: string]: Address;
|
|
16
|
+
};
|
|
17
|
+
export declare abstract class BitcoinBaseWalletProvider<T extends BitcoinBaseChainProvider = any, S = any> extends Wallet<T, S> {
|
|
18
|
+
protected _baseDerivationPath: string;
|
|
19
|
+
protected _network: BitcoinNetwork;
|
|
20
|
+
protected _addressType: BtcAddressType;
|
|
21
|
+
protected _derivationCache: DerivationCache;
|
|
22
|
+
constructor(options: BitcoinWalletProviderOptions, chainProvider?: Chain<T>);
|
|
23
|
+
protected onChainProviderUpdate(chainProvider: Chain<T>): void;
|
|
24
|
+
protected abstract baseDerivationNode(): Promise<BIP32Interface>;
|
|
25
|
+
protected abstract buildTransaction(targets: OutputTarget[], feePerByte?: number, fixedInputs?: Input[]): Promise<{
|
|
26
|
+
hex: string;
|
|
27
|
+
fee: number;
|
|
28
|
+
}>;
|
|
29
|
+
protected abstract buildSweepTransaction(externalChangeAddress: string, feePerByte?: number): Promise<{
|
|
30
|
+
hex: string;
|
|
31
|
+
fee: number;
|
|
32
|
+
}>;
|
|
33
|
+
abstract signPSBT(data: string, inputs: PsbtInputTarget[]): Promise<string>;
|
|
34
|
+
abstract signBatchP2SHTransaction(inputs: P2SHInput[], addresses: string, tx: any, lockTime?: number, segwit?: boolean): Promise<Buffer[]>;
|
|
35
|
+
getDerivationCache(): DerivationCache;
|
|
36
|
+
getUnusedAddress(change?: boolean, numAddressPerCall?: number): Promise<Address>;
|
|
37
|
+
getUsedAddresses(numAddressPerCall?: number): Promise<Address[]>;
|
|
38
|
+
getAddresses(startingIndex?: number, numAddresses?: number, change?: boolean): Promise<Address[]>;
|
|
39
|
+
sendTransaction(options: TransactionRequest): Promise<Transaction<BtcTransaction>>;
|
|
40
|
+
sendBatchTransaction(transactions: TransactionRequest[]): Promise<Transaction<BtcTransaction>[]>;
|
|
41
|
+
sendSweepTransaction(externalChangeAddress: AddressType, _asset: Asset, feePerByte: number): Promise<Transaction<BtcTransaction>>;
|
|
42
|
+
updateTransactionFee(tx: Transaction<BtcTransaction> | string, newFeePerByte: number): Promise<Transaction<BtcTransaction>>;
|
|
43
|
+
getTotalFees(transactions: TransactionRequest[], max: boolean): Promise<any>;
|
|
44
|
+
protected _sendTransaction(transactions: OutputTarget[], feePerByte?: number): Promise<Transaction<BtcTransaction>>;
|
|
45
|
+
protected findAddress(addresses: string[], change?: boolean): Promise<Address>;
|
|
46
|
+
getWalletAddress(address: string): Promise<Address>;
|
|
47
|
+
protected getDerivationPathAddress(path: string): Promise<Address>;
|
|
48
|
+
protected _getUsedUnusedAddresses(numAddressPerCall: number, addressType: AddressSearchType): Promise<{
|
|
49
|
+
usedAddresses: Address[];
|
|
50
|
+
unusedAddress: {
|
|
51
|
+
change: Address;
|
|
52
|
+
external: Address;
|
|
53
|
+
};
|
|
54
|
+
}>;
|
|
55
|
+
protected withCachedUtxos(func: () => any): Promise<any>;
|
|
56
|
+
protected getTotalFee(opts: TransactionRequest, max: boolean): Promise<number>;
|
|
57
|
+
protected getInputsForAmount(_targets: OutputTarget[], feePerByte?: number, fixedInputs?: Input[], numAddressPerCall?: number, sweep?: boolean): Promise<{
|
|
58
|
+
inputs: UTXO[];
|
|
59
|
+
change: CoinSelectTarget;
|
|
60
|
+
outputs: CoinSelectTarget[];
|
|
61
|
+
fee: number;
|
|
62
|
+
}>;
|
|
63
|
+
protected sendOptionsToOutputs(transactions: TransactionRequest[]): OutputTarget[];
|
|
64
|
+
protected getAddressFromPublicKey(publicKey: Buffer): string;
|
|
65
|
+
protected getPaymentVariantFromPublicKey(publicKey: Buffer): payments.Payment;
|
|
66
|
+
}
|
|
67
|
+
export {};
|