UNPKG

bitcore-wallet-client

Version:
276 lines 9.41 kB
'use strict'; Object.defineProperty(exports, "__esModule", { value: true }); exports.Credentials = void 0; const crypto_wallet_core_1 = require("crypto-wallet-core"); const preconditions_1 = require("preconditions"); const common_1 = require("./common"); const $ = (0, preconditions_1.singleton)(); class Credentials { constructor() { this.version = 2; this.account = 0; } static fromDerivedKey(opts) { $.shouldBeString(opts.coin); $.shouldBeString(opts.chain); $.shouldBeString(opts.network); $.shouldBeNumber(opts.account, 'Invalid account'); $.shouldBeString(opts.xPubKey, 'Invalid xPubKey'); $.shouldBeString(opts.rootPath, 'Invalid rootPath'); $.shouldBeString(opts.keyId, 'Invalid keyId'); $.shouldBeString(opts.requestPrivKey, 'Invalid requestPrivKey'); $.checkArgument(opts.nonCompliantDerivation == null); opts = opts || {}; let x = new Credentials(); x.coin = opts.coin; x.chain = opts.chain; x.network = opts.network; x.account = opts.account; x.n = opts.n; x.xPubKey = opts.xPubKey; x.keyId = opts.keyId; if (opts.addressType == null) { x.addressType = opts.n == 1 ? common_1.Constants.SCRIPT_TYPES.P2PKH : common_1.Constants.SCRIPT_TYPES.P2SH; } else { x.addressType = opts.addressType; } x.rootPath = opts.rootPath; if (opts.walletPrivKey) { x.addWalletPrivateKey(opts.walletPrivKey); } x.requestPrivKey = opts.requestPrivKey; const priv = crypto_wallet_core_1.BitcoreLib.PrivateKey(x.requestPrivKey); x.requestPubKey = priv.toPublicKey().toString(); const prefix = 'personalKey'; const entropySource = crypto_wallet_core_1.BitcoreLib.crypto.Hash.sha256(priv.toBuffer()).toString('hex'); const b = Buffer.from(entropySource, 'hex'); const b2 = crypto_wallet_core_1.BitcoreLib.crypto.Hash.sha256hmac(b, Buffer.from(prefix)); x.personalEncryptingKey = b2.slice(0, 16).toString('base64'); x.copayerId = common_1.Utils.xPubToCopayerId(x.chain, x.xPubKey); x.publicKeyRing = [ { xPubKey: x.xPubKey, requestPubKey: x.requestPubKey } ]; x.clientDerivedPublicKey = opts.clientDerivedPublicKey; return x; } getTokenCredentials(token, chain) { const ret = Credentials.fromObj(this.toObj()); ret.walletId = `${ret.walletId}-${token.address}`; ret.coin = token.symbol.toLowerCase(); ret.chain = chain; ret.walletName = token.name; ret.token = token; return ret; } getMultisigEthCredentials(multisigEthInfo) { const ret = Credentials.fromObj(this.toObj()); ret.walletId = `${ret.walletId}-${multisigEthInfo.multisigContractAddress}`; ret.walletName = multisigEthInfo.walletName; ret.n = multisigEthInfo.n; ret.m = multisigEthInfo.m; ret.multisigEthInfo = multisigEthInfo; return ret; } getRootPath() { let legacyRootPath = () => { var _a; let purpose; switch (this.derivationStrategy) { case common_1.Constants.DERIVATION_STRATEGIES.BIP45: return "m/45'"; case common_1.Constants.DERIVATION_STRATEGIES.BIP44: purpose = '44'; break; case common_1.Constants.DERIVATION_STRATEGIES.BIP48: purpose = '48'; break; case common_1.Constants.DERIVATION_STRATEGIES.BIP84: purpose = '84'; break; } let chainPath = '0'; const chain = ((_a = this.chain) === null || _a === void 0 ? void 0 : _a.toLowerCase()) || this.coin; if (this.network != 'livenet' && common_1.Constants.UTXO_CHAINS.includes(chain)) { chainPath = '1'; } else if (chain == 'bch') { if (this.use145forBCH) { chainPath = '145'; } else { chainPath = '0'; } } else if (chain == 'btc') { chainPath = '0'; } else if (chain == 'eth') { chainPath = '60'; } else if (chain == 'matic') { chainPath = '60'; } else if (chain == 'arb') { chainPath = '60'; } else if (chain == 'base') { chainPath = '60'; } else if (chain == 'op') { chainPath = '60'; } else if (chain == 'xrp') { chainPath = '144'; } else if (chain == 'doge') { chainPath = '3'; } else if (chain == 'ltc') { chainPath = '2'; } else if (chain == 'sol') { chainPath = '501'; } else { throw new Error('unknown chain: ' + chain); } return 'm/' + purpose + "'/" + chainPath + "'/" + this.account + "'"; }; if (!this.rootPath) { this.rootPath = legacyRootPath(); } return this.rootPath; } static fromObj(obj) { let x = new Credentials(); if (!obj.version || obj.version < x.version) { throw new Error('Obsolete credentials version'); } if (obj.version != x.version) { throw new Error('Bad credentials version'); } for (const k of Credentials.FIELDS) { x[k] = obj[k]; } if (x.externalSource) { throw new Error('External Wallets are no longer supported'); } x.coin = x.coin || 'btc'; x.chain = x.chain || common_1.Utils.getChain(x.coin); x.addressType = x.addressType || common_1.Constants.SCRIPT_TYPES.P2SH; x.account = x.account || 0; $.checkState(x.xPrivKey || x.xPubKey || x.xPrivKeyEncrypted || x.hardwareSourcePublicKey || x.clientDerivedPublicKey, 'Failed State: x.xPrivKey | x.xPubkey | x.xPrivKeyEncrypted | x.hardwareSourcePublicKey | x.clientDerivedPublicKey at fromObj'); return x; } toObj() { const x = {}; for (const k of Credentials.FIELDS) { x[k] = this[k]; } return x; } addWalletPrivateKey(walletPrivKey) { this.walletPrivKey = walletPrivKey; this.sharedEncryptingKey = common_1.Utils.privateKeyToAESKey(walletPrivKey); } addWalletInfo(walletId, walletName, m, n, copayerName, opts) { opts = opts || {}; this.walletId = walletId; this.walletName = walletName; this.m = m; if (opts.useNativeSegwit) { switch (Number(opts.segwitVersion)) { case 0: default: this.addressType = n == 1 ? common_1.Constants.SCRIPT_TYPES.P2WPKH : common_1.Constants.SCRIPT_TYPES.P2WSH; break; case 1: this.addressType = common_1.Constants.SCRIPT_TYPES.P2TR; break; } } if (this.n != n && !opts.allowOverwrite) { if (this.n == 1 || n == 1) { throw new Error(`Bad nr of copayers in addWalletInfo: this: ${this.n} got: ${n}`); } } this.n = n; if (copayerName) this.copayerName = copayerName; if (n == 1) { this.addPublicKeyRing([ { xPubKey: this.xPubKey, requestPubKey: this.requestPubKey } ]); } } hasWalletInfo() { return !!this.walletId; } addPublicKeyRing(publicKeyRing) { this.publicKeyRing = JSON.parse(JSON.stringify(publicKeyRing)); } isComplete() { if (!this.m || !this.n) return false; if ((this.chain === 'btc' || this.chain === 'bch' || this.chain === 'doge' || this.chain === 'ltc') && (!this.publicKeyRing || this.publicKeyRing.length != this.n)) return false; return true; } } exports.Credentials = Credentials; Credentials.FIELDS = [ 'coin', 'chain', 'network', 'xPrivKey', 'xPrivKeyEncrypted', 'xPubKey', 'requestPrivKey', 'requestPubKey', 'copayerId', 'publicKeyRing', 'walletId', 'walletName', 'm', 'n', 'walletPrivKey', 'personalEncryptingKey', 'sharedEncryptingKey', 'copayerName', 'externalSource', 'mnemonic', 'mnemonicEncrypted', 'entropySource', 'mnemonicHasPassphrase', 'derivationStrategy', 'account', 'compliantDerivation', 'addressType', 'hwInfo', 'entropySourcePath', 'use145forBCH', 'version', 'rootPath', 'keyId', 'token', 'multisigEthInfo', 'hardwareSourcePublicKey', 'clientDerivedPublicKey' ]; //# sourceMappingURL=credentials.js.map