Browse Source

Wallet: revert 2f00c9a

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

96
src/wallet.js

@ -8,7 +8,7 @@ var HDNode = require('./hdnode')
var TransactionBuilder = require('./transaction_builder')
var Script = require('./script')
function Wallet(seed, network, unspents) {
function Wallet(seed, network) {
seed = seed || crypto.randomBytes(32)
network = network || networks.bitcoin
@ -24,7 +24,7 @@ function Wallet(seed, network, unspents) {
this.addresses = []
this.changeAddresses = []
this.network = network
this.outputs = unspents ? processUnspentOutputs(unspents) : {}
this.outputs = {}
// FIXME: remove in 2.x.y
var me = this
@ -202,63 +202,35 @@ Wallet.prototype.getReceiveAddress = function() {
}
Wallet.prototype.getUnspentOutputs = function() {
var utxo = []
var utxos = []
for(var key in this.outputs){
for (var key in this.outputs) {
var output = this.outputs[key]
// Don't include pending spent outputs
if (!output.spent) {
utxo.push(outputToUnspentOutput(output))
// hash is little-endian, we want big-endian
var txid = bufferutils.reverse(output.hash)
utxos.push({
hash: txid.toString('hex'),
index: output.index,
address: output.address,
value: output.value,
pending: output.pending
})
}
}
return utxo
return utxos
}
Wallet.prototype.setUnspentOutputs = function(utxo) {
console.warn('setUnspentOutputs is deprecated, please use the constructor option instead')
Wallet.prototype.setUnspentOutputs = function(utxos) {
utxos.forEach(function(utxo) {
var txid = utxo.hash
assert.equal(typeof txid, 'string', 'Expected txId, got ' + txid)
this.outputs = processUnspentOutputs(utxo)
}
Wallet.prototype.signWith = function(txb, addresses) {
addresses.forEach(function(address, i) {
var privKey = this.getPrivateKeyForAddress(address)
txb.sign(i, privKey)
}, this)
return txb
}
function outputToUnspentOutput(output) {
var txid = new Buffer(output.hash)
// hash is little-endian, we want big-endian
Array.prototype.reverse.call(txid)
return {
hash: txid.toString('hex'),
index: output.index,
address: output.address,
value: output.value,
pending: output.pending
}
}
function estimatePaddedFee(tx, network) {
var tmpTx = tx.clone()
tmpTx.addOutput(Script.EMPTY, network.dustSoftThreshold || 0)
return network.estimateFee(tmpTx)
}
function processUnspentOutputs(utxos) {
var outputs = {}
utxos.forEach(function(utxo){
var hash = new Buffer(utxo.hash, 'hex')
var hash = bufferutils.reverse(new Buffer(txid, 'hex'))
var index = utxo.index
var address = utxo.address
var value = utxo.value
@ -271,21 +243,33 @@ function processUnspentOutputs(utxos) {
assert.doesNotThrow(function() { Address.fromBase58Check(address) }, 'Expected Base58 Address, got ' + address)
assert.equal(typeof value, 'number', 'Expected number value, got ' + value)
var key = utxo.hash + ':' + utxo.index
var key = txid + ':' + index
// little-endian hash is what we use internally
Array.prototype.reverse(hash)
outputs[key] = {
this.outputs[key] = {
address: address,
hash: hash,
index: utxo.index,
index: index,
pending: utxo.pending,
value: value
}
})
}, this)
}
Wallet.prototype.signWith = function(tx, addresses) {
addresses.forEach(function(address, i) {
var privKey = this.getPrivateKeyForAddress(address)
return outputs
tx.sign(i, privKey)
}, this)
return tx
}
function estimatePaddedFee(tx, network) {
var tmpTx = tx.clone()
tmpTx.addOutput(Script.EMPTY, network.dustSoftThreshold || 0)
return network.estimateFee(tmpTx)
}
function getCandidateOutputs(outputs/*, value*/) {

15
test/wallet.js

@ -212,7 +212,8 @@ describe('Wallet', function() {
describe('on construction', function() {
beforeEach(function() {
wallet = new Wallet(seed, networks.bitcoin, [utxo])
wallet = new Wallet(seed, networks.bitcoin)
wallet.setUnspentOutputs([utxo])
})
it('matches the expected behaviour', function() {
@ -229,7 +230,8 @@ describe('Wallet', function() {
var utxo1 = cloneObject(utxo)
utxo1.hash = fakeTxId(5)
wallet = new Wallet(seed, networks.bitcoin, [utxo, utxo1])
wallet = new Wallet(seed, networks.bitcoin)
wallet.setUnspentOutputs([utxo, utxo1])
})
it('sums over utxo values', function() {
@ -239,7 +241,8 @@ describe('Wallet', function() {
describe('getUnspentOutputs', function() {
beforeEach(function() {
wallet = new Wallet(seed, networks.bitcoin, [utxo])
wallet = new Wallet(seed, networks.bitcoin)
wallet.setUnspentOutputs([utxo])
})
it('parses wallet outputs to the expected format', function() {
@ -468,7 +471,8 @@ describe('Wallet', function() {
}
]
wallet = new Wallet(seed, networks.testnet, utxos)
wallet = new Wallet(seed, networks.testnet)
wallet.setUnspentOutputs(utxos)
wallet.generateAddress()
wallet.generateAddress()
})
@ -497,7 +501,8 @@ describe('Wallet', function() {
value: 500000
}
var wallet = new Wallet(seed, networks.litecoin, [utxo])
var wallet = new Wallet(seed, networks.litecoin)
wallet.setUnspentOutputs([utxo])
wallet.generateAddress()
value = 200000

Loading…
Cancel
Save