@airgap/bitcoin 0.13.45-beta.1 → 0.13.45-beta.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +5 -5
- package/v0/index.js +10 -10
- package/v0/index.js.map +1 -1
- package/v0/protocol/BitcoinAddress.js +8 -9
- package/v0/protocol/BitcoinAddress.js.map +1 -1
- package/v0/protocol/BitcoinCryptoClient.js +16 -85
- package/v0/protocol/BitcoinCryptoClient.js.map +1 -1
- package/v0/protocol/BitcoinProtocol.js +590 -987
- package/v0/protocol/BitcoinProtocol.js.map +1 -1
- package/v0/protocol/BitcoinProtocolOptions.js +45 -111
- package/v0/protocol/BitcoinProtocolOptions.js.map +1 -1
- package/v0/protocol/BitcoinSegwitAddress.js +12 -29
- package/v0/protocol/BitcoinSegwitAddress.js.map +1 -1
- package/v0/protocol/BitcoinSegwitProtocol.js +348 -483
- package/v0/protocol/BitcoinSegwitProtocol.js.map +1 -1
- package/v0/protocol/BitcoinTestnetProtocol.js +28 -36
- package/v0/protocol/BitcoinTestnetProtocol.js.map +1 -1
- package/v0/serializer/validators/transaction-validator.js +22 -30
- package/v0/serializer/validators/transaction-validator.js.map +1 -1
- package/v0/serializer/validators/validators.js +23 -23
- package/v0/serializer/validators/validators.js.map +1 -1
- package/v1/block-explorer/BlockCypherBlockExplorer.js +12 -61
- package/v1/block-explorer/BlockCypherBlockExplorer.js.map +1 -1
- package/v1/data/BitcoinAddress.js +9 -10
- package/v1/data/BitcoinAddress.js.map +1 -1
- package/v1/data/BitcoinLegacyAddress.js +11 -12
- package/v1/data/BitcoinLegacyAddress.js.map +1 -1
- package/v1/data/BitcoinSegwitAddress.js +11 -12
- package/v1/data/BitcoinSegwitAddress.js.map +1 -1
- package/v1/data/BitcoinTaprootAddress.js +31 -22
- package/v1/data/BitcoinTaprootAddress.js.map +1 -1
- package/v1/index.js +11 -11
- package/v1/index.js.map +1 -1
- package/v1/module/BitcoinModule.d.ts +1 -1
- package/v1/module/BitcoinModule.js +44 -102
- package/v1/module/BitcoinModule.js.map +1 -1
- package/v1/module.js +2 -3
- package/v1/module.js.map +1 -1
- package/v1/protocol/BitcoinCryptoClient.js +22 -90
- package/v1/protocol/BitcoinCryptoClient.js.map +1 -1
- package/v1/protocol/BitcoinLegacyProtocol.js +520 -796
- package/v1/protocol/BitcoinLegacyProtocol.js.map +1 -1
- package/v1/protocol/BitcoinProtocol.js +735 -1169
- package/v1/protocol/BitcoinProtocol.js.map +1 -1
- package/v1/protocol/BitcoinSegwitProtocol.js +542 -796
- package/v1/protocol/BitcoinSegwitProtocol.js.map +1 -1
- package/v1/protocol/BitcoinTaprootProtocol.js +688 -1000
- package/v1/protocol/BitcoinTaprootProtocol.js.map +1 -1
- package/v1/protocol/BitcoinTestnetProtocol.js +14 -33
- package/v1/protocol/BitcoinTestnetProtocol.js.map +1 -1
- package/v1/serializer/v3/schemas/converter/transaction-converter.js +29 -52
- package/v1/serializer/v3/schemas/converter/transaction-converter.js.map +1 -1
- package/v1/serializer/v3/serializer-companion.js +98 -165
- package/v1/serializer/v3/serializer-companion.js.map +1 -1
- package/v1/serializer/v3/validators/transaction-validator.js +13 -16
- package/v1/serializer/v3/validators/transaction-validator.js.map +1 -1
- package/v1/serializer/v3/validators/validators.js +122 -213
- package/v1/serializer/v3/validators/validators.js.map +1 -1
- package/v1/types/crypto.d.ts +1 -1
- package/v1/types/key.d.ts +6 -6
- package/v1/types/protocol.d.ts +2 -2
- package/v1/types/transaction.d.ts +3 -3
- package/v1/utils/common.js +4 -6
- package/v1/utils/common.js.map +1 -1
- package/v1/utils/key.d.ts +5 -6
- package/v1/utils/key.js +38 -39
- package/v1/utils/key.js.map +1 -1
- package/v1/utils/network.js +3 -4
- package/v1/utils/network.js.map +1 -1
- package/v1/utils/protocol.js +25 -19
- package/v1/utils/protocol.js.map +1 -1
- package/v1/utils/signature.js +4 -5
- package/v1/utils/signature.js.map +1 -1
|
@@ -1,19 +1,4 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
var __extends = (this && this.__extends) || (function () {
|
|
3
|
-
var extendStatics = function (d, b) {
|
|
4
|
-
extendStatics = Object.setPrototypeOf ||
|
|
5
|
-
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
|
|
6
|
-
function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
|
|
7
|
-
return extendStatics(d, b);
|
|
8
|
-
};
|
|
9
|
-
return function (d, b) {
|
|
10
|
-
if (typeof b !== "function" && b !== null)
|
|
11
|
-
throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
|
|
12
|
-
extendStatics(d, b);
|
|
13
|
-
function __() { this.constructor = d; }
|
|
14
|
-
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
|
|
15
|
-
};
|
|
16
|
-
})();
|
|
17
2
|
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
18
3
|
if (k2 === undefined) k2 = k;
|
|
19
4
|
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
@@ -30,75 +15,40 @@ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (
|
|
|
30
15
|
}) : function(o, v) {
|
|
31
16
|
o["default"] = v;
|
|
32
17
|
});
|
|
33
|
-
var __importStar = (this && this.__importStar) || function (
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
};
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
return
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
};
|
|
49
|
-
|
|
50
|
-
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
|
|
51
|
-
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
|
|
52
|
-
function verb(n) { return function (v) { return step([n, v]); }; }
|
|
53
|
-
function step(op) {
|
|
54
|
-
if (f) throw new TypeError("Generator is already executing.");
|
|
55
|
-
while (_) try {
|
|
56
|
-
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;
|
|
57
|
-
if (y = 0, t) op = [op[0] & 2, t.value];
|
|
58
|
-
switch (op[0]) {
|
|
59
|
-
case 0: case 1: t = op; break;
|
|
60
|
-
case 4: _.label++; return { value: op[1], done: false };
|
|
61
|
-
case 5: _.label++; y = op[1]; op = [0]; continue;
|
|
62
|
-
case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
|
63
|
-
default:
|
|
64
|
-
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
|
65
|
-
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
|
66
|
-
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
|
67
|
-
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
|
68
|
-
if (t[2]) _.ops.pop();
|
|
69
|
-
_.trys.pop(); continue;
|
|
70
|
-
}
|
|
71
|
-
op = body.call(thisArg, _);
|
|
72
|
-
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
|
73
|
-
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
|
74
|
-
}
|
|
75
|
-
};
|
|
76
|
-
var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
|
|
77
|
-
if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
|
|
78
|
-
if (ar || !(i in from)) {
|
|
79
|
-
if (!ar) ar = Array.prototype.slice.call(from, 0, i);
|
|
80
|
-
ar[i] = from[i];
|
|
81
|
-
}
|
|
82
|
-
}
|
|
83
|
-
return to.concat(ar || Array.prototype.slice.call(from));
|
|
84
|
-
};
|
|
18
|
+
var __importStar = (this && this.__importStar) || (function () {
|
|
19
|
+
var ownKeys = function(o) {
|
|
20
|
+
ownKeys = Object.getOwnPropertyNames || function (o) {
|
|
21
|
+
var ar = [];
|
|
22
|
+
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
|
23
|
+
return ar;
|
|
24
|
+
};
|
|
25
|
+
return ownKeys(o);
|
|
26
|
+
};
|
|
27
|
+
return function (mod) {
|
|
28
|
+
if (mod && mod.__esModule) return mod;
|
|
29
|
+
var result = {};
|
|
30
|
+
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
|
31
|
+
__setModuleDefault(result, mod);
|
|
32
|
+
return result;
|
|
33
|
+
};
|
|
34
|
+
})();
|
|
85
35
|
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
86
36
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
87
37
|
};
|
|
88
38
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
89
39
|
exports.BitcoinSegwitProtocol = void 0;
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
40
|
+
const index_1 = __importDefault(require("@airgap/coinlib-core/dependencies/src/axios-0.19.0/index"));
|
|
41
|
+
const bignumber_1 = __importDefault(require("@airgap/coinlib-core/dependencies/src/bignumber.js-9.0.0/bignumber"));
|
|
42
|
+
const index_2 = require("@airgap/coinlib-core/dependencies/src/bip39-2.5.0/index");
|
|
43
|
+
const bs58check = __importStar(require("@airgap/coinlib-core/dependencies/src/bs58check-2.1.2"));
|
|
44
|
+
const ProtocolSymbols_1 = require("@airgap/coinlib-core/utils/ProtocolSymbols");
|
|
45
|
+
const bitcoinJS = __importStar(require("bitcoinjs-lib"));
|
|
46
|
+
const BitcoinProtocol_1 = require("./BitcoinProtocol");
|
|
47
|
+
const BitcoinProtocolOptions_1 = require("./BitcoinProtocolOptions");
|
|
48
|
+
const BitcoinSegwitAddress_1 = require("./BitcoinSegwitAddress");
|
|
49
|
+
const bip32_1 = require("bip32");
|
|
50
|
+
const secp256k1_1 = __importDefault(require("@bitcoinerlab/secp256k1"));
|
|
51
|
+
const DUST_AMOUNT = 50;
|
|
102
52
|
// This function handles arrays and objects
|
|
103
53
|
function eachRecursive(obj) {
|
|
104
54
|
for (var k in obj) {
|
|
@@ -112,437 +62,352 @@ function eachRecursive(obj) {
|
|
|
112
62
|
return obj;
|
|
113
63
|
}
|
|
114
64
|
// https://github.com/satoshilabs/slips/blob/master/slip-0132.md
|
|
115
|
-
|
|
116
|
-
|
|
65
|
+
class ExtendedPublicKey {
|
|
66
|
+
constructor(extendedPublicKey) {
|
|
117
67
|
this.rawKey = bs58check.decode(extendedPublicKey).slice(4);
|
|
118
68
|
}
|
|
119
|
-
|
|
69
|
+
toXpub() {
|
|
120
70
|
return this.addPrefix('0488b21e');
|
|
121
|
-
}
|
|
122
|
-
|
|
71
|
+
}
|
|
72
|
+
toYPub() {
|
|
123
73
|
return this.addPrefix('049d7cb2');
|
|
124
|
-
}
|
|
125
|
-
|
|
74
|
+
}
|
|
75
|
+
toZPub() {
|
|
126
76
|
return this.addPrefix('04b24746');
|
|
127
|
-
}
|
|
128
|
-
|
|
129
|
-
|
|
77
|
+
}
|
|
78
|
+
addPrefix(prefix) {
|
|
79
|
+
const data = Buffer.concat([Buffer.from(prefix, 'hex'), this.rawKey]);
|
|
130
80
|
return bs58check.encode(data);
|
|
131
|
-
};
|
|
132
|
-
return ExtendedPublicKey;
|
|
133
|
-
}());
|
|
134
|
-
var BitcoinSegwitProtocol = /** @class */ (function (_super) {
|
|
135
|
-
__extends(BitcoinSegwitProtocol, _super);
|
|
136
|
-
function BitcoinSegwitProtocol(options) {
|
|
137
|
-
if (options === void 0) { options = new BitcoinProtocolOptions_1.BitcoinProtocolOptions(); }
|
|
138
|
-
var _this = _super.call(this, options) || this;
|
|
139
|
-
_this.name = 'Bitcoin (Segwit)';
|
|
140
|
-
_this.identifier = ProtocolSymbols_1.MainProtocolSymbols.BTC_SEGWIT;
|
|
141
|
-
_this.standardDerivationPath = "m/84'/0'/0'";
|
|
142
|
-
_this.addressPlaceholder = 'bc1...';
|
|
143
|
-
_this.bip32 = (0, bip32_1.BIP32Factory)(secp256k1_1.default);
|
|
144
|
-
return _this;
|
|
145
81
|
}
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
}
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
BitcoinSegwitProtocol.prototype.getExtendedPrivateKeyFromHexSecret = function (secret, derivationPath) {
|
|
183
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
184
|
-
var bitcoinNode;
|
|
185
|
-
return __generator(this, function (_a) {
|
|
186
|
-
bitcoinNode = this.bip32.fromSeed(Buffer.from(secret, 'hex'), this.options.network.extras.network);
|
|
187
|
-
return [2 /*return*/, bitcoinNode.derivePath(derivationPath).toBase58()];
|
|
188
|
-
});
|
|
189
|
-
});
|
|
190
|
-
};
|
|
191
|
-
BitcoinSegwitProtocol.prototype.getAddressFromPublicKey = function (publicKey, cursor) {
|
|
192
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
193
|
-
var address;
|
|
194
|
-
return __generator(this, function (_a) {
|
|
195
|
-
address = BitcoinSegwitAddress_1.BitcoinSegwitAddress.fromAddress(this.bip32.fromBase58(publicKey, this.options.network.extras.network).toBase58());
|
|
196
|
-
return [2 /*return*/, {
|
|
197
|
-
address: address.asString(),
|
|
198
|
-
cursor: { hasNext: false }
|
|
199
|
-
}];
|
|
200
|
-
});
|
|
201
|
-
});
|
|
202
|
-
};
|
|
203
|
-
BitcoinSegwitProtocol.prototype.getAddressesFromPublicKey = function (publicKey, cursor) {
|
|
204
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
205
|
-
return __generator(this, function (_a) {
|
|
206
|
-
switch (_a.label) {
|
|
207
|
-
case 0: return [4 /*yield*/, this.getAddressFromPublicKey(publicKey, cursor)];
|
|
208
|
-
case 1: return [2 /*return*/, [_a.sent()]];
|
|
209
|
-
}
|
|
210
|
-
});
|
|
211
|
-
});
|
|
212
|
-
};
|
|
213
|
-
BitcoinSegwitProtocol.prototype.getAddressFromExtendedPublicKey = function (extendedPublicKey, visibilityDerivationIndex, addressDerivationIndex) {
|
|
214
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
215
|
-
var xpub, keyPair, obj, addressRaw, address;
|
|
216
|
-
return __generator(this, function (_a) {
|
|
217
|
-
xpub = new ExtendedPublicKey(extendedPublicKey).toXpub();
|
|
218
|
-
keyPair = this.bip32
|
|
219
|
-
.fromBase58(xpub, this.options.network.extras.network)
|
|
220
|
-
.derive(visibilityDerivationIndex)
|
|
221
|
-
.derive(addressDerivationIndex);
|
|
222
|
-
obj = bitcoinJS.payments.p2wpkh({ pubkey: Buffer.from(keyPair.publicKey) });
|
|
223
|
-
addressRaw = obj.address;
|
|
224
|
-
if (!addressRaw) {
|
|
225
|
-
throw new Error('could not generate address');
|
|
226
|
-
}
|
|
227
|
-
address = BitcoinSegwitAddress_1.BitcoinSegwitAddress.fromAddress(addressRaw);
|
|
228
|
-
return [2 /*return*/, {
|
|
229
|
-
address: address.asString(),
|
|
230
|
-
cursor: { hasNext: false }
|
|
231
|
-
}];
|
|
232
|
-
});
|
|
233
|
-
});
|
|
234
|
-
};
|
|
235
|
-
BitcoinSegwitProtocol.prototype.getAddressesFromExtendedPublicKey = function (extendedPublicKey, visibilityDerivationIndex, addressCount, offset) {
|
|
82
|
+
}
|
|
83
|
+
class BitcoinSegwitProtocol extends BitcoinProtocol_1.BitcoinProtocol {
|
|
84
|
+
constructor(options = new BitcoinProtocolOptions_1.BitcoinProtocolOptions()) {
|
|
85
|
+
super(options);
|
|
86
|
+
this.name = 'Bitcoin (Segwit)';
|
|
87
|
+
this.identifier = ProtocolSymbols_1.MainProtocolSymbols.BTC_SEGWIT;
|
|
88
|
+
this.standardDerivationPath = `m/84'/0'/0'`;
|
|
89
|
+
this.addressPlaceholder = 'bc1...';
|
|
90
|
+
this.bip32 = (0, bip32_1.BIP32Factory)(secp256k1_1.default);
|
|
91
|
+
}
|
|
92
|
+
async getPublicKeyFromHexSecret(secret, derivationPath) {
|
|
93
|
+
const bitcoinNode = this.bip32.fromSeed(Buffer.from(secret, 'hex'), this.options.network.extras.network);
|
|
94
|
+
const neutered = bitcoinNode.derivePath(derivationPath).neutered();
|
|
95
|
+
const zpub = new ExtendedPublicKey(neutered.toBase58()).toZPub();
|
|
96
|
+
return zpub;
|
|
97
|
+
}
|
|
98
|
+
async getPrivateKeyFromHexSecret(secret, derivationPath) {
|
|
99
|
+
if (!derivationPath) {
|
|
100
|
+
return this.bip32.fromSeed(Buffer.from(secret, 'hex'), this.options.network.extras.network);
|
|
101
|
+
}
|
|
102
|
+
const bitcoinNode = this.bip32.fromSeed(Buffer.from(secret, 'hex'), this.options.network.extras.network);
|
|
103
|
+
const privateKey = bitcoinNode.derivePath(derivationPath).privateKey;
|
|
104
|
+
if (!privateKey) {
|
|
105
|
+
throw new Error('No privatekey!');
|
|
106
|
+
}
|
|
107
|
+
return Buffer.from(privateKey).toString('hex');
|
|
108
|
+
}
|
|
109
|
+
async getExtendedPrivateKeyFromMnemonic(mnemonic, derivationPath, password) {
|
|
110
|
+
const secret = (0, index_2.mnemonicToSeed)(mnemonic, password);
|
|
111
|
+
return this.getExtendedPrivateKeyFromHexSecret(secret, derivationPath);
|
|
112
|
+
}
|
|
113
|
+
async getExtendedPrivateKeyFromHexSecret(secret, derivationPath) {
|
|
114
|
+
const bitcoinNode = this.bip32.fromSeed(Buffer.from(secret, 'hex'), this.options.network.extras.network);
|
|
115
|
+
return bitcoinNode.derivePath(derivationPath).toBase58();
|
|
116
|
+
}
|
|
117
|
+
async getAddressFromPublicKey(publicKey, cursor) {
|
|
236
118
|
// broadcaster knows this (both broadcaster and signer)
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
119
|
+
const address = BitcoinSegwitAddress_1.BitcoinSegwitAddress.fromAddress(this.bip32.fromBase58(publicKey, this.options.network.extras.network).toBase58());
|
|
120
|
+
return {
|
|
121
|
+
address: address.asString(),
|
|
122
|
+
cursor: { hasNext: false }
|
|
123
|
+
};
|
|
124
|
+
}
|
|
125
|
+
async getAddressesFromPublicKey(publicKey, cursor) {
|
|
126
|
+
return [await this.getAddressFromPublicKey(publicKey, cursor)];
|
|
127
|
+
}
|
|
128
|
+
async getAddressFromExtendedPublicKey(extendedPublicKey, visibilityDerivationIndex, addressDerivationIndex) {
|
|
129
|
+
const xpub = new ExtendedPublicKey(extendedPublicKey).toXpub();
|
|
130
|
+
// broadcaster knows this (both broadcaster and signer)
|
|
131
|
+
const keyPair = this.bip32
|
|
132
|
+
.fromBase58(xpub, this.options.network.extras.network)
|
|
133
|
+
.derive(visibilityDerivationIndex)
|
|
134
|
+
.derive(addressDerivationIndex);
|
|
135
|
+
const obj = bitcoinJS.payments.p2wpkh({ pubkey: Buffer.from(keyPair.publicKey) });
|
|
136
|
+
const { address: addressRaw } = obj;
|
|
137
|
+
if (!addressRaw) {
|
|
138
|
+
throw new Error('could not generate address');
|
|
139
|
+
}
|
|
140
|
+
const address = BitcoinSegwitAddress_1.BitcoinSegwitAddress.fromAddress(addressRaw);
|
|
141
|
+
return {
|
|
142
|
+
address: address.asString(),
|
|
143
|
+
cursor: { hasNext: false }
|
|
144
|
+
};
|
|
145
|
+
}
|
|
146
|
+
getAddressesFromExtendedPublicKey(extendedPublicKey, visibilityDerivationIndex, addressCount, offset) {
|
|
147
|
+
// broadcaster knows this (both broadcaster and signer)
|
|
148
|
+
const node = this.bip32.fromBase58(new ExtendedPublicKey(extendedPublicKey).toXpub(), this.options.network.extras.network);
|
|
149
|
+
const generatorArray = Array.from(new Array(addressCount), (x, i) => i + offset);
|
|
150
|
+
return Promise.all(generatorArray.map((x) => {
|
|
151
|
+
const keyPair = node.derive(visibilityDerivationIndex).derive(x);
|
|
152
|
+
const { address: addressRaw } = bitcoinJS.payments.p2wpkh({ pubkey: Buffer.from(keyPair.publicKey) });
|
|
242
153
|
if (!addressRaw) {
|
|
243
154
|
throw new Error('could not generate address');
|
|
244
155
|
}
|
|
245
|
-
|
|
156
|
+
const address = BitcoinSegwitAddress_1.BitcoinSegwitAddress.fromAddress(addressRaw);
|
|
246
157
|
return {
|
|
247
158
|
address: address.asString(),
|
|
248
159
|
cursor: { hasNext: false }
|
|
249
160
|
};
|
|
250
161
|
}));
|
|
251
|
-
}
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
{
|
|
278
|
-
|
|
279
|
-
if (unknownKeyVals) {
|
|
280
|
-
var amountArray = unknownKeyVals.filter(function (kv) { return kv.key.equals(Buffer.from('amount')); });
|
|
281
|
-
if (amountArray.length > 0) {
|
|
282
|
-
return new bignumber_1.default(amountArray[0].value.toString()); // Buffer to number
|
|
283
|
-
}
|
|
284
|
-
}
|
|
162
|
+
}
|
|
163
|
+
async getTransactionDetails(unsignedTx) {
|
|
164
|
+
// out of public information (both broadcaster and signer)
|
|
165
|
+
const transaction = unsignedTx.transaction;
|
|
166
|
+
const decodedPSBT = bitcoinJS.Psbt.fromHex(transaction.psbt);
|
|
167
|
+
let feeCalculator = new bignumber_1.default(0);
|
|
168
|
+
for (const txIn of decodedPSBT.data.inputs) {
|
|
169
|
+
feeCalculator = feeCalculator.plus(new bignumber_1.default(txIn.witnessUtxo?.value ?? 0));
|
|
170
|
+
}
|
|
171
|
+
for (const txOut of decodedPSBT.txOutputs) {
|
|
172
|
+
feeCalculator = feeCalculator.minus(new bignumber_1.default(txOut.value));
|
|
173
|
+
}
|
|
174
|
+
const warnings = [];
|
|
175
|
+
const clonedPSBT = decodedPSBT.clone();
|
|
176
|
+
eachRecursive(clonedPSBT); // All buffers to hex string
|
|
177
|
+
// Amount is tricky for BTC. Full amount of inputs is always transferred, but usually a (big) part of the amount is sent back to the sender via change address. So it's not easy to determine what the amount is that the user intended to send.
|
|
178
|
+
const amount = (() => {
|
|
179
|
+
// If tx has only one output, this is the amount
|
|
180
|
+
if (decodedPSBT.txOutputs.length === 1) {
|
|
181
|
+
return new bignumber_1.default(decodedPSBT.txOutputs[0].value);
|
|
182
|
+
}
|
|
183
|
+
// If we can match one output to an exact amount that we add to the PSBT, this is our amount.
|
|
184
|
+
{
|
|
185
|
+
const unknownKeyVals = decodedPSBT.data.globalMap.unknownKeyVals;
|
|
186
|
+
if (unknownKeyVals) {
|
|
187
|
+
const amountArray = unknownKeyVals.filter((kv) => kv.key.equals(Buffer.from('amount')));
|
|
188
|
+
if (amountArray.length > 0) {
|
|
189
|
+
return new bignumber_1.default(amountArray[0].value.toString()); // Buffer to number
|
|
285
190
|
}
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
var output = decodedPSBT.txOutputs[index];
|
|
301
|
-
accumulated = accumulated.plus(output.value);
|
|
302
|
-
});
|
|
303
|
-
if (useAccumulated) {
|
|
304
|
-
return accumulated;
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
// 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.
|
|
194
|
+
let accumulated = new bignumber_1.default(0);
|
|
195
|
+
let useAccumulated = false;
|
|
196
|
+
decodedPSBT.data.outputs.forEach((outputKeyValues, index) => {
|
|
197
|
+
if (outputKeyValues.unknownKeyVals) {
|
|
198
|
+
const derivationPaths = outputKeyValues.unknownKeyVals
|
|
199
|
+
.filter((kv) => kv.key.equals(Buffer.from('dp')))
|
|
200
|
+
.map((kv) => kv.value.toString());
|
|
201
|
+
if (derivationPaths.length > 0) {
|
|
202
|
+
// If one of the outputs has the derivation in the custom key/value map, we can use this to determine the amount.
|
|
203
|
+
useAccumulated = true;
|
|
204
|
+
return;
|
|
305
205
|
}
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
.reduce(function (accumulator, currentValue) { return accumulator.plus(currentValue); });
|
|
310
|
-
})();
|
|
311
|
-
return [2 /*return*/, [
|
|
312
|
-
{
|
|
313
|
-
from: decodedPSBT.data.inputs.map(function (obj) {
|
|
314
|
-
var _a, _b;
|
|
315
|
-
return (_b = (_a = obj.bip32Derivation) === null || _a === void 0 ? void 0 : _a.map(function (el) {
|
|
316
|
-
return bitcoinJS.payments.p2wpkh({
|
|
317
|
-
pubkey: el.pubkey,
|
|
318
|
-
network: bitcoinJS.networks.bitcoin
|
|
319
|
-
}).address;
|
|
320
|
-
}).join(' ')) !== null && _b !== void 0 ? _b : 'INVALID';
|
|
321
|
-
}),
|
|
322
|
-
to: decodedPSBT.txOutputs.map(function (obj) {
|
|
323
|
-
return obj.address || "Script: ".concat(obj.script.toString('hex')) || 'unknown';
|
|
324
|
-
}),
|
|
325
|
-
amount: amount.toString(10),
|
|
326
|
-
fee: feeCalculator.toString(10),
|
|
327
|
-
protocolIdentifier: this.identifier,
|
|
328
|
-
network: this.options.network,
|
|
329
|
-
isInbound: false,
|
|
330
|
-
transactionDetails: {
|
|
331
|
-
// This is some unstructured data about the PSBT. This is shown in the UI as a JSON for advanced users.
|
|
332
|
-
inputTx: eachRecursive(clonedPSBT.txInputs),
|
|
333
|
-
outputTx: eachRecursive(clonedPSBT.txOutputs),
|
|
334
|
-
inputData: clonedPSBT.data.inputs,
|
|
335
|
-
outputData: clonedPSBT.data.outputs,
|
|
336
|
-
PSBTVersion: clonedPSBT.version,
|
|
337
|
-
PSBTLocktime: clonedPSBT.locktime,
|
|
338
|
-
PSBTGlobalMap: clonedPSBT.data.globalMap,
|
|
339
|
-
rawPSBT: unsignedTx.transaction
|
|
340
|
-
},
|
|
341
|
-
warnings: warnings
|
|
342
|
-
}
|
|
343
|
-
]];
|
|
206
|
+
}
|
|
207
|
+
const output = decodedPSBT.txOutputs[index];
|
|
208
|
+
accumulated = accumulated.plus(output.value);
|
|
344
209
|
});
|
|
210
|
+
if (useAccumulated) {
|
|
211
|
+
return accumulated;
|
|
212
|
+
}
|
|
213
|
+
// 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.
|
|
214
|
+
return decodedPSBT.txOutputs
|
|
215
|
+
.map((obj) => new bignumber_1.default(obj.value))
|
|
216
|
+
.reduce((accumulator, currentValue) => accumulator.plus(currentValue));
|
|
217
|
+
})();
|
|
218
|
+
return [
|
|
219
|
+
{
|
|
220
|
+
from: decodedPSBT.data.inputs.map((obj) => obj.bip32Derivation
|
|
221
|
+
?.map((el) => bitcoinJS.payments.p2wpkh({
|
|
222
|
+
pubkey: el.pubkey,
|
|
223
|
+
network: bitcoinJS.networks.bitcoin
|
|
224
|
+
}).address)
|
|
225
|
+
.join(' ') ?? 'INVALID'),
|
|
226
|
+
to: decodedPSBT.txOutputs.map((obj) => {
|
|
227
|
+
return obj.address || `Script: ${obj.script.toString('hex')}` || 'unknown';
|
|
228
|
+
}),
|
|
229
|
+
amount: amount.toString(10),
|
|
230
|
+
fee: feeCalculator.toString(10),
|
|
231
|
+
protocolIdentifier: this.identifier,
|
|
232
|
+
network: this.options.network,
|
|
233
|
+
isInbound: false,
|
|
234
|
+
transactionDetails: {
|
|
235
|
+
// This is some unstructured data about the PSBT. This is shown in the UI as a JSON for advanced users.
|
|
236
|
+
inputTx: eachRecursive(clonedPSBT.txInputs),
|
|
237
|
+
outputTx: eachRecursive(clonedPSBT.txOutputs),
|
|
238
|
+
inputData: clonedPSBT.data.inputs,
|
|
239
|
+
outputData: clonedPSBT.data.outputs,
|
|
240
|
+
PSBTVersion: clonedPSBT.version,
|
|
241
|
+
PSBTLocktime: clonedPSBT.locktime,
|
|
242
|
+
PSBTGlobalMap: clonedPSBT.data.globalMap,
|
|
243
|
+
rawPSBT: unsignedTx.transaction
|
|
244
|
+
},
|
|
245
|
+
warnings
|
|
246
|
+
}
|
|
247
|
+
];
|
|
248
|
+
}
|
|
249
|
+
async getTransactionDetailsFromSigned(signedTx) {
|
|
250
|
+
return this.getTransactionDetails({ publicKey: '', transaction: { psbt: signedTx.transaction } });
|
|
251
|
+
}
|
|
252
|
+
async prepareTransactionFromExtendedPublicKey(extendedPublicKey, offset, recipients, values, fee, extras) {
|
|
253
|
+
if (!extras.masterFingerprint) {
|
|
254
|
+
throw new Error('MasterFingerprint not set!');
|
|
255
|
+
}
|
|
256
|
+
const wrappedValues = values.map((value) => new bignumber_1.default(value));
|
|
257
|
+
const wrappedFee = new bignumber_1.default(fee);
|
|
258
|
+
const transaction = {
|
|
259
|
+
ins: [],
|
|
260
|
+
outs: []
|
|
261
|
+
};
|
|
262
|
+
if (recipients.length !== wrappedValues.length) {
|
|
263
|
+
throw new Error('recipients do not match values');
|
|
264
|
+
}
|
|
265
|
+
const { data: utxos } = await index_1.default.get(`${this.options.network.extras.indexerApi}/api/v2/utxo/${extendedPublicKey}?confirmed=true`, {
|
|
266
|
+
responseType: 'json'
|
|
345
267
|
});
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
268
|
+
if (utxos.length <= 0) {
|
|
269
|
+
throw new Error('not enough balance'); // no transactions found on those addresses, probably won't find anything in the next ones
|
|
270
|
+
}
|
|
271
|
+
const totalRequiredBalance = wrappedValues
|
|
272
|
+
.reduce((accumulator, currentValue) => accumulator.plus(currentValue))
|
|
273
|
+
.plus(wrappedFee);
|
|
274
|
+
let valueAccumulator = new bignumber_1.default(0);
|
|
275
|
+
const getPathIndexes = (path) => {
|
|
276
|
+
const result = path
|
|
277
|
+
.split('/')
|
|
278
|
+
.slice(-2)
|
|
279
|
+
.map((item) => parseInt(item))
|
|
280
|
+
.filter((item) => !isNaN(item));
|
|
281
|
+
if (result.length !== 2) {
|
|
282
|
+
throw new Error('Unexpected path format');
|
|
283
|
+
}
|
|
284
|
+
return [result[0], result[1]];
|
|
285
|
+
};
|
|
286
|
+
for (const utxo of utxos) {
|
|
287
|
+
valueAccumulator = valueAccumulator.plus(utxo.value);
|
|
288
|
+
const indexes = getPathIndexes(utxo.path);
|
|
289
|
+
const derivedAddress = (await this.getAddressFromExtendedPublicKey(extendedPublicKey, indexes[0], indexes[1])).address;
|
|
290
|
+
if (derivedAddress === utxo.address) {
|
|
291
|
+
transaction.ins.push({
|
|
292
|
+
txId: utxo.txid,
|
|
293
|
+
value: new bignumber_1.default(utxo.value).toString(10),
|
|
294
|
+
vout: utxo.vout,
|
|
295
|
+
address: utxo.address,
|
|
296
|
+
derivationPath: utxo.path
|
|
297
|
+
});
|
|
298
|
+
}
|
|
299
|
+
else {
|
|
300
|
+
throw new Error('Invalid address returned from API');
|
|
301
|
+
}
|
|
302
|
+
if (valueAccumulator.isGreaterThanOrEqualTo(totalRequiredBalance)) {
|
|
303
|
+
break;
|
|
304
|
+
}
|
|
305
|
+
}
|
|
306
|
+
if (valueAccumulator.isLessThan(totalRequiredBalance)) {
|
|
307
|
+
throw new Error('not enough balance 2');
|
|
308
|
+
}
|
|
309
|
+
for (let i = 0; i < recipients.length; i++) {
|
|
310
|
+
transaction.outs.push({
|
|
311
|
+
recipient: recipients[i],
|
|
312
|
+
isChange: false,
|
|
313
|
+
value: wrappedValues[i].toString(10)
|
|
314
|
+
});
|
|
315
|
+
valueAccumulator = valueAccumulator.minus(wrappedValues[i]);
|
|
316
|
+
}
|
|
317
|
+
const lastUsedInternalAddress = Math.max(-1, ...utxos
|
|
318
|
+
.map((utxo) => getPathIndexes(utxo.path))
|
|
319
|
+
.filter((indexes) => indexes[0] === 1)
|
|
320
|
+
.map((indexes) => indexes[1]));
|
|
321
|
+
// If the change is considered dust, the transaction will fail.
|
|
322
|
+
// Dust is a variable value around 300-600 satoshis, depending on the configuration.
|
|
323
|
+
// We set a low fee here to not block any transactions, but it might still fail due to "dust".
|
|
324
|
+
const changeValue = valueAccumulator.minus(wrappedFee);
|
|
325
|
+
if (changeValue.isGreaterThan(new bignumber_1.default(DUST_AMOUNT))) {
|
|
326
|
+
const changeAddressIndex = lastUsedInternalAddress + 1;
|
|
327
|
+
const derivedAddress = (await this.getAddressFromExtendedPublicKey(extendedPublicKey, 1, changeAddressIndex)).address;
|
|
328
|
+
transaction.outs.push({
|
|
329
|
+
recipient: derivedAddress,
|
|
330
|
+
isChange: true,
|
|
331
|
+
value: changeValue.toString(10),
|
|
332
|
+
derivationPath: `1/${changeAddressIndex}`
|
|
351
333
|
});
|
|
334
|
+
}
|
|
335
|
+
const psbt = new bitcoinJS.Psbt();
|
|
336
|
+
// 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.
|
|
337
|
+
psbt.addUnknownKeyValToGlobal({
|
|
338
|
+
key: Buffer.from('amount'),
|
|
339
|
+
value: Buffer.from(wrappedValues.reduce((accumulator, currentValue) => accumulator.plus(currentValue)).toString())
|
|
352
340
|
});
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
if (utxos.length <= 0) {
|
|
379
|
-
throw new Error('not enough balance'); // no transactions found on those addresses, probably won't find anything in the next ones
|
|
380
|
-
}
|
|
381
|
-
totalRequiredBalance = wrappedValues
|
|
382
|
-
.reduce(function (accumulator, currentValue) { return accumulator.plus(currentValue); })
|
|
383
|
-
.plus(wrappedFee);
|
|
384
|
-
valueAccumulator = new bignumber_1.default(0);
|
|
385
|
-
getPathIndexes = function (path) {
|
|
386
|
-
var result = path
|
|
387
|
-
.split('/')
|
|
388
|
-
.slice(-2)
|
|
389
|
-
.map(function (item) { return parseInt(item); })
|
|
390
|
-
.filter(function (item) { return !isNaN(item); });
|
|
391
|
-
if (result.length !== 2) {
|
|
392
|
-
throw new Error('Unexpected path format');
|
|
393
|
-
}
|
|
394
|
-
return [result[0], result[1]];
|
|
395
|
-
};
|
|
396
|
-
_i = 0, utxos_1 = utxos;
|
|
397
|
-
_a.label = 2;
|
|
398
|
-
case 2:
|
|
399
|
-
if (!(_i < utxos_1.length)) return [3 /*break*/, 5];
|
|
400
|
-
utxo = utxos_1[_i];
|
|
401
|
-
valueAccumulator = valueAccumulator.plus(utxo.value);
|
|
402
|
-
indexes = getPathIndexes(utxo.path);
|
|
403
|
-
return [4 /*yield*/, this.getAddressFromExtendedPublicKey(extendedPublicKey, indexes[0], indexes[1])];
|
|
404
|
-
case 3:
|
|
405
|
-
derivedAddress = (_a.sent()).address;
|
|
406
|
-
if (derivedAddress === utxo.address) {
|
|
407
|
-
transaction.ins.push({
|
|
408
|
-
txId: utxo.txid,
|
|
409
|
-
value: new bignumber_1.default(utxo.value).toString(10),
|
|
410
|
-
vout: utxo.vout,
|
|
411
|
-
address: utxo.address,
|
|
412
|
-
derivationPath: utxo.path
|
|
413
|
-
});
|
|
414
|
-
}
|
|
415
|
-
else {
|
|
416
|
-
throw new Error('Invalid address returned from API');
|
|
417
|
-
}
|
|
418
|
-
if (valueAccumulator.isGreaterThanOrEqualTo(totalRequiredBalance)) {
|
|
419
|
-
return [3 /*break*/, 5];
|
|
420
|
-
}
|
|
421
|
-
_a.label = 4;
|
|
422
|
-
case 4:
|
|
423
|
-
_i++;
|
|
424
|
-
return [3 /*break*/, 2];
|
|
425
|
-
case 5:
|
|
426
|
-
if (valueAccumulator.isLessThan(totalRequiredBalance)) {
|
|
427
|
-
throw new Error('not enough balance 2');
|
|
428
|
-
}
|
|
429
|
-
for (i = 0; i < recipients.length; i++) {
|
|
430
|
-
transaction.outs.push({
|
|
431
|
-
recipient: recipients[i],
|
|
432
|
-
isChange: false,
|
|
433
|
-
value: wrappedValues[i].toString(10)
|
|
434
|
-
});
|
|
435
|
-
valueAccumulator = valueAccumulator.minus(wrappedValues[i]);
|
|
436
|
-
}
|
|
437
|
-
lastUsedInternalAddress = Math.max.apply(Math, __spreadArray([-1], utxos
|
|
438
|
-
.map(function (utxo) { return getPathIndexes(utxo.path); })
|
|
439
|
-
.filter(function (indexes) { return indexes[0] === 1; })
|
|
440
|
-
.map(function (indexes) { return indexes[1]; }), false));
|
|
441
|
-
changeValue = valueAccumulator.minus(wrappedFee);
|
|
442
|
-
if (!changeValue.isGreaterThan(new bignumber_1.default(DUST_AMOUNT))) return [3 /*break*/, 7];
|
|
443
|
-
changeAddressIndex = lastUsedInternalAddress + 1;
|
|
444
|
-
return [4 /*yield*/, this.getAddressFromExtendedPublicKey(extendedPublicKey, 1, changeAddressIndex)];
|
|
445
|
-
case 6:
|
|
446
|
-
derivedAddress = (_a.sent()).address;
|
|
447
|
-
transaction.outs.push({
|
|
448
|
-
recipient: derivedAddress,
|
|
449
|
-
isChange: true,
|
|
450
|
-
value: changeValue.toString(10),
|
|
451
|
-
derivationPath: "1/".concat(changeAddressIndex)
|
|
452
|
-
});
|
|
453
|
-
_a.label = 7;
|
|
454
|
-
case 7:
|
|
455
|
-
psbt = new bitcoinJS.Psbt();
|
|
456
|
-
// 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.
|
|
457
|
-
psbt.addUnknownKeyValToGlobal({
|
|
458
|
-
key: Buffer.from('amount'),
|
|
459
|
-
value: Buffer.from(wrappedValues.reduce(function (accumulator, currentValue) { return accumulator.plus(currentValue); }).toString())
|
|
460
|
-
});
|
|
461
|
-
keyPair = this.bip32.fromBase58(new ExtendedPublicKey(extendedPublicKey).toXpub());
|
|
462
|
-
replaceByFee = extras.replaceByFee ? true : false;
|
|
463
|
-
transaction.ins.forEach(function (tx) {
|
|
464
|
-
var indexes = getPathIndexes(tx.derivationPath);
|
|
465
|
-
var childNode = keyPair.derivePath(indexes.join('/'));
|
|
466
|
-
var p2wpkh = bitcoinJS.payments.p2wpkh({ pubkey: Buffer.from(childNode.publicKey), network: _this.options.network.extras.network });
|
|
467
|
-
var p2shOutput = p2wpkh.output;
|
|
468
|
-
if (!p2shOutput) {
|
|
469
|
-
throw new Error('no p2shOutput');
|
|
470
|
-
}
|
|
471
|
-
psbt.addInput({
|
|
472
|
-
hash: tx.txId,
|
|
473
|
-
index: tx.vout,
|
|
474
|
-
sequence: replaceByFee ? 0xfffffffd : undefined,
|
|
475
|
-
witnessUtxo: {
|
|
476
|
-
script: p2shOutput,
|
|
477
|
-
value: parseInt(tx.value)
|
|
478
|
-
},
|
|
479
|
-
bip32Derivation: [
|
|
480
|
-
{
|
|
481
|
-
masterFingerprint: Buffer.from(extras.masterFingerprint, 'hex'),
|
|
482
|
-
pubkey: Buffer.from(childNode.publicKey),
|
|
483
|
-
path: tx.derivationPath
|
|
484
|
-
}
|
|
485
|
-
]
|
|
486
|
-
});
|
|
487
|
-
});
|
|
488
|
-
transaction.outs.forEach(function (out, index) {
|
|
489
|
-
psbt.addOutput({ address: out.recipient, value: parseInt(out.value) });
|
|
490
|
-
if (out.derivationPath) {
|
|
491
|
-
// 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.
|
|
492
|
-
psbt.addUnknownKeyValToOutput(index, {
|
|
493
|
-
key: Buffer.from('dp'),
|
|
494
|
-
value: Buffer.from(out.derivationPath, 'utf8')
|
|
495
|
-
});
|
|
496
|
-
}
|
|
497
|
-
});
|
|
498
|
-
tx = {
|
|
499
|
-
psbt: psbt.toHex()
|
|
500
|
-
};
|
|
501
|
-
return [2 /*return*/, tx];
|
|
502
|
-
}
|
|
341
|
+
const keyPair = this.bip32.fromBase58(new ExtendedPublicKey(extendedPublicKey).toXpub());
|
|
342
|
+
const replaceByFee = extras.replaceByFee ? true : false;
|
|
343
|
+
transaction.ins.forEach((tx) => {
|
|
344
|
+
const indexes = getPathIndexes(tx.derivationPath);
|
|
345
|
+
const childNode = keyPair.derivePath(indexes.join('/'));
|
|
346
|
+
const p2wpkh = bitcoinJS.payments.p2wpkh({ pubkey: Buffer.from(childNode.publicKey), network: this.options.network.extras.network });
|
|
347
|
+
const p2shOutput = p2wpkh.output;
|
|
348
|
+
if (!p2shOutput) {
|
|
349
|
+
throw new Error('no p2shOutput');
|
|
350
|
+
}
|
|
351
|
+
psbt.addInput({
|
|
352
|
+
hash: tx.txId,
|
|
353
|
+
index: tx.vout,
|
|
354
|
+
sequence: replaceByFee ? 0xfffffffd : undefined, // Needs to be at least 2 below max int value to be RBF
|
|
355
|
+
witnessUtxo: {
|
|
356
|
+
script: p2shOutput,
|
|
357
|
+
value: parseInt(tx.value)
|
|
358
|
+
},
|
|
359
|
+
bip32Derivation: [
|
|
360
|
+
{
|
|
361
|
+
masterFingerprint: Buffer.from(extras.masterFingerprint, 'hex'),
|
|
362
|
+
pubkey: Buffer.from(childNode.publicKey),
|
|
363
|
+
path: tx.derivationPath
|
|
364
|
+
}
|
|
365
|
+
]
|
|
503
366
|
});
|
|
504
367
|
});
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
decodedPSBT = bitcoinJS.Psbt.fromHex(rawBitcoinSegwitTx.psbt);
|
|
513
|
-
decodedPSBT.data.inputs.forEach(function (input, index) {
|
|
514
|
-
var _a;
|
|
515
|
-
(_a = input.bip32Derivation) === null || _a === void 0 ? void 0 : _a.forEach(function (deriv) {
|
|
516
|
-
try {
|
|
517
|
-
// This uses the same logic to find child key as the "findWalletByFingerprintDerivationPathAndProtocolIdentifier" method in the Vault
|
|
518
|
-
var cutoffFrom = deriv.path.lastIndexOf("'") || deriv.path.lastIndexOf('h');
|
|
519
|
-
var childPath = deriv.path.substr(cutoffFrom + 2);
|
|
520
|
-
var childNode_1 = bip32PK.derivePath(childPath);
|
|
521
|
-
decodedPSBT.signInput(index, {
|
|
522
|
-
publicKey: Buffer.from(childNode_1.publicKey),
|
|
523
|
-
sign: function (hash, lowR) { return Buffer.from(childNode_1.sign(hash, lowR)); }
|
|
524
|
-
});
|
|
525
|
-
console.log("Signed input ".concat(index, " with path ").concat(deriv.path));
|
|
526
|
-
}
|
|
527
|
-
catch (e) {
|
|
528
|
-
console.log("Error signing input ".concat(index), e);
|
|
529
|
-
}
|
|
530
|
-
});
|
|
368
|
+
transaction.outs.forEach((out, index) => {
|
|
369
|
+
psbt.addOutput({ address: out.recipient, value: parseInt(out.value) });
|
|
370
|
+
if (out.derivationPath) {
|
|
371
|
+
// 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.
|
|
372
|
+
psbt.addUnknownKeyValToOutput(index, {
|
|
373
|
+
key: Buffer.from('dp'),
|
|
374
|
+
value: Buffer.from(out.derivationPath, 'utf8')
|
|
531
375
|
});
|
|
532
|
-
|
|
533
|
-
});
|
|
376
|
+
}
|
|
534
377
|
});
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
|
|
378
|
+
const tx = {
|
|
379
|
+
psbt: psbt.toHex()
|
|
380
|
+
};
|
|
381
|
+
return tx;
|
|
382
|
+
}
|
|
383
|
+
async signWithExtendedPrivateKey(extendedPrivateKey, transaction /* RawBitcoinSegwitTransaction */) {
|
|
384
|
+
const rawBitcoinSegwitTx = transaction;
|
|
385
|
+
const bip32PK = this.bip32.fromBase58(extendedPrivateKey);
|
|
386
|
+
const decodedPSBT = bitcoinJS.Psbt.fromHex(rawBitcoinSegwitTx.psbt);
|
|
387
|
+
decodedPSBT.data.inputs.forEach((input, index) => {
|
|
388
|
+
input.bip32Derivation?.forEach((deriv) => {
|
|
389
|
+
try {
|
|
390
|
+
// This uses the same logic to find child key as the "findWalletByFingerprintDerivationPathAndProtocolIdentifier" method in the Vault
|
|
391
|
+
const cutoffFrom = deriv.path.lastIndexOf("'") || deriv.path.lastIndexOf('h');
|
|
392
|
+
const childPath = deriv.path.substr(cutoffFrom + 2);
|
|
393
|
+
const childNode = bip32PK.derivePath(childPath);
|
|
394
|
+
decodedPSBT.signInput(index, {
|
|
395
|
+
publicKey: Buffer.from(childNode.publicKey),
|
|
396
|
+
sign: (hash, lowR) => Buffer.from(childNode.sign(hash, lowR))
|
|
397
|
+
});
|
|
398
|
+
console.log(`Signed input ${index} with path ${deriv.path}`);
|
|
399
|
+
}
|
|
400
|
+
catch (e) {
|
|
401
|
+
console.log(`Error signing input ${index}`, e);
|
|
402
|
+
}
|
|
542
403
|
});
|
|
543
404
|
});
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
|
|
405
|
+
return decodedPSBT.toHex();
|
|
406
|
+
}
|
|
407
|
+
async broadcastTransaction(rawTransaction) {
|
|
408
|
+
const hexTransaction = bitcoinJS.Psbt.fromHex(rawTransaction).finalizeAllInputs().extractTransaction().toHex();
|
|
409
|
+
return super.broadcastTransaction(hexTransaction);
|
|
410
|
+
}
|
|
411
|
+
}
|
|
547
412
|
exports.BitcoinSegwitProtocol = BitcoinSegwitProtocol;
|
|
548
413
|
//# sourceMappingURL=BitcoinSegwitProtocol.js.map
|