|
|
@ -25,6 +25,8 @@ function Wallet(seed, network, unspents) { |
|
|
|
this.addresses = [] |
|
|
|
this.changeAddresses = [] |
|
|
|
|
|
|
|
this.network = network |
|
|
|
|
|
|
|
// Transaction output data
|
|
|
|
this.outputs = unspents ? processUnspentOutputs(unspents) : {} |
|
|
|
|
|
|
@ -100,8 +102,42 @@ function Wallet(seed, network, unspents) { |
|
|
|
}) |
|
|
|
} |
|
|
|
|
|
|
|
this.createTx = function(to, value, fixedFee, changeAddress) { |
|
|
|
assert(value > network.dustThreshold, value + ' must be above dust threshold (' + network.dustThreshold + ' Satoshis)') |
|
|
|
this.getMasterKey = function() { return masterkey } |
|
|
|
this.getAccountZero = function() { return accountZero } |
|
|
|
this.getInternalAccount = function() { return internalAccount } |
|
|
|
this.getExternalAccount = function() { return externalAccount } |
|
|
|
|
|
|
|
this.getPrivateKeyForAddress = function(address) { |
|
|
|
assert(isMyAddress(address), 'Unknown address. Make sure the address is from the keychain and has been generated') |
|
|
|
|
|
|
|
if (isReceiveAddress(address)) { |
|
|
|
var index = this.addresses.indexOf(address) |
|
|
|
|
|
|
|
return this.getPrivateKey(index) |
|
|
|
} |
|
|
|
|
|
|
|
if (isChangeAddress(address)) { |
|
|
|
var index = this.changeAddresses.indexOf(address) |
|
|
|
|
|
|
|
return this.getInternalPrivateKey(index) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
function isReceiveAddress(address){ |
|
|
|
return me.addresses.indexOf(address) > -1 |
|
|
|
} |
|
|
|
|
|
|
|
function isChangeAddress(address){ |
|
|
|
return me.changeAddresses.indexOf(address) > -1 |
|
|
|
} |
|
|
|
|
|
|
|
function isMyAddress(address) { |
|
|
|
return isReceiveAddress(address) || isChangeAddress(address) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
Wallet.prototype.createTx = function(to, value, fixedFee, changeAddress) { |
|
|
|
assert(value > this.network.dustThreshold, value + ' must be above dust threshold (' + this.network.dustThreshold + ' Satoshis)') |
|
|
|
|
|
|
|
var utxos = getCandidateOutputs(this.outputs, value) |
|
|
|
var accum = 0 |
|
|
@ -118,14 +154,14 @@ function Wallet(seed, network, unspents) { |
|
|
|
var outpoint = utxo.from.split(':') |
|
|
|
tx.addInput(outpoint[0], parseInt(outpoint[1])) |
|
|
|
|
|
|
|
var fee = fixedFee == undefined ? estimatePaddedFee(tx, network) : fixedFee |
|
|
|
var fee = fixedFee == undefined ? estimatePaddedFee(tx, this.network) : fixedFee |
|
|
|
|
|
|
|
accum += utxo.value |
|
|
|
subTotal = value + fee |
|
|
|
if (accum >= subTotal) { |
|
|
|
var change = accum - subTotal |
|
|
|
|
|
|
|
if (change > network.dustThreshold) { |
|
|
|
if (change > this.network.dustThreshold) { |
|
|
|
tx.addOutput(changeAddress || this.getChangeAddress(), change) |
|
|
|
} |
|
|
|
|
|
|
@ -139,48 +175,6 @@ function Wallet(seed, network, unspents) { |
|
|
|
return tx |
|
|
|
} |
|
|
|
|
|
|
|
this.getMasterKey = function() { return masterkey } |
|
|
|
this.getAccountZero = function() { return accountZero } |
|
|
|
this.getInternalAccount = function() { return internalAccount } |
|
|
|
this.getExternalAccount = function() { return externalAccount } |
|
|
|
|
|
|
|
this.getPrivateKey = function(index) { |
|
|
|
return externalAccount.derive(index).privKey |
|
|
|
} |
|
|
|
|
|
|
|
this.getInternalPrivateKey = function(index) { |
|
|
|
return internalAccount.derive(index).privKey |
|
|
|
} |
|
|
|
|
|
|
|
this.getPrivateKeyForAddress = function(address) { |
|
|
|
assert(isMyAddress(address), 'Unknown address. Make sure the address is from the keychain and has been generated') |
|
|
|
|
|
|
|
if (isReceiveAddress(address)) { |
|
|
|
var index = this.addresses.indexOf(address) |
|
|
|
|
|
|
|
return this.getPrivateKey(index) |
|
|
|
} |
|
|
|
|
|
|
|
if (isChangeAddress(address)) { |
|
|
|
var index = this.changeAddresses.indexOf(address) |
|
|
|
|
|
|
|
return this.getInternalPrivateKey(index) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
function isReceiveAddress(address){ |
|
|
|
return me.addresses.indexOf(address) > -1 |
|
|
|
} |
|
|
|
|
|
|
|
function isChangeAddress(address){ |
|
|
|
return me.changeAddresses.indexOf(address) > -1 |
|
|
|
} |
|
|
|
|
|
|
|
function isMyAddress(address) { |
|
|
|
return isReceiveAddress(address) || isChangeAddress(address) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
Wallet.prototype.generateAddress = function() { |
|
|
|
var k = this.addresses.length |
|
|
|
var address = this.getExternalAccount().derive(k).getAddress() |
|
|
@ -213,6 +207,14 @@ Wallet.prototype.getChangeAddress = function() { |
|
|
|
return this.changeAddresses[this.changeAddresses.length - 1] |
|
|
|
} |
|
|
|
|
|
|
|
Wallet.prototype.getInternalPrivateKey = function(index) { |
|
|
|
return this.getInternalAccount().derive(index).privKey |
|
|
|
} |
|
|
|
|
|
|
|
Wallet.prototype.getPrivateKey = function(index) { |
|
|
|
return this.getExternalAccount().derive(index).privKey |
|
|
|
} |
|
|
|
|
|
|
|
Wallet.prototype.getReceiveAddress = function() { |
|
|
|
if (this.addresses.length === 0) { |
|
|
|
this.generateAddress() |
|
|
|