Transaction: renames getHash to getId

In turn also removes the inherent calculation of tx.hash after deserialization.
This commit is contained in:
Daniel Cousens 2014-05-20 14:07:22 +10:00
parent 867465a03f
commit bdc7131d0e
4 changed files with 25 additions and 16 deletions

View file

@ -25,7 +25,6 @@ function Transaction(doc) {
this.outs = []
if (doc) {
if (doc.hash) this.hash = doc.hash;
if (doc.version) this.version = doc.version;
if (doc.locktime) this.locktime = doc.locktime;
if (doc.ins && doc.ins.length) {
@ -39,8 +38,6 @@ function Transaction(doc) {
return new TransactionOut(output)
})
}
this.hash = this.hash || this.getHash()
}
}
@ -55,7 +52,15 @@ function Transaction(doc) {
* Note that this method does not sign the created input.
*/
Transaction.prototype.addInput = function(tx, outIndex) {
var hash = typeof tx === "string" ? tx : tx.hash
var hash
if (typeof tx === 'string') {
hash = tx
} else {
assert(tx instanceof Transaction, 'Unexpected input: ' + tx)
hash = tx.getId()
}
this.ins.push(new TransactionIn({
outpoint: {
@ -209,7 +214,7 @@ Transaction.prototype.hashForSignature = function(prevOutScript, inIndex, hashTy
return crypto.hash256(buffer)
}
Transaction.prototype.getHash = function () {
Transaction.prototype.getId = function () {
var buffer = crypto.hash256(this.toBuffer())
// Big-endian is used for TxHash

View file

@ -146,7 +146,7 @@ function Wallet(seed, network) {
}
function processTx(tx, isPending) {
var txhash = tx.getHash()
var txid = tx.getId()
tx.outs.forEach(function(txOut, i) {
var address
@ -158,7 +158,7 @@ function Wallet(seed, network) {
}
if (isMyAddress(address)) {
var output = txhash + ':' + i
var output = txid + ':' + i
me.outputs[output] = {
receive: output,

View file

@ -74,11 +74,6 @@ describe('Transaction', function() {
assert.deepEqual(output.script, Address.fromBase58Check('n1gqLjZbRH1biT5o4qiVMiNig8wcCPQeB9').toOutputScript())
})
it('assigns hash to deserialized object', function() {
var hashHex = "a9d4599e15b53f3eb531608ddb31f48c695c3d0b3538a6bda871e8b34f2f430c"
assert.equal(tx.hash, hashHex)
})
it('decodes large inputs correctly', function() {
// transaction has only 1 input
var tx = new Transaction()
@ -251,5 +246,15 @@ describe('Transaction', function() {
assert.equal(tx.toHex(), expected)
})
})
describe('getId', function() {
it('returns the expected txid', function() {
var tx = new Transaction()
tx.addInput('d6f72aab8ff86ff6289842a0424319bf2ddba85dc7c52757912297f948286389', 0)
tx.addOutput('mrCDrCybB6J1vRfbwM5hemdJz73FwDBC8r', 1)
assert.equal(tx.getId(), '7c3275f1212fd1a2add614f47a1f1f7b6d9570a97cb88e0e2664ab1752976e9f')
})
})
})

View file

@ -303,7 +303,6 @@ describe('Wallet', function() {
function outputCount(){
return Object.keys(wallet.outputs).length
}
})
describe("when tx ins outpoint contains a known txhash:i", function(){
@ -340,7 +339,7 @@ describe('Wallet', function() {
function verifyOutputAdded(index, pending) {
var txOut = tx.outs[index]
var key = tx.getHash() + ":" + index
var key = tx.getId() + ":" + index
var output = wallet.outputs[key]
assert.equal(output.receive, key)
assert.equal(output.value, txOut.value)