@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,408 @@
|
|
|
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
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
12
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
13
|
+
};
|
|
14
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
15
|
+
exports.BitcoinBaseWalletProvider = exports.AddressSearchType = void 0;
|
|
16
|
+
const client_1 = require("@yaswap/client");
|
|
17
|
+
const errors_1 = require("@yaswap/errors");
|
|
18
|
+
const types_1 = require("@yaswap/types");
|
|
19
|
+
const utils_1 = require("@yaswap/utils");
|
|
20
|
+
const bitcoinjs_lib_1 = require("bitcoinjs-lib");
|
|
21
|
+
const memoizee_1 = __importDefault(require("memoizee"));
|
|
22
|
+
const types_2 = require("../types");
|
|
23
|
+
const utils_2 = require("../utils");
|
|
24
|
+
const ADDRESS_GAP = 20;
|
|
25
|
+
var AddressSearchType;
|
|
26
|
+
(function (AddressSearchType) {
|
|
27
|
+
AddressSearchType[AddressSearchType["EXTERNAL"] = 0] = "EXTERNAL";
|
|
28
|
+
AddressSearchType[AddressSearchType["CHANGE"] = 1] = "CHANGE";
|
|
29
|
+
AddressSearchType[AddressSearchType["EXTERNAL_OR_CHANGE"] = 2] = "EXTERNAL_OR_CHANGE";
|
|
30
|
+
})(AddressSearchType = exports.AddressSearchType || (exports.AddressSearchType = {}));
|
|
31
|
+
class BitcoinBaseWalletProvider extends client_1.Wallet {
|
|
32
|
+
constructor(options, chainProvider) {
|
|
33
|
+
const { baseDerivationPath, addressType = types_2.AddressType.BECH32 } = options;
|
|
34
|
+
const addressTypes = Object.values(types_2.AddressType);
|
|
35
|
+
if (!addressTypes.includes(addressType)) {
|
|
36
|
+
throw new Error(`addressType must be one of ${addressTypes.join(',')}`);
|
|
37
|
+
}
|
|
38
|
+
super(chainProvider);
|
|
39
|
+
this._baseDerivationPath = baseDerivationPath;
|
|
40
|
+
this._network = chainProvider ? chainProvider.getNetwork() : options.network;
|
|
41
|
+
this._addressType = addressType;
|
|
42
|
+
this._derivationCache = {};
|
|
43
|
+
}
|
|
44
|
+
onChainProviderUpdate(chainProvider) {
|
|
45
|
+
this._network = chainProvider.getNetwork();
|
|
46
|
+
}
|
|
47
|
+
getDerivationCache() {
|
|
48
|
+
return this._derivationCache;
|
|
49
|
+
}
|
|
50
|
+
getUnusedAddress(change = false, numAddressPerCall = 100) {
|
|
51
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
52
|
+
const addressType = change ? AddressSearchType.CHANGE : AddressSearchType.EXTERNAL;
|
|
53
|
+
const key = change ? 'change' : 'external';
|
|
54
|
+
return this._getUsedUnusedAddresses(numAddressPerCall, addressType).then(({ unusedAddress }) => unusedAddress[key]);
|
|
55
|
+
});
|
|
56
|
+
}
|
|
57
|
+
getUsedAddresses(numAddressPerCall = 100) {
|
|
58
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
59
|
+
return this._getUsedUnusedAddresses(numAddressPerCall, AddressSearchType.EXTERNAL_OR_CHANGE).then(({ usedAddresses }) => usedAddresses);
|
|
60
|
+
});
|
|
61
|
+
}
|
|
62
|
+
getAddresses(startingIndex = 0, numAddresses = 1, change = false) {
|
|
63
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
64
|
+
if (numAddresses < 1) {
|
|
65
|
+
throw new Error('You must return at least one address');
|
|
66
|
+
}
|
|
67
|
+
const addresses = [];
|
|
68
|
+
const lastIndex = startingIndex + numAddresses;
|
|
69
|
+
const changeVal = change ? '1' : '0';
|
|
70
|
+
for (let currentIndex = startingIndex; currentIndex < lastIndex; currentIndex++) {
|
|
71
|
+
const subPath = changeVal + '/' + currentIndex;
|
|
72
|
+
const path = this._baseDerivationPath + '/' + subPath;
|
|
73
|
+
const addressObject = yield this.getDerivationPathAddress(path);
|
|
74
|
+
addresses.push(addressObject);
|
|
75
|
+
yield (0, utils_1.asyncSetImmediate)();
|
|
76
|
+
}
|
|
77
|
+
return addresses;
|
|
78
|
+
});
|
|
79
|
+
}
|
|
80
|
+
sendTransaction(options) {
|
|
81
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
82
|
+
return this._sendTransaction(this.sendOptionsToOutputs([options]), options.fee);
|
|
83
|
+
});
|
|
84
|
+
}
|
|
85
|
+
sendBatchTransaction(transactions) {
|
|
86
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
87
|
+
return [yield this._sendTransaction(this.sendOptionsToOutputs(transactions))];
|
|
88
|
+
});
|
|
89
|
+
}
|
|
90
|
+
sendSweepTransaction(externalChangeAddress, _asset, feePerByte) {
|
|
91
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
92
|
+
const { hex, fee } = yield this.buildSweepTransaction(externalChangeAddress.toString(), feePerByte);
|
|
93
|
+
yield this.chainProvider.sendRawTransaction(hex);
|
|
94
|
+
return (0, utils_2.normalizeTransactionObject)((0, utils_2.decodeRawTransaction)(hex, this._network), fee);
|
|
95
|
+
});
|
|
96
|
+
}
|
|
97
|
+
updateTransactionFee(tx, newFeePerByte) {
|
|
98
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
99
|
+
const txHash = typeof tx === 'string' ? tx : tx.hash;
|
|
100
|
+
const transaction = (yield this.chainProvider.getTransactionByHash(txHash))._raw;
|
|
101
|
+
const fixedInputs = [transaction.vin[0]]; // TODO: should this pick more than 1 input? RBF doesn't mandate it
|
|
102
|
+
const lookupAddresses = transaction.vout.map((vout) => vout.scriptPubKey.addresses[0]);
|
|
103
|
+
const changeAddress = yield this.findAddress(lookupAddresses, true);
|
|
104
|
+
const changeOutput = transaction.vout.find((vout) => vout.scriptPubKey.addresses[0] === changeAddress.address);
|
|
105
|
+
let outputs = transaction.vout;
|
|
106
|
+
if (changeOutput) {
|
|
107
|
+
outputs = outputs.filter((vout) => vout.scriptPubKey.addresses[0] !== changeOutput.scriptPubKey.addresses[0]);
|
|
108
|
+
}
|
|
109
|
+
// TODO more checks?
|
|
110
|
+
const transactions = outputs.map((output) => ({
|
|
111
|
+
address: output.scriptPubKey.addresses[0],
|
|
112
|
+
value: new types_1.BigNumber(output.value).times(1e8).toNumber(),
|
|
113
|
+
}));
|
|
114
|
+
const { hex, fee } = yield this.buildTransaction(transactions, newFeePerByte, fixedInputs);
|
|
115
|
+
yield this.chainProvider.sendRawTransaction(hex);
|
|
116
|
+
return (0, utils_2.normalizeTransactionObject)((0, utils_2.decodeRawTransaction)(hex, this._network), fee);
|
|
117
|
+
});
|
|
118
|
+
}
|
|
119
|
+
getTotalFees(transactions, max) {
|
|
120
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
121
|
+
const fees = yield this.withCachedUtxos(() => __awaiter(this, void 0, void 0, function* () {
|
|
122
|
+
const fees = {};
|
|
123
|
+
for (const tx of transactions) {
|
|
124
|
+
const fee = yield this.getTotalFee(tx, max);
|
|
125
|
+
fees[tx.fee] = new types_1.BigNumber(fee);
|
|
126
|
+
}
|
|
127
|
+
return fees;
|
|
128
|
+
}));
|
|
129
|
+
return fees;
|
|
130
|
+
});
|
|
131
|
+
}
|
|
132
|
+
_sendTransaction(transactions, feePerByte) {
|
|
133
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
134
|
+
const { hex, fee } = yield this.buildTransaction(transactions, feePerByte);
|
|
135
|
+
yield this.chainProvider.sendRawTransaction(hex);
|
|
136
|
+
return (0, utils_2.normalizeTransactionObject)((0, utils_2.decodeRawTransaction)(hex, this._network), fee);
|
|
137
|
+
});
|
|
138
|
+
}
|
|
139
|
+
findAddress(addresses, change = false) {
|
|
140
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
141
|
+
// A maximum number of addresses to lookup after which it is deemed that the wallet does not contain this address
|
|
142
|
+
const maxAddresses = 5000;
|
|
143
|
+
const addressesPerCall = 50;
|
|
144
|
+
let index = 0;
|
|
145
|
+
while (index < maxAddresses) {
|
|
146
|
+
const walletAddresses = yield this.getAddresses(index, addressesPerCall, change);
|
|
147
|
+
const walletAddress = walletAddresses.find((walletAddr) => addresses.find((addr) => walletAddr.toString() === addr.toString()));
|
|
148
|
+
if (walletAddress) {
|
|
149
|
+
return walletAddress;
|
|
150
|
+
}
|
|
151
|
+
index += addressesPerCall;
|
|
152
|
+
}
|
|
153
|
+
});
|
|
154
|
+
}
|
|
155
|
+
getWalletAddress(address) {
|
|
156
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
157
|
+
const externalAddress = yield this.findAddress([address], false);
|
|
158
|
+
if (externalAddress) {
|
|
159
|
+
return externalAddress;
|
|
160
|
+
}
|
|
161
|
+
const changeAddress = yield this.findAddress([address], true);
|
|
162
|
+
if (changeAddress) {
|
|
163
|
+
return changeAddress;
|
|
164
|
+
}
|
|
165
|
+
throw new Error('Wallet does not contain address');
|
|
166
|
+
});
|
|
167
|
+
}
|
|
168
|
+
getDerivationPathAddress(path) {
|
|
169
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
170
|
+
if (path in this._derivationCache) {
|
|
171
|
+
return this._derivationCache[path];
|
|
172
|
+
}
|
|
173
|
+
const baseDerivationNode = yield this.baseDerivationNode();
|
|
174
|
+
const subPath = path.replace(this._baseDerivationPath + '/', '');
|
|
175
|
+
const publicKey = baseDerivationNode.derivePath(subPath).publicKey;
|
|
176
|
+
const address = this.getAddressFromPublicKey(publicKey);
|
|
177
|
+
const addressObject = new types_1.Address({
|
|
178
|
+
address,
|
|
179
|
+
publicKey: publicKey.toString('hex'),
|
|
180
|
+
derivationPath: path,
|
|
181
|
+
});
|
|
182
|
+
this._derivationCache[path] = addressObject;
|
|
183
|
+
return addressObject;
|
|
184
|
+
});
|
|
185
|
+
}
|
|
186
|
+
_getUsedUnusedAddresses(numAddressPerCall = 100, addressType) {
|
|
187
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
188
|
+
const usedAddresses = [];
|
|
189
|
+
const addressCountMap = { change: 0, external: 0 };
|
|
190
|
+
const unusedAddressMap = { change: null, external: null };
|
|
191
|
+
let addrList;
|
|
192
|
+
let addressIndex = 0;
|
|
193
|
+
let changeAddresses = [];
|
|
194
|
+
let externalAddresses = [];
|
|
195
|
+
/* eslint-disable no-unmodified-loop-condition */
|
|
196
|
+
while ((addressType === AddressSearchType.EXTERNAL_OR_CHANGE &&
|
|
197
|
+
(addressCountMap.change < ADDRESS_GAP || addressCountMap.external < ADDRESS_GAP)) ||
|
|
198
|
+
(addressType === AddressSearchType.EXTERNAL && addressCountMap.external < ADDRESS_GAP) ||
|
|
199
|
+
(addressType === AddressSearchType.CHANGE && addressCountMap.change < ADDRESS_GAP)) {
|
|
200
|
+
/* eslint-enable no-unmodified-loop-condition */
|
|
201
|
+
addrList = [];
|
|
202
|
+
if ((addressType === AddressSearchType.EXTERNAL_OR_CHANGE || addressType === AddressSearchType.CHANGE) &&
|
|
203
|
+
addressCountMap.change < ADDRESS_GAP) {
|
|
204
|
+
// Scanning for change addr
|
|
205
|
+
changeAddresses = yield this.getAddresses(addressIndex, numAddressPerCall, true);
|
|
206
|
+
addrList = addrList.concat(changeAddresses);
|
|
207
|
+
}
|
|
208
|
+
else {
|
|
209
|
+
changeAddresses = [];
|
|
210
|
+
}
|
|
211
|
+
if ((addressType === AddressSearchType.EXTERNAL_OR_CHANGE || addressType === AddressSearchType.EXTERNAL) &&
|
|
212
|
+
addressCountMap.external < ADDRESS_GAP) {
|
|
213
|
+
// Scanning for non change addr
|
|
214
|
+
externalAddresses = yield this.getAddresses(addressIndex, numAddressPerCall, false);
|
|
215
|
+
addrList = addrList.concat(externalAddresses);
|
|
216
|
+
}
|
|
217
|
+
const transactionCounts = yield this.chainProvider.getProvider().getAddressTransactionCounts(addrList);
|
|
218
|
+
for (const address of addrList) {
|
|
219
|
+
const isUsed = transactionCounts[address.toString()] > 0;
|
|
220
|
+
const isChangeAddress = changeAddresses.find((a) => address.toString() === a.toString());
|
|
221
|
+
const key = isChangeAddress ? 'change' : 'external';
|
|
222
|
+
if (isUsed) {
|
|
223
|
+
usedAddresses.push(address);
|
|
224
|
+
addressCountMap[key] = 0;
|
|
225
|
+
unusedAddressMap[key] = null;
|
|
226
|
+
}
|
|
227
|
+
else {
|
|
228
|
+
addressCountMap[key]++;
|
|
229
|
+
if (!unusedAddressMap[key]) {
|
|
230
|
+
unusedAddressMap[key] = address;
|
|
231
|
+
}
|
|
232
|
+
}
|
|
233
|
+
}
|
|
234
|
+
addressIndex += numAddressPerCall;
|
|
235
|
+
}
|
|
236
|
+
return {
|
|
237
|
+
usedAddresses,
|
|
238
|
+
unusedAddress: unusedAddressMap,
|
|
239
|
+
};
|
|
240
|
+
});
|
|
241
|
+
}
|
|
242
|
+
withCachedUtxos(func) {
|
|
243
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
244
|
+
const originalProvider = this.chainProvider.getProvider();
|
|
245
|
+
const memoizedGetFeePerByte = (0, memoizee_1.default)(originalProvider.getFeePerByte, { primitive: true });
|
|
246
|
+
const memoizedGetUnspentTransactions = (0, memoizee_1.default)(originalProvider.getUnspentTransactions, { primitive: true });
|
|
247
|
+
const memoizedGetAddressTransactionCounts = (0, memoizee_1.default)(originalProvider.getAddressTransactionCounts, { primitive: true });
|
|
248
|
+
const newProvider = originalProvider;
|
|
249
|
+
newProvider.getFeePerByte = memoizedGetFeePerByte;
|
|
250
|
+
newProvider.getUnspentTransactions = memoizedGetUnspentTransactions;
|
|
251
|
+
newProvider.getAddressTransactionCounts = memoizedGetAddressTransactionCounts;
|
|
252
|
+
this.chainProvider.setProvider(newProvider);
|
|
253
|
+
const result = yield func.bind(this)();
|
|
254
|
+
this.chainProvider.setProvider(originalProvider);
|
|
255
|
+
return result;
|
|
256
|
+
});
|
|
257
|
+
}
|
|
258
|
+
getTotalFee(opts, max) {
|
|
259
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
260
|
+
const targets = this.sendOptionsToOutputs([opts]);
|
|
261
|
+
if (!max) {
|
|
262
|
+
const { fee } = yield this.getInputsForAmount(targets, opts.fee);
|
|
263
|
+
return fee;
|
|
264
|
+
}
|
|
265
|
+
else {
|
|
266
|
+
const { fee } = yield this.getInputsForAmount(targets.filter((t) => !t.value), opts.fee, [], 100, true);
|
|
267
|
+
return fee;
|
|
268
|
+
}
|
|
269
|
+
});
|
|
270
|
+
}
|
|
271
|
+
getInputsForAmount(_targets, feePerByte, fixedInputs = [], numAddressPerCall = 100, sweep = false) {
|
|
272
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
273
|
+
let addressIndex = 0;
|
|
274
|
+
let changeAddresses = [];
|
|
275
|
+
let externalAddresses = [];
|
|
276
|
+
const addressCountMap = {
|
|
277
|
+
change: 0,
|
|
278
|
+
nonChange: 0,
|
|
279
|
+
};
|
|
280
|
+
const feePerBytePromise = this.chainProvider.getProvider().getFeePerByte();
|
|
281
|
+
let utxos = [];
|
|
282
|
+
while (addressCountMap.change < ADDRESS_GAP || addressCountMap.nonChange < ADDRESS_GAP) {
|
|
283
|
+
let addrList = [];
|
|
284
|
+
if (addressCountMap.change < ADDRESS_GAP) {
|
|
285
|
+
// Scanning for change addr
|
|
286
|
+
changeAddresses = yield this.getAddresses(addressIndex, numAddressPerCall, true);
|
|
287
|
+
addrList = addrList.concat(changeAddresses);
|
|
288
|
+
}
|
|
289
|
+
else {
|
|
290
|
+
changeAddresses = [];
|
|
291
|
+
}
|
|
292
|
+
if (addressCountMap.nonChange < ADDRESS_GAP) {
|
|
293
|
+
// Scanning for non change addr
|
|
294
|
+
externalAddresses = yield this.getAddresses(addressIndex, numAddressPerCall, false);
|
|
295
|
+
addrList = addrList.concat(externalAddresses);
|
|
296
|
+
}
|
|
297
|
+
const fixedUtxos = [];
|
|
298
|
+
if (fixedInputs.length > 0) {
|
|
299
|
+
for (const input of fixedInputs) {
|
|
300
|
+
const txHex = yield this.chainProvider.getProvider().getRawTransactionByHash(input.txid);
|
|
301
|
+
const tx = (0, utils_2.decodeRawTransaction)(txHex, this._network);
|
|
302
|
+
const value = new types_1.BigNumber(tx.vout[input.vout].value).times(1e8).toNumber();
|
|
303
|
+
const address = tx.vout[input.vout].scriptPubKey.addresses[0];
|
|
304
|
+
const walletAddress = yield this.getWalletAddress(address);
|
|
305
|
+
const utxo = Object.assign(Object.assign({}, input), { value, address, derivationPath: walletAddress.derivationPath });
|
|
306
|
+
fixedUtxos.push(utxo);
|
|
307
|
+
}
|
|
308
|
+
}
|
|
309
|
+
if (!sweep || fixedUtxos.length === 0) {
|
|
310
|
+
const _utxos = yield this.chainProvider.getProvider().getUnspentTransactions(addrList);
|
|
311
|
+
utxos.push(..._utxos.map((utxo) => {
|
|
312
|
+
const addr = addrList.find((a) => a.address === utxo.address);
|
|
313
|
+
return Object.assign(Object.assign({}, utxo), { derivationPath: addr.derivationPath });
|
|
314
|
+
}));
|
|
315
|
+
}
|
|
316
|
+
else {
|
|
317
|
+
utxos = fixedUtxos;
|
|
318
|
+
}
|
|
319
|
+
const utxoBalance = utxos.reduce((a, b) => a + (b.value || 0), 0);
|
|
320
|
+
const transactionCounts = yield this.chainProvider.getProvider().getAddressTransactionCounts(addrList);
|
|
321
|
+
if (!feePerByte)
|
|
322
|
+
feePerByte = yield feePerBytePromise;
|
|
323
|
+
const minRelayFee = yield this.chainProvider.getProvider().getMinRelayFee();
|
|
324
|
+
if (feePerByte < minRelayFee) {
|
|
325
|
+
throw new Error(`Fee supplied (${feePerByte} sat/b) too low. Minimum relay fee is ${minRelayFee} sat/b`);
|
|
326
|
+
}
|
|
327
|
+
let targets;
|
|
328
|
+
if (sweep) {
|
|
329
|
+
const outputBalance = _targets.reduce((a, b) => a + (b['value'] || 0), 0);
|
|
330
|
+
const sweepOutputSize = 39;
|
|
331
|
+
const paymentOutputSize = _targets.filter((t) => t.value && t.address).length * 39;
|
|
332
|
+
const scriptOutputSize = _targets
|
|
333
|
+
.filter((t) => !t.value && t.script)
|
|
334
|
+
.reduce((size, t) => size + 39 + t.script.byteLength, 0);
|
|
335
|
+
const outputSize = sweepOutputSize + paymentOutputSize + scriptOutputSize;
|
|
336
|
+
const inputSize = utxos.length * 153;
|
|
337
|
+
const sweepFee = feePerByte * (inputSize + outputSize);
|
|
338
|
+
const amountToSend = new types_1.BigNumber(utxoBalance).minus(sweepFee);
|
|
339
|
+
targets = _targets.map((target) => ({ id: 'main', value: target.value, script: target.script }));
|
|
340
|
+
targets.push({ id: 'main', value: amountToSend.minus(outputBalance).toNumber() });
|
|
341
|
+
}
|
|
342
|
+
else {
|
|
343
|
+
targets = _targets.map((target) => ({ id: 'main', value: target.value, script: target.script }));
|
|
344
|
+
}
|
|
345
|
+
const { inputs, outputs, change, fee } = (0, utils_2.selectCoins)(utxos, targets, Math.ceil(feePerByte), fixedUtxos);
|
|
346
|
+
if (inputs && outputs) {
|
|
347
|
+
return {
|
|
348
|
+
inputs,
|
|
349
|
+
change,
|
|
350
|
+
outputs,
|
|
351
|
+
fee,
|
|
352
|
+
};
|
|
353
|
+
}
|
|
354
|
+
for (const address of addrList) {
|
|
355
|
+
const isUsed = transactionCounts[address.address];
|
|
356
|
+
const isChangeAddress = changeAddresses.find((a) => address.address === a.address);
|
|
357
|
+
const key = isChangeAddress ? 'change' : 'nonChange';
|
|
358
|
+
if (isUsed) {
|
|
359
|
+
addressCountMap[key] = 0;
|
|
360
|
+
}
|
|
361
|
+
else {
|
|
362
|
+
addressCountMap[key]++;
|
|
363
|
+
}
|
|
364
|
+
}
|
|
365
|
+
addressIndex += numAddressPerCall;
|
|
366
|
+
}
|
|
367
|
+
throw new errors_1.InsufficientBalanceError('Not enough balance');
|
|
368
|
+
});
|
|
369
|
+
}
|
|
370
|
+
sendOptionsToOutputs(transactions) {
|
|
371
|
+
const targets = [];
|
|
372
|
+
transactions.forEach((tx) => {
|
|
373
|
+
if (tx.to && tx.value && tx.value.gt(0)) {
|
|
374
|
+
targets.push({
|
|
375
|
+
address: tx.to.toString(),
|
|
376
|
+
value: tx.value.toNumber(),
|
|
377
|
+
});
|
|
378
|
+
}
|
|
379
|
+
if (tx.data) {
|
|
380
|
+
const scriptBuffer = bitcoinjs_lib_1.script.compile([bitcoinjs_lib_1.script.OPS.OP_RETURN, Buffer.from(tx.data, 'hex')]);
|
|
381
|
+
targets.push({
|
|
382
|
+
value: 0,
|
|
383
|
+
script: scriptBuffer,
|
|
384
|
+
});
|
|
385
|
+
}
|
|
386
|
+
});
|
|
387
|
+
return targets;
|
|
388
|
+
}
|
|
389
|
+
getAddressFromPublicKey(publicKey) {
|
|
390
|
+
return this.getPaymentVariantFromPublicKey(publicKey).address;
|
|
391
|
+
}
|
|
392
|
+
getPaymentVariantFromPublicKey(publicKey) {
|
|
393
|
+
if (this._addressType === types_2.AddressType.LEGACY) {
|
|
394
|
+
return bitcoinjs_lib_1.payments.p2pkh({ pubkey: publicKey, network: this._network });
|
|
395
|
+
}
|
|
396
|
+
else if (this._addressType === types_2.AddressType.P2SH_SEGWIT) {
|
|
397
|
+
return bitcoinjs_lib_1.payments.p2sh({
|
|
398
|
+
redeem: bitcoinjs_lib_1.payments.p2wpkh({ pubkey: publicKey, network: this._network }),
|
|
399
|
+
network: this._network,
|
|
400
|
+
});
|
|
401
|
+
}
|
|
402
|
+
else if (this._addressType === types_2.AddressType.BECH32) {
|
|
403
|
+
return bitcoinjs_lib_1.payments.p2wpkh({ pubkey: publicKey, network: this._network });
|
|
404
|
+
}
|
|
405
|
+
}
|
|
406
|
+
}
|
|
407
|
+
exports.BitcoinBaseWalletProvider = BitcoinBaseWalletProvider;
|
|
408
|
+
//# sourceMappingURL=BitcoinBaseWallet.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"BitcoinBaseWallet.js","sourceRoot":"","sources":["../../../lib/wallet/BitcoinBaseWallet.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;AAAA,2CAA+C;AAC/C,2CAA0D;AAC1D,yCAAwG;AACxG,yCAAkD;AAElD,iDAAiD;AACjD,wDAA+B;AAE/B,oCAWkB;AAClB,oCAA2G;AAE3G,MAAM,WAAW,GAAG,EAAE,CAAC;AAEvB,IAAY,iBAIX;AAJD,WAAY,iBAAiB;IACzB,iEAAQ,CAAA;IACR,6DAAM,CAAA;IACN,qFAAkB,CAAA;AACtB,CAAC,EAJW,iBAAiB,GAAjB,yBAAiB,KAAjB,yBAAiB,QAI5B;AAID,MAAsB,yBAA6E,SAAQ,eAAY;IAMnH,YAAY,OAAqC,EAAE,aAAwB;QACvE,MAAM,EAAE,kBAAkB,EAAE,WAAW,GAAG,mBAAc,CAAC,MAAM,EAAE,GAAG,OAAO,CAAC;QAC5E,MAAM,YAAY,GAAG,MAAM,CAAC,MAAM,CAAC,mBAAc,CAAC,CAAC;QACnD,IAAI,CAAC,YAAY,CAAC,QAAQ,CAAC,WAAW,CAAC,EAAE;YACrC,MAAM,IAAI,KAAK,CAAC,8BAA8B,YAAY,CAAC,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,CAAC;SAC3E;QAED,KAAK,CAAC,aAAa,CAAC,CAAC;QAErB,IAAI,CAAC,mBAAmB,GAAG,kBAAkB,CAAC;QAC9C,IAAI,CAAC,QAAQ,GAAG,aAAa,CAAC,CAAC,CAAE,aAAa,CAAC,UAAU,EAAqB,CAAC,CAAC,CAAC,OAAO,CAAC,OAAO,CAAC;QACjG,IAAI,CAAC,YAAY,GAAG,W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,CAAC,MAAM,CAAC,EAAE,MAAM,EAAE,SAAS,EAAE,OAAO,EAAE,IAAI,CAAC,QAAQ,EAAE,CAAC;gBACtE,OAAO,EAAE,IAAI,CAAC,QAAQ;aACzB,CAAC,CAAC;SACN;aAAM,IAAI,IAAI,CAAC,YAAY,KAAK,mBAAc,CAAC,MAAM,EAAE;YACpD,OAAO,wBAAQ,CAAC,MAAM,CAAC,EAAE,MAAM,EAAE,SAAS,EAAE,OAAO,EAAE,IAAI,CAAC,QAAQ,EAAE,CAAC,CAAC;SACzE;IACL,CAAC;CACJ;AAjcD,8DAicC"}
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
/// <reference types="node" />
|
|
2
|
+
import { Chain } from '@yaswap/client';
|
|
3
|
+
import { AddressType, Asset, BigNumber } from '@yaswap/types';
|
|
4
|
+
import { BIP32Interface } from 'bip32';
|
|
5
|
+
import { BitcoinBaseChainProvider } from '../chain/BitcoinBaseChainProvider';
|
|
6
|
+
import { BitcoinHDWalletProviderOptions, Input, OutputTarget, PsbtInputTarget } from '../types';
|
|
7
|
+
import { BitcoinBaseWalletProvider } from './BitcoinBaseWallet';
|
|
8
|
+
import { IBitcoinWallet } from './IBitcoinWallet';
|
|
9
|
+
export declare class BitcoinHDWalletProvider extends BitcoinBaseWalletProvider implements IBitcoinWallet<BitcoinBaseChainProvider> {
|
|
10
|
+
private _mnemonic;
|
|
11
|
+
private _seedNode;
|
|
12
|
+
private _baseDerivationNode;
|
|
13
|
+
constructor(options: BitcoinHDWalletProviderOptions, chainProvider?: Chain<BitcoinBaseChainProvider>);
|
|
14
|
+
canUpdateFee(): boolean;
|
|
15
|
+
getSigner(): Promise<null>;
|
|
16
|
+
getAddress(): Promise<AddressType>;
|
|
17
|
+
getBalance(_assets: Asset[]): Promise<BigNumber[]>;
|
|
18
|
+
signMessage(message: string, from: string): Promise<string>;
|
|
19
|
+
exportPrivateKey(): Promise<string>;
|
|
20
|
+
getConnectedNetwork(): Promise<import("../types").BitcoinNetwork>;
|
|
21
|
+
isWalletAvailable(): Promise<boolean>;
|
|
22
|
+
protected baseDerivationNode(): Promise<BIP32Interface>;
|
|
23
|
+
protected buildTransaction(targets: OutputTarget[], feePerByte?: number, fixedInputs?: Input[]): Promise<{
|
|
24
|
+
hex: string;
|
|
25
|
+
fee: number;
|
|
26
|
+
}>;
|
|
27
|
+
protected buildSweepTransaction(externalChangeAddress: string, feePerByte: number): Promise<{
|
|
28
|
+
hex: string;
|
|
29
|
+
fee: number;
|
|
30
|
+
}>;
|
|
31
|
+
signPSBT(data: string, inputs: PsbtInputTarget[]): Promise<string>;
|
|
32
|
+
signBatchP2SHTransaction(inputs: [{
|
|
33
|
+
inputTxHex: string;
|
|
34
|
+
index: number;
|
|
35
|
+
vout: any;
|
|
36
|
+
outputScript: Buffer;
|
|
37
|
+
txInputIndex?: number;
|
|
38
|
+
}], addresses: string, tx: any, lockTime?: number, segwit?: boolean): Promise<Buffer[]>;
|
|
39
|
+
private keyPair;
|
|
40
|
+
private _toWIF;
|
|
41
|
+
private seedNode;
|
|
42
|
+
}
|