2014-04-04 13:16:10 -07:00
|
|
|
|
2014-03-26 11:26:31 -07:00
|
|
|
'use strict';
|
2014-04-04 13:16:10 -07:00
|
|
|
|
|
|
|
|
2014-03-26 21:18:29 -07:00
|
|
|
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;
|
2014-03-27 08:14:49 -07:00
|
|
|
var buffertools = bitcore.buffertools;
|
2014-03-26 11:26:31 -07:00
|
|
|
|
2014-03-26 21:18:29 -07:00
|
|
|
var Storage = imports.Storage || require('./Storage');
|
|
|
|
var storage = Storage.default();
|
2014-03-26 11:26:31 -07:00
|
|
|
|
|
|
|
|
2014-04-04 11:57:28 -07:00
|
|
|
function PublicKeyRing(opts) {
|
2014-03-26 11:26:31 -07:00
|
|
|
opts = opts || {};
|
|
|
|
|
2014-04-07 20:54:38 -07:00
|
|
|
this.network = opts.networkName === 'livenet' ?
|
2014-03-26 11:26:31 -07:00
|
|
|
bitcore.networks.livenet : bitcore.networks.testnet;
|
|
|
|
|
2014-04-04 11:57:28 -07:00
|
|
|
this.requiredCopayers = opts.requiredCopayers || 3;
|
|
|
|
this.totalCopayers = opts.totalCopayers || 5;
|
2014-03-26 11:26:31 -07:00
|
|
|
|
2014-04-04 11:57:28 -07:00
|
|
|
this.id = opts.id || PublicKeyRing.getRandomId();
|
2014-03-26 13:55:02 -07:00
|
|
|
|
2014-04-04 13:16:10 -07:00
|
|
|
this.copayersBIP32 = [];
|
2014-03-26 18:00:42 -07:00
|
|
|
|
2014-03-26 19:55:46 -07:00
|
|
|
this.changeAddressIndex=0;
|
|
|
|
this.addressIndex=0;
|
2014-03-26 11:26:31 -07:00
|
|
|
}
|
|
|
|
|
2014-04-09 13:28:35 -07:00
|
|
|
/*
|
|
|
|
* This follow Electrum convetion, as described in
|
|
|
|
* https://bitcointalk.org/index.php?topic=274182.0
|
|
|
|
*
|
|
|
|
* We should probably adopt the next standard once it's ready, as discussed in:
|
|
|
|
* http://sourceforge.net/p/bitcoin/mailman/message/32148600/
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
PublicKeyRing.PublicBranch = function (index) {
|
|
|
|
return 'm/0/'+index;
|
|
|
|
};
|
|
|
|
|
|
|
|
PublicKeyRing.ChangeBranch = function (index) {
|
|
|
|
return 'm/1/'+index;
|
|
|
|
};
|
2014-03-26 11:26:31 -07:00
|
|
|
|
2014-04-04 11:57:28 -07:00
|
|
|
PublicKeyRing.getRandomId = function () {
|
2014-03-27 08:14:49 -07:00
|
|
|
return buffertools.toHex(coinUtil.generateNonce());
|
2014-03-26 13:55:02 -07:00
|
|
|
};
|
2014-03-26 11:26:31 -07:00
|
|
|
|
2014-04-04 11:57:28 -07:00
|
|
|
PublicKeyRing.decrypt = function (passphrase, encPayload) {
|
2014-04-07 11:31:41 -07:00
|
|
|
console.log('[wallet.js.35] TODO READ: passphrase IGNORED');
|
2014-03-26 13:55:02 -07:00
|
|
|
return encPayload;
|
|
|
|
};
|
|
|
|
|
2014-04-04 11:57:28 -07:00
|
|
|
PublicKeyRing.encrypt = function (passphrase, payload) {
|
2014-04-07 11:31:41 -07:00
|
|
|
console.log('[wallet.js.92] TODO: passphrase IGNORED');
|
2014-03-26 13:55:02 -07:00
|
|
|
return payload;
|
|
|
|
};
|
|
|
|
|
2014-04-08 22:20:28 -07:00
|
|
|
PublicKeyRing.fromObj = function (data) {
|
|
|
|
var config = { networkName: data.networkName || 'livenet' };
|
2014-03-26 13:55:02 -07:00
|
|
|
|
2014-04-04 11:57:28 -07:00
|
|
|
var w = new PublicKeyRing(config);
|
2014-03-26 13:55:02 -07:00
|
|
|
|
2014-04-07 07:46:45 -07:00
|
|
|
w.requiredCopayers = data.requiredCopayers;
|
2014-04-04 11:57:28 -07:00
|
|
|
w.totalCopayers = data.totalCopayers;
|
2014-04-04 16:16:15 -07:00
|
|
|
w.addressIndex = data.addressIndex;
|
|
|
|
w.changeAddressIndex = data.changeAddressIndex;
|
2014-04-04 13:16:10 -07:00
|
|
|
|
|
|
|
// this.bip32 = ;
|
|
|
|
w.copayersBIP32 = data.copayersExtPubKeys.map( function (pk) {
|
|
|
|
return new BIP32(pk);
|
2014-03-26 13:55:02 -07:00
|
|
|
});
|
|
|
|
|
|
|
|
return w;
|
|
|
|
};
|
|
|
|
|
2014-04-09 19:04:22 -07:00
|
|
|
PublicKeyRing.read = function (encPayload, id, passphrase) {
|
2014-04-08 22:20:28 -07:00
|
|
|
if (!encPayload)
|
|
|
|
throw new Error('Could not find wallet data');
|
|
|
|
var data;
|
|
|
|
try {
|
|
|
|
data = JSON.parse( PublicKeyRing.decrypt( passphrase, encPayload ));
|
|
|
|
} catch (e) {
|
2014-04-09 19:04:22 -07:00
|
|
|
throw new Error('error in read: '+ e.toString());
|
|
|
|
}
|
2014-04-08 22:20:28 -07:00
|
|
|
|
|
|
|
if (data.id !== id)
|
|
|
|
throw new Error('Wrong id in data');
|
|
|
|
return PublicKeyRing.fromObj(data);
|
|
|
|
};
|
|
|
|
|
2014-04-07 07:46:45 -07:00
|
|
|
PublicKeyRing.prototype.toObj = function() {
|
2014-04-04 16:16:15 -07:00
|
|
|
return {
|
2014-03-26 13:55:02 -07:00
|
|
|
id: this.id,
|
2014-04-07 07:46:45 -07:00
|
|
|
networkName: this.network.name,
|
|
|
|
requiredCopayers: this.requiredCopayers,
|
2014-04-04 11:57:28 -07:00
|
|
|
totalCopayers: this.totalCopayers,
|
2014-04-07 07:46:45 -07:00
|
|
|
|
2014-04-04 16:16:15 -07:00
|
|
|
changeAddressIndex: this.changeAddressIndex,
|
|
|
|
addressIndex: this.addressIndex,
|
2014-04-04 13:16:10 -07:00
|
|
|
copayersExtPubKeys: this.copayersBIP32.map( function (b) {
|
|
|
|
return b.extendedPublicKeyString();
|
2014-03-26 13:55:02 -07:00
|
|
|
}),
|
2014-04-07 07:46:45 -07:00
|
|
|
ts: parseInt(Date.now() / 1000),
|
2014-04-04 16:16:15 -07:00
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
PublicKeyRing.prototype.serialize = function () {
|
2014-04-07 07:46:45 -07:00
|
|
|
return JSON.stringify(this.toObj());
|
2014-03-26 13:55:02 -07:00
|
|
|
};
|
|
|
|
|
2014-04-04 13:16:10 -07:00
|
|
|
|
2014-04-09 19:04:22 -07:00
|
|
|
PublicKeyRing.prototype.toStore = function (passphrase) {
|
2014-03-26 13:55:02 -07:00
|
|
|
if (!this.id)
|
|
|
|
throw new Error('wallet has no id');
|
|
|
|
|
2014-04-09 19:04:22 -07:00
|
|
|
return PublicKeyRing.encrypt(passphrase,this.serialize());
|
2014-03-26 11:26:31 -07:00
|
|
|
};
|
|
|
|
|
2014-04-04 11:57:28 -07:00
|
|
|
PublicKeyRing.prototype.registeredCopayers = function () {
|
2014-04-04 13:16:10 -07:00
|
|
|
return this.copayersBIP32.length;
|
2014-03-26 11:26:31 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
2014-04-09 10:30:12 -07:00
|
|
|
PublicKeyRing.prototype.isComplete = function () {
|
2014-04-07 20:54:38 -07:00
|
|
|
return this.registeredCopayers() >= this.totalCopayers;
|
2014-03-26 18:00:42 -07:00
|
|
|
};
|
|
|
|
|
2014-04-04 11:57:28 -07:00
|
|
|
PublicKeyRing.prototype._checkKeys = function() {
|
2014-03-26 18:00:42 -07:00
|
|
|
|
2014-04-09 10:30:12 -07:00
|
|
|
if (!this.isComplete())
|
2014-03-26 18:00:42 -07:00
|
|
|
throw new Error('dont have required keys yet');
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2014-04-04 13:16:10 -07:00
|
|
|
PublicKeyRing.prototype._newExtendedPublicKey = function () {
|
|
|
|
return new BIP32(this.network.name)
|
|
|
|
.extendedPublicKeyString();
|
|
|
|
};
|
|
|
|
|
|
|
|
PublicKeyRing.prototype.addCopayer = function (newEpk) {
|
2014-03-26 11:26:31 -07:00
|
|
|
|
2014-04-09 10:30:12 -07:00
|
|
|
if (this.isComplete())
|
2014-04-04 11:57:28 -07:00
|
|
|
throw new Error('already have all required key:' + this.totalCopayers);
|
2014-03-26 11:26:31 -07:00
|
|
|
|
2014-04-04 13:16:10 -07:00
|
|
|
if (!newEpk) {
|
|
|
|
newEpk = this._newExtendedPublicKey();
|
|
|
|
}
|
2014-03-26 11:26:31 -07:00
|
|
|
|
2014-04-04 13:16:10 -07:00
|
|
|
this.copayersBIP32.forEach(function(b){
|
|
|
|
if (b.extendedPublicKeyString() === newEpk)
|
2014-03-26 11:26:31 -07:00
|
|
|
throw new Error('already have that key');
|
|
|
|
});
|
|
|
|
|
2014-04-04 13:16:10 -07:00
|
|
|
this.copayersBIP32.push(new BIP32(newEpk));
|
|
|
|
return newEpk;
|
2014-03-26 11:26:31 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2014-04-09 10:30:12 -07:00
|
|
|
PublicKeyRing.prototype.getPubKeys = function (index, isChange) {
|
2014-03-26 18:00:42 -07:00
|
|
|
this._checkKeys();
|
2014-03-26 11:26:31 -07:00
|
|
|
|
2014-03-26 19:55:46 -07:00
|
|
|
var pubKeys = [];
|
2014-04-04 13:16:10 -07:00
|
|
|
var l = this.copayersBIP32.length;
|
2014-03-26 18:00:42 -07:00
|
|
|
for(var i=0; i<l; i++) {
|
2014-04-09 13:28:35 -07:00
|
|
|
var path = isChange ? PublicKeyRing.ChangeBranch(index) : PublicKeyRing.PublicBranch(index);
|
2014-04-04 13:16:10 -07:00
|
|
|
var bip32 = this.copayersBIP32[i].derive(path);
|
|
|
|
pubKeys[i] = bip32.eckey.public;
|
2014-03-26 18:00:42 -07:00
|
|
|
}
|
|
|
|
|
2014-03-26 19:55:46 -07:00
|
|
|
return pubKeys;
|
|
|
|
};
|
|
|
|
|
2014-04-04 13:16:10 -07:00
|
|
|
PublicKeyRing.prototype._checkIndexRange = function (index, isChange) {
|
|
|
|
if ( (isChange && index > this.changeAddressIndex) ||
|
|
|
|
(!isChange && index > this.addressIndex)) {
|
2014-04-07 11:31:41 -07:00
|
|
|
console.log('Out of bounds at getAddress: Index %d isChange: %d', index, isChange);
|
2014-03-26 21:18:29 -07:00
|
|
|
throw new Error('index out of bound');
|
|
|
|
}
|
2014-04-04 13:16:10 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
PublicKeyRing.prototype.getRedeemScript = function (index, isChange) {
|
|
|
|
this._checkIndexRange(index, isChange);
|
2014-03-26 21:18:29 -07:00
|
|
|
|
2014-04-09 10:30:12 -07:00
|
|
|
var pubKeys = this.getPubKeys(index, isChange);
|
2014-04-04 11:57:28 -07:00
|
|
|
var script = Script.createMultisig(this.requiredCopayers, pubKeys);
|
2014-04-04 13:16:10 -07:00
|
|
|
return script;
|
|
|
|
};
|
|
|
|
|
2014-04-09 13:28:35 -07:00
|
|
|
|
2014-04-04 13:16:10 -07:00
|
|
|
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;
|
2014-03-26 18:00:42 -07:00
|
|
|
var addr = new Address(version, hash);
|
2014-04-04 13:16:10 -07:00
|
|
|
return addr.as('base58');
|
2014-03-26 11:26:31 -07:00
|
|
|
};
|
|
|
|
|
2014-04-04 11:57:28 -07:00
|
|
|
//generate a new address, update index.
|
|
|
|
PublicKeyRing.prototype.generateAddress = function(isChange) {
|
2014-03-26 18:00:42 -07:00
|
|
|
|
|
|
|
var ret =
|
|
|
|
this.getAddress(isChange ? this.changeAddressIndex : this.addressIndex, isChange);
|
|
|
|
if (isChange)
|
|
|
|
this.changeAddressIndex++;
|
2014-04-04 15:59:00 -07:00
|
|
|
else
|
|
|
|
this.addressIndex++;
|
2014-03-26 18:00:42 -07:00
|
|
|
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
};
|
2014-03-26 21:18:29 -07:00
|
|
|
|
2014-04-04 11:57:28 -07:00
|
|
|
PublicKeyRing.prototype.getAddresses = function() {
|
2014-03-26 21:18:29 -07:00
|
|
|
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;
|
|
|
|
};
|
|
|
|
|
2014-04-09 13:28:35 -07:00
|
|
|
PublicKeyRing.prototype.getRedeemScriptMap = function () {
|
|
|
|
var ret = {};
|
|
|
|
|
|
|
|
for (var i=0; i<this.changeAddressIndex; i++) {
|
|
|
|
ret[this.getAddress(i,true)] = this.getRedeemScript(i,true);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (var i=0; i<this.addressIndex; i++) {
|
|
|
|
ret[this.getAddress(i)] = this.getRedeemScript(i);
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
2014-04-07 20:54:38 -07:00
|
|
|
PublicKeyRing.prototype._checkInPRK = function(inPKR, ignoreId) {
|
|
|
|
|
|
|
|
|
|
|
|
if (!inPKR.ts) {
|
|
|
|
throw new Error('inPRK bad format: Did you use .toObj()?');
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!ignoreId && this.id !== inPKR.id) {
|
2014-04-07 07:46:45 -07:00
|
|
|
throw new Error('inPRK id mismatch');
|
2014-04-07 20:54:38 -07:00
|
|
|
}
|
2014-04-07 07:46:45 -07:00
|
|
|
|
|
|
|
if (this.network.name !== inPKR.networkName)
|
|
|
|
throw new Error('inPRK network mismatch');
|
|
|
|
|
|
|
|
if (
|
|
|
|
this.requiredCopayers && inPKR.requiredCopayers &&
|
|
|
|
(this.requiredCopayers !== inPKR.requiredCopayers))
|
|
|
|
throw new Error('inPRK requiredCopayers mismatch');
|
|
|
|
|
|
|
|
if (
|
|
|
|
this.totalCopayers && inPKR.totalCopayers &&
|
|
|
|
(this.totalCopayers !== inPKR.totalCopayers))
|
|
|
|
throw new Error('inPRK requiredCopayers mismatch');
|
|
|
|
|
|
|
|
if (! inPKR.ts)
|
|
|
|
throw new Error('no ts at inPRK');
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
PublicKeyRing.prototype._mergeIndexes = function(inPKR) {
|
|
|
|
var hasChanged = false;
|
|
|
|
|
|
|
|
// Indexes
|
|
|
|
if (inPKR.changeAddressIndex > this.changeAddressIndex) {
|
|
|
|
this.changeAddressIndex = inPKR.changeAddressIndex;
|
|
|
|
hasChanged = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (inPKR.addressIndex > this.addressIndex) {
|
|
|
|
this.addressIndex = inPKR.addressIndex;
|
|
|
|
hasChanged = true;
|
|
|
|
}
|
|
|
|
return hasChanged;
|
|
|
|
};
|
|
|
|
|
|
|
|
PublicKeyRing.prototype._mergePubkeys = function(inPKR) {
|
|
|
|
var hasChanged = false;
|
|
|
|
var l= this.copayersBIP32.length;
|
|
|
|
|
|
|
|
var self = this;
|
|
|
|
|
|
|
|
inPKR.copayersExtPubKeys.forEach( function(epk) {
|
|
|
|
var haveIt = false;
|
|
|
|
for(var j=0; j<l; j++) {
|
|
|
|
if (self.copayersBIP32[j].extendedPublicKeyString() === epk) {
|
|
|
|
haveIt=true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!haveIt) {
|
|
|
|
self.copayersBIP32.push(new BIP32(epk));
|
|
|
|
hasChanged=true;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
return hasChanged;
|
|
|
|
};
|
|
|
|
|
2014-04-07 20:54:38 -07:00
|
|
|
PublicKeyRing.prototype.merge = function(inPKR, ignoreId) {
|
2014-04-07 07:46:45 -07:00
|
|
|
var hasChanged = false;
|
|
|
|
|
2014-04-07 20:54:38 -07:00
|
|
|
this._checkInPRK(inPKR, ignoreId);
|
2014-04-07 07:46:45 -07:00
|
|
|
|
|
|
|
if (this._mergeIndexes(inPKR))
|
|
|
|
hasChanged = true;
|
|
|
|
|
|
|
|
if (this._mergePubkeys(inPKR))
|
|
|
|
hasChanged = true;
|
|
|
|
|
|
|
|
return hasChanged;
|
|
|
|
};
|
|
|
|
|
2014-04-04 11:57:28 -07:00
|
|
|
module.exports = require('soop')(PublicKeyRing);
|