btc-transaction
Advanced tools
Comparing version 0.0.3 to 0.1.0
@@ -1,5 +0,16 @@ | ||
0.0.1 / 2014-01-13 | ||
0.1.0 / 2014-02-13 | ||
------------------ | ||
* initial release | ||
* if there is no `outpoint` default to coinbase `transactionIn`, 32 zero bytes, i.e. `0` as tx hash, `-1` as txIndex | ||
* added `Transaction.defaultNetwork`, defaults to `mainnet` | ||
* commented out `analyze()` | ||
* commented out `getDescription()` | ||
* refactored `TransactionIn` and `TransactionOut` into separate files | ||
* changed dep from `sha256` to `crypto-hashing` | ||
* changed dep from `convert-hex` to `binstring` | ||
0.0.3 / 2014-02-12 | ||
------------------ | ||
* Bugfix - reversing value bytes and removing bigInteger.valueOf as it does not work for numbers greater than 32 bits | ||
* Feature- Added support to deserialize multiple transcation hex | ||
0.0.2 / 2014-02-04 | ||
@@ -9,5 +20,5 @@ ------------------ | ||
0.0.3 / 2014-02-12 | ||
0.0.1 / 2014-01-13 | ||
------------------ | ||
* Bugfix - reversing value bytes and removing bigInteger.valueOf as it does not work for numbers greater than 32 bits | ||
* Feature- Added support to deserialize multiple transcation hex | ||
* initial release | ||
var Address = require('btc-address'); | ||
var BigInteger = require('bigi'); | ||
var Script = require('btc-script'); | ||
var conv = require('convert-hex'); | ||
var sha256 = require('sha256'); | ||
var sha256 = require('crypto-hashing').sha256; | ||
var binConv = require('binstring'); | ||
@@ -14,3 +13,20 @@ var Parser = require('crypto-binary').MessageParser; | ||
var _defaultNetwork = 'mainnet'; | ||
Transaction.defaultNetwork = _defaultNetwork; | ||
Object.defineProperty(Transaction, 'defaultNetwork', { | ||
set: function(val) { | ||
_defaultNetwork = val; | ||
Address.defaultNetwork = val; | ||
Script.defaultNetwork = val; | ||
}, | ||
get: function() { | ||
return _defaultNetwork; | ||
} | ||
}) | ||
module.exports.Transaction = Transaction; | ||
var TransactionIn = require('./transaction-in'); | ||
var TransactionOut = require('./transaction-out'); | ||
module.exports.TransactionIn = TransactionIn; | ||
@@ -121,3 +137,3 @@ module.exports.TransactionOut = TransactionOut; | ||
*/ | ||
Transaction.prototype.addOutput = function(address, value) { | ||
Transaction.prototype.addOutput = function(address, value, network) { | ||
if (arguments[0] instanceof TransactionOut) { | ||
@@ -127,3 +143,3 @@ this.outs.push(arguments[0]); | ||
if ("string" == typeof address) { | ||
address = new Address(address); | ||
address = new Address(address, null, network || Transaction.defaultNetwork); | ||
} | ||
@@ -147,3 +163,3 @@ if ("number" == typeof value) { | ||
value: value, | ||
script: Script.createOutputScript(address) | ||
script: Script.createOutputScript(address, network || Transaction.defaultNetwork) | ||
})); | ||
@@ -202,49 +218,43 @@ } | ||
*/ | ||
Transaction.prototype.hashTransactionForSignature = | ||
function(connectedScript, inIndex, hashType) { | ||
var txTmp = this.clone(); | ||
Transaction.prototype.hashTransactionForSignature = function(connectedScript, inIndex, hashType) { | ||
var txTmp = this.clone(); | ||
// In case concatenating two scripts ends up with two codeseparators, | ||
// or an extra one at the end, this prevents all those possible | ||
// incompatibilities. | ||
/*scriptCode = scriptCode.filter(function (val) { | ||
return val !== OP_CODESEPARATOR; | ||
});*/ | ||
// In case concatenating two scripts ends up with two codeseparators, | ||
// or an extra one at the end, this prevents all those possible | ||
// incompatibilities. | ||
/*scriptCode = scriptCode.filter(function (val) { | ||
return val !== OP_CODESEPARATOR; | ||
});*/ | ||
// Blank out other inputs' signatures | ||
for (var i = 0; i < txTmp.ins.length; i++) { | ||
txTmp.ins[i].script = new Script(); | ||
} | ||
// Blank out other inputs' signatures | ||
for (var i = 0; i < txTmp.ins.length; i++) { | ||
txTmp.ins[i].script = new Script(); | ||
} | ||
txTmp.ins[inIndex].script = connectedScript; | ||
txTmp.ins[inIndex].script = connectedScript; | ||
// Blank out some of the outputs | ||
if ((hashType & 0x1f) == SIGHASH_NONE) { | ||
txTmp.outs = []; | ||
// Blank out some of the outputs | ||
if ((hashType & 0x1f) == SIGHASH_NONE) { | ||
txTmp.outs = []; | ||
// Let the others update at will | ||
for (var i = 0; i < txTmp.ins.length; i++) | ||
if (i != inIndex) | ||
txTmp.ins[i].sequence = 0; | ||
} else if ((hashType & 0x1f) == SIGHASH_SINGLE) { | ||
// TODO: Implement | ||
} | ||
// Let the others update at will | ||
for (var i = 0; i < txTmp.ins.length; i++) | ||
if (i != inIndex) | ||
txTmp.ins[i].sequence = 0; | ||
} else if ((hashType & 0x1f) == SIGHASH_SINGLE) { | ||
// TODO: Implement | ||
} | ||
// Blank out other inputs completely, not recommended for open transactions | ||
if (hashType & SIGHASH_ANYONECANPAY) { | ||
txTmp.ins = [txTmp.ins[inIndex]]; | ||
} | ||
// Blank out other inputs completely, not recommended for open transactions | ||
if (hashType & SIGHASH_ANYONECANPAY) { | ||
txTmp.ins = [txTmp.ins[inIndex]]; | ||
} | ||
var buffer = txTmp.serialize(); | ||
var buffer = txTmp.serialize(); | ||
buffer = buffer.concat(wordsToBytes([parseInt(hashType)]).reverse()); | ||
//todo: change to buffer | ||
buffer = buffer.concat(wordsToBytes([parseInt(hashType)]).reverse()); | ||
return sha256.x2(buffer, {in: 'bytes', out: 'bytes'}); | ||
} | ||
/*var hash1 = Crypto.SHA256(buffer, {asBytes: true}); | ||
return Crypto.SHA256(hash1, {asBytes: true});*/ | ||
return sha256.x2(buffer, { | ||
asBytes: true | ||
}); | ||
}; | ||
/** | ||
@@ -255,6 +265,5 @@ * Calculate and return the transaction's hash. | ||
var buffer = this.serialize(); | ||
//return Crypto.SHA256(Crypto.SHA256(buffer, {asBytes: true}), {asBytes: true}).reverse(); | ||
return sha256.x2(buffer, { | ||
asBytes: true | ||
}).reverse(); | ||
//todo: change to buffer | ||
return sha256.x2(buffer, {in: 'bytes', out: 'bytes'}).reverse(); | ||
}; | ||
@@ -305,3 +314,3 @@ | ||
*/ | ||
Transaction.prototype.analyze = function(wallet) { | ||
/*Transaction.prototype.analyze = function(wallet) { | ||
var Wallet = require('./wallet'); | ||
@@ -357,3 +366,3 @@ if (!(wallet instanceof Wallet)) return null; | ||
return analysis; | ||
}; | ||
};*/ | ||
@@ -366,3 +375,3 @@ /** | ||
*/ | ||
Transaction.prototype.getDescription = function(wallet) { | ||
/*Transaction.prototype.getDescription = function(wallet) { | ||
var analysis = this.analyze(wallet); | ||
@@ -389,3 +398,3 @@ | ||
} | ||
}; | ||
};*/ | ||
@@ -411,56 +420,3 @@ /** | ||
function TransactionIn(data) { | ||
this.outpoint = data.outpoint; | ||
if (data.script instanceof Script) { | ||
this.script = data.script; | ||
} else { | ||
if (data.scriptSig) { | ||
this.script = Script.fromScriptSig(data.scriptSig); | ||
} else { | ||
this.script = new Script(data.script); | ||
} | ||
} | ||
this.sequence = data.sequence; | ||
}; | ||
TransactionIn.prototype.clone = function() { | ||
var newTxin = new TransactionIn({ | ||
outpoint: { | ||
hash: this.outpoint.hash, | ||
index: this.outpoint.index | ||
}, | ||
script: this.script.clone(), | ||
sequence: this.sequence | ||
}); | ||
return newTxin; | ||
}; | ||
function TransactionOut(data) { | ||
if (data.script instanceof Script) { | ||
this.script = data.script; | ||
} else { | ||
if (data.scriptPubKey) { | ||
this.script = Script.fromScriptSig(data.scriptPubKey); | ||
} else { | ||
this.script = new Script(data.script); | ||
} | ||
} | ||
if (Array.isArray(data.value)) { | ||
this.value = data.value; | ||
} else if ("string" == typeof data.value) { | ||
var valueHex = (new BigInteger(data.value, 10)).toString(16); | ||
while (valueHex.length < 16) valueHex = "0" + valueHex; | ||
this.value = conv.hexToBytes(valueHex).reverse(); | ||
} | ||
}; | ||
TransactionOut.prototype.clone = function() { | ||
var newTxout = new TransactionOut({ | ||
script: this.script.clone(), | ||
value: this.value.slice(0) | ||
}); | ||
return newTxout; | ||
}; | ||
// Second argument txCount is optional; It indicates | ||
@@ -467,0 +423,0 @@ // the number of the transactions that you expect to |
{ | ||
"name": "btc-transaction", | ||
"version": "0.0.3", | ||
"version": "0.1.0", | ||
"description": "Create, parse, serialize Bitcoin transactions.", | ||
@@ -24,11 +24,9 @@ "keywords": [ | ||
"btc-opcode": "0.0.1", | ||
"convert-hex": "~0.1.0", | ||
"btc-address": "~0.2.0", | ||
"btc-address": "~0.4.0", | ||
"bigi": "~0.2.0", | ||
"btc-script": "0.0.1", | ||
"sha256": "~0.1.1", | ||
"terst": "0.0.1", | ||
"btc-script": "~0.1.1", | ||
"crypto-binary": "~0.1.0", | ||
"binstring": "~0.2.0" | ||
"binstring": "~0.2.0", | ||
"crypto-hashing": "~0.1.0" | ||
} | ||
} |
17455
7
7
491
+ Addedcrypto-hashing@~0.1.0
+ Addedbtc-address@0.4.0(transitive)
+ Addedbtc-script@0.1.12(transitive)
+ Addedcrypto-hashing@0.1.00.3.1(transitive)
+ Addedripemd160@0.2.1(transitive)
- Removedconvert-hex@~0.1.0
- Removedsha256@~0.1.1
- Removedterst@0.0.1
- Removedbtc-address@0.2.0(transitive)
- Removedbtc-script@0.0.1(transitive)
- Removedconvert-hex@0.1.0(transitive)
- Removedconvert-string@0.1.0(transitive)
- Removedsha256@0.1.1(transitive)
- Removedterst@0.0.1(transitive)
Updatedbtc-address@~0.4.0
Updatedbtc-script@~0.1.1