UNPKG

bivcore-wallet-service

Version:
1,656 lines (1,470 loc) 140 kB
import * as async from 'async'; import * as _ from 'lodash'; import 'source-map-support/register'; import logger from './logger'; import { BlockChainExplorer } from './blockchainexplorer'; import { V8 } from './blockchainexplorers/v8'; import { ChainService } from './chain/index'; import { ClientError } from './errors/clienterror'; import { FiatRateService } from './fiatrateservice'; import { Lock } from './lock'; import { MessageBroker } from './messagebroker'; import { Advertisement, Copayer, INotification, ITxProposal, IWallet, Notification, Preferences, PushNotificationSub, Session, TxConfirmationSub, TxNote, TxProposal, Wallet } from './model'; import { Storage } from './storage'; const config = require('../config'); const Uuid = require('uuid'); const $ = require('preconditions').singleton(); const deprecatedServerMessage = require('../deprecated-serverMessages'); const serverMessages = require('../serverMessages'); const BCHAddressTranslator = require('./bchaddresstranslator'); const EmailValidator = require('email-validator'); import { Validation } from 'crypto-value-wallet-core'; const Bitcore = require('bitcore-lib'); const Bitcore_ = { btc: Bitcore, bch: require('bitcore-lib-cash'), biv: require('bitcore-lib-value'), eth: Bitcore, xrp: Bitcore }; const Common = require('./common'); const Utils = Common.Utils; const Constants = Common.Constants; const Defaults = Common.Defaults; const Errors = require('./errors/errordefinitions'); let request = require('request'); let initialized = false; let doNotCheckV8 = false; let lock; let storage; let blockchainExplorer; let blockchainExplorerOpts; let messageBroker; let fiatRateService; let serviceVersion; interface IAddress { coin: string; network: string; address: string; hasActivity: boolean; isChange?: boolean; } export interface IWalletService { lock: any; storage: Storage; blockchainExplorer: any; blockchainExplorerOpts: any; messageBroker: any; fiatRateService: any; notifyTicker: number; userAgent: string; walletId: string; copayerId: string; appName: string; appVersion: string; parsedClientVersion: { agent: number; major: number; minor: number }; clientVersion: string; copayerIsSupportStaff: boolean; copayerIsMarketingStaff: boolean; } function boolToNum(x: boolean) { return x ? 1 : 0; } /** * Creates an instance of the Bitcore Wallet Service. * @constructor */ export class WalletService { lock: any; storage: Storage; blockchainExplorer: V8; blockchainExplorerOpts: any; messageBroker: any; fiatRateService: any; notifyTicker: number; userAgent: string; walletId: string; copayerId: string; appName: string; appVersion: string; parsedClientVersion: { agent: string; major: number; minor: number }; clientVersion: string; copayerIsSupportStaff: boolean; copayerIsMarketingStaff: boolean; request; constructor() { if (!initialized) { throw new Error('Server not initialized'); } this.lock = lock; this.storage = storage; this.blockchainExplorer = blockchainExplorer; this.blockchainExplorerOpts = blockchainExplorerOpts; this.messageBroker = messageBroker; this.fiatRateService = fiatRateService; this.notifyTicker = 0; // for testing // this.request = request; } /** * Gets the current version of BWS */ static getServiceVersion() { if (!serviceVersion) { serviceVersion = 'bws-' + require('../../package').version; } return serviceVersion; } /** * Initializes global settings for all instances. * @param {Object} opts * @param {Storage} [opts.storage] - The storage provider. * @param {Storage} [opts.blockchainExplorer] - The blockchainExporer provider. * @param {Storage} [opts.doNotCheckV8] - only for testing * @param {Callback} cb */ static initialize(opts, cb) { $.shouldBeFunction(cb); opts = opts || {}; blockchainExplorer = opts.blockchainExplorer; blockchainExplorerOpts = opts.blockchainExplorerOpts; doNotCheckV8 = opts.doNotCheckV8; if (opts.request) { request = opts.request; } const initStorage = cb => { if (opts.storage) { storage = opts.storage; return cb(); } else { const newStorage = new Storage(); newStorage.connect(opts.storageOpts, err => { if (err) { return cb(err); } storage = newStorage; return cb(); }); } }; const initMessageBroker = cb => { messageBroker = opts.messageBroker || new MessageBroker(opts.messageBrokerOpts); if (messageBroker) { messageBroker.onMessage(WalletService.handleIncomingNotifications); } return cb(); }; const initFiatRateService = cb => { if (opts.fiatRateService) { fiatRateService = opts.fiatRateService; return cb(); } else { const newFiatRateService = new FiatRateService(); const opts2 = opts.fiatRateServiceOpts || {}; opts2.storage = storage; newFiatRateService.init(opts2, err => { if (err) { return cb(err); } fiatRateService = newFiatRateService; return cb(); }); } }; async.series( [ next => { initStorage(next); }, next => { initMessageBroker(next); }, next => { initFiatRateService(next); } ], err => { lock = opts.lock || new Lock(storage, opts.lockOpts); if (err) { logger.error('Could not initialize', err); throw err; } initialized = true; return cb(); } ); } static handleIncomingNotifications(notification, cb) { cb = cb || function() {}; // do nothing here.... // bc height cache is cleared on bcmonitor return cb(); } static shutDown(cb) { if (!initialized) { return cb(); } storage.disconnect(err => { if (err) { return cb(err); } initialized = false; return cb(); }); } /** * Gets an instance of the server without authentication. * @param {Object} opts * @param {string} opts.clientVersion - A string that identifies the client issuing the request */ static getInstance(opts): WalletService { opts = opts || {}; const version = Utils.parseVersion(opts.clientVersion); if (version && version.agent === 'bwc') { if (version.major === 0 || (version.major === 1 && version.minor < 2)) { throw new ClientError(Errors.codes.UPGRADE_NEEDED, 'BWC clients < 1.2 are no longer supported.'); } } const server = new WalletService(); server._setClientVersion(opts.clientVersion); server._setAppVersion(opts.userAgent); server.userAgent = opts.userAgent; return server; } /** * Gets an instance of the server after authenticating the copayer. * @param {Object} opts * @param {string} opts.copayerId - The copayer id making the request. * @param {string} opts.message - (Optional) The contents of the request to be signed. * Only needed if no session token is provided. * @param {string} opts.signature - (Optional) Signature of message to be verified using * one of the copayer's requestPubKeys. * Only needed if no session token is provided. * @param {string} opts.session - (Optional) A valid session token previously obtained using * the #login method * @param {string} opts.clientVersion - A string that identifies the client issuing the request * @param {string} [opts.walletId] - The wallet id to use as current wallet * for this request (only when copayer is support staff). */ static getInstanceWithAuth(opts, cb) { const withSignature = cb => { if (!checkRequired(opts, ['copayerId', 'message', 'signature'], cb)) { return; } let server: WalletService; try { server = WalletService.getInstance(opts); } catch (ex) { return cb(ex); } server.storage.fetchCopayerLookup(opts.copayerId, (err, copayer) => { if (err) { return cb(err); } if (!copayer) { return cb(new ClientError(Errors.codes.NOT_AUTHORIZED, 'Copayer not found')); } const isValid = !!server._getSigningKey(opts.message, opts.signature, copayer.requestPubKeys); if (!isValid) { return cb(new ClientError(Errors.codes.NOT_AUTHORIZED, 'Invalid signature')); } server.walletId = copayer.walletId; // allow overwrite walletid if the copayer is from the support team if (copayer.isSupportStaff) { server.walletId = opts.walletId || copayer.walletId; server.copayerIsSupportStaff = true; } if (copayer.isMarketingStaff) { server.copayerIsMarketingStaff = true; } server.copayerId = opts.copayerId; return cb(null, server); }); }; const withSession = cb => { if (!checkRequired(opts, ['copayerId', 'session'], cb)) { return; } let server; try { server = WalletService.getInstance(opts); } catch (ex) { return cb(ex); } server.storage.getSession(opts.copayerId, (err, s) => { if (err) { return cb(err); } const isValid = s && s.id === opts.session && s.isValid(); if (!isValid) { return cb(new ClientError(Errors.codes.NOT_AUTHORIZED, 'Session expired')); } server.storage.fetchCopayerLookup(opts.copayerId, (err, copayer) => { if (err) { return cb(err); } if (!copayer) { return cb(new ClientError(Errors.codes.NOT_AUTHORIZED, 'Copayer not found')); } server.copayerId = opts.copayerId; server.walletId = copayer.walletId; return cb(null, server); }); }); }; const authFn = opts.session ? withSession : withSignature; return authFn(cb); } _runLocked(cb, task, waitTime?: number) { $.checkState(this.walletId); this.lock.runLocked(this.walletId, { waitTime }, cb, task); } logi(message, ...args) { if (!this || !this.walletId) { return logger.warn(message, ...args); } message = '<' + this.walletId + '>' + message; return logger.info(message, ...args); } logw(message, ...args) { if (!this || !this.walletId) { return logger.warn(message, ...args); } message = '<' + this.walletId + '>' + message; return logger.warn(message, ...args); } logd(message, ...args) { if (!this || !this.walletId) { return logger.verbose(message, ...args); } message = '<' + this.walletId + '>' + message; return logger.verbose(message, ...args); } login(opts, cb) { let session; async.series( [ next => { this.storage.getSession(this.copayerId, (err, s) => { if (err) { return next(err); } session = s; next(); }); }, next => { if (!session || !session.isValid()) { session = Session.create({ copayerId: this.copayerId, walletId: this.walletId }); } else { session.touch(); } next(); }, next => { this.storage.storeSession(session, next); } ], err => { if (err) { return cb(err); } if (!session) { return cb(new Error('Could not get current session for this copayer')); } return cb(null, session.id); } ); } logout(opts, cb) { // this.storage.removeSession(this.copayerId, cb); } /** * Creates a new wallet. * @param {Object} opts * @param {string} opts.id - The wallet id. * @param {string} opts.name - The wallet name. * @param {number} opts.m - Required copayers. * @param {number} opts.n - Total copayers. * @param {string} opts.pubKey - Public key to verify copayers joining have access to the wallet secret. * @param {string} opts.singleAddress[=false] - The wallet will only ever have one address. * @param {string} opts.coin[='btc'] - The coin for this wallet (btc, bch, biv). * @param {string} opts.network[='livenet'] - The Bitcoin network for this wallet. * @param {string} opts.account[=0] - BIP44 account number * @param {string} opts.usePurpose48 - for Multisig wallet, use purpose=48 * @param {string} opts.useNativeSegwit - for Segwit address, set addressType to P2WPKH or P2WSH */ createWallet(opts, cb) { let pubKey; if (opts.coin === 'bch' && opts.n > 1) { const version = Utils.parseVersion(this.clientVersion); if (version && version.agent === 'bwc') { if (version.major < 8 || (version.major === 8 && version.minor < 3)) { return cb( new ClientError( Errors.codes.UPGRADE_NEEDED, 'BWC clients < 8.3 are no longer supported for multisig BCH wallets.' ) ); } } } if (!checkRequired(opts, ['name', 'm', 'n', 'pubKey'], cb)) { return; } if (_.isEmpty(opts.name)) { return cb(new ClientError('Invalid wallet name')); } if (!Wallet.verifyCopayerLimits(opts.m, opts.n)) { return cb(new ClientError('Invalid combination of required copayers / total copayers')); } opts.coin = opts.coin || Defaults.COIN; if (!Utils.checkValueInCollection(opts.coin, Constants.COINS)) { return cb(new ClientError('Invalid coin')); } opts.network = opts.network || 'livenet'; if (!Utils.checkValueInCollection(opts.network, Constants.NETWORKS)) { return cb(new ClientError('Invalid network')); } const derivationStrategy = Constants.DERIVATION_STRATEGIES.BIP44; let addressType = opts.n === 1 ? Constants.SCRIPT_TYPES.P2PKH : Constants.SCRIPT_TYPES.P2SH; if (opts.useNativeSegwit) { addressType = opts.n === 1 ? Constants.SCRIPT_TYPES.P2WPKH : Constants.SCRIPT_TYPES.P2WSH; } try { pubKey = new Bitcore.PublicKey.fromString(opts.pubKey); } catch (ex) { return cb(new ClientError('Invalid public key')); } if (opts.n > 1 && !ChainService.supportsMultisig(opts.coin)) { return cb(new ClientError('Multisig wallets are not supported for this coin')); } if (ChainService.isSingleAddress(opts.coin)) { opts.singleAddress = true; } let newWallet; async.series( [ acb => { if (!opts.id) { return acb(); } this.storage.fetchWallet(opts.id, (err, wallet) => { if (wallet) { return acb(Errors.WALLET_ALREADY_EXISTS); } return acb(err); }); }, acb => { const wallet = Wallet.create({ id: opts.id, name: opts.name, m: opts.m, n: opts.n, coin: opts.coin, network: opts.network, pubKey: pubKey.toString(), singleAddress: !!opts.singleAddress, derivationStrategy, addressType, nativeCashAddr: opts.nativeCashAddr, usePurpose48: opts.n > 1 && !!opts.usePurpose48 }); this.storage.storeWallet(wallet, err => { this.logd('Wallet created', wallet.id, opts.network); newWallet = wallet; return acb(err); }); } ], err => { return cb(err, newWallet ? newWallet.id : null); } ); } /** * Retrieves a wallet from storage. * @param {Object} opts * @returns {Object} wallet */ getWallet(opts, cb) { this.storage.fetchWallet(this.walletId, (err, wallet) => { if (err) return cb(err); if (!wallet) return cb(Errors.WALLET_NOT_FOUND); // cashAddress migration if (wallet.coin != 'bch' || wallet.nativeCashAddr) return cb(null, wallet); // only for testing if (opts.doNotMigrate) return cb(null, wallet); // remove someday... logger.info(`Migrating wallet ${wallet.id} to cashAddr`); this.storage.migrateToCashAddr(this.walletId, e => { if (e) return cb(e); wallet.nativeCashAddr = true; return this.storage.storeWallet(wallet, e => { if (e) return cb(e); return cb(e, wallet); }); }); }); } /** * Retrieves a wallet from storage. * @param {Object} opts * @param {string} opts.identifier - The identifier associated with the wallet (one of: walletId, address, txid). * @param {string} opts.walletCheck - Check v8 wallet sync * @returns {Object} wallet */ getWalletFromIdentifier(opts, cb) { if (!opts.identifier) return cb(); const end = (err, ret) => { if (opts.walletCheck && !err && ret) { return this.syncWallet(ret, cb); } else { return cb(err, ret); } }; let walletId; async.parallel( [ done => { this.storage.fetchWallet(opts.identifier, (err, wallet) => { if (wallet) walletId = wallet.id; return done(err); }); }, done => { this.storage.fetchAddressByCoin(Defaults.COIN, opts.identifier, (err, address) => { if (address) walletId = address.walletId; return done(err); }); }, done => { // sent txs this.storage.fetchTxByHash(opts.identifier, (err, tx) => { if (tx) walletId = tx.walletId; return done(err); }); } ], err => { if (err) return cb(err); if (walletId) { return this.storage.fetchWallet(walletId, end); } return cb(); } ); } /** * Retrieves wallet status. * @param {Object} opts * @param {Object} opts.includeExtendedInfo - Include PKR info & address managers for wallet & copayers * @param {Object} opts.includeServerMessages - Include server messages array * @param {Object} opts.tokenAddress - (Optional) Token contract address to pass in getBalance * @param {Object} opts.multisigContractAddress - (Optional) Multisig ETH contract address to pass in getBalance * @param {Object} opts.network - (Optional ETH MULTISIG) Multisig ETH contract address network * @returns {Object} status */ getStatus(opts, cb) { opts = opts || {}; const status: { wallet?: IWallet; serverMessage?: { title: string; body: string; link: string; id: string; dismissible: boolean; category: string; app: string; }; serverMessages?: Array<{ title: string; body: string; link: string; id: string; dismissible: boolean; category: string; app: string; priority: number; }>; balance?: string; pendingTxps?: ITxProposal[]; preferences?: boolean; } = {}; async.parallel( [ next => { this.getWallet({}, (err, wallet) => { if (err) return next(err); const walletExtendedKeys = ['publicKeyRing', 'pubKey', 'addressManager']; const copayerExtendedKeys = ['xPubKey', 'requestPubKey', 'signature', 'addressManager', 'customData']; wallet.copayers = _.map(wallet.copayers, copayer => { if (copayer.id == this.copayerId) return copayer; return _.omit(copayer, 'customData'); }); if (!opts.includeExtendedInfo) { wallet = _.omit(wallet, walletExtendedKeys); wallet.copayers = _.map(wallet.copayers, copayer => { return _.omit(copayer, copayerExtendedKeys); }); } status.wallet = wallet; if (opts.includeServerMessages) { status.serverMessages = serverMessages(wallet, this.appName, this.appVersion); } else { status.serverMessage = deprecatedServerMessage(wallet, this.appName, this.appVersion); } next(); }); }, next => { opts.wallet = status.wallet; this.getBalance(opts, (err, balance) => { // ignore WALLET_NEED_SCAN err is includeExtendedInfo is given // (to allow `importWallet` to import a wallet, while scan has // failed) if (opts.includeExtendedInfo) { if (err && err.code != 'WALLET_NEED_SCAN') { return next(err); } } else if (err) { return next(err); } status.balance = balance; next(); }); }, next => { this.getPendingTxs(opts, (err, pendingTxps) => { if (err) return next(err); status.pendingTxps = pendingTxps; next(); }); }, next => { this.getPreferences({}, (err, preferences) => { if (err) return next(err); status.preferences = preferences; next(); }); } ], err => { if (err) return cb(err); return cb(null, status); } ); } /* * Verifies a signature * @param text * @param signature * @param pubKeys */ _verifySignature(text, signature, pubkey) { return Utils.verifyMessage(text, signature, pubkey); } /* * Verifies a request public key * @param requestPubKey * @param signature * @param xPubKey */ _verifyRequestPubKey(requestPubKey, signature, xPubKey) { const pub = new Bitcore.HDPublicKey(xPubKey).deriveChild(Constants.PATHS.REQUEST_KEY_AUTH).publicKey; return Utils.verifyMessage(requestPubKey, signature, pub.toString()); } /* * Verifies signature againt a collection of pubkeys * @param text * @param signature * @param pubKeys */ _getSigningKey(text, signature, pubKeys) { return _.find(pubKeys, item => { return this._verifySignature(text, signature, item.key); }); } /** * _notify * * @param {String} type * @param {Object} data * @param {Object} opts * @param {Boolean} opts.isGlobal - If true, the notification is not issued on behalf of any particular copayer (defaults to false) */ _notify(type, data, opts, cb?: (err?: any, data?: any) => void) { if (_.isFunction(opts)) { cb = opts; opts = {}; } opts = opts || {}; // this.logi('Notification', type); cb = cb || function() {}; const walletId = this.walletId || data.walletId; const copayerId = this.copayerId || data.copayerId; $.checkState(walletId); const notification = Notification.create({ type, data, ticker: this.notifyTicker++, creatorId: opts.isGlobal ? null : copayerId, walletId }); this.storage.storeNotification(walletId, notification, () => { this.messageBroker.send(notification); return cb(); }); } _notifyTxProposalAction(type, txp, extraArgs, cb?: (err?: any, data?: any) => void) { if (_.isFunction(extraArgs)) { cb = extraArgs; extraArgs = {}; } const data = _.assign( { txProposalId: txp.id, creatorId: txp.creatorId, amount: txp.getTotalAmount(), message: txp.message }, extraArgs ); this._notify(type, data, {}, cb); } _addCopayerToWallet(wallet, opts, cb) { const copayer = Copayer.create({ coin: wallet.coin, name: opts.name, copayerIndex: wallet.copayers.length, xPubKey: opts.xPubKey, requestPubKey: opts.requestPubKey, signature: opts.copayerSignature, customData: opts.customData, derivationStrategy: wallet.derivationStrategy }); this.storage.fetchCopayerLookup(copayer.id, (err, res) => { if (err) return cb(err); if (res) return cb(Errors.COPAYER_REGISTERED); if (opts.dryRun) return cb(null, { copayerId: null, wallet }); wallet.addCopayer(copayer); this.storage.storeWalletAndUpdateCopayersLookup(wallet, err => { if (err) return cb(err); async.series( [ next => { this._notify( 'NewCopayer', { walletId: opts.walletId, copayerId: copayer.id, copayerName: copayer.name }, {}, next ); }, next => { if (wallet.isComplete() && wallet.isShared()) { this._notify( 'WalletComplete', { walletId: opts.walletId }, { isGlobal: true }, next ); } else { next(); } } ], () => { return cb(null, { copayerId: copayer.id, wallet }); } ); }); }); } _addKeyToCopayer(wallet, copayer, opts, cb) { wallet.addCopayerRequestKey(copayer.copayerId, opts.requestPubKey, opts.signature, opts.restrictions, opts.name); this.storage.storeWalletAndUpdateCopayersLookup(wallet, err => { if (err) return cb(err); return cb(null, { copayerId: copayer.id, wallet }); }); } /** * Adds access to a given copayer * * @param {Object} opts * @param {string} opts.copayerId - The copayer id * @param {string} opts.requestPubKey - Public Key used to check requests from this copayer. * @param {string} opts.copayerSignature - S(requestPubKey). Used by other copayers to verify the that the copayer is himself (signed with REQUEST_KEY_AUTH) * @param {string} opts.restrictions * - cannotProposeTXs * - cannotXXX TODO * @param {string} opts.name (name for the new access) */ addAccess(opts, cb) { if (!checkRequired(opts, ['copayerId', 'requestPubKey', 'signature'], cb)) return; this.storage.fetchCopayerLookup(opts.copayerId, (err, copayer) => { if (err) return cb(err); if (!copayer) return cb(Errors.NOT_AUTHORIZED); this.storage.fetchWallet(copayer.walletId, (err, wallet) => { if (err) return cb(err); if (!wallet) return cb(Errors.NOT_AUTHORIZED); const xPubKey = wallet.copayers.find(c => c.id === opts.copayerId).xPubKey; if (!this._verifyRequestPubKey(opts.requestPubKey, opts.signature, xPubKey)) { return cb(Errors.NOT_AUTHORIZED); } if (copayer.requestPubKeys.length > Defaults.MAX_KEYS) return cb(Errors.TOO_MANY_KEYS); this._addKeyToCopayer(wallet, copayer, opts, cb); }); }); } _setClientVersion(version) { delete this.parsedClientVersion; this.clientVersion = version; } _setAppVersion(userAgent) { const parsed = Utils.parseAppVersion(userAgent); if (!parsed) { this.appName = this.appVersion = null; } else { this.appName = parsed.app; this.appVersion = parsed; } } _parseClientVersion() { if (_.isUndefined(this.parsedClientVersion)) { this.parsedClientVersion = Utils.parseVersion(this.clientVersion); } return this.parsedClientVersion; } _clientSupportsPayProRefund() { const version = this._parseClientVersion(); if (!version) return false; if (version.agent != 'bwc') return true; if (version.major < 1 || (version.major == 1 && version.minor < 2)) return false; return true; } static _getCopayerHash(name, xPubKey, requestPubKey) { return [name, xPubKey, requestPubKey].join('|'); } /** * Joins a wallet in creation. * @param {Object} opts * @param {string} opts.walletId - The wallet id. * @param {string} opts.coin[='btc'] - The expected coin for this wallet (btc, bch, biv). * @param {string} opts.name - The copayer name. * @param {string} opts.xPubKey - Extended Public Key for this copayer. * @param {string} opts.requestPubKey - Public Key used to check requests from this copayer. * @param {string} opts.copayerSignature - S(name|xPubKey|requestPubKey). Used by other copayers to verify that the copayer joining knows the wallet secret. * @param {string} opts.customData - (optional) Custom data for this copayer. * @param {string} opts.dryRun[=false] - (optional) Simulate the action but do not change server state. */ joinWallet(opts, cb) { if (!checkRequired(opts, ['walletId', 'name', 'xPubKey', 'requestPubKey', 'copayerSignature'], cb)) return; if (_.isEmpty(opts.name)) return cb(new ClientError('Invalid copayer name')); opts.coin = opts.coin || Defaults.COIN; if (!Utils.checkValueInCollection(opts.coin, Constants.COINS)) return cb(new ClientError('Invalid coin')); let xPubKey; try { xPubKey = Bitcore.HDPublicKey(opts.xPubKey); } catch (ex) { return cb(new ClientError('Invalid extended public key')); } if (_.isUndefined(xPubKey.network)) { return cb(new ClientError('Invalid extended public key')); } this.walletId = opts.walletId; this._runLocked(cb, cb => { this.storage.fetchWallet(opts.walletId, (err, wallet) => { if (err) return cb(err); if (!wallet) return cb(Errors.WALLET_NOT_FOUND); if (opts.coin === 'bch' && wallet.n > 1) { const version = Utils.parseVersion(this.clientVersion); if (version && version.agent === 'bwc') { if (version.major < 8 || (version.major === 8 && version.minor < 3)) { return cb( new ClientError( Errors.codes.UPGRADE_NEEDED, 'BWC clients < 8.3 are no longer supported for multisig BCH wallets.' ) ); } } } if (wallet.n > 1 && wallet.usePurpose48) { const version = Utils.parseVersion(this.clientVersion); if (version && version.agent === 'bwc') { if (version.major < 8 || (version.major === 8 && version.minor < 4)) { return cb( new ClientError(Errors.codes.UPGRADE_NEEDED, 'Please upgrade your client to join this multisig wallet') ); } } } if (wallet.n > 1 && wallet.addressType === 'P2WSH') { const version = Utils.parseVersion(this.clientVersion); if (version && version.agent === 'bwc') { if (version.major < 8 || (version.major === 8 && version.minor < 17)) { return cb( new ClientError(Errors.codes.UPGRADE_NEEDED, 'Please upgrade your client to join this multisig wallet') ); } } } if (opts.coin != wallet.coin) { return cb(new ClientError('The wallet you are trying to join was created for a different coin')); } if (wallet.network != xPubKey.network.name) { return cb(new ClientError('The wallet you are trying to join was created for a different network')); } // New client trying to join legacy wallet if (wallet.derivationStrategy == Constants.DERIVATION_STRATEGIES.BIP45) { return cb( new ClientError('The wallet you are trying to join was created with an older version of the client app.') ); } const hash = WalletService._getCopayerHash(opts.name, opts.xPubKey, opts.requestPubKey); if (!this._verifySignature(hash, opts.copayerSignature, wallet.pubKey)) { return cb(new ClientError()); } if ( _.find(wallet.copayers, { xPubKey: opts.xPubKey }) ) return cb(Errors.COPAYER_IN_WALLET); if (wallet.copayers.length == wallet.n) return cb(Errors.WALLET_FULL); this._addCopayerToWallet(wallet, opts, cb); }); }); } /** * Save copayer preferences for the current wallet/copayer pair. * @param {Object} opts * @param {string} opts.email - Email address for notifications. * @param {string} opts.language - Language used for notifications. * @param {string} opts.unit - Bitcoin unit used to format amounts in notifications. * @param {string} opts.tokenAddresses - Linked token addresses * @param {string} opts.multisigEthInfo - Linked multisig eth wallet info * */ savePreferences(opts, cb) { opts = opts || {}; const preferences = [ { name: 'email', isValid(value) { return EmailValidator.validate(value); } }, { name: 'language', isValid(value) { return _.isString(value) && value.length == 2; } }, { name: 'unit', isValid(value) { return _.isString(value) && _.includes(['btc', 'bit'], value.toLowerCase()); } }, { name: 'tokenAddresses', isValid(value) { return _.isArray(value) && value.every(x => Validation.validateAddress('eth', 'mainnet', x)); } }, { name: 'multisigEthInfo', isValid(value) { return ( _.isArray(value) && value.every(x => Validation.validateAddress('eth', 'mainnet', x.multisigContractAddress)) ); } } ]; opts = _.pick(opts, _.map(preferences, 'name')); try { _.each(preferences, preference => { const value = opts[preference.name]; if (!value) return; if (!preference.isValid(value)) { throw new Error('Invalid ' + preference.name); } }); } catch (ex) { return cb(new ClientError(ex)); } this.getWallet({}, (err, wallet) => { if (err) return cb(err); if (wallet.coin != 'eth') { opts.tokenAddresses = null; opts.multisigEthInfo = null; } this._runLocked(cb, cb => { this.storage.fetchPreferences(this.walletId, this.copayerId, (err, oldPref) => { if (err) return cb(err); const newPref = Preferences.create({ walletId: this.walletId, copayerId: this.copayerId }); const preferences = Preferences.fromObj(_.defaults(newPref, opts, oldPref)); // merge tokenAddresses if (opts.tokenAddresses) { oldPref = oldPref || {}; oldPref.tokenAddresses = oldPref.tokenAddresses || []; preferences.tokenAddresses = _.uniq(oldPref.tokenAddresses.concat(opts.tokenAddresses)); } // merge multisigEthInfo if (opts.multisigEthInfo) { oldPref = oldPref || {}; oldPref.multisigEthInfo = oldPref.multisigEthInfo || []; preferences.multisigEthInfo = _.uniq( oldPref.multisigEthInfo.concat(opts.multisigEthInfo).reduce((x, y) => { let exists = false; x.forEach(e => { // add new token addresses linked to the multisig wallet if (e.multisigContractAddress === y.multisigContractAddress) { e.tokenAddresses = e.tokenAddresses || []; y.tokenAddresses = _.uniq(e.tokenAddresses.concat(y.tokenAddresses)); e = Object.assign(e, y); exists = true; } }); return exists ? x : [...x, y]; }, []) ); } this.storage.storePreferences(preferences, err => { return cb(err); }); }); }); }); } /** * Retrieves a preferences for the current wallet/copayer pair. * @param {Object} opts * @returns {Object} preferences */ getPreferences(opts, cb) { this.storage.fetchPreferences(this.walletId, this.copayerId, (err, preferences) => { if (err) return cb(err); return cb(null, preferences || {}); }); } _canCreateAddress(ignoreMaxGap, cb) { if (ignoreMaxGap) return cb(null, true); this.storage.fetchAddresses(this.walletId, (err, addresses: IAddress[]) => { if (err) return cb(err); const latestAddresses = addresses.filter(x => !x.isChange).slice(-Defaults.MAX_MAIN_ADDRESS_GAP) as IAddress[]; if ( latestAddresses.length < Defaults.MAX_MAIN_ADDRESS_GAP || _.some(latestAddresses, { hasActivity: true }) ) return cb(null, true); const bc = this._getBlockchainExplorer(latestAddresses[0].coin, latestAddresses[0].network); if (!bc) return cb(new Error('Could not get blockchain explorer instance')); let activityFound = false; let i = latestAddresses.length; async.whilst( () => { return i > 0 && !activityFound; }, next => { bc.getAddressActivity(latestAddresses[--i].address, (err, res) => { if (err) return next(err); activityFound = !!res; return next(); }); }, err => { if (err) return cb(err); if (!activityFound) return cb(null, false); const address = latestAddresses[i]; address.hasActivity = true; this.storage.storeAddress(address, err => { return cb(err, true); }); } ); }); } _store(wallet, address, cb, checkSync = false) { let stoAddress = _.clone(address); ChainService.addressToStorageTransform(wallet.coin, wallet.network, stoAddress); this.storage.storeAddressAndWallet(wallet, stoAddress, (err, isDuplicate) => { if (err) return cb(err); this.syncWallet( wallet, err2 => { if (err2) { this.logw('Error syncing v8 addresses: ', err2); } return cb(null, isDuplicate); }, !checkSync ); }); } /** * Creates a new address. * @param {Object} opts * @param {Boolean} [opts.ignoreMaxGap=false] - Ignore constraint of maximum number of consecutive addresses without activity * @param {Boolean} opts.noCashAddr (do not use cashaddr, only for backwards compat) * @returns {Address} address */ createAddress(opts, cb) { opts = opts || {}; const createNewAddress = (wallet, cb) => { let address; try { address = wallet.createAddress(false); } catch (e) { this.logw('Error creating address', e); return cb('Bad xPub'); } this._store( wallet, address, (err, duplicate) => { if (err) return cb(err); if (duplicate) return cb(null, address); if (wallet.coin == 'bch' && opts.noCashAddr) { address = _.cloneDeep(address); address.address = BCHAddressTranslator.translate(address.address, 'copay'); } this._notify( 'NewAddress', { address: address.address }, () => { return cb(null, address); } ); }, true ); }; const getFirstAddress = (wallet, cb) => { this.storage.fetchAddresses(this.walletId, (err, addresses) => { if (err) return cb(err); if (!_.isEmpty(addresses)) { let x = _.head(addresses); ChainService.addressFromStorageTransform(wallet.coin, wallet.network, x); return cb(null, x); } return createNewAddress(wallet, cb); }); }; this.getWallet({ doNotMigrate: opts.doNotMigrate }, (err, wallet) => { if (err) return cb(err); if (ChainService.isSingleAddress(wallet.coin)) { opts.ignoreMaxGap = true; opts.singleAddress = true; } this._canCreateAddress(opts.ignoreMaxGap || opts.singleAddress || wallet.singleAddress, (err, canCreate) => { if (err) return cb(err); if (!canCreate) return cb(Errors.MAIN_ADDRESS_GAP_REACHED); this._runLocked( cb, cb => { this.getWallet({ doNotMigrate: opts.doNotMigrate }, (err, wallet) => { if (err) return cb(err); if (!wallet.isComplete()) return cb(Errors.WALLET_NOT_COMPLETE); if (wallet.scanStatus == 'error') return cb(Errors.WALLET_NEED_SCAN); const createFn = opts.singleAddress || wallet.singleAddress ? getFirstAddress : createNewAddress; return createFn(wallet, (err, address) => { if (err) { return cb(err); } return cb(err, address); }); }); }, 10 * 1000 ); }); }); } /** * Get all addresses. * @param {Object} opts * @param {Numeric} opts.limit (optional) - Limit the resultset. Return all addresses by default. * @param {Boolean} [opts.reverse=false] (optional) - Reverse the order of returned addresses. * @returns {Address[]} */ getMainAddresses(opts, cb) { opts = opts || {}; this.storage.fetchAddresses(this.walletId, (err, addresses) => { if (err) return cb(err); let onlyMain = _.reject(addresses, { isChange: true }); if (opts.reverse) onlyMain.reverse(); if (opts.limit > 0) onlyMain = _.take(onlyMain, opts.limit); this.getWallet({}, (err, wallet) => { _.each(onlyMain, x => { ChainService.addressFromStorageTransform(wallet.coin, wallet.network, x); }); return cb(null, onlyMain); }); }); } /** * Verifies that a given message was actually sent by an authorized copayer. * @param {Object} opts * @param {string} opts.message - The message to verify. * @param {string} opts.signature - The signature of message to verify. * @returns {truthy} The result of the verification. */ verifyMessageSignature(opts, cb) { if (!checkRequired(opts, ['message', 'signature'], cb)) return; this.getWallet({}, (err, wallet) => { if (err) return cb(err); const copayer = wallet.getCopayer(this.copayerId); const isValid = !!this._getSigningKey(opts.message, opts.signature, copayer.requestPubKeys); return cb(null, isValid); }); } _getBlockchainExplorer(coin, network): ReturnType<typeof BlockChainExplorer> { let opts: Partial<{ provider: string; coin: string; network: string; userAgent: string; }> = {}; let provider; if (this.blockchainExplorer) return this.blockchainExplorer; if (this.blockchainExplorerOpts) { if (this.blockchainExplorerOpts[coin] && this.blockchainExplorerOpts[coin][network]) { opts = this.blockchainExplorerOpts[coin][network]; provider = opts.provider; } else if (this.blockchainExplorerOpts[network]) { opts = this.blockchainExplorerOpts[network]; } } opts.provider = provider; opts.coin = coin; opts.network = network; opts.userAgent = WalletService.getServiceVersion(); let bc; try { bc = BlockChainExplorer(opts); } catch (ex) { this.logw('Could not instantiate blockchain explorer', ex); } return bc; } getUtxosForCurrentWallet(opts, cb) { opts = opts || {}; const utxoKey = utxo => { return utxo.txid + '|' + utxo.vout; }; let coin, allAddresses, allUtxos, utxoIndex, addressStrs, bc, wallet; async.series( [ next => { this.getWallet({}, (err, w) => { if (err) return next(err); wallet = w; if (wallet.scanStatus == 'error') return cb(Errors.WALLET_NEED_SCAN); coin = wallet.coin; bc = this._getBlockchainExplorer(coin, wallet.network); if (!bc) return cb(new Error('Could not get blockchain explorer instance')); return next(); }); }, next => { if (_.isArray(opts.addresses)) { allAddresses = opts.addresses; return next(); } // even with Grouping we need address for pubkeys and path (see last step) this.storage.fetchAddresses(this.walletId, (err, addresses) => { _.each(addresses, x => { ChainService.addressFromStorageTransform(wallet.coin, wallet.network, x); }); allAddresses = addresses; if (allAddresses.length == 0) return cb(null, []); return next(); }); }, next => { addressStrs = _.map(allAddresses, 'address'); return next(); }, next => { if (!wallet.isComplete()) return next(); this._getBlockchainHeight(wallet.coin, wallet.network, (err, height, hash) => { if (err) return next(err); const dustThreshold = Bitcore_[wallet.coin].Transaction.DUST_AMOUNT; bc.getUtxos(wallet, height, (err, utxos) => { if (err) return next(err); if (utxos.length == 0) return cb(null, []); // filter out DUST allUtxos = _.filter(utxos, x => { return x.satoshis >= dustThreshold; }); utxoIndex = _.keyBy(allUtxos, utxoKey); return next(); }); }); }, next => { this.getPendingTxs({}, (err, txps) => { if (err) return next(err); const lockedInputs = _.map(_.flatten(_.map(txps, 'inputs')), utxoKey); _.each(lockedInputs, input => { if (utxoIndex[input]) { utxoIndex[input].locked = true; } }); logger.debug(`Got ${lockedInputs.length} locked utxos`); return next(); }); }, next => { const now = Math.floor(Date.now() / 1000); // Fetch latest broadcasted txs and remove any spent inputs from the // list of UTXOs returned by the block explorer. This counteracts any out-of-sync // effects between broadcasting a tx and getting the list of UTXOs. // This is especially true in the case of having multiple instances of the block explorer. this.storage.fetchBroadcastedTxs( this.walletId, { minTs: now - 24 * 3600, limit: 100 }, (err, txs) => { if (err) return next(err); const spentInputs = _.map(_.flatten(_.map(txs, 'inputs')), utxoKey); _.each(spentInputs, input => { if (utxoIndex[input]) { utxoIndex[input].spent = true; } }); allUtxos = _.reject(allUtxos, { spent: true }); logger.debug(`Got ${allUtxos.length} usable UTXOs`); return next(); } ); }, next => { // Needed for the clients to sign UTXOs const addressToPath = _.keyBy(allAddresses, 'address'); _.each(allUtxos, utxo => { if (!addressToPath[utxo.address]) { if (!opts.addresses) this.logw('Ignored UTXO!: ' + utxo.address); return; } utxo.path = addressToPath[utxo.address].path; utxo.publicKeys = addressToPath[utxo.address].publicKeys; }); return next(); } ], (err: any) => { // TODO` if (err && err.statusCode == 404) { return this.registerWalletV8(wallet, cb); } return cb(err, allUtxos); } ); } /** * Returns list of UTXOs * @param {Object} opts * @param {Array} [opts.addresses] - List of addresses. options. only one address is supported * @returns {Array} utxos - List of UTXOs. */ getUtxos(opts, cb) { opts = opts || {}; if (opts.coin) { return cb(new ClientError('coins option no longer supported')); } if (opts.addresses) { if (opts.addresses.length > 1) return cb(new ClientError('Addresses option only support 1 address')); this.getWallet({}, (err, wallet) => { if (err) return cb(err); const bc = this._getBlockchainExplorer(wallet.coin, wallet.network); if (!bc) { return cb(new Error('Could not get blockchain explorer instance')); } const address = opts.addresses[0]; const A = Bitcore_[wallet.coin].Address; let addrObj: { network?: { name?: string } } = {}; try { addrObj = new A(address); } catch (ex) { return cb(null, []); } if (addrObj.network.name != wallet.network) { return cb(null, []); } this._getBlockchainHeight(wallet.coin, wallet.network, (err, height, hash) => { if (err) return cb(err); bc.getAddressUtxos(address, height, (err, utxos) => { if (err) return cb(err); return cb(null, utxos); }); }); }); } else { this.getUtxosForCurrentWallet({}, cb); } } /** * Returns list of Coins for TX * @param {Object} opts * @param {string} opts.coin - The coin of the transaction. * @param {string} opts.network - the network of the transaction. * @param {string} opts.txId - the transaction id. * @returns {Obejct} coins - Inputs and Outputs of the transaction. */ getCoinsForTx(opts, cb) { this.getWallet({}, (err, wallet) =