UNPKG

bitcore-wallet-service

Version:
92 lines 3.8 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.Copayer = void 0; const common_1 = require("../common"); const address_1 = require("./address"); const addressmanager_1 = require("./addressmanager"); const $ = require('preconditions').singleton(); const sjcl = require('sjcl'); const Constants = common_1.Common.Constants, Defaults = common_1.Common.Defaults, Utils = common_1.Common.Utils; class Copayer { static _xPubToCopayerId(coin, xpub) { const str = coin == Defaults.COIN ? xpub : coin + xpub; const hash = sjcl.hash.sha256.hash(str); return sjcl.codec.hex.fromBits(hash); } static create(opts) { opts = opts || {}; if (!opts.hardwareSourcePublicKey && !opts.clientDerivedPublicKey) { $.checkArgument(opts.xPubKey, 'Missing copayer extended public key'); } $.checkArgument(opts.requestPubKey, 'Missing copayer request public key') .checkArgument(opts.signature, 'Missing copayer request public key signature'); $.checkArgument(Utils.checkValueInCollection(opts.coin, Constants.CHAINS)); opts.copayerIndex = opts.copayerIndex || 0; const x = new Copayer(); x.version = 2; x.createdOn = Math.floor(Date.now() / 1000); x.coin = opts.coin; x.chain = opts.chain || opts.coin; x.xPubKey = opts.xPubKey; x.hardwareSourcePublicKey = opts.hardwareSourcePublicKey; x.clientDerivedPublicKey = opts.clientDerivedPublicKey; x.id = Copayer._xPubToCopayerId(opts.chain, x.xPubKey); x.name = opts.name; x.requestPubKey = opts.requestPubKey; x.signature = opts.signature; x.requestPubKeys = [ { key: opts.requestPubKey, signature: opts.signature } ]; const derivationStrategy = opts.derivationStrategy || Constants.DERIVATION_STRATEGIES.BIP45; if (addressmanager_1.AddressManager.supportsCopayerBranches(derivationStrategy)) { x.addressManager = addressmanager_1.AddressManager.fromObj({ derivationStrategy, copayerIndex: opts.copayerIndex }); } x.customData = opts.customData; return x; } static fromObj(obj) { const x = new Copayer(); x.version = obj.version; x.createdOn = obj.createdOn; x.coin = obj.coin || Defaults.COIN; x.chain = obj.chain || x.coin; x.id = obj.id; x.name = obj.name; x.xPubKey = obj.xPubKey; x.hardwareSourcePublicKey = obj.hardwareSourcePublicKey; x.clientDerivedPublicKey = obj.clientDerivedPublicKey; x.requestPubKey = obj.requestPubKey; x.signature = obj.signature; if (parseInt(x.version.toString()) == 1) { x.requestPubKeys = [ { key: x.requestPubKey, signature: x.signature } ]; x.version = 2; } else { x.requestPubKeys = obj.requestPubKeys; } if (obj.addressManager) { x.addressManager = addressmanager_1.AddressManager.fromObj(obj.addressManager); } x.customData = obj.customData; return x; } createAddress(wallet, isChange) { $.checkState(wallet.isComplete(), 'Failed state: wallet incomplete at <createAddress()>'); const path = this.addressManager.getNewAddressPath(isChange); const address = address_1.Address.derive(wallet.id, wallet.addressType, wallet.publicKeyRing, path, wallet.m, wallet.coin, wallet.network, isChange, wallet.chain); return address; } } exports.Copayer = Copayer; //# sourceMappingURL=copayer.js.map