@airgap/bitcoin 0.13.45-beta.2 → 0.13.45-beta.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +5 -5
- package/v0/index.js +10 -10
- package/v0/index.js.map +1 -1
- package/v0/protocol/BitcoinAddress.js +8 -9
- package/v0/protocol/BitcoinAddress.js.map +1 -1
- package/v0/protocol/BitcoinCryptoClient.js +16 -85
- package/v0/protocol/BitcoinCryptoClient.js.map +1 -1
- package/v0/protocol/BitcoinProtocol.js +590 -987
- package/v0/protocol/BitcoinProtocol.js.map +1 -1
- package/v0/protocol/BitcoinProtocolOptions.js +45 -111
- package/v0/protocol/BitcoinProtocolOptions.js.map +1 -1
- package/v0/protocol/BitcoinSegwitAddress.js +12 -29
- package/v0/protocol/BitcoinSegwitAddress.js.map +1 -1
- package/v0/protocol/BitcoinSegwitProtocol.js +348 -483
- package/v0/protocol/BitcoinSegwitProtocol.js.map +1 -1
- package/v0/protocol/BitcoinTestnetProtocol.js +28 -36
- package/v0/protocol/BitcoinTestnetProtocol.js.map +1 -1
- package/v0/serializer/validators/transaction-validator.js +22 -30
- package/v0/serializer/validators/transaction-validator.js.map +1 -1
- package/v0/serializer/validators/validators.js +23 -23
- package/v0/serializer/validators/validators.js.map +1 -1
- package/v1/block-explorer/BlockCypherBlockExplorer.js +12 -61
- package/v1/block-explorer/BlockCypherBlockExplorer.js.map +1 -1
- package/v1/data/BitcoinAddress.js +9 -10
- package/v1/data/BitcoinAddress.js.map +1 -1
- package/v1/data/BitcoinLegacyAddress.js +11 -12
- package/v1/data/BitcoinLegacyAddress.js.map +1 -1
- package/v1/data/BitcoinSegwitAddress.js +11 -12
- package/v1/data/BitcoinSegwitAddress.js.map +1 -1
- package/v1/data/BitcoinTaprootAddress.js +31 -22
- package/v1/data/BitcoinTaprootAddress.js.map +1 -1
- package/v1/index.js +11 -11
- package/v1/index.js.map +1 -1
- package/v1/module/BitcoinModule.d.ts +1 -1
- package/v1/module/BitcoinModule.js +44 -102
- package/v1/module/BitcoinModule.js.map +1 -1
- package/v1/module.js +2 -3
- package/v1/module.js.map +1 -1
- package/v1/protocol/BitcoinCryptoClient.js +22 -90
- package/v1/protocol/BitcoinCryptoClient.js.map +1 -1
- package/v1/protocol/BitcoinLegacyProtocol.js +520 -796
- package/v1/protocol/BitcoinLegacyProtocol.js.map +1 -1
- package/v1/protocol/BitcoinProtocol.js +735 -1169
- package/v1/protocol/BitcoinProtocol.js.map +1 -1
- package/v1/protocol/BitcoinSegwitProtocol.js +542 -796
- package/v1/protocol/BitcoinSegwitProtocol.js.map +1 -1
- package/v1/protocol/BitcoinTaprootProtocol.js +688 -1000
- package/v1/protocol/BitcoinTaprootProtocol.js.map +1 -1
- package/v1/protocol/BitcoinTestnetProtocol.js +14 -33
- package/v1/protocol/BitcoinTestnetProtocol.js.map +1 -1
- package/v1/serializer/v3/schemas/converter/transaction-converter.js +29 -52
- package/v1/serializer/v3/schemas/converter/transaction-converter.js.map +1 -1
- package/v1/serializer/v3/serializer-companion.js +98 -165
- package/v1/serializer/v3/serializer-companion.js.map +1 -1
- package/v1/serializer/v3/validators/transaction-validator.js +13 -16
- package/v1/serializer/v3/validators/transaction-validator.js.map +1 -1
- package/v1/serializer/v3/validators/validators.js +122 -213
- package/v1/serializer/v3/validators/validators.js.map +1 -1
- package/v1/types/crypto.d.ts +1 -1
- package/v1/types/key.d.ts +6 -6
- package/v1/types/protocol.d.ts +2 -2
- package/v1/types/transaction.d.ts +3 -3
- package/v1/utils/common.js +4 -6
- package/v1/utils/common.js.map +1 -1
- package/v1/utils/key.d.ts +5 -6
- package/v1/utils/key.js +38 -39
- package/v1/utils/key.js.map +1 -1
- package/v1/utils/network.js +3 -4
- package/v1/utils/network.js.map +1 -1
- package/v1/utils/protocol.js +25 -19
- package/v1/utils/protocol.js.map +1 -1
- package/v1/utils/signature.js +4 -5
- package/v1/utils/signature.js.map +1 -1
|
@@ -1,15 +1,4 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
var __assign = (this && this.__assign) || function () {
|
|
3
|
-
__assign = Object.assign || function(t) {
|
|
4
|
-
for (var s, i = 1, n = arguments.length; i < n; i++) {
|
|
5
|
-
s = arguments[i];
|
|
6
|
-
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
|
|
7
|
-
t[p] = s[p];
|
|
8
|
-
}
|
|
9
|
-
return t;
|
|
10
|
-
};
|
|
11
|
-
return __assign.apply(this, arguments);
|
|
12
|
-
};
|
|
13
2
|
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
14
3
|
if (k2 === undefined) k2 = k;
|
|
15
4
|
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
@@ -26,83 +15,47 @@ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (
|
|
|
26
15
|
}) : function(o, v) {
|
|
27
16
|
o["default"] = v;
|
|
28
17
|
});
|
|
29
|
-
var __importStar = (this && this.__importStar) || function (
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
};
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
return
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
};
|
|
45
|
-
|
|
46
|
-
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
|
|
47
|
-
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
|
|
48
|
-
function verb(n) { return function (v) { return step([n, v]); }; }
|
|
49
|
-
function step(op) {
|
|
50
|
-
if (f) throw new TypeError("Generator is already executing.");
|
|
51
|
-
while (_) try {
|
|
52
|
-
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
|
|
53
|
-
if (y = 0, t) op = [op[0] & 2, t.value];
|
|
54
|
-
switch (op[0]) {
|
|
55
|
-
case 0: case 1: t = op; break;
|
|
56
|
-
case 4: _.label++; return { value: op[1], done: false };
|
|
57
|
-
case 5: _.label++; y = op[1]; op = [0]; continue;
|
|
58
|
-
case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
|
59
|
-
default:
|
|
60
|
-
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
|
61
|
-
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
|
62
|
-
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
|
63
|
-
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
|
64
|
-
if (t[2]) _.ops.pop();
|
|
65
|
-
_.trys.pop(); continue;
|
|
66
|
-
}
|
|
67
|
-
op = body.call(thisArg, _);
|
|
68
|
-
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
|
69
|
-
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
|
70
|
-
}
|
|
71
|
-
};
|
|
72
|
-
var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
|
|
73
|
-
if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
|
|
74
|
-
if (ar || !(i in from)) {
|
|
75
|
-
if (!ar) ar = Array.prototype.slice.call(from, 0, i);
|
|
76
|
-
ar[i] = from[i];
|
|
77
|
-
}
|
|
78
|
-
}
|
|
79
|
-
return to.concat(ar || Array.prototype.slice.call(from));
|
|
80
|
-
};
|
|
18
|
+
var __importStar = (this && this.__importStar) || (function () {
|
|
19
|
+
var ownKeys = function(o) {
|
|
20
|
+
ownKeys = Object.getOwnPropertyNames || function (o) {
|
|
21
|
+
var ar = [];
|
|
22
|
+
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
|
23
|
+
return ar;
|
|
24
|
+
};
|
|
25
|
+
return ownKeys(o);
|
|
26
|
+
};
|
|
27
|
+
return function (mod) {
|
|
28
|
+
if (mod && mod.__esModule) return mod;
|
|
29
|
+
var result = {};
|
|
30
|
+
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
|
31
|
+
__setModuleDefault(result, mod);
|
|
32
|
+
return result;
|
|
33
|
+
};
|
|
34
|
+
})();
|
|
81
35
|
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
82
36
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
83
37
|
};
|
|
84
38
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
85
|
-
exports.
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
39
|
+
exports.BitcoinLegacyProtocolImpl = void 0;
|
|
40
|
+
exports.createBitcoinLegacyProtocol = createBitcoinLegacyProtocol;
|
|
41
|
+
const coinlib_core_1 = require("@airgap/coinlib-core");
|
|
42
|
+
const index_1 = __importDefault(require("@airgap/coinlib-core/dependencies/src/axios-0.19.0/index"));
|
|
43
|
+
const bignumber_1 = __importDefault(require("@airgap/coinlib-core/dependencies/src/bignumber.js-9.0.0/bignumber"));
|
|
44
|
+
const errors_1 = require("@airgap/coinlib-core/errors");
|
|
45
|
+
const crypto_1 = require("@airgap/crypto");
|
|
46
|
+
const module_kit_1 = require("@airgap/module-kit");
|
|
47
|
+
const bitcoin = __importStar(require("bitcoinjs-lib"));
|
|
48
|
+
const common_1 = require("../utils/common");
|
|
49
|
+
const key_1 = require("../utils/key");
|
|
50
|
+
const network_1 = require("../utils/network");
|
|
51
|
+
const bip32_1 = require("bip32");
|
|
52
|
+
const secp256k1_1 = __importDefault(require("@bitcoinerlab/secp256k1"));
|
|
53
|
+
const BitcoinProtocol_1 = require("./BitcoinProtocol");
|
|
54
|
+
const BitcoinLegacyAddress_1 = require("../data/BitcoinLegacyAddress");
|
|
100
55
|
// Implementation
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
if (options === void 0) { options = {}; }
|
|
105
|
-
if (bitcoinJS === void 0) { bitcoinJS = bitcoin; }
|
|
56
|
+
const DUST_AMOUNT = 50;
|
|
57
|
+
class BitcoinLegacyProtocolImpl {
|
|
58
|
+
constructor(options = {}, bitcoinJS = bitcoin) {
|
|
106
59
|
this._isBitcoinProtocol = true;
|
|
107
60
|
this.bip32 = (0, bip32_1.BIP32Factory)(secp256k1_1.default);
|
|
108
61
|
this.options = (0, BitcoinProtocol_1.createBitcoinProtocolOptions)(options.network);
|
|
@@ -116,743 +69,514 @@ var BitcoinLegacyProtocolImpl = /** @class */ (function () {
|
|
|
116
69
|
this.legacy = new BitcoinProtocol_1.BitcoinProtocolImpl(options);
|
|
117
70
|
this.metadata = this.legacy.metadata;
|
|
118
71
|
}
|
|
119
|
-
|
|
120
|
-
return
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
72
|
+
async getMetadata() {
|
|
73
|
+
return this.metadata;
|
|
74
|
+
}
|
|
75
|
+
async getAddressFromPublicKey(publicKey) {
|
|
76
|
+
switch (publicKey.type) {
|
|
77
|
+
case 'pub':
|
|
78
|
+
return this.getAddressFromNonExtendedPublicKey(publicKey);
|
|
79
|
+
case 'xpub':
|
|
80
|
+
return this.getAddressFromExtendedPublicKey(publicKey);
|
|
81
|
+
default:
|
|
82
|
+
(0, coinlib_core_1.assertNever)(publicKey);
|
|
83
|
+
throw new errors_1.UnsupportedError(coinlib_core_1.Domain.BITCOIN, 'Public key type is not supported.');
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
async getAddressFromNonExtendedPublicKey(publicKey) {
|
|
87
|
+
const hexPublicKey = (0, key_1.convertPublicKey)(publicKey, 'hex');
|
|
88
|
+
const payment = this.bitcoinJS.lib.payments.p2pkh({ pubkey: Buffer.from(hexPublicKey.value, 'hex') });
|
|
89
|
+
return BitcoinLegacyAddress_1.BitcoinLegacyAddress.fromPayment(payment).asString();
|
|
90
|
+
}
|
|
91
|
+
async getAddressFromExtendedPublicKey(extendedPublicKey) {
|
|
92
|
+
const encodedExtendedPublicKey = (0, key_1.convertExtendedPublicKey)(extendedPublicKey, { format: 'encoded', type: 'xpub' });
|
|
93
|
+
const bip32 = this.bip32.fromBase58(encodedExtendedPublicKey.value, this.bitcoinJS.config.network);
|
|
94
|
+
return BitcoinLegacyAddress_1.BitcoinLegacyAddress.fromBip32(bip32).asString();
|
|
95
|
+
}
|
|
96
|
+
async deriveFromExtendedPublicKey(extendedPublicKey, visibilityIndex, addressIndex) {
|
|
97
|
+
const encodedPublicKey = (0, key_1.convertExtendedPublicKey)(extendedPublicKey, { format: 'encoded', type: 'xpub' });
|
|
98
|
+
const derivedBip32 = this.bip32
|
|
99
|
+
.fromBase58(encodedPublicKey.value, this.bitcoinJS.config.network)
|
|
100
|
+
.derive(visibilityIndex)
|
|
101
|
+
.derive(addressIndex);
|
|
102
|
+
return (0, module_kit_1.newPublicKey)(Buffer.from(derivedBip32.publicKey).toString('hex'), 'hex');
|
|
103
|
+
}
|
|
104
|
+
async getDetailsFromTransaction(transaction, _publicKey) {
|
|
105
|
+
return this.getDetailsFromPSBT(transaction.psbt, _publicKey);
|
|
106
|
+
}
|
|
107
|
+
async getDetailsFromPSBT(psbt, publickey) {
|
|
108
|
+
const decodedPSBT = this.bitcoinJS.lib.Psbt.fromHex(psbt);
|
|
109
|
+
let inTotal = new bignumber_1.default(0);
|
|
110
|
+
for (let i = 0; i < decodedPSBT.data.inputs.length; i++) {
|
|
111
|
+
const input = decodedPSBT.data.inputs[i];
|
|
112
|
+
if (input.witnessUtxo) {
|
|
113
|
+
inTotal = inTotal.plus(input.witnessUtxo.value);
|
|
114
|
+
}
|
|
115
|
+
else if (input.nonWitnessUtxo) {
|
|
116
|
+
// bitcoinjs-lib v6: prevout index comes from the underlying unsigned tx
|
|
117
|
+
const prevIndex = decodedPSBT.txInputs[i].index;
|
|
118
|
+
const prevTx = this.bitcoinJS.lib.Transaction.fromBuffer(input.nonWitnessUtxo);
|
|
119
|
+
inTotal = inTotal.plus(prevTx.outs[prevIndex].value);
|
|
120
|
+
}
|
|
121
|
+
else {
|
|
122
|
+
throw new errors_1.UnsupportedError(coinlib_core_1.Domain.BITCOIN, 'PSBT input missing UTXO data.');
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
let fee = inTotal;
|
|
126
|
+
for (const o of decodedPSBT.txOutputs) {
|
|
127
|
+
fee = fee.minus(o.value);
|
|
128
|
+
}
|
|
129
|
+
const alerts = [];
|
|
130
|
+
const clonedPSBT = decodedPSBT.clone();
|
|
131
|
+
(0, common_1.eachRecursive)(clonedPSBT);
|
|
132
|
+
const amount = (() => {
|
|
133
|
+
if (decodedPSBT.txOutputs.length === 1) {
|
|
134
|
+
return new bignumber_1.default(decodedPSBT.txOutputs[0].value);
|
|
135
|
+
}
|
|
136
|
+
{
|
|
137
|
+
const unknownKeyVals = decodedPSBT.data.globalMap.unknownKeyVals;
|
|
138
|
+
if (unknownKeyVals) {
|
|
139
|
+
const amountArray = unknownKeyVals.filter((kv) => kv.key.equals(Buffer.from('amount')));
|
|
140
|
+
if (amountArray.length > 0) {
|
|
141
|
+
return new bignumber_1.default(amountArray[0].value.toString());
|
|
142
|
+
}
|
|
137
143
|
}
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
});
|
|
151
|
-
};
|
|
152
|
-
BitcoinLegacyProtocolImpl.prototype.getAddressFromExtendedPublicKey = function (extendedPublicKey) {
|
|
153
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
154
|
-
var encodedExtendedPublicKey, bip32;
|
|
155
|
-
return __generator(this, function (_a) {
|
|
156
|
-
encodedExtendedPublicKey = (0, key_1.convertExtendedPublicKey)(extendedPublicKey, { format: 'encoded', type: 'xpub' });
|
|
157
|
-
bip32 = this.bip32.fromBase58(encodedExtendedPublicKey.value, this.bitcoinJS.config.network);
|
|
158
|
-
return [2 /*return*/, BitcoinLegacyAddress_1.BitcoinLegacyAddress.fromBip32(bip32).asString()];
|
|
159
|
-
});
|
|
160
|
-
});
|
|
161
|
-
};
|
|
162
|
-
BitcoinLegacyProtocolImpl.prototype.deriveFromExtendedPublicKey = function (extendedPublicKey, visibilityIndex, addressIndex) {
|
|
163
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
164
|
-
var encodedPublicKey, derivedBip32;
|
|
165
|
-
return __generator(this, function (_a) {
|
|
166
|
-
encodedPublicKey = (0, key_1.convertExtendedPublicKey)(extendedPublicKey, { format: 'encoded', type: 'xpub' });
|
|
167
|
-
derivedBip32 = this.bip32
|
|
168
|
-
.fromBase58(encodedPublicKey.value, this.bitcoinJS.config.network)
|
|
169
|
-
.derive(visibilityIndex)
|
|
170
|
-
.derive(addressIndex);
|
|
171
|
-
return [2 /*return*/, (0, module_kit_1.newPublicKey)(Buffer.from(derivedBip32.publicKey).toString('hex'), 'hex')];
|
|
172
|
-
});
|
|
173
|
-
});
|
|
174
|
-
};
|
|
175
|
-
BitcoinLegacyProtocolImpl.prototype.getDetailsFromTransaction = function (transaction, _publicKey) {
|
|
176
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
177
|
-
return __generator(this, function (_a) {
|
|
178
|
-
return [2 /*return*/, this.getDetailsFromPSBT(transaction.psbt, _publicKey)];
|
|
179
|
-
});
|
|
180
|
-
});
|
|
181
|
-
};
|
|
182
|
-
BitcoinLegacyProtocolImpl.prototype.getDetailsFromPSBT = function (psbt, publickey) {
|
|
183
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
184
|
-
var decodedPSBT, inTotal, i, input, prevIndex, prevTx, fee, _i, _a, o, alerts, clonedPSBT, amount, changeAddressDatas, changeAddressInfo;
|
|
185
|
-
var _this = this;
|
|
186
|
-
return __generator(this, function (_b) {
|
|
187
|
-
switch (_b.label) {
|
|
188
|
-
case 0:
|
|
189
|
-
decodedPSBT = this.bitcoinJS.lib.Psbt.fromHex(psbt);
|
|
190
|
-
inTotal = new bignumber_1.default(0);
|
|
191
|
-
for (i = 0; i < decodedPSBT.data.inputs.length; i++) {
|
|
192
|
-
input = decodedPSBT.data.inputs[i];
|
|
193
|
-
if (input.witnessUtxo) {
|
|
194
|
-
inTotal = inTotal.plus(input.witnessUtxo.value);
|
|
195
|
-
}
|
|
196
|
-
else if (input.nonWitnessUtxo) {
|
|
197
|
-
prevIndex = decodedPSBT.txInputs[i].index;
|
|
198
|
-
prevTx = this.bitcoinJS.lib.Transaction.fromBuffer(input.nonWitnessUtxo);
|
|
199
|
-
inTotal = inTotal.plus(prevTx.outs[prevIndex].value);
|
|
200
|
-
}
|
|
201
|
-
else {
|
|
202
|
-
throw new errors_1.UnsupportedError(coinlib_core_1.Domain.BITCOIN, 'PSBT input missing UTXO data.');
|
|
203
|
-
}
|
|
204
|
-
}
|
|
205
|
-
fee = inTotal;
|
|
206
|
-
for (_i = 0, _a = decodedPSBT.txOutputs; _i < _a.length; _i++) {
|
|
207
|
-
o = _a[_i];
|
|
208
|
-
fee = fee.minus(o.value);
|
|
209
|
-
}
|
|
210
|
-
alerts = [];
|
|
211
|
-
clonedPSBT = decodedPSBT.clone();
|
|
212
|
-
(0, common_1.eachRecursive)(clonedPSBT);
|
|
213
|
-
amount = (function () {
|
|
214
|
-
if (decodedPSBT.txOutputs.length === 1) {
|
|
215
|
-
return new bignumber_1.default(decodedPSBT.txOutputs[0].value);
|
|
216
|
-
}
|
|
217
|
-
{
|
|
218
|
-
var unknownKeyVals = decodedPSBT.data.globalMap.unknownKeyVals;
|
|
219
|
-
if (unknownKeyVals) {
|
|
220
|
-
var amountArray = unknownKeyVals.filter(function (kv) { return kv.key.equals(Buffer.from('amount')); });
|
|
221
|
-
if (amountArray.length > 0) {
|
|
222
|
-
return new bignumber_1.default(amountArray[0].value.toString());
|
|
223
|
-
}
|
|
224
|
-
}
|
|
225
|
-
}
|
|
226
|
-
var accumulated = new bignumber_1.default(0);
|
|
227
|
-
var useAccumulated = false;
|
|
228
|
-
decodedPSBT.data.outputs.forEach(function (outputKeyValues, index) {
|
|
229
|
-
if (outputKeyValues.unknownKeyVals) {
|
|
230
|
-
var derivationPaths = outputKeyValues.unknownKeyVals
|
|
231
|
-
.filter(function (kv) { return kv.key.equals(Buffer.from('dp')); })
|
|
232
|
-
.map(function (kv) { return kv.value.toString(); });
|
|
233
|
-
if (derivationPaths.length > 0) {
|
|
234
|
-
useAccumulated = true;
|
|
235
|
-
return;
|
|
236
|
-
}
|
|
237
|
-
}
|
|
238
|
-
var output = decodedPSBT.txOutputs[index];
|
|
239
|
-
accumulated = accumulated.plus(output.value);
|
|
240
|
-
});
|
|
241
|
-
if (useAccumulated) {
|
|
242
|
-
return accumulated;
|
|
243
|
-
}
|
|
244
|
-
return decodedPSBT.txOutputs
|
|
245
|
-
.map(function (obj) { return new bignumber_1.default(obj.value); })
|
|
246
|
-
.reduce(function (accumulator, currentValue) { return accumulator.plus(currentValue); });
|
|
247
|
-
})();
|
|
248
|
-
return [4 /*yield*/, Promise.all(decodedPSBT.data.outputs.map(function (obj, index) { return __awaiter(_this, void 0, void 0, function () {
|
|
249
|
-
var isChangeAddress, isOwned, addressIndex, address, amount, ourGeneratedAddress, getIndexes, ourPublickey, x, ourPublickey, x, ourPublickey;
|
|
250
|
-
return __generator(this, function (_a) {
|
|
251
|
-
switch (_a.label) {
|
|
252
|
-
case 0:
|
|
253
|
-
isChangeAddress = false;
|
|
254
|
-
isOwned = false;
|
|
255
|
-
addressIndex = 0;
|
|
256
|
-
address = decodedPSBT.txOutputs[index].address;
|
|
257
|
-
amount = decodedPSBT.txOutputs[index].value;
|
|
258
|
-
if (!obj.bip32Derivation) return [3 /*break*/, 11];
|
|
259
|
-
isChangeAddress = true;
|
|
260
|
-
getIndexes = obj.bip32Derivation[0].path.split('/');
|
|
261
|
-
if (!(publickey.type === 'xpub')) return [3 /*break*/, 3];
|
|
262
|
-
return [4 /*yield*/, this.deriveFromExtendedPublicKey(publickey, 1, +getIndexes[getIndexes.length - 1])];
|
|
263
|
-
case 1:
|
|
264
|
-
ourPublickey = _a.sent();
|
|
265
|
-
return [4 /*yield*/, this.getAddressFromPublicKey(ourPublickey)];
|
|
266
|
-
case 2:
|
|
267
|
-
ourGeneratedAddress = _a.sent();
|
|
268
|
-
return [3 /*break*/, 5];
|
|
269
|
-
case 3: return [4 /*yield*/, this.getAddressFromNonExtendedPublicKey(publickey)];
|
|
270
|
-
case 4:
|
|
271
|
-
ourGeneratedAddress = _a.sent();
|
|
272
|
-
_a.label = 5;
|
|
273
|
-
case 5:
|
|
274
|
-
if (ourGeneratedAddress === address) {
|
|
275
|
-
isOwned = true;
|
|
276
|
-
addressIndex = +getIndexes[getIndexes.length - 1];
|
|
277
|
-
}
|
|
278
|
-
x = 0;
|
|
279
|
-
_a.label = 6;
|
|
280
|
-
case 6:
|
|
281
|
-
if (!(x < 1000)) return [3 /*break*/, 10];
|
|
282
|
-
return [4 /*yield*/, this.deriveFromExtendedPublicKey(publickey, 1, x)];
|
|
283
|
-
case 7:
|
|
284
|
-
ourPublickey = _a.sent();
|
|
285
|
-
return [4 /*yield*/, this.getAddressFromPublicKey(ourPublickey)];
|
|
286
|
-
case 8:
|
|
287
|
-
ourGeneratedAddress = _a.sent();
|
|
288
|
-
if (ourGeneratedAddress === address) {
|
|
289
|
-
isOwned = true;
|
|
290
|
-
addressIndex = x;
|
|
291
|
-
return [3 /*break*/, 10];
|
|
292
|
-
}
|
|
293
|
-
_a.label = 9;
|
|
294
|
-
case 9:
|
|
295
|
-
x++;
|
|
296
|
-
return [3 /*break*/, 6];
|
|
297
|
-
case 10: return [3 /*break*/, 16];
|
|
298
|
-
case 11:
|
|
299
|
-
if (!obj.unknownKeyVals) return [3 /*break*/, 16];
|
|
300
|
-
x = 0;
|
|
301
|
-
_a.label = 12;
|
|
302
|
-
case 12:
|
|
303
|
-
if (!(x < 1000)) return [3 /*break*/, 16];
|
|
304
|
-
isChangeAddress = true;
|
|
305
|
-
return [4 /*yield*/, this.deriveFromExtendedPublicKey(publickey, 1, x)];
|
|
306
|
-
case 13:
|
|
307
|
-
ourPublickey = _a.sent();
|
|
308
|
-
return [4 /*yield*/, this.getAddressFromPublicKey(ourPublickey)];
|
|
309
|
-
case 14:
|
|
310
|
-
ourGeneratedAddress = _a.sent();
|
|
311
|
-
if (ourGeneratedAddress === address) {
|
|
312
|
-
isOwned = true;
|
|
313
|
-
addressIndex = x;
|
|
314
|
-
return [3 /*break*/, 16];
|
|
315
|
-
}
|
|
316
|
-
_a.label = 15;
|
|
317
|
-
case 15:
|
|
318
|
-
x++;
|
|
319
|
-
return [3 /*break*/, 12];
|
|
320
|
-
case 16:
|
|
321
|
-
if (isChangeAddress && isOwned) {
|
|
322
|
-
alerts.push({
|
|
323
|
-
type: 'success',
|
|
324
|
-
title: {
|
|
325
|
-
type: 'plain',
|
|
326
|
-
value: ''
|
|
327
|
-
},
|
|
328
|
-
description: {
|
|
329
|
-
type: 'plain',
|
|
330
|
-
value: 'Note: your change address has been verified'
|
|
331
|
-
},
|
|
332
|
-
icon: undefined,
|
|
333
|
-
actions: undefined
|
|
334
|
-
});
|
|
335
|
-
}
|
|
336
|
-
else if (isChangeAddress && !isOwned) {
|
|
337
|
-
alerts.push({
|
|
338
|
-
type: 'warning',
|
|
339
|
-
title: {
|
|
340
|
-
type: 'plain',
|
|
341
|
-
value: ''
|
|
342
|
-
},
|
|
343
|
-
description: {
|
|
344
|
-
type: 'plain',
|
|
345
|
-
value: 'Note: your change address has not been verified'
|
|
346
|
-
},
|
|
347
|
-
icon: undefined,
|
|
348
|
-
actions: undefined
|
|
349
|
-
});
|
|
350
|
-
}
|
|
351
|
-
return [2 /*return*/, [
|
|
352
|
-
address,
|
|
353
|
-
{
|
|
354
|
-
isChangeAddress: isChangeAddress,
|
|
355
|
-
isOwned: isOwned,
|
|
356
|
-
path: addressIndex === 0 ? '' : "m/44'/0'/0'/1/".concat(addressIndex),
|
|
357
|
-
amount: amount
|
|
358
|
-
}
|
|
359
|
-
]];
|
|
360
|
-
}
|
|
361
|
-
});
|
|
362
|
-
}); }))];
|
|
363
|
-
case 1:
|
|
364
|
-
changeAddressDatas = _b.sent();
|
|
365
|
-
changeAddressInfo = {};
|
|
366
|
-
changeAddressDatas.forEach(function (changeAddressData) {
|
|
367
|
-
changeAddressInfo[changeAddressData[0]] = changeAddressData[1];
|
|
368
|
-
});
|
|
369
|
-
return [2 /*return*/, [
|
|
370
|
-
{
|
|
371
|
-
from: decodedPSBT.data.inputs.map(function (obj) {
|
|
372
|
-
var _a, _b;
|
|
373
|
-
return (_b = (_a = obj.bip32Derivation) === null || _a === void 0 ? void 0 : _a.map(function (el) {
|
|
374
|
-
return _this.bitcoinJS.lib.payments.p2pkh({
|
|
375
|
-
pubkey: el.pubkey,
|
|
376
|
-
network: _this.bitcoinJS.lib.networks.bitcoin
|
|
377
|
-
}).address;
|
|
378
|
-
}).join(' ')) !== null && _b !== void 0 ? _b : 'INVALID';
|
|
379
|
-
}),
|
|
380
|
-
to: decodedPSBT.txOutputs.map(function (obj) {
|
|
381
|
-
return obj.address || "Script: ".concat(obj.script.toString('hex')) || 'unknown';
|
|
382
|
-
}),
|
|
383
|
-
isInbound: false,
|
|
384
|
-
amount: (0, module_kit_1.newAmount)(amount, 'blockchain'),
|
|
385
|
-
fee: (0, module_kit_1.newAmount)(fee, 'blockchain'),
|
|
386
|
-
network: this.options.network,
|
|
387
|
-
changeAddressInfo: changeAddressInfo,
|
|
388
|
-
uiAlerts: alerts,
|
|
389
|
-
json: {
|
|
390
|
-
inputTx: (0, common_1.eachRecursive)(clonedPSBT.txInputs),
|
|
391
|
-
outputTx: (0, common_1.eachRecursive)(clonedPSBT.txOutputs),
|
|
392
|
-
inputData: clonedPSBT.data.inputs,
|
|
393
|
-
outputData: clonedPSBT.data.outputs,
|
|
394
|
-
PSBTVersion: clonedPSBT.version,
|
|
395
|
-
PSBTLocktime: clonedPSBT.locktime,
|
|
396
|
-
PSBTGlobalMap: clonedPSBT.data.globalMap,
|
|
397
|
-
rawPSBT: psbt
|
|
398
|
-
}
|
|
399
|
-
}
|
|
400
|
-
]];
|
|
144
|
+
}
|
|
145
|
+
let accumulated = new bignumber_1.default(0);
|
|
146
|
+
let useAccumulated = false;
|
|
147
|
+
decodedPSBT.data.outputs.forEach((outputKeyValues, index) => {
|
|
148
|
+
if (outputKeyValues.unknownKeyVals) {
|
|
149
|
+
const derivationPaths = outputKeyValues.unknownKeyVals
|
|
150
|
+
.filter((kv) => kv.key.equals(Buffer.from('dp')))
|
|
151
|
+
.map((kv) => kv.value.toString());
|
|
152
|
+
if (derivationPaths.length > 0) {
|
|
153
|
+
useAccumulated = true;
|
|
154
|
+
return;
|
|
155
|
+
}
|
|
401
156
|
}
|
|
157
|
+
const output = decodedPSBT.txOutputs[index];
|
|
158
|
+
accumulated = accumulated.plus(output.value);
|
|
402
159
|
});
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
});
|
|
424
|
-
});
|
|
425
|
-
};
|
|
426
|
-
BitcoinLegacyProtocolImpl.prototype.getKeyPairFromDerivative = function (derivative) {
|
|
427
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
428
|
-
var bip32, privateKeyUint8, privateKey, publicKeyUint8, publicKey;
|
|
429
|
-
return __generator(this, function (_a) {
|
|
430
|
-
bip32 = this.derivativeToBip32Node(derivative);
|
|
431
|
-
privateKeyUint8 = bip32.privateKey;
|
|
432
|
-
if (privateKeyUint8 === undefined) {
|
|
433
|
-
throw new Error('No private key!');
|
|
160
|
+
if (useAccumulated) {
|
|
161
|
+
return accumulated;
|
|
162
|
+
}
|
|
163
|
+
return decodedPSBT.txOutputs
|
|
164
|
+
.map((obj) => new bignumber_1.default(obj.value))
|
|
165
|
+
.reduce((accumulator, currentValue) => accumulator.plus(currentValue));
|
|
166
|
+
})();
|
|
167
|
+
const changeAddressDatas = await Promise.all(decodedPSBT.data.outputs.map(async (obj, index) => {
|
|
168
|
+
let isChangeAddress = false;
|
|
169
|
+
let isOwned = false;
|
|
170
|
+
let addressIndex = 0;
|
|
171
|
+
const address = decodedPSBT.txOutputs[index].address;
|
|
172
|
+
const amount = decodedPSBT.txOutputs[index].value;
|
|
173
|
+
let ourGeneratedAddress;
|
|
174
|
+
if (obj.bip32Derivation) {
|
|
175
|
+
isChangeAddress = true;
|
|
176
|
+
const getIndexes = obj.bip32Derivation[0].path.split('/');
|
|
177
|
+
if (publickey.type === 'xpub') {
|
|
178
|
+
const ourPublickey = await this.deriveFromExtendedPublicKey(publickey, 1, +getIndexes[getIndexes.length - 1]);
|
|
179
|
+
ourGeneratedAddress = await this.getAddressFromPublicKey(ourPublickey);
|
|
434
180
|
}
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
publicKey = Buffer.from(publicKeyUint8);
|
|
438
|
-
return [2 /*return*/, {
|
|
439
|
-
secretKey: (0, module_kit_1.newSecretKey)(privateKey.toString('hex'), 'hex'),
|
|
440
|
-
publicKey: (0, module_kit_1.newPublicKey)(publicKey.toString('hex'), 'hex')
|
|
441
|
-
}];
|
|
442
|
-
});
|
|
443
|
-
});
|
|
444
|
-
};
|
|
445
|
-
BitcoinLegacyProtocolImpl.prototype.getExtendedKeyPairFromDerivative = function (derivative) {
|
|
446
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
447
|
-
var bip32;
|
|
448
|
-
return __generator(this, function (_a) {
|
|
449
|
-
bip32 = this.derivativeToBip32Node(derivative);
|
|
450
|
-
return [2 /*return*/, {
|
|
451
|
-
secretKey: (0, module_kit_1.newExtendedSecretKey)(bip32.toBase58(), 'encoded'),
|
|
452
|
-
publicKey: (0, key_1.convertExtendedPublicKey)((0, module_kit_1.newExtendedPublicKey)(bip32.neutered().toBase58(), 'encoded'), {
|
|
453
|
-
format: 'encoded',
|
|
454
|
-
type: 'xpub'
|
|
455
|
-
})
|
|
456
|
-
}];
|
|
457
|
-
});
|
|
458
|
-
});
|
|
459
|
-
};
|
|
460
|
-
BitcoinLegacyProtocolImpl.prototype.deriveFromExtendedSecretKey = function (extendedSecretKey, visibilityIndex, addressIndex) {
|
|
461
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
462
|
-
var encodedSecretKey, derivedBip32, privateKeyUint8, privateKey;
|
|
463
|
-
return __generator(this, function (_a) {
|
|
464
|
-
encodedSecretKey = (0, key_1.convertExtendedSecretKey)(extendedSecretKey, { format: 'encoded', type: 'xprv' });
|
|
465
|
-
derivedBip32 = this.bip32
|
|
466
|
-
.fromBase58(encodedSecretKey.value, this.bitcoinJS.config.network)
|
|
467
|
-
.derive(visibilityIndex)
|
|
468
|
-
.derive(addressIndex);
|
|
469
|
-
privateKeyUint8 = derivedBip32.privateKey;
|
|
470
|
-
if (privateKeyUint8 === undefined) {
|
|
471
|
-
throw new Error('No private key!');
|
|
181
|
+
else {
|
|
182
|
+
ourGeneratedAddress = await this.getAddressFromNonExtendedPublicKey(publickey);
|
|
472
183
|
}
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
});
|
|
477
|
-
};
|
|
478
|
-
BitcoinLegacyProtocolImpl.prototype.signTransactionWithSecretKey = function (transaction, secretKey) {
|
|
479
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
480
|
-
return __generator(this, function (_a) {
|
|
481
|
-
switch (secretKey.type) {
|
|
482
|
-
case 'priv':
|
|
483
|
-
return [2 /*return*/, this.signTransactionWithNonExtendedSecretKey(transaction, secretKey)];
|
|
484
|
-
case 'xpriv':
|
|
485
|
-
return [2 /*return*/, this.signTransactionWithExtendedSecretKey(transaction, secretKey)];
|
|
486
|
-
default:
|
|
487
|
-
(0, coinlib_core_1.assertNever)(secretKey);
|
|
488
|
-
throw new errors_1.UnsupportedError(coinlib_core_1.Domain.BITCOIN, 'Secret key type not supported.');
|
|
184
|
+
if (ourGeneratedAddress === address) {
|
|
185
|
+
isOwned = true;
|
|
186
|
+
addressIndex = +getIndexes[getIndexes.length - 1];
|
|
489
187
|
}
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
188
|
+
for (let x = 0; x < 1000; x++) {
|
|
189
|
+
const ourPublickey = await this.deriveFromExtendedPublicKey(publickey, 1, x);
|
|
190
|
+
ourGeneratedAddress = await this.getAddressFromPublicKey(ourPublickey);
|
|
191
|
+
if (ourGeneratedAddress === address) {
|
|
192
|
+
isOwned = true;
|
|
193
|
+
addressIndex = x;
|
|
194
|
+
break;
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
else if (obj.unknownKeyVals) {
|
|
199
|
+
for (let x = 0; x < 1000; x++) {
|
|
200
|
+
isChangeAddress = true;
|
|
201
|
+
const ourPublickey = await this.deriveFromExtendedPublicKey(publickey, 1, x);
|
|
202
|
+
ourGeneratedAddress = await this.getAddressFromPublicKey(ourPublickey);
|
|
203
|
+
if (ourGeneratedAddress === address) {
|
|
204
|
+
isOwned = true;
|
|
205
|
+
addressIndex = x;
|
|
206
|
+
break;
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
|
+
if (isChangeAddress && isOwned) {
|
|
211
|
+
alerts.push({
|
|
212
|
+
type: 'success',
|
|
213
|
+
title: {
|
|
214
|
+
type: 'plain',
|
|
215
|
+
value: ''
|
|
216
|
+
},
|
|
217
|
+
description: {
|
|
218
|
+
type: 'plain',
|
|
219
|
+
value: 'Note: your change address has been verified'
|
|
220
|
+
},
|
|
221
|
+
icon: undefined,
|
|
222
|
+
actions: undefined
|
|
524
223
|
});
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
}
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
|
|
224
|
+
}
|
|
225
|
+
else if (isChangeAddress && !isOwned) {
|
|
226
|
+
alerts.push({
|
|
227
|
+
type: 'warning',
|
|
228
|
+
title: {
|
|
229
|
+
type: 'plain',
|
|
230
|
+
value: ''
|
|
231
|
+
},
|
|
232
|
+
description: {
|
|
233
|
+
type: 'plain',
|
|
234
|
+
value: 'Note: your change address has not been verified'
|
|
235
|
+
},
|
|
236
|
+
icon: undefined,
|
|
237
|
+
actions: undefined
|
|
238
|
+
});
|
|
239
|
+
}
|
|
240
|
+
return [
|
|
241
|
+
address,
|
|
242
|
+
{
|
|
243
|
+
isChangeAddress: isChangeAddress,
|
|
244
|
+
isOwned: isOwned,
|
|
245
|
+
path: addressIndex === 0 ? '' : `m/44'/0'/0'/1/${addressIndex}`,
|
|
246
|
+
amount
|
|
247
|
+
}
|
|
248
|
+
];
|
|
249
|
+
}));
|
|
250
|
+
const changeAddressInfo = {};
|
|
251
|
+
changeAddressDatas.forEach((changeAddressData) => {
|
|
252
|
+
changeAddressInfo[changeAddressData[0]] = changeAddressData[1];
|
|
253
|
+
});
|
|
254
|
+
return [
|
|
255
|
+
{
|
|
256
|
+
from: decodedPSBT.data.inputs.map((obj) => obj.bip32Derivation
|
|
257
|
+
?.map((el) => this.bitcoinJS.lib.payments.p2pkh({
|
|
258
|
+
pubkey: el.pubkey,
|
|
259
|
+
network: this.bitcoinJS.lib.networks.bitcoin
|
|
260
|
+
}).address)
|
|
261
|
+
.join(' ') ?? 'INVALID'),
|
|
262
|
+
to: decodedPSBT.txOutputs.map((obj) => {
|
|
263
|
+
return obj.address || `Script: ${obj.script.toString('hex')}` || 'unknown';
|
|
264
|
+
}),
|
|
265
|
+
isInbound: false,
|
|
266
|
+
amount: (0, module_kit_1.newAmount)(amount, 'blockchain'),
|
|
267
|
+
fee: (0, module_kit_1.newAmount)(fee, 'blockchain'),
|
|
268
|
+
network: this.options.network,
|
|
269
|
+
changeAddressInfo,
|
|
270
|
+
uiAlerts: alerts,
|
|
271
|
+
json: {
|
|
272
|
+
inputTx: (0, common_1.eachRecursive)(clonedPSBT.txInputs),
|
|
273
|
+
outputTx: (0, common_1.eachRecursive)(clonedPSBT.txOutputs),
|
|
274
|
+
inputData: clonedPSBT.data.inputs,
|
|
275
|
+
outputData: clonedPSBT.data.outputs,
|
|
276
|
+
PSBTVersion: clonedPSBT.version,
|
|
277
|
+
PSBTLocktime: clonedPSBT.locktime,
|
|
278
|
+
PSBTGlobalMap: clonedPSBT.data.globalMap,
|
|
279
|
+
rawPSBT: psbt
|
|
280
|
+
}
|
|
281
|
+
}
|
|
282
|
+
];
|
|
283
|
+
}
|
|
284
|
+
async verifyMessageWithPublicKey(message, signature, publicKey) {
|
|
285
|
+
return this.legacy.verifyMessageWithPublicKey(message, signature, publicKey);
|
|
286
|
+
}
|
|
287
|
+
async encryptAsymmetricWithPublicKey(payload, publicKey) {
|
|
288
|
+
return this.legacy.encryptAsymmetricWithPublicKey(payload, publicKey);
|
|
289
|
+
}
|
|
290
|
+
async getCryptoConfiguration() {
|
|
291
|
+
return this.legacy.getCryptoConfiguration();
|
|
292
|
+
}
|
|
293
|
+
async getKeyPairFromDerivative(derivative) {
|
|
294
|
+
const bip32 = this.derivativeToBip32Node(derivative);
|
|
295
|
+
const privateKeyUint8 = bip32.privateKey;
|
|
296
|
+
if (privateKeyUint8 === undefined) {
|
|
297
|
+
throw new Error('No private key!');
|
|
298
|
+
}
|
|
299
|
+
const privateKey = Buffer.from(privateKeyUint8);
|
|
300
|
+
const publicKeyUint8 = bip32.publicKey;
|
|
301
|
+
const publicKey = Buffer.from(publicKeyUint8);
|
|
302
|
+
return {
|
|
303
|
+
secretKey: (0, module_kit_1.newSecretKey)(privateKey.toString('hex'), 'hex'),
|
|
304
|
+
publicKey: (0, module_kit_1.newPublicKey)(publicKey.toString('hex'), 'hex')
|
|
305
|
+
};
|
|
306
|
+
}
|
|
307
|
+
async getExtendedKeyPairFromDerivative(derivative) {
|
|
308
|
+
const bip32 = this.derivativeToBip32Node(derivative);
|
|
309
|
+
return {
|
|
310
|
+
secretKey: (0, module_kit_1.newExtendedSecretKey)(bip32.toBase58(), 'encoded'),
|
|
311
|
+
publicKey: (0, key_1.convertExtendedPublicKey)((0, module_kit_1.newExtendedPublicKey)(bip32.neutered().toBase58(), 'encoded'), {
|
|
312
|
+
format: 'encoded',
|
|
313
|
+
type: 'xpub'
|
|
314
|
+
})
|
|
315
|
+
};
|
|
316
|
+
}
|
|
317
|
+
async deriveFromExtendedSecretKey(extendedSecretKey, visibilityIndex, addressIndex) {
|
|
318
|
+
const encodedSecretKey = (0, key_1.convertExtendedSecretKey)(extendedSecretKey, { format: 'encoded', type: 'xprv' });
|
|
319
|
+
const derivedBip32 = this.bip32
|
|
320
|
+
.fromBase58(encodedSecretKey.value, this.bitcoinJS.config.network)
|
|
321
|
+
.derive(visibilityIndex)
|
|
322
|
+
.derive(addressIndex);
|
|
323
|
+
const privateKeyUint8 = derivedBip32.privateKey;
|
|
324
|
+
if (privateKeyUint8 === undefined) {
|
|
325
|
+
throw new Error('No private key!');
|
|
326
|
+
}
|
|
327
|
+
const privateKey = Buffer.from(privateKeyUint8);
|
|
328
|
+
return (0, module_kit_1.newSecretKey)(privateKey.toString('hex'), 'hex');
|
|
329
|
+
}
|
|
330
|
+
async signTransactionWithSecretKey(transaction, secretKey) {
|
|
331
|
+
switch (secretKey.type) {
|
|
332
|
+
case 'priv':
|
|
333
|
+
return this.signTransactionWithNonExtendedSecretKey(transaction, secretKey);
|
|
334
|
+
case 'xpriv':
|
|
335
|
+
return this.signTransactionWithExtendedSecretKey(transaction, secretKey);
|
|
336
|
+
default:
|
|
337
|
+
(0, coinlib_core_1.assertNever)(secretKey);
|
|
338
|
+
throw new errors_1.UnsupportedError(coinlib_core_1.Domain.BITCOIN, 'Secret key type not supported.');
|
|
339
|
+
}
|
|
340
|
+
}
|
|
341
|
+
async signTransactionWithNonExtendedSecretKey(transaction, secretKey) {
|
|
342
|
+
throw new errors_1.UnsupportedError(coinlib_core_1.Domain.BITCOIN, 'Sign with non extended secret key not supported');
|
|
343
|
+
}
|
|
344
|
+
async signTransactionWithExtendedSecretKey(transaction, extendedSecretKey) {
|
|
345
|
+
const encodedExtendedSecretKey = (0, key_1.convertExtendedSecretKey)(extendedSecretKey, { format: 'encoded', type: 'xprv' });
|
|
346
|
+
const bip32 = this.bip32.fromBase58(encodedExtendedSecretKey.value);
|
|
347
|
+
const decodedPSBT = this.bitcoinJS.lib.Psbt.fromHex(transaction.psbt);
|
|
348
|
+
decodedPSBT.data.inputs.forEach((input, index) => {
|
|
349
|
+
input.bip32Derivation?.forEach((deriv) => {
|
|
350
|
+
try {
|
|
351
|
+
// This uses the same logic to find child key as the "findWalletByFingerprintDerivationPathAndProtocolIdentifier" method in the Vault
|
|
352
|
+
const cutoffFrom = deriv.path.lastIndexOf("'") || deriv.path.lastIndexOf('h');
|
|
353
|
+
const childPath = deriv.path.substr(cutoffFrom + 2);
|
|
354
|
+
const childNode = bip32.derivePath(childPath);
|
|
355
|
+
// Wrap publicKey as Buffer to match Signer interface
|
|
356
|
+
decodedPSBT.signInput(index, {
|
|
357
|
+
publicKey: Buffer.from(childNode.publicKey),
|
|
358
|
+
sign: (hash, lowR) => Buffer.from(childNode.sign(hash, lowR))
|
|
359
|
+
});
|
|
360
|
+
}
|
|
361
|
+
catch (e) { }
|
|
554
362
|
});
|
|
555
363
|
});
|
|
556
|
-
|
|
364
|
+
return (0, module_kit_1.newSignedTransaction)({ psbt: decodedPSBT.toHex() });
|
|
365
|
+
}
|
|
366
|
+
async signMessageWithKeyPair(message, keyPair) {
|
|
367
|
+
return this.legacy.signMessageWithKeyPair(message, keyPair);
|
|
368
|
+
}
|
|
369
|
+
async decryptAsymmetricWithKeyPair(payload, keyPair) {
|
|
370
|
+
return this.legacy.decryptAsymmetricWithKeyPair(payload, keyPair);
|
|
371
|
+
}
|
|
372
|
+
async encryptAESWithSecretKey(payload, secretKey) {
|
|
373
|
+
return this.legacy.encryptAESWithSecretKey(payload, secretKey);
|
|
374
|
+
}
|
|
375
|
+
async decryptAESWithSecretKey(payload, secretKey) {
|
|
376
|
+
return this.legacy.decryptAESWithSecretKey(payload, secretKey);
|
|
377
|
+
}
|
|
557
378
|
// Online
|
|
558
|
-
|
|
559
|
-
return
|
|
560
|
-
|
|
561
|
-
|
|
562
|
-
|
|
563
|
-
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
|
|
570
|
-
|
|
571
|
-
|
|
572
|
-
|
|
573
|
-
|
|
574
|
-
|
|
575
|
-
|
|
576
|
-
|
|
577
|
-
|
|
578
|
-
}
|
|
579
|
-
|
|
580
|
-
return
|
|
581
|
-
|
|
582
|
-
|
|
583
|
-
|
|
584
|
-
|
|
585
|
-
|
|
586
|
-
|
|
587
|
-
|
|
588
|
-
|
|
589
|
-
|
|
590
|
-
|
|
591
|
-
|
|
592
|
-
|
|
593
|
-
|
|
594
|
-
|
|
595
|
-
|
|
596
|
-
|
|
597
|
-
|
|
598
|
-
|
|
599
|
-
|
|
600
|
-
|
|
601
|
-
|
|
602
|
-
|
|
603
|
-
|
|
604
|
-
|
|
605
|
-
|
|
606
|
-
|
|
607
|
-
|
|
608
|
-
|
|
609
|
-
|
|
610
|
-
|
|
611
|
-
|
|
612
|
-
|
|
613
|
-
|
|
614
|
-
|
|
615
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
616
|
-
return __generator(this, function (_a) {
|
|
617
|
-
return [2 /*return*/, this.legacy.getTransactionFeeWithPublicKey(_publicKey, _details, _configuration)];
|
|
618
|
-
});
|
|
379
|
+
async getNetwork() {
|
|
380
|
+
return this.options.network;
|
|
381
|
+
}
|
|
382
|
+
async getTransactionsForPublicKey(publicKey, limit, cursor) {
|
|
383
|
+
return this.legacy.getTransactionsForPublicKey(publicKey, limit, cursor);
|
|
384
|
+
}
|
|
385
|
+
async getTransactionsForAddress(address, limit, cursor) {
|
|
386
|
+
return this.legacy.getTransactionsForAddress(address, limit, cursor);
|
|
387
|
+
}
|
|
388
|
+
async getTransactionsForAddresses(addresses, limit, cursor) {
|
|
389
|
+
return this.legacy.getTransactionsForAddresses(addresses, limit, cursor);
|
|
390
|
+
}
|
|
391
|
+
async getBalanceOfPublicKey(publicKey) {
|
|
392
|
+
return this.legacy.getBalanceOfPublicKey(publicKey);
|
|
393
|
+
}
|
|
394
|
+
async getBalanceOfAddress(address) {
|
|
395
|
+
return this.legacy.getBalanceOfAddress(address);
|
|
396
|
+
}
|
|
397
|
+
async getBalanceOfAddresses(addresses) {
|
|
398
|
+
return this.legacy.getBalanceOfAddresses(addresses);
|
|
399
|
+
}
|
|
400
|
+
async getTransactionMaxAmountWithPublicKey(publicKey, to, configuration) {
|
|
401
|
+
return this.legacy.getTransactionMaxAmountWithPublicKey(publicKey, to, configuration);
|
|
402
|
+
}
|
|
403
|
+
async getTransactionFeeWithPublicKey(_publicKey, _details, _configuration) {
|
|
404
|
+
return this.legacy.getTransactionFeeWithPublicKey(_publicKey, _details, _configuration);
|
|
405
|
+
}
|
|
406
|
+
async prepareTransactionWithPublicKey(publicKey, details, configuration) {
|
|
407
|
+
switch (publicKey.type) {
|
|
408
|
+
case 'pub':
|
|
409
|
+
return this.prepareTransactionWithNonExtendedPublicKey(publicKey, details, configuration);
|
|
410
|
+
case 'xpub':
|
|
411
|
+
return this.prepareTransactionWithExtendedPublicKey(publicKey, details, configuration);
|
|
412
|
+
default:
|
|
413
|
+
(0, coinlib_core_1.assertNever)(publicKey);
|
|
414
|
+
throw new errors_1.UnsupportedError(coinlib_core_1.Domain.BITCOIN, 'Unuspported public key type.');
|
|
415
|
+
}
|
|
416
|
+
}
|
|
417
|
+
async prepareTransactionWithNonExtendedPublicKey(publicKey, details, configuration) {
|
|
418
|
+
throw new errors_1.UnsupportedError(coinlib_core_1.Domain.BITCOIN, 'Prepare transaction with non extended public key not supported');
|
|
419
|
+
}
|
|
420
|
+
async prepareTransactionWithExtendedPublicKey(extendedPublicKey, details, configuration) {
|
|
421
|
+
if (configuration?.masterFingerprint === undefined) {
|
|
422
|
+
throw new errors_1.ConditionViolationError(coinlib_core_1.Domain.BITCOIN, 'Master fingerprint not set.');
|
|
423
|
+
}
|
|
424
|
+
let fee;
|
|
425
|
+
if (configuration?.fee !== undefined) {
|
|
426
|
+
fee = configuration.fee;
|
|
427
|
+
}
|
|
428
|
+
else {
|
|
429
|
+
const estimatedFee = await this.getTransactionFeeWithPublicKey(extendedPublicKey, details);
|
|
430
|
+
fee = estimatedFee.medium;
|
|
431
|
+
}
|
|
432
|
+
const wrappedFee = new bignumber_1.default((0, module_kit_1.newAmount)(fee).blockchain(this.legacy.units).value);
|
|
433
|
+
const transaction = (0, module_kit_1.newUnsignedTransaction)({
|
|
434
|
+
ins: [],
|
|
435
|
+
outs: []
|
|
619
436
|
});
|
|
620
|
-
|
|
621
|
-
|
|
622
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
623
|
-
return __generator(this, function (_a) {
|
|
624
|
-
switch (publicKey.type) {
|
|
625
|
-
case 'pub':
|
|
626
|
-
return [2 /*return*/, this.prepareTransactionWithNonExtendedPublicKey(publicKey, details, configuration)];
|
|
627
|
-
case 'xpub':
|
|
628
|
-
return [2 /*return*/, this.prepareTransactionWithExtendedPublicKey(publicKey, details, configuration)];
|
|
629
|
-
default:
|
|
630
|
-
(0, coinlib_core_1.assertNever)(publicKey);
|
|
631
|
-
throw new errors_1.UnsupportedError(coinlib_core_1.Domain.BITCOIN, 'Unuspported public key type.');
|
|
632
|
-
}
|
|
633
|
-
return [2 /*return*/];
|
|
634
|
-
});
|
|
437
|
+
const { data: utxos } = await index_1.default.get(`${this.options.network.indexerApi}/api/v2/utxo/${extendedPublicKey.value}?confirmed=true`, {
|
|
438
|
+
responseType: 'json'
|
|
635
439
|
});
|
|
636
|
-
|
|
637
|
-
|
|
638
|
-
|
|
639
|
-
|
|
640
|
-
|
|
440
|
+
if (utxos.length <= 0) {
|
|
441
|
+
throw new errors_1.BalanceError(coinlib_core_1.Domain.BITCOIN, 'Not enough balance.');
|
|
442
|
+
}
|
|
443
|
+
const totalRequiredBalance = details
|
|
444
|
+
.map(({ amount }) => new bignumber_1.default((0, module_kit_1.newAmount)(amount).blockchain(this.legacy.units).value))
|
|
445
|
+
.reduce((accumulator, currentValue) => accumulator.plus(currentValue))
|
|
446
|
+
.plus(wrappedFee);
|
|
447
|
+
let valueAccumulator = new bignumber_1.default(0);
|
|
448
|
+
const getPathIndexes = (path) => {
|
|
449
|
+
const result = path
|
|
450
|
+
.split('/')
|
|
451
|
+
.slice(-2)
|
|
452
|
+
.map((item) => parseInt(item))
|
|
453
|
+
.filter((item) => !isNaN(item));
|
|
454
|
+
if (result.length !== 2) {
|
|
455
|
+
throw new Error('Unexpected path format');
|
|
456
|
+
}
|
|
457
|
+
return [result[0], result[1]];
|
|
458
|
+
};
|
|
459
|
+
for (const utxo of utxos) {
|
|
460
|
+
valueAccumulator = valueAccumulator.plus(utxo.value);
|
|
461
|
+
const indexes = getPathIndexes(utxo.path);
|
|
462
|
+
const derivedPublicKey = await this.deriveFromExtendedPublicKey(extendedPublicKey, indexes[0], indexes[1]);
|
|
463
|
+
const derivedAddress = await this.getAddressFromPublicKey(derivedPublicKey);
|
|
464
|
+
if (derivedAddress === utxo.address) {
|
|
465
|
+
transaction.ins.push({
|
|
466
|
+
txId: utxo.txid,
|
|
467
|
+
value: new bignumber_1.default(utxo.value).toString(10),
|
|
468
|
+
vout: utxo.vout,
|
|
469
|
+
address: utxo.address,
|
|
470
|
+
derivationPath: utxo.path
|
|
471
|
+
});
|
|
472
|
+
}
|
|
473
|
+
else {
|
|
474
|
+
throw new Error('Invalid address returned from API');
|
|
475
|
+
}
|
|
476
|
+
if (valueAccumulator.isGreaterThanOrEqualTo(totalRequiredBalance)) {
|
|
477
|
+
break;
|
|
478
|
+
}
|
|
479
|
+
}
|
|
480
|
+
if (valueAccumulator.isLessThan(totalRequiredBalance)) {
|
|
481
|
+
throw new Error('not enough balance 2');
|
|
482
|
+
}
|
|
483
|
+
for (let i = 0; i < details.length; i++) {
|
|
484
|
+
const value = (0, module_kit_1.newAmount)(details[i].amount).blockchain(this.legacy.units).value;
|
|
485
|
+
transaction.outs.push({
|
|
486
|
+
recipient: details[i].to,
|
|
487
|
+
isChange: false,
|
|
488
|
+
value
|
|
489
|
+
});
|
|
490
|
+
valueAccumulator = valueAccumulator.minus(value);
|
|
491
|
+
}
|
|
492
|
+
const lastUsedInternalAddress = Math.max(-1, ...utxos
|
|
493
|
+
.map((utxo) => getPathIndexes(utxo.path))
|
|
494
|
+
.filter((indexes) => indexes[0] === 1)
|
|
495
|
+
.map((indexes) => indexes[1]));
|
|
496
|
+
const changeValue = valueAccumulator.minus(wrappedFee);
|
|
497
|
+
if (changeValue.isGreaterThan(new bignumber_1.default(DUST_AMOUNT))) {
|
|
498
|
+
const changeAddressIndex = lastUsedInternalAddress + 1;
|
|
499
|
+
const derivedPublicKey = await this.deriveFromExtendedPublicKey(extendedPublicKey, 1, changeAddressIndex);
|
|
500
|
+
const derivedAddress = await this.getAddressFromPublicKey(derivedPublicKey);
|
|
501
|
+
transaction.outs.push({
|
|
502
|
+
recipient: derivedAddress,
|
|
503
|
+
isChange: true,
|
|
504
|
+
value: changeValue.toString(10),
|
|
505
|
+
derivationPath: `1/${changeAddressIndex}`
|
|
641
506
|
});
|
|
642
|
-
}
|
|
643
|
-
|
|
644
|
-
|
|
645
|
-
|
|
646
|
-
|
|
647
|
-
|
|
648
|
-
|
|
649
|
-
|
|
650
|
-
|
|
651
|
-
|
|
652
|
-
|
|
653
|
-
|
|
654
|
-
|
|
655
|
-
|
|
656
|
-
|
|
657
|
-
|
|
658
|
-
|
|
659
|
-
|
|
660
|
-
|
|
661
|
-
|
|
662
|
-
|
|
663
|
-
|
|
664
|
-
|
|
665
|
-
|
|
666
|
-
|
|
667
|
-
|
|
668
|
-
|
|
669
|
-
|
|
670
|
-
|
|
671
|
-
|
|
672
|
-
|
|
673
|
-
|
|
674
|
-
|
|
675
|
-
|
|
676
|
-
|
|
677
|
-
.map(function (_a) {
|
|
678
|
-
var amount = _a.amount;
|
|
679
|
-
return new bignumber_1.default((0, module_kit_1.newAmount)(amount).blockchain(_this.legacy.units).value);
|
|
680
|
-
})
|
|
681
|
-
.reduce(function (accumulator, currentValue) { return accumulator.plus(currentValue); })
|
|
682
|
-
.plus(wrappedFee);
|
|
683
|
-
valueAccumulator = new bignumber_1.default(0);
|
|
684
|
-
getPathIndexes = function (path) {
|
|
685
|
-
var result = path
|
|
686
|
-
.split('/')
|
|
687
|
-
.slice(-2)
|
|
688
|
-
.map(function (item) { return parseInt(item); })
|
|
689
|
-
.filter(function (item) { return !isNaN(item); });
|
|
690
|
-
if (result.length !== 2) {
|
|
691
|
-
throw new Error('Unexpected path format');
|
|
692
|
-
}
|
|
693
|
-
return [result[0], result[1]];
|
|
694
|
-
};
|
|
695
|
-
_i = 0, utxos_1 = utxos;
|
|
696
|
-
_c.label = 5;
|
|
697
|
-
case 5:
|
|
698
|
-
if (!(_i < utxos_1.length)) return [3 /*break*/, 9];
|
|
699
|
-
utxo = utxos_1[_i];
|
|
700
|
-
valueAccumulator = valueAccumulator.plus(utxo.value);
|
|
701
|
-
indexes = getPathIndexes(utxo.path);
|
|
702
|
-
return [4 /*yield*/, this.deriveFromExtendedPublicKey(extendedPublicKey, indexes[0], indexes[1])];
|
|
703
|
-
case 6:
|
|
704
|
-
derivedPublicKey = _c.sent();
|
|
705
|
-
return [4 /*yield*/, this.getAddressFromPublicKey(derivedPublicKey)];
|
|
706
|
-
case 7:
|
|
707
|
-
derivedAddress = _c.sent();
|
|
708
|
-
if (derivedAddress === utxo.address) {
|
|
709
|
-
transaction.ins.push({
|
|
710
|
-
txId: utxo.txid,
|
|
711
|
-
value: new bignumber_1.default(utxo.value).toString(10),
|
|
712
|
-
vout: utxo.vout,
|
|
713
|
-
address: utxo.address,
|
|
714
|
-
derivationPath: utxo.path
|
|
715
|
-
});
|
|
716
|
-
}
|
|
717
|
-
else {
|
|
718
|
-
throw new Error('Invalid address returned from API');
|
|
719
|
-
}
|
|
720
|
-
if (valueAccumulator.isGreaterThanOrEqualTo(totalRequiredBalance)) {
|
|
721
|
-
return [3 /*break*/, 9];
|
|
722
|
-
}
|
|
723
|
-
_c.label = 8;
|
|
724
|
-
case 8:
|
|
725
|
-
_i++;
|
|
726
|
-
return [3 /*break*/, 5];
|
|
727
|
-
case 9:
|
|
728
|
-
if (valueAccumulator.isLessThan(totalRequiredBalance)) {
|
|
729
|
-
throw new Error('not enough balance 2');
|
|
730
|
-
}
|
|
731
|
-
for (i = 0; i < details.length; i++) {
|
|
732
|
-
value = (0, module_kit_1.newAmount)(details[i].amount).blockchain(this.legacy.units).value;
|
|
733
|
-
transaction.outs.push({
|
|
734
|
-
recipient: details[i].to,
|
|
735
|
-
isChange: false,
|
|
736
|
-
value: value
|
|
737
|
-
});
|
|
738
|
-
valueAccumulator = valueAccumulator.minus(value);
|
|
739
|
-
}
|
|
740
|
-
lastUsedInternalAddress = Math.max.apply(Math, __spreadArray([-1], utxos
|
|
741
|
-
.map(function (utxo) { return getPathIndexes(utxo.path); })
|
|
742
|
-
.filter(function (indexes) { return indexes[0] === 1; })
|
|
743
|
-
.map(function (indexes) { return indexes[1]; }), false));
|
|
744
|
-
changeValue = valueAccumulator.minus(wrappedFee);
|
|
745
|
-
if (!changeValue.isGreaterThan(new bignumber_1.default(DUST_AMOUNT))) return [3 /*break*/, 12];
|
|
746
|
-
changeAddressIndex = lastUsedInternalAddress + 1;
|
|
747
|
-
return [4 /*yield*/, this.deriveFromExtendedPublicKey(extendedPublicKey, 1, changeAddressIndex)];
|
|
748
|
-
case 10:
|
|
749
|
-
derivedPublicKey = _c.sent();
|
|
750
|
-
return [4 /*yield*/, this.getAddressFromPublicKey(derivedPublicKey)];
|
|
751
|
-
case 11:
|
|
752
|
-
derivedAddress = _c.sent();
|
|
753
|
-
transaction.outs.push({
|
|
754
|
-
recipient: derivedAddress,
|
|
755
|
-
isChange: true,
|
|
756
|
-
value: changeValue.toString(10),
|
|
757
|
-
derivationPath: "1/".concat(changeAddressIndex)
|
|
758
|
-
});
|
|
759
|
-
_c.label = 12;
|
|
760
|
-
case 12:
|
|
761
|
-
psbt = new this.bitcoinJS.lib.Psbt();
|
|
762
|
-
psbt.addUnknownKeyValToGlobal({
|
|
763
|
-
key: Buffer.from('amount'),
|
|
764
|
-
value: Buffer.from(details
|
|
765
|
-
.reduce(function (accumulator, next) {
|
|
766
|
-
return accumulator.plus((0, module_kit_1.newAmount)(next.amount).blockchain(_this.legacy.units).value);
|
|
767
|
-
}, new bignumber_1.default(0))
|
|
768
|
-
.toString())
|
|
769
|
-
});
|
|
770
|
-
xpubExtendedPublicKey = (0, key_1.convertExtendedPublicKey)(extendedPublicKey, { format: 'encoded', type: 'xpub' });
|
|
771
|
-
keyPair = this.bip32.fromBase58(xpubExtendedPublicKey.value);
|
|
772
|
-
replaceByFee = (configuration === null || configuration === void 0 ? void 0 : configuration.replaceByFee) ? true : false;
|
|
773
|
-
_a = 0, _b = transaction.ins;
|
|
774
|
-
_c.label = 13;
|
|
775
|
-
case 13:
|
|
776
|
-
if (!(_a < _b.length)) return [3 /*break*/, 16];
|
|
777
|
-
tx = _b[_a];
|
|
778
|
-
indexes = getPathIndexes(tx.derivationPath);
|
|
779
|
-
childNode = keyPair.derivePath(indexes.join('/'));
|
|
780
|
-
return [4 /*yield*/, index_1.default.get("".concat(this.options.network.indexerApi, "/api/v2/tx/").concat(tx.txId), {
|
|
781
|
-
responseType: 'json'
|
|
782
|
-
})];
|
|
783
|
-
case 14:
|
|
784
|
-
data = (_c.sent()).data;
|
|
785
|
-
prevTxHex = data.hex;
|
|
786
|
-
if (!prevTxHex) {
|
|
787
|
-
throw new Error("Missing prev tx hex for ".concat(tx.txId));
|
|
788
|
-
}
|
|
789
|
-
psbt.addInput({
|
|
790
|
-
hash: tx.txId,
|
|
791
|
-
index: tx.vout,
|
|
792
|
-
sequence: replaceByFee ? 0xfffffffd : undefined,
|
|
793
|
-
nonWitnessUtxo: Buffer.from(prevTxHex, 'hex'),
|
|
794
|
-
bip32Derivation: [
|
|
795
|
-
{
|
|
796
|
-
masterFingerprint: Buffer.from(configuration.masterFingerprint.value, 'hex'),
|
|
797
|
-
pubkey: Buffer.from(childNode.publicKey),
|
|
798
|
-
path: tx.derivationPath
|
|
799
|
-
}
|
|
800
|
-
]
|
|
801
|
-
});
|
|
802
|
-
_c.label = 15;
|
|
803
|
-
case 15:
|
|
804
|
-
_a++;
|
|
805
|
-
return [3 /*break*/, 13];
|
|
806
|
-
case 16:
|
|
807
|
-
transaction.outs.forEach(function (out, index) {
|
|
808
|
-
psbt.addOutput({ address: out.recipient, value: parseInt(out.value, 10) });
|
|
809
|
-
if (out.derivationPath) {
|
|
810
|
-
psbt.addUnknownKeyValToOutput(index, {
|
|
811
|
-
key: Buffer.from('dp'),
|
|
812
|
-
value: Buffer.from(out.derivationPath, 'utf8')
|
|
813
|
-
});
|
|
814
|
-
}
|
|
815
|
-
});
|
|
816
|
-
return [2 /*return*/, (0, module_kit_1.newUnsignedTransaction)({ psbt: psbt.toHex() })];
|
|
817
|
-
}
|
|
507
|
+
}
|
|
508
|
+
const psbt = new this.bitcoinJS.lib.Psbt();
|
|
509
|
+
psbt.addUnknownKeyValToGlobal({
|
|
510
|
+
key: Buffer.from('amount'),
|
|
511
|
+
value: Buffer.from(details
|
|
512
|
+
.reduce((accumulator, next) => {
|
|
513
|
+
return accumulator.plus((0, module_kit_1.newAmount)(next.amount).blockchain(this.legacy.units).value);
|
|
514
|
+
}, new bignumber_1.default(0))
|
|
515
|
+
.toString())
|
|
516
|
+
});
|
|
517
|
+
const xpubExtendedPublicKey = (0, key_1.convertExtendedPublicKey)(extendedPublicKey, { format: 'encoded', type: 'xpub' });
|
|
518
|
+
const keyPair = this.bip32.fromBase58(xpubExtendedPublicKey.value);
|
|
519
|
+
const replaceByFee = configuration?.replaceByFee ? true : false;
|
|
520
|
+
for (const tx of transaction.ins) {
|
|
521
|
+
const indexes = getPathIndexes(tx.derivationPath);
|
|
522
|
+
const childNode = keyPair.derivePath(indexes.join('/'));
|
|
523
|
+
const { data } = await index_1.default.get(`${this.options.network.indexerApi}/api/v2/tx/${tx.txId}`, {
|
|
524
|
+
responseType: 'json'
|
|
525
|
+
});
|
|
526
|
+
const prevTxHex = data.hex;
|
|
527
|
+
if (!prevTxHex) {
|
|
528
|
+
throw new Error(`Missing prev tx hex for ${tx.txId}`);
|
|
529
|
+
}
|
|
530
|
+
psbt.addInput({
|
|
531
|
+
hash: tx.txId,
|
|
532
|
+
index: tx.vout,
|
|
533
|
+
sequence: replaceByFee ? 0xfffffffd : undefined,
|
|
534
|
+
nonWitnessUtxo: Buffer.from(prevTxHex, 'hex'),
|
|
535
|
+
bip32Derivation: [
|
|
536
|
+
{
|
|
537
|
+
masterFingerprint: Buffer.from(configuration.masterFingerprint.value, 'hex'),
|
|
538
|
+
pubkey: Buffer.from(childNode.publicKey),
|
|
539
|
+
path: tx.derivationPath
|
|
540
|
+
}
|
|
541
|
+
]
|
|
818
542
|
});
|
|
543
|
+
}
|
|
544
|
+
transaction.outs.forEach((out, index) => {
|
|
545
|
+
psbt.addOutput({ address: out.recipient, value: parseInt(out.value, 10) });
|
|
546
|
+
if (out.derivationPath) {
|
|
547
|
+
psbt.addUnknownKeyValToOutput(index, {
|
|
548
|
+
key: Buffer.from('dp'),
|
|
549
|
+
value: Buffer.from(out.derivationPath, 'utf8')
|
|
550
|
+
});
|
|
551
|
+
}
|
|
819
552
|
});
|
|
820
|
-
|
|
821
|
-
|
|
822
|
-
|
|
823
|
-
|
|
824
|
-
|
|
825
|
-
|
|
826
|
-
|
|
827
|
-
|
|
828
|
-
|
|
829
|
-
|
|
830
|
-
|
|
831
|
-
return [2 /*return*/, data.result];
|
|
832
|
-
}
|
|
833
|
-
});
|
|
553
|
+
return (0, module_kit_1.newUnsignedTransaction)({ psbt: psbt.toHex() });
|
|
554
|
+
}
|
|
555
|
+
async broadcastTransaction(transaction) {
|
|
556
|
+
const hexTransaction = this.bitcoinJS.lib.Psbt.fromHex(transaction.psbt).finalizeAllInputs().extractTransaction().toHex();
|
|
557
|
+
const { data } = await index_1.default.post(`${this.options.network.indexerApi}/api/v2/sendtx/`, hexTransaction);
|
|
558
|
+
return data.result;
|
|
559
|
+
}
|
|
560
|
+
convertCryptoDerivative(derivative) {
|
|
561
|
+
const hexNode = (0, crypto_1.encodeDerivative)('hex', {
|
|
562
|
+
...derivative,
|
|
563
|
+
secretKey: `00${derivative.secretKey}`
|
|
834
564
|
});
|
|
835
|
-
|
|
836
|
-
BitcoinLegacyProtocolImpl.prototype.convertCryptoDerivative = function (derivative) {
|
|
837
|
-
var hexNode = (0, crypto_1.encodeDerivative)('hex', __assign(__assign({}, derivative), { secretKey: "00".concat(derivative.secretKey) }));
|
|
838
|
-
var extendedSecretKey = {
|
|
565
|
+
const extendedSecretKey = {
|
|
839
566
|
type: 'xpriv',
|
|
840
567
|
format: 'hex',
|
|
841
568
|
value: hexNode.secretKey
|
|
842
569
|
};
|
|
843
570
|
return (0, key_1.convertExtendedSecretKey)(extendedSecretKey, { format: 'encoded', type: 'xprv' });
|
|
844
|
-
}
|
|
845
|
-
|
|
846
|
-
|
|
571
|
+
}
|
|
572
|
+
derivativeToBip32Node(derivative) {
|
|
573
|
+
const extendedSecretKey = this.convertCryptoDerivative(derivative);
|
|
847
574
|
return this.bip32.fromBase58(extendedSecretKey.value, this.bitcoinJS.config.network);
|
|
848
|
-
}
|
|
849
|
-
|
|
850
|
-
}());
|
|
575
|
+
}
|
|
576
|
+
}
|
|
851
577
|
exports.BitcoinLegacyProtocolImpl = BitcoinLegacyProtocolImpl;
|
|
852
578
|
// Factory
|
|
853
|
-
function createBitcoinLegacyProtocol(options) {
|
|
854
|
-
if (options === void 0) { options = {}; }
|
|
579
|
+
function createBitcoinLegacyProtocol(options = {}) {
|
|
855
580
|
return new BitcoinLegacyProtocolImpl(options);
|
|
856
581
|
}
|
|
857
|
-
exports.createBitcoinLegacyProtocol = createBitcoinLegacyProtocol;
|
|
858
582
|
//# sourceMappingURL=BitcoinLegacyProtocol.js.map
|