'use strict'; var imports = require('soop').imports(); var bitcore = require('bitcore'); var HK = bitcore.HierarchicalKey; var PrivateKey = require('./PrivateKey'); 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.copayersHK = opts.copayersHK || []; this.changeAddressIndex= opts.changeAddressIndex || 0; this.addressIndex= opts.addressIndex || 0; this.publicKeysCache = opts.publicKeysCache || {}; this.nicknameFor = opts.nicknameFor || {}; this.copayerIds = []; } /* * Based on https://github.com/maraoz/bips/blob/master/bip-NNNN.mediawiki * m / purpose' / cosigner_index / change / address_index */ var PURPOSE = 45; var MAX_NON_HARDENED = 0x8000000 - 1; var SHARED_INDEX = MAX_NON_HARDENED - 0; var ID_INDEX = MAX_NON_HARDENED - 1; var BIP45_PUBLIC_PREFIX = 'm/'+ PURPOSE+'\''; PublicKeyRing.BIP45_PUBLIC_PREFIX = BIP45_PUBLIC_PREFIX; PublicKeyRing.Branch = function(address_index, isChange, cosigner_index) { var ret = 'm/'+ (typeof cosigner_index !== 'undefined'? cosigner_index: SHARED_INDEX)+'/'+ (isChange?1:0)+'/'+ address_index; return ret; }; PublicKeyRing.FullBranch = function(address_index, isChange, cosigner_index) { var sub = PublicKeyRing.Branch(address_index, isChange, cosigner_index); sub = sub.substring(2); return BIP45_PUBLIC_PREFIX + '/' + sub; }; PublicKeyRing.IdFullBranch = function() { return PublicKeyRing.FullBranch(0, 0, ID_INDEX); }; PublicKeyRing.fromObj = function (data) { if (data instanceof PublicKeyRing) { throw new Error('bad data format: Did you use .toObj()?'); } var ret = new PublicKeyRing(data); for (var k in data.copayersExtPubKeys) { ret.addCopayer(data.copayersExtPubKeys[k]); } return ret; }; 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.copayersHK.map( function (b) { return b.extendedPublicKeyString(); }), nicknameFor: this.nicknameFor, publicKeysCache: this.publicKeysCache }; }; PublicKeyRing.prototype.getCopayerId = function(i) { return this.copayerIds[i]; }; PublicKeyRing.prototype.registeredCopayers = function () { return this.copayersHK.length; }; PublicKeyRing.prototype.isComplete = function () { return this.registeredCopayers() === this.totalCopayers; }; PublicKeyRing.prototype.getAllCopayerIds = function() { return this.copayerIds; }; PublicKeyRing.prototype.myCopayerId = function(i) { return this.getCopayerId(0); }; PublicKeyRing.prototype._checkKeys = function() { if (!this.isComplete()) throw new Error('dont have required keys yet'); }; PublicKeyRing.prototype._newExtendedPublicKey = function () { return new PrivateKey({networkName: this.network.name}) .deriveBIP45Branch() .extendedPublicKeyString(); }; PublicKeyRing.prototype._updateBip = function (index) { var hk = this.copayersHK[index].derive(PublicKeyRing.Branch(0, 0, ID_INDEX)); this.copayerIds[index]= hk.eckey.public.toString('hex'); }; PublicKeyRing.prototype._setNicknameForIndex = function (index, nickname) { this.nicknameFor[this.copayerIds[index]] = nickname; }; PublicKeyRing.prototype.nicknameForIndex = function(index) { return this.nicknameFor[this.copayerIds[index]]; }; PublicKeyRing.prototype.nicknameForCopayer = function(copayerId) { return this.nicknameFor[copayerId]; }; PublicKeyRing.prototype.addCopayer = function(newEpk, nickname) { if (this.isComplete()) throw new Error('PKR already has all required key:' + this.totalCopayers); this.copayersHK.forEach(function(b){ if (b.extendedPublicKeyString() === newEpk) throw new Error('PKR already has that key'); }); if (!newEpk) { newEpk = this._newExtendedPublicKey(); } var i = this.copayersHK.length; var bip = new HK(newEpk); this.copayersHK.push(bip); this._updateBip(i); if (nickname) { this._setNicknameForIndex(i, nickname); } 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.copayersHK.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) { var script = this.getRedeemScript(index,isChange); return Address.fromScript(script, this.network.name); }; // TODO this could be cached PublicKeyRing.prototype.getScriptPubKeyHex = function (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(opts) { return this.getAddressesInfo(opts).map(function(info) { return info.address; }); }; PublicKeyRing.prototype.getAddressesInfo = function(opts) { opts = opts || {}; var ret = []; if (!opts.excludeChange) { for (var i=0; i