Browse Source

bufferutils: add Buffer reverse

hk-custom-address
Daniel Cousens 11 years ago
parent
commit
71d4c78b88
  1. 7
      src/bufferutils.js
  2. 12
      src/transaction.js
  3. 6
      src/wallet.js
  4. 13
      test/bufferutils.js

7
src/bufferutils.js

@ -159,11 +159,18 @@ function writeVarInt(buffer, number, offset) {
return size return size
} }
function reverse(buffer) {
var buffer2 = new Buffer(buffer)
Array.prototype.reverse.call(buffer2)
return buffer2
}
module.exports = { module.exports = {
pushDataSize: pushDataSize, pushDataSize: pushDataSize,
readPushDataInt: readPushDataInt, readPushDataInt: readPushDataInt,
readUInt64LE: readUInt64LE, readUInt64LE: readUInt64LE,
readVarInt: readVarInt, readVarInt: readVarInt,
reverse: reverse,
varIntSize: varIntSize, varIntSize: varIntSize,
writePushDataInt: writePushDataInt, writePushDataInt: writePushDataInt,
writeUInt64LE: writeUInt64LE, writeUInt64LE: writeUInt64LE,

12
src/transaction.js

@ -37,10 +37,8 @@ Transaction.prototype.addInput = function(tx, index, sequence) {
var hash var hash
if (typeof tx === 'string') { if (typeof tx === 'string') {
hash = new Buffer(tx, 'hex')
// TxId hex is big-endian, we need little-endian // TxId hex is big-endian, we need little-endian
Array.prototype.reverse.call(hash) hash = bufferutils.reverse(new Buffer(tx, 'hex'))
} else if (tx instanceof Transaction) { } else if (tx instanceof Transaction) {
hash = tx.getHash() hash = tx.getHash()
@ -211,12 +209,8 @@ Transaction.prototype.getHash = function () {
} }
Transaction.prototype.getId = function () { Transaction.prototype.getId = function () {
var buffer = this.getHash() // TxHash is little-endian, we need big-endian
return bufferutils.reverse(this.getHash()).toString('hex')
// Big-endian is used for TxHash
Array.prototype.reverse.call(buffer)
return buffer.toString('hex')
} }
Transaction.prototype.clone = function () { Transaction.prototype.clone = function () {

6
src/wallet.js

@ -1,4 +1,5 @@
var assert = require('assert') var assert = require('assert')
var bufferutils = require('./bufferutils')
var crypto = require('crypto') var crypto = require('crypto')
var networks = require('./networks') var networks = require('./networks')
@ -123,10 +124,7 @@ Wallet.prototype.__processTx = function(tx, isPending) {
tx.ins.forEach(function(txIn, i) { tx.ins.forEach(function(txIn, i) {
// copy and convert to big-endian hex // copy and convert to big-endian hex
var txinId = new Buffer(txIn.hash) var txinId = bufferutils.reverse(txIn.hash).toString('hex')
Array.prototype.reverse.call(txinId)
txinId = txinId.toString('hex')
var output = txinId + ':' + txIn.index var output = txinId + ':' + txIn.index
if (!(output in this.outputs)) return if (!(output in this.outputs)) return

13
test/bufferutils.js

@ -75,6 +75,19 @@ describe('bufferutils', function() {
}) })
}) })
describe('reverse', function() {
fixtures.valid.forEach(function(f) {
it('reverses ' + f.hex64 + ' correctly', function() {
var buffer = new Buffer(f.hex64, 'hex')
var buffer2 = bufferutils.reverse(buffer)
Array.prototype.reverse.call(buffer)
assert.deepEqual(buffer, buffer2)
})
})
})
describe('varIntSize', function() { describe('varIntSize', function() {
fixtures.valid.forEach(function(f) { fixtures.valid.forEach(function(f) {
it('determines the varIntSize of ' + f.dec + ' correctly', function() { it('determines the varIntSize of ' + f.dec + ' correctly', function() {

Loading…
Cancel
Save