@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.
- package/index.d.ts +1 -0
- package/index.js +18 -0
- package/index.js.map +1 -0
- package/package.json +1 -5
- package/v0/index.d.ts +14 -0
- package/v0/index.js +34 -0
- package/v0/index.js.map +1 -0
- package/v0/protocol/BitcoinAddress.d.ts +9 -0
- package/v0/protocol/BitcoinAddress.js +23 -0
- package/v0/protocol/BitcoinAddress.js.map +1 -0
- package/v0/protocol/BitcoinCryptoClient.d.ts +11 -0
- package/v0/protocol/BitcoinCryptoClient.js +91 -0
- package/v0/protocol/BitcoinCryptoClient.js.map +1 -0
- package/v0/protocol/BitcoinProtocol.d.ts +167 -0
- package/v0/protocol/BitcoinProtocol.js +1063 -0
- package/v0/protocol/BitcoinProtocol.js.map +1 -0
- package/v0/protocol/BitcoinProtocolOptions.d.ts +26 -0
- package/v0/protocol/BitcoinProtocolOptions.js +148 -0
- package/v0/protocol/BitcoinProtocolOptions.js.map +1 -0
- package/v0/protocol/BitcoinSegwitAddress.d.ts +7 -0
- package/v0/protocol/BitcoinSegwitAddress.js +41 -0
- package/v0/protocol/BitcoinSegwitAddress.js.map +1 -0
- package/v0/protocol/BitcoinSegwitProtocol.d.ts +28 -0
- package/v0/protocol/BitcoinSegwitProtocol.js +541 -0
- package/v0/protocol/BitcoinSegwitProtocol.js.map +1 -0
- package/v0/protocol/BitcoinTestnetProtocol.d.ts +7 -0
- package/v0/protocol/BitcoinTestnetProtocol.js +58 -0
- package/v0/protocol/BitcoinTestnetProtocol.js.map +1 -0
- package/v0/protocol/BitcoinTypes.d.ts +21 -0
- package/v0/protocol/BitcoinTypes.js +3 -0
- package/v0/protocol/BitcoinTypes.js.map +1 -0
- package/v0/serializer/schemas/v2/transaction-sign-request-bitcoin-segwit.json +0 -0
- package/v0/serializer/schemas/v2/transaction-sign-request-bitcoin.json +73 -0
- package/v0/serializer/schemas/v2/transaction-sign-response-bitcoin-segwit.json +0 -0
- package/v0/serializer/schemas/v2/transaction-sign-response-bitcoin.json +37 -0
- package/v0/serializer/schemas/v3/transaction-sign-request-bitcoin-segwit.json +29 -0
- package/v0/serializer/schemas/v3/transaction-sign-request-bitcoin.json +73 -0
- package/v0/serializer/schemas/v3/transaction-sign-response-bitcoin-segwit.json +19 -0
- package/v0/serializer/schemas/v3/transaction-sign-response-bitcoin.json +37 -0
- package/v0/serializer/validators/transaction-validator.d.ts +15 -0
- package/v0/serializer/validators/transaction-validator.js +81 -0
- package/v0/serializer/validators/transaction-validator.js.map +1 -0
- package/v0/serializer/validators/validators.d.ts +1 -0
- package/v0/serializer/validators/validators.js +151 -0
- package/v0/serializer/validators/validators.js.map +1 -0
- package/v0/types/signed-transaction-bitcoin-segwit.d.ts +5 -0
- package/v0/types/signed-transaction-bitcoin-segwit.js +3 -0
- package/v0/types/signed-transaction-bitcoin-segwit.js.map +1 -0
- package/v0/types/signed-transaction-bitcoin.d.ts +9 -0
- package/v0/types/signed-transaction-bitcoin.js +3 -0
- package/v0/types/signed-transaction-bitcoin.js.map +1 -0
- package/v0/types/transaction-bitcoin.d.ts +20 -0
- package/v0/types/transaction-bitcoin.js +3 -0
- package/v0/types/transaction-bitcoin.js.map +1 -0
- package/v0/types/unsigned-transaction-bitcoin-segwit.d.ts +7 -0
- package/v0/types/unsigned-transaction-bitcoin-segwit.js +3 -0
- package/v0/types/unsigned-transaction-bitcoin-segwit.js.map +1 -0
- package/v0/types/unsigned-transaction-bitcoin.d.ts +22 -0
- package/v0/types/unsigned-transaction-bitcoin.js +3 -0
- package/v0/types/unsigned-transaction-bitcoin.js.map +1 -0
- package/v1/block-explorer/BlockCypherBlockExplorer.d.ts +9 -0
- package/v1/block-explorer/BlockCypherBlockExplorer.js +74 -0
- package/v1/block-explorer/BlockCypherBlockExplorer.js.map +1 -0
- package/v1/data/BitcoinAddress.d.ts +7 -0
- package/v1/data/BitcoinAddress.js +20 -0
- package/v1/data/BitcoinAddress.js.map +1 -0
- package/v1/data/BitcoinSegwitAddress.d.ts +8 -0
- package/v1/data/BitcoinSegwitAddress.js +25 -0
- package/v1/data/BitcoinSegwitAddress.js.map +1 -0
- package/v1/index.d.ts +20 -0
- package/v1/index.js +24 -0
- package/v1/index.js.map +1 -0
- package/v1/module/BitcoinModule.d.ts +16 -0
- package/v1/module/BitcoinModule.js +112 -0
- package/v1/module/BitcoinModule.js.map +1 -0
- package/v1/module.d.ts +3 -0
- package/v1/module.js +24 -0
- package/v1/module.js.map +1 -0
- package/v1/protocol/BitcoinCryptoClient.d.ts +12 -0
- package/v1/protocol/BitcoinCryptoClient.js +96 -0
- package/v1/protocol/BitcoinCryptoClient.js.map +1 -0
- package/v1/protocol/BitcoinProtocol.d.ts +72 -0
- package/v1/protocol/BitcoinProtocol.js +1193 -0
- package/v1/protocol/BitcoinProtocol.js.map +1 -0
- package/v1/protocol/BitcoinSegwitProtocol.d.ts +52 -0
- package/v1/protocol/BitcoinSegwitProtocol.js +720 -0
- package/v1/protocol/BitcoinSegwitProtocol.js.map +1 -0
- package/v1/protocol/BitcoinTestnetProtocol.d.ts +12 -0
- package/v1/protocol/BitcoinTestnetProtocol.js +73 -0
- package/v1/protocol/BitcoinTestnetProtocol.js.map +1 -0
- package/v1/serializer/v3/schemas/converter/transaction-converter.d.ts +13 -0
- package/v1/serializer/v3/schemas/converter/transaction-converter.js +75 -0
- package/v1/serializer/v3/schemas/converter/transaction-converter.js.map +1 -0
- package/v1/serializer/v3/schemas/definitions/transaction-sign-request-bitcoin-segwit.d.ts +4 -0
- package/v1/serializer/v3/schemas/definitions/transaction-sign-request-bitcoin-segwit.js +3 -0
- package/v1/serializer/v3/schemas/definitions/transaction-sign-request-bitcoin-segwit.js.map +1 -0
- package/v1/serializer/v3/schemas/definitions/transaction-sign-request-bitcoin.d.ts +4 -0
- package/v1/serializer/v3/schemas/definitions/transaction-sign-request-bitcoin.js +3 -0
- package/v1/serializer/v3/schemas/definitions/transaction-sign-request-bitcoin.js.map +1 -0
- package/v1/serializer/v3/schemas/definitions/transaction-sign-response-bitcoin-segwit.d.ts +3 -0
- package/v1/serializer/v3/schemas/definitions/transaction-sign-response-bitcoin-segwit.js +3 -0
- package/v1/serializer/v3/schemas/definitions/transaction-sign-response-bitcoin-segwit.js.map +1 -0
- package/v1/serializer/v3/schemas/definitions/transaction-sign-response-bitcoin.d.ts +7 -0
- package/v1/serializer/v3/schemas/definitions/transaction-sign-response-bitcoin.js +3 -0
- package/v1/serializer/v3/schemas/definitions/transaction-sign-response-bitcoin.js.map +1 -0
- package/v1/serializer/v3/schemas/generated/transaction-sign-request-bitcoin-segwit.json +29 -0
- package/v1/serializer/v3/schemas/generated/transaction-sign-request-bitcoin.json +79 -0
- package/v1/serializer/v3/schemas/generated/transaction-sign-response-bitcoin-segwit.json +19 -0
- package/v1/serializer/v3/schemas/generated/transaction-sign-response-bitcoin.json +37 -0
- package/v1/serializer/v3/serializer-companion.d.ts +14 -0
- package/v1/serializer/v3/serializer-companion.js +197 -0
- package/v1/serializer/v3/serializer-companion.js.map +1 -0
- package/v1/serializer/v3/validators/transaction-validator.d.ts +7 -0
- package/v1/serializer/v3/validators/transaction-validator.js +56 -0
- package/v1/serializer/v3/validators/transaction-validator.js.map +1 -0
- package/v1/serializer/v3/validators/validators.d.ts +7 -0
- package/v1/serializer/v3/validators/validators.js +238 -0
- package/v1/serializer/v3/validators/validators.js.map +1 -0
- package/v1/types/bitcoinjs.d.ts +14 -0
- package/v1/types/bitcoinjs.js +3 -0
- package/v1/types/bitcoinjs.js.map +1 -0
- package/v1/types/crypto.d.ts +2 -0
- package/v1/types/crypto.js +3 -0
- package/v1/types/crypto.js.map +1 -0
- package/v1/types/indexer.d.ts +81 -0
- package/v1/types/indexer.js +3 -0
- package/v1/types/indexer.js.map +1 -0
- package/v1/types/key.d.ts +6 -0
- package/v1/types/key.js +3 -0
- package/v1/types/key.js.map +1 -0
- package/v1/types/protocol.d.ts +17 -0
- package/v1/types/protocol.js +3 -0
- package/v1/types/protocol.js.map +1 -0
- package/v1/types/transaction.d.ts +39 -0
- package/v1/types/transaction.js +3 -0
- package/v1/types/transaction.js.map +1 -0
- package/v1/utils/common.d.ts +2 -0
- package/v1/utils/common.js +28 -0
- package/v1/utils/common.js.map +1 -0
- package/v1/utils/key.d.ts +34 -0
- package/v1/utils/key.js +190 -0
- package/v1/utils/key.js.map +1 -0
- package/v1/utils/network.d.ts +2 -0
- package/v1/utils/network.js +22 -0
- package/v1/utils/network.js.map +1 -0
- package/v1/utils/signature.d.ts +2 -0
- package/v1/utils/signature.js +34 -0
- package/v1/utils/signature.js.map +1 -0
|
@@ -0,0 +1,1193 @@
|
|
|
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.createBitcoinProtocolOptions = exports.BITCOIN_MAINNET_PROTOCOL_NETWORK = exports.createBitcoinProtocol = exports.BitcoinProtocolImpl = 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 BigInteger = __importStar(require("@airgap/coinlib-core/dependencies/src/bigi-1.4.2"));
|
|
89
|
+
var bignumber_1 = __importDefault(require("@airgap/coinlib-core/dependencies/src/bignumber.js-9.0.0/bignumber"));
|
|
90
|
+
var bitcoinMessage = __importStar(require("@airgap/coinlib-core/dependencies/src/bitcoinjs-message-2.1.1/index"));
|
|
91
|
+
var BitGo = __importStar(require("@airgap/coinlib-core/dependencies/src/bitgo-utxo-lib-5d91049fd7a988382df81c8260e244ee56d57aac/src/index"));
|
|
92
|
+
var errors_1 = require("@airgap/coinlib-core/errors");
|
|
93
|
+
var crypto_1 = require("@airgap/crypto");
|
|
94
|
+
var module_kit_1 = require("@airgap/module-kit");
|
|
95
|
+
var BitcoinAddress_1 = require("../data/BitcoinAddress");
|
|
96
|
+
var common_1 = require("../utils/common");
|
|
97
|
+
var key_1 = require("../utils/key");
|
|
98
|
+
var network_1 = require("../utils/network");
|
|
99
|
+
var signature_1 = require("../utils/signature");
|
|
100
|
+
var BitcoinCryptoClient_1 = require("./BitcoinCryptoClient");
|
|
101
|
+
// Implementation
|
|
102
|
+
var DUST_AMOUNT = 50;
|
|
103
|
+
var BitcoinProtocolImpl = /** @class */ (function () {
|
|
104
|
+
function BitcoinProtocolImpl(options, bitcoinJS, bitcoinJSMessage) {
|
|
105
|
+
if (options === void 0) { options = {}; }
|
|
106
|
+
if (bitcoinJS === void 0) { bitcoinJS = BitGo; }
|
|
107
|
+
if (bitcoinJSMessage === void 0) { bitcoinJSMessage = bitcoinMessage; }
|
|
108
|
+
// Common
|
|
109
|
+
this.units = {
|
|
110
|
+
BTC: {
|
|
111
|
+
symbol: { value: 'BTC', market: 'btc' },
|
|
112
|
+
decimals: 8
|
|
113
|
+
},
|
|
114
|
+
mBTC: {
|
|
115
|
+
symbol: { value: 'mBTC' },
|
|
116
|
+
decimals: 4
|
|
117
|
+
},
|
|
118
|
+
Satoshi: {
|
|
119
|
+
symbol: { value: 'Satoshi' },
|
|
120
|
+
decimals: 0
|
|
121
|
+
}
|
|
122
|
+
};
|
|
123
|
+
this.feeDefaults = {
|
|
124
|
+
low: (0, module_kit_1.newAmount)(0.00002, 'BTC').blockchain(this.units),
|
|
125
|
+
medium: (0, module_kit_1.newAmount)(0.00004, 'BTC').blockchain(this.units),
|
|
126
|
+
high: (0, module_kit_1.newAmount)(0.00005, 'BTC').blockchain(this.units)
|
|
127
|
+
};
|
|
128
|
+
this.metadata = {
|
|
129
|
+
identifier: coinlib_core_1.MainProtocolSymbols.BTC,
|
|
130
|
+
name: 'Bitcoin (Legacy)',
|
|
131
|
+
units: this.units,
|
|
132
|
+
mainUnit: 'BTC',
|
|
133
|
+
fee: {
|
|
134
|
+
defaults: this.feeDefaults
|
|
135
|
+
},
|
|
136
|
+
account: {
|
|
137
|
+
standardDerivationPath: "m/44'/0'/0'",
|
|
138
|
+
address: {
|
|
139
|
+
isCaseSensitive: true,
|
|
140
|
+
placeholder: '1ABC...',
|
|
141
|
+
regex: '^(?:[13]{1}[a-km-zA-HJ-NP-Z1-9]{25,34}|bc1[a-z0-9]{39,59})$'
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
};
|
|
145
|
+
// Offline
|
|
146
|
+
this.cryptoConfiguration = {
|
|
147
|
+
algorithm: 'secp256k1'
|
|
148
|
+
};
|
|
149
|
+
this.options = createBitcoinProtocolOptions(options.network);
|
|
150
|
+
this.bitcoinJS = {
|
|
151
|
+
lib: bitcoinJS,
|
|
152
|
+
message: bitcoinJSMessage,
|
|
153
|
+
config: {
|
|
154
|
+
network: (0, network_1.getBitcoinJSNetwork)(this.options.network, bitcoinJS)
|
|
155
|
+
}
|
|
156
|
+
};
|
|
157
|
+
this.cryptoClient = new BitcoinCryptoClient_1.BitcoinCryptoClient(this, this.bitcoinJS);
|
|
158
|
+
}
|
|
159
|
+
BitcoinProtocolImpl.prototype.getMetadata = function () {
|
|
160
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
161
|
+
return __generator(this, function (_a) {
|
|
162
|
+
return [2 /*return*/, this.metadata];
|
|
163
|
+
});
|
|
164
|
+
});
|
|
165
|
+
};
|
|
166
|
+
BitcoinProtocolImpl.prototype.getAddressFromPublicKey = function (publicKey) {
|
|
167
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
168
|
+
return __generator(this, function (_a) {
|
|
169
|
+
switch (publicKey.type) {
|
|
170
|
+
case 'pub':
|
|
171
|
+
return [2 /*return*/, this.getAddressFromNonExtendedPublicKey(publicKey)];
|
|
172
|
+
case 'xpub':
|
|
173
|
+
return [2 /*return*/, this.getAddressFromExtendedPublicKey(publicKey)];
|
|
174
|
+
default:
|
|
175
|
+
(0, coinlib_core_1.assertNever)(publicKey);
|
|
176
|
+
throw new errors_1.UnsupportedError(coinlib_core_1.Domain.BITCOIN, 'Public key type is not supported.');
|
|
177
|
+
}
|
|
178
|
+
return [2 /*return*/];
|
|
179
|
+
});
|
|
180
|
+
});
|
|
181
|
+
};
|
|
182
|
+
BitcoinProtocolImpl.prototype.getAddressFromNonExtendedPublicKey = function (publicKey) {
|
|
183
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
184
|
+
var hexPublicKey, keyPair;
|
|
185
|
+
return __generator(this, function (_a) {
|
|
186
|
+
hexPublicKey = (0, key_1.convertPublicKey)(publicKey, 'hex');
|
|
187
|
+
keyPair = this.bitcoinJS.lib.ECPair.fromPublicKeyBuffer(Buffer.from(hexPublicKey.value, 'hex'), this.bitcoinJS.config.network);
|
|
188
|
+
return [2 /*return*/, BitcoinAddress_1.BitcoinAddress.fromECPair(keyPair).asString()];
|
|
189
|
+
});
|
|
190
|
+
});
|
|
191
|
+
};
|
|
192
|
+
BitcoinProtocolImpl.prototype.getAddressFromExtendedPublicKey = function (extendedPublicKey) {
|
|
193
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
194
|
+
var encodedExtendedPublicKey, node;
|
|
195
|
+
return __generator(this, function (_a) {
|
|
196
|
+
encodedExtendedPublicKey = this.convertExtendedPublicKey(extendedPublicKey, 'encoded');
|
|
197
|
+
node = this.bitcoinJS.lib.HDNode.fromBase58(encodedExtendedPublicKey.value, this.bitcoinJS.config.network);
|
|
198
|
+
return [2 /*return*/, BitcoinAddress_1.BitcoinAddress.fromHDNode(node).asString()];
|
|
199
|
+
});
|
|
200
|
+
});
|
|
201
|
+
};
|
|
202
|
+
BitcoinProtocolImpl.prototype.deriveFromExtendedPublicKey = function (extendedPublicKey, visibilityIndex, addressIndex) {
|
|
203
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
204
|
+
var encodedExtendedPublicKey, childPublicKey;
|
|
205
|
+
return __generator(this, function (_a) {
|
|
206
|
+
encodedExtendedPublicKey = this.convertExtendedPublicKey(extendedPublicKey, 'encoded');
|
|
207
|
+
childPublicKey = this.bitcoinJS.lib.HDNode.fromBase58(encodedExtendedPublicKey.value, this.bitcoinJS.config.network)
|
|
208
|
+
.derive(visibilityIndex)
|
|
209
|
+
.derive(addressIndex)
|
|
210
|
+
.getPublicKeyBuffer();
|
|
211
|
+
return [2 /*return*/, (0, module_kit_1.newPublicKey)(childPublicKey.toString('hex'), 'hex')];
|
|
212
|
+
});
|
|
213
|
+
});
|
|
214
|
+
};
|
|
215
|
+
BitcoinProtocolImpl.prototype.getDetailsFromTransaction = function (transaction, _publicKey) {
|
|
216
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
217
|
+
return __generator(this, function (_a) {
|
|
218
|
+
switch (transaction.type) {
|
|
219
|
+
case 'signed':
|
|
220
|
+
return [2 /*return*/, this.getDetailsFromSignedTransaction(transaction)];
|
|
221
|
+
case 'unsigned':
|
|
222
|
+
return [2 /*return*/, this.getDetailsFromUnsignedTransaction(transaction)];
|
|
223
|
+
default:
|
|
224
|
+
(0, coinlib_core_1.assertNever)(transaction);
|
|
225
|
+
throw new errors_1.UnsupportedError(coinlib_core_1.Domain.BITCOIN, 'Unsupported transaction type.');
|
|
226
|
+
}
|
|
227
|
+
return [2 /*return*/];
|
|
228
|
+
});
|
|
229
|
+
});
|
|
230
|
+
};
|
|
231
|
+
BitcoinProtocolImpl.prototype.getDetailsFromSignedTransaction = function (transaction) {
|
|
232
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
233
|
+
var tx, bitcoinTx;
|
|
234
|
+
var _this = this;
|
|
235
|
+
return __generator(this, function (_a) {
|
|
236
|
+
tx = {
|
|
237
|
+
from: transaction.from,
|
|
238
|
+
to: [],
|
|
239
|
+
isInbound: false,
|
|
240
|
+
amount: (0, module_kit_1.newAmount)(transaction.amount, 'blockchain'),
|
|
241
|
+
fee: (0, module_kit_1.newAmount)(transaction.fee, 'blockchain'),
|
|
242
|
+
network: this.options.network
|
|
243
|
+
};
|
|
244
|
+
bitcoinTx = this.bitcoinJS.lib.Transaction.fromHex(transaction.transaction);
|
|
245
|
+
bitcoinTx.outs.forEach(function (output) {
|
|
246
|
+
var address = _this.bitcoinJS.lib.address.fromOutputScript(output.script, _this.bitcoinJS.config.network);
|
|
247
|
+
// only works if one output is target and rest is change, but this way we can filter out change addresses
|
|
248
|
+
if (new bignumber_1.default(output.value).isEqualTo(transaction.amount)) {
|
|
249
|
+
tx.to.push(address);
|
|
250
|
+
}
|
|
251
|
+
});
|
|
252
|
+
return [2 /*return*/, [tx]];
|
|
253
|
+
});
|
|
254
|
+
});
|
|
255
|
+
};
|
|
256
|
+
BitcoinProtocolImpl.prototype.getDetailsFromUnsignedTransaction = function (transaction) {
|
|
257
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
258
|
+
var fee, _i, _a, txIn, _b, _c, txOut;
|
|
259
|
+
return __generator(this, function (_d) {
|
|
260
|
+
fee = new bignumber_1.default(0);
|
|
261
|
+
for (_i = 0, _a = transaction.ins; _i < _a.length; _i++) {
|
|
262
|
+
txIn = _a[_i];
|
|
263
|
+
fee = fee.plus(new bignumber_1.default(txIn.value));
|
|
264
|
+
}
|
|
265
|
+
for (_b = 0, _c = transaction.outs; _b < _c.length; _b++) {
|
|
266
|
+
txOut = _c[_b];
|
|
267
|
+
fee = fee.minus(new bignumber_1.default(txOut.value));
|
|
268
|
+
}
|
|
269
|
+
return [2 /*return*/, [
|
|
270
|
+
{
|
|
271
|
+
from: transaction.ins.map(function (obj) { return obj.address; }),
|
|
272
|
+
to: transaction.outs.filter(function (obj) { return !obj.isChange; }).map(function (obj) { return obj.recipient; }),
|
|
273
|
+
isInbound: false,
|
|
274
|
+
amount: (0, module_kit_1.newAmount)(transaction.outs
|
|
275
|
+
.filter(function (obj) { return !obj.isChange; })
|
|
276
|
+
.map(function (obj) { return new bignumber_1.default(obj.value); })
|
|
277
|
+
.reduce(function (accumulator, currentValue) { return accumulator.plus(currentValue); }), 'blockchain'),
|
|
278
|
+
fee: (0, module_kit_1.newAmount)(fee, 'blockchain'),
|
|
279
|
+
network: this.options.network
|
|
280
|
+
}
|
|
281
|
+
]];
|
|
282
|
+
});
|
|
283
|
+
});
|
|
284
|
+
};
|
|
285
|
+
BitcoinProtocolImpl.prototype.verifyMessageWithPublicKey = function (message, signature, publicKey) {
|
|
286
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
287
|
+
var encodedSignature;
|
|
288
|
+
return __generator(this, function (_a) {
|
|
289
|
+
encodedSignature = (0, signature_1.convertSignature)(signature, 'encoded');
|
|
290
|
+
return [2 /*return*/, this.cryptoClient.verifyMessage(message, encodedSignature.value, publicKey.value)];
|
|
291
|
+
});
|
|
292
|
+
});
|
|
293
|
+
};
|
|
294
|
+
BitcoinProtocolImpl.prototype.encryptAsymmetricWithPublicKey = function (payload, publicKey) {
|
|
295
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
296
|
+
var nonExtendedPublicKey, _a, hexNonExtendedPublicKey;
|
|
297
|
+
return __generator(this, function (_b) {
|
|
298
|
+
switch (_b.label) {
|
|
299
|
+
case 0:
|
|
300
|
+
if (!(publicKey.type === 'pub')) return [3 /*break*/, 1];
|
|
301
|
+
_a = publicKey;
|
|
302
|
+
return [3 /*break*/, 3];
|
|
303
|
+
case 1: return [4 /*yield*/, this.deriveFromExtendedPublicKey(publicKey, 0, 0)];
|
|
304
|
+
case 2:
|
|
305
|
+
_a = _b.sent();
|
|
306
|
+
_b.label = 3;
|
|
307
|
+
case 3:
|
|
308
|
+
nonExtendedPublicKey = _a;
|
|
309
|
+
hexNonExtendedPublicKey = (0, key_1.convertPublicKey)(nonExtendedPublicKey, 'hex');
|
|
310
|
+
return [2 /*return*/, this.cryptoClient.encryptAsymmetric(payload, hexNonExtendedPublicKey.value)];
|
|
311
|
+
}
|
|
312
|
+
});
|
|
313
|
+
});
|
|
314
|
+
};
|
|
315
|
+
BitcoinProtocolImpl.prototype.getCryptoConfiguration = function () {
|
|
316
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
317
|
+
return __generator(this, function (_a) {
|
|
318
|
+
return [2 /*return*/, this.cryptoConfiguration];
|
|
319
|
+
});
|
|
320
|
+
});
|
|
321
|
+
};
|
|
322
|
+
BitcoinProtocolImpl.prototype.getKeyPairFromDerivative = function (derivative) {
|
|
323
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
324
|
+
var node;
|
|
325
|
+
return __generator(this, function (_a) {
|
|
326
|
+
node = this.derivativeToBip32Node(derivative);
|
|
327
|
+
return [2 /*return*/, {
|
|
328
|
+
secretKey: (0, module_kit_1.newSecretKey)(node.keyPair.getPrivateKeyBuffer().toString('hex'), 'hex'),
|
|
329
|
+
publicKey: (0, module_kit_1.newPublicKey)(node.keyPair.getPublicKeyBuffer().toString('hex'), 'hex')
|
|
330
|
+
}];
|
|
331
|
+
});
|
|
332
|
+
});
|
|
333
|
+
};
|
|
334
|
+
BitcoinProtocolImpl.prototype.getExtendedKeyPairFromDerivative = function (derivative) {
|
|
335
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
336
|
+
var node;
|
|
337
|
+
return __generator(this, function (_a) {
|
|
338
|
+
node = this.derivativeToBip32Node(derivative);
|
|
339
|
+
return [2 /*return*/, {
|
|
340
|
+
secretKey: (0, module_kit_1.newExtendedSecretKey)(node.toBase58(), 'encoded'),
|
|
341
|
+
publicKey: (0, module_kit_1.newExtendedPublicKey)(node.neutered().toBase58(), 'encoded')
|
|
342
|
+
}];
|
|
343
|
+
});
|
|
344
|
+
});
|
|
345
|
+
};
|
|
346
|
+
BitcoinProtocolImpl.prototype.deriveFromExtendedSecretKey = function (extendedSecretKey, visibilityIndex, addressIndex) {
|
|
347
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
348
|
+
var encodedExtendedSecretKey, childSecretKey;
|
|
349
|
+
return __generator(this, function (_a) {
|
|
350
|
+
encodedExtendedSecretKey = this.convertExtendedSecretKey(extendedSecretKey, 'encoded');
|
|
351
|
+
childSecretKey = this.bitcoinJS.lib.HDNode.fromBase58(encodedExtendedSecretKey.value, this.bitcoinJS.config.network)
|
|
352
|
+
.derive(visibilityIndex)
|
|
353
|
+
.derive(addressIndex)
|
|
354
|
+
.getPrivateKeyBuffer();
|
|
355
|
+
return [2 /*return*/, (0, module_kit_1.newSecretKey)(childSecretKey.toString('hex'), 'hex')];
|
|
356
|
+
});
|
|
357
|
+
});
|
|
358
|
+
};
|
|
359
|
+
BitcoinProtocolImpl.prototype.signTransactionWithSecretKey = function (transaction, secretKey) {
|
|
360
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
361
|
+
return __generator(this, function (_a) {
|
|
362
|
+
switch (secretKey.type) {
|
|
363
|
+
case 'priv':
|
|
364
|
+
return [2 /*return*/, this.signTransactionWithNonExtendedSecretKey(transaction, secretKey)];
|
|
365
|
+
case 'xpriv':
|
|
366
|
+
return [2 /*return*/, this.signTransactionWithExtendedSecretKey(transaction, secretKey)];
|
|
367
|
+
default:
|
|
368
|
+
(0, coinlib_core_1.assertNever)(secretKey);
|
|
369
|
+
throw new errors_1.UnsupportedError(coinlib_core_1.Domain.BITCOIN, 'Secret key type not supported.');
|
|
370
|
+
}
|
|
371
|
+
return [2 /*return*/];
|
|
372
|
+
});
|
|
373
|
+
});
|
|
374
|
+
};
|
|
375
|
+
BitcoinProtocolImpl.prototype.signTransactionWithNonExtendedSecretKey = function (transaction, secretKey) {
|
|
376
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
377
|
+
var hexSecretKey, transactionBuilder, _i, _a, input, _b, _c, output, bufferSecretKey, keyPair, publicKey, generatedChangeAddress, i;
|
|
378
|
+
return __generator(this, function (_d) {
|
|
379
|
+
switch (_d.label) {
|
|
380
|
+
case 0:
|
|
381
|
+
hexSecretKey = (0, key_1.convertSecretKey)(secretKey, 'hex');
|
|
382
|
+
transactionBuilder = new this.bitcoinJS.lib.TransactionBuilder(this.bitcoinJS.config.network);
|
|
383
|
+
for (_i = 0, _a = transaction.ins; _i < _a.length; _i++) {
|
|
384
|
+
input = _a[_i];
|
|
385
|
+
transactionBuilder.addInput(input.txId, input.vout);
|
|
386
|
+
}
|
|
387
|
+
_b = 0, _c = transaction.outs;
|
|
388
|
+
_d.label = 1;
|
|
389
|
+
case 1:
|
|
390
|
+
if (!(_b < _c.length)) return [3 /*break*/, 5];
|
|
391
|
+
output = _c[_b];
|
|
392
|
+
if (!output.isChange) return [3 /*break*/, 3];
|
|
393
|
+
bufferSecretKey = Buffer.from(secretKey.value, 'hex');
|
|
394
|
+
keyPair = this.bitcoinJS.lib.ECPair(BigInteger.fromBuffer(bufferSecretKey), null, {
|
|
395
|
+
network: this.bitcoinJS.config.network
|
|
396
|
+
});
|
|
397
|
+
publicKey = (0, module_kit_1.newPublicKey)(keyPair.getPublicKeyBuffer().toString('hex'), 'hex');
|
|
398
|
+
return [4 /*yield*/, this.getAddressFromPublicKey(publicKey)];
|
|
399
|
+
case 2:
|
|
400
|
+
generatedChangeAddress = _d.sent();
|
|
401
|
+
if (generatedChangeAddress !== output.recipient) {
|
|
402
|
+
throw new errors_1.ConditionViolationError(coinlib_core_1.Domain.BITCOIN, 'Change address could not be verified.');
|
|
403
|
+
}
|
|
404
|
+
_d.label = 3;
|
|
405
|
+
case 3:
|
|
406
|
+
transactionBuilder.addOutput(output.recipient, new bignumber_1.default(output.value).toNumber());
|
|
407
|
+
_d.label = 4;
|
|
408
|
+
case 4:
|
|
409
|
+
_b++;
|
|
410
|
+
return [3 /*break*/, 1];
|
|
411
|
+
case 5:
|
|
412
|
+
for (i = 0; i < transaction.ins.length; i++) {
|
|
413
|
+
transactionBuilder.sign(i, Buffer.from(hexSecretKey.value, 'hex'));
|
|
414
|
+
}
|
|
415
|
+
return [2 /*return*/, this.createSignedTransaction(transaction, transactionBuilder)];
|
|
416
|
+
}
|
|
417
|
+
});
|
|
418
|
+
});
|
|
419
|
+
};
|
|
420
|
+
BitcoinProtocolImpl.prototype.signTransactionWithExtendedSecretKey = function (transaction, extendedSecretKey) {
|
|
421
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
422
|
+
var encodedExtendedSecretKey, transactionBuilder, node, _i, _a, input, changeAddressBatchSize, changeAddressMaxAddresses, _loop_1, this_1, _b, _c, output, i;
|
|
423
|
+
var _this = this;
|
|
424
|
+
return __generator(this, function (_d) {
|
|
425
|
+
switch (_d.label) {
|
|
426
|
+
case 0:
|
|
427
|
+
encodedExtendedSecretKey = this.convertExtendedSecretKey(extendedSecretKey, 'encoded');
|
|
428
|
+
transactionBuilder = new this.bitcoinJS.lib.TransactionBuilder(this.bitcoinJS.config.network);
|
|
429
|
+
node = this.bitcoinJS.lib.HDNode.fromBase58(encodedExtendedSecretKey.value, this.bitcoinJS.config.network);
|
|
430
|
+
for (_i = 0, _a = transaction.ins; _i < _a.length; _i++) {
|
|
431
|
+
input = _a[_i];
|
|
432
|
+
transactionBuilder.addInput(input.txId, input.vout);
|
|
433
|
+
}
|
|
434
|
+
changeAddressBatchSize = 10;
|
|
435
|
+
changeAddressMaxAddresses = 500;
|
|
436
|
+
_loop_1 = function (output) {
|
|
437
|
+
var changeAddressIsValid, extendedPublicKey_1, derivedPublicKey, generatedChangeAddress, _loop_2, out_x_1, x;
|
|
438
|
+
return __generator(this, function (_e) {
|
|
439
|
+
switch (_e.label) {
|
|
440
|
+
case 0:
|
|
441
|
+
changeAddressIsValid = false;
|
|
442
|
+
if (!output.isChange) return [3 /*break*/, 8];
|
|
443
|
+
extendedPublicKey_1 = (0, module_kit_1.newExtendedPublicKey)(node.neutered().toBase58(), 'encoded');
|
|
444
|
+
if (!output.derivationPath) return [3 /*break*/, 3];
|
|
445
|
+
return [4 /*yield*/, this_1.deriveFromExtendedPublicKey(extendedPublicKey_1, 1, parseInt(output.derivationPath, 10))];
|
|
446
|
+
case 1:
|
|
447
|
+
derivedPublicKey = _e.sent();
|
|
448
|
+
return [4 /*yield*/, this_1.getAddressFromPublicKey(derivedPublicKey)];
|
|
449
|
+
case 2:
|
|
450
|
+
generatedChangeAddress = _e.sent();
|
|
451
|
+
changeAddressIsValid = generatedChangeAddress === output.recipient;
|
|
452
|
+
return [3 /*break*/, 7];
|
|
453
|
+
case 3:
|
|
454
|
+
_loop_2 = function (x) {
|
|
455
|
+
var derivedPublicKeys, addresses;
|
|
456
|
+
return __generator(this, function (_f) {
|
|
457
|
+
switch (_f.label) {
|
|
458
|
+
case 0: return [4 /*yield*/, Promise.all(Array.from(new Array(changeAddressBatchSize)).map(function () { return __awaiter(_this, void 0, void 0, function () {
|
|
459
|
+
return __generator(this, function (_a) {
|
|
460
|
+
return [2 /*return*/, this.deriveFromExtendedPublicKey(extendedPublicKey_1, 1, x)];
|
|
461
|
+
});
|
|
462
|
+
}); }))];
|
|
463
|
+
case 1:
|
|
464
|
+
derivedPublicKeys = _f.sent();
|
|
465
|
+
return [4 /*yield*/, Promise.all(derivedPublicKeys.map(function (publicKey) {
|
|
466
|
+
return _this.getAddressFromPublicKey(publicKey);
|
|
467
|
+
}))];
|
|
468
|
+
case 2:
|
|
469
|
+
addresses = _f.sent();
|
|
470
|
+
if (addresses.indexOf(output.recipient) >= 0) {
|
|
471
|
+
changeAddressIsValid = true;
|
|
472
|
+
x = changeAddressMaxAddresses;
|
|
473
|
+
}
|
|
474
|
+
out_x_1 = x;
|
|
475
|
+
return [2 /*return*/];
|
|
476
|
+
}
|
|
477
|
+
});
|
|
478
|
+
};
|
|
479
|
+
x = 0;
|
|
480
|
+
_e.label = 4;
|
|
481
|
+
case 4:
|
|
482
|
+
if (!(x < changeAddressMaxAddresses)) return [3 /*break*/, 7];
|
|
483
|
+
return [5 /*yield**/, _loop_2(x)];
|
|
484
|
+
case 5:
|
|
485
|
+
_e.sent();
|
|
486
|
+
x = out_x_1;
|
|
487
|
+
_e.label = 6;
|
|
488
|
+
case 6:
|
|
489
|
+
x += changeAddressBatchSize;
|
|
490
|
+
return [3 /*break*/, 4];
|
|
491
|
+
case 7:
|
|
492
|
+
if (!changeAddressIsValid) {
|
|
493
|
+
throw new errors_1.InvalidValueError(coinlib_core_1.Domain.BITCOIN, 'Change address could not be verified.');
|
|
494
|
+
}
|
|
495
|
+
_e.label = 8;
|
|
496
|
+
case 8:
|
|
497
|
+
transactionBuilder.addOutput(output.recipient, new bignumber_1.default(output.value).toNumber());
|
|
498
|
+
return [2 /*return*/];
|
|
499
|
+
}
|
|
500
|
+
});
|
|
501
|
+
};
|
|
502
|
+
this_1 = this;
|
|
503
|
+
_b = 0, _c = transaction.outs;
|
|
504
|
+
_d.label = 1;
|
|
505
|
+
case 1:
|
|
506
|
+
if (!(_b < _c.length)) return [3 /*break*/, 4];
|
|
507
|
+
output = _c[_b];
|
|
508
|
+
return [5 /*yield**/, _loop_1(output)];
|
|
509
|
+
case 2:
|
|
510
|
+
_d.sent();
|
|
511
|
+
_d.label = 3;
|
|
512
|
+
case 3:
|
|
513
|
+
_b++;
|
|
514
|
+
return [3 /*break*/, 1];
|
|
515
|
+
case 4:
|
|
516
|
+
for (i = 0; i < transaction.ins.length; i++) {
|
|
517
|
+
transactionBuilder.sign(i, node.derivePath(transaction.ins[i].derivationPath));
|
|
518
|
+
}
|
|
519
|
+
return [2 /*return*/, this.createSignedTransaction(transaction, transactionBuilder)];
|
|
520
|
+
}
|
|
521
|
+
});
|
|
522
|
+
});
|
|
523
|
+
};
|
|
524
|
+
BitcoinProtocolImpl.prototype.createSignedTransaction = function (unsignedTransaction, transactionBuilder) {
|
|
525
|
+
var fee = new bignumber_1.default(0);
|
|
526
|
+
for (var _i = 0, _a = unsignedTransaction.ins; _i < _a.length; _i++) {
|
|
527
|
+
var txIn = _a[_i];
|
|
528
|
+
fee = fee.plus(new bignumber_1.default(txIn.value));
|
|
529
|
+
}
|
|
530
|
+
for (var _b = 0, _c = unsignedTransaction.outs; _b < _c.length; _b++) {
|
|
531
|
+
var txOut = _c[_b];
|
|
532
|
+
fee = fee.minus(new bignumber_1.default(txOut.value));
|
|
533
|
+
}
|
|
534
|
+
return (0, module_kit_1.newSignedTransaction)({
|
|
535
|
+
from: unsignedTransaction.ins.map(function (obj) { return obj.address; }),
|
|
536
|
+
to: unsignedTransaction.outs.filter(function (obj) { return !obj.isChange; }).map(function (obj) { return obj.recipient; }),
|
|
537
|
+
amount: unsignedTransaction.outs
|
|
538
|
+
.filter(function (obj) { return !obj.isChange; })
|
|
539
|
+
.map(function (obj) { return new bignumber_1.default(obj.value); })
|
|
540
|
+
.reduce(function (accumulator, currentValue) { return accumulator.plus(currentValue); })
|
|
541
|
+
.toString(10),
|
|
542
|
+
fee: fee.toString(10),
|
|
543
|
+
transaction: transactionBuilder.build().toHex()
|
|
544
|
+
});
|
|
545
|
+
};
|
|
546
|
+
BitcoinProtocolImpl.prototype.signMessageWithKeyPair = function (message, keyPair) {
|
|
547
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
548
|
+
var hexSecretKey, signature;
|
|
549
|
+
return __generator(this, function (_a) {
|
|
550
|
+
switch (_a.label) {
|
|
551
|
+
case 0:
|
|
552
|
+
hexSecretKey = keyPair.secretKey.type === 'priv'
|
|
553
|
+
? (0, key_1.convertSecretKey)(keyPair.secretKey, 'hex')
|
|
554
|
+
: (0, key_1.convertExtendedSecretKey)(keyPair.secretKey, { format: 'hex' });
|
|
555
|
+
return [4 /*yield*/, this.cryptoClient.signMessage(message, { privateKey: hexSecretKey.value })];
|
|
556
|
+
case 1:
|
|
557
|
+
signature = _a.sent();
|
|
558
|
+
return [2 /*return*/, (0, module_kit_1.newSignature)(signature, 'encoded')];
|
|
559
|
+
}
|
|
560
|
+
});
|
|
561
|
+
});
|
|
562
|
+
};
|
|
563
|
+
BitcoinProtocolImpl.prototype.decryptAsymmetricWithKeyPair = function (payload, keyPair) {
|
|
564
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
565
|
+
var hexSecretKey, encodedExtendedSecretKey, node, derivedNode;
|
|
566
|
+
return __generator(this, function (_a) {
|
|
567
|
+
hexSecretKey = undefined;
|
|
568
|
+
if (keyPair.secretKey.type === 'priv') {
|
|
569
|
+
hexSecretKey = (0, key_1.convertSecretKey)(keyPair.secretKey, 'hex');
|
|
570
|
+
}
|
|
571
|
+
else {
|
|
572
|
+
encodedExtendedSecretKey = this.convertExtendedSecretKey(keyPair.secretKey, 'encoded');
|
|
573
|
+
node = this.bitcoinJS.lib.HDNode.fromBase58(encodedExtendedSecretKey.value, this.bitcoinJS.config.network);
|
|
574
|
+
derivedNode = node.derive(0).derive(0);
|
|
575
|
+
hexSecretKey = (0, module_kit_1.newSecretKey)(derivedNode.keyPair.getPrivateKeyBuffer(), 'hex');
|
|
576
|
+
}
|
|
577
|
+
return [2 /*return*/, this.cryptoClient.decryptAsymmetric(payload, { publicKey: '', privateKey: hexSecretKey.value })];
|
|
578
|
+
});
|
|
579
|
+
});
|
|
580
|
+
};
|
|
581
|
+
BitcoinProtocolImpl.prototype.encryptAESWithSecretKey = function (payload, secretKey) {
|
|
582
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
583
|
+
return __generator(this, function (_a) {
|
|
584
|
+
return [2 /*return*/, this.cryptoClient.encryptAES(payload, secretKey.value)];
|
|
585
|
+
});
|
|
586
|
+
});
|
|
587
|
+
};
|
|
588
|
+
BitcoinProtocolImpl.prototype.decryptAESWithSecretKey = function (payload, secretKey) {
|
|
589
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
590
|
+
return __generator(this, function (_a) {
|
|
591
|
+
return [2 /*return*/, this.cryptoClient.decryptAES(payload, secretKey.value)];
|
|
592
|
+
});
|
|
593
|
+
});
|
|
594
|
+
};
|
|
595
|
+
// Online
|
|
596
|
+
BitcoinProtocolImpl.prototype.getNetwork = function () {
|
|
597
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
598
|
+
return __generator(this, function (_a) {
|
|
599
|
+
return [2 /*return*/, this.options.network];
|
|
600
|
+
});
|
|
601
|
+
});
|
|
602
|
+
};
|
|
603
|
+
BitcoinProtocolImpl.prototype.getTransactionsForPublicKey = function (publicKey, limit, cursor) {
|
|
604
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
605
|
+
return __generator(this, function (_a) {
|
|
606
|
+
switch (publicKey.type) {
|
|
607
|
+
case 'pub':
|
|
608
|
+
return [2 /*return*/, this.getTransactionsForNonExtendedPublicKey(publicKey, limit, cursor)];
|
|
609
|
+
case 'xpub':
|
|
610
|
+
return [2 /*return*/, this.getTransactionsForExtendedPublicKey(publicKey, limit, cursor)];
|
|
611
|
+
default:
|
|
612
|
+
(0, coinlib_core_1.assertNever)(publicKey);
|
|
613
|
+
throw new errors_1.UnsupportedError(coinlib_core_1.Domain.BITCOIN, 'Public key type not supported');
|
|
614
|
+
}
|
|
615
|
+
return [2 /*return*/];
|
|
616
|
+
});
|
|
617
|
+
});
|
|
618
|
+
};
|
|
619
|
+
BitcoinProtocolImpl.prototype.getTransactionsForNonExtendedPublicKey = function (publicKey, limit, cursor) {
|
|
620
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
621
|
+
var address;
|
|
622
|
+
return __generator(this, function (_a) {
|
|
623
|
+
switch (_a.label) {
|
|
624
|
+
case 0: return [4 /*yield*/, this.getAddressFromPublicKey(publicKey)];
|
|
625
|
+
case 1:
|
|
626
|
+
address = _a.sent();
|
|
627
|
+
return [2 /*return*/, this.getTransactionsForAddresses([address], limit, cursor)];
|
|
628
|
+
}
|
|
629
|
+
});
|
|
630
|
+
});
|
|
631
|
+
};
|
|
632
|
+
BitcoinProtocolImpl.prototype.getTransactionsForExtendedPublicKey = function (extendedPublicKey, limit, cursor) {
|
|
633
|
+
var _a;
|
|
634
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
635
|
+
var encodedExtendedPublicKey, page, url, data, ourAddresses, airGapTransactions, _i, _b, transaction, tempAirGapTransactionFrom, tempAirGapTransactionTo, tempAirGapTransactionIsInbound, amount, _c, _d, vin, _e, _f, vout, airGapTransaction, hasNext;
|
|
636
|
+
return __generator(this, function (_g) {
|
|
637
|
+
switch (_g.label) {
|
|
638
|
+
case 0:
|
|
639
|
+
encodedExtendedPublicKey = this.convertExtendedPublicKey(extendedPublicKey, 'encoded');
|
|
640
|
+
page = (_a = cursor === null || cursor === void 0 ? void 0 : cursor.page) !== null && _a !== void 0 ? _a : 1;
|
|
641
|
+
url = "".concat(this.options.network.indexerApi, "/api/v2/xpub/").concat(encodedExtendedPublicKey.value, "?details=txs&tokens=used&pageSize=").concat(limit, "&page=").concat(page);
|
|
642
|
+
return [4 /*yield*/, index_1.default.get(url, {
|
|
643
|
+
responseType: 'json'
|
|
644
|
+
})];
|
|
645
|
+
case 1:
|
|
646
|
+
data = (_g.sent()).data;
|
|
647
|
+
ourAddresses = (data.tokens || []).filter(function (token) { return token.type === 'XPUBAddress'; }).map(function (token) { return token.name; });
|
|
648
|
+
airGapTransactions = [];
|
|
649
|
+
if (data.page === page) {
|
|
650
|
+
for (_i = 0, _b = data.transactions || []; _i < _b.length; _i++) {
|
|
651
|
+
transaction = _b[_i];
|
|
652
|
+
tempAirGapTransactionFrom = [];
|
|
653
|
+
tempAirGapTransactionTo = [];
|
|
654
|
+
tempAirGapTransactionIsInbound = true;
|
|
655
|
+
amount = new bignumber_1.default(0);
|
|
656
|
+
for (_c = 0, _d = transaction.vin; _c < _d.length; _c++) {
|
|
657
|
+
vin = _d[_c];
|
|
658
|
+
if ((0, common_1.containsSome)(vin.addresses, ourAddresses)) {
|
|
659
|
+
tempAirGapTransactionIsInbound = false;
|
|
660
|
+
}
|
|
661
|
+
tempAirGapTransactionFrom.push.apply(tempAirGapTransactionFrom, vin.addresses);
|
|
662
|
+
amount = amount.plus(vin.value);
|
|
663
|
+
}
|
|
664
|
+
for (_e = 0, _f = transaction.vout; _e < _f.length; _e++) {
|
|
665
|
+
vout = _f[_e];
|
|
666
|
+
if (vout.addresses) {
|
|
667
|
+
tempAirGapTransactionTo.push.apply(tempAirGapTransactionTo, vout.addresses);
|
|
668
|
+
// If receiving address is our address, and transaction is outbound => our change
|
|
669
|
+
if ((0, common_1.containsSome)(vout.addresses, ourAddresses) && !tempAirGapTransactionIsInbound) {
|
|
670
|
+
// remove only if related to this address
|
|
671
|
+
amount = amount.minus(vout.value);
|
|
672
|
+
}
|
|
673
|
+
// If receiving address is not ours, and transaction isbound => senders change
|
|
674
|
+
if (!(0, common_1.containsSome)(vout.addresses, ourAddresses) && tempAirGapTransactionIsInbound) {
|
|
675
|
+
amount = amount.minus(vout.value);
|
|
676
|
+
}
|
|
677
|
+
}
|
|
678
|
+
}
|
|
679
|
+
// deduct fee from amount
|
|
680
|
+
amount = amount.minus(transaction.fees);
|
|
681
|
+
airGapTransaction = {
|
|
682
|
+
from: tempAirGapTransactionFrom,
|
|
683
|
+
to: tempAirGapTransactionTo,
|
|
684
|
+
isInbound: tempAirGapTransactionIsInbound,
|
|
685
|
+
amount: (0, module_kit_1.newAmount)(amount, 'blockchain'),
|
|
686
|
+
fee: (0, module_kit_1.newAmount)(transaction.fees, 'blockchain'),
|
|
687
|
+
status: {
|
|
688
|
+
type: 'applied',
|
|
689
|
+
hash: transaction.txid,
|
|
690
|
+
block: transaction.blockHeight.toString()
|
|
691
|
+
},
|
|
692
|
+
network: this.options.network,
|
|
693
|
+
timestamp: transaction.blockTime
|
|
694
|
+
};
|
|
695
|
+
airGapTransactions.push(airGapTransaction);
|
|
696
|
+
}
|
|
697
|
+
}
|
|
698
|
+
hasNext = page < data.totalPages;
|
|
699
|
+
return [2 /*return*/, {
|
|
700
|
+
transactions: airGapTransactions,
|
|
701
|
+
cursor: {
|
|
702
|
+
hasNext: hasNext,
|
|
703
|
+
page: hasNext ? page + 1 : undefined
|
|
704
|
+
}
|
|
705
|
+
}];
|
|
706
|
+
}
|
|
707
|
+
});
|
|
708
|
+
});
|
|
709
|
+
};
|
|
710
|
+
BitcoinProtocolImpl.prototype.getTransactionsForAddress = function (address, limit, cursor) {
|
|
711
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
712
|
+
return __generator(this, function (_a) {
|
|
713
|
+
return [2 /*return*/, this.getTransactionsForAddresses([address], limit, cursor)];
|
|
714
|
+
});
|
|
715
|
+
});
|
|
716
|
+
};
|
|
717
|
+
BitcoinProtocolImpl.prototype.getTransactionsForAddresses = function (addresses, limit, cursor) {
|
|
718
|
+
var _a;
|
|
719
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
720
|
+
var airGapTransactions, page, url, data, _i, _b, transaction, tempAirGapTransactionFrom, tempAirGapTransactionTo, tempAirGapTransactionIsInbound, amount, _c, _d, vin, _e, _f, vout, airGapTransaction, hasNext;
|
|
721
|
+
return __generator(this, function (_g) {
|
|
722
|
+
switch (_g.label) {
|
|
723
|
+
case 0:
|
|
724
|
+
airGapTransactions = [];
|
|
725
|
+
page = (_a = cursor === null || cursor === void 0 ? void 0 : cursor.page) !== null && _a !== void 0 ? _a : 1;
|
|
726
|
+
url = "".concat(this.options.network.indexerApi, "/api/v2/address/").concat(addresses[0], "?page=").concat(page, "&pageSize=").concat(limit, "&details=txs");
|
|
727
|
+
return [4 /*yield*/, index_1.default.get(url, {
|
|
728
|
+
responseType: 'json'
|
|
729
|
+
})];
|
|
730
|
+
case 1:
|
|
731
|
+
data = (_g.sent()).data;
|
|
732
|
+
if (data.page == page) {
|
|
733
|
+
for (_i = 0, _b = data.transactions || []; _i < _b.length; _i++) {
|
|
734
|
+
transaction = _b[_i];
|
|
735
|
+
tempAirGapTransactionFrom = [];
|
|
736
|
+
tempAirGapTransactionTo = [];
|
|
737
|
+
tempAirGapTransactionIsInbound = true;
|
|
738
|
+
amount = new bignumber_1.default(0);
|
|
739
|
+
for (_c = 0, _d = transaction.vin; _c < _d.length; _c++) {
|
|
740
|
+
vin = _d[_c];
|
|
741
|
+
if (vin.addresses && (0, common_1.containsSome)(vin.addresses, addresses)) {
|
|
742
|
+
tempAirGapTransactionIsInbound = false;
|
|
743
|
+
}
|
|
744
|
+
tempAirGapTransactionFrom.push.apply(tempAirGapTransactionFrom, vin.addresses);
|
|
745
|
+
amount = vin.value ? amount.plus(vin.value) : amount;
|
|
746
|
+
}
|
|
747
|
+
for (_e = 0, _f = transaction.vout; _e < _f.length; _e++) {
|
|
748
|
+
vout = _f[_e];
|
|
749
|
+
if (vout.addresses) {
|
|
750
|
+
tempAirGapTransactionTo.push.apply(tempAirGapTransactionTo, vout.addresses);
|
|
751
|
+
// If receiving address is our address, and transaction is outbound => our change
|
|
752
|
+
if ((0, common_1.containsSome)(vout.addresses, addresses) && !tempAirGapTransactionIsInbound) {
|
|
753
|
+
// remove only if related to this address
|
|
754
|
+
amount = amount.minus(new bignumber_1.default(vout.value));
|
|
755
|
+
}
|
|
756
|
+
// If receiving address is not ours, and transaction isbound => senders change
|
|
757
|
+
if (!(0, common_1.containsSome)(vout.addresses, addresses) && tempAirGapTransactionIsInbound) {
|
|
758
|
+
amount = amount.minus(new bignumber_1.default(vout.value));
|
|
759
|
+
}
|
|
760
|
+
}
|
|
761
|
+
}
|
|
762
|
+
// deduct fee from amount
|
|
763
|
+
amount = amount.minus(new bignumber_1.default(transaction.fees));
|
|
764
|
+
airGapTransaction = {
|
|
765
|
+
from: tempAirGapTransactionFrom,
|
|
766
|
+
to: tempAirGapTransactionTo,
|
|
767
|
+
isInbound: tempAirGapTransactionIsInbound,
|
|
768
|
+
amount: (0, module_kit_1.newAmount)(amount, 'blockchain'),
|
|
769
|
+
fee: (0, module_kit_1.newAmount)(transaction.fees, 'blockchain'),
|
|
770
|
+
status: {
|
|
771
|
+
type: 'applied',
|
|
772
|
+
hash: transaction.txid,
|
|
773
|
+
block: transaction.blockHeight.toString()
|
|
774
|
+
},
|
|
775
|
+
network: this.options.network,
|
|
776
|
+
timestamp: transaction.blockTime
|
|
777
|
+
};
|
|
778
|
+
airGapTransactions.push(airGapTransaction);
|
|
779
|
+
}
|
|
780
|
+
}
|
|
781
|
+
hasNext = page < data.totalPages;
|
|
782
|
+
return [2 /*return*/, {
|
|
783
|
+
transactions: airGapTransactions,
|
|
784
|
+
cursor: {
|
|
785
|
+
hasNext: hasNext,
|
|
786
|
+
page: hasNext ? page + 1 : undefined
|
|
787
|
+
}
|
|
788
|
+
}];
|
|
789
|
+
}
|
|
790
|
+
});
|
|
791
|
+
});
|
|
792
|
+
};
|
|
793
|
+
BitcoinProtocolImpl.prototype.getBalanceOfPublicKey = function (publicKey) {
|
|
794
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
795
|
+
return __generator(this, function (_a) {
|
|
796
|
+
switch (publicKey.type) {
|
|
797
|
+
case 'pub':
|
|
798
|
+
return [2 /*return*/, this.getBalanceOfNonExtendedPublicKey(publicKey)];
|
|
799
|
+
case 'xpub':
|
|
800
|
+
return [2 /*return*/, this.getBalanceOfExtendedPublicKey(publicKey)];
|
|
801
|
+
default:
|
|
802
|
+
(0, coinlib_core_1.assertNever)(publicKey);
|
|
803
|
+
throw new errors_1.UnsupportedError(coinlib_core_1.Domain.BITCOIN, 'Unsupported public key type.');
|
|
804
|
+
}
|
|
805
|
+
return [2 /*return*/];
|
|
806
|
+
});
|
|
807
|
+
});
|
|
808
|
+
};
|
|
809
|
+
BitcoinProtocolImpl.prototype.getBalanceOfNonExtendedPublicKey = function (publicKey) {
|
|
810
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
811
|
+
var address;
|
|
812
|
+
return __generator(this, function (_a) {
|
|
813
|
+
switch (_a.label) {
|
|
814
|
+
case 0: return [4 /*yield*/, this.getAddressFromPublicKey(publicKey)];
|
|
815
|
+
case 1:
|
|
816
|
+
address = _a.sent();
|
|
817
|
+
return [2 /*return*/, this.getBalanceOfAddresses([address])];
|
|
818
|
+
}
|
|
819
|
+
});
|
|
820
|
+
});
|
|
821
|
+
};
|
|
822
|
+
BitcoinProtocolImpl.prototype.getBalanceOfExtendedPublicKey = function (extendedPublicKey) {
|
|
823
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
824
|
+
var encodedExtendedPublicKey, data;
|
|
825
|
+
return __generator(this, function (_a) {
|
|
826
|
+
switch (_a.label) {
|
|
827
|
+
case 0:
|
|
828
|
+
encodedExtendedPublicKey = this.convertExtendedPublicKey(extendedPublicKey, 'encoded');
|
|
829
|
+
return [4 /*yield*/, index_1.default.get("".concat(this.options.network.indexerApi, "/api/v2/xpub/").concat(encodedExtendedPublicKey.value, "?pageSize=1"), {
|
|
830
|
+
responseType: 'json'
|
|
831
|
+
})];
|
|
832
|
+
case 1:
|
|
833
|
+
data = (_a.sent()).data;
|
|
834
|
+
return [2 /*return*/, {
|
|
835
|
+
total: (0, module_kit_1.newAmount)(data.balance, 'blockchain')
|
|
836
|
+
}];
|
|
837
|
+
}
|
|
838
|
+
});
|
|
839
|
+
});
|
|
840
|
+
};
|
|
841
|
+
BitcoinProtocolImpl.prototype.getBalanceOfAddress = function (address) {
|
|
842
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
843
|
+
return __generator(this, function (_a) {
|
|
844
|
+
return [2 /*return*/, this.getBalanceOfAddresses([address])];
|
|
845
|
+
});
|
|
846
|
+
});
|
|
847
|
+
};
|
|
848
|
+
BitcoinProtocolImpl.prototype.getBalanceOfAddresses = function (addresses) {
|
|
849
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
850
|
+
var valueAccumulator, _i, addresses_1, address, data;
|
|
851
|
+
return __generator(this, function (_a) {
|
|
852
|
+
switch (_a.label) {
|
|
853
|
+
case 0:
|
|
854
|
+
valueAccumulator = new bignumber_1.default(0);
|
|
855
|
+
_i = 0, addresses_1 = addresses;
|
|
856
|
+
_a.label = 1;
|
|
857
|
+
case 1:
|
|
858
|
+
if (!(_i < addresses_1.length)) return [3 /*break*/, 4];
|
|
859
|
+
address = addresses_1[_i];
|
|
860
|
+
return [4 /*yield*/, index_1.default.get("".concat(this.options.network.indexerApi, "/api/v2/address/").concat(address, "?details=basic"), {
|
|
861
|
+
responseType: 'json'
|
|
862
|
+
})];
|
|
863
|
+
case 2:
|
|
864
|
+
data = (_a.sent()).data;
|
|
865
|
+
valueAccumulator = valueAccumulator.plus(new bignumber_1.default(data.balance));
|
|
866
|
+
_a.label = 3;
|
|
867
|
+
case 3:
|
|
868
|
+
_i++;
|
|
869
|
+
return [3 /*break*/, 1];
|
|
870
|
+
case 4: return [2 /*return*/, {
|
|
871
|
+
total: (0, module_kit_1.newAmount)(valueAccumulator, 'blockchain')
|
|
872
|
+
}];
|
|
873
|
+
}
|
|
874
|
+
});
|
|
875
|
+
});
|
|
876
|
+
};
|
|
877
|
+
BitcoinProtocolImpl.prototype.getTransactionMaxAmountWithPublicKey = function (publicKey, to, configuration) {
|
|
878
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
879
|
+
return __generator(this, function (_a) {
|
|
880
|
+
switch (_a.label) {
|
|
881
|
+
case 0: return [4 /*yield*/, this.getBalanceOfPublicKey(publicKey)];
|
|
882
|
+
case 1: return [2 /*return*/, (_a.sent()).total];
|
|
883
|
+
}
|
|
884
|
+
});
|
|
885
|
+
});
|
|
886
|
+
};
|
|
887
|
+
BitcoinProtocolImpl.prototype.getTransactionFeeWithPublicKey = function (publicKey, details) {
|
|
888
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
889
|
+
var result, estimatedFee, feeStepFactor, mediumFee, lowFee, highFee;
|
|
890
|
+
return __generator(this, function (_a) {
|
|
891
|
+
switch (_a.label) {
|
|
892
|
+
case 0: return [4 /*yield*/, index_1.default.get("".concat(this.options.network.indexerApi, "/api/v2/estimatefee/5"))];
|
|
893
|
+
case 1:
|
|
894
|
+
result = (_a.sent()).data.result;
|
|
895
|
+
estimatedFee = new bignumber_1.default((0, module_kit_1.newAmount)(result, 'BTC').blockchain(this.units).value);
|
|
896
|
+
if (estimatedFee.isZero()) {
|
|
897
|
+
return [2 /*return*/, this.feeDefaults];
|
|
898
|
+
}
|
|
899
|
+
feeStepFactor = new bignumber_1.default(0.5);
|
|
900
|
+
mediumFee = estimatedFee;
|
|
901
|
+
lowFee = mediumFee.minus(mediumFee.times(feeStepFactor)).integerValue(bignumber_1.default.ROUND_FLOOR);
|
|
902
|
+
highFee = mediumFee.plus(mediumFee.times(feeStepFactor)).integerValue(bignumber_1.default.ROUND_FLOOR);
|
|
903
|
+
return [2 /*return*/, {
|
|
904
|
+
low: (0, module_kit_1.newAmount)(lowFee, 'blockchain'),
|
|
905
|
+
medium: (0, module_kit_1.newAmount)(mediumFee, 'blockchain'),
|
|
906
|
+
high: (0, module_kit_1.newAmount)(highFee, 'blockchain')
|
|
907
|
+
}];
|
|
908
|
+
}
|
|
909
|
+
});
|
|
910
|
+
});
|
|
911
|
+
};
|
|
912
|
+
BitcoinProtocolImpl.prototype.prepareTransactionWithPublicKey = function (publicKey, details, configuration) {
|
|
913
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
914
|
+
return __generator(this, function (_a) {
|
|
915
|
+
switch (publicKey.type) {
|
|
916
|
+
case 'pub':
|
|
917
|
+
return [2 /*return*/, this.prepareTransactionWithNonExtendedPublicKey(publicKey, details, configuration)];
|
|
918
|
+
case 'xpub':
|
|
919
|
+
return [2 /*return*/, this.prepareTransactionWithExtendedPublicKey(publicKey, details, configuration)];
|
|
920
|
+
default:
|
|
921
|
+
(0, coinlib_core_1.assertNever)(publicKey);
|
|
922
|
+
throw new errors_1.UnsupportedError(coinlib_core_1.Domain.BITCOIN, 'Unuspported public key type.');
|
|
923
|
+
}
|
|
924
|
+
return [2 /*return*/];
|
|
925
|
+
});
|
|
926
|
+
});
|
|
927
|
+
};
|
|
928
|
+
BitcoinProtocolImpl.prototype.prepareTransactionWithNonExtendedPublicKey = function (publicKey, details, configuration) {
|
|
929
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
930
|
+
var fee, estimatedFee, wrappedFee, transaction, address, utxos, totalRequiredBalance, valueAccumulator, _i, utxos_1, utxo, i, value, changeValue;
|
|
931
|
+
var _this = this;
|
|
932
|
+
return __generator(this, function (_a) {
|
|
933
|
+
switch (_a.label) {
|
|
934
|
+
case 0:
|
|
935
|
+
if (!((configuration === null || configuration === void 0 ? void 0 : configuration.fee) !== undefined)) return [3 /*break*/, 1];
|
|
936
|
+
fee = configuration.fee;
|
|
937
|
+
return [3 /*break*/, 3];
|
|
938
|
+
case 1: return [4 /*yield*/, this.getTransactionFeeWithPublicKey(publicKey, details)];
|
|
939
|
+
case 2:
|
|
940
|
+
estimatedFee = _a.sent();
|
|
941
|
+
fee = estimatedFee.medium;
|
|
942
|
+
_a.label = 3;
|
|
943
|
+
case 3:
|
|
944
|
+
wrappedFee = new bignumber_1.default((0, module_kit_1.newAmount)(fee).blockchain(this.units).value);
|
|
945
|
+
transaction = (0, module_kit_1.newUnsignedTransaction)({
|
|
946
|
+
ins: [],
|
|
947
|
+
outs: []
|
|
948
|
+
});
|
|
949
|
+
return [4 /*yield*/, this.getAddressFromPublicKey(publicKey)];
|
|
950
|
+
case 4:
|
|
951
|
+
address = _a.sent();
|
|
952
|
+
return [4 /*yield*/, index_1.default.get("".concat(this.options.network.indexerApi, "/api/v2/utxo/").concat(address), {
|
|
953
|
+
responseType: 'json'
|
|
954
|
+
})];
|
|
955
|
+
case 5:
|
|
956
|
+
utxos = (_a.sent()).data;
|
|
957
|
+
totalRequiredBalance = details
|
|
958
|
+
.map(function (_a) {
|
|
959
|
+
var amount = _a.amount;
|
|
960
|
+
return new bignumber_1.default((0, module_kit_1.newAmount)(amount).blockchain(_this.units).value);
|
|
961
|
+
})
|
|
962
|
+
.reduce(function (accumulator, currentValue) { return accumulator.plus(currentValue); })
|
|
963
|
+
.plus(wrappedFee);
|
|
964
|
+
valueAccumulator = new bignumber_1.default(0);
|
|
965
|
+
for (_i = 0, utxos_1 = utxos; _i < utxos_1.length; _i++) {
|
|
966
|
+
utxo = utxos_1[_i];
|
|
967
|
+
valueAccumulator = valueAccumulator.plus(new bignumber_1.default(utxo.value));
|
|
968
|
+
transaction.ins.push({
|
|
969
|
+
txId: utxo.txid,
|
|
970
|
+
value: new bignumber_1.default(utxo.value).toString(10),
|
|
971
|
+
vout: utxo.vout,
|
|
972
|
+
address: address
|
|
973
|
+
});
|
|
974
|
+
if (valueAccumulator.isGreaterThanOrEqualTo(totalRequiredBalance)) {
|
|
975
|
+
break;
|
|
976
|
+
}
|
|
977
|
+
}
|
|
978
|
+
if (valueAccumulator.isLessThan(totalRequiredBalance)) {
|
|
979
|
+
throw new errors_1.BalanceError(coinlib_core_1.Domain.BITCOIN, "not enough balance, having ".concat(valueAccumulator.toFixed(), " of ").concat(totalRequiredBalance.toFixed()));
|
|
980
|
+
}
|
|
981
|
+
// tx.addInput(utxo.txid, utxo.vout)
|
|
982
|
+
for (i = 0; i < details.length; i++) {
|
|
983
|
+
value = (0, module_kit_1.newAmount)(details[i].amount).blockchain(this.units).value;
|
|
984
|
+
transaction.outs.push({
|
|
985
|
+
recipient: details[i].to,
|
|
986
|
+
isChange: false,
|
|
987
|
+
value: value
|
|
988
|
+
});
|
|
989
|
+
valueAccumulator = valueAccumulator.minus(value);
|
|
990
|
+
// tx.addOutput(details[i].to, details[i].amount)
|
|
991
|
+
}
|
|
992
|
+
changeValue = valueAccumulator.minus(wrappedFee);
|
|
993
|
+
if (changeValue.isGreaterThan(new bignumber_1.default(DUST_AMOUNT))) {
|
|
994
|
+
transaction.outs.push({
|
|
995
|
+
recipient: address,
|
|
996
|
+
isChange: true,
|
|
997
|
+
value: changeValue.toString(10)
|
|
998
|
+
});
|
|
999
|
+
}
|
|
1000
|
+
return [2 /*return*/, transaction];
|
|
1001
|
+
}
|
|
1002
|
+
});
|
|
1003
|
+
});
|
|
1004
|
+
};
|
|
1005
|
+
BitcoinProtocolImpl.prototype.prepareTransactionWithExtendedPublicKey = function (extendedPublicKey, details, configuration) {
|
|
1006
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
1007
|
+
var targetFee, estimatedFee, wrappedFee, transaction, utxos, totalRequiredBalance, valueAccumulator, getPathIndexes, _i, utxos_2, utxo, indexes, derivedPublicKey, derivedAddress, i, value, lastUsedInternalAddress, changeValue, changeAddressIndex, derivedPublicKey, derivedAddress;
|
|
1008
|
+
var _this = this;
|
|
1009
|
+
return __generator(this, function (_a) {
|
|
1010
|
+
switch (_a.label) {
|
|
1011
|
+
case 0:
|
|
1012
|
+
if (!((configuration === null || configuration === void 0 ? void 0 : configuration.fee) !== undefined)) return [3 /*break*/, 1];
|
|
1013
|
+
targetFee = configuration.fee;
|
|
1014
|
+
return [3 /*break*/, 3];
|
|
1015
|
+
case 1: return [4 /*yield*/, this.getTransactionFeeWithPublicKey(extendedPublicKey, details)];
|
|
1016
|
+
case 2:
|
|
1017
|
+
estimatedFee = _a.sent();
|
|
1018
|
+
targetFee = estimatedFee.medium;
|
|
1019
|
+
_a.label = 3;
|
|
1020
|
+
case 3:
|
|
1021
|
+
wrappedFee = new bignumber_1.default((0, module_kit_1.newAmount)(targetFee).blockchain(this.units).value);
|
|
1022
|
+
transaction = (0, module_kit_1.newUnsignedTransaction)({
|
|
1023
|
+
ins: [],
|
|
1024
|
+
outs: []
|
|
1025
|
+
});
|
|
1026
|
+
return [4 /*yield*/, index_1.default
|
|
1027
|
+
.get("".concat(this.options.network.indexerApi, "/api/v2/utxo/").concat(extendedPublicKey.value, "?confirmed=true"), {
|
|
1028
|
+
responseType: 'json'
|
|
1029
|
+
})
|
|
1030
|
+
.catch(function (error) {
|
|
1031
|
+
throw new errors_1.NetworkError(coinlib_core_1.Domain.BITCOIN, error);
|
|
1032
|
+
})];
|
|
1033
|
+
case 4:
|
|
1034
|
+
utxos = (_a.sent()).data;
|
|
1035
|
+
if (utxos.length <= 0) {
|
|
1036
|
+
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
|
|
1037
|
+
}
|
|
1038
|
+
totalRequiredBalance = details
|
|
1039
|
+
.map(function (_a) {
|
|
1040
|
+
var amount = _a.amount;
|
|
1041
|
+
return new bignumber_1.default((0, module_kit_1.newAmount)(amount).blockchain(_this.units).value);
|
|
1042
|
+
})
|
|
1043
|
+
.reduce(function (accumulator, currentValue) { return accumulator.plus(currentValue); })
|
|
1044
|
+
.plus(wrappedFee);
|
|
1045
|
+
valueAccumulator = new bignumber_1.default(0);
|
|
1046
|
+
getPathIndexes = function (path) {
|
|
1047
|
+
var result = path
|
|
1048
|
+
.split('/')
|
|
1049
|
+
.slice(-2)
|
|
1050
|
+
.map(function (item) { return parseInt(item, 10); })
|
|
1051
|
+
.filter(function (item) { return !isNaN(item); });
|
|
1052
|
+
if (result.length !== 2) {
|
|
1053
|
+
throw new errors_1.ConditionViolationError(coinlib_core_1.Domain.BITCOIN, 'Unexpected path format');
|
|
1054
|
+
}
|
|
1055
|
+
return [result[0], result[1]];
|
|
1056
|
+
};
|
|
1057
|
+
_i = 0, utxos_2 = utxos;
|
|
1058
|
+
_a.label = 5;
|
|
1059
|
+
case 5:
|
|
1060
|
+
if (!(_i < utxos_2.length)) return [3 /*break*/, 9];
|
|
1061
|
+
utxo = utxos_2[_i];
|
|
1062
|
+
valueAccumulator = valueAccumulator.plus(utxo.value);
|
|
1063
|
+
indexes = getPathIndexes(utxo.path);
|
|
1064
|
+
return [4 /*yield*/, this.deriveFromExtendedPublicKey(extendedPublicKey, indexes[0], indexes[1])];
|
|
1065
|
+
case 6:
|
|
1066
|
+
derivedPublicKey = _a.sent();
|
|
1067
|
+
return [4 /*yield*/, this.getAddressFromPublicKey(derivedPublicKey)];
|
|
1068
|
+
case 7:
|
|
1069
|
+
derivedAddress = _a.sent();
|
|
1070
|
+
if (derivedAddress === utxo.address) {
|
|
1071
|
+
transaction.ins.push({
|
|
1072
|
+
txId: utxo.txid,
|
|
1073
|
+
value: new bignumber_1.default(utxo.value).toString(10),
|
|
1074
|
+
vout: utxo.vout,
|
|
1075
|
+
address: utxo.address,
|
|
1076
|
+
derivationPath: indexes.join('/')
|
|
1077
|
+
});
|
|
1078
|
+
}
|
|
1079
|
+
else {
|
|
1080
|
+
throw new errors_1.InvalidValueError(coinlib_core_1.Domain.BITCOIN, "Invalid address ".concat(JSON.stringify(utxo.address), " returned from API"));
|
|
1081
|
+
}
|
|
1082
|
+
if (valueAccumulator.isGreaterThanOrEqualTo(totalRequiredBalance)) {
|
|
1083
|
+
return [3 /*break*/, 9];
|
|
1084
|
+
}
|
|
1085
|
+
_a.label = 8;
|
|
1086
|
+
case 8:
|
|
1087
|
+
_i++;
|
|
1088
|
+
return [3 /*break*/, 5];
|
|
1089
|
+
case 9:
|
|
1090
|
+
if (valueAccumulator.isLessThan(totalRequiredBalance)) {
|
|
1091
|
+
throw new errors_1.BalanceError(coinlib_core_1.Domain.BITCOIN, 'not enough balance');
|
|
1092
|
+
}
|
|
1093
|
+
for (i = 0; i < details.length; i++) {
|
|
1094
|
+
value = (0, module_kit_1.newAmount)(details[i].amount).blockchain(this.units).value;
|
|
1095
|
+
transaction.outs.push({
|
|
1096
|
+
recipient: details[i].to,
|
|
1097
|
+
isChange: false,
|
|
1098
|
+
value: value,
|
|
1099
|
+
derivationPath: '' // TODO: Remove this as soon as our serializer supports optional properties
|
|
1100
|
+
});
|
|
1101
|
+
valueAccumulator = valueAccumulator.minus(value);
|
|
1102
|
+
}
|
|
1103
|
+
lastUsedInternalAddress = Math.max.apply(Math, __spreadArray([-1], utxos
|
|
1104
|
+
.map(function (utxo) { return getPathIndexes(utxo.path); })
|
|
1105
|
+
.filter(function (indexes) { return indexes[0] === 1; })
|
|
1106
|
+
.map(function (indexes) { return indexes[1]; }), false));
|
|
1107
|
+
changeValue = valueAccumulator.minus(wrappedFee);
|
|
1108
|
+
if (!changeValue.isGreaterThan(new bignumber_1.default(DUST_AMOUNT))) return [3 /*break*/, 12];
|
|
1109
|
+
changeAddressIndex = lastUsedInternalAddress + 1;
|
|
1110
|
+
return [4 /*yield*/, this.deriveFromExtendedPublicKey(extendedPublicKey, 1, changeAddressIndex)];
|
|
1111
|
+
case 10:
|
|
1112
|
+
derivedPublicKey = _a.sent();
|
|
1113
|
+
return [4 /*yield*/, this.getAddressFromPublicKey(derivedPublicKey)];
|
|
1114
|
+
case 11:
|
|
1115
|
+
derivedAddress = _a.sent();
|
|
1116
|
+
transaction.outs.push({
|
|
1117
|
+
recipient: derivedAddress,
|
|
1118
|
+
isChange: true,
|
|
1119
|
+
value: changeValue.toString(10),
|
|
1120
|
+
derivationPath: changeAddressIndex.toString()
|
|
1121
|
+
});
|
|
1122
|
+
_a.label = 12;
|
|
1123
|
+
case 12: return [2 /*return*/, transaction];
|
|
1124
|
+
}
|
|
1125
|
+
});
|
|
1126
|
+
});
|
|
1127
|
+
};
|
|
1128
|
+
BitcoinProtocolImpl.prototype.broadcastTransaction = function (transaction) {
|
|
1129
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
1130
|
+
var data;
|
|
1131
|
+
return __generator(this, function (_a) {
|
|
1132
|
+
switch (_a.label) {
|
|
1133
|
+
case 0: return [4 /*yield*/, index_1.default.post("".concat(this.options.network.indexerApi, "/api/v2/sendtx/"), transaction.transaction)];
|
|
1134
|
+
case 1:
|
|
1135
|
+
data = (_a.sent()).data;
|
|
1136
|
+
return [2 /*return*/, data.result];
|
|
1137
|
+
}
|
|
1138
|
+
});
|
|
1139
|
+
});
|
|
1140
|
+
};
|
|
1141
|
+
// Custom
|
|
1142
|
+
BitcoinProtocolImpl.prototype.convertExtendedSecretKey = function (extendedSecretKey, targetFormat) {
|
|
1143
|
+
return (0, key_1.convertExtendedSecretKey)(extendedSecretKey, {
|
|
1144
|
+
format: targetFormat,
|
|
1145
|
+
type: 'xprv',
|
|
1146
|
+
hashFunction: this.bitcoinJS.config.network.hashFunctions.address
|
|
1147
|
+
});
|
|
1148
|
+
};
|
|
1149
|
+
BitcoinProtocolImpl.prototype.convertExtendedPublicKey = function (extendedPublicKey, targetFormat) {
|
|
1150
|
+
return (0, key_1.convertExtendedPublicKey)(extendedPublicKey, {
|
|
1151
|
+
format: targetFormat,
|
|
1152
|
+
type: 'xpub',
|
|
1153
|
+
hashFunction: this.bitcoinJS.config.network.hashFunctions.address
|
|
1154
|
+
});
|
|
1155
|
+
};
|
|
1156
|
+
BitcoinProtocolImpl.prototype.convertCryptoDerivative = function (derivative) {
|
|
1157
|
+
var hexNode = (0, crypto_1.encodeDerivative)('hex', __assign(__assign({}, derivative), { secretKey: "00".concat(derivative.secretKey) }));
|
|
1158
|
+
var extendedSecretKey = {
|
|
1159
|
+
type: 'xpriv',
|
|
1160
|
+
format: 'hex',
|
|
1161
|
+
value: hexNode.secretKey
|
|
1162
|
+
};
|
|
1163
|
+
return this.convertExtendedSecretKey(extendedSecretKey, 'encoded');
|
|
1164
|
+
};
|
|
1165
|
+
BitcoinProtocolImpl.prototype.derivativeToBip32Node = function (derivative) {
|
|
1166
|
+
var extendedSecretKey = this.convertCryptoDerivative(derivative);
|
|
1167
|
+
return this.bitcoinJS.lib.HDNode.fromBase58(extendedSecretKey.value, this.bitcoinJS.config.network);
|
|
1168
|
+
};
|
|
1169
|
+
return BitcoinProtocolImpl;
|
|
1170
|
+
}());
|
|
1171
|
+
exports.BitcoinProtocolImpl = BitcoinProtocolImpl;
|
|
1172
|
+
// Factory
|
|
1173
|
+
function createBitcoinProtocol(options) {
|
|
1174
|
+
if (options === void 0) { options = {}; }
|
|
1175
|
+
return new BitcoinProtocolImpl(options);
|
|
1176
|
+
}
|
|
1177
|
+
exports.createBitcoinProtocol = createBitcoinProtocol;
|
|
1178
|
+
exports.BITCOIN_MAINNET_PROTOCOL_NETWORK = {
|
|
1179
|
+
name: 'Mainnet',
|
|
1180
|
+
type: 'mainnet',
|
|
1181
|
+
rpcUrl: '',
|
|
1182
|
+
indexerApi: 'https://bitcoin.prod.gke.papers.tech'
|
|
1183
|
+
};
|
|
1184
|
+
var DEFAULT_BITCOIN_PROTOCOL_NETWORK = exports.BITCOIN_MAINNET_PROTOCOL_NETWORK;
|
|
1185
|
+
function createBitcoinProtocolOptions(network) {
|
|
1186
|
+
if (network === void 0) { network = {}; }
|
|
1187
|
+
return {
|
|
1188
|
+
network: network.type === 'custom'
|
|
1189
|
+
? __assign(__assign(__assign({}, DEFAULT_BITCOIN_PROTOCOL_NETWORK), { bitcoinjsNetworkName: 'bitcoin' }), network) : __assign(__assign({}, DEFAULT_BITCOIN_PROTOCOL_NETWORK), network)
|
|
1190
|
+
};
|
|
1191
|
+
}
|
|
1192
|
+
exports.createBitcoinProtocolOptions = createBitcoinProtocolOptions;
|
|
1193
|
+
//# sourceMappingURL=BitcoinProtocol.js.map
|