get Script Address for Wallet
This commit is contained in:
parent
3bf02173c5
commit
b57c1544bb
2 changed files with 78 additions and 16 deletions
|
|
@ -2,6 +2,8 @@
|
||||||
var imports = require('soop').imports();
|
var imports = require('soop').imports();
|
||||||
var bitcore = require('bitcore');
|
var bitcore = require('bitcore');
|
||||||
var BIP32 = bitcore.BIP32;
|
var BIP32 = bitcore.BIP32;
|
||||||
|
var Address = bitcore.Address;
|
||||||
|
var Script = bitcore.Script;
|
||||||
var coinUtil= bitcore.util;
|
var coinUtil= bitcore.util;
|
||||||
|
|
||||||
var Storage = imports.Storage || require('./Storage');
|
var Storage = imports.Storage || require('./Storage');
|
||||||
|
|
@ -24,7 +26,7 @@ function Wallet(opts) {
|
||||||
this.network = opts.network === 'livenet' ?
|
this.network = opts.network === 'livenet' ?
|
||||||
bitcore.networks.livenet : bitcore.networks.testnet;
|
bitcore.networks.livenet : bitcore.networks.testnet;
|
||||||
|
|
||||||
this.neededCosigners = opts.neededCosigners || 3;
|
this.requiredCosigners = opts.neededCosigners || 3;
|
||||||
this.totalCosigners = opts.totalCosigners || 5;
|
this.totalCosigners = opts.totalCosigners || 5;
|
||||||
|
|
||||||
this.id = opts.id || Wallet.getRandomId();
|
this.id = opts.id || Wallet.getRandomId();
|
||||||
|
|
@ -32,6 +34,9 @@ function Wallet(opts) {
|
||||||
this.dirty = 1;
|
this.dirty = 1;
|
||||||
this.cosignersWallets = [];
|
this.cosignersWallets = [];
|
||||||
this.bip32 = new BIP32(opts.bytes || this.network.name);
|
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);
|
var w = new Wallet(config);
|
||||||
|
|
||||||
w.neededCosigners = data.neededCosigners;
|
w.requiredCosigners = data.neededCosigners;
|
||||||
w.totalCosigners = data.totalCosigners;
|
w.totalCosigners = data.totalCosigners;
|
||||||
w.cosignersWallets = data.cosignersExtPubKeys.map( function (pk) {
|
w.cosignersWallets = data.cosignersExtPubKeys.map( function (pk) {
|
||||||
return new Wallet({bytes:pk, network: w.network.name});
|
return new Wallet({bytes:pk, network: w.network.name});
|
||||||
|
|
@ -85,7 +90,7 @@ Wallet.prototype.serialize = function () {
|
||||||
return JSON.stringify({
|
return JSON.stringify({
|
||||||
id: this.id,
|
id: this.id,
|
||||||
network: this.network.name,
|
network: this.network.name,
|
||||||
neededCosigners: this.neededCosigners,
|
requiredCosigners: this.neededCosigners,
|
||||||
totalCosigners: this.totalCosigners,
|
totalCosigners: this.totalCosigners,
|
||||||
cosignersExtPubKeys: this.cosignersWallets.map( function (b) {
|
cosignersExtPubKeys: this.cosignersWallets.map( function (b) {
|
||||||
return b.getExtendedPubKey();
|
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?
|
// should receive an array also?
|
||||||
Wallet.prototype.addCosignerExtendedPubKey = function (newEpk) {
|
Wallet.prototype.addCosignerExtendedPubKey = function (newEpk) {
|
||||||
|
|
||||||
if (this.haveAllNeededPubKeys())
|
if (this.haveAllRequiredPubKeys())
|
||||||
throw new Error('already have all needed key:' + this.totalCosigners);
|
throw new Error('already have all required key:' + this.totalCosigners);
|
||||||
|
|
||||||
if (this.getExtendedPubKey() === newEpk)
|
if (this.getExtendedPubKey() === newEpk)
|
||||||
throw new Error('already have that key (self kehy)');
|
throw new Error('already have that key (self kehy)');
|
||||||
|
|
@ -147,18 +163,47 @@ Wallet.prototype.addCosignerExtendedPubKey = function (newEpk) {
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
Wallet.prototype.haveAllNeededPubKeys = function () {
|
Wallet.prototype.getPubKey = function (index,isChange) {
|
||||||
return this.registeredCosigners() === this.totalCosigners;
|
|
||||||
|
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.
|
var pubkey = [];
|
||||||
if (! this.haveAllNeededPubKeys() )
|
var l = this.cosignersWallets.length;
|
||||||
throw new Error('cosigners pub key missing');
|
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
|
// Input: Bitcore's Transaction, sign with ownPK
|
||||||
// return partially signed or fully signed tx
|
// return partially signed or fully signed tx
|
||||||
|
|
|
||||||
|
|
@ -6,6 +6,7 @@ var bitcore = bitcore || require('../node_modules/bitcore');
|
||||||
|
|
||||||
var cosign = cosign || {};
|
var cosign = cosign || {};
|
||||||
|
|
||||||
|
var Address = bitcore.Address;
|
||||||
var fakeStorage = require('./FakeStorage');
|
var fakeStorage = require('./FakeStorage');
|
||||||
var Wallet = cosign.Wallet || require('soop').load('../js/models/Wallet', {Storage: fakeStorage});
|
var Wallet = cosign.Wallet || require('soop').load('../js/models/Wallet', {Storage: fakeStorage});
|
||||||
|
|
||||||
|
|
@ -21,7 +22,7 @@ var createW = function () {
|
||||||
var cosigners = [];
|
var cosigners = [];
|
||||||
for(var i=0; i<4; i++) {
|
for(var i=0; i<4; i++) {
|
||||||
var c = new Wallet(config);
|
var c = new Wallet(config);
|
||||||
w.haveAllNeededPubKeys().should.equal(false);
|
w.haveAllRequiredPubKeys().should.equal(false);
|
||||||
|
|
||||||
w.addCosignerExtendedPubKey(c.getExtendedPubKey());
|
w.addCosignerExtendedPubKey(c.getExtendedPubKey());
|
||||||
cosigners.push(c);
|
cosigners.push(c);
|
||||||
|
|
@ -71,9 +72,9 @@ describe('Wallet model', function() {
|
||||||
should.exist(w2);
|
should.exist(w2);
|
||||||
|
|
||||||
w2.registeredCosigners().should.equal(1);
|
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 () {
|
it('should add and check when adding shared pub keys', function () {
|
||||||
|
|
@ -81,7 +82,7 @@ describe('Wallet model', function() {
|
||||||
var w = k.w;
|
var w = k.w;
|
||||||
var cosigners = k.cosigners;
|
var cosigners = k.cosigners;
|
||||||
|
|
||||||
w.haveAllNeededPubKeys().should.equal(true);
|
w.haveAllRequiredPubKeys().should.equal(true);
|
||||||
w.addCosignerExtendedPubKey.bind(w.getExtendedPubKey()).should.throw();
|
w.addCosignerExtendedPubKey.bind(w.getExtendedPubKey()).should.throw();
|
||||||
w.addCosignerExtendedPubKey.bind(cosigners[0].getExtendedPubKey()).should.throw();
|
w.addCosignerExtendedPubKey.bind(cosigners[0].getExtendedPubKey()).should.throw();
|
||||||
w.addCosignerExtendedPubKey.bind((new Wallet(config)).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();
|
w.store.bind().should.throw();
|
||||||
|
|
||||||
var w2 = Wallet.read(ID);
|
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(w.getExtendedPubKey()).should.throw();
|
||||||
w2.addCosignerExtendedPubKey.bind(cosigners[0].getExtendedPubKey()).should.throw();
|
w2.addCosignerExtendedPubKey.bind(cosigners[0].getExtendedPubKey()).should.throw();
|
||||||
w2.addCosignerExtendedPubKey.bind((new Wallet(config)).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');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue