@airgap/bitcoin 0.13.11-beta.1 → 0.13.11-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.
Files changed (148) hide show
  1. package/index.d.ts +1 -0
  2. package/index.js +18 -0
  3. package/index.js.map +1 -0
  4. package/package.json +1 -5
  5. package/v0/index.d.ts +14 -0
  6. package/v0/index.js +34 -0
  7. package/v0/index.js.map +1 -0
  8. package/v0/protocol/BitcoinAddress.d.ts +9 -0
  9. package/v0/protocol/BitcoinAddress.js +23 -0
  10. package/v0/protocol/BitcoinAddress.js.map +1 -0
  11. package/v0/protocol/BitcoinCryptoClient.d.ts +11 -0
  12. package/v0/protocol/BitcoinCryptoClient.js +91 -0
  13. package/v0/protocol/BitcoinCryptoClient.js.map +1 -0
  14. package/v0/protocol/BitcoinProtocol.d.ts +167 -0
  15. package/v0/protocol/BitcoinProtocol.js +1063 -0
  16. package/v0/protocol/BitcoinProtocol.js.map +1 -0
  17. package/v0/protocol/BitcoinProtocolOptions.d.ts +26 -0
  18. package/v0/protocol/BitcoinProtocolOptions.js +148 -0
  19. package/v0/protocol/BitcoinProtocolOptions.js.map +1 -0
  20. package/v0/protocol/BitcoinSegwitAddress.d.ts +7 -0
  21. package/v0/protocol/BitcoinSegwitAddress.js +41 -0
  22. package/v0/protocol/BitcoinSegwitAddress.js.map +1 -0
  23. package/v0/protocol/BitcoinSegwitProtocol.d.ts +28 -0
  24. package/v0/protocol/BitcoinSegwitProtocol.js +541 -0
  25. package/v0/protocol/BitcoinSegwitProtocol.js.map +1 -0
  26. package/v0/protocol/BitcoinTestnetProtocol.d.ts +7 -0
  27. package/v0/protocol/BitcoinTestnetProtocol.js +58 -0
  28. package/v0/protocol/BitcoinTestnetProtocol.js.map +1 -0
  29. package/v0/protocol/BitcoinTypes.d.ts +21 -0
  30. package/v0/protocol/BitcoinTypes.js +3 -0
  31. package/v0/protocol/BitcoinTypes.js.map +1 -0
  32. package/v0/serializer/schemas/v2/transaction-sign-request-bitcoin-segwit.json +0 -0
  33. package/v0/serializer/schemas/v2/transaction-sign-request-bitcoin.json +73 -0
  34. package/v0/serializer/schemas/v2/transaction-sign-response-bitcoin-segwit.json +0 -0
  35. package/v0/serializer/schemas/v2/transaction-sign-response-bitcoin.json +37 -0
  36. package/v0/serializer/schemas/v3/transaction-sign-request-bitcoin-segwit.json +29 -0
  37. package/v0/serializer/schemas/v3/transaction-sign-request-bitcoin.json +73 -0
  38. package/v0/serializer/schemas/v3/transaction-sign-response-bitcoin-segwit.json +19 -0
  39. package/v0/serializer/schemas/v3/transaction-sign-response-bitcoin.json +37 -0
  40. package/v0/serializer/validators/transaction-validator.d.ts +15 -0
  41. package/v0/serializer/validators/transaction-validator.js +81 -0
  42. package/v0/serializer/validators/transaction-validator.js.map +1 -0
  43. package/v0/serializer/validators/validators.d.ts +1 -0
  44. package/v0/serializer/validators/validators.js +151 -0
  45. package/v0/serializer/validators/validators.js.map +1 -0
  46. package/v0/types/signed-transaction-bitcoin-segwit.d.ts +5 -0
  47. package/v0/types/signed-transaction-bitcoin-segwit.js +3 -0
  48. package/v0/types/signed-transaction-bitcoin-segwit.js.map +1 -0
  49. package/v0/types/signed-transaction-bitcoin.d.ts +9 -0
  50. package/v0/types/signed-transaction-bitcoin.js +3 -0
  51. package/v0/types/signed-transaction-bitcoin.js.map +1 -0
  52. package/v0/types/transaction-bitcoin.d.ts +20 -0
  53. package/v0/types/transaction-bitcoin.js +3 -0
  54. package/v0/types/transaction-bitcoin.js.map +1 -0
  55. package/v0/types/unsigned-transaction-bitcoin-segwit.d.ts +7 -0
  56. package/v0/types/unsigned-transaction-bitcoin-segwit.js +3 -0
  57. package/v0/types/unsigned-transaction-bitcoin-segwit.js.map +1 -0
  58. package/v0/types/unsigned-transaction-bitcoin.d.ts +22 -0
  59. package/v0/types/unsigned-transaction-bitcoin.js +3 -0
  60. package/v0/types/unsigned-transaction-bitcoin.js.map +1 -0
  61. package/v1/block-explorer/BlockCypherBlockExplorer.d.ts +9 -0
  62. package/v1/block-explorer/BlockCypherBlockExplorer.js +74 -0
  63. package/v1/block-explorer/BlockCypherBlockExplorer.js.map +1 -0
  64. package/v1/data/BitcoinAddress.d.ts +7 -0
  65. package/v1/data/BitcoinAddress.js +20 -0
  66. package/v1/data/BitcoinAddress.js.map +1 -0
  67. package/v1/data/BitcoinSegwitAddress.d.ts +8 -0
  68. package/v1/data/BitcoinSegwitAddress.js +25 -0
  69. package/v1/data/BitcoinSegwitAddress.js.map +1 -0
  70. package/v1/index.d.ts +20 -0
  71. package/v1/index.js +24 -0
  72. package/v1/index.js.map +1 -0
  73. package/v1/module/BitcoinModule.d.ts +16 -0
  74. package/v1/module/BitcoinModule.js +112 -0
  75. package/v1/module/BitcoinModule.js.map +1 -0
  76. package/v1/module.d.ts +3 -0
  77. package/v1/module.js +24 -0
  78. package/v1/module.js.map +1 -0
  79. package/v1/protocol/BitcoinCryptoClient.d.ts +12 -0
  80. package/v1/protocol/BitcoinCryptoClient.js +96 -0
  81. package/v1/protocol/BitcoinCryptoClient.js.map +1 -0
  82. package/v1/protocol/BitcoinProtocol.d.ts +72 -0
  83. package/v1/protocol/BitcoinProtocol.js +1193 -0
  84. package/v1/protocol/BitcoinProtocol.js.map +1 -0
  85. package/v1/protocol/BitcoinSegwitProtocol.d.ts +52 -0
  86. package/v1/protocol/BitcoinSegwitProtocol.js +720 -0
  87. package/v1/protocol/BitcoinSegwitProtocol.js.map +1 -0
  88. package/v1/protocol/BitcoinTestnetProtocol.d.ts +12 -0
  89. package/v1/protocol/BitcoinTestnetProtocol.js +73 -0
  90. package/v1/protocol/BitcoinTestnetProtocol.js.map +1 -0
  91. package/v1/serializer/v3/schemas/converter/transaction-converter.d.ts +13 -0
  92. package/v1/serializer/v3/schemas/converter/transaction-converter.js +75 -0
  93. package/v1/serializer/v3/schemas/converter/transaction-converter.js.map +1 -0
  94. package/v1/serializer/v3/schemas/definitions/transaction-sign-request-bitcoin-segwit.d.ts +4 -0
  95. package/v1/serializer/v3/schemas/definitions/transaction-sign-request-bitcoin-segwit.js +3 -0
  96. package/v1/serializer/v3/schemas/definitions/transaction-sign-request-bitcoin-segwit.js.map +1 -0
  97. package/v1/serializer/v3/schemas/definitions/transaction-sign-request-bitcoin.d.ts +4 -0
  98. package/v1/serializer/v3/schemas/definitions/transaction-sign-request-bitcoin.js +3 -0
  99. package/v1/serializer/v3/schemas/definitions/transaction-sign-request-bitcoin.js.map +1 -0
  100. package/v1/serializer/v3/schemas/definitions/transaction-sign-response-bitcoin-segwit.d.ts +3 -0
  101. package/v1/serializer/v3/schemas/definitions/transaction-sign-response-bitcoin-segwit.js +3 -0
  102. package/v1/serializer/v3/schemas/definitions/transaction-sign-response-bitcoin-segwit.js.map +1 -0
  103. package/v1/serializer/v3/schemas/definitions/transaction-sign-response-bitcoin.d.ts +7 -0
  104. package/v1/serializer/v3/schemas/definitions/transaction-sign-response-bitcoin.js +3 -0
  105. package/v1/serializer/v3/schemas/definitions/transaction-sign-response-bitcoin.js.map +1 -0
  106. package/v1/serializer/v3/schemas/generated/transaction-sign-request-bitcoin-segwit.json +29 -0
  107. package/v1/serializer/v3/schemas/generated/transaction-sign-request-bitcoin.json +79 -0
  108. package/v1/serializer/v3/schemas/generated/transaction-sign-response-bitcoin-segwit.json +19 -0
  109. package/v1/serializer/v3/schemas/generated/transaction-sign-response-bitcoin.json +37 -0
  110. package/v1/serializer/v3/serializer-companion.d.ts +14 -0
  111. package/v1/serializer/v3/serializer-companion.js +197 -0
  112. package/v1/serializer/v3/serializer-companion.js.map +1 -0
  113. package/v1/serializer/v3/validators/transaction-validator.d.ts +7 -0
  114. package/v1/serializer/v3/validators/transaction-validator.js +56 -0
  115. package/v1/serializer/v3/validators/transaction-validator.js.map +1 -0
  116. package/v1/serializer/v3/validators/validators.d.ts +7 -0
  117. package/v1/serializer/v3/validators/validators.js +238 -0
  118. package/v1/serializer/v3/validators/validators.js.map +1 -0
  119. package/v1/types/bitcoinjs.d.ts +14 -0
  120. package/v1/types/bitcoinjs.js +3 -0
  121. package/v1/types/bitcoinjs.js.map +1 -0
  122. package/v1/types/crypto.d.ts +2 -0
  123. package/v1/types/crypto.js +3 -0
  124. package/v1/types/crypto.js.map +1 -0
  125. package/v1/types/indexer.d.ts +81 -0
  126. package/v1/types/indexer.js +3 -0
  127. package/v1/types/indexer.js.map +1 -0
  128. package/v1/types/key.d.ts +6 -0
  129. package/v1/types/key.js +3 -0
  130. package/v1/types/key.js.map +1 -0
  131. package/v1/types/protocol.d.ts +17 -0
  132. package/v1/types/protocol.js +3 -0
  133. package/v1/types/protocol.js.map +1 -0
  134. package/v1/types/transaction.d.ts +39 -0
  135. package/v1/types/transaction.js +3 -0
  136. package/v1/types/transaction.js.map +1 -0
  137. package/v1/utils/common.d.ts +2 -0
  138. package/v1/utils/common.js +28 -0
  139. package/v1/utils/common.js.map +1 -0
  140. package/v1/utils/key.d.ts +34 -0
  141. package/v1/utils/key.js +190 -0
  142. package/v1/utils/key.js.map +1 -0
  143. package/v1/utils/network.d.ts +2 -0
  144. package/v1/utils/network.js +22 -0
  145. package/v1/utils/network.js.map +1 -0
  146. package/v1/utils/signature.d.ts +2 -0
  147. package/v1/utils/signature.js +34 -0
  148. package/v1/utils/signature.js.map +1 -0
@@ -0,0 +1,720 @@
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 errors_1 = require("@airgap/coinlib-core/errors");
90
+ var crypto_1 = require("@airgap/crypto");
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.getCryptoConfiguration = function () {
288
+ return __awaiter(this, void 0, void 0, function () {
289
+ return __generator(this, function (_a) {
290
+ return [2 /*return*/, this.legacy.getCryptoConfiguration()];
291
+ });
292
+ });
293
+ };
294
+ BitcoinSegwitProtocolImpl.prototype.getKeyPairFromDerivative = function (derivative) {
295
+ return __awaiter(this, void 0, void 0, function () {
296
+ var bip32, privateKey, publicKey;
297
+ return __generator(this, function (_a) {
298
+ bip32 = this.derivativeToBip32Node(derivative);
299
+ privateKey = bip32.privateKey;
300
+ if (privateKey === undefined) {
301
+ throw new Error('No private key!');
302
+ }
303
+ publicKey = bip32.publicKey;
304
+ return [2 /*return*/, {
305
+ secretKey: (0, module_kit_1.newSecretKey)(privateKey.toString('hex'), 'hex'),
306
+ publicKey: (0, module_kit_1.newPublicKey)(publicKey.toString('hex'), 'hex')
307
+ }];
308
+ });
309
+ });
310
+ };
311
+ BitcoinSegwitProtocolImpl.prototype.getExtendedKeyPairFromDerivative = function (derivative) {
312
+ return __awaiter(this, void 0, void 0, function () {
313
+ var bip32;
314
+ return __generator(this, function (_a) {
315
+ bip32 = this.derivativeToBip32Node(derivative);
316
+ return [2 /*return*/, {
317
+ secretKey: (0, module_kit_1.newExtendedSecretKey)(bip32.toBase58(), 'encoded'),
318
+ publicKey: (0, key_1.convertExtendedPublicKey)((0, module_kit_1.newExtendedPublicKey)(bip32.neutered().toBase58(), 'encoded'), {
319
+ format: 'encoded',
320
+ type: 'zpub'
321
+ })
322
+ }];
323
+ });
324
+ });
325
+ };
326
+ BitcoinSegwitProtocolImpl.prototype.deriveFromExtendedSecretKey = function (extendedSecretKey, visibilityIndex, addressIndex) {
327
+ return __awaiter(this, void 0, void 0, function () {
328
+ var encodedSecretKey, derivedBip32, privateKey;
329
+ return __generator(this, function (_a) {
330
+ encodedSecretKey = (0, key_1.convertExtendedSecretKey)(extendedSecretKey, { format: 'encoded', type: 'xprv' });
331
+ derivedBip32 = this.bitcoinJS.lib.bip32
332
+ .fromBase58(encodedSecretKey.value, this.bitcoinJS.config.network)
333
+ .derive(visibilityIndex)
334
+ .derive(addressIndex);
335
+ privateKey = derivedBip32.privateKey;
336
+ if (privateKey === undefined) {
337
+ throw new Error('No private key!');
338
+ }
339
+ return [2 /*return*/, (0, module_kit_1.newSecretKey)(privateKey.toString('hex'), 'hex')];
340
+ });
341
+ });
342
+ };
343
+ BitcoinSegwitProtocolImpl.prototype.signTransactionWithSecretKey = function (transaction, secretKey) {
344
+ return __awaiter(this, void 0, void 0, function () {
345
+ return __generator(this, function (_a) {
346
+ switch (secretKey.type) {
347
+ case 'priv':
348
+ return [2 /*return*/, this.signTransactionWithNonExtendedSecretKey(transaction, secretKey)];
349
+ case 'xpriv':
350
+ return [2 /*return*/, this.signTransactionWithExtendedSecretKey(transaction, secretKey)];
351
+ default:
352
+ (0, coinlib_core_1.assertNever)(secretKey);
353
+ throw new errors_1.UnsupportedError(coinlib_core_1.Domain.BITCOIN, 'Secret key type not supported.');
354
+ }
355
+ return [2 /*return*/];
356
+ });
357
+ });
358
+ };
359
+ BitcoinSegwitProtocolImpl.prototype.signTransactionWithNonExtendedSecretKey = function (transaction, secretKey) {
360
+ return __awaiter(this, void 0, void 0, function () {
361
+ return __generator(this, function (_a) {
362
+ // No reference implementation in v0
363
+ throw new errors_1.UnsupportedError(coinlib_core_1.Domain.BITCOIN, 'Sign with non extended secret key not supported (Segwit).');
364
+ });
365
+ });
366
+ };
367
+ BitcoinSegwitProtocolImpl.prototype.signTransactionWithExtendedSecretKey = function (transaction, extendedSecretKey) {
368
+ return __awaiter(this, void 0, void 0, function () {
369
+ var encodedExtendedSecretKey, bip32, decodedPSBT;
370
+ return __generator(this, function (_a) {
371
+ encodedExtendedSecretKey = (0, key_1.convertExtendedSecretKey)(extendedSecretKey, { format: 'encoded', type: 'xprv' });
372
+ bip32 = this.bitcoinJS.lib.bip32.fromBase58(encodedExtendedSecretKey.value);
373
+ decodedPSBT = this.bitcoinJS.lib.Psbt.fromHex(transaction.psbt);
374
+ decodedPSBT.data.inputs.forEach(function (input, index) {
375
+ var _a;
376
+ (_a = input.bip32Derivation) === null || _a === void 0 ? void 0 : _a.forEach(function (deriv) {
377
+ try {
378
+ // This uses the same logic to find child key as the "findWalletByFingerprintDerivationPathAndProtocolIdentifier" method in the Vault
379
+ var cutoffFrom = deriv.path.lastIndexOf("'") || deriv.path.lastIndexOf('h');
380
+ var childPath = deriv.path.substr(cutoffFrom + 2);
381
+ decodedPSBT.signInput(index, bip32.derivePath(childPath));
382
+ console.log("Signed input ".concat(index, " with path ").concat(deriv.path));
383
+ }
384
+ catch (e) {
385
+ console.log("Error signing input ".concat(index), e);
386
+ }
387
+ });
388
+ });
389
+ return [2 /*return*/, (0, module_kit_1.newSignedTransaction)({ psbt: decodedPSBT.toHex() })];
390
+ });
391
+ });
392
+ };
393
+ BitcoinSegwitProtocolImpl.prototype.signMessageWithKeyPair = function (message, keyPair) {
394
+ return __awaiter(this, void 0, void 0, function () {
395
+ return __generator(this, function (_a) {
396
+ return [2 /*return*/, this.legacy.signMessageWithKeyPair(message, keyPair)];
397
+ });
398
+ });
399
+ };
400
+ BitcoinSegwitProtocolImpl.prototype.decryptAsymmetricWithKeyPair = function (payload, keyPair) {
401
+ return __awaiter(this, void 0, void 0, function () {
402
+ return __generator(this, function (_a) {
403
+ return [2 /*return*/, this.legacy.decryptAsymmetricWithKeyPair(payload, keyPair)];
404
+ });
405
+ });
406
+ };
407
+ BitcoinSegwitProtocolImpl.prototype.encryptAESWithSecretKey = function (payload, secretKey) {
408
+ return __awaiter(this, void 0, void 0, function () {
409
+ return __generator(this, function (_a) {
410
+ return [2 /*return*/, this.legacy.encryptAESWithSecretKey(payload, secretKey)];
411
+ });
412
+ });
413
+ };
414
+ BitcoinSegwitProtocolImpl.prototype.decryptAESWithSecretKey = function (payload, secretKey) {
415
+ return __awaiter(this, void 0, void 0, function () {
416
+ return __generator(this, function (_a) {
417
+ return [2 /*return*/, this.legacy.decryptAESWithSecretKey(payload, secretKey)];
418
+ });
419
+ });
420
+ };
421
+ // Online
422
+ BitcoinSegwitProtocolImpl.prototype.getNetwork = function () {
423
+ return __awaiter(this, void 0, void 0, function () {
424
+ return __generator(this, function (_a) {
425
+ return [2 /*return*/, this.options.network];
426
+ });
427
+ });
428
+ };
429
+ BitcoinSegwitProtocolImpl.prototype.getTransactionsForPublicKey = function (publicKey, limit, cursor) {
430
+ return __awaiter(this, void 0, void 0, function () {
431
+ return __generator(this, function (_a) {
432
+ return [2 /*return*/, this.legacy.getTransactionsForPublicKey(publicKey, limit, cursor)];
433
+ });
434
+ });
435
+ };
436
+ BitcoinSegwitProtocolImpl.prototype.getTransactionsForAddress = function (address, limit, cursor) {
437
+ return __awaiter(this, void 0, void 0, function () {
438
+ return __generator(this, function (_a) {
439
+ return [2 /*return*/, this.legacy.getTransactionsForAddress(address, limit, cursor)];
440
+ });
441
+ });
442
+ };
443
+ BitcoinSegwitProtocolImpl.prototype.getTransactionsForAddresses = function (addresses, limit, cursor) {
444
+ return __awaiter(this, void 0, void 0, function () {
445
+ return __generator(this, function (_a) {
446
+ return [2 /*return*/, this.legacy.getTransactionsForAddresses(addresses, limit, cursor)];
447
+ });
448
+ });
449
+ };
450
+ BitcoinSegwitProtocolImpl.prototype.getBalanceOfPublicKey = function (publicKey) {
451
+ return __awaiter(this, void 0, void 0, function () {
452
+ return __generator(this, function (_a) {
453
+ return [2 /*return*/, this.legacy.getBalanceOfPublicKey(publicKey)];
454
+ });
455
+ });
456
+ };
457
+ BitcoinSegwitProtocolImpl.prototype.getBalanceOfAddress = function (address) {
458
+ return __awaiter(this, void 0, void 0, function () {
459
+ return __generator(this, function (_a) {
460
+ return [2 /*return*/, this.legacy.getBalanceOfAddress(address)];
461
+ });
462
+ });
463
+ };
464
+ BitcoinSegwitProtocolImpl.prototype.getBalanceOfAddresses = function (addresses) {
465
+ return __awaiter(this, void 0, void 0, function () {
466
+ return __generator(this, function (_a) {
467
+ return [2 /*return*/, this.legacy.getBalanceOfAddresses(addresses)];
468
+ });
469
+ });
470
+ };
471
+ BitcoinSegwitProtocolImpl.prototype.getTransactionMaxAmountWithPublicKey = function (publicKey, to, configuration) {
472
+ return __awaiter(this, void 0, void 0, function () {
473
+ return __generator(this, function (_a) {
474
+ return [2 /*return*/, this.legacy.getTransactionMaxAmountWithPublicKey(publicKey, to, configuration)];
475
+ });
476
+ });
477
+ };
478
+ BitcoinSegwitProtocolImpl.prototype.getTransactionFeeWithPublicKey = function (publicKey, details) {
479
+ return __awaiter(this, void 0, void 0, function () {
480
+ return __generator(this, function (_a) {
481
+ return [2 /*return*/, this.getTransactionFeeWithPublicKey(publicKey, details)];
482
+ });
483
+ });
484
+ };
485
+ BitcoinSegwitProtocolImpl.prototype.prepareTransactionWithPublicKey = function (publicKey, details, configuration) {
486
+ return __awaiter(this, void 0, void 0, function () {
487
+ return __generator(this, function (_a) {
488
+ switch (publicKey.type) {
489
+ case 'pub':
490
+ return [2 /*return*/, this.prepareTransactionWithNonExtendedPublicKey(publicKey, details, configuration)];
491
+ case 'xpub':
492
+ return [2 /*return*/, this.prepareTransactionWithExtendedPublicKey(publicKey, details, configuration)];
493
+ default:
494
+ (0, coinlib_core_1.assertNever)(publicKey);
495
+ throw new errors_1.UnsupportedError(coinlib_core_1.Domain.BITCOIN, 'Unuspported public key type.');
496
+ }
497
+ return [2 /*return*/];
498
+ });
499
+ });
500
+ };
501
+ BitcoinSegwitProtocolImpl.prototype.prepareTransactionWithNonExtendedPublicKey = function (publicKey, details, configuration) {
502
+ return __awaiter(this, void 0, void 0, function () {
503
+ return __generator(this, function (_a) {
504
+ // No reference implementation in v0
505
+ throw new errors_1.UnsupportedError(coinlib_core_1.Domain.BITCOIN, 'Prepare transaction with non extended public key not supported (Segwit).');
506
+ });
507
+ });
508
+ };
509
+ BitcoinSegwitProtocolImpl.prototype.prepareTransactionWithExtendedPublicKey = function (extendedPublicKey, details, configuration) {
510
+ return __awaiter(this, void 0, void 0, function () {
511
+ 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;
512
+ var _this = this;
513
+ return __generator(this, function (_a) {
514
+ switch (_a.label) {
515
+ case 0:
516
+ if ((configuration === null || configuration === void 0 ? void 0 : configuration.masterFingerprint) === undefined) {
517
+ throw new errors_1.ConditionViolationError(coinlib_core_1.Domain.BITCOIN, 'Master fingerprint not set.');
518
+ }
519
+ if (!((configuration === null || configuration === void 0 ? void 0 : configuration.fee) !== undefined)) return [3 /*break*/, 1];
520
+ fee = configuration.fee;
521
+ return [3 /*break*/, 3];
522
+ case 1: return [4 /*yield*/, this.getTransactionFeeWithPublicKey(extendedPublicKey, details)];
523
+ case 2:
524
+ estimatedFee = _a.sent();
525
+ fee = estimatedFee.medium;
526
+ _a.label = 3;
527
+ case 3:
528
+ wrappedFee = new bignumber_1.default((0, module_kit_1.newAmount)(fee).blockchain(this.legacy.units).value);
529
+ transaction = (0, module_kit_1.newUnsignedTransaction)({
530
+ ins: [],
531
+ outs: []
532
+ });
533
+ return [4 /*yield*/, index_1.default.get("".concat(this.options.network.indexerApi, "/api/v2/utxo/").concat(extendedPublicKey.value, "?confirmed=true"), {
534
+ responseType: 'json'
535
+ })];
536
+ case 4:
537
+ utxos = (_a.sent()).data;
538
+ if (utxos.length <= 0) {
539
+ 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
540
+ }
541
+ totalRequiredBalance = details
542
+ .map(function (_a) {
543
+ var amount = _a.amount;
544
+ return new bignumber_1.default((0, module_kit_1.newAmount)(amount).blockchain(_this.legacy.units).value);
545
+ })
546
+ .reduce(function (accumulator, currentValue) { return accumulator.plus(currentValue); })
547
+ .plus(wrappedFee);
548
+ valueAccumulator = new bignumber_1.default(0);
549
+ getPathIndexes = function (path) {
550
+ var result = path
551
+ .split('/')
552
+ .slice(-2)
553
+ .map(function (item) { return parseInt(item); })
554
+ .filter(function (item) { return !isNaN(item); });
555
+ if (result.length !== 2) {
556
+ throw new Error('Unexpected path format');
557
+ }
558
+ return [result[0], result[1]];
559
+ };
560
+ _i = 0, utxos_1 = utxos;
561
+ _a.label = 5;
562
+ case 5:
563
+ if (!(_i < utxos_1.length)) return [3 /*break*/, 9];
564
+ utxo = utxos_1[_i];
565
+ valueAccumulator = valueAccumulator.plus(utxo.value);
566
+ indexes = getPathIndexes(utxo.path);
567
+ return [4 /*yield*/, this.deriveFromExtendedPublicKey(extendedPublicKey, indexes[0], indexes[1])];
568
+ case 6:
569
+ derivedPublicKey = _a.sent();
570
+ return [4 /*yield*/, this.getAddressFromPublicKey(derivedPublicKey)];
571
+ case 7:
572
+ derivedAddress = _a.sent();
573
+ if (derivedAddress === utxo.address) {
574
+ transaction.ins.push({
575
+ txId: utxo.txid,
576
+ value: new bignumber_1.default(utxo.value).toString(10),
577
+ vout: utxo.vout,
578
+ address: utxo.address,
579
+ derivationPath: utxo.path
580
+ });
581
+ }
582
+ else {
583
+ throw new Error('Invalid address returned from API');
584
+ }
585
+ if (valueAccumulator.isGreaterThanOrEqualTo(totalRequiredBalance)) {
586
+ return [3 /*break*/, 9];
587
+ }
588
+ _a.label = 8;
589
+ case 8:
590
+ _i++;
591
+ return [3 /*break*/, 5];
592
+ case 9:
593
+ if (valueAccumulator.isLessThan(totalRequiredBalance)) {
594
+ throw new Error('not enough balance 2');
595
+ }
596
+ for (i = 0; i < details.length; i++) {
597
+ value = (0, module_kit_1.newAmount)(details[i].amount).blockchain(this.legacy.units).value;
598
+ transaction.outs.push({
599
+ recipient: details[i].to,
600
+ isChange: false,
601
+ value: value
602
+ });
603
+ valueAccumulator = valueAccumulator.minus(value);
604
+ }
605
+ lastUsedInternalAddress = Math.max.apply(Math, __spreadArray([-1], utxos
606
+ .map(function (utxo) { return getPathIndexes(utxo.path); })
607
+ .filter(function (indexes) { return indexes[0] === 1; })
608
+ .map(function (indexes) { return indexes[1]; }), false));
609
+ changeValue = valueAccumulator.minus(wrappedFee);
610
+ if (!changeValue.isGreaterThan(new bignumber_1.default(DUST_AMOUNT))) return [3 /*break*/, 12];
611
+ changeAddressIndex = lastUsedInternalAddress + 1;
612
+ return [4 /*yield*/, this.deriveFromExtendedPublicKey(extendedPublicKey, 1, changeAddressIndex)];
613
+ case 10:
614
+ derivedPublicKey = _a.sent();
615
+ return [4 /*yield*/, this.getAddressFromPublicKey(derivedPublicKey)];
616
+ case 11:
617
+ derivedAddress = _a.sent();
618
+ transaction.outs.push({
619
+ recipient: derivedAddress,
620
+ isChange: true,
621
+ value: changeValue.toString(10),
622
+ derivationPath: "1/".concat(changeAddressIndex)
623
+ });
624
+ _a.label = 12;
625
+ case 12:
626
+ psbt = new this.bitcoinJS.lib.Psbt();
627
+ // 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.
628
+ psbt.addUnknownKeyValToGlobal({
629
+ key: Buffer.from('amount'),
630
+ value: Buffer.from(details
631
+ .reduce(function (accumulator, next) {
632
+ return accumulator.plus((0, module_kit_1.newAmount)(next.amount).blockchain(_this.legacy.units).value);
633
+ }, new bignumber_1.default(0))
634
+ .toString())
635
+ });
636
+ xpubExtendedPublicKey = (0, key_1.convertExtendedPublicKey)(extendedPublicKey, { format: 'encoded', type: 'xpub' });
637
+ keyPair = this.bitcoinJS.lib.bip32.fromBase58(xpubExtendedPublicKey.value);
638
+ replaceByFee = (configuration === null || configuration === void 0 ? void 0 : configuration.replaceByFee) ? true : false;
639
+ transaction.ins.forEach(function (tx) {
640
+ var indexes = getPathIndexes(tx.derivationPath);
641
+ var childNode = keyPair.derivePath(indexes.join('/'));
642
+ var p2wpkh = _this.bitcoinJS.lib.payments.p2wpkh({
643
+ pubkey: childNode.publicKey,
644
+ network: _this.bitcoinJS.config.network
645
+ });
646
+ var p2shOutput = p2wpkh.output;
647
+ if (!p2shOutput) {
648
+ throw new Error('no p2shOutput');
649
+ }
650
+ psbt.addInput({
651
+ hash: tx.txId,
652
+ index: tx.vout,
653
+ sequence: replaceByFee ? 0xfffffffd : undefined,
654
+ witnessUtxo: {
655
+ script: p2shOutput,
656
+ value: parseInt(tx.value, 10)
657
+ },
658
+ bip32Derivation: [
659
+ {
660
+ masterFingerprint: Buffer.from(configuration.masterFingerprint.value, 'hex'),
661
+ pubkey: childNode.publicKey,
662
+ path: tx.derivationPath
663
+ }
664
+ ]
665
+ });
666
+ });
667
+ transaction.outs.forEach(function (out, index) {
668
+ psbt.addOutput({ address: out.recipient, value: parseInt(out.value, 10) });
669
+ if (out.derivationPath) {
670
+ // 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.
671
+ psbt.addUnknownKeyValToOutput(index, {
672
+ key: Buffer.from('dp'),
673
+ value: Buffer.from(out.derivationPath, 'utf8')
674
+ });
675
+ }
676
+ });
677
+ return [2 /*return*/, (0, module_kit_1.newUnsignedTransaction)({ psbt: psbt.toHex() })];
678
+ }
679
+ });
680
+ });
681
+ };
682
+ BitcoinSegwitProtocolImpl.prototype.broadcastTransaction = function (transaction) {
683
+ return __awaiter(this, void 0, void 0, function () {
684
+ var hexTransaction, data;
685
+ return __generator(this, function (_a) {
686
+ switch (_a.label) {
687
+ case 0:
688
+ hexTransaction = this.bitcoinJS.lib.Psbt.fromHex(transaction.psbt).finalizeAllInputs().extractTransaction().toHex();
689
+ return [4 /*yield*/, index_1.default.post("".concat(this.options.network.indexerApi, "/api/v2/sendtx/"), hexTransaction)];
690
+ case 1:
691
+ data = (_a.sent()).data;
692
+ return [2 /*return*/, data.result];
693
+ }
694
+ });
695
+ });
696
+ };
697
+ // Custom
698
+ BitcoinSegwitProtocolImpl.prototype.convertCryptoDerivative = function (derivative) {
699
+ var hexNode = (0, crypto_1.encodeDerivative)('hex', __assign(__assign({}, derivative), { secretKey: "00".concat(derivative.secretKey) }));
700
+ var extendedSecretKey = {
701
+ type: 'xpriv',
702
+ format: 'hex',
703
+ value: hexNode.secretKey
704
+ };
705
+ return (0, key_1.convertExtendedSecretKey)(extendedSecretKey, { format: 'encoded', type: 'xprv' });
706
+ };
707
+ BitcoinSegwitProtocolImpl.prototype.derivativeToBip32Node = function (derivative) {
708
+ var extendedSecretKey = this.convertCryptoDerivative(derivative);
709
+ return this.bitcoinJS.lib.bip32.fromBase58(extendedSecretKey.value, this.bitcoinJS.config.network);
710
+ };
711
+ return BitcoinSegwitProtocolImpl;
712
+ }());
713
+ exports.BitcoinSegwitProtocolImpl = BitcoinSegwitProtocolImpl;
714
+ // Factory
715
+ function createBitcoinSegwitProtocol(options) {
716
+ if (options === void 0) { options = {}; }
717
+ return new BitcoinSegwitProtocolImpl(options);
718
+ }
719
+ exports.createBitcoinSegwitProtocol = createBitcoinSegwitProtocol;
720
+ //# sourceMappingURL=BitcoinSegwitProtocol.js.map