wallet exposes unspent outputs via a getter
also add reverseEndian method to convert
This commit is contained in:
parent
68e8834c66
commit
16dc68cbaa
4 changed files with 67 additions and 8 deletions
|
@ -158,6 +158,10 @@ function wordArrayToBytes(wordArray) {
|
||||||
return wordsToBytes(wordArray.words)
|
return wordsToBytes(wordArray.words)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function reverseEndian (hex) {
|
||||||
|
return bytesToHex(hexToBytes(hex).reverse())
|
||||||
|
}
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
lpad: lpad,
|
lpad: lpad,
|
||||||
bytesToHex: bytesToHex,
|
bytesToHex: bytesToHex,
|
||||||
|
@ -175,5 +179,6 @@ module.exports = {
|
||||||
bytesToWords: bytesToWords,
|
bytesToWords: bytesToWords,
|
||||||
wordsToBytes: wordsToBytes,
|
wordsToBytes: wordsToBytes,
|
||||||
bytesToWordArray: bytesToWordArray,
|
bytesToWordArray: bytesToWordArray,
|
||||||
wordArrayToBytes: wordArrayToBytes
|
wordArrayToBytes: wordArrayToBytes,
|
||||||
|
reverseEndian: reverseEndian
|
||||||
}
|
}
|
||||||
|
|
|
@ -60,6 +60,26 @@ var Wallet = function (seed, options) {
|
||||||
return this.changeAddresses[this.changeAddresses.length - 1]
|
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
|
// Processes a transaction object
|
||||||
// If "verified" is true, then we trust the transaction as "final"
|
// If "verified" is true, then we trust the transaction as "final"
|
||||||
this.processTx = function(tx, verified) {
|
this.processTx = function(tx, verified) {
|
||||||
|
|
|
@ -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)
|
||||||
|
})
|
||||||
|
})
|
||||||
})
|
})
|
||||||
|
|
|
@ -6,17 +6,13 @@ var SHA256 = require('crypto-js/sha256')
|
||||||
var Crypto = require('crypto-js')
|
var Crypto = require('crypto-js')
|
||||||
|
|
||||||
describe('Wallet', function() {
|
describe('Wallet', function() {
|
||||||
var seed;
|
var seed, wallet;
|
||||||
beforeEach(function(){
|
beforeEach(function(){
|
||||||
seed = convert.wordArrayToBytes(SHA256("don't use a string seed like this in real life"))
|
seed = convert.wordArrayToBytes(SHA256("don't use a string seed like this in real life"))
|
||||||
|
wallet = new Wallet(seed)
|
||||||
})
|
})
|
||||||
|
|
||||||
describe('constructor', function() {
|
describe('constructor', function() {
|
||||||
var wallet;
|
|
||||||
beforeEach(function() {
|
|
||||||
wallet = new Wallet(seed)
|
|
||||||
})
|
|
||||||
|
|
||||||
it('defaults to Bitcoin mainnet', function() {
|
it('defaults to Bitcoin mainnet', function() {
|
||||||
assert.equal(wallet.getMasterKey().network, 'mainnet')
|
assert.equal(wallet.getMasterKey().network, 'mainnet')
|
||||||
})
|
})
|
||||||
|
@ -47,7 +43,6 @@ describe('Wallet', function() {
|
||||||
})
|
})
|
||||||
|
|
||||||
describe('constructor options', function() {
|
describe('constructor options', function() {
|
||||||
var wallet;
|
|
||||||
beforeEach(function() {
|
beforeEach(function() {
|
||||||
wallet = new Wallet(seed, {network: 'testnet'})
|
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){
|
function assertEqual(obj1, obj2){
|
||||||
assert.equal(obj1.toString(), obj2.toString())
|
assert.equal(obj1.toString(), obj2.toString())
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue