mirror of https://github.com/BTCPrivate/copay.git
change names to HD and fix HD* tests
This commit is contained in:
parent
1a40e29fd2
commit
0ee93403f3
4
copay.js
4
copay.js
|
@ -3,8 +3,8 @@ module.exports.PublicKeyRing = require('./js/models/core/PublicKeyRing');
|
|||
module.exports.TxProposals = require('./js/models/core/TxProposals');
|
||||
module.exports.PrivateKey = require('./js/models/core/PrivateKey');
|
||||
module.exports.Passphrase = require('./js/models/core/Passphrase');
|
||||
module.exports.Structure = require('./js/models/core/Structure');
|
||||
module.exports.AddressIndex = require('./js/models/core/AddressIndex');
|
||||
module.exports.HDPath = require('./js/models/core/HDPath');
|
||||
module.exports.HDParams = require('./js/models/core/HDParams');
|
||||
|
||||
|
||||
// components
|
||||
|
|
|
@ -1,98 +0,0 @@
|
|||
'use strict';
|
||||
|
||||
var preconditions = require('preconditions').singleton();
|
||||
var Structure = require('./Structure');
|
||||
|
||||
function AddressIndex(opts) {
|
||||
opts = opts || {};
|
||||
this.cosigner = opts.cosigner
|
||||
this.changeIndex = opts.changeIndex || 0;
|
||||
this.receiveIndex = opts.receiveIndex || 0;
|
||||
|
||||
if (typeof this.cosigner === 'undefined') {
|
||||
this.cosigner = Structure.SHARED_INDEX;
|
||||
}
|
||||
}
|
||||
|
||||
AddressIndex.init = function(totalCopayers) {
|
||||
preconditions.shouldBeNumber(totalCopayers);
|
||||
var indexes = [new AddressIndex()];
|
||||
for (var i = 0 ; i < totalCopayers ; i++) {
|
||||
indexes.push(new AddressIndex({cosigner: i}));
|
||||
}
|
||||
return indexes;
|
||||
}
|
||||
|
||||
AddressIndex.fromList = function(indexes) {
|
||||
return indexes.map(function(i) { return AddressIndex.fromObj(i); });
|
||||
}
|
||||
|
||||
AddressIndex.fromObj = function(data) {
|
||||
if (data instanceof AddressIndex) {
|
||||
throw new Error('bad data format: Did you use .toObj()?');
|
||||
}
|
||||
return new AddressIndex(data);
|
||||
};
|
||||
|
||||
AddressIndex.serialize = function(indexes) {
|
||||
return indexes.map(function(i) { return i.toObj(); });
|
||||
}
|
||||
|
||||
AddressIndex.update = function(shared, totalCopayers) {
|
||||
var indexes = this.init(totalCopayers);
|
||||
indexes[0].changeIndex = shared.changeIndex;
|
||||
indexes[0].receiveIndex = shared.receiveIndex;
|
||||
return this.serialize(indexes);
|
||||
};
|
||||
|
||||
AddressIndex.prototype.toObj = function() {
|
||||
return {
|
||||
cosigner: this.cosigner,
|
||||
changeIndex: this.changeIndex,
|
||||
receiveIndex: this.receiveIndex
|
||||
};
|
||||
};
|
||||
|
||||
AddressIndex.prototype.checkRange = function(index, isChange) {
|
||||
if ((isChange && index > this.changeIndex) ||
|
||||
(!isChange && index > this.receiveIndex)) {
|
||||
throw new Error('Out of bounds at index ' + index + ' isChange: ' + isChange);
|
||||
}
|
||||
};
|
||||
|
||||
AddressIndex.prototype.getChangeIndex = function() {
|
||||
return this.changeIndex;
|
||||
};
|
||||
|
||||
AddressIndex.prototype.getReceiveIndex = function() {
|
||||
return this.receiveIndex;
|
||||
};
|
||||
|
||||
AddressIndex.prototype.increment = function(isChange) {
|
||||
if (isChange) {
|
||||
this.changeIndex++;
|
||||
} else {
|
||||
this.receiveIndex++;
|
||||
}
|
||||
};
|
||||
|
||||
AddressIndex.prototype.merge = function(inAddressIndex) {
|
||||
preconditions.shouldBeObject(inAddressIndex)
|
||||
.checkArgument(this.cosigner == inAddressIndex.cosigner);
|
||||
|
||||
var hasChanged = false;
|
||||
|
||||
// Indexes
|
||||
if (inAddressIndex.changeIndex > this.changeIndex) {
|
||||
this.changeIndex = inAddressIndex.changeIndex;
|
||||
hasChanged = true;
|
||||
}
|
||||
|
||||
if (inAddressIndex.receiveIndex > this.receiveIndex) {
|
||||
this.receiveIndex = inAddressIndex.receiveIndex;
|
||||
hasChanged = true;
|
||||
}
|
||||
return hasChanged;
|
||||
};
|
||||
|
||||
module.exports = AddressIndex;
|
|
@ -0,0 +1,100 @@
|
|||
'use strict';
|
||||
|
||||
var preconditions = require('preconditions').singleton();
|
||||
var HDPath = require('./HDPath');
|
||||
|
||||
function HDParams(opts) {
|
||||
opts = opts || {};
|
||||
|
||||
//opts.cosigner is for backwards compatibility only
|
||||
this.copayerIndex = typeof opts.copayerIndex === 'undefined' ? opts.cosigner : opts.copayerIndex;
|
||||
this.changeIndex = opts.changeIndex || 0;
|
||||
this.receiveIndex = opts.receiveIndex || 0;
|
||||
|
||||
if (typeof this.copayerIndex === 'undefined') {
|
||||
this.copayerIndex = HDPath.SHARED_INDEX;
|
||||
}
|
||||
}
|
||||
|
||||
HDParams.init = function(totalCopayers) {
|
||||
preconditions.shouldBeNumber(totalCopayers);
|
||||
var indexes = [new HDParams()];
|
||||
for (var i = 0 ; i < totalCopayers ; i++) {
|
||||
indexes.push(new HDParams({copayerIndex: i}));
|
||||
}
|
||||
return indexes;
|
||||
}
|
||||
|
||||
HDParams.fromList = function(indexes) {
|
||||
return indexes.map(function(i) { return HDParams.fromObj(i); });
|
||||
}
|
||||
|
||||
HDParams.fromObj = function(data) {
|
||||
if (data instanceof HDParams) {
|
||||
throw new Error('bad data format: Did you use .toObj()?');
|
||||
}
|
||||
return new HDParams(data);
|
||||
};
|
||||
|
||||
HDParams.serialize = function(indexes) {
|
||||
return indexes.map(function(i) { return i.toObj(); });
|
||||
}
|
||||
|
||||
HDParams.update = function(shared, totalCopayers) {
|
||||
var indexes = this.init(totalCopayers);
|
||||
indexes[0].changeIndex = shared.changeIndex;
|
||||
indexes[0].receiveIndex = shared.receiveIndex;
|
||||
return this.serialize(indexes);
|
||||
};
|
||||
|
||||
HDParams.prototype.toObj = function() {
|
||||
return {
|
||||
copayerIndex: this.copayerIndex,
|
||||
changeIndex: this.changeIndex,
|
||||
receiveIndex: this.receiveIndex
|
||||
};
|
||||
};
|
||||
|
||||
HDParams.prototype.checkRange = function(index, isChange) {
|
||||
if ((isChange && index > this.changeIndex) ||
|
||||
(!isChange && index > this.receiveIndex)) {
|
||||
throw new Error('Out of bounds at index ' + index + ' isChange: ' + isChange);
|
||||
}
|
||||
};
|
||||
|
||||
HDParams.prototype.getChangeIndex = function() {
|
||||
return this.changeIndex;
|
||||
};
|
||||
|
||||
HDParams.prototype.getReceiveIndex = function() {
|
||||
return this.receiveIndex;
|
||||
};
|
||||
|
||||
HDParams.prototype.increment = function(isChange) {
|
||||
if (isChange) {
|
||||
this.changeIndex++;
|
||||
} else {
|
||||
this.receiveIndex++;
|
||||
}
|
||||
};
|
||||
|
||||
HDParams.prototype.merge = function(inHDParams) {
|
||||
preconditions.shouldBeObject(inHDParams)
|
||||
.checkArgument(this.copayerIndex == inHDParams.copayerIndex);
|
||||
|
||||
var hasChanged = false;
|
||||
|
||||
// Indexes
|
||||
if (inHDParams.changeIndex > this.changeIndex) {
|
||||
this.changeIndex = inHDParams.changeIndex;
|
||||
hasChanged = true;
|
||||
}
|
||||
|
||||
if (inHDParams.receiveIndex > this.receiveIndex) {
|
||||
this.receiveIndex = inHDParams.receiveIndex;
|
||||
hasChanged = true;
|
||||
}
|
||||
return hasChanged;
|
||||
};
|
||||
|
||||
module.exports = HDParams;
|
|
@ -2,7 +2,7 @@
|
|||
|
||||
var preconditions = require('preconditions').singleton();
|
||||
|
||||
function Structure() {}
|
||||
function HDPath() {}
|
||||
|
||||
/*
|
||||
* Based on https://github.com/maraoz/bips/blob/master/bip-NNNN.mediawiki
|
||||
|
@ -15,9 +15,9 @@ var SHARED_INDEX = MAX_NON_HARDENED - 0;
|
|||
var ID_INDEX = MAX_NON_HARDENED - 1;
|
||||
|
||||
var BIP45_PUBLIC_PREFIX = 'm/' + PURPOSE + '\'';
|
||||
Structure.BIP45_PUBLIC_PREFIX = BIP45_PUBLIC_PREFIX;
|
||||
HDPath.BIP45_PUBLIC_PREFIX = BIP45_PUBLIC_PREFIX;
|
||||
|
||||
Structure.Branch = function(addressIndex, isChange, copayerIndex) {
|
||||
HDPath.Branch = function(addressIndex, isChange, copayerIndex) {
|
||||
preconditions.shouldBeNumber(addressIndex);
|
||||
preconditions.shouldBeBoolean(isChange);
|
||||
var ret = 'm/' +
|
||||
|
@ -27,13 +27,13 @@ Structure.Branch = function(addressIndex, isChange, copayerIndex) {
|
|||
return ret;
|
||||
};
|
||||
|
||||
Structure.FullBranch = function(addressIndex, isChange, copayerIndex) {
|
||||
var sub = Structure.Branch(addressIndex, isChange, copayerIndex);
|
||||
HDPath.FullBranch = function(addressIndex, isChange, copayerIndex) {
|
||||
var sub = HDPath.Branch(addressIndex, isChange, copayerIndex);
|
||||
sub = sub.substring(2);
|
||||
return BIP45_PUBLIC_PREFIX + '/' + sub;
|
||||
};
|
||||
|
||||
Structure.indicesForPath = function(path) {
|
||||
HDPath.indicesForPath = function(path) {
|
||||
preconditions.shouldBeString(path);
|
||||
var s = path.split('/');
|
||||
return {
|
||||
|
@ -43,14 +43,14 @@ Structure.indicesForPath = function(path) {
|
|||
};
|
||||
};
|
||||
|
||||
Structure.IdFullBranch = Structure.FullBranch(0, false, ID_INDEX);
|
||||
Structure.IdBranch = Structure.Branch(0, false, ID_INDEX);
|
||||
Structure.PURPOSE = PURPOSE;
|
||||
Structure.MAX_NON_HARDENED = MAX_NON_HARDENED;
|
||||
Structure.SHARED_INDEX = SHARED_INDEX;
|
||||
Structure.ID_INDEX = ID_INDEX;
|
||||
HDPath.IdFullBranch = HDPath.FullBranch(0, false, ID_INDEX);
|
||||
HDPath.IdBranch = HDPath.Branch(0, false, ID_INDEX);
|
||||
HDPath.PURPOSE = PURPOSE;
|
||||
HDPath.MAX_NON_HARDENED = MAX_NON_HARDENED;
|
||||
HDPath.SHARED_INDEX = SHARED_INDEX;
|
||||
HDPath.ID_INDEX = ID_INDEX;
|
||||
|
||||
Structure.parseBitcoinURI = function(uri) {
|
||||
HDPath.parseBitcoinURI = function(uri) {
|
||||
var ret = {};
|
||||
var data = decodeURIComponent(uri);
|
||||
var splitDots = data.split(':');
|
||||
|
@ -72,4 +72,4 @@ Structure.parseBitcoinURI = function(uri) {
|
|||
return ret;
|
||||
};
|
||||
|
||||
module.exports = Structure;
|
||||
module.exports = HDPath;
|
|
@ -1,13 +1,12 @@
|
|||
'use strict';
|
||||
|
||||
|
||||
var imports = require('soop').imports();
|
||||
var bitcore = require('bitcore');
|
||||
var HK = bitcore.HierarchicalKey;
|
||||
var WalletKey = bitcore.WalletKey;
|
||||
var networks = bitcore.networks;
|
||||
var util = bitcore.util;
|
||||
var Structure = require('./Structure');
|
||||
var HDPath = require('./HDPath');
|
||||
|
||||
function PrivateKey(opts) {
|
||||
opts = opts || {};
|
||||
|
@ -41,7 +40,7 @@ PrivateKey.prototype.getIdKey = function() {
|
|||
};
|
||||
|
||||
PrivateKey.prototype.cacheId = function() {
|
||||
var path = Structure.IdFullBranch;
|
||||
var path = HDPath.IdFullBranch;
|
||||
var idhk = this.bip.derive(path);
|
||||
this.idkey = idhk.eckey;
|
||||
this.id = idhk.eckey.public.toString('hex');
|
||||
|
@ -50,7 +49,7 @@ PrivateKey.prototype.cacheId = function() {
|
|||
|
||||
PrivateKey.prototype.deriveBIP45Branch = function() {
|
||||
if (!this.bip45Branch) {
|
||||
this.bip45Branch = this.bip.derive(Structure.BIP45_PUBLIC_PREFIX);
|
||||
this.bip45Branch = this.bip.derive(HDPath.BIP45_PUBLIC_PREFIX);
|
||||
}
|
||||
return this.bip45Branch;
|
||||
}
|
||||
|
@ -103,7 +102,7 @@ PrivateKey.prototype.getForPath = function(path) {
|
|||
};
|
||||
|
||||
PrivateKey.prototype.get = function(index, isChange, cosigner) {
|
||||
var path = Structure.FullBranch(index, isChange, cosigner);
|
||||
var path = HDPath.FullBranch(index, isChange, cosigner);
|
||||
return this.getForPath(path);
|
||||
};
|
||||
|
||||
|
@ -123,4 +122,4 @@ PrivateKey.prototype.getAll = function(receiveIndex, changeIndex, cosigner) {
|
|||
|
||||
|
||||
|
||||
module.exports = require('soop')(PrivateKey);
|
||||
module.exports = PrivateKey;
|
||||
|
|
|
@ -1,13 +1,12 @@
|
|||
'use strict';
|
||||
|
||||
|
||||
var imports = require('soop').imports();
|
||||
var preconditions = require('preconditions').instance();
|
||||
var bitcore = require('bitcore');
|
||||
var HK = bitcore.HierarchicalKey;
|
||||
var PrivateKey = require('./PrivateKey');
|
||||
var Structure = require('./Structure');
|
||||
var AddressIndex = require('./AddressIndex');
|
||||
var HDPath = require('./HDPath');
|
||||
var HDParams = require('./HDParams');
|
||||
var Address = bitcore.Address;
|
||||
var Script = bitcore.Script;
|
||||
|
||||
|
@ -24,14 +23,14 @@ function PublicKeyRing(opts) {
|
|||
|
||||
this.copayersHK = opts.copayersHK || [];
|
||||
|
||||
this.indexes = opts.indexes ? AddressIndex.fromList(opts.indexes)
|
||||
: AddressIndex.init(this.totalCopayers);
|
||||
this.indexes = opts.indexes ? HDParams.fromList(opts.indexes)
|
||||
: HDParams.init(this.totalCopayers);
|
||||
|
||||
this.publicKeysCache = opts.publicKeysCache || {};
|
||||
this.nicknameFor = opts.nicknameFor || {};
|
||||
this.copayerIds = [];
|
||||
this.copayersBackup = opts.copayersBackup || [];
|
||||
this.addressToPath = {};
|
||||
this.publicKeysCache = opts.publicKeysCache || {};
|
||||
this.nicknameFor = opts.nicknameFor || {};
|
||||
this.copayerIds = [];
|
||||
this.copayersBackup = opts.copayersBackup || [];
|
||||
this.addressToPath = {};
|
||||
}
|
||||
|
||||
PublicKeyRing.fromObj = function(data) {
|
||||
|
@ -41,7 +40,7 @@ PublicKeyRing.fromObj = function(data) {
|
|||
|
||||
// Support old indexes schema
|
||||
if (!Array.isArray(data.indexes)) {
|
||||
data.indexes = AddressIndex.update(data.indexes, data.totalCopayers);
|
||||
data.indexes = HDParams.update(data.indexes, data.totalCopayers);
|
||||
}
|
||||
|
||||
var ret = new PublicKeyRing(data);
|
||||
|
@ -59,7 +58,7 @@ PublicKeyRing.prototype.toObj = function() {
|
|||
networkName: this.network.name,
|
||||
requiredCopayers: this.requiredCopayers,
|
||||
totalCopayers: this.totalCopayers,
|
||||
indexes: AddressIndex.serialize(this.indexes),
|
||||
indexes: HDParams.serialize(this.indexes),
|
||||
copayersBackup: this.copayersBackup,
|
||||
|
||||
copayersExtPubKeys: this.copayersHK.map(function(b) {
|
||||
|
@ -103,14 +102,14 @@ PublicKeyRing.prototype._checkKeys = function() {
|
|||
|
||||
PublicKeyRing.prototype._newExtendedPublicKey = function() {
|
||||
return new PrivateKey({
|
||||
networkName: this.network.name
|
||||
})
|
||||
.deriveBIP45Branch()
|
||||
.extendedPublicKeyString();
|
||||
networkName: this.network.name
|
||||
})
|
||||
.deriveBIP45Branch()
|
||||
.extendedPublicKeyString();
|
||||
};
|
||||
|
||||
PublicKeyRing.prototype._updateBip = function(index) {
|
||||
var hk = this.copayersHK[index].derive(Structure.IdBranch);
|
||||
var hk = this.copayersHK[index].derive(HDPath.IdBranch);
|
||||
this.copayerIds[index] = hk.eckey.public.toString('hex');
|
||||
};
|
||||
|
||||
|
@ -149,10 +148,10 @@ PublicKeyRing.prototype.addCopayer = function(newEpk, nickname) {
|
|||
return newEpk;
|
||||
};
|
||||
|
||||
PublicKeyRing.prototype.getPubKeys = function(index, isChange, cosigner) {
|
||||
PublicKeyRing.prototype.getPubKeys = function(index, isChange, copayerIndex) {
|
||||
this._checkKeys();
|
||||
|
||||
var path = Structure.Branch(index, isChange, cosigner);
|
||||
var path = HDPath.Branch(index, isChange, copayerIndex);
|
||||
var pubKeys = this.publicKeysCache[path];
|
||||
if (!pubKeys) {
|
||||
pubKeys = [];
|
||||
|
@ -175,27 +174,28 @@ PublicKeyRing.prototype.getPubKeys = function(index, isChange, cosigner) {
|
|||
};
|
||||
|
||||
// TODO this could be cached
|
||||
PublicKeyRing.prototype.getRedeemScript = function(index, isChange, cosigner) {
|
||||
var pubKeys = this.getPubKeys(index, isChange, cosigner);
|
||||
PublicKeyRing.prototype.getRedeemScript = function(index, isChange, copayerIndex) {
|
||||
var pubKeys = this.getPubKeys(index, isChange, copayerIndex);
|
||||
var script = Script.createMultisig(this.requiredCopayers, pubKeys);
|
||||
return script;
|
||||
};
|
||||
|
||||
// TODO this could be cached
|
||||
PublicKeyRing.prototype.getAddress = function(index, isChange, id) {
|
||||
var cosigner = this.getCosigner(id);
|
||||
var script = this.getRedeemScript(index, isChange, cosigner);
|
||||
var copayerIndex = this.getCosigner(id);
|
||||
var script = this.getRedeemScript(index, isChange, copayerIndex);
|
||||
var address = Address.fromScript(script, this.network.name);
|
||||
this.addressToPath[address.toString()] = Structure.FullBranch(index, isChange, cosigner);
|
||||
this.addressToPath[address.toString()] = HDPath.FullBranch(index, isChange, copayerIndex);
|
||||
return address;
|
||||
};
|
||||
|
||||
// Overloaded to receive a PubkeyString or a consigner index
|
||||
PublicKeyRing.prototype.getIndex = function(id) {
|
||||
var cosigner = this.getCosigner(id);
|
||||
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];
|
||||
var copayerIndex = this.getCosigner(id);
|
||||
var index = this.indexes.filter(function(i) { return i.copayerIndex == copayerIndex });
|
||||
if (index.length != 1) throw new Error('no index for copayerIndex');
|
||||
|
||||
return index[0];
|
||||
};
|
||||
|
||||
PublicKeyRing.prototype.pathForAddress = function(address) {
|
||||
|
@ -206,8 +206,8 @@ PublicKeyRing.prototype.pathForAddress = function(address) {
|
|||
|
||||
// TODO this could be cached
|
||||
PublicKeyRing.prototype.getScriptPubKeyHex = function(index, isChange, pubkey) {
|
||||
var cosigner = this.getCosigner(pubkey);
|
||||
var addr = this.getAddress(index, isChange, cosigner);
|
||||
var copayerIndex = this.getCosigner(pubkey);
|
||||
var addr = this.getAddress(index, isChange, copayerIndex);
|
||||
return Script.createP2SH(addr.payload()).getBuffer().toString('hex');
|
||||
};
|
||||
|
||||
|
@ -216,7 +216,7 @@ PublicKeyRing.prototype.generateAddress = function(isChange, pubkey) {
|
|||
isChange = !!isChange;
|
||||
var addrIndex = this.getIndex(pubkey);
|
||||
var index = isChange ? addrIndex.getChangeIndex() : addrIndex.getReceiveIndex();
|
||||
var ret = this.getAddress(index, isChange, addrIndex.cosigner);
|
||||
var ret = this.getAddress(index, isChange, addrIndex.copayerIndex);
|
||||
addrIndex.increment(isChange);
|
||||
return ret;
|
||||
};
|
||||
|
@ -228,7 +228,7 @@ PublicKeyRing.prototype.getAddresses = function(opts) {
|
|||
};
|
||||
|
||||
PublicKeyRing.prototype.getCosigner = function(pubKey) {
|
||||
if (typeof pubKey == 'undefined') return Structure.SHARED_INDEX;
|
||||
if (typeof pubKey == 'undefined') return HDPath.SHARED_INDEX;
|
||||
if (typeof pubKey == 'number') return pubKey;
|
||||
|
||||
var sorted = this.copayersHK.map(function(h, i){
|
||||
|
@ -245,51 +245,51 @@ PublicKeyRing.prototype.getCosigner = function(pubKey) {
|
|||
PublicKeyRing.prototype.getAddressesInfo = function(opts, pubkey) {
|
||||
var ret = [];
|
||||
var self = this;
|
||||
var cosigner = pubkey && this.getCosigner(pubkey);
|
||||
var copayerIndex = pubkey && this.getCosigner(pubkey);
|
||||
this.indexes.forEach(function(index) {
|
||||
ret = ret.concat(self.getAddressesInfoForIndex(index, opts, cosigner));
|
||||
ret = ret.concat(self.getAddressesInfoForIndex(index, opts, copayerIndex));
|
||||
});
|
||||
return ret;
|
||||
}
|
||||
|
||||
PublicKeyRing.prototype.getAddressesInfoForIndex = function(index, opts, cosigner) {
|
||||
PublicKeyRing.prototype.getAddressesInfoForIndex = function(index, opts, copayerIndex) {
|
||||
opts = opts || {};
|
||||
|
||||
var isOwned = index.cosigner == Structure.SHARED_INDEX
|
||||
|| index.cosigner == cosigner;
|
||||
var isOwned = index.copayerIndex == HDPath.SHARED_INDEX
|
||||
|| index.copayerIndex == copayerIndex;
|
||||
|
||||
var ret = [];
|
||||
if (!opts.excludeChange) {
|
||||
for (var i = 0; i < index.changeIndex; i++) {
|
||||
var a = this.getAddress(i, true, index.cosigner);
|
||||
ret.unshift({
|
||||
address: a,
|
||||
addressStr: a.toString(),
|
||||
isChange: true,
|
||||
owned: isOwned
|
||||
});
|
||||
var ret = [];
|
||||
if (!opts.excludeChange) {
|
||||
for (var i = 0; i < index.changeIndex; i++) {
|
||||
var a = this.getAddress(i, true, index.copayerIndex);
|
||||
ret.unshift({
|
||||
address: a,
|
||||
addressStr: a.toString(),
|
||||
isChange: true,
|
||||
owned: isOwned
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (!opts.excludeMain) {
|
||||
for (var i = 0; i < index.receiveIndex; i++) {
|
||||
var a = this.getAddress(i, false, index.cosigner);
|
||||
ret.unshift({
|
||||
address: a,
|
||||
addressStr: a.toString(),
|
||||
isChange: false,
|
||||
owned: isOwned
|
||||
});
|
||||
if (!opts.excludeMain) {
|
||||
for (var i = 0; i < index.receiveIndex; i++) {
|
||||
var a = this.getAddress(i, false, index.copayerIndex);
|
||||
ret.unshift({
|
||||
address: a,
|
||||
addressStr: a.toString(),
|
||||
isChange: false,
|
||||
owned: isOwned
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return ret;
|
||||
return ret;
|
||||
};
|
||||
|
||||
// TODO this could be cached
|
||||
PublicKeyRing.prototype._addScriptMap = function(map, path) {
|
||||
var p = Structure.indicesForPath(path);
|
||||
var script = this.getRedeemScript(p.index, p.isChange, p.cosigner);
|
||||
var p = HDPath.indicesForPath(path);
|
||||
var script = this.getRedeemScript(p.index, p.isChange, p.copayerIndex);
|
||||
map[Address.fromScript(script, this.network.name).toString()] = script.getBuffer().toString('hex');
|
||||
};
|
||||
|
||||
|
@ -310,18 +310,18 @@ PublicKeyRing.prototype._checkInPKR = function(inPKR, ignoreId) {
|
|||
|
||||
if (this.network.name !== inPKR.network.name) {
|
||||
throw new Error('Network mismatch. Should be ' + this.network.name +
|
||||
' and found ' + inPKR.network.name);
|
||||
' and found ' + inPKR.network.name);
|
||||
}
|
||||
|
||||
if (
|
||||
this.requiredCopayers && inPKR.requiredCopayers &&
|
||||
(this.requiredCopayers !== inPKR.requiredCopayers))
|
||||
throw new Error('inPKR requiredCopayers mismatch ' + this.requiredCopayers + '!=' + inPKR.requiredCopayers);
|
||||
(this.requiredCopayers !== inPKR.requiredCopayers))
|
||||
throw new Error('inPKR requiredCopayers mismatch ' + this.requiredCopayers + '!=' + inPKR.requiredCopayers);
|
||||
|
||||
if (
|
||||
this.totalCopayers && inPKR.totalCopayers &&
|
||||
(this.totalCopayers !== inPKR.totalCopayers))
|
||||
throw new Error('inPKR totalCopayers mismatch' + this.totalCopayers + '!=' + inPKR.requiredCopayers);
|
||||
if (
|
||||
this.totalCopayers && inPKR.totalCopayers &&
|
||||
(this.totalCopayers !== inPKR.totalCopayers))
|
||||
throw new Error('inPKR totalCopayers mismatch' + this.totalCopayers + '!=' + inPKR.requiredCopayers);
|
||||
};
|
||||
|
||||
|
||||
|
@ -393,7 +393,7 @@ PublicKeyRing.prototype.mergeIndexes = function(indexes) {
|
|||
var hasChanged = false;
|
||||
|
||||
indexes.forEach(function(theirs) {
|
||||
var mine = self.getIndex(theirs.cosigner);
|
||||
var mine = self.getIndex(theirs.copayerIndex);
|
||||
hasChanged |= mine.merge(theirs);
|
||||
});
|
||||
|
||||
|
@ -414,4 +414,4 @@ PublicKeyRing.prototype.mergeBackups = function(backups) {
|
|||
}
|
||||
|
||||
|
||||
module.exports = require('soop')(PublicKeyRing);
|
||||
module.exports = PublicKeyRing;
|
||||
|
|
|
@ -15,7 +15,7 @@ var SecureRandom = bitcore.SecureRandom;
|
|||
var Base58Check = bitcore.Base58.base58Check;
|
||||
var Address = bitcore.Address;
|
||||
|
||||
var AddressIndex = require('./AddressIndex');
|
||||
var HDParams = require('./HDParams');
|
||||
var PublicKeyRing = require('./PublicKeyRing');
|
||||
var TxProposals = require('./TxProposals');
|
||||
var PrivateKey = require('./PrivateKey');
|
||||
|
@ -93,7 +93,7 @@ Wallet.prototype.connectToAll = function() {
|
|||
|
||||
Wallet.prototype._handleIndexes = function(senderId, data, isInbound) {
|
||||
this.log('RECV INDEXES:', data);
|
||||
var inIndexes = AddressIndex.fromList(data.indexes);
|
||||
var inIndexes = HDParams.fromList(data.indexes);
|
||||
var hasChanged = this.publicKeyRing.mergeIndexes(inIndexes);
|
||||
if (hasChanged) {
|
||||
this.emit('publicKeyRingUpdated');
|
||||
|
@ -132,8 +132,10 @@ Wallet.prototype._handlePublicKeyRing = function(senderId, data, isInbound) {
|
|||
|
||||
Wallet.prototype._handleTxProposal = function(senderId, data) {
|
||||
this.log('RECV TXPROPOSAL: ', data);
|
||||
|
||||
var inTxp = TxProposals.TxProposal.fromObj(data.txProposal, Wallet.builderOpts);
|
||||
|
||||
|
||||
|
||||
var valid = inTxp.isValid();
|
||||
if (!valid) {
|
||||
var corruptEvent = {
|
||||
|
@ -458,7 +460,7 @@ Wallet.prototype.sendPublicKeyRing = function(recipients) {
|
|||
});
|
||||
};
|
||||
Wallet.prototype.sendIndexes = function(recipients) {
|
||||
var indexes = AddressIndex.serialize(this.publicKeyRing.indexes);
|
||||
var indexes = HDParams.serialize(this.publicKeyRing.indexes);
|
||||
this.log('### INDEXES TO:', recipients || 'All', indexes);
|
||||
|
||||
this.send(recipients, {
|
||||
|
|
|
@ -11,8 +11,8 @@ try {
|
|||
var copay = require('../copay'); //node
|
||||
}
|
||||
var PublicKeyRing = copay.PublicKeyRing;
|
||||
var AddressIndex = copay.AddressIndex;
|
||||
var Structure = copay.Structure;
|
||||
var HDParams = copay.HDParams;
|
||||
var HDPath = copay.HDPath;
|
||||
|
||||
|
||||
var config = {
|
||||
|
@ -20,42 +20,42 @@ var config = {
|
|||
};
|
||||
|
||||
var createAI = function() {
|
||||
var i = new AddressIndex();
|
||||
var i = new HDParams();
|
||||
should.exist(i);
|
||||
|
||||
i.cosigner = 1;
|
||||
i.copayerIndex = 1;
|
||||
|
||||
return i;
|
||||
};
|
||||
|
||||
describe('AddressIndex model', function() {
|
||||
describe('HDParams model', function() {
|
||||
|
||||
it('should create an instance (livenet)', function() {
|
||||
var i = new AddressIndex();
|
||||
var i = new HDParams();
|
||||
should.exist(i);
|
||||
});
|
||||
|
||||
it('should init indexes', function() {
|
||||
var is = AddressIndex.init(2);
|
||||
var is = HDParams.init(2);
|
||||
should.exist(is);
|
||||
is.length.should.equal(3);
|
||||
|
||||
var cosigners = is.map(function(i) { return i.cosigner; });
|
||||
cosigners.indexOf(Structure.SHARED_INDEX).should.not.equal(-1);
|
||||
var cosigners = is.map(function(i) { return i.copayerIndex; });
|
||||
cosigners.indexOf(HDPath.SHARED_INDEX).should.not.equal(-1);
|
||||
cosigners.indexOf(0).should.not.equal(-1);
|
||||
cosigners.indexOf(1).should.not.equal(-1);
|
||||
cosigners.indexOf(2).should.equal(-1);
|
||||
});
|
||||
|
||||
it('should serialize to object list and back', function() {
|
||||
var is = AddressIndex.init(3);
|
||||
var is = HDParams.init(3);
|
||||
should.exist(is);
|
||||
is.length.should.equal(4);
|
||||
|
||||
var list = AddressIndex.serialize(is);
|
||||
var list = HDParams.serialize(is);
|
||||
list.length.should.equal(4);
|
||||
|
||||
var is2 = AddressIndex.fromList(list);
|
||||
var is2 = HDParams.fromList(list);
|
||||
is2.length.should.equal(4);
|
||||
});
|
||||
|
||||
|
@ -73,8 +73,8 @@ describe('AddressIndex model', function() {
|
|||
var data = i.toObj();
|
||||
should.exist(data);
|
||||
|
||||
var i2 = AddressIndex.fromObj(data);
|
||||
i2.cosigner.should.equal(i.cosigner);
|
||||
var i2 = HDParams.fromObj(data);
|
||||
i2.copayerIndex.should.equal(i.copayerIndex);
|
||||
|
||||
i2.getChangeIndex().should.equal(changeN);
|
||||
i2.getReceiveIndex().should.equal(addressN);
|
||||
|
@ -83,9 +83,9 @@ describe('AddressIndex model', function() {
|
|||
it('should count generation indexes', function() {
|
||||
var j = createAI();
|
||||
for (var i = 0; i < 3; i++)
|
||||
j.increment(true);
|
||||
j.increment(true);
|
||||
for (var i = 0; i < 2; i++)
|
||||
j.increment(false);
|
||||
j.increment(false);
|
||||
|
||||
j.changeIndex.should.equal(3);
|
||||
j.receiveIndex.should.equal(2);
|
||||
|
@ -95,11 +95,11 @@ describe('AddressIndex model', function() {
|
|||
var j = createAI();
|
||||
|
||||
for (var i = 0; i < 15; i++)
|
||||
j.increment(true);
|
||||
j.increment(true);
|
||||
for (var i = 0; i < 7; i++)
|
||||
j.increment(false);
|
||||
var j2 = new AddressIndex({
|
||||
cosigner: j.cosigner,
|
||||
j.increment(false);
|
||||
var j2 = new HDParams({
|
||||
copayerIndex: j.copayerIndex,
|
||||
});
|
||||
j2.merge(j).should.equal(true);
|
||||
j2.changeIndex.should.equal(15);
|
||||
|
@ -108,9 +108,9 @@ describe('AddressIndex model', function() {
|
|||
j2.merge(j).should.equal(false);
|
||||
});
|
||||
|
||||
it('#merge should fail with different cosigner index', function() {
|
||||
var j1 = new AddressIndex({ walletId: '1234', cosigner: 2 });
|
||||
var j2 = new AddressIndex({ walletId: '1234', cosigner: 3 });
|
||||
it('#merge should fail with different copayerIndex index', function() {
|
||||
var j1 = new HDParams({ walletId: '1234', copayerIndex: 2 });
|
||||
var j2 = new HDParams({ walletId: '1234', copayerIndex: 3 });
|
||||
|
||||
var merge = function() { j2.merge(j1); };
|
||||
merge.should.throw(Error);
|
|
@ -8,77 +8,77 @@ try {
|
|||
} catch (e) {
|
||||
var copay = require('../copay'); //node
|
||||
}
|
||||
var Structure = require('../js/models/core/Structure');
|
||||
var HDPath = require('../js/models/core/HDPath');
|
||||
|
||||
describe('Structure model', function() {
|
||||
describe('HDPath model', function() {
|
||||
it('should have the correct constants', function() {
|
||||
Structure.MAX_NON_HARDENED.should.equal(Math.pow(2, 31) - 1);
|
||||
Structure.SHARED_INDEX.should.equal(Structure.MAX_NON_HARDENED);
|
||||
Structure.ID_INDEX.should.equal(Structure.SHARED_INDEX - 1);
|
||||
Structure.IdFullBranch.should.equal('m/45\'/2147483646/0/0');
|
||||
HDPath.MAX_NON_HARDENED.should.equal(Math.pow(2, 31) - 1);
|
||||
HDPath.SHARED_INDEX.should.equal(HDPath.MAX_NON_HARDENED);
|
||||
HDPath.ID_INDEX.should.equal(HDPath.SHARED_INDEX - 1);
|
||||
HDPath.IdFullBranch.should.equal('m/45\'/2147483646/0/0');
|
||||
});
|
||||
|
||||
it('should get the correct branches', function() {
|
||||
// shared branch (no cosigner index specified)
|
||||
Structure.FullBranch(0, false).should.equal('m/45\'/2147483647/0/0');
|
||||
HDPath.FullBranch(0, false).should.equal('m/45\'/2147483647/0/0');
|
||||
|
||||
// copayer 0, address 0, external address (receiving)
|
||||
Structure.FullBranch(0, false, 0).should.equal('m/45\'/0/0/0');
|
||||
HDPath.FullBranch(0, false, 0).should.equal('m/45\'/0/0/0');
|
||||
|
||||
// copayer 0, address 10, external address (receiving)
|
||||
Structure.FullBranch(0, false, 10).should.equal('m/45\'/10/0/0');
|
||||
HDPath.FullBranch(0, false, 10).should.equal('m/45\'/10/0/0');
|
||||
|
||||
// copayer 0, address 0, internal address (change)
|
||||
Structure.FullBranch(0, true, 0).should.equal('m/45\'/0/1/0');
|
||||
HDPath.FullBranch(0, true, 0).should.equal('m/45\'/0/1/0');
|
||||
|
||||
// copayer 0, address 10, internal address (change)
|
||||
Structure.FullBranch(10, true, 0).should.equal('m/45\'/0/1/10');
|
||||
HDPath.FullBranch(10, true, 0).should.equal('m/45\'/0/1/10');
|
||||
|
||||
// copayer 7, address 10, internal address (change)
|
||||
Structure.FullBranch(10, true, 7).should.equal('m/45\'/7/1/10');
|
||||
HDPath.FullBranch(10, true, 7).should.equal('m/45\'/7/1/10');
|
||||
});
|
||||
|
||||
[
|
||||
['m/45\'/0/0/0', {
|
||||
index: 0,
|
||||
isChange: false
|
||||
}],
|
||||
['m/45\'/0/0/1', {
|
||||
index: 1,
|
||||
isChange: false
|
||||
}],
|
||||
['m/45\'/0/0/2', {
|
||||
index: 2,
|
||||
isChange: false
|
||||
}],
|
||||
['m/45\'/0/1/0', {
|
||||
index: 0,
|
||||
isChange: true
|
||||
}],
|
||||
['m/45\'/0/1/1', {
|
||||
index: 1,
|
||||
isChange: true
|
||||
}],
|
||||
['m/45\'/0/1/2', {
|
||||
index: 2,
|
||||
isChange: true
|
||||
}],
|
||||
['m/45\'/0/0/900', {
|
||||
index: 900,
|
||||
isChange: false
|
||||
}],
|
||||
index: 0,
|
||||
isChange: false
|
||||
}],
|
||||
['m/45\'/0/0/1', {
|
||||
index: 1,
|
||||
isChange: false
|
||||
}],
|
||||
['m/45\'/0/0/2', {
|
||||
index: 2,
|
||||
isChange: false
|
||||
}],
|
||||
['m/45\'/0/1/0', {
|
||||
index: 0,
|
||||
isChange: true
|
||||
}],
|
||||
['m/45\'/0/1/1', {
|
||||
index: 1,
|
||||
isChange: true
|
||||
}],
|
||||
['m/45\'/0/1/2', {
|
||||
index: 2,
|
||||
isChange: true
|
||||
}],
|
||||
['m/45\'/0/0/900', {
|
||||
index: 900,
|
||||
isChange: false
|
||||
}],
|
||||
].forEach(function(datum) {
|
||||
var path = datum[0];
|
||||
var result = datum[1];
|
||||
it('should get the correct indices for path ' + path, function() {
|
||||
var i = Structure.indicesForPath(path);
|
||||
var i = HDPath.indicesForPath(path);
|
||||
i.index.should.equal(result.index);
|
||||
i.isChange.should.equal(result.isChange);
|
||||
});
|
||||
});
|
||||
it('should get the correct result for bitcoin uri', function() {
|
||||
var uri = 'bitcoin:19mP9FKrXqL46Si58pHdhGKow88SUPy1V8%3Famount=0.1&message=a%20bitcoin%20donation';
|
||||
var result = Structure.parseBitcoinURI(uri);
|
||||
var result = HDPath.parseBitcoinURI(uri);
|
||||
result.address.should.equal('19mP9FKrXqL46Si58pHdhGKow88SUPy1V8');
|
||||
result.amount.should.equal(0.1);
|
||||
result.message.should.equal('a bitcoin donation');
|
|
@ -6,7 +6,7 @@ var bitcore = bitcore || require('bitcore');
|
|||
var Address = bitcore.Address;
|
||||
var buffertools = bitcore.buffertools;
|
||||
|
||||
var Structure = require('../js/models/core/Structure');
|
||||
var HDPath = require('../js/models/core/HDPath');
|
||||
|
||||
try {
|
||||
var copay = require('copay'); //browser
|
||||
|
@ -135,7 +135,7 @@ describe('PublicKeyRing model', function() {
|
|||
a.network().name.should.equal('livenet');
|
||||
if (i > 1) {
|
||||
w.getAddress(i - 1, isChange).toString().should
|
||||
.not.equal(w.getAddress(i - 2, isChange).toString());
|
||||
.not.equal(w.getAddress(i - 2, isChange).toString());
|
||||
}
|
||||
}
|
||||
});
|
||||
|
@ -170,9 +170,9 @@ describe('PublicKeyRing model', function() {
|
|||
var w = k.w;
|
||||
|
||||
for (var i = 0; i < 3; i++)
|
||||
w.generateAddress(true, k.pub);
|
||||
w.generateAddress(true, k.pub);
|
||||
for (var i = 0; i < 2; i++)
|
||||
w.generateAddress(false, k.pub);
|
||||
w.generateAddress(false, k.pub);
|
||||
|
||||
w.getIndex(k.pub).getChangeIndex().should.equal(3);
|
||||
w.getIndex(k.pub).getReceiveIndex().should.equal(2);
|
||||
|
@ -240,9 +240,9 @@ describe('PublicKeyRing model', function() {
|
|||
var w = k.w;
|
||||
|
||||
for (var i = 0; i < 2; i++)
|
||||
w.generateAddress(true, k.pub);
|
||||
w.generateAddress(true, k.pub);
|
||||
for (var i = 0; i < 3; i++)
|
||||
w.generateAddress(false, k.pub);
|
||||
w.generateAddress(false, k.pub);
|
||||
|
||||
var w2 = new PublicKeyRing({
|
||||
networkName: 'livenet',
|
||||
|
@ -445,9 +445,9 @@ describe('PublicKeyRing model', function() {
|
|||
networkName: 'livenet',
|
||||
};
|
||||
var p = new PublicKeyRing(config);
|
||||
var i = p.getIndex(Structure.SHARED_INDEX);
|
||||
var i = p.getIndex(HDPath.SHARED_INDEX);
|
||||
should.exist(i);
|
||||
i.cosigner.should.equal(Structure.SHARED_INDEX);
|
||||
i.copayerIndex.should.equal(HDPath.SHARED_INDEX);
|
||||
});
|
||||
|
||||
it('#getIndex should throw error', function() {
|
||||
|
@ -467,9 +467,9 @@ describe('PublicKeyRing model', function() {
|
|||
var amount = 2;
|
||||
|
||||
for (var i = 0; i < amount; i++)
|
||||
w.generateAddress(true, k.pub);
|
||||
w.generateAddress(true, k.pub);
|
||||
for (var i = 0; i < amount; i++)
|
||||
w.generateAddress(false, k.pub);
|
||||
w.generateAddress(false, k.pub);
|
||||
|
||||
var m = w.getRedeemScriptMap([
|
||||
'm/45\'/2147483647/1/0',
|
||||
|
|
Loading…
Reference in New Issue