bitcore-wallet-service
Version:
A service for Mutisig HD Bitcoin Wallets
1,586 lines (1,421 loc) • 178 kB
text/typescript
import * as async from 'async';
import {
Constants as ConstantsCWC,
Validation
} from 'crypto-wallet-core';
import * as _ from 'lodash';
import Moralis from 'moralis';
import 'source-map-support/register';
import config from '../config';
import logger from './logger';
import { serverMessages as deprecatedServerMessage } from '../deprecated-serverMessages';
import { BanxaService } from '../externalServices/banxa';
import { ChangellyService } from '../externalServices/changelly';
import { MoonpayService } from '../externalServices/moonpay';
import { OneInchService } from '../externalServices/oneInch';
import { RampService } from '../externalServices/ramp';
import { SardineService } from '../externalServices/sardine';
import { SimplexService } from '../externalServices/simplex';
import { ThorswapService } from '../externalServices/thorswap';
import { TransakService } from '../externalServices/transak';
import { WyreService } from '../externalServices/wyre';
import { serverMessages } from '../serverMessages';
import { BCHAddressTranslator } from './bchaddresstranslator';
import { BlockChainExplorer } from './blockchainexplorer';
import { V8 } from './blockchainexplorers/v8';
import { ChainService } from './chain/index';
import { Common } from './common';
import { ClientError } from './errors/clienterror';
import { Errors } from './errors/errordefinitions';
import { FiatRateService } from './fiatrateservice';
import { Lock } from './lock';
import { MessageBroker } from './messagebroker';
import {
Advertisement,
Copayer,
ExternalServicesConfig,
INotification,
ITxProposal,
IWallet,
Notification,
Preferences,
PushNotificationSub,
Session,
TxConfirmationSub,
TxNote,
TxProposal,
Wallet
} from './model';
import { Storage } from './storage';
const Uuid = require('uuid');
const $ = require('preconditions').singleton();
const EmailValidator = require('email-validator');
const Bitcore = require('bitcore-lib');
const Bitcore_ = {
btc: Bitcore,
bch: require('bitcore-lib-cash'),
eth: Bitcore,
matic: Bitcore,
arb: Bitcore,
base: Bitcore,
op: Bitcore,
xrp: Bitcore,
doge: require('bitcore-lib-doge'),
ltc: require('bitcore-lib-ltc'),
sol: Bitcore,
};
const Utils = Common.Utils;
const Constants = Common.Constants;
const Defaults = Common.Defaults;
const Services = Common.Services;
let request = require('request');
let initialized = false;
let doNotCheckV8 = false;
let isMoralisInitialized = false;
let lock: Lock;
let storage: Storage;
let blockchainExplorer: V8;
let blockchainExplorerOpts;
let messageBroker: MessageBroker;
let fiatRateService: FiatRateService;
let serviceVersion: string;
interface IAddress {
coin: string;
chain: string;
network: string;
address: string;
hasActivity: boolean;
isChange?: boolean;
}
export interface IWalletService {
lock: Lock;
storage: Storage;
blockchainExplorer: V8;
blockchainExplorerOpts: any;
messageBroker: MessageBroker;
fiatRateService: FiatRateService;
notifyTicker: number;
userAgent: string;
walletId: string;
copayerId: string;
appName: string;
appVersion: { agent?: string; major?: number; minor?: number };
parsedClientVersion: { agent?: string; major?: number; minor?: number };
clientVersion: string;
copayerIsSupportStaff: boolean;
copayerIsMarketingStaff: boolean;
request: any;
}
function boolToNum(x: boolean) {
return x ? 1 : 0;
}
/**
* Creates an instance of the Bitcore Wallet Service.
* @constructor
*/
export class WalletService implements IWalletService {
lock: Lock;
storage: Storage;
blockchainExplorer: V8;
blockchainExplorerOpts: any;
messageBroker: MessageBroker;
fiatRateService: FiatRateService;
notifyTicker: number;
userAgent: string;
walletId: string;
copayerId: string;
appName: string;
appVersion: { agent?: string; major?: number; minor?: number };
parsedClientVersion: { agent?: string; major?: number; minor?: number };
clientVersion: string;
copayerIsSupportStaff: boolean;
copayerIsMarketingStaff: boolean;
request: any;
externalServices: {
banxa: BanxaService;
changelly: ChangellyService;
moonpay: MoonpayService;
oneInch: OneInchService;
ramp: RampService;
sardine: SardineService;
simplex: SimplexService;
thorswap: ThorswapService;
transak: TransakService;
wyre: WyreService;
}
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;
this.externalServices = {
banxa: new BanxaService(),
changelly: new ChangellyService(),
moonpay: new MoonpayService(),
oneInch: new OneInchService(this.storage),
ramp: new RampService(),
sardine: new SardineService(),
simplex: new SimplexService(),
thorswap: new ThorswapService(),
transak: new TransakService(),
wyre: new WyreService(),
}
}
/**
* 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();
});
}
};
// Init Moralis
const initMoralis = async cb => {
if (!config.moralis || !config.moralis.apiKey) {
logger.warn('Moralis missing credentials');
return cb();
}
if (!isMoralisInitialized) {
try {
logger.info('Initializing Moralis...');
const API_KEY = config.moralis.apiKey;
await Moralis.start({
apiKey: API_KEY,
});
logger.info('Moralis initialized successfully!');
isMoralisInitialized = true;
return cb();
} catch (err) {
logger.error('Error initializing Moralis: ', err);
isMoralisInitialized = false;
return cb();
}
} else {
return cb();
}
};
async.series(
[
next => {
initStorage(next);
},
next => {
initMessageBroker(next);
},
next => {
initFiatRateService(next);
},
next => {
initMoralis(next);
}
],
err => {
lock = opts.lock || new Lock(storage);
if (err) {
logger.error('Could not initialize: %o', 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, 'Failed state: this.walletId undefined at <_runLocked()>');
this.lock.runLocked(this.walletId, { waitTime }, cb, task);
}
logi(message, ...args) {
if (typeof message === 'string' && args.length > 0 && !message.endsWith('%o')) {
for (let i = 0; i < args.length; i++) {
message += ' %o';
}
}
if (!this || !this.walletId) {
return logger.warn(message, ...args);
}
message = '<' + this.walletId + '>' + message;
return logger.info(message, ...args);
}
logw(message, ...args) {
if (typeof message === 'string' && args.length > 0 && !message.endsWith('%o')) {
for (let i = 0; i < args.length; i++) {
message += ' %o';
args[i] = args[i]?.stack || args[i]?.message || args[i];
}
}
if (!this || !this.walletId) {
return logger.warn(message, ...args);
}
message = '<' + this.walletId + '>' + message;
return logger.warn(message, ...args);
}
logd(message, ...args) {
if (typeof message === 'string' && args.length > 0 && !message.endsWith('%o')) {
for (let i = 0; i < args.length; i++) {
message += ' %o';
}
}
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.hardwareSourcePublicKey - public key from a hardware device for this copayer
* @param {string} opts.clientDerivedPublicKey - public key from the client for this walet
* @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, eth, doge, ltc).
* @param {string} opts.chain[='btc'] - The chain for this wallet (btc, bch, eth, doge, ltc).
* @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 {boolean} opts.useNativeSegwit - set addressType to P2WPKH, P2WSH, or P2TR (segwitVersion = 1)
* @param {number} opts.segwitVersion - 0 (default) = P2WPKH, P2WSH; 1 = P2TR
*/
createWallet(opts, cb) {
let pubKey;
opts.coin = opts.coin || Defaults.COIN;
if (!opts.chain) {
opts.chain = opts.coin; // chain === coin for stored clients
}
if (opts.chain === '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 (!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'));
}
if (!Utils.checkValueInCollection(opts.chain, Constants.CHAINS)) {
return cb(new ClientError('Invalid chain'));
}
opts.network = Utils.getNetworkName(opts.chain, opts.network) || 'livenet';
if (!Utils.checkValueInCollection(opts.network, Constants.NETWORKS[opts.chain])) {
return cb(new ClientError('Invalid network'));
}
if (opts.network === 'regtest' && !config.allowRegtest) {
return cb(new ClientError('Regtest is not allowed for this environment'));
}
const derivationStrategy = Constants.DERIVATION_STRATEGIES.BIP44;
let addressType = opts.n === 1 ? Constants.SCRIPT_TYPES.P2PKH : Constants.SCRIPT_TYPES.P2SH;
if (opts.useNativeSegwit && Utils.checkValueInCollection(opts.chain, Constants.NATIVE_SEGWIT_CHAINS)) {
switch (Number(opts.segwitVersion)) {
case 0:
default:
addressType = opts.n === 1 ? Constants.SCRIPT_TYPES.P2WPKH : Constants.SCRIPT_TYPES.P2WSH;
break;
case 1:
if (!Utils.checkValueInCollection(opts.chain, Constants.TAPROOT_CHAINS)) {
return cb(new ClientError('Invalid chain for P2TR'));
}
addressType = Constants.SCRIPT_TYPES.P2TR;
break;
}
}
try {
pubKey = new Bitcore.PublicKey.fromString(opts.pubKey);
} catch (ex) {
return cb(new ClientError('Invalid public key'));
}
// using coin for simplicity
if (opts.n > 1 && !ChainService.supportsMultisig(opts.chain)) {
return cb(new ClientError('Multisig wallets are not supported for this coin'));
}
// using coin for simplicity
if (ChainService.isSingleAddress(opts.chain)) {
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,
chain: opts.chain, // chain === coin for stored wallets
network: opts.network,
pubKey: pubKey.toString(),
singleAddress: !!opts.singleAddress,
derivationStrategy,
addressType,
nativeCashAddr: opts.nativeCashAddr,
usePurpose48: opts.n > 1 && !!opts.usePurpose48,
hardwareSourcePublicKey: opts.hardwareSourcePublicKey,
clientDerivedPublicKey: opts.clientDerivedPublicKey
});
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);
// backwards compatibility
if (!wallet.chain) wallet.chain = ChainService.getChain(wallet.coin);
// 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.fetchAddressByChain(Defaults.CHAIN, 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 = (wallet.copayers || []).map(copayer => {
if (copayer.id == this.copayerId) return copayer;
return _.omit(copayer, 'customData');
});
if (!opts.includeExtendedInfo) {
wallet = _.omit(wallet, walletExtendedKeys);
wallet.copayers = (wallet.copayers || []).map(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 pubKeys.find(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 (typeof opts === 'function') {
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, 'Failed state: walletId undefined at <_notify()>');
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 (typeof extraArgs === 'function') {
cb = extraArgs;
extraArgs = {};
}
const data = Object.assign(
{
txProposalId: txp.id,
creatorId: txp.creatorId,
amount: txp.getTotalAmount(),
message: txp.message,
tokenAddress: txp.tokenAddress,
multisigContractAddress: txp.multisigContractAddress
},
extraArgs
);
this._notify(type, data, {}, cb);
}
_addCopayerToWallet(wallet, opts, cb) {
const copayer = Copayer.create({
coin: wallet.coin,
chain: wallet.chain, // chain === coin for stored clients
name: opts.name,
copayerIndex: wallet.copayers.length,
xPubKey: opts.xPubKey,
hardwareSourcePublicKey: opts.hardwareSourcePublicKey,
clientDerivedPublicKey: opts.clientDerivedPublicKey,
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 (this.parsedClientVersion == null) {
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, eth, doge, ltc).
* @param {string} opts.chain[='btc'] - The expected chain for this wallet (btc, bch, eth, doge, ltc).
* @param {string} opts.name - The copayer name.
* @param {string} opts.xPubKey - Extended Public Key for this copayer
* @param {string} opts.hardwareSourcePublicKey - public key from a hardware device for this copayer
* @param {string} opts.clientDerivedPublicKey - public key from the client for this wallet
* @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', 'requestPubKey', 'copayerSignature'], cb)) return;
if (!opts.name) return cb(new ClientError('Invalid copayer name'));
opts.coin = opts.coin || Defaults.COIN;
if (!opts.chain) {
opts.chain = opts.coin; // chain === coin for stored clients
}
if (!Utils.checkValueInCollection(opts.chain, Constants.CHAINS)) return cb(new ClientError('Invalid coin'));
let xPubKey;
if (!opts.hardwareSourcePublicKey && !opts.clientDerivedPublicKey) {
if (!checkRequired(opts, ['xPubKey'], cb)) return;
try {
xPubKey = Bitcore_[opts.chain].HDPublicKey(opts.xPubKey);
} catch (ex) {
return cb(new ClientError('Invalid extended public key'));
}
if (xPubKey.network == null) {
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.hardwareSourcePublicKey || opts.clientDerivedPublicKey) {
this._addCopayerToWallet(wallet, opts, cb);
return;
}
if (opts.chain === '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.chain != wallet.chain) {
return cb(new ClientError('The wallet you are trying to join was created for a different chain'));
}
if (!Utils.compareNetworks(wallet.network, xPubKey.network.name, wallet.chain)) {
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 (wallet.copayers?.find(c => c.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
* @param {string} opts.maticTokenAddresses - Linked token addresses
* @param {string} opts.opTokenAddresses - Linked token addresses
* @param {string} opts.baseTokenAddresses - Linked token addresses
* @param {string} opts.arbTokenAddresses - Linked token addresses
* @param {string} opts.solTokenAddresses - Linked token addresses
* @param {string} opts.multisigMaticInfo - 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 typeof value === 'string' && value.length == 2;
}
},
{
name: 'unit',
isValid(value) {
return typeof value === 'string' && ['btc', 'bit'].includes(value.toLowerCase());
}
},
{
name: 'tokenAddresses',
isValid(value) {
return Array.isArray(value) && value.every(x => Validation.validateAddress('eth', 'mainnet', x));
}
},
{
name: 'multisigEthInfo',
isValid(value) {
return (
Array.isArray(value) &&
value.every(x => Validation.validateAddress('eth', 'mainnet', x.multisigContractAddress))
);
}
},
{
name: 'maticTokenAddresses',
isValid(value) {
return Array.isArray(value) && value.every(x => Validation.validateAddress('matic', 'mainnet', x));
}
},
{
name: 'multisigMaticInfo',
isValid(value) {
return (
Array.isArray(value) &&
value.every(x => Validation.validateAddress('matic', 'mainnet', x.multisigContractAddress))
);
}
},
{
name: 'opTokenAddresses',
isValid(value) {
return Array.isArray(value) && value.every(x => Validation.validateAddress('op', 'mainnet', x));
}
},
{
name: 'baseTokenAddresses',
isValid(value) {
return Array.isArray(value) && value.every(x => Validation.validateAddress('base', 'mainnet', x));
}
},
{
name: 'arbTokenAddresses',
isValid(value) {
return Array.isArray(value) && value.every(x => Validation.validateAddress('arb', 'mainnet', x));
}
},
{
name: 'solTokenAddresses',
isValid(value) {
return Array.isArray(value) && value.every(x => Validation.validateAddress('sol', 'mainnet', x));
}
},
];
opts = _.pick(opts, preferences.map(p => p.name));
try {
for (const preference of preferences) {
const value = opts[preference.name];
if (!value) continue;
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 (!Constants.EVM_CHAINS[wallet.chain.toUpperCase()]) {
opts.tokenAddresses = null;
opts.multisigEthInfo = null;
}
if (wallet.coin != 'matic') {
opts.maticTokenAddresses = null;
opts.multisigMaticInfo = null;
}
this._runLocked(cb, cb => {
this.storage.fetchPreferences<Preferences>(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 eth tokenAddresses
if (opts.tokenAddresses) {
oldPref = oldPref || {} as Preferences;
oldPref.tokenAddresses = oldPref.tokenAddresses || [];
preferences.tokenAddresses = _.uniq(oldPref.tokenAddresses.concat(opts.tokenAddresses));
}
// merge eth multisigEthInfo
if (opts.multisigEthInfo) {
oldPref = oldPref || {} as Preferences;
oldPref.multisigEthInfo = oldPref.multisigEthInfo || [];
preferences.multisigEthInfo = _.uniq(
oldPref.multisigEthInfo.concat(opts.multisigEthInfo).reduce((x: any[], y: any) => {
let exists = false;
for (let e of x) {
// 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];
}, []) as object[]
);
}
// merge matic tokenAddresses
if (opts.maticTokenAddresses) {
oldPref = oldPref || {} as Preferences;
oldPref.maticTokenAddresses = oldPref.maticTokenAddresses || [];
preferences.maticTokenAddresses = _.uniq(oldPref.maticTokenAddresses.concat(opts.maticTokenAddresses));
}
// merge op tokenAddresses
if (opts.opTokenAddresses) {
oldPref = oldPref || {} as Preferences;
oldPref.opTokenAddresses = oldPref.opTokenAddresses || [];
preferences.opTokenAddresses = _.uniq(oldPref.opTokenAddresses.concat(opts.opTokenAddresses));
}
// merge base tokenAddresses
if (opts.baseTokenAddresses) {
oldPref = oldPref || {} as Preferences;
oldPref.baseTokenAddresses = oldPref.baseTokenAddresses || [];
preferences.baseTokenAddresses = _.uniq(oldPref.baseTokenAddresses.concat(opts.baseTokenAddresses));
}
// merge arb tokenAddresses
if (opts.arbTokenAddresses) {
oldPref = oldPref || {} as Preferences;
oldPref.arbTokenAddresses = oldPref.arbTokenAddresses || [];
preferences.arbTokenAddresses = _.uniq(oldPref.arbTokenAddresses.concat(opts.arbTokenAddresses));
}
if (opts.solTokenAddresses) {
oldPref = oldPref || {} as Preferences;
oldPref.solTokenAddresses = oldPref.solTokenAddresses || [];
preferences.solTokenAddresses = _.uniq(oldPref.solTokenAddresses.concat(opts.solTokenAddresses));
}
// merge matic multisigMaticInfo
if (opts.multisigMaticInfo) {
oldPref = oldPref || {} as Preferences;
oldPref.multisigMaticInfo = oldPref.multisigMaticInfo || [];
preferences.multisigMaticInfo = _.uniq(
oldPref.multisigMaticInfo.concat(opts.multisigMaticInfo).reduce((x: any[], y: any) => {
let exists = false;
for (let e of x) {
// add new token addresses linked to the multisig wallet
if (e.multisigContractAddress === y.multisigContractAddress) {
e.maticTokenAddresses = e.maticTokenAddresses || [];
y.maticTokenAddresses = _.uniq(e.maticTokenAddresses.concat(y.maticTokenAddresses));
e = Object.assign(e, y);
exists = true;
}
}
return exists ? x : [...x, y];
}, []) as object[]
);
}
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 ||
latestAddresses.some(a => a.hasActivity)
)
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, forceSync = false) {
let stoAddress = _.clone(address);
ChainService.addressToStorageTransform(wallet.chain, 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,
null,
forceSync
);
});
}
/**
* 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(!!opts.isChange);
} 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.chain == 'bch' && opts.noCashAddr) {
address = _.cloneDeep(address);
address.address = BCHAddressTranslator.translate(address.address, 'copay');
}
this._notify(
'NewAddress',
{
address: address.address
},
() => {
return cb(null, address);
}
);
},
true
);