diff --git a/js/models/wallet.js b/js/models/PublicKeyRing.js similarity index 50% rename from js/models/wallet.js rename to js/models/PublicKeyRing.js index 5d0ad3642..fd29e26c8 100644 --- a/js/models/wallet.js +++ b/js/models/PublicKeyRing.js @@ -26,48 +26,47 @@ var PUBLIC_BRANCH = 'm/0/'; var CHANGE_BRANCH = 'm/1/'; -function Wallet(opts) { +function PublicKeyRing(opts) { opts = opts || {}; this.network = opts.network === 'livenet' ? bitcore.networks.livenet : bitcore.networks.testnet; - this.requiredCosigners = opts.requiredCosigners || 3; - this.totalCosigners = opts.totalCosigners || 5; + this.requiredCopayers = opts.requiredCopayers || 3; + this.totalCopayers = opts.totalCopayers || 5; - this.id = opts.id || Wallet.getRandomId(); + this.id = opts.id || PublicKeyRing.getRandomId(); this.dirty = 1; - this.cosignersWallets = []; + this.copayersWallets = []; this.bip32 = new BIP32(opts.bytes || this.network.name); this.changeAddressIndex=0; this.addressIndex=0; - } -Wallet.getRandomId = function () { +PublicKeyRing.getRandomId = function () { return buffertools.toHex(coinUtil.generateNonce()); }; -Wallet.decrypt = function (passphrase, encPayload) { +PublicKeyRing.decrypt = function (passphrase, encPayload) { log('[wallet.js.35] TODO READ: passphrase IGNORED'); return encPayload; }; -Wallet.encrypt = function (passphrase, payload) { +PublicKeyRing.encrypt = function (passphrase, payload) { log('[wallet.js.92] TODO: passphrase IGNORED'); return payload; }; -Wallet.read = function (id, passphrase) { +PublicKeyRing.read = function (id, passphrase) { var encPayload = storage.read(id); if (!encPayload) throw new Error('Could not find wallet data'); var data; try { - data = JSON.parse( Wallet.decrypt( passphrase, encPayload )); + data = JSON.parse( PublicKeyRing.decrypt( passphrase, encPayload )); } catch (e) { throw new Error('error in storage: '+ e.toString()); return; @@ -80,12 +79,12 @@ Wallet.read = function (id, passphrase) { bitcore.networks.livenet : bitcore.networks.testnet }; - var w = new Wallet(config); + var w = new PublicKeyRing(config); - w.requiredCosigners = data.neededCosigners; - w.totalCosigners = data.totalCosigners; - w.cosignersWallets = data.cosignersExtPubKeys.map( function (pk) { - return new Wallet({bytes:pk, network: w.network.name}); + w.requiredCopayers = data.neededCopayers; + w.totalCopayers = data.totalCopayers; + w.copayersWallets = data.copayersExtPubKeys.map( function (pk) { + return new PublicKeyRing({bytes:pk, network: w.network.name}); }); w.dirty = 0; @@ -93,57 +92,47 @@ Wallet.read = function (id, passphrase) { return w; }; -Wallet.prototype.serialize = function () { +PublicKeyRing.prototype.serialize = function () { return JSON.stringify({ id: this.id, network: this.network.name, - requiredCosigners: this.neededCosigners, - totalCosigners: this.totalCosigners, - cosignersExtPubKeys: this.cosignersWallets.map( function (b) { + requiredCopayers: this.neededCopayers, + totalCopayers: this.totalCopayers, + copayersExtPubKeys: this.copayersWallets.map( function (b) { return b.getMasterExtendedPubKey(); }), - priv: this.getMasterExtendedPrivKey(), }); }; -Wallet.prototype.store = function (passphrase) { +PublicKeyRing.prototype.store = function (passphrase) { if (!this.id) throw new Error('wallet has no id'); - storage.save(this.id, Wallet.encrypt(passphrase,this.serialize())); + storage.save(this.id, PublicKeyRing.encrypt(passphrase,this.serialize())); this.dirty = 0; return true; }; -Wallet.prototype.registeredCosigners = function () { - if (! this.cosignersWallets) return 1; +PublicKeyRing.prototype.registeredCopayers = function () { + if (! this.copayersWallets) return 1; // 1 is self. - return 1 + this.cosignersWallets.length; + return 1 + this.copayersWallets.length; }; -Wallet.prototype.getMasterExtendedPrivKey = function () { - - if (!this.bip32) - throw new Error('no priv key defined on the wallet'); - - return this.bip32.extended_private_key_string(); +PublicKeyRing.prototype.getMasterExtendedPubKey = function () { + return this.bip32.extendedPublicKeyString(); }; -Wallet.prototype.getMasterExtendedPubKey = function () { - return this.bip32.extended_public_key_string(); +PublicKeyRing.prototype.haveAllRequiredPubKeys = function () { + return this.registeredCopayers() === this.totalCopayers; }; - -Wallet.prototype.haveAllRequiredPubKeys = function () { - return this.registeredCosigners() === this.totalCosigners; -}; - -Wallet.prototype._checkKeys = function() { +PublicKeyRing.prototype._checkKeys = function() { if (!this.haveAllRequiredPubKeys()) throw new Error('dont have required keys yet'); @@ -151,26 +140,26 @@ Wallet.prototype._checkKeys = function() { // should receive an array also? -Wallet.prototype.addCosignerExtendedPubKey = function (newEpk) { +PublicKeyRing.prototype.addCopayerExtendedPubKey = function (newEpk) { if (this.haveAllRequiredPubKeys()) - throw new Error('already have all required key:' + this.totalCosigners); + throw new Error('already have all required key:' + this.totalCopayers); if (this.getMasterExtendedPubKey() === newEpk) throw new Error('already have that key (self key)'); - this.cosignersWallets.forEach(function(b){ + this.copayersWallets.forEach(function(b){ if (b.getMasterExtendedPubKey() === newEpk) throw new Error('already have that key'); }); - this.cosignersWallets.push(new Wallet({bytes:newEpk, network: this.network.name } )); + this.copayersWallets.push(new PublicKeyRing({bytes:newEpk, network: this.network.name } )); this.dirty = 1; }; -Wallet.prototype.getPubKey = function (index,isChange) { +PublicKeyRing.prototype.getPubKey = function (index,isChange) { var path = (isChange ? CHANGE_BRANCH : PUBLIC_BRANCH) + index; var bip32 = this.bip32.derive(path); @@ -178,49 +167,19 @@ Wallet.prototype.getPubKey = function (index,isChange) { return pub; }; - - - -Wallet.prototype.getCosignersPubKeys = function (index, isChange) { +PublicKeyRing.prototype.getCopayersPubKeys = function (index, isChange) { this._checkKeys(); var pubKeys = []; - var l = this.cosignersWallets.length; + var l = this.copayersWallets.length; for(var i=0; i buf1.length ? buf1.length : buf2.length; - for (var i = 0; i <= len; i++) { - if (buf1[i] === undefined) - return -1; //shorter strings come first - if (buf2[i] === undefined) - return 1; - if (buf1[i] < buf2[i]) - return -1; - if (buf1[i] > buf2[i]) - return 1; - else - continue; - } - return 0; - }); -}; - - - -Wallet.prototype.getAddress = function (index, isChange) { +PublicKeyRing.prototype.getAddress = function (index, isChange) { if ( (isChange && index > this.changeAddressIndex) || (!isChange && index > this.addressIndex)) { @@ -228,10 +187,9 @@ Wallet.prototype.getAddress = function (index, isChange) { throw new Error('index out of bound'); } - var pubKeys = this.getCosignersSortedPubKeys(index, isChange); - + var pubKeys = this.getCopayersPubKeys(); var version = this.network.addressScript; - var script = Script.createMultisig(this.requiredCosigners, pubKeys); + var script = Script.createMultisig(this.requiredCopayers, pubKeys); var buf = script.buffer; var hash = coinUtil.sha256ripe160(buf); var addr = new Address(version, hash); @@ -239,11 +197,11 @@ Wallet.prototype.getAddress = function (index, isChange) { return addrStr; }; -Wallet.prototype.createAddress = function(isChange) { +//generate a new address, update index. +PublicKeyRing.prototype.generateAddress = function(isChange) { var ret = this.getAddress(isChange ? this.changeAddressIndex : this.addressIndex, isChange); - if (isChange) this.addressIndex++; else @@ -253,7 +211,7 @@ Wallet.prototype.createAddress = function(isChange) { }; -Wallet.prototype.getAddresses = function() { +PublicKeyRing.prototype.getAddresses = function() { var ret = []; for (var i=0; i