@airgap/bitcoin 0.13.11-beta.1 → 0.13.11-beta.2

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