Merge pull request #172 from dcousens/ext159
Extends #159 to further verify behaviour
This commit is contained in:
commit
bd2a6db260
3 changed files with 60 additions and 25 deletions
|
@ -84,7 +84,7 @@ Transaction.prototype.addInput = function (tx, outIndex) {
|
|||
* Can be called with:
|
||||
*
|
||||
* i) An existing TransactionOut object
|
||||
* ii) An address object or an address and a value
|
||||
* ii) An address object or a string address, and a value
|
||||
* iii) An address:value string
|
||||
* iv) Either ii), iii) with an optional network argument
|
||||
*
|
||||
|
@ -96,23 +96,24 @@ Transaction.prototype.addOutput = function (address, value, network) {
|
|||
return
|
||||
}
|
||||
|
||||
if (arguments[0].indexOf(':') >= 0) {
|
||||
network = value
|
||||
if (typeof address === 'string') {
|
||||
if (arguments[0].indexOf(':') >= 0) {
|
||||
var args = arguments[0].split(':')
|
||||
address = args[0]
|
||||
value = parseInt(args[1])
|
||||
|
||||
var args = arguments[0].split(':')
|
||||
address = args[0]
|
||||
value = parseInt(args[1])
|
||||
network = arguments[1]
|
||||
}
|
||||
|
||||
address = Address.fromBase58Check(address)
|
||||
}
|
||||
|
||||
network = network || Network.bitcoin
|
||||
|
||||
if (typeof address === 'string') {
|
||||
address = Address.fromBase58Check(address)
|
||||
}
|
||||
|
||||
this.outs.push(new TransactionOut({
|
||||
value: value,
|
||||
script: Script.createOutputScript(address, network)
|
||||
script: Script.createOutputScript(address, network),
|
||||
network: network
|
||||
}))
|
||||
}
|
||||
|
||||
|
@ -485,7 +486,10 @@ var TransactionOut = function (data) {
|
|||
: data.address ? Script.createOutputScript(data.address)
|
||||
: new Script()
|
||||
|
||||
if (this.script.buffer.length > 0) this.address = this.script.getToAddress();
|
||||
var network = data.network || Network.bitcoin
|
||||
if (this.script.buffer.length > 0) {
|
||||
this.address = this.script.getToAddress(network)
|
||||
}
|
||||
|
||||
this.value =
|
||||
Array.isArray(data.value) ? convert.bytesToNum(data.value)
|
||||
|
|
|
@ -154,6 +154,11 @@ describe('Transaction', function() {
|
|||
verifyTransactionOut()
|
||||
})
|
||||
|
||||
it('allows an Address object and value to be passed in', function(){
|
||||
tx.addOutput(Address.fromBase58Check('15mMHKL96tWAUtqF3tbVf99Z8arcmnJrr3'), 40000)
|
||||
verifyTransactionOut()
|
||||
})
|
||||
|
||||
it('allows a string in the form of address:index to be passed in', function(){
|
||||
tx.addOutput("15mMHKL96tWAUtqF3tbVf99Z8arcmnJrr3:40000")
|
||||
verifyTransactionOut()
|
||||
|
@ -168,6 +173,15 @@ describe('Transaction', function() {
|
|||
verifyTransactionOut()
|
||||
})
|
||||
|
||||
it('supports alternative networks', function(){
|
||||
var addr = 'mkHJaNR7uuwRG1JrmTZsV4MszaTKjCBvCR'
|
||||
|
||||
tx.addOutput(addr, 40000, network.testnet)
|
||||
verifyTransactionOut()
|
||||
|
||||
assert.equal(tx.outs[0].address.toString(), addr)
|
||||
})
|
||||
|
||||
function verifyTransactionOut(){
|
||||
assert.equal(tx.outs.length, 1)
|
||||
|
||||
|
|
|
@ -141,10 +141,14 @@ describe('Wallet', function() {
|
|||
wallet.generateAddress()
|
||||
wallet.generateAddress()
|
||||
|
||||
assertEqual(wallet.getPrivateKeyForAddress("n2fiWrHqD6GM5GiEqkbWAc6aaZQp3ba93X"),
|
||||
wallet.getExternalAccount().derive(1).priv)
|
||||
assertEqual(wallet.getPrivateKeyForAddress("mnXiDR4MKsFxcKJEZjx4353oXvo55iuptn"),
|
||||
wallet.getInternalAccount().derive(0).priv)
|
||||
assertEqual(
|
||||
wallet.getPrivateKeyForAddress("n2fiWrHqD6GM5GiEqkbWAc6aaZQp3ba93X"),
|
||||
wallet.getExternalAccount().derive(1).priv
|
||||
)
|
||||
assertEqual(
|
||||
wallet.getPrivateKeyForAddress("mnXiDR4MKsFxcKJEZjx4353oXvo55iuptn"),
|
||||
wallet.getInternalAccount().derive(0).priv
|
||||
)
|
||||
})
|
||||
|
||||
it('raises an error when address is not found', function(){
|
||||
|
@ -463,7 +467,10 @@ describe('Wallet', function() {
|
|||
var to = 'mt7MyTVVEWnbwpF5hBn6fgnJcv95Syk2ue'
|
||||
var wallet = new Wallet(seed, {network: 'testnet'})
|
||||
var tx = wallet.createTx(to, value)
|
||||
|
||||
assert.equal(tx.outs.length, 1)
|
||||
assert.equal(tx.outs[0].address.toString(), to)
|
||||
assert.equal(tx.outs[0].value, value)
|
||||
})
|
||||
})
|
||||
|
||||
|
@ -471,17 +478,27 @@ describe('Wallet', function() {
|
|||
it('should allow custom changeAddress', function(){
|
||||
var wallet = new Wallet(seed, {network: 'testnet'})
|
||||
var address = wallet.generateAddress()
|
||||
utxo = {
|
||||
"hash":"b3c5fde139dc0a3bba2729bfd5b9e16f5894131dc3dc46a91151da3053e7e3a5",
|
||||
"outputIndex": 0,
|
||||
"address" : address,
|
||||
"value": 100000
|
||||
}
|
||||
var to = "mt7MyTVVEWnbwpF5hBn6fgnJcv95Syk2ue"
|
||||
|
||||
wallet.setUnspentOutputs([{
|
||||
hash: fakeTxHash(0),
|
||||
outputIndex: 0,
|
||||
address: address,
|
||||
value: value
|
||||
}])
|
||||
assert.equal(wallet.getBalance(), value)
|
||||
|
||||
var changeAddress = 'mfrFjnKZUvTcvdAK2fUX5D8v1Epu5H8JCk'
|
||||
wallet.setUnspentOutputs([utxo])
|
||||
var tx = wallet.createTx(to, 10000, 1000, changeAddress)
|
||||
var to = 'mt7MyTVVEWnbwpF5hBn6fgnJcv95Syk2ue'
|
||||
var toValue = value / 2
|
||||
var fee = 1e3
|
||||
|
||||
var tx = wallet.createTx(to, toValue, fee, changeAddress)
|
||||
assert.equal(tx.outs.length, 2)
|
||||
assert.equal(tx.outs[0].address.toString(), to)
|
||||
assert.equal(tx.outs[0].value, toValue)
|
||||
|
||||
assert.equal(tx.outs[1].address.toString(), changeAddress)
|
||||
assert.equal(tx.outs[1].value, value - (toValue + fee))
|
||||
})
|
||||
})
|
||||
|
||||
|
|
Loading…
Reference in a new issue