Split buildPKH arguments, add Copy constructor on publickey

This commit is contained in:
Esteban Ordano 2014-12-10 12:40:05 -03:00
parent 5c974a8ef2
commit 0c28bc1786
5 changed files with 40 additions and 12 deletions

View File

@ -19,6 +19,9 @@ module.exports = [{
}, {
name: 'InvalidState',
message: format('Invalid state: {0}')
}, {
name: 'NotImplemented',
message: format('Function {0} was not implemented yet')
}, {
name: 'InvalidNetworkArgument',
message: format('Invalid network: must be "livenet" or "testnet", got {0}')

View File

@ -33,6 +33,10 @@ var PublicKey = function PublicKey(data, compressed) {
if (!data) {
throw new TypeError('First argument is required, please include public key data.');
}
if (data instanceof PublicKey) {
// Return copy, but as it's an immutable object, return same argument
return data;
}
var info = {
compressed: typeof(compressed) !== 'undefined' ? compressed : true
@ -42,7 +46,7 @@ var PublicKey = function PublicKey(data, compressed) {
if (data instanceof Point) {
info.point = data;
} else if (typeof(data) === 'string'){
info = PublicKey._transformDER(new Buffer(data, 'hex' ));
info = PublicKey._transformDER(new Buffer(data, 'hex'));
} else if (data instanceof Buffer || data instanceof Uint8Array){
info = PublicKey._transformDER(data);
} else if (data.constructor && (data.constructor.name &&

View File

@ -5,10 +5,9 @@ var Address = require('./address');
var BufferReader = require('./encoding/bufferreader');
var BufferWriter = require('./encoding/bufferwriter');
var Hash = require('./crypto/hash');
var Hash = require('./crypto/hash');
var Opcode = require('./opcode');
var PublicKey = require('./publickey');
var PublicKey = require('./publickey');
var Signature = require('./crypto/signature');
var _ = require('lodash');
var buffer = require('buffer');
@ -545,18 +544,17 @@ Script.buildScriptHashOut = function(script) {
* Builds a scriptSig (a script for an input) that signs a public key hash
* output script.
*
* @param {Object} signature
* @param {PublicKey} signature.publicKey
* @param {Buffer} signature.signature
* @param {number} signature.sigtype
* @param {Buffer|string|PublicKey} publicKey
* @param {Buffer} signature - the signature in DER cannonical encoding
* @param {number=1} sigtype - the type of the signature (defaults to SIGHASH_ALL)
*/
Script.buildPublicKeyHashIn = function(signature) {
Script.buildPublicKeyHashIn = function(publicKey, signature, sigtype) {
var script = new Script()
.add(bufferUtil.concat([
signature.signature.toDER(),
bufferUtil.integerAsSingleByteBuffer(signature.sigtype)
signature,
bufferUtil.integerAsSingleByteBuffer(sigtype || Signature.SIGHASH_ALL)
]))
.add(signature.publicKey.toBuffer());
.add(new PublicKey(publicKey).toBuffer());
return script;
};

View File

@ -12,6 +12,8 @@ var Hash = require('../crypto/hash');
var Sighash = require('./sighash');
var Signature = require('../crypto/signature');
var errors = require('../errors');
var Address = require('../address');
var Unit = require('../unit');
var Input = require('./input');
@ -176,6 +178,7 @@ Transaction.prototype.from = function(utxo, pubkeys, threshold) {
};
Transaction.prototype._fromMultiSigP2SH = function(utxo, pubkeys, threshold) {
throw new errors.NotImplemented('Transaction#_fromMultiSigP2SH');
};
Transaction.prototype._fromNonP2SH = function(utxo) {
@ -275,6 +278,7 @@ Transaction.prototype.to = function() {
};
Transaction.prototype._payToMultisig = function(pubkeys, threshold, amount) {
throw new errors.NotImplemented('Transaction#_payToMultisig');
};
Transaction.prototype._payToAddress = function(address, amount) {
@ -344,7 +348,13 @@ Transaction.prototype._getPrivateKeySignatures = function(privKey) {
};
Transaction.prototype.applySignature = function(signature) {
this.inputs[signature.inputIndex].setScript(Script.buildPublicKeyHashIn(signature));
this.inputs[signature.inputIndex].setScript(
Script.buildPublicKeyHashIn(
signature.publicKey,
signature.signature.toDER(),
signature.sigtype
)
);
return this;
};

View File

@ -43,6 +43,19 @@ describe('PublicKey', function() {
pk.toString().should.equal(pubhex);
});
it('should instantiate from a compressed public key', function() {
var publicKeyHex = '031ff0fe0f7b15ffaa85ff9f4744d539139c252a49710fb053bb9f2b933173ff9a';
var publicKey = new PublicKey(publicKeyHex);
publicKey.toString().should.equal(publicKeyHex);
});
it('should instantiate from another publicKey', function() {
var publicKeyHex = '031ff0fe0f7b15ffaa85ff9f4744d539139c252a49710fb053bb9f2b933173ff9a';
var publicKey = new PublicKey(publicKeyHex);
var publicKey2 = new PublicKey(publicKey);
publicKey.should.equal(publicKey2);
});
it('should instantiate from a hex encoded DER string', function() {
var pk = new PublicKey('041ff0fe0f7b15ffaa85ff9f4744d539139c252a49710fb053bb9f2b933173ff9a7baad41d04514751e6851f5304fd243751703bed21b914f6be218c0fa354a341');
should.exist(pk.point);