Browse Source

Wallet: remove use of hashLittleEndian

hk-custom-address
Daniel Cousens 11 years ago
parent
commit
2df790e2ab
  1. 15
      src/wallet.js
  2. 28
      test/wallet.js

15
src/wallet.js

@ -1,5 +1,4 @@
var Address = require('./address') var Address = require('./address')
var convert = require('./convert')
var HDNode = require('./hdwallet.js') var HDNode = require('./hdwallet.js')
var networks = require('./networks') var networks = require('./networks')
var rng = require('secure-random') var rng = require('secure-random')
@ -92,7 +91,6 @@ function Wallet(seed, options) {
return { return {
hash: hashAndIndex[0], hash: hashAndIndex[0],
hashLittleEndian: convert.reverseEndian(hashAndIndex[0]),
outputIndex: parseInt(hashAndIndex[1]), outputIndex: parseInt(hashAndIndex[1]),
address: output.address, address: output.address,
value: output.value value: output.value
@ -100,7 +98,7 @@ function Wallet(seed, options) {
} }
function unspentOutputToOutput(o) { function unspentOutputToOutput(o) {
var hash = o.hash || convert.reverseEndian(o.hashLittleEndian) var hash = o.hash
var key = hash + ":" + o.outputIndex var key = hash + ":" + o.outputIndex
return { return {
receive: key, receive: key,
@ -112,8 +110,8 @@ function Wallet(seed, options) {
function validateUnspentOutput(uo) { function validateUnspentOutput(uo) {
var missingField var missingField
if (isNullOrUndefined(uo.hash) && isNullOrUndefined(uo.hashLittleEndian)) { if (isNullOrUndefined(uo.hash)) {
missingField = "hash(or hashLittleEndian)" missingField = "hash"
} }
var requiredKeys = ['outputIndex', 'address', 'value'] var requiredKeys = ['outputIndex', 'address', 'value']
@ -129,7 +127,7 @@ function Wallet(seed, options) {
'A valid unspent output must contain' 'A valid unspent output must contain'
] ]
message.push(requiredKeys.join(', ')) message.push(requiredKeys.join(', '))
message.push("and hash(or hashLittleEndian)") message.push("and hash")
throw new Error(message.join(' ')) throw new Error(message.join(' '))
} }
} }
@ -163,9 +161,10 @@ function Wallet(seed, options) {
tx.ins.forEach(function(txIn, i){ tx.ins.forEach(function(txIn, i){
var op = txIn.outpoint var op = txIn.outpoint
var o = me.outputs[op.hash+':'+op.index]
var o = me.outputs[op.hash + ':' + op.index]
if (o) { if (o) {
o.spend = txhash + ':' +i o.spend = txhash + ':' + i
} }
}) })
} }

28
test/wallet.js

@ -168,7 +168,6 @@ describe('Wallet', function() {
beforeEach(function(){ beforeEach(function(){
expectedUtxo = { expectedUtxo = {
"hash":"6a4062273ac4f9ea4ffca52d9fd102b08f6c32faa0a4d1318e3a7b2e437bb9c7", "hash":"6a4062273ac4f9ea4ffca52d9fd102b08f6c32faa0a4d1318e3a7b2e437bb9c7",
"hashLittleEndian":"c7b97b432e7b3a8e31d1a4a0fa326c8fb002d19f2da5fc4feaf9c43a2762406a",
"outputIndex": 0, "outputIndex": 0,
"address" : "1AZpKpcfCzKDUeTFBQUL4MokQai3m3HMXv", "address" : "1AZpKpcfCzKDUeTFBQUL4MokQai3m3HMXv",
"value": 20000 "value": 20000
@ -230,36 +229,13 @@ describe('Wallet', function() {
utxo = cloneObject([expectedUtxo]) utxo = cloneObject([expectedUtxo])
}) })
it('uses hashLittleEndian when hash is not present', function(){ it('matches the expected behaviour', function(){
delete utxo[0]['hash']
wallet.setUnspentOutputs(utxo)
verifyOutputs()
})
it('uses hash when hashLittleEndian is not present', function(){
delete utxo[0]['hashLittleEndian']
wallet.setUnspentOutputs(utxo)
verifyOutputs()
})
it('uses hash when both hash and hashLittleEndian are present', function(){
wallet.setUnspentOutputs(utxo) wallet.setUnspentOutputs(utxo)
verifyOutputs() verifyOutputs()
}) })
describe('required fields', function(){ describe('required fields', function(){
it("throws an error when hash and hashLittleEndian are both missing", function(){ ['outputIndex', 'address', 'hash', 'value'].forEach(function(field){
delete utxo[0]['hash']
delete utxo[0]['hashLittleEndian']
assert.throws(function() {
wallet.setUnspentOutputs(utxo)
}, /Invalid unspent output: key hash\(or hashLittleEndian\) is missing/)
});
['outputIndex', 'address', 'value'].forEach(function(field){
it("throws an error when " + field + " is missing", function(){ it("throws an error when " + field + " is missing", function(){
delete utxo[0][field] delete utxo[0][field]

Loading…
Cancel
Save