From b57c1544bbdc683debe36a3895ee04f87743bc76 Mon Sep 17 00:00:00 2001 From: Matias Alejo Garcia Date: Wed, 26 Mar 2014 22:00:42 -0300 Subject: [PATCH] get Script Address for Wallet --- js/models/wallet.js | 67 +++++++++++++++++++++++++++++++++++++-------- test/test.wallet.js | 27 ++++++++++++++---- 2 files changed, 78 insertions(+), 16 deletions(-) diff --git a/js/models/wallet.js b/js/models/wallet.js index 924f10127..b88832dac 100644 --- a/js/models/wallet.js +++ b/js/models/wallet.js @@ -2,6 +2,8 @@ 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 Storage = imports.Storage || require('./Storage'); @@ -24,7 +26,7 @@ function Wallet(opts) { this.network = opts.network === 'livenet' ? bitcore.networks.livenet : bitcore.networks.testnet; - this.neededCosigners = opts.neededCosigners || 3; + this.requiredCosigners = opts.neededCosigners || 3; this.totalCosigners = opts.totalCosigners || 5; this.id = opts.id || Wallet.getRandomId(); @@ -32,6 +34,9 @@ function Wallet(opts) { this.dirty = 1; this.cosignersWallets = []; this.bip32 = new BIP32(opts.bytes || this.network.name); + + this.changeAddress + } @@ -70,7 +75,7 @@ Wallet.read = function (id, passphrase) { var w = new Wallet(config); - w.neededCosigners = data.neededCosigners; + w.requiredCosigners = data.neededCosigners; w.totalCosigners = data.totalCosigners; w.cosignersWallets = data.cosignersExtPubKeys.map( function (pk) { return new Wallet({bytes:pk, network: w.network.name}); @@ -85,7 +90,7 @@ Wallet.prototype.serialize = function () { return JSON.stringify({ id: this.id, network: this.network.name, - neededCosigners: this.neededCosigners, + requiredCosigners: this.neededCosigners, totalCosigners: this.totalCosigners, cosignersExtPubKeys: this.cosignersWallets.map( function (b) { return b.getExtendedPubKey(); @@ -127,11 +132,22 @@ Wallet.prototype.getExtendedPubKey = function () { }; +Wallet.prototype.haveAllRequiredPubKeys = function () { + return this.registeredCosigners() === this.totalCosigners; +}; + +Wallet.prototype._checkKeys = function() { + + if (!this.haveAllRequiredPubKeys()) + throw new Error('dont have required keys yet'); +}; + + // should receive an array also? Wallet.prototype.addCosignerExtendedPubKey = function (newEpk) { - if (this.haveAllNeededPubKeys()) - throw new Error('already have all needed key:' + this.totalCosigners); + if (this.haveAllRequiredPubKeys()) + throw new Error('already have all required key:' + this.totalCosigners); if (this.getExtendedPubKey() === newEpk) throw new Error('already have that key (self kehy)'); @@ -147,18 +163,47 @@ Wallet.prototype.addCosignerExtendedPubKey = function (newEpk) { }; -Wallet.prototype.haveAllNeededPubKeys = function () { - return this.registeredCosigners() === this.totalCosigners; +Wallet.prototype.getPubKey = function (index,isChange) { + + var path = (isChange ? CHANGE_BRANCH : PUBLIC_BRANCH) + index; + var bip32 = this.bip32.derive(path); + var pub = bip32.eckey.public; + return pub; }; -Wallet.prototype.getChangeAddress = function (index) { +Wallet.prototype.getAddress = function (index, isChange) { + this._checkKeys(); - //index can be 0, 1, 2, etc. - if (! this.haveAllNeededPubKeys() ) - throw new Error('cosigners pub key missing'); + var pubkey = []; + var l = this.cosignersWallets.length; + for(var i=0; i