Merge pull request #739 from maraoz/optimize/redeemscriptmap
optimize redeemscript map generation
This commit is contained in:
commit
139f6f8e18
9 changed files with 66 additions and 35 deletions
|
|
@ -17,7 +17,6 @@ FakeBlockchain.prototype.fixUnspent = function(u) {
|
|||
};
|
||||
|
||||
FakeBlockchain.prototype.getUnspent = function(addresses, cb) {
|
||||
if (!addresses || !addresses.length) return cb(null, []);
|
||||
return cb(null, this.u || [{
|
||||
'address': 'mji7zocy8QzYywQakwWf99w9bCT6orY1C1',
|
||||
'txid': '0be0fb4579911be829e3077202e1ab47fcc12cf3ab8f8487ccceae768e1f95fa',
|
||||
|
|
@ -31,7 +30,6 @@ FakeBlockchain.prototype.getUnspent = function(addresses, cb) {
|
|||
};
|
||||
|
||||
FakeBlockchain.prototype.getUnspent2 = function(addresses, cb) {
|
||||
if (!addresses || !addresses.length) return cb(null, []);
|
||||
return cb(null, this.u || [{
|
||||
'address': 'mji7zocy8QzYywQakwWf99w9bCT6orY1C1',
|
||||
'txid': '0be0fb4579911be829e3077202e1ab47fcc12cf3ab8f8487ccceae768e1f95fa',
|
||||
|
|
|
|||
|
|
@ -381,13 +381,19 @@ describe('PublicKeyRing model', function() {
|
|||
it('#getRedeemScriptMap check tests', function() {
|
||||
var k = createW();
|
||||
var w = k.w;
|
||||
var amount = 2;
|
||||
|
||||
for (var i = 0; i < 2; i++)
|
||||
for (var i = 0; i < amount; i++)
|
||||
w.generateAddress(true);
|
||||
for (var i = 0; i < 2; i++)
|
||||
for (var i = 0; i < amount; i++)
|
||||
w.generateAddress(false);
|
||||
|
||||
var m = w.getRedeemScriptMap();
|
||||
var m = w.getRedeemScriptMap([
|
||||
'm/45\'/2147483647/1/0',
|
||||
'm/45\'/2147483647/1/1',
|
||||
'm/45\'/2147483647/0/0',
|
||||
'm/45\'/2147483647/0/1'
|
||||
]);
|
||||
Object.keys(m).length.should.equal(4);
|
||||
Object.keys(m).forEach(function(k) {
|
||||
should.exist(m[k]);
|
||||
|
|
|
|||
|
|
@ -38,4 +38,22 @@ describe('Structure model', function() {
|
|||
Structure.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}],
|
||||
].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);
|
||||
i.index.should.equal(result.index);
|
||||
i.isChange.should.equal(result.isChange);
|
||||
});
|
||||
});
|
||||
|
||||
});
|
||||
|
|
|
|||
|
|
@ -144,7 +144,6 @@ describe('TxProposals model', function() {
|
|||
|
||||
var b = new Builder(opts)
|
||||
.setUnspent(utxos)
|
||||
.setHashToScriptMap(pkr.getRedeemScriptMap())
|
||||
.setOutputs([{
|
||||
address: toAddress,
|
||||
amountSat: amountSat
|
||||
|
|
@ -154,6 +153,12 @@ describe('TxProposals model', function() {
|
|||
return pkr.pathForAddress(utxo.address);
|
||||
});
|
||||
|
||||
var selectedUtxos = b.getSelectedUnspent();
|
||||
var inputChainPaths = selectedUtxos.map(function(utxo) {
|
||||
return pkr.pathForAddress(utxo.address);
|
||||
});
|
||||
b.setHashToScriptMap(pkr.getRedeemScriptMap(inputChainPaths));
|
||||
|
||||
var signRet;
|
||||
if (priv) {
|
||||
var pkeys = priv.getForPaths(inputChainPaths);
|
||||
|
|
|
|||
|
|
@ -42,30 +42,36 @@ describe('Wallet model', function() {
|
|||
w.getNetworkName().should.equal('testnet');
|
||||
});
|
||||
|
||||
|
||||
var createW = function(netKey, N, conf) {
|
||||
|
||||
var c = JSON.parse(JSON.stringify(conf || config));
|
||||
if (!N) N = c.totalCopayers;
|
||||
|
||||
if (netKey) c.netKey = netKey;
|
||||
c.privateKey = new copay.PrivateKey({
|
||||
networkName: c.networkName
|
||||
var mainPrivateKey = new copay.PrivateKey({
|
||||
networkName: config.networkName
|
||||
});
|
||||
var mainCopayerEPK = mainPrivateKey.deriveBIP45Branch().extendedPublicKeyString();
|
||||
c.privateKey = mainPrivateKey;
|
||||
|
||||
c.publicKeyRing = new copay.PublicKeyRing({
|
||||
networkName: c.networkName,
|
||||
requiredCopayers: Math.min(N, c.requiredCopayers),
|
||||
totalCopayers: N,
|
||||
});
|
||||
var copayerEPK = c.privateKey.deriveBIP45Branch().extendedPublicKeyString()
|
||||
c.publicKeyRing.addCopayer(copayerEPK);
|
||||
c.publicKeyRing.addCopayer(mainCopayerEPK);
|
||||
|
||||
c.txProposals = new copay.TxProposals({
|
||||
networkName: c.networkName,
|
||||
});
|
||||
c.storage = new Storage(config.storage);
|
||||
c.network = new Network(config.network);
|
||||
c.blockchain = new Blockchain(config.blockchain);
|
||||
|
||||
var storage = new Storage(config.storage);
|
||||
var network = new Network(config.network);
|
||||
var blockchain = new Blockchain(config.blockchain);
|
||||
c.storage = storage;
|
||||
c.network = network;
|
||||
c.blockchain = blockchain;
|
||||
|
||||
c.addressBook = {
|
||||
'2NFR2kzH9NUdp8vsXTB4wWQtTtzhpKxsyoJ': {
|
||||
|
|
@ -138,13 +144,6 @@ describe('Wallet model', function() {
|
|||
pkr.addCopayer();
|
||||
}
|
||||
}
|
||||
pkr.generateAddress(true);
|
||||
pkr.generateAddress(true);
|
||||
pkr.generateAddress(true);
|
||||
pkr.generateAddress(false);
|
||||
pkr.generateAddress(false);
|
||||
pkr.generateAddress(false);
|
||||
//3x3 indexes
|
||||
|
||||
return w;
|
||||
};
|
||||
|
|
@ -234,7 +233,7 @@ describe('Wallet model', function() {
|
|||
var w = createW2();
|
||||
|
||||
var ts = Date.now();
|
||||
for (var isChange = 0; isChange < 2; isChange++) {
|
||||
for (var isChange = false; !isChange; isChange = true) {
|
||||
for (var index = 0; index < 3; index++) {
|
||||
unspentTest[0].address = w.publicKeyRing.getAddress(index, isChange).toString();
|
||||
unspentTest[0].scriptPubKey = w.publicKeyRing.getScriptPubKeyHex(index, isChange);
|
||||
|
|
@ -531,6 +530,7 @@ describe('Wallet model', function() {
|
|||
it('should get balance', function(done) {
|
||||
var w = createW();
|
||||
var spy = sinon.spy(w.blockchain, 'getUnspent');
|
||||
w.blockchain.fixUnspent([]);
|
||||
w.getBalance(function(err, balance, balanceByAddr, safeBalance) {
|
||||
sinon.assert.callCount(spy, 1);
|
||||
balance.should.equal(0);
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue