'use strict'; var imports = require('soop').imports(); var bitcore = require('bitcore'); var BIP32 = bitcore.BIP32; var Address = bitcore.Address; var Script = bitcore.Script; var coinUtil = bitcore.util; var Transaction = bitcore.Transaction; var util = bitcore.util; var Storage = imports.Storage || require('../storage/Base.js'); var storage = Storage.default(); function PublicKeyRing(opts) { opts = opts || {}; this.walletId = opts.walletId; this.network = opts.networkName === 'livenet' ? bitcore.networks.livenet : bitcore.networks.testnet; this.requiredCopayers = opts.requiredCopayers || 3; this.totalCopayers = opts.totalCopayers || 5; this.copayersBIP32 = opts.copayersBIP32 || []; this.changeAddressIndex= opts.changeAddressIndex || 0; this.addressIndex= opts.addressIndex || 0; this.publicKeysCache = opts.publicKeysCache || {}; } /* * This follow Electrum convetion, as described in * https://bitcointalk.org/index.php?topic=274182.0 * * We should probably adopt the next standard once it's ready, as discussed in: * http://sourceforge.net/p/bitcoin/mailman/message/32148600/ * */ PublicKeyRing.Branch = function (index, isChange) { // first 0 is for future use: could be copayerId. return 'm/0/'+(isChange?1:0)+'/'+index; }; PublicKeyRing.SIGNING_BRANCH = 'm/100/0/0'; PublicKeyRing.fromObj = function (data) { if (data instanceof PublicKeyRing) { throw new Error('bad data format: Did you use .toObj()?'); } data.copayersBIP32 = data.copayersExtPubKeys.map(function(pk) { return new BIP32(pk); }); return new PublicKeyRing(data); }; PublicKeyRing.prototype.toObj = function() { return { walletId: this.walletId, networkName: this.network.name, requiredCopayers: this.requiredCopayers, totalCopayers: this.totalCopayers, changeAddressIndex: this.changeAddressIndex, addressIndex: this.addressIndex, copayersExtPubKeys: this.copayersBIP32.map( function (b) { return b.extendedPublicKeyString(); }), publicKeysCache: this.publicKeysCache }; }; PublicKeyRing.prototype.serialize = function () { return JSON.stringify(this.toObj()); }; PublicKeyRing.prototype.getCopayerId = function(i) { this.copayerIds = this.copayerIds || []; if (!this.copayerIds[i]) { var path = PublicKeyRing.SIGNING_BRANCH; var bip32 = this.copayersBIP32[i].derive(path); this.copayerIds[i]= bip32.eckey.public.toString('hex'); } return this.copayerIds[i]; }; PublicKeyRing.prototype.myCopayerId = function(i) { return this.getCopayerId(0); }; PublicKeyRing.prototype.registeredCopayers = function () { return this.copayersBIP32.length; }; PublicKeyRing.prototype.isComplete = function () { return this.registeredCopayers() >= this.totalCopayers; }; PublicKeyRing.prototype._checkKeys = function() { if (!this.isComplete()) throw new Error('dont have required keys yet'); }; PublicKeyRing.prototype._newExtendedPublicKey = function () { return new BIP32(this.network.name) .extendedPublicKeyString(); }; PublicKeyRing.prototype.addCopayer = function (newEpk) { if (this.isComplete()) throw new Error('already have all required key:' + this.totalCopayers); if (!newEpk) { newEpk = this._newExtendedPublicKey(); } this.copayersBIP32.forEach(function(b){ if (b.extendedPublicKeyString() === newEpk) throw new Error('already have that key'); }); this.copayersBIP32.push(new BIP32(newEpk)); return newEpk; }; PublicKeyRing.prototype.getPubKeys = function (index, isChange) { this._checkKeys(); var path = PublicKeyRing.Branch(index, isChange); var pubKeys = this.publicKeysCache[path]; if (!pubKeys) { pubKeys = []; var l = this.copayersBIP32.length; for(var i=0; i this.changeAddressIndex) || (!isChange && index > this.addressIndex)) { console.log('Out of bounds at getAddress: Index %d isChange: %d', index, isChange); throw new Error('index out of bound'); } }; // TODO this could be cached PublicKeyRing.prototype.getRedeemScript = function (index, isChange) { this._checkIndexRange(index, isChange); var pubKeys = this.getPubKeys(index, isChange); var script = Script.createMultisig(this.requiredCopayers, pubKeys); return script; }; // TODO this could be cached PublicKeyRing.prototype.getAddress = function (index, isChange) { this._checkIndexRange(index, isChange); var script = this.getRedeemScript(index,isChange); return Address.fromScript(script, this.network.name); }; // TODO this could be cached PublicKeyRing.prototype._addScriptMap = function (map, index, isChange) { this._checkIndexRange(index, isChange); var script = this.getRedeemScript(index,isChange); map[Address.fromScript(script, this.network.name).toString()] = script.getBuffer().toString('hex'); }; // TODO this could be cached PublicKeyRing.prototype.getScriptPubKeyHex = function (index, isChange) { this._checkIndexRange(index, isChange); var addr = this.getAddress(index,isChange); return Script.createP2SH(addr.payload()).getBuffer().toString('hex'); }; //generate a new address, update index. PublicKeyRing.prototype.generateAddress = function(isChange) { var ret = this.getAddress(isChange ? this.changeAddressIndex : this.addressIndex, isChange); if (isChange) { this.changeAddressIndex++; } else { this.addressIndex++; } return ret; }; PublicKeyRing.prototype.getAddresses = function(onlyMain) { var ret = []; for (var i=0; i