Wallet: revert 2f00c9a
This commit is contained in:
parent
71d4c78b88
commit
02e71e430c
2 changed files with 50 additions and 61 deletions
|
@ -8,7 +8,7 @@ var HDNode = require('./hdnode')
|
||||||
var TransactionBuilder = require('./transaction_builder')
|
var TransactionBuilder = require('./transaction_builder')
|
||||||
var Script = require('./script')
|
var Script = require('./script')
|
||||||
|
|
||||||
function Wallet(seed, network, unspents) {
|
function Wallet(seed, network) {
|
||||||
seed = seed || crypto.randomBytes(32)
|
seed = seed || crypto.randomBytes(32)
|
||||||
network = network || networks.bitcoin
|
network = network || networks.bitcoin
|
||||||
|
|
||||||
|
@ -24,7 +24,7 @@ function Wallet(seed, network, unspents) {
|
||||||
this.addresses = []
|
this.addresses = []
|
||||||
this.changeAddresses = []
|
this.changeAddresses = []
|
||||||
this.network = network
|
this.network = network
|
||||||
this.outputs = unspents ? processUnspentOutputs(unspents) : {}
|
this.outputs = {}
|
||||||
|
|
||||||
// FIXME: remove in 2.x.y
|
// FIXME: remove in 2.x.y
|
||||||
var me = this
|
var me = this
|
||||||
|
@ -202,63 +202,35 @@ Wallet.prototype.getReceiveAddress = function() {
|
||||||
}
|
}
|
||||||
|
|
||||||
Wallet.prototype.getUnspentOutputs = 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]
|
var output = this.outputs[key]
|
||||||
|
|
||||||
// Don't include pending spent outputs
|
// Don't include pending spent outputs
|
||||||
if (!output.spent) {
|
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) {
|
Wallet.prototype.setUnspentOutputs = function(utxos) {
|
||||||
console.warn('setUnspentOutputs is deprecated, please use the constructor option instead')
|
utxos.forEach(function(utxo) {
|
||||||
|
var txid = utxo.hash
|
||||||
|
assert.equal(typeof txid, 'string', 'Expected txId, got ' + txid)
|
||||||
|
|
||||||
this.outputs = processUnspentOutputs(utxo)
|
var hash = bufferutils.reverse(new Buffer(txid, 'hex'))
|
||||||
}
|
|
||||||
|
|
||||||
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 index = utxo.index
|
var index = utxo.index
|
||||||
var address = utxo.address
|
var address = utxo.address
|
||||||
var value = utxo.value
|
var value = utxo.value
|
||||||
|
@ -271,21 +243,33 @@ function processUnspentOutputs(utxos) {
|
||||||
assert.doesNotThrow(function() { Address.fromBase58Check(address) }, 'Expected Base58 Address, got ' + address)
|
assert.doesNotThrow(function() { Address.fromBase58Check(address) }, 'Expected Base58 Address, got ' + address)
|
||||||
assert.equal(typeof value, 'number', 'Expected number value, got ' + value)
|
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
|
this.outputs[key] = {
|
||||||
Array.prototype.reverse(hash)
|
|
||||||
|
|
||||||
outputs[key] = {
|
|
||||||
address: address,
|
address: address,
|
||||||
hash: hash,
|
hash: hash,
|
||||||
index: utxo.index,
|
index: index,
|
||||||
pending: utxo.pending,
|
pending: utxo.pending,
|
||||||
value: value
|
value: value
|
||||||
}
|
}
|
||||||
})
|
}, this)
|
||||||
|
}
|
||||||
|
|
||||||
return outputs
|
Wallet.prototype.signWith = function(tx, addresses) {
|
||||||
|
addresses.forEach(function(address, i) {
|
||||||
|
var privKey = this.getPrivateKeyForAddress(address)
|
||||||
|
|
||||||
|
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*/) {
|
function getCandidateOutputs(outputs/*, value*/) {
|
||||||
|
|
|
@ -212,7 +212,8 @@ describe('Wallet', function() {
|
||||||
|
|
||||||
describe('on construction', function() {
|
describe('on construction', function() {
|
||||||
beforeEach(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() {
|
it('matches the expected behaviour', function() {
|
||||||
|
@ -229,7 +230,8 @@ describe('Wallet', function() {
|
||||||
var utxo1 = cloneObject(utxo)
|
var utxo1 = cloneObject(utxo)
|
||||||
utxo1.hash = fakeTxId(5)
|
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() {
|
it('sums over utxo values', function() {
|
||||||
|
@ -239,7 +241,8 @@ describe('Wallet', function() {
|
||||||
|
|
||||||
describe('getUnspentOutputs', function() {
|
describe('getUnspentOutputs', function() {
|
||||||
beforeEach(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() {
|
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()
|
||||||
wallet.generateAddress()
|
wallet.generateAddress()
|
||||||
})
|
})
|
||||||
|
@ -497,7 +501,8 @@ describe('Wallet', function() {
|
||||||
value: 500000
|
value: 500000
|
||||||
}
|
}
|
||||||
|
|
||||||
var wallet = new Wallet(seed, networks.litecoin, [utxo])
|
var wallet = new Wallet(seed, networks.litecoin)
|
||||||
|
wallet.setUnspentOutputs([utxo])
|
||||||
wallet.generateAddress()
|
wallet.generateAddress()
|
||||||
|
|
||||||
value = 200000
|
value = 200000
|
||||||
|
|
Loading…
Add table
Reference in a new issue