copay/js/models/core/PublicKeyRing.js

366 lines
9.7 KiB
JavaScript
Raw Normal View History

2014-03-26 11:26:31 -07:00
'use strict';
var imports = require('soop').imports();
var bitcore = require('bitcore');
2014-05-14 16:55:34 -07:00
var HK = bitcore.HierarchicalKey;
var Address = bitcore.Address;
var Script = bitcore.Script;
var coinUtil = bitcore.util;
var Transaction = bitcore.Transaction
2014-04-18 14:25:51 -07:00
var util = bitcore.util;
2014-03-26 11:26:31 -07:00
2014-04-14 11:31:10 -07:00
var Storage = imports.Storage || require('../storage/Base.js');
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-15 08:17:28 -07:00
this.walletId = opts.walletId;
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-05-14 16:55:34 -07:00
this.copayersHK = opts.copayersHK || [];
2014-03-26 18:00:42 -07:00
this.changeAddressIndex= opts.changeAddressIndex || 0;
this.addressIndex= opts.addressIndex || 0;
this.publicKeysCache = opts.publicKeysCache || {};
2014-05-01 05:41:18 -07:00
this.nicknameFor = opts.nicknameFor || {};
this.copayerIds = [];
2014-03-26 11:26:31 -07:00
}
2014-04-09 13:28:35 -07:00
/*
* Based on https://github.com/maraoz/bips/blob/master/bip-NNNN.mediawiki
* m / purpose' / cosigner_index / change / address_index
2014-04-09 13:28:35 -07:00
*/
var PURPOSE = 45;
var MAX_NON_HARDENED = 0x8000000 - 1;
var SHARED_INDEX = MAX_NON_HARDENED - 0;
var ID_INDEX = MAX_NON_HARDENED - 1;
var BIP45_PUBLIC_PREFIX = 'm/'+ PURPOSE+'\'';
PublicKeyRing.BIP45_PUBLIC_PREFIX = BIP45_PUBLIC_PREFIX;
2014-04-09 13:28:35 -07:00
PublicKeyRing.Branch = function(address_index, isChange, cosigner_index) {
var ret = 'm/'+
(typeof cosigner_index !== 'undefined'? cosigner_index: SHARED_INDEX)+'/'+
(isChange?1:0)+'/'+
address_index;
return ret;
2014-04-09 13:28:35 -07:00
};
2014-03-26 11:26:31 -07:00
PublicKeyRing.FullBranch = function(address_index, isChange, cosigner_index) {
var sub = PublicKeyRing.Branch(address_index, isChange, cosigner_index);
sub = sub.substring(2);
return BIP45_PUBLIC_PREFIX + '/' + sub;
};
PublicKeyRing.IdFullBranch = function() {
return PublicKeyRing.FullBranch(0, 0, ID_INDEX);
};
2014-04-23 18:43:17 -07:00
PublicKeyRing.fromObj = function (data) {
if (data instanceof PublicKeyRing) {
throw new Error('bad data format: Did you use .toObj()?');
}
2014-05-01 05:41:18 -07:00
var ret = new PublicKeyRing(data);
for (var k in data.copayersExtPubKeys) {
ret.addCopayer(data.copayersExtPubKeys[k]);
}
return ret;
2014-03-26 13:55:02 -07:00
};
2014-04-07 07:46:45 -07:00
PublicKeyRing.prototype.toObj = function() {
2014-04-04 16:16:15 -07:00
return {
2014-04-15 08:17:28 -07:00
walletId: this.walletId,
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-05-14 16:55:34 -07:00
copayersExtPubKeys: this.copayersHK.map( function (b) {
return b.extendedPublicKeyString();
2014-03-26 13:55:02 -07:00
}),
2014-05-01 05:41:18 -07:00
nicknameFor: this.nicknameFor,
publicKeysCache: this.publicKeysCache
2014-04-04 16:16:15 -07:00
};
};
2014-04-23 18:43:17 -07:00
PublicKeyRing.prototype.getCopayerId = function(i) {
return this.copayerIds[i];
2014-04-20 08:41:28 -07:00
};
2014-04-18 14:25:51 -07:00
2014-04-04 11:57:28 -07:00
PublicKeyRing.prototype.registeredCopayers = function () {
2014-05-14 16:55:34 -07:00
return this.copayersHK.length;
2014-03-26 11:26:31 -07:00
};
2014-04-09 10:30:12 -07:00
PublicKeyRing.prototype.isComplete = function () {
2014-04-24 19:13:55 -07:00
return this.registeredCopayers() === this.totalCopayers;
};
PublicKeyRing.prototype.getAllCopayerIds = function() {
2014-05-01 05:41:18 -07:00
return this.copayerIds;
2014-04-24 19:13:55 -07:00
};
PublicKeyRing.prototype.myCopayerId = function(i) {
return this.getCopayerId(0);
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');
};
PublicKeyRing.prototype._newExtendedPublicKey = function () {
2014-05-14 16:55:34 -07:00
return new HK(this.network.name)
.extendedPublicKeyString();
};
2014-05-01 05:41:18 -07:00
PublicKeyRing.prototype._updateBip = function (index) {
var hk = this.copayersHK[index].derive(PublicKeyRing.Branch(0, 0, ID_INDEX));
2014-05-14 16:55:34 -07:00
this.copayerIds[index]= hk.eckey.public.toString('hex');
2014-05-01 05:41:18 -07:00
};
PublicKeyRing.prototype._setNicknameForIndex = function (index, nickname) {
this.nicknameFor[this.copayerIds[index]] = nickname;
};
2014-03-26 11:26:31 -07:00
PublicKeyRing.prototype.nicknameForIndex = function(index) {
2014-05-01 05:41:18 -07:00
return this.nicknameFor[this.copayerIds[index]];
};
PublicKeyRing.prototype.nicknameForCopayer = function(copayerId) {
2014-05-01 05:41:18 -07:00
return this.nicknameFor[copayerId];
};
PublicKeyRing.prototype.addCopayer = function(newEpk, nickname) {
2014-04-09 10:30:12 -07:00
if (this.isComplete())
throw new Error('PKR already has all required key:' + this.totalCopayers);
2014-03-26 11:26:31 -07:00
2014-05-14 16:55:34 -07:00
this.copayersHK.forEach(function(b){
if (b.extendedPublicKeyString() === newEpk)
throw new Error('PKR already has that key');
2014-03-26 11:26:31 -07:00
});
var i = this.copayersHK.length;
2014-05-14 16:55:34 -07:00
var bip = new HK(newEpk);
this.copayersHK.push(bip);
2014-05-01 05:41:18 -07:00
this._updateBip(i);
if (nickname) {
this._setNicknameForIndex(i, nickname);
2014-05-01 05:41:18 -07:00
}
return newEpk;
2014-03-26 11:26:31 -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
var path = PublicKeyRing.Branch(index, isChange);
var pubKeys = this.publicKeysCache[path];
if (!pubKeys) {
pubKeys = [];
2014-05-14 16:55:34 -07:00
var l = this.copayersHK.length;
for(var i=0; i<l; i++) {
2014-05-14 16:55:34 -07:00
var hk = this.copayersHK[i].derive(path);
pubKeys[i] = hk.eckey.public;
}
2014-04-22 18:07:18 -07:00
this.publicKeysCache[path] = pubKeys.map(function(pk){return pk.toString('hex');});
2014-05-01 05:41:18 -07:00
}
else {
pubKeys = pubKeys.map(function(s){return new Buffer(s,'hex');});
2014-03-26 18:00:42 -07:00
}
2014-03-26 19:55:46 -07:00
return pubKeys;
};
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);
throw new Error('index out of bound');
}
};
2014-04-22 18:07:18 -07:00
// TODO this could be cached
PublicKeyRing.prototype.getRedeemScript = function (index, isChange) {
this._checkIndexRange(index, isChange);
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);
return script;
};
2014-04-22 18:07:18 -07:00
// TODO this could be cached
PublicKeyRing.prototype.getAddress = function (index, isChange) {
var script = this.getRedeemScript(index,isChange);
2014-04-17 11:04:39 -07:00
return Address.fromScript(script, this.network.name);
2014-03-26 11:26:31 -07:00
};
2014-04-22 18:07:18 -07:00
// TODO this could be cached
2014-04-10 21:09:42 -07:00
PublicKeyRing.prototype.getScriptPubKeyHex = function (index, isChange) {
var addr = this.getAddress(index,isChange);
return Script.createP2SH(addr.payload()).getBuffer().toString('hex');
};
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);
2014-04-17 11:04:39 -07:00
if (isChange) {
2014-03-26 18:00:42 -07:00
this.changeAddressIndex++;
2014-04-17 11:04:39 -07:00
} else {
2014-04-04 15:59:00 -07:00
this.addressIndex++;
2014-04-17 11:04:39 -07:00
}
2014-03-26 18:00:42 -07:00
return ret;
};
2014-05-01 06:07:30 -07:00
PublicKeyRing.prototype.getAddresses = function(opts) {
return this.getAddressesInfo(opts).map(function(info) {
2014-04-30 15:50:13 -07:00
return info.address;
});
};
2014-05-01 06:07:30 -07:00
PublicKeyRing.prototype.getAddressesInfo = function(opts) {
opts = opts || {};
2014-04-30 15:50:13 -07:00
var ret = [];
2014-05-01 06:07:30 -07:00
if (!opts.excludeChange) {
for (var i=0; i<this.changeAddressIndex; i++) {
2014-04-30 15:50:13 -07:00
ret.unshift({
address: this.getAddress(i,true),
isChange: true
});
}
}
2014-04-30 15:50:13 -07:00
2014-05-01 06:07:30 -07:00
if (!opts.excludeMain) {
for (var i=0; i<this.addressIndex; i++) {
ret.unshift({
address: this.getAddress(i,false),
isChange: false
});
}
2014-04-30 15:50:13 -07:00
}
return ret;
};
2014-04-30 08:25:33 -07:00
// TODO this could be cached
PublicKeyRing.prototype._addScriptMap = function (map, index, isChange) {
var script = this.getRedeemScript(index,isChange);
map[Address.fromScript(script, this.network.name).toString()] = script.getBuffer().toString('hex');
};
2014-04-09 13:28:35 -07:00
PublicKeyRing.prototype.getRedeemScriptMap = function () {
var ret = {};
for (var i=0; i<this.changeAddressIndex; i++) {
2014-04-22 18:07:18 -07:00
this._addScriptMap(ret,i,true);
2014-04-09 13:28:35 -07:00
}
for (var i=0; i<this.addressIndex; i++) {
2014-04-22 18:07:18 -07:00
this._addScriptMap(ret,i,false);
2014-04-09 13:28:35 -07:00
}
return ret;
};
2014-04-07 20:54:38 -07:00
PublicKeyRing.prototype._checkInPRK = function(inPKR, ignoreId) {
2014-04-15 08:17:28 -07:00
if (!ignoreId && this.walletId !== inPKR.walletId) {
throw new Error('inPRK walletId mismatch');
2014-04-07 20:54:38 -07:00
}
2014-04-07 07:46:45 -07:00
if (this.network.name !== inPKR.network.name)
2014-04-07 07:46:45 -07:00
throw new Error('inPRK network mismatch');
if (
this.requiredCopayers && inPKR.requiredCopayers &&
(this.requiredCopayers !== inPKR.requiredCopayers))
2014-04-18 14:25:51 -07:00
throw new Error('inPRK requiredCopayers mismatch '+this.requiredCopayers+'!='+inPKR.requiredCopayers);
2014-04-07 07:46:45 -07:00
if (
this.totalCopayers && inPKR.totalCopayers &&
(this.totalCopayers !== inPKR.totalCopayers))
2014-04-18 14:25:51 -07:00
throw new Error('inPRK totalCopayers mismatch'+this.totalCopayers+'!='+inPKR.requiredCopayers);
2014-04-07 07:46:45 -07:00
};
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 self = this;
var hasChanged = false;
2014-05-14 16:55:34 -07:00
var l= self.copayersHK.length;
2014-04-22 22:01:54 -07:00
if (self.isComplete())
return;
2014-04-07 07:46:45 -07:00
2014-05-14 16:55:34 -07:00
inPKR.copayersHK.forEach( function(b) {
2014-04-07 07:46:45 -07:00
var haveIt = false;
var epk = b.extendedPublicKeyString();
2014-04-07 07:46:45 -07:00
for(var j=0; j<l; j++) {
2014-05-14 16:55:34 -07:00
if (self.copayersHK[j].extendedPublicKeyString() === epk) {
2014-04-07 07:46:45 -07:00
haveIt=true;
break;
}
}
if (!haveIt) {
if (self.isComplete()) {
throw new Error('trying to add more pubkeys, when PKR isComplete at merge');
}
2014-05-14 16:55:34 -07:00
var l2 = self.copayersHK.length;
self.copayersHK.push(new HK(epk));
2014-05-01 05:41:18 -07:00
self._updateBip(l2);
if (inPKR.nicknameFor[self.getCopayerId(l2)])
self._setNicknameForIndex(l2,inPKR.nicknameFor[self.getCopayerId(l2)]);
2014-04-07 07:46:45 -07:00
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);