@airgap/bitcoin 0.13.11-beta.0 → 0.13.11-beta.1
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 +11 -9
- package/airgap-coinlib-bitcoin.min.js +0 -99733
- package/index.d.ts +0 -1
- package/index.js +0 -18
- package/index.js.map +0 -1
- package/v0/index.d.ts +0 -14
- package/v0/index.js +0 -34
- package/v0/index.js.map +0 -1
- package/v0/protocol/BitcoinAddress.d.ts +0 -9
- package/v0/protocol/BitcoinAddress.js +0 -23
- package/v0/protocol/BitcoinAddress.js.map +0 -1
- package/v0/protocol/BitcoinCryptoClient.d.ts +0 -11
- package/v0/protocol/BitcoinCryptoClient.js +0 -91
- package/v0/protocol/BitcoinCryptoClient.js.map +0 -1
- package/v0/protocol/BitcoinProtocol.d.ts +0 -167
- package/v0/protocol/BitcoinProtocol.js +0 -1063
- package/v0/protocol/BitcoinProtocol.js.map +0 -1
- package/v0/protocol/BitcoinProtocolOptions.d.ts +0 -26
- package/v0/protocol/BitcoinProtocolOptions.js +0 -148
- package/v0/protocol/BitcoinProtocolOptions.js.map +0 -1
- package/v0/protocol/BitcoinSegwitAddress.d.ts +0 -7
- package/v0/protocol/BitcoinSegwitAddress.js +0 -41
- package/v0/protocol/BitcoinSegwitAddress.js.map +0 -1
- package/v0/protocol/BitcoinSegwitProtocol.d.ts +0 -28
- package/v0/protocol/BitcoinSegwitProtocol.js +0 -541
- package/v0/protocol/BitcoinSegwitProtocol.js.map +0 -1
- package/v0/protocol/BitcoinTestnetProtocol.d.ts +0 -7
- package/v0/protocol/BitcoinTestnetProtocol.js +0 -58
- package/v0/protocol/BitcoinTestnetProtocol.js.map +0 -1
- package/v0/protocol/BitcoinTypes.d.ts +0 -21
- package/v0/protocol/BitcoinTypes.js +0 -3
- package/v0/protocol/BitcoinTypes.js.map +0 -1
- package/v0/serializer/schemas/v2/transaction-sign-request-bitcoin-segwit.json +0 -0
- package/v0/serializer/schemas/v2/transaction-sign-request-bitcoin.json +0 -73
- package/v0/serializer/schemas/v2/transaction-sign-response-bitcoin-segwit.json +0 -0
- package/v0/serializer/schemas/v2/transaction-sign-response-bitcoin.json +0 -37
- package/v0/serializer/schemas/v3/transaction-sign-request-bitcoin-segwit.json +0 -29
- package/v0/serializer/schemas/v3/transaction-sign-request-bitcoin.json +0 -73
- package/v0/serializer/schemas/v3/transaction-sign-response-bitcoin-segwit.json +0 -19
- package/v0/serializer/schemas/v3/transaction-sign-response-bitcoin.json +0 -37
- package/v0/serializer/validators/transaction-validator.d.ts +0 -15
- package/v0/serializer/validators/transaction-validator.js +0 -81
- package/v0/serializer/validators/transaction-validator.js.map +0 -1
- package/v0/serializer/validators/validators.d.ts +0 -1
- package/v0/serializer/validators/validators.js +0 -151
- package/v0/serializer/validators/validators.js.map +0 -1
- package/v0/types/signed-transaction-bitcoin-segwit.d.ts +0 -5
- package/v0/types/signed-transaction-bitcoin-segwit.js +0 -3
- package/v0/types/signed-transaction-bitcoin-segwit.js.map +0 -1
- package/v0/types/signed-transaction-bitcoin.d.ts +0 -9
- package/v0/types/signed-transaction-bitcoin.js +0 -3
- package/v0/types/signed-transaction-bitcoin.js.map +0 -1
- package/v0/types/transaction-bitcoin.d.ts +0 -20
- package/v0/types/transaction-bitcoin.js +0 -3
- package/v0/types/transaction-bitcoin.js.map +0 -1
- package/v0/types/unsigned-transaction-bitcoin-segwit.d.ts +0 -7
- package/v0/types/unsigned-transaction-bitcoin-segwit.js +0 -3
- package/v0/types/unsigned-transaction-bitcoin-segwit.js.map +0 -1
- package/v0/types/unsigned-transaction-bitcoin.d.ts +0 -22
- package/v0/types/unsigned-transaction-bitcoin.js +0 -3
- package/v0/types/unsigned-transaction-bitcoin.js.map +0 -1
- package/v1/block-explorer/BlockCypherBlockExplorer.d.ts +0 -9
- package/v1/block-explorer/BlockCypherBlockExplorer.js +0 -74
- package/v1/block-explorer/BlockCypherBlockExplorer.js.map +0 -1
- package/v1/data/BitcoinAddress.d.ts +0 -7
- package/v1/data/BitcoinAddress.js +0 -20
- package/v1/data/BitcoinAddress.js.map +0 -1
- package/v1/data/BitcoinSegwitAddress.d.ts +0 -8
- package/v1/data/BitcoinSegwitAddress.js +0 -25
- package/v1/data/BitcoinSegwitAddress.js.map +0 -1
- package/v1/index.d.ts +0 -19
- package/v1/index.js +0 -24
- package/v1/index.js.map +0 -1
- package/v1/module/BitcoinModule.d.ts +0 -16
- package/v1/module/BitcoinModule.js +0 -112
- package/v1/module/BitcoinModule.js.map +0 -1
- package/v1/module.d.ts +0 -3
- package/v1/module.js +0 -24
- package/v1/module.js.map +0 -1
- package/v1/protocol/BitcoinCryptoClient.d.ts +0 -12
- package/v1/protocol/BitcoinCryptoClient.js +0 -96
- package/v1/protocol/BitcoinCryptoClient.js.map +0 -1
- package/v1/protocol/BitcoinProtocol.d.ts +0 -69
- package/v1/protocol/BitcoinProtocol.js +0 -1211
- package/v1/protocol/BitcoinProtocol.js.map +0 -1
- package/v1/protocol/BitcoinSegwitProtocol.d.ts +0 -52
- package/v1/protocol/BitcoinSegwitProtocol.js +0 -751
- package/v1/protocol/BitcoinSegwitProtocol.js.map +0 -1
- package/v1/protocol/BitcoinTestnetProtocol.d.ts +0 -11
- package/v1/protocol/BitcoinTestnetProtocol.js +0 -67
- package/v1/protocol/BitcoinTestnetProtocol.js.map +0 -1
- package/v1/serializer/v3/schemas/converter/transaction-converter.d.ts +0 -13
- package/v1/serializer/v3/schemas/converter/transaction-converter.js +0 -75
- package/v1/serializer/v3/schemas/converter/transaction-converter.js.map +0 -1
- package/v1/serializer/v3/schemas/definitions/transaction-sign-request-bitcoin-segwit.d.ts +0 -4
- package/v1/serializer/v3/schemas/definitions/transaction-sign-request-bitcoin-segwit.js +0 -3
- package/v1/serializer/v3/schemas/definitions/transaction-sign-request-bitcoin-segwit.js.map +0 -1
- package/v1/serializer/v3/schemas/definitions/transaction-sign-request-bitcoin.d.ts +0 -4
- package/v1/serializer/v3/schemas/definitions/transaction-sign-request-bitcoin.js +0 -3
- package/v1/serializer/v3/schemas/definitions/transaction-sign-request-bitcoin.js.map +0 -1
- package/v1/serializer/v3/schemas/definitions/transaction-sign-response-bitcoin-segwit.d.ts +0 -3
- package/v1/serializer/v3/schemas/definitions/transaction-sign-response-bitcoin-segwit.js +0 -3
- package/v1/serializer/v3/schemas/definitions/transaction-sign-response-bitcoin-segwit.js.map +0 -1
- package/v1/serializer/v3/schemas/definitions/transaction-sign-response-bitcoin.d.ts +0 -7
- package/v1/serializer/v3/schemas/definitions/transaction-sign-response-bitcoin.js +0 -3
- package/v1/serializer/v3/schemas/definitions/transaction-sign-response-bitcoin.js.map +0 -1
- package/v1/serializer/v3/schemas/generated/transaction-sign-request-bitcoin-segwit.json +0 -29
- package/v1/serializer/v3/schemas/generated/transaction-sign-request-bitcoin.json +0 -79
- package/v1/serializer/v3/schemas/generated/transaction-sign-response-bitcoin-segwit.json +0 -19
- package/v1/serializer/v3/schemas/generated/transaction-sign-response-bitcoin.json +0 -37
- package/v1/serializer/v3/serializer-companion.d.ts +0 -14
- package/v1/serializer/v3/serializer-companion.js +0 -197
- package/v1/serializer/v3/serializer-companion.js.map +0 -1
- package/v1/serializer/v3/validators/transaction-validator.d.ts +0 -7
- package/v1/serializer/v3/validators/transaction-validator.js +0 -56
- package/v1/serializer/v3/validators/transaction-validator.js.map +0 -1
- package/v1/serializer/v3/validators/validators.d.ts +0 -7
- package/v1/serializer/v3/validators/validators.js +0 -254
- package/v1/serializer/v3/validators/validators.js.map +0 -1
- package/v1/types/bitcoinjs.d.ts +0 -14
- package/v1/types/bitcoinjs.js +0 -3
- package/v1/types/bitcoinjs.js.map +0 -1
- package/v1/types/indexer.d.ts +0 -81
- package/v1/types/indexer.js +0 -3
- package/v1/types/indexer.js.map +0 -1
- package/v1/types/key.d.ts +0 -6
- package/v1/types/key.js +0 -3
- package/v1/types/key.js.map +0 -1
- package/v1/types/protocol.d.ts +0 -17
- package/v1/types/protocol.js +0 -3
- package/v1/types/protocol.js.map +0 -1
- package/v1/types/transaction.d.ts +0 -39
- package/v1/types/transaction.js +0 -3
- package/v1/types/transaction.js.map +0 -1
- package/v1/utils/common.d.ts +0 -2
- package/v1/utils/common.js +0 -28
- package/v1/utils/common.js.map +0 -1
- package/v1/utils/key.d.ts +0 -12
- package/v1/utils/key.js +0 -158
- package/v1/utils/key.js.map +0 -1
- package/v1/utils/network.d.ts +0 -2
- package/v1/utils/network.js +0 -22
- package/v1/utils/network.js.map +0 -1
- package/v1/utils/signature.d.ts +0 -2
- package/v1/utils/signature.js +0 -34
- package/v1/utils/signature.js.map +0 -1
|
@@ -1,751 +0,0 @@
|
|
|
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
|
-
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
14
|
-
if (k2 === undefined) k2 = k;
|
|
15
|
-
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
16
|
-
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
17
|
-
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
18
|
-
}
|
|
19
|
-
Object.defineProperty(o, k2, desc);
|
|
20
|
-
}) : (function(o, m, k, k2) {
|
|
21
|
-
if (k2 === undefined) k2 = k;
|
|
22
|
-
o[k2] = m[k];
|
|
23
|
-
}));
|
|
24
|
-
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
25
|
-
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
26
|
-
}) : function(o, v) {
|
|
27
|
-
o["default"] = v;
|
|
28
|
-
});
|
|
29
|
-
var __importStar = (this && this.__importStar) || function (mod) {
|
|
30
|
-
if (mod && mod.__esModule) return mod;
|
|
31
|
-
var result = {};
|
|
32
|
-
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
|
33
|
-
__setModuleDefault(result, mod);
|
|
34
|
-
return result;
|
|
35
|
-
};
|
|
36
|
-
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
37
|
-
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
38
|
-
return new (P || (P = Promise))(function (resolve, reject) {
|
|
39
|
-
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
40
|
-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
41
|
-
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
42
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
43
|
-
});
|
|
44
|
-
};
|
|
45
|
-
var __generator = (this && this.__generator) || function (thisArg, body) {
|
|
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
|
-
};
|
|
81
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
82
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
83
|
-
};
|
|
84
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
85
|
-
exports.createBitcoinSegwitProtocol = exports.BitcoinSegwitProtocolImpl = void 0;
|
|
86
|
-
var coinlib_core_1 = require("@airgap/coinlib-core");
|
|
87
|
-
var index_1 = __importDefault(require("@airgap/coinlib-core/dependencies/src/axios-0.19.0/index"));
|
|
88
|
-
var bignumber_1 = __importDefault(require("@airgap/coinlib-core/dependencies/src/bignumber.js-9.0.0/bignumber"));
|
|
89
|
-
var index_2 = require("@airgap/coinlib-core/dependencies/src/bip39-2.5.0/index");
|
|
90
|
-
var errors_1 = require("@airgap/coinlib-core/errors");
|
|
91
|
-
var module_kit_1 = require("@airgap/module-kit");
|
|
92
|
-
var bitcoin = __importStar(require("bitcoinjs-lib"));
|
|
93
|
-
var BitcoinSegwitAddress_1 = require("../data/BitcoinSegwitAddress");
|
|
94
|
-
var common_1 = require("../utils/common");
|
|
95
|
-
var key_1 = require("../utils/key");
|
|
96
|
-
var network_1 = require("../utils/network");
|
|
97
|
-
var BitcoinProtocol_1 = require("./BitcoinProtocol");
|
|
98
|
-
// Implementation
|
|
99
|
-
var DUST_AMOUNT = 50;
|
|
100
|
-
var BitcoinSegwitProtocolImpl = /** @class */ (function () {
|
|
101
|
-
function BitcoinSegwitProtocolImpl(options, bitcoinJS) {
|
|
102
|
-
if (options === void 0) { options = {}; }
|
|
103
|
-
if (bitcoinJS === void 0) { bitcoinJS = bitcoin; }
|
|
104
|
-
var _a, _b, _c;
|
|
105
|
-
this.legacy = new BitcoinProtocol_1.BitcoinProtocolImpl(options);
|
|
106
|
-
this.options = (0, BitcoinProtocol_1.createBitcoinProtocolOptions)(options.network);
|
|
107
|
-
this.bitcoinJS = {
|
|
108
|
-
lib: bitcoinJS,
|
|
109
|
-
config: {
|
|
110
|
-
network: (0, network_1.getBitcoinJSNetwork)(this.options.network, bitcoinJS)
|
|
111
|
-
}
|
|
112
|
-
};
|
|
113
|
-
this.metadata = __assign(__assign({}, this.legacy.metadata), { identifier: coinlib_core_1.MainProtocolSymbols.BTC_SEGWIT, name: 'Bitcoin (Segwit)', account: __assign(__assign({}, ((_a = this.legacy.metadata.account) !== null && _a !== void 0 ? _a : {})), { standardDerivationPath: "m/84'/0'/0'", address: __assign(__assign({}, ((_c = (_b = this.legacy.metadata.account) === null || _b === void 0 ? void 0 : _b.address) !== null && _c !== void 0 ? _c : {})), { regex: 'bc1...' }) }) });
|
|
114
|
-
}
|
|
115
|
-
BitcoinSegwitProtocolImpl.prototype.getMetadata = function () {
|
|
116
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
117
|
-
return __generator(this, function (_a) {
|
|
118
|
-
return [2 /*return*/, this.metadata];
|
|
119
|
-
});
|
|
120
|
-
});
|
|
121
|
-
};
|
|
122
|
-
BitcoinSegwitProtocolImpl.prototype.getAddressFromPublicKey = function (publicKey) {
|
|
123
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
124
|
-
return __generator(this, function (_a) {
|
|
125
|
-
switch (publicKey.type) {
|
|
126
|
-
case 'pub':
|
|
127
|
-
return [2 /*return*/, this.getAddressFromNonExtendedPublicKey(publicKey)];
|
|
128
|
-
case 'xpub':
|
|
129
|
-
return [2 /*return*/, this.getAddressFromExtendedPublicKey(publicKey)];
|
|
130
|
-
default:
|
|
131
|
-
(0, coinlib_core_1.assertNever)(publicKey);
|
|
132
|
-
throw new errors_1.UnsupportedError(coinlib_core_1.Domain.BITCOIN, 'Public key type is not supported.');
|
|
133
|
-
}
|
|
134
|
-
return [2 /*return*/];
|
|
135
|
-
});
|
|
136
|
-
});
|
|
137
|
-
};
|
|
138
|
-
BitcoinSegwitProtocolImpl.prototype.getAddressFromNonExtendedPublicKey = function (publicKey) {
|
|
139
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
140
|
-
var hexPublicKey, payment;
|
|
141
|
-
return __generator(this, function (_a) {
|
|
142
|
-
hexPublicKey = (0, key_1.convertPublicKey)(publicKey, 'hex');
|
|
143
|
-
payment = this.bitcoinJS.lib.payments.p2wpkh({ pubkey: Buffer.from(hexPublicKey.value, 'hex') });
|
|
144
|
-
return [2 /*return*/, BitcoinSegwitAddress_1.BitcoinSegwitAddress.fromPayment(payment).asString()];
|
|
145
|
-
});
|
|
146
|
-
});
|
|
147
|
-
};
|
|
148
|
-
BitcoinSegwitProtocolImpl.prototype.getAddressFromExtendedPublicKey = function (extendedPublicKey) {
|
|
149
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
150
|
-
var encodedExtendedPublicKey, bip32;
|
|
151
|
-
return __generator(this, function (_a) {
|
|
152
|
-
encodedExtendedPublicKey = (0, key_1.convertExtendedPublicKey)(extendedPublicKey, { format: 'encoded', type: 'xpub' });
|
|
153
|
-
bip32 = this.bitcoinJS.lib.bip32.fromBase58(encodedExtendedPublicKey.value, this.bitcoinJS.config.network);
|
|
154
|
-
return [2 /*return*/, BitcoinSegwitAddress_1.BitcoinSegwitAddress.fromBip32(bip32).asString()];
|
|
155
|
-
});
|
|
156
|
-
});
|
|
157
|
-
};
|
|
158
|
-
BitcoinSegwitProtocolImpl.prototype.deriveFromExtendedPublicKey = function (publicKey, visibilityIndex, addressIndex) {
|
|
159
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
160
|
-
var encodedPublicKey, derivedBip32;
|
|
161
|
-
return __generator(this, function (_a) {
|
|
162
|
-
encodedPublicKey = (0, key_1.convertExtendedPublicKey)(publicKey, { format: 'encoded', type: 'xpub' });
|
|
163
|
-
derivedBip32 = this.bitcoinJS.lib.bip32
|
|
164
|
-
.fromBase58(encodedPublicKey.value, this.bitcoinJS.config.network)
|
|
165
|
-
.derive(visibilityIndex)
|
|
166
|
-
.derive(addressIndex);
|
|
167
|
-
return [2 /*return*/, (0, module_kit_1.newPublicKey)(derivedBip32.publicKey.toString('hex'), 'hex')];
|
|
168
|
-
});
|
|
169
|
-
});
|
|
170
|
-
};
|
|
171
|
-
BitcoinSegwitProtocolImpl.prototype.getDetailsFromTransaction = function (transaction, _publicKey) {
|
|
172
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
173
|
-
return __generator(this, function (_a) {
|
|
174
|
-
return [2 /*return*/, this.getDetailsFromPSBT(transaction.psbt)];
|
|
175
|
-
});
|
|
176
|
-
});
|
|
177
|
-
};
|
|
178
|
-
BitcoinSegwitProtocolImpl.prototype.getDetailsFromPSBT = function (psbt) {
|
|
179
|
-
var _a, _b;
|
|
180
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
181
|
-
var decodedPSBT, fee, _i, _c, txIn, _d, _e, txOut, alerts, clonedPSBT, amount;
|
|
182
|
-
var _this = this;
|
|
183
|
-
return __generator(this, function (_f) {
|
|
184
|
-
decodedPSBT = this.bitcoinJS.lib.Psbt.fromHex(psbt);
|
|
185
|
-
fee = new bignumber_1.default(0);
|
|
186
|
-
for (_i = 0, _c = decodedPSBT.data.inputs; _i < _c.length; _i++) {
|
|
187
|
-
txIn = _c[_i];
|
|
188
|
-
fee = fee.plus(new bignumber_1.default((_b = (_a = txIn.witnessUtxo) === null || _a === void 0 ? void 0 : _a.value) !== null && _b !== void 0 ? _b : 0));
|
|
189
|
-
}
|
|
190
|
-
for (_d = 0, _e = decodedPSBT.txOutputs; _d < _e.length; _d++) {
|
|
191
|
-
txOut = _e[_d];
|
|
192
|
-
fee = fee.minus(new bignumber_1.default(txOut.value));
|
|
193
|
-
}
|
|
194
|
-
alerts = [];
|
|
195
|
-
clonedPSBT = decodedPSBT.clone();
|
|
196
|
-
(0, common_1.eachRecursive)(clonedPSBT); // All buffers to hex string
|
|
197
|
-
amount = (function () {
|
|
198
|
-
// If tx has only one output, this is the amount
|
|
199
|
-
if (decodedPSBT.txOutputs.length === 1) {
|
|
200
|
-
return new bignumber_1.default(decodedPSBT.txOutputs[0].value);
|
|
201
|
-
}
|
|
202
|
-
// If we can match one output to an exact amount that we add to the PSBT, this is our amount.
|
|
203
|
-
{
|
|
204
|
-
var unknownKeyVals = decodedPSBT.data.globalMap.unknownKeyVals;
|
|
205
|
-
if (unknownKeyVals) {
|
|
206
|
-
var amountArray = unknownKeyVals.filter(function (kv) { return kv.key.equals(Buffer.from('amount')); });
|
|
207
|
-
if (amountArray.length > 0) {
|
|
208
|
-
return new bignumber_1.default(amountArray[0].value.toString()); // Buffer to number
|
|
209
|
-
}
|
|
210
|
-
}
|
|
211
|
-
}
|
|
212
|
-
// If tx has an output and we added a derivation path in the PSBT (in the wallet, prepareTransaction), we know that it is a change address and we ignore it.
|
|
213
|
-
var accumulated = new bignumber_1.default(0);
|
|
214
|
-
var useAccumulated = false;
|
|
215
|
-
decodedPSBT.data.outputs.forEach(function (outputKeyValues, index) {
|
|
216
|
-
if (outputKeyValues.unknownKeyVals) {
|
|
217
|
-
var derivationPaths = outputKeyValues.unknownKeyVals
|
|
218
|
-
.filter(function (kv) { return kv.key.equals(Buffer.from('dp')); })
|
|
219
|
-
.map(function (kv) { return kv.value.toString(); });
|
|
220
|
-
if (derivationPaths.length > 0) {
|
|
221
|
-
// If one of the outputs has the derivation in the custom key/value map, we can use this to determine the amount.
|
|
222
|
-
useAccumulated = true;
|
|
223
|
-
return;
|
|
224
|
-
}
|
|
225
|
-
}
|
|
226
|
-
var output = decodedPSBT.txOutputs[index];
|
|
227
|
-
accumulated = accumulated.plus(output.value);
|
|
228
|
-
});
|
|
229
|
-
if (useAccumulated) {
|
|
230
|
-
return accumulated;
|
|
231
|
-
}
|
|
232
|
-
// If we cannot match anything above, we need to assume that the whole amount is being sent and the user has to check the outputs.
|
|
233
|
-
return decodedPSBT.txOutputs
|
|
234
|
-
.map(function (obj) { return new bignumber_1.default(obj.value); })
|
|
235
|
-
.reduce(function (accumulator, currentValue) { return accumulator.plus(currentValue); });
|
|
236
|
-
})();
|
|
237
|
-
return [2 /*return*/, [
|
|
238
|
-
{
|
|
239
|
-
from: decodedPSBT.data.inputs.map(function (obj) {
|
|
240
|
-
var _a, _b;
|
|
241
|
-
return (_b = (_a = obj.bip32Derivation) === null || _a === void 0 ? void 0 : _a.map(function (el) {
|
|
242
|
-
return _this.bitcoinJS.lib.payments.p2wpkh({
|
|
243
|
-
pubkey: el.pubkey,
|
|
244
|
-
network: _this.bitcoinJS.lib.networks.bitcoin
|
|
245
|
-
}).address;
|
|
246
|
-
}).join(' ')) !== null && _b !== void 0 ? _b : 'INVALID';
|
|
247
|
-
}),
|
|
248
|
-
to: decodedPSBT.txOutputs.map(function (obj) {
|
|
249
|
-
return obj.address || "Script: ".concat(obj.script.toString('hex')) || 'unknown';
|
|
250
|
-
}),
|
|
251
|
-
isInbound: false,
|
|
252
|
-
amount: (0, module_kit_1.newAmount)(amount, 'blockchain'),
|
|
253
|
-
fee: (0, module_kit_1.newAmount)(fee, 'blockchain'),
|
|
254
|
-
network: this.options.network,
|
|
255
|
-
uiAlerts: alerts,
|
|
256
|
-
json: {
|
|
257
|
-
// This is some unstructured data about the PSBT. This is shown in the UI as a JSON for advanced users.
|
|
258
|
-
inputTx: (0, common_1.eachRecursive)(clonedPSBT.txInputs),
|
|
259
|
-
outputTx: (0, common_1.eachRecursive)(clonedPSBT.txOutputs),
|
|
260
|
-
inputData: clonedPSBT.data.inputs,
|
|
261
|
-
outputData: clonedPSBT.data.outputs,
|
|
262
|
-
PSBTVersion: clonedPSBT.version,
|
|
263
|
-
PSBTLocktime: clonedPSBT.locktime,
|
|
264
|
-
PSBTGlobalMap: clonedPSBT.data.globalMap,
|
|
265
|
-
rawPSBT: psbt
|
|
266
|
-
}
|
|
267
|
-
}
|
|
268
|
-
]];
|
|
269
|
-
});
|
|
270
|
-
});
|
|
271
|
-
};
|
|
272
|
-
BitcoinSegwitProtocolImpl.prototype.verifyMessageWithPublicKey = function (message, signature, publicKey) {
|
|
273
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
274
|
-
return __generator(this, function (_a) {
|
|
275
|
-
return [2 /*return*/, this.legacy.verifyMessageWithPublicKey(message, signature, publicKey)];
|
|
276
|
-
});
|
|
277
|
-
});
|
|
278
|
-
};
|
|
279
|
-
BitcoinSegwitProtocolImpl.prototype.encryptAsymmetricWithPublicKey = function (payload, publicKey) {
|
|
280
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
281
|
-
return __generator(this, function (_a) {
|
|
282
|
-
return [2 /*return*/, this.legacy.encryptAsymmetricWithPublicKey(payload, publicKey)];
|
|
283
|
-
});
|
|
284
|
-
});
|
|
285
|
-
};
|
|
286
|
-
// Offline
|
|
287
|
-
BitcoinSegwitProtocolImpl.prototype.getKeyPairFromSecret = function (secret, derivationPath) {
|
|
288
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
289
|
-
return __generator(this, function (_a) {
|
|
290
|
-
switch (secret.type) {
|
|
291
|
-
case 'hex':
|
|
292
|
-
return [2 /*return*/, this.getKeyPairFromHexSecret(secret.value, derivationPath)];
|
|
293
|
-
case 'mnemonic':
|
|
294
|
-
return [2 /*return*/, this.getKeyPairFromMnemonic(secret.value, derivationPath, secret.password)];
|
|
295
|
-
default:
|
|
296
|
-
(0, coinlib_core_1.assertNever)(secret);
|
|
297
|
-
throw new errors_1.UnsupportedError(coinlib_core_1.Domain.BITCOIN, 'Unsupported secret type.');
|
|
298
|
-
}
|
|
299
|
-
return [2 /*return*/];
|
|
300
|
-
});
|
|
301
|
-
});
|
|
302
|
-
};
|
|
303
|
-
BitcoinSegwitProtocolImpl.prototype.getKeyPairFromHexSecret = function (secret, derivationPath) {
|
|
304
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
305
|
-
var bip32, derivedBip32, privateKey, publicKey;
|
|
306
|
-
return __generator(this, function (_a) {
|
|
307
|
-
bip32 = this.bitcoinJS.lib.bip32.fromSeed(Buffer.from(secret, 'hex'), this.bitcoinJS.config.network);
|
|
308
|
-
derivedBip32 = derivationPath ? bip32.derivePath(derivationPath) : bip32;
|
|
309
|
-
privateKey = derivedBip32.privateKey;
|
|
310
|
-
if (privateKey === undefined) {
|
|
311
|
-
throw new Error('No private key!');
|
|
312
|
-
}
|
|
313
|
-
publicKey = derivedBip32.publicKey;
|
|
314
|
-
return [2 /*return*/, {
|
|
315
|
-
secretKey: (0, module_kit_1.newSecretKey)(privateKey.toString('hex'), 'hex'),
|
|
316
|
-
publicKey: (0, module_kit_1.newPublicKey)(publicKey.toString('hex'), 'hex')
|
|
317
|
-
}];
|
|
318
|
-
});
|
|
319
|
-
});
|
|
320
|
-
};
|
|
321
|
-
BitcoinSegwitProtocolImpl.prototype.getKeyPairFromMnemonic = function (mnemonic, derivationPath, password) {
|
|
322
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
323
|
-
var secret;
|
|
324
|
-
return __generator(this, function (_a) {
|
|
325
|
-
secret = (0, index_2.mnemonicToSeed)(mnemonic, password);
|
|
326
|
-
return [2 /*return*/, this.getKeyPairFromHexSecret(secret.toString('hex'), derivationPath)];
|
|
327
|
-
});
|
|
328
|
-
});
|
|
329
|
-
};
|
|
330
|
-
BitcoinSegwitProtocolImpl.prototype.getExtendedKeyPairFromSecret = function (secret, derivationPath) {
|
|
331
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
332
|
-
return __generator(this, function (_a) {
|
|
333
|
-
switch (secret.type) {
|
|
334
|
-
case 'hex':
|
|
335
|
-
return [2 /*return*/, this.getExtendedKeyPairFromHexSecret(secret.value, derivationPath)];
|
|
336
|
-
case 'mnemonic':
|
|
337
|
-
return [2 /*return*/, this.getExtendedKeyPairFromMnemonic(secret.value, derivationPath, secret.password)];
|
|
338
|
-
default:
|
|
339
|
-
(0, coinlib_core_1.assertNever)(secret);
|
|
340
|
-
throw new errors_1.UnsupportedError(coinlib_core_1.Domain.BITCOIN, 'Unsupported secret type.');
|
|
341
|
-
}
|
|
342
|
-
return [2 /*return*/];
|
|
343
|
-
});
|
|
344
|
-
});
|
|
345
|
-
};
|
|
346
|
-
BitcoinSegwitProtocolImpl.prototype.getExtendedKeyPairFromHexSecret = function (secret, derivationPath) {
|
|
347
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
348
|
-
var bip32, derivedBip32;
|
|
349
|
-
return __generator(this, function (_a) {
|
|
350
|
-
bip32 = this.bitcoinJS.lib.bip32.fromSeed(Buffer.from(secret, 'hex'), this.bitcoinJS.config.network);
|
|
351
|
-
derivedBip32 = derivationPath ? bip32.derivePath(derivationPath) : bip32;
|
|
352
|
-
return [2 /*return*/, {
|
|
353
|
-
secretKey: (0, module_kit_1.newExtendedSecretKey)(derivedBip32.toBase58(), 'encoded'),
|
|
354
|
-
publicKey: (0, key_1.convertExtendedPublicKey)((0, module_kit_1.newExtendedPublicKey)(derivedBip32.neutered().toBase58(), 'encoded'), {
|
|
355
|
-
format: 'encoded',
|
|
356
|
-
type: 'zpub'
|
|
357
|
-
})
|
|
358
|
-
}];
|
|
359
|
-
});
|
|
360
|
-
});
|
|
361
|
-
};
|
|
362
|
-
BitcoinSegwitProtocolImpl.prototype.getExtendedKeyPairFromMnemonic = function (mnemonic, derivationPath, password) {
|
|
363
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
364
|
-
var secret;
|
|
365
|
-
return __generator(this, function (_a) {
|
|
366
|
-
secret = (0, index_2.mnemonicToSeed)(mnemonic, password);
|
|
367
|
-
return [2 /*return*/, this.getExtendedKeyPairFromHexSecret(secret.toString('hex'), derivationPath)];
|
|
368
|
-
});
|
|
369
|
-
});
|
|
370
|
-
};
|
|
371
|
-
BitcoinSegwitProtocolImpl.prototype.deriveFromExtendedSecretKey = function (extendedSecretKey, visibilityIndex, addressIndex) {
|
|
372
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
373
|
-
var encodedSecretKey, derivedBip32, privateKey;
|
|
374
|
-
return __generator(this, function (_a) {
|
|
375
|
-
encodedSecretKey = (0, key_1.convertExtendedSecretKey)(extendedSecretKey, 'encoded');
|
|
376
|
-
derivedBip32 = this.bitcoinJS.lib.bip32
|
|
377
|
-
.fromBase58(encodedSecretKey.value, this.bitcoinJS.config.network)
|
|
378
|
-
.derive(visibilityIndex)
|
|
379
|
-
.derive(addressIndex);
|
|
380
|
-
privateKey = derivedBip32.privateKey;
|
|
381
|
-
if (privateKey === undefined) {
|
|
382
|
-
throw new Error('No private key!');
|
|
383
|
-
}
|
|
384
|
-
return [2 /*return*/, (0, module_kit_1.newSecretKey)(privateKey.toString('hex'), 'hex')];
|
|
385
|
-
});
|
|
386
|
-
});
|
|
387
|
-
};
|
|
388
|
-
BitcoinSegwitProtocolImpl.prototype.signTransactionWithSecretKey = function (transaction, secretKey) {
|
|
389
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
390
|
-
return __generator(this, function (_a) {
|
|
391
|
-
switch (secretKey.type) {
|
|
392
|
-
case 'priv':
|
|
393
|
-
return [2 /*return*/, this.signTransactionWithNonExtendedSecretKey(transaction, secretKey)];
|
|
394
|
-
case 'xpriv':
|
|
395
|
-
return [2 /*return*/, this.signTransactionWithExtendedSecretKey(transaction, secretKey)];
|
|
396
|
-
default:
|
|
397
|
-
(0, coinlib_core_1.assertNever)(secretKey);
|
|
398
|
-
throw new errors_1.UnsupportedError(coinlib_core_1.Domain.BITCOIN, 'Secret key type not supported.');
|
|
399
|
-
}
|
|
400
|
-
return [2 /*return*/];
|
|
401
|
-
});
|
|
402
|
-
});
|
|
403
|
-
};
|
|
404
|
-
BitcoinSegwitProtocolImpl.prototype.signTransactionWithNonExtendedSecretKey = function (transaction, secretKey) {
|
|
405
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
406
|
-
return __generator(this, function (_a) {
|
|
407
|
-
// No reference implementation in v0
|
|
408
|
-
throw new errors_1.UnsupportedError(coinlib_core_1.Domain.BITCOIN, 'Sign with non extended secret key not supported (Segwit).');
|
|
409
|
-
});
|
|
410
|
-
});
|
|
411
|
-
};
|
|
412
|
-
BitcoinSegwitProtocolImpl.prototype.signTransactionWithExtendedSecretKey = function (transaction, extendedSecretKey) {
|
|
413
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
414
|
-
var encodedExtendedSecretKey, bip32, decodedPSBT;
|
|
415
|
-
return __generator(this, function (_a) {
|
|
416
|
-
encodedExtendedSecretKey = (0, key_1.convertExtendedSecretKey)(extendedSecretKey, 'encoded');
|
|
417
|
-
bip32 = this.bitcoinJS.lib.bip32.fromBase58(encodedExtendedSecretKey.value);
|
|
418
|
-
decodedPSBT = this.bitcoinJS.lib.Psbt.fromHex(transaction.psbt);
|
|
419
|
-
decodedPSBT.data.inputs.forEach(function (input, index) {
|
|
420
|
-
var _a;
|
|
421
|
-
(_a = input.bip32Derivation) === null || _a === void 0 ? void 0 : _a.forEach(function (deriv) {
|
|
422
|
-
try {
|
|
423
|
-
// This uses the same logic to find child key as the "findWalletByFingerprintDerivationPathAndProtocolIdentifier" method in the Vault
|
|
424
|
-
var cutoffFrom = deriv.path.lastIndexOf("'") || deriv.path.lastIndexOf('h');
|
|
425
|
-
var childPath = deriv.path.substr(cutoffFrom + 2);
|
|
426
|
-
decodedPSBT.signInput(index, bip32.derivePath(childPath));
|
|
427
|
-
console.log("Signed input ".concat(index, " with path ").concat(deriv.path));
|
|
428
|
-
}
|
|
429
|
-
catch (e) {
|
|
430
|
-
console.log("Error signing input ".concat(index), e);
|
|
431
|
-
}
|
|
432
|
-
});
|
|
433
|
-
});
|
|
434
|
-
return [2 /*return*/, (0, module_kit_1.newSignedTransaction)({ psbt: decodedPSBT.toHex() })];
|
|
435
|
-
});
|
|
436
|
-
});
|
|
437
|
-
};
|
|
438
|
-
BitcoinSegwitProtocolImpl.prototype.signMessageWithKeyPair = function (message, keyPair) {
|
|
439
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
440
|
-
return __generator(this, function (_a) {
|
|
441
|
-
return [2 /*return*/, this.legacy.signMessageWithKeyPair(message, keyPair)];
|
|
442
|
-
});
|
|
443
|
-
});
|
|
444
|
-
};
|
|
445
|
-
BitcoinSegwitProtocolImpl.prototype.decryptAsymmetricWithKeyPair = function (payload, keyPair) {
|
|
446
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
447
|
-
return __generator(this, function (_a) {
|
|
448
|
-
return [2 /*return*/, this.legacy.decryptAsymmetricWithKeyPair(payload, keyPair)];
|
|
449
|
-
});
|
|
450
|
-
});
|
|
451
|
-
};
|
|
452
|
-
BitcoinSegwitProtocolImpl.prototype.encryptAESWithSecretKey = function (payload, secretKey) {
|
|
453
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
454
|
-
return __generator(this, function (_a) {
|
|
455
|
-
return [2 /*return*/, this.legacy.encryptAESWithSecretKey(payload, secretKey)];
|
|
456
|
-
});
|
|
457
|
-
});
|
|
458
|
-
};
|
|
459
|
-
BitcoinSegwitProtocolImpl.prototype.decryptAESWithSecretKey = function (payload, secretKey) {
|
|
460
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
461
|
-
return __generator(this, function (_a) {
|
|
462
|
-
return [2 /*return*/, this.legacy.decryptAESWithSecretKey(payload, secretKey)];
|
|
463
|
-
});
|
|
464
|
-
});
|
|
465
|
-
};
|
|
466
|
-
// Online
|
|
467
|
-
BitcoinSegwitProtocolImpl.prototype.getNetwork = function () {
|
|
468
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
469
|
-
return __generator(this, function (_a) {
|
|
470
|
-
return [2 /*return*/, this.options.network];
|
|
471
|
-
});
|
|
472
|
-
});
|
|
473
|
-
};
|
|
474
|
-
BitcoinSegwitProtocolImpl.prototype.getTransactionsForPublicKey = function (publicKey, limit, cursor) {
|
|
475
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
476
|
-
return __generator(this, function (_a) {
|
|
477
|
-
return [2 /*return*/, this.legacy.getTransactionsForPublicKey(publicKey, limit, cursor)];
|
|
478
|
-
});
|
|
479
|
-
});
|
|
480
|
-
};
|
|
481
|
-
BitcoinSegwitProtocolImpl.prototype.getTransactionsForAddress = function (address, limit, cursor) {
|
|
482
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
483
|
-
return __generator(this, function (_a) {
|
|
484
|
-
return [2 /*return*/, this.legacy.getTransactionsForAddress(address, limit, cursor)];
|
|
485
|
-
});
|
|
486
|
-
});
|
|
487
|
-
};
|
|
488
|
-
BitcoinSegwitProtocolImpl.prototype.getTransactionsForAddresses = function (addresses, limit, cursor) {
|
|
489
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
490
|
-
return __generator(this, function (_a) {
|
|
491
|
-
return [2 /*return*/, this.legacy.getTransactionsForAddresses(addresses, limit, cursor)];
|
|
492
|
-
});
|
|
493
|
-
});
|
|
494
|
-
};
|
|
495
|
-
BitcoinSegwitProtocolImpl.prototype.getBalanceOfPublicKey = function (publicKey) {
|
|
496
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
497
|
-
return __generator(this, function (_a) {
|
|
498
|
-
return [2 /*return*/, this.legacy.getBalanceOfPublicKey(publicKey)];
|
|
499
|
-
});
|
|
500
|
-
});
|
|
501
|
-
};
|
|
502
|
-
BitcoinSegwitProtocolImpl.prototype.getBalanceOfAddress = function (address) {
|
|
503
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
504
|
-
return __generator(this, function (_a) {
|
|
505
|
-
return [2 /*return*/, this.legacy.getBalanceOfAddress(address)];
|
|
506
|
-
});
|
|
507
|
-
});
|
|
508
|
-
};
|
|
509
|
-
BitcoinSegwitProtocolImpl.prototype.getBalanceOfAddresses = function (addresses) {
|
|
510
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
511
|
-
return __generator(this, function (_a) {
|
|
512
|
-
return [2 /*return*/, this.legacy.getBalanceOfAddresses(addresses)];
|
|
513
|
-
});
|
|
514
|
-
});
|
|
515
|
-
};
|
|
516
|
-
BitcoinSegwitProtocolImpl.prototype.getTransactionMaxAmountWithPublicKey = function (publicKey, to, configuration) {
|
|
517
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
518
|
-
return __generator(this, function (_a) {
|
|
519
|
-
return [2 /*return*/, this.legacy.getTransactionMaxAmountWithPublicKey(publicKey, to, configuration)];
|
|
520
|
-
});
|
|
521
|
-
});
|
|
522
|
-
};
|
|
523
|
-
BitcoinSegwitProtocolImpl.prototype.getTransactionFeeWithPublicKey = function (publicKey, details) {
|
|
524
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
525
|
-
return __generator(this, function (_a) {
|
|
526
|
-
return [2 /*return*/, this.getTransactionFeeWithPublicKey(publicKey, details)];
|
|
527
|
-
});
|
|
528
|
-
});
|
|
529
|
-
};
|
|
530
|
-
BitcoinSegwitProtocolImpl.prototype.prepareTransactionWithPublicKey = function (publicKey, details, configuration) {
|
|
531
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
532
|
-
return __generator(this, function (_a) {
|
|
533
|
-
switch (publicKey.type) {
|
|
534
|
-
case 'pub':
|
|
535
|
-
return [2 /*return*/, this.prepareTransactionWithNonExtendedPublicKey(publicKey, details, configuration)];
|
|
536
|
-
case 'xpub':
|
|
537
|
-
return [2 /*return*/, this.prepareTransactionWithExtendedPublicKey(publicKey, details, configuration)];
|
|
538
|
-
default:
|
|
539
|
-
(0, coinlib_core_1.assertNever)(publicKey);
|
|
540
|
-
throw new errors_1.UnsupportedError(coinlib_core_1.Domain.BITCOIN, 'Unuspported public key type.');
|
|
541
|
-
}
|
|
542
|
-
return [2 /*return*/];
|
|
543
|
-
});
|
|
544
|
-
});
|
|
545
|
-
};
|
|
546
|
-
BitcoinSegwitProtocolImpl.prototype.prepareTransactionWithNonExtendedPublicKey = function (publicKey, details, configuration) {
|
|
547
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
548
|
-
return __generator(this, function (_a) {
|
|
549
|
-
// No reference implementation in v0
|
|
550
|
-
throw new errors_1.UnsupportedError(coinlib_core_1.Domain.BITCOIN, 'Prepare transaction with non extended public key not supported (Segwit).');
|
|
551
|
-
});
|
|
552
|
-
});
|
|
553
|
-
};
|
|
554
|
-
BitcoinSegwitProtocolImpl.prototype.prepareTransactionWithExtendedPublicKey = function (extendedPublicKey, details, configuration) {
|
|
555
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
556
|
-
var fee, estimatedFee, wrappedFee, transaction, utxos, totalRequiredBalance, valueAccumulator, getPathIndexes, _i, utxos_1, utxo, indexes, derivedPublicKey, derivedAddress, i, value, lastUsedInternalAddress, changeValue, changeAddressIndex, derivedPublicKey, derivedAddress, psbt, xpubExtendedPublicKey, keyPair, replaceByFee;
|
|
557
|
-
var _this = this;
|
|
558
|
-
return __generator(this, function (_a) {
|
|
559
|
-
switch (_a.label) {
|
|
560
|
-
case 0:
|
|
561
|
-
if ((configuration === null || configuration === void 0 ? void 0 : configuration.masterFingerprint) === undefined) {
|
|
562
|
-
throw new errors_1.ConditionViolationError(coinlib_core_1.Domain.BITCOIN, 'Master fingerprint not set.');
|
|
563
|
-
}
|
|
564
|
-
if (!((configuration === null || configuration === void 0 ? void 0 : configuration.fee) !== undefined)) return [3 /*break*/, 1];
|
|
565
|
-
fee = configuration.fee;
|
|
566
|
-
return [3 /*break*/, 3];
|
|
567
|
-
case 1: return [4 /*yield*/, this.getTransactionFeeWithPublicKey(extendedPublicKey, details)];
|
|
568
|
-
case 2:
|
|
569
|
-
estimatedFee = _a.sent();
|
|
570
|
-
fee = estimatedFee.medium;
|
|
571
|
-
_a.label = 3;
|
|
572
|
-
case 3:
|
|
573
|
-
wrappedFee = new bignumber_1.default((0, module_kit_1.newAmount)(fee).blockchain(this.legacy.units).value);
|
|
574
|
-
transaction = (0, module_kit_1.newUnsignedTransaction)({
|
|
575
|
-
ins: [],
|
|
576
|
-
outs: []
|
|
577
|
-
});
|
|
578
|
-
return [4 /*yield*/, index_1.default.get("".concat(this.options.network.indexerApi, "/api/v2/utxo/").concat(extendedPublicKey.value, "?confirmed=true"), {
|
|
579
|
-
responseType: 'json'
|
|
580
|
-
})];
|
|
581
|
-
case 4:
|
|
582
|
-
utxos = (_a.sent()).data;
|
|
583
|
-
if (utxos.length <= 0) {
|
|
584
|
-
throw new errors_1.BalanceError(coinlib_core_1.Domain.BITCOIN, 'Not enough balance.'); // no transactions found on those addresses, probably won't find anything in the next ones
|
|
585
|
-
}
|
|
586
|
-
totalRequiredBalance = details
|
|
587
|
-
.map(function (_a) {
|
|
588
|
-
var amount = _a.amount;
|
|
589
|
-
return new bignumber_1.default((0, module_kit_1.newAmount)(amount).blockchain(_this.legacy.units).value);
|
|
590
|
-
})
|
|
591
|
-
.reduce(function (accumulator, currentValue) { return accumulator.plus(currentValue); })
|
|
592
|
-
.plus(wrappedFee);
|
|
593
|
-
valueAccumulator = new bignumber_1.default(0);
|
|
594
|
-
getPathIndexes = function (path) {
|
|
595
|
-
var result = path
|
|
596
|
-
.split('/')
|
|
597
|
-
.slice(-2)
|
|
598
|
-
.map(function (item) { return parseInt(item); })
|
|
599
|
-
.filter(function (item) { return !isNaN(item); });
|
|
600
|
-
if (result.length !== 2) {
|
|
601
|
-
throw new Error('Unexpected path format');
|
|
602
|
-
}
|
|
603
|
-
return [result[0], result[1]];
|
|
604
|
-
};
|
|
605
|
-
_i = 0, utxos_1 = utxos;
|
|
606
|
-
_a.label = 5;
|
|
607
|
-
case 5:
|
|
608
|
-
if (!(_i < utxos_1.length)) return [3 /*break*/, 9];
|
|
609
|
-
utxo = utxos_1[_i];
|
|
610
|
-
valueAccumulator = valueAccumulator.plus(utxo.value);
|
|
611
|
-
indexes = getPathIndexes(utxo.path);
|
|
612
|
-
return [4 /*yield*/, this.deriveFromExtendedPublicKey(extendedPublicKey, indexes[0], indexes[1])];
|
|
613
|
-
case 6:
|
|
614
|
-
derivedPublicKey = _a.sent();
|
|
615
|
-
return [4 /*yield*/, this.getAddressFromPublicKey(derivedPublicKey)];
|
|
616
|
-
case 7:
|
|
617
|
-
derivedAddress = _a.sent();
|
|
618
|
-
if (derivedAddress === utxo.address) {
|
|
619
|
-
transaction.ins.push({
|
|
620
|
-
txId: utxo.txid,
|
|
621
|
-
value: new bignumber_1.default(utxo.value).toString(10),
|
|
622
|
-
vout: utxo.vout,
|
|
623
|
-
address: utxo.address,
|
|
624
|
-
derivationPath: utxo.path
|
|
625
|
-
});
|
|
626
|
-
}
|
|
627
|
-
else {
|
|
628
|
-
throw new Error('Invalid address returned from API');
|
|
629
|
-
}
|
|
630
|
-
if (valueAccumulator.isGreaterThanOrEqualTo(totalRequiredBalance)) {
|
|
631
|
-
return [3 /*break*/, 9];
|
|
632
|
-
}
|
|
633
|
-
_a.label = 8;
|
|
634
|
-
case 8:
|
|
635
|
-
_i++;
|
|
636
|
-
return [3 /*break*/, 5];
|
|
637
|
-
case 9:
|
|
638
|
-
if (valueAccumulator.isLessThan(totalRequiredBalance)) {
|
|
639
|
-
throw new Error('not enough balance 2');
|
|
640
|
-
}
|
|
641
|
-
for (i = 0; i < details.length; i++) {
|
|
642
|
-
value = (0, module_kit_1.newAmount)(details[i].amount).blockchain(this.legacy.units).value;
|
|
643
|
-
transaction.outs.push({
|
|
644
|
-
recipient: details[i].to,
|
|
645
|
-
isChange: false,
|
|
646
|
-
value: value
|
|
647
|
-
});
|
|
648
|
-
valueAccumulator = valueAccumulator.minus(value);
|
|
649
|
-
}
|
|
650
|
-
lastUsedInternalAddress = Math.max.apply(Math, __spreadArray([-1], utxos
|
|
651
|
-
.map(function (utxo) { return getPathIndexes(utxo.path); })
|
|
652
|
-
.filter(function (indexes) { return indexes[0] === 1; })
|
|
653
|
-
.map(function (indexes) { return indexes[1]; }), false));
|
|
654
|
-
changeValue = valueAccumulator.minus(wrappedFee);
|
|
655
|
-
if (!changeValue.isGreaterThan(new bignumber_1.default(DUST_AMOUNT))) return [3 /*break*/, 12];
|
|
656
|
-
changeAddressIndex = lastUsedInternalAddress + 1;
|
|
657
|
-
return [4 /*yield*/, this.deriveFromExtendedPublicKey(extendedPublicKey, 1, changeAddressIndex)];
|
|
658
|
-
case 10:
|
|
659
|
-
derivedPublicKey = _a.sent();
|
|
660
|
-
return [4 /*yield*/, this.getAddressFromPublicKey(derivedPublicKey)];
|
|
661
|
-
case 11:
|
|
662
|
-
derivedAddress = _a.sent();
|
|
663
|
-
transaction.outs.push({
|
|
664
|
-
recipient: derivedAddress,
|
|
665
|
-
isChange: true,
|
|
666
|
-
value: changeValue.toString(10),
|
|
667
|
-
derivationPath: "1/".concat(changeAddressIndex)
|
|
668
|
-
});
|
|
669
|
-
_a.label = 12;
|
|
670
|
-
case 12:
|
|
671
|
-
psbt = new this.bitcoinJS.lib.Psbt();
|
|
672
|
-
// We add the total amount of the transaction to the global map. This can be used to show the info in the "from-to" component after the transaction was signed.
|
|
673
|
-
psbt.addUnknownKeyValToGlobal({
|
|
674
|
-
key: Buffer.from('amount'),
|
|
675
|
-
value: Buffer.from(details
|
|
676
|
-
.reduce(function (accumulator, next) {
|
|
677
|
-
return accumulator.plus((0, module_kit_1.newAmount)(next.amount).blockchain(_this.legacy.units).value);
|
|
678
|
-
}, new bignumber_1.default(0))
|
|
679
|
-
.toString())
|
|
680
|
-
});
|
|
681
|
-
xpubExtendedPublicKey = (0, key_1.convertExtendedPublicKey)(extendedPublicKey, { format: 'encoded', type: 'xpub' });
|
|
682
|
-
keyPair = this.bitcoinJS.lib.bip32.fromBase58(xpubExtendedPublicKey.value);
|
|
683
|
-
replaceByFee = (configuration === null || configuration === void 0 ? void 0 : configuration.replaceByFee) ? true : false;
|
|
684
|
-
transaction.ins.forEach(function (tx) {
|
|
685
|
-
var indexes = getPathIndexes(tx.derivationPath);
|
|
686
|
-
var childNode = keyPair.derivePath(indexes.join('/'));
|
|
687
|
-
var p2wpkh = _this.bitcoinJS.lib.payments.p2wpkh({
|
|
688
|
-
pubkey: childNode.publicKey,
|
|
689
|
-
network: _this.bitcoinJS.config.network
|
|
690
|
-
});
|
|
691
|
-
var p2shOutput = p2wpkh.output;
|
|
692
|
-
if (!p2shOutput) {
|
|
693
|
-
throw new Error('no p2shOutput');
|
|
694
|
-
}
|
|
695
|
-
psbt.addInput({
|
|
696
|
-
hash: tx.txId,
|
|
697
|
-
index: tx.vout,
|
|
698
|
-
sequence: replaceByFee ? 0xfffffffd : undefined,
|
|
699
|
-
witnessUtxo: {
|
|
700
|
-
script: p2shOutput,
|
|
701
|
-
value: parseInt(tx.value, 10)
|
|
702
|
-
},
|
|
703
|
-
bip32Derivation: [
|
|
704
|
-
{
|
|
705
|
-
masterFingerprint: Buffer.from(configuration.masterFingerprint.value, 'hex'),
|
|
706
|
-
pubkey: childNode.publicKey,
|
|
707
|
-
path: tx.derivationPath
|
|
708
|
-
}
|
|
709
|
-
]
|
|
710
|
-
});
|
|
711
|
-
});
|
|
712
|
-
transaction.outs.forEach(function (out, index) {
|
|
713
|
-
psbt.addOutput({ address: out.recipient, value: parseInt(out.value, 10) });
|
|
714
|
-
if (out.derivationPath) {
|
|
715
|
-
// We add the derivation path of our change address to the key value map of the PSBT. This will allow us to later "filter" out this address when displaying the transaction info.
|
|
716
|
-
psbt.addUnknownKeyValToOutput(index, {
|
|
717
|
-
key: Buffer.from('dp'),
|
|
718
|
-
value: Buffer.from(out.derivationPath, 'utf8')
|
|
719
|
-
});
|
|
720
|
-
}
|
|
721
|
-
});
|
|
722
|
-
return [2 /*return*/, (0, module_kit_1.newUnsignedTransaction)({ psbt: psbt.toHex() })];
|
|
723
|
-
}
|
|
724
|
-
});
|
|
725
|
-
});
|
|
726
|
-
};
|
|
727
|
-
BitcoinSegwitProtocolImpl.prototype.broadcastTransaction = function (transaction) {
|
|
728
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
729
|
-
var hexTransaction, data;
|
|
730
|
-
return __generator(this, function (_a) {
|
|
731
|
-
switch (_a.label) {
|
|
732
|
-
case 0:
|
|
733
|
-
hexTransaction = this.bitcoinJS.lib.Psbt.fromHex(transaction.psbt).finalizeAllInputs().extractTransaction().toHex();
|
|
734
|
-
return [4 /*yield*/, index_1.default.post("".concat(this.options.network.indexerApi, "/api/v2/sendtx/"), hexTransaction)];
|
|
735
|
-
case 1:
|
|
736
|
-
data = (_a.sent()).data;
|
|
737
|
-
return [2 /*return*/, data.result];
|
|
738
|
-
}
|
|
739
|
-
});
|
|
740
|
-
});
|
|
741
|
-
};
|
|
742
|
-
return BitcoinSegwitProtocolImpl;
|
|
743
|
-
}());
|
|
744
|
-
exports.BitcoinSegwitProtocolImpl = BitcoinSegwitProtocolImpl;
|
|
745
|
-
// Factory
|
|
746
|
-
function createBitcoinSegwitProtocol(options) {
|
|
747
|
-
if (options === void 0) { options = {}; }
|
|
748
|
-
return new BitcoinSegwitProtocolImpl(options);
|
|
749
|
-
}
|
|
750
|
-
exports.createBitcoinSegwitProtocol = createBitcoinSegwitProtocol;
|
|
751
|
-
//# sourceMappingURL=BitcoinSegwitProtocol.js.map
|