9 changed files with 339 additions and 79 deletions
@ -0,0 +1,46 @@ |
|||||
|
'use strict'; |
||||
|
|
||||
|
var should = require('chai').should(); |
||||
|
var expect = require('chai').expect; |
||||
|
var _ = require('lodash'); |
||||
|
|
||||
|
var bitcore = require('../../..'); |
||||
|
var errors = bitcore.errors; |
||||
|
var PrivateKey = bitcore.PrivateKey; |
||||
|
var Address = bitcore.Address; |
||||
|
var Script = bitcore.Script; |
||||
|
var Networks = bitcore.Networks; |
||||
|
var Input = bitcore.Transaction.Input; |
||||
|
|
||||
|
describe('Transaction.Input', function() { |
||||
|
|
||||
|
var privateKey = new PrivateKey('KwF9LjRraetZuEjR8VqEq539z137LW5anYDUnVK11vM3mNMHTWb4'); |
||||
|
var publicKey = privateKey.publicKey; |
||||
|
var address = new Address(publicKey, Networks.livenet); |
||||
|
var output = { |
||||
|
address: '33zbk2aSZYdNbRsMPPt6jgy6Kq1kQreqeb', |
||||
|
prevTxId: '66e64ef8a3b384164b78453fa8c8194de9a473ba14f89485a0e433699daec140', |
||||
|
outputIndex: 0, |
||||
|
script: new Script(address), |
||||
|
satoshis: 1000000 |
||||
|
}; |
||||
|
var coinbase = { |
||||
|
prevTxId: '0000000000000000000000000000000000000000000000000000000000000000', |
||||
|
outputIndex: 0xFFFFFFFF, |
||||
|
script: new Script(), |
||||
|
satoshis: 1000000 |
||||
|
}; |
||||
|
|
||||
|
it('has abstract methods: "getSignatures", "isFullySigned", "addSignature", "clearSignatures"', function() { |
||||
|
var input = new Input(output); |
||||
|
_.each(['getSignatures', 'isFullySigned', 'addSignature', 'clearSignatures'], function(method) { |
||||
|
expect(function() { |
||||
|
return input[method](); |
||||
|
}).to.throw(errors.AbstractMethodInvoked); |
||||
|
}); |
||||
|
}); |
||||
|
it('detects coinbase transactions', function() { |
||||
|
new Input(output).isNull().should.equal(false); |
||||
|
new Input(coinbase).isNull().should.equal(true); |
||||
|
}); |
||||
|
}); |
@ -0,0 +1,96 @@ |
|||||
|
'use strict'; |
||||
|
/* jshint unused: false */ |
||||
|
|
||||
|
var should = require('chai').should(); |
||||
|
var expect = require('chai').expect; |
||||
|
var _ = require('lodash'); |
||||
|
|
||||
|
var bitcore = require('../../..'); |
||||
|
var Transaction = bitcore.Transaction; |
||||
|
var PrivateKey = bitcore.PrivateKey; |
||||
|
var Address = bitcore.Address; |
||||
|
var Script = bitcore.Script; |
||||
|
var Signature = bitcore.crypto.Signature; |
||||
|
|
||||
|
describe('MultiSigScriptHashInput', function() { |
||||
|
|
||||
|
var privateKey1 = new PrivateKey('KwF9LjRraetZuEjR8VqEq539z137LW5anYDUnVK11vM3mNMHTWb4'); |
||||
|
var privateKey2 = new PrivateKey('L4PqnaPTCkYhAqH3YQmefjxQP6zRcF4EJbdGqR8v6adtG9XSsadY'); |
||||
|
var privateKey3 = new PrivateKey('L4CTX79zFeksZTyyoFuPQAySfmP7fL3R41gWKTuepuN7hxuNuJwV'); |
||||
|
var public1 = privateKey1.publicKey; |
||||
|
var public2 = privateKey2.publicKey; |
||||
|
var public3 = privateKey3.publicKey; |
||||
|
var address = new Address('33zbk2aSZYdNbRsMPPt6jgy6Kq1kQreqeb'); |
||||
|
|
||||
|
var output = { |
||||
|
address: '33zbk2aSZYdNbRsMPPt6jgy6Kq1kQreqeb', |
||||
|
txId: '66e64ef8a3b384164b78453fa8c8194de9a473ba14f89485a0e433699daec140', |
||||
|
outputIndex: 0, |
||||
|
script: new Script(address), |
||||
|
satoshis: 1000000 |
||||
|
}; |
||||
|
it('can count missing signatures', function() { |
||||
|
var transaction = new Transaction() |
||||
|
.from(output, [public1, public2, public3], 2) |
||||
|
.to(address, 1000000); |
||||
|
var input = transaction.inputs[0]; |
||||
|
|
||||
|
input.countSignatures().should.equal(0); |
||||
|
|
||||
|
transaction.sign(privateKey1); |
||||
|
input.countSignatures().should.equal(1); |
||||
|
input.countMissingSignatures().should.equal(1); |
||||
|
input.isFullySigned().should.equal(false); |
||||
|
|
||||
|
transaction.sign(privateKey2); |
||||
|
input.countSignatures().should.equal(2); |
||||
|
input.countMissingSignatures().should.equal(0); |
||||
|
input.isFullySigned().should.equal(true); |
||||
|
}); |
||||
|
it('returns a list of public keys with missing signatures', function() { |
||||
|
var transaction = new Transaction() |
||||
|
.from(output, [public1, public2, public3], 2) |
||||
|
.to(address, 1000000); |
||||
|
var input = transaction.inputs[0]; |
||||
|
|
||||
|
_.all(input.publicKeysWithoutSignature(), function(publicKeyMissing) { |
||||
|
var serialized = publicKeyMissing.toString(); |
||||
|
return serialized === public1.toString() || |
||||
|
serialized === public2.toString() || |
||||
|
serialized === public3.toString(); |
||||
|
}).should.equal(true); |
||||
|
transaction.sign(privateKey1); |
||||
|
_.all(input.publicKeysWithoutSignature(), function(publicKeyMissing) { |
||||
|
var serialized = publicKeyMissing.toString(); |
||||
|
return serialized === public2.toString() || |
||||
|
serialized === public3.toString(); |
||||
|
}).should.equal(true); |
||||
|
}); |
||||
|
it('can clear all signatures', function() { |
||||
|
var transaction = new Transaction() |
||||
|
.from(output, [public1, public2, public3], 2) |
||||
|
.to(address, 1000000) |
||||
|
.sign(privateKey1) |
||||
|
.sign(privateKey2); |
||||
|
|
||||
|
var input = transaction.inputs[0]; |
||||
|
input.isFullySigned().should.equal(true); |
||||
|
input.clearSignatures(); |
||||
|
input.isFullySigned().should.equal(false); |
||||
|
}); |
||||
|
it('can estimate how heavy is the output going to be', function() { |
||||
|
var transaction = new Transaction() |
||||
|
.from(output, [public1, public2, public3], 2) |
||||
|
.to(address, 1000000); |
||||
|
var input = transaction.inputs[0]; |
||||
|
input._estimateSize().should.equal(257); |
||||
|
}); |
||||
|
it('uses SIGHASH_ALL by default', function() { |
||||
|
var transaction = new Transaction() |
||||
|
.from(output, [public1, public2, public3], 2) |
||||
|
.to(address, 1000000); |
||||
|
var input = transaction.inputs[0]; |
||||
|
var sigs = input.getSignatures(transaction, privateKey1, 0); |
||||
|
sigs[0].sigtype.should.equal(Signature.SIGHASH_ALL); |
||||
|
}); |
||||
|
}); |
@ -0,0 +1,65 @@ |
|||||
|
'use strict'; |
||||
|
/* jshint unused: false */ |
||||
|
|
||||
|
var should = require('chai').should(); |
||||
|
var expect = require('chai').expect; |
||||
|
var _ = require('lodash'); |
||||
|
|
||||
|
var bitcore = require('../../..'); |
||||
|
var Transaction = bitcore.Transaction; |
||||
|
var PrivateKey = bitcore.PrivateKey; |
||||
|
var Address = bitcore.Address; |
||||
|
var Script = bitcore.Script; |
||||
|
var Networks = bitcore.Networks; |
||||
|
var Signature = bitcore.crypto.Signature; |
||||
|
|
||||
|
describe('PublicKeyHashInput', function() { |
||||
|
|
||||
|
var privateKey = new PrivateKey('KwF9LjRraetZuEjR8VqEq539z137LW5anYDUnVK11vM3mNMHTWb4'); |
||||
|
var publicKey = privateKey.publicKey; |
||||
|
var address = new Address(publicKey, Networks.livenet); |
||||
|
|
||||
|
var output = { |
||||
|
address: '33zbk2aSZYdNbRsMPPt6jgy6Kq1kQreqeb', |
||||
|
txId: '66e64ef8a3b384164b78453fa8c8194de9a473ba14f89485a0e433699daec140', |
||||
|
outputIndex: 0, |
||||
|
script: new Script(address), |
||||
|
satoshis: 1000000 |
||||
|
}; |
||||
|
it('can count missing signatures', function() { |
||||
|
var transaction = new Transaction() |
||||
|
.from(output) |
||||
|
.to(address, 1000000); |
||||
|
var input = transaction.inputs[0]; |
||||
|
|
||||
|
input.isFullySigned().should.equal(false); |
||||
|
transaction.sign(privateKey); |
||||
|
input.isFullySigned().should.equal(true); |
||||
|
}); |
||||
|
it('it\'s size can be estimated', function() { |
||||
|
var transaction = new Transaction() |
||||
|
.from(output) |
||||
|
.to(address, 1000000); |
||||
|
var input = transaction.inputs[0]; |
||||
|
input._estimateSize().should.equal(107); |
||||
|
}); |
||||
|
it('it\'s signature can be removed', function() { |
||||
|
var transaction = new Transaction() |
||||
|
.from(output) |
||||
|
.to(address, 1000000); |
||||
|
var input = transaction.inputs[0]; |
||||
|
|
||||
|
transaction.sign(privateKey); |
||||
|
input.clearSignatures(); |
||||
|
input.isFullySigned().should.equal(false); |
||||
|
}); |
||||
|
it('returns an empty array if private key mismatches', function() { |
||||
|
var transaction = new Transaction() |
||||
|
.from(output) |
||||
|
.to(address, 1000000); |
||||
|
var input = transaction.inputs[0]; |
||||
|
|
||||
|
input.getSignatures(transaction, new PrivateKey(), 0); |
||||
|
input.isFullySigned().should.equal(false); |
||||
|
}); |
||||
|
}); |
Loading…
Reference in new issue