Merge pull request #18 from matiu/feature/PublicKeyRing
Feature/public key ring
This commit is contained in:
commit
1619fe7f07
4 changed files with 376 additions and 0 deletions
231
js/models/PublicKeyRing.js
Normal file
231
js/models/PublicKeyRing.js
Normal file
|
|
@ -0,0 +1,231 @@
|
||||||
|
|
||||||
|
'use strict';
|
||||||
|
|
||||||
|
|
||||||
|
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 Transaction = bitcore.Transaction;
|
||||||
|
var buffertools = bitcore.buffertools;
|
||||||
|
|
||||||
|
var Storage = imports.Storage || require('./Storage');
|
||||||
|
var log = imports.log || console.log;
|
||||||
|
|
||||||
|
var storage = Storage.default();
|
||||||
|
|
||||||
|
/*
|
||||||
|
* This follow Electrum convetion, as described on
|
||||||
|
* https://bitcointalk.org/index.php?topic=274182.0
|
||||||
|
*
|
||||||
|
* We should probably adapt the next standard once its ready as discussed at:
|
||||||
|
* http://sourceforge.net/p/bitcoin/mailman/message/32148600/
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
|
||||||
|
var PUBLIC_BRANCH = 'm/0/';
|
||||||
|
var CHANGE_BRANCH = 'm/1/';
|
||||||
|
|
||||||
|
function PublicKeyRing(opts) {
|
||||||
|
opts = opts || {};
|
||||||
|
|
||||||
|
this.network = opts.network === 'livenet' ?
|
||||||
|
bitcore.networks.livenet : bitcore.networks.testnet;
|
||||||
|
|
||||||
|
this.requiredCopayers = opts.requiredCopayers || 3;
|
||||||
|
this.totalCopayers = opts.totalCopayers || 5;
|
||||||
|
|
||||||
|
this.id = opts.id || PublicKeyRing.getRandomId();
|
||||||
|
|
||||||
|
this.dirty = 1;
|
||||||
|
this.copayersBIP32 = [];
|
||||||
|
|
||||||
|
this.changeAddressIndex=0;
|
||||||
|
this.addressIndex=0;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
PublicKeyRing.getRandomId = function () {
|
||||||
|
return buffertools.toHex(coinUtil.generateNonce());
|
||||||
|
};
|
||||||
|
|
||||||
|
PublicKeyRing.decrypt = function (passphrase, encPayload) {
|
||||||
|
log('[wallet.js.35] TODO READ: passphrase IGNORED');
|
||||||
|
return encPayload;
|
||||||
|
};
|
||||||
|
|
||||||
|
PublicKeyRing.encrypt = function (passphrase, payload) {
|
||||||
|
log('[wallet.js.92] TODO: passphrase IGNORED');
|
||||||
|
return payload;
|
||||||
|
};
|
||||||
|
|
||||||
|
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( PublicKeyRing.decrypt( passphrase, encPayload ));
|
||||||
|
} catch (e) {
|
||||||
|
throw new Error('error in storage: '+ e.toString());
|
||||||
|
return;
|
||||||
|
};
|
||||||
|
|
||||||
|
if (data.id !== id)
|
||||||
|
throw new Error('Wrong id in data');
|
||||||
|
|
||||||
|
var config = { network: data.network === 'livenet' ?
|
||||||
|
bitcore.networks.livenet : bitcore.networks.testnet
|
||||||
|
};
|
||||||
|
|
||||||
|
var w = new PublicKeyRing(config);
|
||||||
|
|
||||||
|
w.requiredCopayers = data.neededCopayers;
|
||||||
|
w.totalCopayers = data.totalCopayers;
|
||||||
|
|
||||||
|
// this.bip32 = ;
|
||||||
|
w.copayersBIP32 = data.copayersExtPubKeys.map( function (pk) {
|
||||||
|
return new BIP32(pk);
|
||||||
|
});
|
||||||
|
|
||||||
|
w.dirty = 0;
|
||||||
|
|
||||||
|
return w;
|
||||||
|
};
|
||||||
|
|
||||||
|
PublicKeyRing.prototype.serialize = function () {
|
||||||
|
return JSON.stringify({
|
||||||
|
id: this.id,
|
||||||
|
network: this.network.name,
|
||||||
|
requiredCopayers: this.neededCopayers,
|
||||||
|
totalCopayers: this.totalCopayers,
|
||||||
|
copayersExtPubKeys: this.copayersBIP32.map( function (b) {
|
||||||
|
return b.extendedPublicKeyString();
|
||||||
|
}),
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
PublicKeyRing.prototype.store = function (passphrase) {
|
||||||
|
|
||||||
|
if (!this.id)
|
||||||
|
throw new Error('wallet has no id');
|
||||||
|
|
||||||
|
storage.save(this.id, PublicKeyRing.encrypt(passphrase,this.serialize()));
|
||||||
|
this.dirty = 0;
|
||||||
|
|
||||||
|
return true;
|
||||||
|
};
|
||||||
|
|
||||||
|
PublicKeyRing.prototype.registeredCopayers = function () {
|
||||||
|
return this.copayersBIP32.length;
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
PublicKeyRing.prototype.haveAllRequiredPubKeys = function () {
|
||||||
|
return this.registeredCopayers() === this.totalCopayers;
|
||||||
|
};
|
||||||
|
|
||||||
|
PublicKeyRing.prototype._checkKeys = function() {
|
||||||
|
|
||||||
|
if (!this.haveAllRequiredPubKeys())
|
||||||
|
throw new Error('dont have required keys yet');
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
PublicKeyRing.prototype._newExtendedPublicKey = function () {
|
||||||
|
return new BIP32(this.network.name)
|
||||||
|
.extendedPublicKeyString();
|
||||||
|
};
|
||||||
|
|
||||||
|
PublicKeyRing.prototype.addCopayer = function (newEpk) {
|
||||||
|
|
||||||
|
if (this.haveAllRequiredPubKeys())
|
||||||
|
throw new Error('already have all required key:' + this.totalCopayers);
|
||||||
|
|
||||||
|
if (!newEpk) {
|
||||||
|
newEpk = this._newExtendedPublicKey();
|
||||||
|
}
|
||||||
|
|
||||||
|
this.copayersBIP32.forEach(function(b){
|
||||||
|
if (b.extendedPublicKeyString() === newEpk)
|
||||||
|
throw new Error('already have that key');
|
||||||
|
});
|
||||||
|
|
||||||
|
this.copayersBIP32.push(new BIP32(newEpk));
|
||||||
|
this.dirty = 1;
|
||||||
|
return newEpk;
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
PublicKeyRing.prototype.getCopayersPubKeys = function (index, isChange) {
|
||||||
|
this._checkKeys();
|
||||||
|
|
||||||
|
var pubKeys = [];
|
||||||
|
var l = this.copayersBIP32.length;
|
||||||
|
for(var i=0; i<l; i++) {
|
||||||
|
var path = (isChange ? CHANGE_BRANCH : PUBLIC_BRANCH) + index;
|
||||||
|
var bip32 = this.copayersBIP32[i].derive(path);
|
||||||
|
pubKeys[i] = bip32.eckey.public;
|
||||||
|
}
|
||||||
|
|
||||||
|
return pubKeys;
|
||||||
|
};
|
||||||
|
|
||||||
|
PublicKeyRing.prototype._checkIndexRange = function (index, isChange) {
|
||||||
|
if ( (isChange && index > this.changeAddressIndex) ||
|
||||||
|
(!isChange && index > this.addressIndex)) {
|
||||||
|
log('Out of bounds at getAddress: Index %d isChange: %d', index, isChange);
|
||||||
|
throw new Error('index out of bound');
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
PublicKeyRing.prototype.getRedeemScript = function (index, isChange) {
|
||||||
|
this._checkIndexRange(index, isChange);
|
||||||
|
|
||||||
|
var pubKeys = this.getCopayersPubKeys();
|
||||||
|
var script = Script.createMultisig(this.requiredCopayers, pubKeys);
|
||||||
|
return script;
|
||||||
|
};
|
||||||
|
|
||||||
|
PublicKeyRing.prototype.getAddress = function (index, isChange) {
|
||||||
|
this._checkIndexRange(index, isChange);
|
||||||
|
|
||||||
|
var script = this.getRedeemScript(index,isChange);
|
||||||
|
var hash = coinUtil.sha256ripe160(script.getBuffer());
|
||||||
|
var version = this.network.addressScript;
|
||||||
|
var addr = new Address(version, hash);
|
||||||
|
return addr.as('base58');
|
||||||
|
};
|
||||||
|
|
||||||
|
//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
|
||||||
|
this.changeAddressIndex++;
|
||||||
|
|
||||||
|
return ret;
|
||||||
|
|
||||||
|
};
|
||||||
|
|
||||||
|
PublicKeyRing.prototype.getAddresses = function() {
|
||||||
|
var ret = [];
|
||||||
|
|
||||||
|
for (var i=0; i<this.changeAddressIndex; i++) {
|
||||||
|
ret.push(this.getAddress(i,true));
|
||||||
|
}
|
||||||
|
|
||||||
|
for (var i=0; i<this.addressIndex; i++) {
|
||||||
|
ret.push(this.getAddress(i,false));
|
||||||
|
}
|
||||||
|
return ret;
|
||||||
|
};
|
||||||
|
|
||||||
|
module.exports = require('soop')(PublicKeyRing);
|
||||||
14
test/FakeStorage.js
Normal file
14
test/FakeStorage.js
Normal file
|
|
@ -0,0 +1,14 @@
|
||||||
|
|
||||||
|
var FakeStorage = function(){
|
||||||
|
this.storage = {};
|
||||||
|
};
|
||||||
|
|
||||||
|
FakeStorage.prototype.read = function (id) {
|
||||||
|
return this.storage[id];
|
||||||
|
};
|
||||||
|
|
||||||
|
FakeStorage.prototype.save = function(id, payload) {
|
||||||
|
this.storage[id] = payload;
|
||||||
|
}
|
||||||
|
|
||||||
|
module.exports = require('soop')(FakeStorage);
|
||||||
1
test/mocha.opts
Normal file
1
test/mocha.opts
Normal file
|
|
@ -0,0 +1 @@
|
||||||
|
-R spec
|
||||||
130
test/test.publickeyring.js
Normal file
130
test/test.publickeyring.js
Normal file
|
|
@ -0,0 +1,130 @@
|
||||||
|
'use strict';
|
||||||
|
|
||||||
|
var chai = chai || require('chai');
|
||||||
|
var should = chai.should();
|
||||||
|
var bitcore = bitcore || require('../node_modules/bitcore');
|
||||||
|
var Address = bitcore.Address;
|
||||||
|
var buffertools = bitcore.buffertools;
|
||||||
|
var copay = copay || {};
|
||||||
|
var fakeStorage = require('./FakeStorage');
|
||||||
|
var PublicKeyRing = copay.PublicKeyRing || require('soop').load('../js/models/PublicKeyRing', {Storage: fakeStorage});
|
||||||
|
|
||||||
|
var aMasterPubKey = 'tprv8ZgxMBicQKsPdSVTiWXEqCCzqRaRr9EAQdn5UVMpT9UHX67Dh1FmzEMbavPumpAicsUm2XvC6NTdcWB89yN5DUWx5HQ7z3KByUg7Ht74VRZ';
|
||||||
|
|
||||||
|
|
||||||
|
var config = {
|
||||||
|
network:'livenet',
|
||||||
|
};
|
||||||
|
|
||||||
|
var createW = function (network) {
|
||||||
|
|
||||||
|
var config = {
|
||||||
|
network: network || 'livenet',
|
||||||
|
};
|
||||||
|
|
||||||
|
var w = new PublicKeyRing(config);
|
||||||
|
should.exist(w);
|
||||||
|
|
||||||
|
var copayers = [];
|
||||||
|
for(var i=0; i<5; i++) {
|
||||||
|
w.haveAllRequiredPubKeys().should.equal(false);
|
||||||
|
var newEpk = w.addCopayer();
|
||||||
|
copayers.push(newEpk);
|
||||||
|
}
|
||||||
|
|
||||||
|
return {w:w, copayers: copayers};
|
||||||
|
};
|
||||||
|
|
||||||
|
describe('PublicKeyRing model', function() {
|
||||||
|
|
||||||
|
it('should create an instance (livenet)', function () {
|
||||||
|
var w = new PublicKeyRing({
|
||||||
|
network: config.network
|
||||||
|
});
|
||||||
|
should.exist(w);
|
||||||
|
w.network.name.should.equal('livenet');
|
||||||
|
});
|
||||||
|
it('should create an instance (testnet)', function () {
|
||||||
|
var w2 = new PublicKeyRing();
|
||||||
|
should.exist(w2);
|
||||||
|
w2.network.name.should.equal('testnet');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should fail to generate shared pub keys wo extended key', function () {
|
||||||
|
var w2 = new PublicKeyRing(config);
|
||||||
|
should.exist(w2);
|
||||||
|
|
||||||
|
w2.registeredCopayers().should.equal(0);
|
||||||
|
w2.haveAllRequiredPubKeys().should.equal(false);
|
||||||
|
|
||||||
|
w2.getAddress.bind(false).should.throw();
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should add and check when adding shared pub keys', function () {
|
||||||
|
var k = createW();
|
||||||
|
var w = k.w;
|
||||||
|
var copayers = k.copayers;
|
||||||
|
|
||||||
|
w.haveAllRequiredPubKeys().should.equal(true);
|
||||||
|
w.addCopayer.bind().should.throw();
|
||||||
|
for(var i =0; i<5; i++)
|
||||||
|
w.addCopayer.bind(copayers[i]).should.throw();
|
||||||
|
});
|
||||||
|
|
||||||
|
it('show be able to store and retrieve', function () {
|
||||||
|
var k = createW();
|
||||||
|
var w = k.w;
|
||||||
|
var copayers = k.copayers;
|
||||||
|
|
||||||
|
w.store().should.equal(true);
|
||||||
|
var ID = w.id;
|
||||||
|
delete w['id'];
|
||||||
|
w.store.bind().should.throw();
|
||||||
|
|
||||||
|
var w2 = PublicKeyRing.read(ID);
|
||||||
|
w2.haveAllRequiredPubKeys().should.equal(true);
|
||||||
|
w2.addCopayer.bind().should.throw();
|
||||||
|
for(var i =0; i<5; i++)
|
||||||
|
w2.addCopayer.bind(copayers[i]).should.throw();
|
||||||
|
|
||||||
|
});
|
||||||
|
|
||||||
|
|
||||||
|
it('should generate some p2sh addresses', 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.generateAddress(isChange);
|
||||||
|
var a = new Address(addr);
|
||||||
|
a.isValid().should.equal(true);
|
||||||
|
a.isScript().should.equal(true);
|
||||||
|
a.network().name.should.equal('livenet');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should return PublicKeyRing addresses', function () {
|
||||||
|
var k = createW();
|
||||||
|
var w = k.w;
|
||||||
|
|
||||||
|
|
||||||
|
var a = w.getAddresses();
|
||||||
|
a.length.should.equal(0);
|
||||||
|
|
||||||
|
for(var isChange=0; isChange<2; isChange++)
|
||||||
|
for(var i=0; i<6; i++)
|
||||||
|
w.generateAddress(isChange);
|
||||||
|
|
||||||
|
var as = w.getAddresses();
|
||||||
|
as.length.should.equal(12);
|
||||||
|
for(var i in as) {
|
||||||
|
var a = new Address(as[i]);
|
||||||
|
a.isValid().should.equal(true);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
});
|
||||||
|
|
||||||
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue