Wallet: move getPrivateKeyForAddress to prototype
also, move the assert to the end to simplify the logic
This commit is contained in:
parent
2b4d94cd92
commit
00d58604a4
1 changed files with 16 additions and 18 deletions
|
@ -53,24 +53,6 @@ function Wallet(seed, network, unspents) {
|
|||
this.getAccountZero = function() { return accountZero }
|
||||
this.getExternalAccount = function() { return externalAccount }
|
||||
this.getInternalAccount = function() { return internalAccount }
|
||||
|
||||
this.getPrivateKeyForAddress = function(address) {
|
||||
var myAddresses = this.addresses.concat(this.changeAddresses)
|
||||
assert(includeAddress(myAddresses, address),
|
||||
'Unknown address. Make sure the address is from the keychain and has been generated')
|
||||
|
||||
if (includeAddress(this.addresses, address)) {
|
||||
var index = this.addresses.indexOf(address)
|
||||
|
||||
return this.getPrivateKey(index)
|
||||
}
|
||||
|
||||
if (includeAddress(this.changeAddresses, address)) {
|
||||
var index = this.changeAddresses.indexOf(address)
|
||||
|
||||
return this.getInternalPrivateKey(index)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Wallet.prototype.createTx = function(to, value, fixedFee, changeAddress) {
|
||||
|
@ -160,6 +142,22 @@ Wallet.prototype.getPrivateKey = function(index) {
|
|||
return this.getExternalAccount().derive(index).privKey
|
||||
}
|
||||
|
||||
Wallet.prototype.getPrivateKeyForAddress = function(address) {
|
||||
if (includeAddress(this.addresses, address)) {
|
||||
var index = this.addresses.indexOf(address)
|
||||
|
||||
return this.getPrivateKey(index)
|
||||
}
|
||||
|
||||
if (includeAddress(this.changeAddresses, address)) {
|
||||
var index = this.changeAddresses.indexOf(address)
|
||||
|
||||
return this.getInternalPrivateKey(index)
|
||||
}
|
||||
|
||||
assert(false, 'Unknown address. Make sure the address is from the keychain and has been generated')
|
||||
}
|
||||
|
||||
Wallet.prototype.getReceiveAddress = function() {
|
||||
if (this.addresses.length === 0) {
|
||||
this.generateAddress()
|
||||
|
|
Loading…
Add table
Reference in a new issue