copay/js/models/core/PublicKeyRing.js

352 lines
9.6 KiB
JavaScript
Raw Normal View History

2014-03-26 11:26:31 -07:00
'use strict';
2014-06-24 08:57:15 -07:00
var imports = require('soop').imports();
var preconditions = require('preconditions').instance();
2014-06-24 08:57:15 -07:00
var bitcore = require('bitcore');
var HK = bitcore.HierarchicalKey;
var PrivateKey = require('./PrivateKey');
var Structure = require('./Structure');
var AddressIndex = require('./AddressIndex');
var Address = bitcore.Address;
var Script = bitcore.Script;
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-06-24 08:57:15 -07:00
this.network = opts.networkName === 'livenet' ?
bitcore.networks.livenet : bitcore.networks.testnet;
2014-03-26 11:26:31 -07:00
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
2014-07-01 08:49:50 -07:00
this.indexes = opts.indexes ? AddressIndex.fromList(opts.indexes)
: AddressIndex.init(this.totalCopayers);
this.publicKeysCache = opts.publicKeysCache || {};
2014-05-01 05:41:18 -07:00
this.nicknameFor = opts.nicknameFor || {};
this.copayerIds = [];
2014-05-30 11:07:52 -07:00
this.addressToPath = {};
2014-03-26 11:26:31 -07:00
}
2014-06-24 08:57:15 -07:00
PublicKeyRing.fromObj = function(data) {
if (data instanceof PublicKeyRing) {
throw new Error('bad data format: Did you use .toObj()?');
}
// Support old indexes schema
if (!Array.isArray(data.indexes)) {
data.indexes.cosigner = Structure.SHARED_INDEX;
data.indexes = [data.indexes];
}
2014-06-24 08:57:15 -07:00
var ret = new PublicKeyRing(data);
2014-05-01 05:41:18 -07:00
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,
indexes: this.getIndexesObj(),
2014-04-07 07:46:45 -07:00
2014-06-24 08:57:15 -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
};
};
PublicKeyRing.prototype.getIndexesObj = function(i) {
return this.indexes.map(function(i) { return i.toObj(); });
}
2014-04-23 18:43:17 -07:00
PublicKeyRing.prototype.getCopayerId = function(i) {
preconditions.checkArgument(typeof i !== 'undefined');
2014-04-23 18:43:17 -07:00
return this.copayerIds[i];
2014-04-20 08:41:28 -07:00
};
2014-04-18 14:25:51 -07:00
2014-06-24 08:57:15 -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-06-24 08:57:15 -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-06-24 08:57:15 -07:00
throw new Error('dont have required keys yet');
2014-03-26 18:00:42 -07:00
};
2014-06-24 08:57:15 -07:00
PublicKeyRing.prototype._newExtendedPublicKey = function() {
return new PrivateKey({
networkName: this.network.name
})
2014-05-29 12:17:17 -07:00
.deriveBIP45Branch()
.extendedPublicKeyString();
};
2014-06-24 08:57:15 -07:00
PublicKeyRing.prototype._updateBip = function(index) {
2014-05-29 13:18:55 -07:00
var hk = this.copayersHK[index].derive(Structure.IdBranch);
2014-06-24 08:57:15 -07:00
this.copayerIds[index] = hk.eckey.public.toString('hex');
2014-05-01 05:41:18 -07:00
};
2014-06-24 08:57:15 -07:00
PublicKeyRing.prototype._setNicknameForIndex = function(index, nickname) {
2014-05-01 05:41:18 -07:00
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) {
return this.nicknameFor[copayerId] || 'NN';
2014-05-01 05:41:18 -07:00
};
PublicKeyRing.prototype.addCopayer = function(newEpk, nickname) {
2014-04-09 10:30:12 -07:00
if (this.isComplete())
2014-06-24 08:57:15 -07:00
throw new Error('PKR already has all required key:' + this.totalCopayers);
2014-03-26 11:26:31 -07:00
2014-06-24 08:57:15 -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
});
2014-05-29 10:02:52 -07:00
if (!newEpk) {
newEpk = this._newExtendedPublicKey();
}
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);
2014-06-24 08:57:15 -07:00
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
2014-06-24 08:57:15 -07:00
var path = Structure.Branch(index, isChange);
var pubKeys = this.publicKeysCache[path];
if (!pubKeys) {
pubKeys = [];
2014-05-14 16:55:34 -07:00
var l = this.copayersHK.length;
2014-06-24 08:57:15 -07:00
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-06-24 08:57:15 -07:00
this.publicKeysCache[path] = pubKeys.map(function(pk) {
return pk.toString('hex');
});
} else {
pubKeys = pubKeys.map(function(s) {
return new Buffer(s, 'hex');
});
2014-03-26 18:00:42 -07:00
}
2014-06-12 10:27:53 -07:00
2014-03-26 19:55:46 -07:00
return pubKeys;
};
2014-04-22 18:07:18 -07:00
// TODO this could be cached
2014-06-24 08:57:15 -07:00
PublicKeyRing.prototype.getRedeemScript = function(index, isChange) {
2014-04-09 10:30:12 -07:00
var pubKeys = this.getPubKeys(index, isChange);
2014-06-24 08:57:15 -07:00
var script = Script.createMultisig(this.requiredCopayers, pubKeys);
return script;
};
2014-04-22 18:07:18 -07:00
// TODO this could be cached
2014-06-24 08:57:15 -07:00
PublicKeyRing.prototype.getAddress = function(index, isChange) {
var script = this.getRedeemScript(index, isChange);
2014-05-30 11:07:52 -07:00
var address = Address.fromScript(script, this.network.name);
this.addressToPath[address.toString()] = Structure.FullBranch(index, isChange);
return address;
};
PublicKeyRing.prototype.getSharedIndex = function() {
return this.getIndex(Structure.SHARED_INDEX);
};
PublicKeyRing.prototype.getIndex = function(cosigner) {
var index = this.indexes.filter(function(i) { return i.cosigner == cosigner });
if (index.length != 1) throw new Error('no index for cosigner');
return index[0];
};
2014-05-30 11:07:52 -07:00
PublicKeyRing.prototype.pathForAddress = function(address) {
var path = this.addressToPath[address];
2014-06-24 08:57:15 -07:00
if (!path) throw new Error('Couldn\'t find path for address ' + address);
2014-05-30 11:07:52 -07:00
return path;
2014-03-26 11:26:31 -07:00
};
2014-04-22 18:07:18 -07:00
// TODO this could be cached
2014-06-24 08:57:15 -07:00
PublicKeyRing.prototype.getScriptPubKeyHex = function(index, isChange) {
var addr = this.getAddress(index, isChange);
2014-04-10 21:09:42 -07:00
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-06-24 10:40:03 -07:00
isChange = !!isChange;
var shared = this.getIndex(Structure.SHARED_INDEX);
var index = isChange ? shared.getChangeIndex() : shared.getReceiveIndex();
2014-06-04 08:28:26 -07:00
var ret = this.getAddress(index, isChange);
shared.increment(isChange);
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 || {};
var shared = this.getIndex(Structure.SHARED_INDEX);
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 < shared.getChangeIndex(); i++) {
2014-06-24 08:57:15 -07:00
var a = this.getAddress(i, true);
2014-04-30 15:50:13 -07:00
ret.unshift({
2014-06-24 08:57:15 -07:00
address: this.getAddress(i, true),
addressStr: a.toString(),
2014-04-30 15:50:13 -07:00
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 < shared.getReceiveIndex(); i++) {
2014-06-24 08:57:15 -07:00
var a = this.getAddress(i, false);
2014-05-01 06:07:30 -07:00
ret.unshift({
2014-06-24 08:57:15 -07:00
address: a,
addressStr: a.toString(),
2014-05-01 06:07:30 -07:00
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
2014-06-24 09:17:22 -07:00
PublicKeyRing.prototype._addScriptMap = function(map, path) {
var p = Structure.indicesForPath(path);
var script = this.getRedeemScript(p.index, p.isChange);
2014-04-30 08:25:33 -07:00
map[Address.fromScript(script, this.network.name).toString()] = script.getBuffer().toString('hex');
};
2014-06-24 09:17:22 -07:00
PublicKeyRing.prototype.getRedeemScriptMap = function(paths) {
2014-04-09 13:28:35 -07:00
var ret = {};
2014-06-24 09:17:22 -07:00
for (var i = 0; i < paths.length; i++) {
var path = paths[i];
this._addScriptMap(ret, path);
2014-04-09 13:28:35 -07:00
}
return ret;
};
2014-06-09 13:28:56 -07:00
PublicKeyRing.prototype._checkInPKR = function(inPKR, ignoreId) {
2014-04-07 20:54:38 -07:00
2014-06-24 08:57:15 -07:00
if (!ignoreId && this.walletId !== inPKR.walletId) {
2014-06-09 13:28:56 -07:00
throw new Error('inPKR walletId mismatch');
2014-04-07 20:54:38 -07:00
}
2014-04-07 07:46:45 -07:00
2014-06-09 13:28:56 -07:00
if (this.network.name !== inPKR.network.name) {
2014-06-24 08:57:15 -07:00
throw new Error('Network mismatch. Should be ' + this.network.name +
' and found ' + inPKR.network.name);
2014-06-09 13:28:56 -07:00
}
2014-04-07 07:46:45 -07:00
if (
this.requiredCopayers && inPKR.requiredCopayers &&
(this.requiredCopayers !== inPKR.requiredCopayers))
2014-06-24 08:57:15 -07:00
throw new Error('inPKR requiredCopayers mismatch ' + this.requiredCopayers + '!=' + inPKR.requiredCopayers);
2014-04-07 07:46:45 -07:00
if (
this.totalCopayers && inPKR.totalCopayers &&
(this.totalCopayers !== inPKR.totalCopayers))
2014-06-24 08:57:15 -07:00
throw new Error('inPKR totalCopayers mismatch' + this.totalCopayers + '!=' + inPKR.requiredCopayers);
2014-04-07 07:46:45 -07:00
};
PublicKeyRing.prototype._mergePubkeys = function(inPKR) {
var self = this;
var hasChanged = false;
2014-06-24 08:57:15 -07:00
var l = self.copayersHK.length;
if (self.isComplete())
2014-04-22 22:01:54 -07:00
return;
2014-04-07 07:46:45 -07:00
2014-06-24 08:57:15 -07:00
inPKR.copayersHK.forEach(function(b) {
2014-04-07 07:46:45 -07:00
var haveIt = false;
2014-06-24 08:57:15 -07:00
var epk = b.extendedPublicKeyString();
for (var j = 0; j < l; j++) {
2014-05-14 16:55:34 -07:00
if (self.copayersHK[j].extendedPublicKeyString() === epk) {
2014-06-24 08:57:15 -07:00
haveIt = true;
2014-04-07 07:46:45 -07:00
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)])
2014-06-24 08:57:15 -07:00
self._setNicknameForIndex(l2, inPKR.nicknameFor[self.getCopayerId(l2)]);
hasChanged = true;
2014-04-07 07:46:45 -07:00
}
});
return hasChanged;
};
2014-04-07 20:54:38 -07:00
PublicKeyRing.prototype.merge = function(inPKR, ignoreId) {
this._checkInPKR(inPKR, ignoreId);
2014-04-07 07:46:45 -07:00
var hasChanged = false;
hasChanged |= this.mergeIndexes(inPKR.indexes);
hasChanged |= this._mergePubkeys(inPKR);
2014-04-07 07:46:45 -07:00
return !!hasChanged;
};
2014-04-07 07:46:45 -07:00
PublicKeyRing.prototype.mergeIndexes = function(indexes) {
var self = this;
var hasChanged = false;
2014-04-07 07:46:45 -07:00
indexes.forEach(function(theirs) {
var mine = self.getIndex(theirs.cosigner);
hasChanged |= mine.merge(theirs);
});
2014-04-07 07:46:45 -07:00
return !!hasChanged
}
2014-04-07 07:46:45 -07:00
2014-04-04 11:57:28 -07:00
module.exports = require('soop')(PublicKeyRing);