get Script Address for Wallet

This commit is contained in:
Matias Alejo Garcia 2014-03-26 22:00:42 -03:00
parent 3bf02173c5
commit b57c1544bb
2 changed files with 78 additions and 16 deletions

View File

@ -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<l; i++) {
pubkey[i] = this.cosignersWallets[i].getPubKey(index, isChange);
}
var version = this.network.addressScript;
var script = Script.createMultisig(this.requiredCosigners, pubkey);
var buf = script.buffer;
var hash = coinUtil.sha256ripe160(buf);
var addr = new Address(version, hash);
var addrStr = addr.as('base58');
return addrStr;
};
Wallet.prototype.createAddress = function(isChange) {
var ret =
this.getAddress(isChange ? this.changeAddressIndex : this.addressIndex, isChange);
if (isChange)
this.addressIndex++;
else
this.changeAddressIndex++;
return ret;
};
// Input: Bitcore's Transaction, sign with ownPK
// return partially signed or fully signed tx

View File

@ -6,6 +6,7 @@ var bitcore = bitcore || require('../node_modules/bitcore');
var cosign = cosign || {};
var Address = bitcore.Address;
var fakeStorage = require('./FakeStorage');
var Wallet = cosign.Wallet || require('soop').load('../js/models/Wallet', {Storage: fakeStorage});
@ -21,7 +22,7 @@ var createW = function () {
var cosigners = [];
for(var i=0; i<4; i++) {
var c = new Wallet(config);
w.haveAllNeededPubKeys().should.equal(false);
w.haveAllRequiredPubKeys().should.equal(false);
w.addCosignerExtendedPubKey(c.getExtendedPubKey());
cosigners.push(c);
@ -71,9 +72,9 @@ describe('Wallet model', function() {
should.exist(w2);
w2.registeredCosigners().should.equal(1);
w2.haveAllNeededPubKeys().should.equal(false);
w2.haveAllRequiredPubKeys().should.equal(false);
w2.getChangeAddress.bind(0).should.throw();
w2.getAddress.bind(false).should.throw();
});
it('should add and check when adding shared pub keys', function () {
@ -81,7 +82,7 @@ describe('Wallet model', function() {
var w = k.w;
var cosigners = k.cosigners;
w.haveAllNeededPubKeys().should.equal(true);
w.haveAllRequiredPubKeys().should.equal(true);
w.addCosignerExtendedPubKey.bind(w.getExtendedPubKey()).should.throw();
w.addCosignerExtendedPubKey.bind(cosigners[0].getExtendedPubKey()).should.throw();
w.addCosignerExtendedPubKey.bind((new Wallet(config)).getExtendedPubKey()).should.throw();
@ -98,13 +99,29 @@ describe('Wallet model', function() {
w.store.bind().should.throw();
var w2 = Wallet.read(ID);
w2.haveAllNeededPubKeys().should.equal(true);
w2.haveAllRequiredPubKeys().should.equal(true);
w2.addCosignerExtendedPubKey.bind(w.getExtendedPubKey()).should.throw();
w2.addCosignerExtendedPubKey.bind(cosigners[0].getExtendedPubKey()).should.throw();
w2.addCosignerExtendedPubKey.bind((new Wallet(config)).getExtendedPubKey()).should.throw();
});
it('should create some p2sh address', function () {
var k = createW();
var w = k.w;
for(var isChange=0; isChange<2; isChange++) {
for(var i=0; i<5; i++) {
var addr = w.getAddress(i,isChange);
var a = new Address(addr);
a.isValid().should.equal(true);
a.isScript().should.equal(true);
a.network().name.should.equal('livenet');
}
}
});
});