Wallet: move most instance methods to prototype
This commit is contained in:
parent
2f00c9ab35
commit
7fd41fae4a
1 changed files with 70 additions and 53 deletions
123
src/wallet.js
123
src/wallet.js
|
@ -28,35 +28,6 @@ function Wallet(seed, network, unspents) {
|
||||||
// Transaction output data
|
// Transaction output data
|
||||||
this.outputs = unspents ? processUnspentOutputs(unspents) : {}
|
this.outputs = unspents ? processUnspentOutputs(unspents) : {}
|
||||||
|
|
||||||
this.generateAddress = function() {
|
|
||||||
var key = externalAccount.derive(this.addresses.length)
|
|
||||||
this.addresses.push(key.getAddress().toString())
|
|
||||||
return this.addresses[this.addresses.length - 1]
|
|
||||||
}
|
|
||||||
|
|
||||||
this.generateChangeAddress = function() {
|
|
||||||
var key = internalAccount.derive(this.changeAddresses.length)
|
|
||||||
this.changeAddresses.push(key.getAddress().toString())
|
|
||||||
return this.changeAddresses[this.changeAddresses.length - 1]
|
|
||||||
}
|
|
||||||
|
|
||||||
this.getBalance = function() {
|
|
||||||
return this.getUnspentOutputs().reduce(function(memo, output){
|
|
||||||
return memo + output.value
|
|
||||||
}, 0)
|
|
||||||
}
|
|
||||||
|
|
||||||
this.getUnspentOutputs = function() {
|
|
||||||
var utxo = []
|
|
||||||
|
|
||||||
for(var key in this.outputs){
|
|
||||||
var output = this.outputs[key]
|
|
||||||
if(!output.to) utxo.push(outputToUnspentOutput(output))
|
|
||||||
}
|
|
||||||
|
|
||||||
return utxo
|
|
||||||
}
|
|
||||||
|
|
||||||
// FIXME: remove in 2.x.y
|
// FIXME: remove in 2.x.y
|
||||||
this.newMasterKey = function(seed) {
|
this.newMasterKey = function(seed) {
|
||||||
console.warn('newMasterKey is deprecated, please make a new Wallet instance instead')
|
console.warn('newMasterKey is deprecated, please make a new Wallet instance instead')
|
||||||
|
@ -76,12 +47,6 @@ function Wallet(seed, network, unspents) {
|
||||||
me.outputs = {}
|
me.outputs = {}
|
||||||
}
|
}
|
||||||
|
|
||||||
this.setUnspentOutputs = function(utxo) {
|
|
||||||
console.warn('setUnspentOutputs is deprecated, please use the constructor option instead')
|
|
||||||
|
|
||||||
this.outputs = processUnspentOutputs(utxo)
|
|
||||||
}
|
|
||||||
|
|
||||||
this.processPendingTx = function(tx){
|
this.processPendingTx = function(tx){
|
||||||
processTx(tx, true)
|
processTx(tx, true)
|
||||||
}
|
}
|
||||||
|
@ -161,7 +126,7 @@ function Wallet(seed, network, unspents) {
|
||||||
var change = accum - subTotal
|
var change = accum - subTotal
|
||||||
|
|
||||||
if (change > network.dustThreshold) {
|
if (change > network.dustThreshold) {
|
||||||
tx.addOutput(changeAddress || getChangeAddress(), change)
|
tx.addOutput(changeAddress || this.getChangeAddress(), change)
|
||||||
}
|
}
|
||||||
|
|
||||||
break
|
break
|
||||||
|
@ -174,23 +139,6 @@ function Wallet(seed, network, unspents) {
|
||||||
return tx
|
return tx
|
||||||
}
|
}
|
||||||
|
|
||||||
function getChangeAddress() {
|
|
||||||
if(me.changeAddresses.length === 0) me.generateChangeAddress();
|
|
||||||
return me.changeAddresses[me.changeAddresses.length - 1]
|
|
||||||
}
|
|
||||||
|
|
||||||
this.signWith = function(tx, addresses) {
|
|
||||||
assert.equal(tx.ins.length, addresses.length, 'Number of addresses must match number of transaction inputs')
|
|
||||||
|
|
||||||
addresses.forEach(function(address, i) {
|
|
||||||
var key = me.getPrivateKeyForAddress(address)
|
|
||||||
|
|
||||||
tx.sign(i, key)
|
|
||||||
})
|
|
||||||
|
|
||||||
return tx
|
|
||||||
}
|
|
||||||
|
|
||||||
this.getMasterKey = function() { return masterkey }
|
this.getMasterKey = function() { return masterkey }
|
||||||
this.getAccountZero = function() { return accountZero }
|
this.getAccountZero = function() { return accountZero }
|
||||||
this.getInternalAccount = function() { return internalAccount }
|
this.getInternalAccount = function() { return internalAccount }
|
||||||
|
@ -233,6 +181,75 @@ function Wallet(seed, network, unspents) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Wallet.prototype.generateAddress = function() {
|
||||||
|
var k = this.addresses.length
|
||||||
|
var address = this.getExternalAccount().derive(k).getAddress()
|
||||||
|
|
||||||
|
this.addresses.push(address.toString())
|
||||||
|
|
||||||
|
return this.getReceiveAddress()
|
||||||
|
}
|
||||||
|
|
||||||
|
Wallet.prototype.generateChangeAddress = function() {
|
||||||
|
var k = this.changeAddresses.length
|
||||||
|
var address = this.getInternalAccount().derive(k).getAddress()
|
||||||
|
|
||||||
|
this.changeAddresses.push(address.toString())
|
||||||
|
|
||||||
|
return this.getChangeAddress()
|
||||||
|
}
|
||||||
|
|
||||||
|
Wallet.prototype.getBalance = function() {
|
||||||
|
return this.getUnspentOutputs().reduce(function(accum, output) {
|
||||||
|
return accum + output.value
|
||||||
|
}, 0)
|
||||||
|
}
|
||||||
|
|
||||||
|
Wallet.prototype.getChangeAddress = function() {
|
||||||
|
if (this.changeAddresses.length === 0) {
|
||||||
|
this.generateChangeAddress()
|
||||||
|
}
|
||||||
|
|
||||||
|
return this.changeAddresses[this.changeAddresses.length - 1]
|
||||||
|
}
|
||||||
|
|
||||||
|
Wallet.prototype.getReceiveAddress = function() {
|
||||||
|
if (this.addresses.length === 0) {
|
||||||
|
this.generateAddress()
|
||||||
|
}
|
||||||
|
|
||||||
|
return this.addresses[this.addresses.length - 1]
|
||||||
|
}
|
||||||
|
|
||||||
|
Wallet.prototype.getUnspentOutputs = function() {
|
||||||
|
var utxo = []
|
||||||
|
|
||||||
|
for(var key in this.outputs){
|
||||||
|
var output = this.outputs[key]
|
||||||
|
if(!output.to) utxo.push(outputToUnspentOutput(output))
|
||||||
|
}
|
||||||
|
|
||||||
|
return utxo
|
||||||
|
}
|
||||||
|
|
||||||
|
Wallet.prototype.setUnspentOutputs = function(utxo) {
|
||||||
|
console.warn('setUnspentOutputs is deprecated, please use the constructor option instead')
|
||||||
|
|
||||||
|
this.outputs = processUnspentOutputs(utxo)
|
||||||
|
}
|
||||||
|
|
||||||
|
Wallet.prototype.signWith = function(tx, addresses) {
|
||||||
|
assert.equal(tx.ins.length, addresses.length, 'Number of addresses must match number of transaction inputs')
|
||||||
|
|
||||||
|
addresses.forEach(function(address, i) {
|
||||||
|
var key = this.getPrivateKeyForAddress(address)
|
||||||
|
|
||||||
|
tx.sign(i, key)
|
||||||
|
}, this)
|
||||||
|
|
||||||
|
return tx
|
||||||
|
}
|
||||||
|
|
||||||
function outputToUnspentOutput(output){
|
function outputToUnspentOutput(output){
|
||||||
var hashAndIndex = output.from.split(":")
|
var hashAndIndex = output.from.split(":")
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue