edge-currency-plugins 1.0.0-rc.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +3 -0
- package/lib/bch.js +15 -0
- package/lib/bsv.js +15 -0
- package/lib/btc.js +15 -0
- package/lib/common/constants.js +11 -0
- package/lib/common/fees/calcMinerFeePerByte.js +62 -0
- package/lib/common/fees/makeFees.js +182 -0
- package/lib/common/fees/processInfoServerFees.js +30 -0
- package/lib/common/fees/processMempoolSpaceFees.js +38 -0
- package/lib/common/plugin/makeCurrencyPlugin.js +66 -0
- package/lib/common/plugin/makeCurrencyTools.js +211 -0
- package/lib/common/plugin/makeEngineEmitter.js +102 -0
- package/lib/common/plugin/makeMetadata.js +99 -0
- package/lib/common/plugin/pluginState.js +253 -0
- package/lib/common/plugin/serverScores.js +328 -0
- package/lib/common/plugin/types.js +173 -0
- package/lib/common/plugin/utils.js +18 -0
- package/lib/common/utxobased/db/Models/ProcessorTransaction.js +122 -0
- package/lib/common/utxobased/db/Models/baselet.js +81 -0
- package/lib/common/utxobased/db/makeBaselets.js +123 -0
- package/lib/common/utxobased/db/makeMutex.js +17 -0
- package/lib/common/utxobased/db/makeProcessor.js +554 -0
- package/lib/common/utxobased/db/types.js +67 -0
- package/lib/common/utxobased/engine/await-lock.js +65 -0
- package/lib/common/utxobased/engine/constants.js +7 -0
- package/lib/common/utxobased/engine/makeServerStates.js +394 -0
- package/lib/common/utxobased/engine/makeUtxoEngine.js +690 -0
- package/lib/common/utxobased/engine/makeUtxoEngineState.js +1443 -0
- package/lib/common/utxobased/engine/makeUtxoWalletTools.js +216 -0
- package/lib/common/utxobased/engine/paymentRequest.js +102 -0
- package/lib/common/utxobased/engine/types.js +21 -0
- package/lib/common/utxobased/engine/util/defaultValidateConfirmations.js +19 -0
- package/lib/common/utxobased/engine/util/getOwnUtxosFromTx.js +54 -0
- package/lib/common/utxobased/engine/utils.js +239 -0
- package/lib/common/utxobased/info/all.js +67 -0
- package/lib/common/utxobased/info/badcoin.js +73 -0
- package/lib/common/utxobased/info/bitcoin.js +91 -0
- package/lib/common/utxobased/info/bitcoincash.js +93 -0
- package/lib/common/utxobased/info/bitcoincashtestnet.js +72 -0
- package/lib/common/utxobased/info/bitcoingold.js +85 -0
- package/lib/common/utxobased/info/bitcoingoldtestnet.js +71 -0
- package/lib/common/utxobased/info/bitcoinsv.js +76 -0
- package/lib/common/utxobased/info/bitcointestnet.js +81 -0
- package/lib/common/utxobased/info/dash.js +76 -0
- package/lib/common/utxobased/info/digibyte.js +76 -0
- package/lib/common/utxobased/info/dogecoin.js +76 -0
- package/lib/common/utxobased/info/eboost.js +70 -0
- package/lib/common/utxobased/info/feathercoin.js +76 -0
- package/lib/common/utxobased/info/groestlcoin.js +88 -0
- package/lib/common/utxobased/info/litecoin.js +82 -0
- package/lib/common/utxobased/info/qtum.js +67 -0
- package/lib/common/utxobased/info/ravencoin.js +70 -0
- package/lib/common/utxobased/info/scriptTemplates/bitcoincashScriptTemplates.js +56 -0
- package/lib/common/utxobased/info/scriptTemplates/types.js +14 -0
- package/lib/common/utxobased/info/smartcash.js +78 -0
- package/lib/common/utxobased/info/ufo.js +75 -0
- package/lib/common/utxobased/info/utxoPickers/dogeUtxoPicker.js +34 -0
- package/lib/common/utxobased/info/vertcoin.js +82 -0
- package/lib/common/utxobased/info/zcoin.js +72 -0
- package/lib/common/utxobased/keymanager/base.js +60 -0
- package/lib/common/utxobased/keymanager/bip69.js +31 -0
- package/lib/common/utxobased/keymanager/bitcoincashUtils/base32.js +88 -0
- package/lib/common/utxobased/keymanager/bitcoincashUtils/bn.js +210 -0
- package/lib/common/utxobased/keymanager/bitcoincashUtils/cashAddress.js +282 -0
- package/lib/common/utxobased/keymanager/cleaners.js +198 -0
- package/lib/common/utxobased/keymanager/coinmapper.js +12 -0
- package/lib/common/utxobased/keymanager/declarations/base58grscheck.d.js +1 -0
- package/lib/common/utxobased/keymanager/declarations/blake-hash.d.js +1 -0
- package/lib/common/utxobased/keymanager/declarations/bn.d.js +1 -0
- package/lib/common/utxobased/keymanager/declarations/bs58smartcheck.d.js +1 -0
- package/lib/common/utxobased/keymanager/declarations/wif-smart.d.js +1 -0
- package/lib/common/utxobased/keymanager/declarations/wifgrs.d.js +1 -0
- package/lib/common/utxobased/keymanager/keymanager.js +1214 -0
- package/lib/common/utxobased/keymanager/utxopicker/accumulative.js +52 -0
- package/lib/common/utxobased/keymanager/utxopicker/forceUseUtxo.js +59 -0
- package/lib/common/utxobased/keymanager/utxopicker/subtractFee.js +17 -0
- package/lib/common/utxobased/keymanager/utxopicker/types.js +42 -0
- package/lib/common/utxobased/keymanager/utxopicker/utils.js +159 -0
- package/lib/common/utxobased/keymanager/utxopicker.js +17 -0
- package/lib/common/utxobased/network/BlockBook.js +200 -0
- package/lib/common/utxobased/network/BlockBookAPI.js +335 -0
- package/lib/common/utxobased/network/Deferred.js +29 -0
- package/lib/common/utxobased/network/MakeSocketEmitter.js +35 -0
- package/lib/common/utxobased/network/Socket.js +362 -0
- package/lib/common/utxobased/network/nodejsWS.js +59 -0
- package/lib/common/utxobased/network/socketQueue.js +60 -0
- package/lib/common/utxobased/network/types.js +19 -0
- package/lib/common/utxobased/network/windowWS.js +54 -0
- package/lib/firo.js +15 -0
- package/lib/ftc.js +15 -0
- package/lib/index.js +19 -0
- package/lib/ltc.js +15 -0
- package/lib/src/bch.d.ts +3 -0
- package/lib/src/bsv.d.ts +3 -0
- package/lib/src/btc.d.ts +3 -0
- package/lib/src/common/constants.d.ts +9 -0
- package/lib/src/common/fees/calcMinerFeePerByte.d.ts +13 -0
- package/lib/src/common/fees/makeFees.d.ts +20 -0
- package/lib/src/common/fees/processInfoServerFees.d.ts +7 -0
- package/lib/src/common/fees/processMempoolSpaceFees.d.ts +13 -0
- package/lib/src/common/plugin/makeCurrencyPlugin.d.ts +3 -0
- package/lib/src/common/plugin/makeCurrencyTools.d.ts +8 -0
- package/lib/src/common/plugin/makeEngineEmitter.d.ts +23 -0
- package/lib/src/common/plugin/makeMetadata.d.ts +14 -0
- package/lib/src/common/plugin/pluginState.d.ts +34 -0
- package/lib/src/common/plugin/serverScores.d.ts +61 -0
- package/lib/src/common/plugin/types.d.ts +135 -0
- package/lib/src/common/plugin/utils.d.ts +6 -0
- package/lib/src/common/utxobased/db/Models/ProcessorTransaction.d.ts +16 -0
- package/lib/src/common/utxobased/db/Models/baselet.d.ts +28 -0
- package/lib/src/common/utxobased/db/makeBaselets.d.ts +33 -0
- package/lib/src/common/utxobased/db/makeMutex.d.ts +3 -0
- package/lib/src/common/utxobased/db/makeProcessor.d.ts +42 -0
- package/lib/src/common/utxobased/db/types.d.ts +47 -0
- package/lib/src/common/utxobased/engine/await-lock.d.ts +29 -0
- package/lib/src/common/utxobased/engine/constants.d.ts +4 -0
- package/lib/src/common/utxobased/engine/makeServerStates.d.ts +37 -0
- package/lib/src/common/utxobased/engine/makeUtxoEngine.d.ts +3 -0
- package/lib/src/common/utxobased/engine/makeUtxoEngineState.d.ts +108 -0
- package/lib/src/common/utxobased/engine/makeUtxoWalletTools.d.ts +52 -0
- package/lib/src/common/utxobased/engine/paymentRequest.d.ts +18 -0
- package/lib/src/common/utxobased/engine/types.d.ts +19 -0
- package/lib/src/common/utxobased/engine/util/defaultValidateConfirmations.d.ts +3 -0
- package/lib/src/common/utxobased/engine/util/getOwnUtxosFromTx.d.ts +4 -0
- package/lib/src/common/utxobased/engine/utils.d.ts +43 -0
- package/lib/src/common/utxobased/info/all.d.ts +22 -0
- package/lib/src/common/utxobased/info/badcoin.d.ts +3 -0
- package/lib/src/common/utxobased/info/bitcoin.d.ts +3 -0
- package/lib/src/common/utxobased/info/bitcoincash.d.ts +3 -0
- package/lib/src/common/utxobased/info/bitcoincashtestnet.d.ts +3 -0
- package/lib/src/common/utxobased/info/bitcoingold.d.ts +3 -0
- package/lib/src/common/utxobased/info/bitcoingoldtestnet.d.ts +3 -0
- package/lib/src/common/utxobased/info/bitcoinsv.d.ts +3 -0
- package/lib/src/common/utxobased/info/bitcointestnet.d.ts +6 -0
- package/lib/src/common/utxobased/info/dash.d.ts +3 -0
- package/lib/src/common/utxobased/info/digibyte.d.ts +3 -0
- package/lib/src/common/utxobased/info/dogecoin.d.ts +3 -0
- package/lib/src/common/utxobased/info/eboost.d.ts +3 -0
- package/lib/src/common/utxobased/info/feathercoin.d.ts +6 -0
- package/lib/src/common/utxobased/info/groestlcoin.d.ts +3 -0
- package/lib/src/common/utxobased/info/litecoin.d.ts +6 -0
- package/lib/src/common/utxobased/info/qtum.d.ts +3 -0
- package/lib/src/common/utxobased/info/ravencoin.d.ts +3 -0
- package/lib/src/common/utxobased/info/scriptTemplates/bitcoincashScriptTemplates.d.ts +2 -0
- package/lib/src/common/utxobased/info/scriptTemplates/types.d.ts +9 -0
- package/lib/src/common/utxobased/info/smartcash.d.ts +3 -0
- package/lib/src/common/utxobased/info/ufo.d.ts +3 -0
- package/lib/src/common/utxobased/info/utxoPickers/dogeUtxoPicker.d.ts +2 -0
- package/lib/src/common/utxobased/info/vertcoin.d.ts +3 -0
- package/lib/src/common/utxobased/info/zcoin.d.ts +6 -0
- package/lib/src/common/utxobased/keymanager/base.d.ts +9 -0
- package/lib/src/common/utxobased/keymanager/bip69.d.ts +19 -0
- package/lib/src/common/utxobased/keymanager/bitcoincashUtils/base32.d.ts +19 -0
- package/lib/src/common/utxobased/keymanager/bitcoincashUtils/bn.d.ts +2 -0
- package/lib/src/common/utxobased/keymanager/bitcoincashUtils/cashAddress.d.ts +12 -0
- package/lib/src/common/utxobased/keymanager/cleaners.d.ts +55 -0
- package/lib/src/common/utxobased/keymanager/coinmapper.d.ts +2 -0
- package/lib/src/common/utxobased/keymanager/keymanager.d.ts +198 -0
- package/lib/src/common/utxobased/keymanager/utxopicker/accumulative.d.ts +2 -0
- package/lib/src/common/utxobased/keymanager/utxopicker/forceUseUtxo.d.ts +2 -0
- package/lib/src/common/utxobased/keymanager/utxopicker/subtractFee.d.ts +2 -0
- package/lib/src/common/utxobased/keymanager/utxopicker/types.d.ts +36 -0
- package/lib/src/common/utxobased/keymanager/utxopicker/utils.d.ts +15 -0
- package/lib/src/common/utxobased/keymanager/utxopicker.d.ts +8 -0
- package/lib/src/common/utxobased/network/BlockBook.d.ts +33 -0
- package/lib/src/common/utxobased/network/BlockBookAPI.d.ts +163 -0
- package/lib/src/common/utxobased/network/Deferred.d.ts +8 -0
- package/lib/src/common/utxobased/network/MakeSocketEmitter.d.ts +13 -0
- package/lib/src/common/utxobased/network/Socket.d.ts +41 -0
- package/lib/src/common/utxobased/network/nodejsWS.d.ts +2 -0
- package/lib/src/common/utxobased/network/socketQueue.d.ts +8 -0
- package/lib/src/common/utxobased/network/types.d.ts +17 -0
- package/lib/src/common/utxobased/network/windowWS.d.ts +2 -0
- package/lib/src/firo.d.ts +3 -0
- package/lib/src/ftc.d.ts +3 -0
- package/lib/src/index.d.ts +4 -0
- package/lib/src/ltc.d.ts +3 -0
- package/lib/src/tbch.d.ts +3 -0
- package/lib/src/tbtc.d.ts +3 -0
- package/lib/src/tbtg.d.ts +3 -0
- package/lib/src/types.d.ts +6 -0
- package/lib/src/util/delay.d.ts +1 -0
- package/lib/src/util/filterUndefined.d.ts +1 -0
- package/lib/src/util/indexAtProtected.d.ts +5 -0
- package/lib/src/util/typeUtil.d.ts +8 -0
- package/lib/src/util/undefinedIfEmptyString.d.ts +1 -0
- package/lib/src/util/unixTime.d.ts +1 -0
- package/lib/tbch.js +15 -0
- package/lib/tbtc.js +15 -0
- package/lib/tbtg.js +15 -0
- package/lib/test/common/fees/calcMinerFeePerByte.spec.d.ts +1 -0
- package/lib/test/common/fees/fees.spec.d.ts +1 -0
- package/lib/test/common/plugin/currencyPlugin.fixtures/common.d.ts +29 -0
- package/lib/test/common/plugin/currencyPlugin.fixtures/currencies/bitcoin.d.ts +2 -0
- package/lib/test/common/plugin/currencyPlugin.fixtures/currencies/bitcoincash.d.ts +2 -0
- package/lib/test/common/plugin/currencyPlugin.fixtures/currencies/bitcoinsv.d.ts +2 -0
- package/lib/test/common/plugin/currencyPlugin.fixtures/currencies/digibyte.d.ts +2 -0
- package/lib/test/common/plugin/currencyPlugin.fixtures/currencies/feathercoin.d.ts +2 -0
- package/lib/test/common/plugin/currencyPlugin.fixtures/currencies/groestlcoin.d.ts +2 -0
- package/lib/test/common/plugin/currencyPlugin.fixtures/currencies/litecoin.d.ts +2 -0
- package/lib/test/common/plugin/currencyPlugin.fixtures/currencies/smartcash.d.ts +2 -0
- package/lib/test/common/plugin/currencyPlugin.fixtures/currencies/zcoin.d.ts +2 -0
- package/lib/test/common/plugin/currencyPlugin.fixtures/index.d.ts +2 -0
- package/lib/test/common/plugin/currencyPlugin.spec.d.ts +1 -0
- package/lib/test/common/plugin/makeMetadata.spec.d.ts +1 -0
- package/lib/test/common/plugin/serverScores.spec.d.ts +1 -0
- package/lib/test/common/utxobased/addressFormat/addressFormatIndex/addressFormat.spec.d.ts +1 -0
- package/lib/test/common/utxobased/db/Processor.spec.d.ts +1 -0
- package/lib/test/common/utxobased/engine/PaymentRequest.spec.d.ts +1 -0
- package/lib/test/common/utxobased/engine/UtxoWalletTools.spec.d.ts +1 -0
- package/lib/test/common/utxobased/engine/engine.fixtures/bitcoinTestnet/tests.d.ts +70 -0
- package/lib/test/common/utxobased/engine/engine.fixtures/index.d.ts +65 -0
- package/lib/test/common/utxobased/engine/engine.spec.d.ts +1 -0
- package/lib/test/common/utxobased/keymanager/coins/altcointestfixtures.d.ts +54 -0
- package/lib/test/common/utxobased/keymanager/coins/bitcoincashtransactiontest.spec.d.ts +1 -0
- package/lib/test/common/utxobased/keymanager/coins/bitcointransactiontest.spec.d.ts +1 -0
- package/lib/test/common/utxobased/keymanager/coins/cashaddr.spec.d.ts +1 -0
- package/lib/test/common/utxobased/keymanager/coins/checkdatasig.spec.d.ts +1 -0
- package/lib/test/common/utxobased/keymanager/coins/groestlcointransactiontest.spec.d.ts +1 -0
- package/lib/test/common/utxobased/keymanager/coins/keymanagertest.spec.d.ts +1 -0
- package/lib/test/common/utxobased/network/BlockBook.spec.d.ts +1 -0
- package/lib/test/util/objectKeys.d.ts +8 -0
- package/lib/test/util/testLog.d.ts +3 -0
- package/lib/test/utils.d.ts +10 -0
- package/lib/types.js +7 -0
- package/lib/util/delay.js +4 -0
- package/lib/util/filterUndefined.js +4 -0
- package/lib/util/indexAtProtected.js +9 -0
- package/lib/util/typeUtil.js +9 -0
- package/lib/util/undefinedIfEmptyString.js +2 -0
- package/lib/util/unixTime.js +2 -0
- package/package.json +101 -0
|
@@ -0,0 +1,1214 @@
|
|
|
1
|
+
"use strict";Object.defineProperty(exports, "__esModule", {value: true}); function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { newObj[key] = obj[key]; } } } newObj.default = obj; return newObj; } } function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } function _nullishCoalesce(lhs, rhsFn) { if (lhs != null) { return lhs; } else { return rhsFn(); } } function _optionalChain(ops) { let lastAccessLHS = undefined; let value = ops[0]; let i = 1; while (i < ops.length) { const op = ops[i]; const fn = ops[i + 1]; i += 2; if ((op === 'optionalAccess' || op === 'optionalCall') && value == null) { return undefined; } if (op === 'access' || op === 'optionalAccess') { lastAccessLHS = value; value = fn(value); } else if (op === 'call' || op === 'optionalCall') { value = fn((...args) => value.call(lastAccessLHS, ...args)); lastAccessLHS = undefined; } } return value; }/* eslint-disable camelcase */
|
|
2
|
+
var _altcoinjs = require('altcoin-js'); var bitcoin = _interopRequireWildcard(_altcoinjs);
|
|
3
|
+
var _biggystring = require('biggystring');
|
|
4
|
+
var _bip32 = require('bip32'); var bip32 = _interopRequireWildcard(_bip32);
|
|
5
|
+
var _bip39 = require('bip39'); var bip39 = _interopRequireWildcard(_bip39);
|
|
6
|
+
var _bitcoinjsmessage = require('bitcoinjs-message'); var _bitcoinjsmessage2 = _interopRequireDefault(_bitcoinjsmessage);
|
|
7
|
+
var _types = require('edge-core-js/types');
|
|
8
|
+
|
|
9
|
+
var _indexAtProtected = require('../../../util/indexAtProtected');
|
|
10
|
+
var _undefinedIfEmptyString = require('../../../util/undefinedIfEmptyString');
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
var _bip69 = require('./bip69');
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
var _cashAddress = require('./bitcoincashUtils/cashAddress');
|
|
20
|
+
var _coinmapper = require('./coinmapper');
|
|
21
|
+
var _utxopicker = require('./utxopicker'); var utxopicker = _interopRequireWildcard(_utxopicker);
|
|
22
|
+
|
|
23
|
+
// in bitcoin these are bip44, bip49, bip84 xpub prefixes
|
|
24
|
+
// other coins contain different formats which still need to be gathered.
|
|
25
|
+
var BIP43PurposeTypeEnum; (function (BIP43PurposeTypeEnum) {
|
|
26
|
+
const Airbitz = 'airbitz'; BIP43PurposeTypeEnum["Airbitz"] = Airbitz;
|
|
27
|
+
const Legacy = 'legacy'; BIP43PurposeTypeEnum["Legacy"] = Legacy; // xpub/xprv tpub/tprv etc.
|
|
28
|
+
const Segwit = 'segwit'; BIP43PurposeTypeEnum["Segwit"] = Segwit; // zpub/zprv vpub/vprv etc.
|
|
29
|
+
const WrappedSegwit = 'wrappedSegwit'; BIP43PurposeTypeEnum["WrappedSegwit"] = WrappedSegwit; // ypub/yprv upub/uprv etc.
|
|
30
|
+
})(BIP43PurposeTypeEnum || (exports.BIP43PurposeTypeEnum = BIP43PurposeTypeEnum = {}));
|
|
31
|
+
|
|
32
|
+
// supported address types.
|
|
33
|
+
var AddressTypeEnum; (function (AddressTypeEnum) {
|
|
34
|
+
const p2pkh = 'p2pkh'; AddressTypeEnum["p2pkh"] = p2pkh;
|
|
35
|
+
const p2sh = 'p2sh'; AddressTypeEnum["p2sh"] = p2sh;
|
|
36
|
+
const p2wpkh = 'p2wpkh'; AddressTypeEnum["p2wpkh"] = p2wpkh; // short bech32 address
|
|
37
|
+
const p2wsh = 'p2wsh'; AddressTypeEnum["p2wsh"] = p2wsh; // long bech32 address
|
|
38
|
+
})(AddressTypeEnum || (exports.AddressTypeEnum = AddressTypeEnum = {}));
|
|
39
|
+
|
|
40
|
+
var ScriptTypeEnum; (function (ScriptTypeEnum) {
|
|
41
|
+
const p2wpkh = 'p2wpkh'; ScriptTypeEnum["p2wpkh"] = p2wpkh;
|
|
42
|
+
const p2wpkhp2sh = 'p2wpkhp2sh'; ScriptTypeEnum["p2wpkhp2sh"] = p2wpkhp2sh;
|
|
43
|
+
const p2wsh = 'p2wsh'; ScriptTypeEnum["p2wsh"] = p2wsh;
|
|
44
|
+
const p2pk = 'p2pk'; ScriptTypeEnum["p2pk"] = p2pk;
|
|
45
|
+
const p2pkh = 'p2pkh'; ScriptTypeEnum["p2pkh"] = p2pkh;
|
|
46
|
+
const p2sh = 'p2sh'; ScriptTypeEnum["p2sh"] = p2sh;
|
|
47
|
+
const replayProtection = 'replayprotection'; ScriptTypeEnum["replayProtection"] = replayProtection;
|
|
48
|
+
const replayProtectionP2SH = 'replayprotectionp2sh'; ScriptTypeEnum["replayProtectionP2SH"] = replayProtectionP2SH;
|
|
49
|
+
})(ScriptTypeEnum || (exports.ScriptTypeEnum = ScriptTypeEnum = {}));
|
|
50
|
+
|
|
51
|
+
// A transaction input is either legacy or segwit. This is used for transaction creation and passed per input
|
|
52
|
+
var TransactionInputTypeEnum; (function (TransactionInputTypeEnum) {
|
|
53
|
+
const Legacy = 'legacy'; TransactionInputTypeEnum["Legacy"] = Legacy;
|
|
54
|
+
const Segwit = 'segwit'; TransactionInputTypeEnum["Segwit"] = Segwit;
|
|
55
|
+
})(TransactionInputTypeEnum || (exports.TransactionInputTypeEnum = TransactionInputTypeEnum = {}));
|
|
56
|
+
|
|
57
|
+
|
|
58
|
+
|
|
59
|
+
|
|
60
|
+
|
|
61
|
+
|
|
62
|
+
var VerifyAddressEnum; (function (VerifyAddressEnum) {
|
|
63
|
+
const good = 'good'; VerifyAddressEnum["good"] = good;
|
|
64
|
+
const legacy = 'legacy'; VerifyAddressEnum["legacy"] = legacy;
|
|
65
|
+
const bad = 'bad'; VerifyAddressEnum["bad"] = bad;
|
|
66
|
+
})(VerifyAddressEnum || (exports.VerifyAddressEnum = VerifyAddressEnum = {}));
|
|
67
|
+
|
|
68
|
+
|
|
69
|
+
|
|
70
|
+
|
|
71
|
+
|
|
72
|
+
|
|
73
|
+
|
|
74
|
+
|
|
75
|
+
|
|
76
|
+
|
|
77
|
+
|
|
78
|
+
|
|
79
|
+
|
|
80
|
+
|
|
81
|
+
|
|
82
|
+
|
|
83
|
+
|
|
84
|
+
|
|
85
|
+
|
|
86
|
+
|
|
87
|
+
|
|
88
|
+
|
|
89
|
+
|
|
90
|
+
|
|
91
|
+
|
|
92
|
+
|
|
93
|
+
|
|
94
|
+
|
|
95
|
+
|
|
96
|
+
|
|
97
|
+
|
|
98
|
+
|
|
99
|
+
|
|
100
|
+
|
|
101
|
+
|
|
102
|
+
|
|
103
|
+
|
|
104
|
+
|
|
105
|
+
|
|
106
|
+
|
|
107
|
+
|
|
108
|
+
|
|
109
|
+
|
|
110
|
+
|
|
111
|
+
|
|
112
|
+
|
|
113
|
+
|
|
114
|
+
|
|
115
|
+
|
|
116
|
+
|
|
117
|
+
|
|
118
|
+
|
|
119
|
+
|
|
120
|
+
|
|
121
|
+
|
|
122
|
+
|
|
123
|
+
|
|
124
|
+
|
|
125
|
+
|
|
126
|
+
|
|
127
|
+
|
|
128
|
+
|
|
129
|
+
|
|
130
|
+
|
|
131
|
+
|
|
132
|
+
|
|
133
|
+
|
|
134
|
+
|
|
135
|
+
|
|
136
|
+
|
|
137
|
+
|
|
138
|
+
|
|
139
|
+
|
|
140
|
+
|
|
141
|
+
|
|
142
|
+
|
|
143
|
+
|
|
144
|
+
|
|
145
|
+
|
|
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
|
+
|
|
183
|
+
|
|
184
|
+
|
|
185
|
+
|
|
186
|
+
|
|
187
|
+
|
|
188
|
+
|
|
189
|
+
|
|
190
|
+
|
|
191
|
+
|
|
192
|
+
|
|
193
|
+
|
|
194
|
+
|
|
195
|
+
|
|
196
|
+
|
|
197
|
+
|
|
198
|
+
|
|
199
|
+
|
|
200
|
+
|
|
201
|
+
|
|
202
|
+
|
|
203
|
+
|
|
204
|
+
|
|
205
|
+
|
|
206
|
+
|
|
207
|
+
|
|
208
|
+
|
|
209
|
+
|
|
210
|
+
|
|
211
|
+
|
|
212
|
+
|
|
213
|
+
|
|
214
|
+
|
|
215
|
+
|
|
216
|
+
|
|
217
|
+
|
|
218
|
+
|
|
219
|
+
|
|
220
|
+
|
|
221
|
+
|
|
222
|
+
|
|
223
|
+
|
|
224
|
+
|
|
225
|
+
|
|
226
|
+
|
|
227
|
+
|
|
228
|
+
|
|
229
|
+
|
|
230
|
+
|
|
231
|
+
|
|
232
|
+
|
|
233
|
+
|
|
234
|
+
function bip43PurposeNumberToTypeEnum(
|
|
235
|
+
num
|
|
236
|
+
) {
|
|
237
|
+
switch (num) {
|
|
238
|
+
case 32:
|
|
239
|
+
return BIP43PurposeTypeEnum.Airbitz
|
|
240
|
+
case 44:
|
|
241
|
+
return BIP43PurposeTypeEnum.Legacy
|
|
242
|
+
case 49:
|
|
243
|
+
return BIP43PurposeTypeEnum.WrappedSegwit
|
|
244
|
+
case 84:
|
|
245
|
+
return BIP43PurposeTypeEnum.Segwit
|
|
246
|
+
default:
|
|
247
|
+
throw new Error('InvalidPurposeNumber')
|
|
248
|
+
}
|
|
249
|
+
} exports.bip43PurposeNumberToTypeEnum = bip43PurposeNumberToTypeEnum;
|
|
250
|
+
|
|
251
|
+
|
|
252
|
+
|
|
253
|
+
|
|
254
|
+
|
|
255
|
+
|
|
256
|
+
|
|
257
|
+
|
|
258
|
+
|
|
259
|
+
|
|
260
|
+
|
|
261
|
+
|
|
262
|
+
|
|
263
|
+
|
|
264
|
+
|
|
265
|
+
|
|
266
|
+
const verifyAddressInternal = (
|
|
267
|
+
prefixIndex,
|
|
268
|
+
args
|
|
269
|
+
) => {
|
|
270
|
+
const coinInfo = _coinmapper.getCoinFromString.call(void 0, args.coin)
|
|
271
|
+
const network = bip32NetworkFromCoin({
|
|
272
|
+
coinString: args.coin,
|
|
273
|
+
prefixIndex
|
|
274
|
+
})
|
|
275
|
+
|
|
276
|
+
// Special case for CashAddr
|
|
277
|
+
const cashAddrPrefixes = getCashAddrPrefixes(coinInfo)
|
|
278
|
+
if (cashAddrPrefixes.length > 0) {
|
|
279
|
+
// If coin has cashaddr prefixes, and address passes cashaddr validation,
|
|
280
|
+
// then we return as "valid", otherwise we return as "legacy" if it passes
|
|
281
|
+
// the normal guess check.
|
|
282
|
+
try {
|
|
283
|
+
_cashAddress.cashAddressToHash.call(void 0, args.address, cashAddrPrefixes)
|
|
284
|
+
return VerifyAddressEnum.good
|
|
285
|
+
} catch (e) {}
|
|
286
|
+
guessAddressTypeFromAddress(args.address, network, args.coin, undefined)
|
|
287
|
+
return VerifyAddressEnum.legacy
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
// Normal case
|
|
291
|
+
guessAddressTypeFromAddress(args.address, network, args.coin, undefined)
|
|
292
|
+
// All indices above 0 are treated as legacy
|
|
293
|
+
return prefixIndex === 0 ? VerifyAddressEnum.good : VerifyAddressEnum.legacy
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
const verifyAddress = (args) => {
|
|
297
|
+
const verification = filterCoinPrefixes(
|
|
298
|
+
args.coin,
|
|
299
|
+
prefixIndex => verifyAddressInternal(prefixIndex, args),
|
|
300
|
+
[`Could not determine address type of ${args.address}`]
|
|
301
|
+
)
|
|
302
|
+
return _nullishCoalesce(verification, () => ( VerifyAddressEnum.bad))
|
|
303
|
+
}; exports.verifyAddress = verifyAddress
|
|
304
|
+
|
|
305
|
+
const getAddressTypeFromAddress = (
|
|
306
|
+
address,
|
|
307
|
+
coinName
|
|
308
|
+
) => {
|
|
309
|
+
const addressType = filterCoinPrefixes(
|
|
310
|
+
coinName,
|
|
311
|
+
prefixIndex => {
|
|
312
|
+
const network = bip32NetworkFromCoin({
|
|
313
|
+
coinString: coinName,
|
|
314
|
+
prefixIndex
|
|
315
|
+
})
|
|
316
|
+
return guessAddressTypeFromAddress(address, network, coinName)
|
|
317
|
+
},
|
|
318
|
+
[`Could not determine address type of ${address}`]
|
|
319
|
+
)
|
|
320
|
+
if (addressType == null) throw new Error('Could not determine address type')
|
|
321
|
+
return addressType
|
|
322
|
+
}; exports.getAddressTypeFromAddress = getAddressTypeFromAddress
|
|
323
|
+
|
|
324
|
+
function seedOrMnemonicToXPriv(args) {
|
|
325
|
+
// match hexadecimal number from beginning to end of string
|
|
326
|
+
const isMnemonic = args.seed.includes(' ')
|
|
327
|
+
const seed = isMnemonic
|
|
328
|
+
? bip39.mnemonicToSeedSync(args.seed)
|
|
329
|
+
: Buffer.from(args.seed, 'base64')
|
|
330
|
+
const network = bip32NetworkFromCoin({
|
|
331
|
+
coinString: args.coin,
|
|
332
|
+
sigType: args.type,
|
|
333
|
+
prefixIndex: 0
|
|
334
|
+
})
|
|
335
|
+
const coin = _coinmapper.getCoinFromString.call(void 0, args.coin)
|
|
336
|
+
const purpose = bip43PurposeTypeEnumToNumber(args.type)
|
|
337
|
+
const coinType = _nullishCoalesce(args.coinType, () => ( coin.coinType))
|
|
338
|
+
const account = _nullishCoalesce(args.account, () => ( 0))
|
|
339
|
+
const bip32FromSeedFunc = _nullishCoalesce(coin.bip32FromSeedFunc, () => ( bip32.fromSeed))
|
|
340
|
+
const root = bip32FromSeedFunc(seed)
|
|
341
|
+
root.network = network
|
|
342
|
+
// treat a detected seed as an airbitz seed
|
|
343
|
+
return purpose === 32 || !isMnemonic
|
|
344
|
+
? root.derive(0).toBase58()
|
|
345
|
+
: root
|
|
346
|
+
.deriveHardened(purpose)
|
|
347
|
+
.deriveHardened(coinType)
|
|
348
|
+
.deriveHardened(account)
|
|
349
|
+
.toBase58()
|
|
350
|
+
} exports.seedOrMnemonicToXPriv = seedOrMnemonicToXPriv;
|
|
351
|
+
const xprivToXPubInternal = (
|
|
352
|
+
prefixIndex,
|
|
353
|
+
args
|
|
354
|
+
) => {
|
|
355
|
+
const network = bip32NetworkFromCoin({
|
|
356
|
+
coinString: args.coin,
|
|
357
|
+
sigType: args.type,
|
|
358
|
+
prefixIndex
|
|
359
|
+
})
|
|
360
|
+
const coin = _coinmapper.getCoinFromString.call(void 0, args.coin)
|
|
361
|
+
const bip32FromBase58Func = _nullishCoalesce(coin.bip32FromBase58Func, () => ( bip32.fromBase58))
|
|
362
|
+
return bip32FromBase58Func(args.xpriv, network).neutered().toBase58()
|
|
363
|
+
}
|
|
364
|
+
|
|
365
|
+
const xprivToXPub = (args) => {
|
|
366
|
+
const xpub = filterCoinPrefixes(
|
|
367
|
+
args.coin,
|
|
368
|
+
prefixIndex => xprivToXPubInternal(prefixIndex, args),
|
|
369
|
+
['Invalid private key']
|
|
370
|
+
)
|
|
371
|
+
if (xpub == null) {
|
|
372
|
+
throw Error('unable to convert xpriv to xpub')
|
|
373
|
+
}
|
|
374
|
+
return xpub
|
|
375
|
+
}; exports.xprivToXPub = xprivToXPub
|
|
376
|
+
|
|
377
|
+
function derivationLevelScriptHash(
|
|
378
|
+
scriptTemplate
|
|
379
|
+
) {
|
|
380
|
+
// currently returns the derivation for an empty script template for a bitcoin cash
|
|
381
|
+
// replay protection script (without key material)
|
|
382
|
+
let hash = '0000'
|
|
383
|
+
hash = bitcoin.crypto
|
|
384
|
+
.hash160(Buffer.from(scriptTemplate(''), 'hex'))
|
|
385
|
+
.slice(0, 4)
|
|
386
|
+
.toString('hex')
|
|
387
|
+
return parseInt(hash, 16)
|
|
388
|
+
} exports.derivationLevelScriptHash = derivationLevelScriptHash;
|
|
389
|
+
|
|
390
|
+
const isPathUsingDerivationLevelScriptHash = (
|
|
391
|
+
scriptTemplate,
|
|
392
|
+
path
|
|
393
|
+
) => {
|
|
394
|
+
return path.changeIndex === derivationLevelScriptHash(scriptTemplate)
|
|
395
|
+
}; exports.isPathUsingDerivationLevelScriptHash = isPathUsingDerivationLevelScriptHash
|
|
396
|
+
|
|
397
|
+
const xpubToPubkeyInternal = (
|
|
398
|
+
prefixIndex,
|
|
399
|
+
args
|
|
400
|
+
) => {
|
|
401
|
+
const coin = _coinmapper.getCoinFromString.call(void 0, args.coin)
|
|
402
|
+
const network = bip32NetworkFromCoin({
|
|
403
|
+
coinString: args.coin,
|
|
404
|
+
sigType: args.type,
|
|
405
|
+
prefixIndex
|
|
406
|
+
})
|
|
407
|
+
const bip32FromBase58Func = _nullishCoalesce(coin.bip32FromBase58Func, () => ( bip32.fromBase58))
|
|
408
|
+
const node = bip32FromBase58Func(args.xpub, network)
|
|
409
|
+
return node
|
|
410
|
+
.derive(args.bip44ChangeIndex)
|
|
411
|
+
.derive(args.bip44AddressIndex)
|
|
412
|
+
.publicKey.toString('hex')
|
|
413
|
+
}
|
|
414
|
+
|
|
415
|
+
const xpubToPubkey = (args) => {
|
|
416
|
+
const pubkey = filterCoinPrefixes(
|
|
417
|
+
args.coin,
|
|
418
|
+
prefixIndex => xpubToPubkeyInternal(prefixIndex, args),
|
|
419
|
+
[`Invalid network version`]
|
|
420
|
+
)
|
|
421
|
+
if (pubkey == null) {
|
|
422
|
+
throw Error('unable to derive pubkey from xpub')
|
|
423
|
+
}
|
|
424
|
+
return pubkey
|
|
425
|
+
}; exports.xpubToPubkey = xpubToPubkey
|
|
426
|
+
|
|
427
|
+
const addressToScriptPubkeyInternal = (
|
|
428
|
+
prefixIndex,
|
|
429
|
+
args
|
|
430
|
+
) => {
|
|
431
|
+
const coin = _coinmapper.getCoinFromString.call(void 0, args.coin)
|
|
432
|
+
const cashAddrPrefixes = getCashAddrPrefixes(coin)
|
|
433
|
+
const network = bip32NetworkFromCoin({
|
|
434
|
+
coinString: args.coin,
|
|
435
|
+
prefixIndex
|
|
436
|
+
})
|
|
437
|
+
const selectedPrefixes = selectedCoinPrefixes(coin.prefixes, prefixIndex)
|
|
438
|
+
const addressType = guessAddressTypeFromAddress(
|
|
439
|
+
args.address,
|
|
440
|
+
network,
|
|
441
|
+
args.coin,
|
|
442
|
+
args.addressType
|
|
443
|
+
)
|
|
444
|
+
let payment
|
|
445
|
+
|
|
446
|
+
const maybeScriptHashFromCashAddress = () => {
|
|
447
|
+
try {
|
|
448
|
+
return _cashAddress.cashAddressToHash.call(void 0,
|
|
449
|
+
args.address,
|
|
450
|
+
cashAddrPrefixes
|
|
451
|
+
).scriptHash.toString('hex')
|
|
452
|
+
} catch (e) {}
|
|
453
|
+
}
|
|
454
|
+
|
|
455
|
+
switch (addressType) {
|
|
456
|
+
case AddressTypeEnum.p2pkh:
|
|
457
|
+
if (selectedPrefixes.cashaddr != null) {
|
|
458
|
+
const scriptHash = maybeScriptHashFromCashAddress()
|
|
459
|
+
if (scriptHash != null) {
|
|
460
|
+
const scriptPubkey = scriptHashToScriptPubkey({
|
|
461
|
+
scriptHash,
|
|
462
|
+
scriptType: ScriptTypeEnum.p2pkh,
|
|
463
|
+
coin: args.coin
|
|
464
|
+
})
|
|
465
|
+
if (scriptPubkey == null) {
|
|
466
|
+
throw new Error(
|
|
467
|
+
`unable to convert address ${args.address} to script pubkey`
|
|
468
|
+
)
|
|
469
|
+
}
|
|
470
|
+
return scriptPubkey
|
|
471
|
+
}
|
|
472
|
+
}
|
|
473
|
+
payment = bitcoin.payments.p2pkh
|
|
474
|
+
break
|
|
475
|
+
case AddressTypeEnum.p2sh:
|
|
476
|
+
if (selectedPrefixes.cashaddr != null) {
|
|
477
|
+
const scriptHash = maybeScriptHashFromCashAddress()
|
|
478
|
+
if (scriptHash != null) {
|
|
479
|
+
const scriptPubkey = scriptHashToScriptPubkey({
|
|
480
|
+
scriptHash,
|
|
481
|
+
scriptType: ScriptTypeEnum.p2sh,
|
|
482
|
+
coin: args.coin
|
|
483
|
+
})
|
|
484
|
+
if (scriptPubkey == null) {
|
|
485
|
+
throw new Error(
|
|
486
|
+
`unable to convert address ${args.address} to script pubkey`
|
|
487
|
+
)
|
|
488
|
+
}
|
|
489
|
+
return scriptPubkey
|
|
490
|
+
}
|
|
491
|
+
}
|
|
492
|
+
payment = bitcoin.payments.p2sh
|
|
493
|
+
break
|
|
494
|
+
case AddressTypeEnum.p2wpkh:
|
|
495
|
+
payment = bitcoin.payments.p2wpkh
|
|
496
|
+
break
|
|
497
|
+
case AddressTypeEnum.p2wsh:
|
|
498
|
+
payment = bitcoin.payments.p2wsh
|
|
499
|
+
break
|
|
500
|
+
default:
|
|
501
|
+
throw new Error('invalid address type in address to script pubkey')
|
|
502
|
+
}
|
|
503
|
+
const scriptPubkey = payment({
|
|
504
|
+
address: args.address,
|
|
505
|
+
network: network,
|
|
506
|
+
bs58DecodeFunc: coin.bs58DecodeFunc,
|
|
507
|
+
bs58EncodeFunc: coin.bs58EncodeFunc
|
|
508
|
+
}).output
|
|
509
|
+
if (scriptPubkey == null) {
|
|
510
|
+
throw new Error('failed converting address to scriptPubkey')
|
|
511
|
+
}
|
|
512
|
+
return scriptPubkey.toString('hex')
|
|
513
|
+
}
|
|
514
|
+
|
|
515
|
+
const addressToScriptPubkey = (
|
|
516
|
+
args
|
|
517
|
+
) => {
|
|
518
|
+
const scriptPubkey = filterCoinPrefixes(
|
|
519
|
+
args.coin,
|
|
520
|
+
prefixIndex => addressToScriptPubkeyInternal(prefixIndex, args),
|
|
521
|
+
[
|
|
522
|
+
'Invalid version or Network mismatch',
|
|
523
|
+
'Invalid prefix or Network mismatch',
|
|
524
|
+
`InvalidArgument: ${args.address} has Mixed case`,
|
|
525
|
+
`Could not determine address type of ${args.address}`
|
|
526
|
+
]
|
|
527
|
+
)
|
|
528
|
+
if (scriptPubkey == null) {
|
|
529
|
+
throw new Error(
|
|
530
|
+
`unable to convert address ${args.address} to script pubkey`
|
|
531
|
+
)
|
|
532
|
+
}
|
|
533
|
+
return scriptPubkey
|
|
534
|
+
}; exports.addressToScriptPubkey = addressToScriptPubkey
|
|
535
|
+
|
|
536
|
+
function scriptPubkeyToAddress(
|
|
537
|
+
args
|
|
538
|
+
) {
|
|
539
|
+
const network = bip32NetworkFromCoin({
|
|
540
|
+
coinString: args.coin,
|
|
541
|
+
prefixIndex: 0 // standard serialization at index 0
|
|
542
|
+
})
|
|
543
|
+
const legacyNetwork = bip32NetworkFromCoin({
|
|
544
|
+
coinString: args.coin,
|
|
545
|
+
prefixIndex: 1 // legacy serialziation at index 1
|
|
546
|
+
})
|
|
547
|
+
const coinClass = _coinmapper.getCoinFromString.call(void 0, args.coin)
|
|
548
|
+
const coinPrefixes = coinClass.prefixes
|
|
549
|
+
const standardPrefixes = selectedCoinPrefixes(coinPrefixes, 0)
|
|
550
|
+
const legacyPrefixes = selectedCoinPrefixes(coinPrefixes, 1)
|
|
551
|
+
let address
|
|
552
|
+
let legacyAddress
|
|
553
|
+
let payment
|
|
554
|
+
switch (args.addressType) {
|
|
555
|
+
case AddressTypeEnum.p2pkh:
|
|
556
|
+
if (standardPrefixes.cashaddr != null) {
|
|
557
|
+
address = _cashAddress.hashToCashAddress.call(void 0,
|
|
558
|
+
scriptPubkeyToScriptHash({
|
|
559
|
+
scriptPubkey: args.scriptPubkey,
|
|
560
|
+
scriptType: ScriptTypeEnum.p2pkh,
|
|
561
|
+
coin: args.coin
|
|
562
|
+
}),
|
|
563
|
+
_cashAddress.CashaddrTypeEnum.pubkeyhash,
|
|
564
|
+
standardPrefixes.cashaddr
|
|
565
|
+
)
|
|
566
|
+
}
|
|
567
|
+
if (legacyPrefixes.cashaddr != null) {
|
|
568
|
+
legacyAddress = _cashAddress.hashToCashAddress.call(void 0,
|
|
569
|
+
scriptPubkeyToScriptHash({
|
|
570
|
+
scriptPubkey: args.scriptPubkey,
|
|
571
|
+
scriptType: ScriptTypeEnum.p2pkh,
|
|
572
|
+
coin: args.coin
|
|
573
|
+
}),
|
|
574
|
+
_cashAddress.CashaddrTypeEnum.pubkeyhash,
|
|
575
|
+
legacyPrefixes.cashaddr
|
|
576
|
+
)
|
|
577
|
+
}
|
|
578
|
+
payment = bitcoin.payments.p2pkh
|
|
579
|
+
break
|
|
580
|
+
case AddressTypeEnum.p2sh:
|
|
581
|
+
if (standardPrefixes.cashaddr != null) {
|
|
582
|
+
address = _cashAddress.hashToCashAddress.call(void 0,
|
|
583
|
+
scriptPubkeyToScriptHash({
|
|
584
|
+
scriptPubkey: args.scriptPubkey,
|
|
585
|
+
scriptType: ScriptTypeEnum.p2sh,
|
|
586
|
+
coin: args.coin
|
|
587
|
+
}),
|
|
588
|
+
_cashAddress.CashaddrTypeEnum.scripthash,
|
|
589
|
+
standardPrefixes.cashaddr
|
|
590
|
+
)
|
|
591
|
+
}
|
|
592
|
+
if (legacyPrefixes.cashaddr != null) {
|
|
593
|
+
legacyAddress = _cashAddress.hashToCashAddress.call(void 0,
|
|
594
|
+
scriptPubkeyToScriptHash({
|
|
595
|
+
scriptPubkey: args.scriptPubkey,
|
|
596
|
+
scriptType: ScriptTypeEnum.p2sh,
|
|
597
|
+
coin: args.coin
|
|
598
|
+
}),
|
|
599
|
+
_cashAddress.CashaddrTypeEnum.scripthash,
|
|
600
|
+
legacyPrefixes.cashaddr
|
|
601
|
+
)
|
|
602
|
+
}
|
|
603
|
+
payment = bitcoin.payments.p2sh
|
|
604
|
+
break
|
|
605
|
+
case AddressTypeEnum.p2wpkh:
|
|
606
|
+
payment = bitcoin.payments.p2wpkh
|
|
607
|
+
break
|
|
608
|
+
case AddressTypeEnum.p2wsh:
|
|
609
|
+
payment = bitcoin.payments.p2wsh
|
|
610
|
+
break
|
|
611
|
+
default:
|
|
612
|
+
throw new Error('invalid address type in address to script pubkey')
|
|
613
|
+
}
|
|
614
|
+
address =
|
|
615
|
+
_nullishCoalesce(address, () => (
|
|
616
|
+
payment({
|
|
617
|
+
output: Buffer.from(args.scriptPubkey, 'hex'),
|
|
618
|
+
network: network,
|
|
619
|
+
bs58DecodeFunc: coinClass.bs58DecodeFunc,
|
|
620
|
+
bs58EncodeFunc: coinClass.bs58EncodeFunc
|
|
621
|
+
}).address))
|
|
622
|
+
|
|
623
|
+
legacyAddress =
|
|
624
|
+
_nullishCoalesce(legacyAddress, () => (
|
|
625
|
+
payment({
|
|
626
|
+
output: Buffer.from(args.scriptPubkey, 'hex'),
|
|
627
|
+
network: legacyNetwork,
|
|
628
|
+
bs58DecodeFunc: coinClass.bs58DecodeFunc,
|
|
629
|
+
bs58EncodeFunc: coinClass.bs58EncodeFunc
|
|
630
|
+
}).address))
|
|
631
|
+
|
|
632
|
+
if (address == null || legacyAddress == null) {
|
|
633
|
+
throw new Error('failed converting scriptPubkey to address')
|
|
634
|
+
}
|
|
635
|
+
return { address, legacyAddress }
|
|
636
|
+
} exports.scriptPubkeyToAddress = scriptPubkeyToAddress;
|
|
637
|
+
|
|
638
|
+
function scriptPubkeyToScriptHash(
|
|
639
|
+
args
|
|
640
|
+
) {
|
|
641
|
+
const network = bip32NetworkFromCoin({
|
|
642
|
+
coinString: args.coin,
|
|
643
|
+
prefixIndex: 0
|
|
644
|
+
})
|
|
645
|
+
let payment
|
|
646
|
+
switch (args.scriptType) {
|
|
647
|
+
case ScriptTypeEnum.p2pkh:
|
|
648
|
+
payment = bitcoin.payments.p2pkh
|
|
649
|
+
break
|
|
650
|
+
case ScriptTypeEnum.p2sh:
|
|
651
|
+
payment = bitcoin.payments.p2sh
|
|
652
|
+
break
|
|
653
|
+
case ScriptTypeEnum.p2wpkh:
|
|
654
|
+
payment = bitcoin.payments.p2wpkh
|
|
655
|
+
break
|
|
656
|
+
default:
|
|
657
|
+
throw new Error('invalid address type in address to script pubkey')
|
|
658
|
+
}
|
|
659
|
+
const scriptHash = payment({
|
|
660
|
+
output: Buffer.from(args.scriptPubkey, 'hex'),
|
|
661
|
+
network: network
|
|
662
|
+
}).hash
|
|
663
|
+
if (scriptHash == null) {
|
|
664
|
+
throw new Error('failed converting scriptPubkey to address')
|
|
665
|
+
}
|
|
666
|
+
return scriptHash.toString('hex')
|
|
667
|
+
} exports.scriptPubkeyToScriptHash = scriptPubkeyToScriptHash;
|
|
668
|
+
|
|
669
|
+
function scriptPubkeyToP2SH(
|
|
670
|
+
args
|
|
671
|
+
) {
|
|
672
|
+
const { scriptPubkey, coin } = args
|
|
673
|
+
let bip32Network
|
|
674
|
+
if (coin != null) {
|
|
675
|
+
bip32Network = bip32NetworkFromCoin({
|
|
676
|
+
coinString: coin,
|
|
677
|
+
prefixIndex: 0
|
|
678
|
+
})
|
|
679
|
+
}
|
|
680
|
+
const p2sh = bitcoin.payments.p2sh({
|
|
681
|
+
network: bip32Network,
|
|
682
|
+
redeem: {
|
|
683
|
+
output: Buffer.from(scriptPubkey, 'hex')
|
|
684
|
+
}
|
|
685
|
+
})
|
|
686
|
+
if (p2sh.output == null || _optionalChain([p2sh, 'access', _ => _.redeem, 'optionalAccess', _2 => _2.output]) == null) {
|
|
687
|
+
throw new Error('unable to convert script to p2sh')
|
|
688
|
+
}
|
|
689
|
+
return {
|
|
690
|
+
scriptPubkey: p2sh.output.toString('hex'),
|
|
691
|
+
redeemScript: p2sh.redeem.output.toString('hex'),
|
|
692
|
+
address: p2sh.address
|
|
693
|
+
}
|
|
694
|
+
} exports.scriptPubkeyToP2SH = scriptPubkeyToP2SH;
|
|
695
|
+
|
|
696
|
+
function pubkeyToScriptPubkey(
|
|
697
|
+
args
|
|
698
|
+
) {
|
|
699
|
+
let payment
|
|
700
|
+
switch (args.scriptType) {
|
|
701
|
+
case ScriptTypeEnum.p2pkh:
|
|
702
|
+
payment = bitcoin.payments.p2pkh({
|
|
703
|
+
pubkey: Buffer.from(args.pubkey, 'hex')
|
|
704
|
+
})
|
|
705
|
+
if (payment.output == null) {
|
|
706
|
+
throw new Error('failed converting pubkey to script pubkey')
|
|
707
|
+
}
|
|
708
|
+
return { scriptPubkey: payment.output.toString('hex') }
|
|
709
|
+
case ScriptTypeEnum.p2wpkhp2sh:
|
|
710
|
+
return scriptPubkeyToP2SH({
|
|
711
|
+
scriptPubkey: pubkeyToScriptPubkey({
|
|
712
|
+
pubkey: args.pubkey,
|
|
713
|
+
scriptType: ScriptTypeEnum.p2wpkh
|
|
714
|
+
}).scriptPubkey
|
|
715
|
+
})
|
|
716
|
+
case ScriptTypeEnum.p2wpkh:
|
|
717
|
+
payment = bitcoin.payments.p2wpkh({
|
|
718
|
+
pubkey: Buffer.from(args.pubkey, 'hex')
|
|
719
|
+
})
|
|
720
|
+
if (payment.output == null) {
|
|
721
|
+
throw new Error('failed converting pubkey to script pubkey')
|
|
722
|
+
}
|
|
723
|
+
return { scriptPubkey: payment.output.toString('hex') }
|
|
724
|
+
default:
|
|
725
|
+
throw new Error('invalid address type in pubkey to script pubkey')
|
|
726
|
+
}
|
|
727
|
+
} exports.pubkeyToScriptPubkey = pubkeyToScriptPubkey;
|
|
728
|
+
|
|
729
|
+
const toNewFormat = (address, coinName) => {
|
|
730
|
+
const addressType = exports.getAddressTypeFromAddress.call(void 0, address, coinName)
|
|
731
|
+
return scriptPubkeyToAddress({
|
|
732
|
+
scriptPubkey: exports.addressToScriptPubkey.call(void 0, {
|
|
733
|
+
address: address,
|
|
734
|
+
coin: coinName
|
|
735
|
+
}),
|
|
736
|
+
addressType,
|
|
737
|
+
coin: coinName
|
|
738
|
+
}).address
|
|
739
|
+
}; exports.toNewFormat = toNewFormat
|
|
740
|
+
|
|
741
|
+
const xprivToPrivateKeyInternal = (
|
|
742
|
+
prefixIndex,
|
|
743
|
+
args
|
|
744
|
+
) => {
|
|
745
|
+
const coin = _coinmapper.getCoinFromString.call(void 0, args.coin)
|
|
746
|
+
const network = bip32NetworkFromCoin({
|
|
747
|
+
coinString: args.coin,
|
|
748
|
+
sigType: args.type,
|
|
749
|
+
prefixIndex
|
|
750
|
+
})
|
|
751
|
+
const bip32FromBase58Func = _nullishCoalesce(coin.bip32FromBase58Func, () => ( bip32.fromBase58))
|
|
752
|
+
const node = bip32FromBase58Func(args.xpriv, network)
|
|
753
|
+
return _optionalChain([node
|
|
754
|
+
, 'access', _3 => _3.derive, 'call', _4 => _4(args.bip44ChangeIndex)
|
|
755
|
+
, 'access', _5 => _5.derive, 'call', _6 => _6(args.bip44AddressIndex)
|
|
756
|
+
, 'access', _7 => _7.privateKey, 'optionalAccess', _8 => _8.toString, 'call', _9 => _9('hex')])
|
|
757
|
+
}
|
|
758
|
+
|
|
759
|
+
const xprivToPrivateKey = (args) => {
|
|
760
|
+
const privateKey = filterCoinPrefixes(
|
|
761
|
+
args.coin,
|
|
762
|
+
prefixIndex => xprivToPrivateKeyInternal(prefixIndex, args),
|
|
763
|
+
['Invalid network version']
|
|
764
|
+
)
|
|
765
|
+
if (privateKey == null) {
|
|
766
|
+
throw new Error('Failed to generate private key from xpriv')
|
|
767
|
+
}
|
|
768
|
+
return privateKey
|
|
769
|
+
}; exports.xprivToPrivateKey = xprivToPrivateKey
|
|
770
|
+
|
|
771
|
+
function privateKeyToWIF(args) {
|
|
772
|
+
const network = bip32NetworkFromCoin({
|
|
773
|
+
coinString: args.coin,
|
|
774
|
+
forWIF: true,
|
|
775
|
+
prefixIndex: 0
|
|
776
|
+
})
|
|
777
|
+
const coinClass = _coinmapper.getCoinFromString.call(void 0, args.coin)
|
|
778
|
+
return bitcoin.ECPair.fromPrivateKey(Buffer.from(args.privateKey, 'hex'), {
|
|
779
|
+
network
|
|
780
|
+
}).toWIF(coinClass.wifEncodeFunc)
|
|
781
|
+
} exports.privateKeyToWIF = privateKeyToWIF;
|
|
782
|
+
|
|
783
|
+
const wifToPrivateKeyInternal = (
|
|
784
|
+
prefixIndex,
|
|
785
|
+
args
|
|
786
|
+
) => {
|
|
787
|
+
const coin = _coinmapper.getCoinFromString.call(void 0, args.coin)
|
|
788
|
+
const network = bip32NetworkFromCoin({
|
|
789
|
+
coinString: args.coin,
|
|
790
|
+
forWIF: true,
|
|
791
|
+
prefixIndex
|
|
792
|
+
})
|
|
793
|
+
return _optionalChain([bitcoin, 'access', _10 => _10.ECPair, 'access', _11 => _11.fromWIF, 'call', _12 => _12(
|
|
794
|
+
args.wifKey,
|
|
795
|
+
network,
|
|
796
|
+
coin.bs58DecodeFunc
|
|
797
|
+
), 'access', _13 => _13.privateKey, 'optionalAccess', _14 => _14.toString, 'call', _15 => _15('hex')])
|
|
798
|
+
}
|
|
799
|
+
const wifToPrivateKey = (args) => {
|
|
800
|
+
const privateKey = filterCoinPrefixes(
|
|
801
|
+
args.coin,
|
|
802
|
+
prefixIndex => wifToPrivateKeyInternal(prefixIndex, args),
|
|
803
|
+
['Invalid network version']
|
|
804
|
+
)
|
|
805
|
+
if (privateKey == null) {
|
|
806
|
+
throw new Error('Failed to convert WIF key to private key')
|
|
807
|
+
}
|
|
808
|
+
return privateKey
|
|
809
|
+
}; exports.wifToPrivateKey = wifToPrivateKey
|
|
810
|
+
|
|
811
|
+
function privateKeyToPubkey(privateKey) {
|
|
812
|
+
return bitcoin.ECPair.fromPrivateKey(
|
|
813
|
+
Buffer.from(privateKey, 'hex')
|
|
814
|
+
).publicKey.toString('hex')
|
|
815
|
+
} exports.privateKeyToPubkey = privateKeyToPubkey;
|
|
816
|
+
|
|
817
|
+
function signMessageBase64(message, privateKey) {
|
|
818
|
+
const keyPair = bitcoin.ECPair.fromPrivateKey(Buffer.from(privateKey, 'hex'))
|
|
819
|
+
if (keyPair.privateKey == null) {
|
|
820
|
+
throw new Error('Address could not sign message')
|
|
821
|
+
}
|
|
822
|
+
return _bitcoinjsmessage2.default
|
|
823
|
+
.sign(message, keyPair.privateKey, keyPair.compressed)
|
|
824
|
+
.toString('base64')
|
|
825
|
+
} exports.signMessageBase64 = signMessageBase64;
|
|
826
|
+
|
|
827
|
+
async function makeTx(args) {
|
|
828
|
+
let sequence = 0xffffffff
|
|
829
|
+
if (args.setRBF) {
|
|
830
|
+
sequence -= 2
|
|
831
|
+
}
|
|
832
|
+
|
|
833
|
+
// get coin specific replay protection sighhash bits
|
|
834
|
+
let sighashType = bitcoin.Transaction.SIGHASH_ALL
|
|
835
|
+
const coin = _coinmapper.getCoinFromString.call(void 0, args.coin)
|
|
836
|
+
if (coin.sighash != null) {
|
|
837
|
+
sighashType = coin.sighash
|
|
838
|
+
}
|
|
839
|
+
|
|
840
|
+
const useUtxos = []
|
|
841
|
+
const mappedUtxos = []
|
|
842
|
+
|
|
843
|
+
const mergedArray = [...args.utxos, ...args.forceUseUtxo]
|
|
844
|
+
const uniqueUtxos = [
|
|
845
|
+
...mergedArray
|
|
846
|
+
.filter(utxo => !utxo.spent)
|
|
847
|
+
.reduce((map, obj) => map.set(obj.id, obj), new Map())
|
|
848
|
+
.values()
|
|
849
|
+
].sort((a, b) => {
|
|
850
|
+
if (a.blockHeight <= 0 && b.blockHeight > 0) return 1
|
|
851
|
+
if (b.blockHeight <= 0 && a.blockHeight > 0) return -1
|
|
852
|
+
return _biggystring.lt.call(void 0, a.value, b.value) ? 1 : _biggystring.gt.call(void 0, a.value, b.value) ? -1 : 0
|
|
853
|
+
})
|
|
854
|
+
|
|
855
|
+
for (const utxo of uniqueUtxos) {
|
|
856
|
+
// Cannot use a utxo without a script
|
|
857
|
+
if (utxo.script == null) continue
|
|
858
|
+
const input = {
|
|
859
|
+
hash: Buffer.from(utxo.txid, 'hex').reverse(),
|
|
860
|
+
index: utxo.vout,
|
|
861
|
+
value: parseInt(utxo.value),
|
|
862
|
+
script: Buffer.from(utxo.script, 'hex'),
|
|
863
|
+
scriptPubkey: Buffer.from(utxo.scriptPubkey, 'hex'),
|
|
864
|
+
scriptType: utxo.scriptType,
|
|
865
|
+
sequence,
|
|
866
|
+
sighashType
|
|
867
|
+
}
|
|
868
|
+
// Map script type to the correct PsbtInput UTXO field
|
|
869
|
+
switch (utxo.scriptType) {
|
|
870
|
+
// Non-segwit
|
|
871
|
+
case ScriptTypeEnum.p2pk:
|
|
872
|
+
case ScriptTypeEnum.p2pkh:
|
|
873
|
+
case ScriptTypeEnum.p2sh:
|
|
874
|
+
case ScriptTypeEnum.replayProtection:
|
|
875
|
+
case ScriptTypeEnum.replayProtectionP2SH: {
|
|
876
|
+
input.nonWitnessUtxo = input.script
|
|
877
|
+
break
|
|
878
|
+
}
|
|
879
|
+
// Segwit
|
|
880
|
+
case ScriptTypeEnum.p2wpkh:
|
|
881
|
+
case ScriptTypeEnum.p2wpkhp2sh:
|
|
882
|
+
case ScriptTypeEnum.p2wsh: {
|
|
883
|
+
input.witnessUtxo = {
|
|
884
|
+
script: input.script,
|
|
885
|
+
value: parseInt(utxo.value)
|
|
886
|
+
}
|
|
887
|
+
break
|
|
888
|
+
}
|
|
889
|
+
default:
|
|
890
|
+
throw new Error(`unknown script type ${utxo.scriptType}`)
|
|
891
|
+
}
|
|
892
|
+
if (utxo.redeemScript != null) {
|
|
893
|
+
input.redeemScript = Buffer.from(utxo.redeemScript, 'hex')
|
|
894
|
+
}
|
|
895
|
+
let forceUsage = false
|
|
896
|
+
for (const forceUtxo of args.forceUseUtxo) {
|
|
897
|
+
if (forceUtxo.id === utxo.id) {
|
|
898
|
+
useUtxos.push(input)
|
|
899
|
+
forceUsage = true
|
|
900
|
+
}
|
|
901
|
+
}
|
|
902
|
+
if (!forceUsage) mappedUtxos.push(input)
|
|
903
|
+
}
|
|
904
|
+
|
|
905
|
+
const targets = args.targets.map(target => {
|
|
906
|
+
const script = exports.addressToScriptPubkey.call(void 0, {
|
|
907
|
+
address: target.address,
|
|
908
|
+
coin: coin.name
|
|
909
|
+
})
|
|
910
|
+
return {
|
|
911
|
+
script,
|
|
912
|
+
value: target.value
|
|
913
|
+
}
|
|
914
|
+
})
|
|
915
|
+
const changeScript = exports.addressToScriptPubkey.call(void 0, {
|
|
916
|
+
address: args.freshChangeAddress,
|
|
917
|
+
coin: coin.name
|
|
918
|
+
})
|
|
919
|
+
|
|
920
|
+
const utxoPicker = _nullishCoalesce(coin.utxoPicker, () => ( utxopicker.utxoPicker))
|
|
921
|
+
|
|
922
|
+
const utxopicking =
|
|
923
|
+
args.forceUseUtxo.length > 0
|
|
924
|
+
? utxoPicker.forceUseUtxo
|
|
925
|
+
: _nullishCoalesce(args.subtractFee, () => ( false))
|
|
926
|
+
? utxoPicker.subtractFee
|
|
927
|
+
: utxoPicker.accumulative
|
|
928
|
+
const result = utxopicking({
|
|
929
|
+
utxos: mappedUtxos,
|
|
930
|
+
useUtxos,
|
|
931
|
+
targets,
|
|
932
|
+
feeRate: args.feeRate,
|
|
933
|
+
changeScript
|
|
934
|
+
})
|
|
935
|
+
if (result.inputs == null || result.outputs == null) {
|
|
936
|
+
throw new (0, _types.InsufficientFundsError)(args.coin)
|
|
937
|
+
}
|
|
938
|
+
|
|
939
|
+
const sortedInputs = _bip69.sortInputs.call(void 0, result.inputs)
|
|
940
|
+
const sortedOutputs = _bip69.sortOutputs.call(void 0, result.outputs)
|
|
941
|
+
|
|
942
|
+
const psbt = new bitcoin.Psbt()
|
|
943
|
+
psbt.addInputs(sortedInputs)
|
|
944
|
+
psbt.addOutputs(sortedOutputs)
|
|
945
|
+
|
|
946
|
+
return {
|
|
947
|
+
inputs: sortedInputs,
|
|
948
|
+
outputs: sortedOutputs,
|
|
949
|
+
changeUsed: result.changeUsed,
|
|
950
|
+
fee: result.fee,
|
|
951
|
+
psbtBase64: psbt.toBase64(),
|
|
952
|
+
hex: _optionalChain([psbt, 'access', _16 => _16.data, 'optionalAccess', _17 => _17.globalMap, 'optionalAccess', _18 => _18.unsignedTx, 'optionalAccess', _19 => _19.toBuffer, 'call', _20 => _20(), 'optionalAccess', _21 => _21.toString, 'call', _22 => _22('hex')])
|
|
953
|
+
}
|
|
954
|
+
} exports.makeTx = makeTx;
|
|
955
|
+
|
|
956
|
+
async function signTx(args) {
|
|
957
|
+
const psbt = bitcoin.Psbt.fromBase64(args.psbtBase64)
|
|
958
|
+
const coin = _coinmapper.getCoinFromString.call(void 0, args.coin)
|
|
959
|
+
|
|
960
|
+
for (let i = 0; i < psbt.inputCount; i++) {
|
|
961
|
+
const privateKey = Buffer.from(
|
|
962
|
+
_nullishCoalesce(args.privateKeys[i], () => ( args.privateKeys[args.privateKeys.length - 1])),
|
|
963
|
+
'hex'
|
|
964
|
+
)
|
|
965
|
+
psbt.signInput(
|
|
966
|
+
i,
|
|
967
|
+
bitcoin.ECPair.fromPrivateKey(privateKey),
|
|
968
|
+
bitcoin.Psbt.DEFAULT_SIGHASHES,
|
|
969
|
+
coin.sighashFunction
|
|
970
|
+
)
|
|
971
|
+
psbt.validateSignaturesOfInput(i)
|
|
972
|
+
psbt.finalizeInput(i)
|
|
973
|
+
}
|
|
974
|
+
const tx = psbt.extractTransaction(true)
|
|
975
|
+
return {
|
|
976
|
+
id: tx.getId(coin.txHashFunction),
|
|
977
|
+
hex: tx.toHex()
|
|
978
|
+
}
|
|
979
|
+
} exports.signTx = signTx;
|
|
980
|
+
|
|
981
|
+
// ---------------------------------------------------------------------
|
|
982
|
+
// Internal Utilities
|
|
983
|
+
// ---------------------------------------------------------------------
|
|
984
|
+
|
|
985
|
+
const bip32NetworkFromCoin = (
|
|
986
|
+
args
|
|
987
|
+
) => {
|
|
988
|
+
const sigType = _nullishCoalesce(args.sigType, () => ( BIP43PurposeTypeEnum.Legacy))
|
|
989
|
+
const forWIF = _nullishCoalesce(args.forWIF, () => ( false))
|
|
990
|
+
const coin = _coinmapper.getCoinFromString.call(void 0, args.coinString)
|
|
991
|
+
return bip32NetworkFromCoinPrefix(
|
|
992
|
+
sigType,
|
|
993
|
+
coin.prefixes,
|
|
994
|
+
coin.segwit,
|
|
995
|
+
forWIF,
|
|
996
|
+
args.prefixIndex
|
|
997
|
+
)
|
|
998
|
+
}
|
|
999
|
+
|
|
1000
|
+
const bip32NetworkFromCoinPrefix = (
|
|
1001
|
+
sigType,
|
|
1002
|
+
coinPrefixes,
|
|
1003
|
+
_segwit,
|
|
1004
|
+
forWIF,
|
|
1005
|
+
prefixIndex
|
|
1006
|
+
) => {
|
|
1007
|
+
let xKeyPrefixes
|
|
1008
|
+
const prefixes = selectedCoinPrefixes(coinPrefixes, prefixIndex)
|
|
1009
|
+
|
|
1010
|
+
switch (sigType) {
|
|
1011
|
+
case BIP43PurposeTypeEnum.Segwit:
|
|
1012
|
+
if (prefixes.segwitXPub == null || prefixes.segwitXPriv == null) {
|
|
1013
|
+
throw new Error('segwit xpub prefix is undefined')
|
|
1014
|
+
}
|
|
1015
|
+
xKeyPrefixes = {
|
|
1016
|
+
public: prefixes.segwitXPub,
|
|
1017
|
+
private: prefixes.segwitXPriv
|
|
1018
|
+
}
|
|
1019
|
+
break
|
|
1020
|
+
case BIP43PurposeTypeEnum.WrappedSegwit:
|
|
1021
|
+
if (
|
|
1022
|
+
prefixes.wrappedSegwitXPub == null ||
|
|
1023
|
+
prefixes.wrappedSegwitXPriv == null
|
|
1024
|
+
) {
|
|
1025
|
+
throw new Error('wrapped segwit xpub prefix is undefined')
|
|
1026
|
+
}
|
|
1027
|
+
xKeyPrefixes = {
|
|
1028
|
+
public: prefixes.wrappedSegwitXPub,
|
|
1029
|
+
private: prefixes.wrappedSegwitXPriv
|
|
1030
|
+
}
|
|
1031
|
+
break
|
|
1032
|
+
case BIP43PurposeTypeEnum.Legacy:
|
|
1033
|
+
case BIP43PurposeTypeEnum.Airbitz:
|
|
1034
|
+
xKeyPrefixes = {
|
|
1035
|
+
public: prefixes.legacyXPub,
|
|
1036
|
+
private: prefixes.legacyXPriv
|
|
1037
|
+
}
|
|
1038
|
+
break
|
|
1039
|
+
default:
|
|
1040
|
+
throw new Error('invalid bip43 purpose type')
|
|
1041
|
+
}
|
|
1042
|
+
|
|
1043
|
+
const bech32 = _nullishCoalesce(prefixes.bech32, () => ( ''))
|
|
1044
|
+
if (forWIF) {
|
|
1045
|
+
return {
|
|
1046
|
+
messagePrefix: prefixes.messagePrefix,
|
|
1047
|
+
wif: prefixes.wif,
|
|
1048
|
+
bip32: xKeyPrefixes,
|
|
1049
|
+
bech32: bech32,
|
|
1050
|
+
pubKeyHash: prefixes.pubkeyHash,
|
|
1051
|
+
scriptHash: prefixes.scriptHash
|
|
1052
|
+
}
|
|
1053
|
+
}
|
|
1054
|
+
return {
|
|
1055
|
+
messagePrefix: prefixes.messagePrefix,
|
|
1056
|
+
wif: 0x80,
|
|
1057
|
+
bip32: xKeyPrefixes,
|
|
1058
|
+
bech32: bech32,
|
|
1059
|
+
pubKeyHash: prefixes.pubkeyHash,
|
|
1060
|
+
scriptHash: prefixes.scriptHash
|
|
1061
|
+
}
|
|
1062
|
+
}
|
|
1063
|
+
|
|
1064
|
+
const bip43PurposeTypeEnumToNumber = (
|
|
1065
|
+
purpose
|
|
1066
|
+
) => {
|
|
1067
|
+
switch (purpose) {
|
|
1068
|
+
case BIP43PurposeTypeEnum.Airbitz:
|
|
1069
|
+
return 32
|
|
1070
|
+
case BIP43PurposeTypeEnum.Legacy:
|
|
1071
|
+
return 44
|
|
1072
|
+
case BIP43PurposeTypeEnum.WrappedSegwit:
|
|
1073
|
+
return 49
|
|
1074
|
+
case BIP43PurposeTypeEnum.Segwit:
|
|
1075
|
+
return 84
|
|
1076
|
+
}
|
|
1077
|
+
}
|
|
1078
|
+
|
|
1079
|
+
/**
|
|
1080
|
+
* Filters through a coin's prefixes calling a filter function for each coin
|
|
1081
|
+
* prefixIndex until the function returns a value.
|
|
1082
|
+
* If the function throws an error, the error is can be ignored if it's message
|
|
1083
|
+
* matches one of the given ignoredErrorMessages.
|
|
1084
|
+
*/
|
|
1085
|
+
const filterCoinPrefixes = (
|
|
1086
|
+
coinName,
|
|
1087
|
+
filterer,
|
|
1088
|
+
ignoredErrorMessages = []
|
|
1089
|
+
) => {
|
|
1090
|
+
const coinPrefixes = _coinmapper.getCoinFromString.call(void 0, coinName).prefixes
|
|
1091
|
+
const maxLength = Object.values(coinPrefixes).reduce(
|
|
1092
|
+
(max, prefixes) => Math.max(prefixes.length, max),
|
|
1093
|
+
0
|
|
1094
|
+
)
|
|
1095
|
+
let out
|
|
1096
|
+
for (let prefixIndex = 0; prefixIndex < maxLength; prefixIndex++) {
|
|
1097
|
+
try {
|
|
1098
|
+
out = filterer(prefixIndex)
|
|
1099
|
+
} catch (e) {
|
|
1100
|
+
if (ignoredErrorMessages.includes(e.message)) {
|
|
1101
|
+
continue
|
|
1102
|
+
}
|
|
1103
|
+
throw e
|
|
1104
|
+
}
|
|
1105
|
+
if (out != null) break
|
|
1106
|
+
}
|
|
1107
|
+
return out
|
|
1108
|
+
}
|
|
1109
|
+
|
|
1110
|
+
const getCashAddrPrefixes = (coinInfo) => {
|
|
1111
|
+
return _nullishCoalesce(coinInfo.prefixes.cashaddr, () => ( []))
|
|
1112
|
+
}
|
|
1113
|
+
|
|
1114
|
+
const guessAddressTypeFromAddress = (
|
|
1115
|
+
address,
|
|
1116
|
+
network,
|
|
1117
|
+
coin,
|
|
1118
|
+
addressType
|
|
1119
|
+
) => {
|
|
1120
|
+
if (addressType != null) {
|
|
1121
|
+
return addressType
|
|
1122
|
+
}
|
|
1123
|
+
const coinClass = _coinmapper.getCoinFromString.call(void 0, coin)
|
|
1124
|
+
try {
|
|
1125
|
+
bitcoin.payments.p2pkh({
|
|
1126
|
+
address,
|
|
1127
|
+
network,
|
|
1128
|
+
bs58DecodeFunc: coinClass.bs58DecodeFunc,
|
|
1129
|
+
bs58EncodeFunc: coinClass.bs58EncodeFunc
|
|
1130
|
+
})
|
|
1131
|
+
return AddressTypeEnum.p2pkh
|
|
1132
|
+
} catch (e) {}
|
|
1133
|
+
try {
|
|
1134
|
+
bitcoin.payments.p2sh({
|
|
1135
|
+
address,
|
|
1136
|
+
network,
|
|
1137
|
+
bs58DecodeFunc: coinClass.bs58DecodeFunc,
|
|
1138
|
+
bs58EncodeFunc: coinClass.bs58EncodeFunc
|
|
1139
|
+
})
|
|
1140
|
+
return AddressTypeEnum.p2sh
|
|
1141
|
+
} catch (e) {}
|
|
1142
|
+
try {
|
|
1143
|
+
bitcoin.payments.p2wsh({ address, network })
|
|
1144
|
+
return AddressTypeEnum.p2wsh
|
|
1145
|
+
} catch (e) {}
|
|
1146
|
+
try {
|
|
1147
|
+
bitcoin.payments.p2wpkh({ address, network })
|
|
1148
|
+
return AddressTypeEnum.p2wpkh
|
|
1149
|
+
} catch (e) {}
|
|
1150
|
+
const cashAddrPrefixes = getCashAddrPrefixes(coinClass)
|
|
1151
|
+
if (cashAddrPrefixes.length > 0) {
|
|
1152
|
+
try {
|
|
1153
|
+
const info = _cashAddress.cashAddressToHash.call(void 0, address, cashAddrPrefixes)
|
|
1154
|
+
if (info.type === _cashAddress.CashaddrTypeEnum.pubkeyhash) {
|
|
1155
|
+
return AddressTypeEnum.p2pkh
|
|
1156
|
+
}
|
|
1157
|
+
return AddressTypeEnum.p2sh
|
|
1158
|
+
} catch (e) {}
|
|
1159
|
+
}
|
|
1160
|
+
throw new Error(`Could not determine address type of ${address}`)
|
|
1161
|
+
}
|
|
1162
|
+
|
|
1163
|
+
const selectedCoinPrefixes = (
|
|
1164
|
+
prefixes,
|
|
1165
|
+
index
|
|
1166
|
+
) => {
|
|
1167
|
+
return {
|
|
1168
|
+
messagePrefix: _indexAtProtected.indexAtProtected.call(void 0, prefixes.messagePrefix, index),
|
|
1169
|
+
wif: _indexAtProtected.indexAtProtected.call(void 0, prefixes.wif, index),
|
|
1170
|
+
legacyXPriv: _indexAtProtected.indexAtProtected.call(void 0, prefixes.legacyXPriv, index),
|
|
1171
|
+
legacyXPub: _indexAtProtected.indexAtProtected.call(void 0, prefixes.legacyXPub, index),
|
|
1172
|
+
wrappedSegwitXPriv: _indexAtProtected.indexAtProtected.call(void 0, prefixes.wrappedSegwitXPriv, index),
|
|
1173
|
+
wrappedSegwitXPub: _indexAtProtected.indexAtProtected.call(void 0, prefixes.wrappedSegwitXPub, index),
|
|
1174
|
+
segwitXPriv: _indexAtProtected.indexAtProtected.call(void 0, prefixes.segwitXPriv, index),
|
|
1175
|
+
segwitXPub: _indexAtProtected.indexAtProtected.call(void 0, prefixes.segwitXPub, index),
|
|
1176
|
+
pubkeyHash: _indexAtProtected.indexAtProtected.call(void 0, prefixes.pubkeyHash, index),
|
|
1177
|
+
scriptHash: _indexAtProtected.indexAtProtected.call(void 0, prefixes.scriptHash, index),
|
|
1178
|
+
bech32: _indexAtProtected.indexAtProtected.call(void 0, prefixes.bech32, index),
|
|
1179
|
+
// Special handling for cashaddr to accommodate legacy addresses.
|
|
1180
|
+
// By allowing them to overflow we will fallback to p2pkh.
|
|
1181
|
+
cashaddr: _undefinedIfEmptyString.undefinedIfEmptyString.call(void 0, (_nullishCoalesce(prefixes.cashaddr, () => ( [])))[index])
|
|
1182
|
+
}
|
|
1183
|
+
}
|
|
1184
|
+
|
|
1185
|
+
const scriptHashToScriptPubkey = (
|
|
1186
|
+
args
|
|
1187
|
+
) => {
|
|
1188
|
+
const network = bip32NetworkFromCoin({
|
|
1189
|
+
coinString: args.coin,
|
|
1190
|
+
prefixIndex: 0
|
|
1191
|
+
})
|
|
1192
|
+
let payment
|
|
1193
|
+
switch (args.scriptType) {
|
|
1194
|
+
case ScriptTypeEnum.p2pkh:
|
|
1195
|
+
payment = bitcoin.payments.p2pkh
|
|
1196
|
+
break
|
|
1197
|
+
case ScriptTypeEnum.p2sh:
|
|
1198
|
+
payment = bitcoin.payments.p2sh
|
|
1199
|
+
break
|
|
1200
|
+
case ScriptTypeEnum.p2wpkh:
|
|
1201
|
+
payment = bitcoin.payments.p2wpkh
|
|
1202
|
+
break
|
|
1203
|
+
default:
|
|
1204
|
+
throw new Error('invalid address type in address to script pubkey')
|
|
1205
|
+
}
|
|
1206
|
+
const scriptPubkey = payment({
|
|
1207
|
+
hash: Buffer.from(args.scriptHash, 'hex'),
|
|
1208
|
+
network: network
|
|
1209
|
+
}).output
|
|
1210
|
+
if (scriptPubkey == null) {
|
|
1211
|
+
throw new Error('failed converting scriptPubkey to address')
|
|
1212
|
+
}
|
|
1213
|
+
return scriptPubkey.toString('hex')
|
|
1214
|
+
}
|