Transaction/Script: bitcoin network no longer implied

A Transaction (and its subsequent scripts) do not carry any network
specific information in the Bitcoin protocol.
Therefore they can not (without further context) produce the network
specific constants for the generation of the base58 Addresses.

As TransactionOut.address is used heavily throughout Wallet and other
areas of the library, this could not be entirely removed without a large
number of changes.
For now, TransactionOut.address is only defined in the case of
Tx.addOutput being used directly:

      Transaction.addOutput(address, value)
This commit is contained in:
Daniel Cousens 2014-05-06 16:52:31 +10:00
parent 4207a0df99
commit 708aa03390
6 changed files with 47 additions and 142 deletions

View file

@ -1,8 +1,9 @@
var Address = require('./address')
var convert = require('./convert')
var Transaction = require('./transaction').Transaction
var HDNode = require('./hdwallet.js')
var rng = require('secure-random')
var networks = require('./networks')
var rng = require('secure-random')
var Transaction = require('./transaction').Transaction
function Wallet(seed, options) {
if (!(this instanceof Wallet)) { return new Wallet(seed, options); }
@ -42,7 +43,6 @@ function Wallet(seed, options) {
}
this.newMasterKey(seed, network)
this.generateAddress = function() {
var key = externalAccount.derive(this.addresses.length)
this.addresses.push(key.getAddress().toString())
@ -150,9 +150,17 @@ function Wallet(seed, options) {
var txhash = tx.getHash()
tx.outs.forEach(function(txOut, i){
var address = txOut.address.toString()
var address
try {
address = Address.fromScriptPubKey(txOut.script, networks[network]).toString()
} catch(e) {
if (!(e instanceof Address.Error)) throw e
}
if (isMyAddress(address)) {
var output = txhash+':'+i
var output = txhash + ':' + i
me.outputs[output] = {
receive: output,
value: txOut.value,
@ -165,7 +173,7 @@ function Wallet(seed, options) {
var op = txIn.outpoint
var o = me.outputs[op.hash+':'+op.index]
if (o) {
o.spend = txhash+':'+i
o.spend = txhash + ':' +i
}
})
}
@ -174,7 +182,7 @@ function Wallet(seed, options) {
checkDust(value)
var tx = new Transaction()
tx.addOutput(to, value, networks[network])
tx.addOutput(to, value)
var utxo = getCandidateOutputs(value)
var totalInValue = 0
@ -190,7 +198,7 @@ function Wallet(seed, options) {
var change = totalInValue - value - fee
if(change > 0 && !isDust(change)) {
tx.addOutput(changeAddress || getChangeAddress(), change, networks[network])
tx.addOutput(changeAddress || getChangeAddress(), change)
}
break
}
@ -246,7 +254,7 @@ function Wallet(seed, options) {
function estimateFeePadChangeOutput(tx){
var tmpTx = tx.clone()
tmpTx.addOutput(getChangeAddress(), 0, networks[network])
tmpTx.addOutput(getChangeAddress(), 0)
return tmpTx.estimateFee()
}
@ -266,7 +274,7 @@ function Wallet(seed, options) {
tx.ins.forEach(function(inp,i) {
var output = me.outputs[inp.outpoint.hash + ':' + inp.outpoint.index]
if (output) {
tx.sign(i, me.getPrivateKeyForAddress(output.address), false, networks[network])
tx.sign(i, me.getPrivateKeyForAddress(output.address), false)
}
})
return tx