Browse Source

wallet exposes unspent outputs via a getter

also add reverseEndian method to convert
hk-custom-address
Wei Lu 11 years ago
parent
commit
16dc68cbaa
  1. 7
      src/convert.js
  2. 20
      src/wallet.js
  3. 9
      test/convert.js
  4. 39
      test/wallet.js

7
src/convert.js

@ -158,6 +158,10 @@ function wordArrayToBytes(wordArray) {
return wordsToBytes(wordArray.words)
}
function reverseEndian (hex) {
return bytesToHex(hexToBytes(hex).reverse())
}
module.exports = {
lpad: lpad,
bytesToHex: bytesToHex,
@ -175,5 +179,6 @@ module.exports = {
bytesToWords: bytesToWords,
wordsToBytes: wordsToBytes,
bytesToWordArray: bytesToWordArray,
wordArrayToBytes: wordArrayToBytes
wordArrayToBytes: wordArrayToBytes,
reverseEndian: reverseEndian
}

20
src/wallet.js

@ -60,6 +60,26 @@ var Wallet = function (seed, options) {
return this.changeAddresses[this.changeAddresses.length - 1]
}
this.getUnspentOutputs = function() {
var utxo = []
for(var key in this.outputs){
var hashAndIndex = key.split(":")
var output = this.outputs[key]
utxo.push({
hash: hashAndIndex[0],
hashLittleEndian: convert.reverseEndian(hashAndIndex[0]),
outputIndex: parseInt(hashAndIndex[1]),
scriptPubKey: output.scriptPubKey,
address: output.address,
value: output.value
})
}
return utxo
}
// Processes a transaction object
// If "verified" is true, then we trust the transaction as "final"
this.processTx = function(tx, verified) {

9
test/convert.js

@ -93,4 +93,13 @@ describe('convert', function() {
}
})
})
describe('reverseEndian', function() {
it('works', function() {
var bigEndian = "6a4062273ac4f9ea4ffca52d9fd102b08f6c32faa0a4d1318e3a7b2e437bb9c7"
var littleEdian = "c7b97b432e7b3a8e31d1a4a0fa326c8fb002d19f2da5fc4feaf9c43a2762406a"
assert.deepEqual(convert.reverseEndian(bigEndian), littleEdian)
assert.deepEqual(convert.reverseEndian(littleEdian), bigEndian)
})
})
})

39
test/wallet.js

@ -6,17 +6,13 @@ var SHA256 = require('crypto-js/sha256')
var Crypto = require('crypto-js')
describe('Wallet', function() {
var seed;
var seed, wallet;
beforeEach(function(){
seed = convert.wordArrayToBytes(SHA256("don't use a string seed like this in real life"))
wallet = new Wallet(seed)
})
describe('constructor', function() {
var wallet;
beforeEach(function() {
wallet = new Wallet(seed)
})
it('defaults to Bitcoin mainnet', function() {
assert.equal(wallet.getMasterKey().network, 'mainnet')
})
@ -47,7 +43,6 @@ describe('Wallet', function() {
})
describe('constructor options', function() {
var wallet;
beforeEach(function() {
wallet = new Wallet(seed, {network: 'testnet'})
})
@ -149,6 +144,36 @@ describe('Wallet', function() {
})
})
describe('Unspent Outputs', function(){
var expectedUtxo, expectedOutputKey;
beforeEach(function(){
expectedUtxo = [
{
"hash":"6a4062273ac4f9ea4ffca52d9fd102b08f6c32faa0a4d1318e3a7b2e437bb9c7",
"hashLittleEndian":"c7b97b432e7b3a8e31d1a4a0fa326c8fb002d19f2da5fc4feaf9c43a2762406a",
"outputIndex": 0,
"scriptPubKey":"76a91468edf28474ee22f68dfe7e56e76c017c1701b84f88ac",
"address" : "1azpkpcfczkduetfbqul4mokqai3m3hmxv",
"value": 20000
}
]
expectedOutputKey = expectedUtxo[0].hash + ":" + expectedUtxo[0].outputIndex
})
describe('getUnspentOutputs', function(){
it('parses wallet outputs to the expect format', function(){
wallet.outputs[expectedOutputKey] = {
output: expectedOutputKey,
scriptPubKey: expectedUtxo[0].scriptPubKey,
address: expectedUtxo[0].address,
value: expectedUtxo[0].value
}
assert.deepEqual(wallet.getUnspentOutputs(), expectedUtxo)
})
})
})
function assertEqual(obj1, obj2){
assert.equal(obj1.toString(), obj2.toString())
}

Loading…
Cancel
Save