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:
|
* Can be called with:
|
||||||
*
|
*
|
||||||
* i) An existing TransactionOut object
|
* 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
|
* iii) An address:value string
|
||||||
* iv) Either ii), iii) with an optional network argument
|
* iv) Either ii), iii) with an optional network argument
|
||||||
*
|
*
|
||||||
|
@ -96,23 +96,24 @@ Transaction.prototype.addOutput = function (address, value, network) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if (arguments[0].indexOf(':') >= 0) {
|
if (typeof address === 'string') {
|
||||||
network = value
|
if (arguments[0].indexOf(':') >= 0) {
|
||||||
|
var args = arguments[0].split(':')
|
||||||
|
address = args[0]
|
||||||
|
value = parseInt(args[1])
|
||||||
|
|
||||||
var args = arguments[0].split(':')
|
network = arguments[1]
|
||||||
address = args[0]
|
}
|
||||||
value = parseInt(args[1])
|
|
||||||
|
address = Address.fromBase58Check(address)
|
||||||
}
|
}
|
||||||
|
|
||||||
network = network || Network.bitcoin
|
network = network || Network.bitcoin
|
||||||
|
|
||||||
if (typeof address === 'string') {
|
|
||||||
address = Address.fromBase58Check(address)
|
|
||||||
}
|
|
||||||
|
|
||||||
this.outs.push(new TransactionOut({
|
this.outs.push(new TransactionOut({
|
||||||
value: value,
|
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)
|
: data.address ? Script.createOutputScript(data.address)
|
||||||
: new Script()
|
: 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 =
|
this.value =
|
||||||
Array.isArray(data.value) ? convert.bytesToNum(data.value)
|
Array.isArray(data.value) ? convert.bytesToNum(data.value)
|
||||||
|
|
|
@ -154,6 +154,11 @@ describe('Transaction', function() {
|
||||||
verifyTransactionOut()
|
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(){
|
it('allows a string in the form of address:index to be passed in', function(){
|
||||||
tx.addOutput("15mMHKL96tWAUtqF3tbVf99Z8arcmnJrr3:40000")
|
tx.addOutput("15mMHKL96tWAUtqF3tbVf99Z8arcmnJrr3:40000")
|
||||||
verifyTransactionOut()
|
verifyTransactionOut()
|
||||||
|
@ -168,6 +173,15 @@ describe('Transaction', function() {
|
||||||
verifyTransactionOut()
|
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(){
|
function verifyTransactionOut(){
|
||||||
assert.equal(tx.outs.length, 1)
|
assert.equal(tx.outs.length, 1)
|
||||||
|
|
||||||
|
|
|
@ -141,10 +141,14 @@ describe('Wallet', function() {
|
||||||
wallet.generateAddress()
|
wallet.generateAddress()
|
||||||
wallet.generateAddress()
|
wallet.generateAddress()
|
||||||
|
|
||||||
assertEqual(wallet.getPrivateKeyForAddress("n2fiWrHqD6GM5GiEqkbWAc6aaZQp3ba93X"),
|
assertEqual(
|
||||||
wallet.getExternalAccount().derive(1).priv)
|
wallet.getPrivateKeyForAddress("n2fiWrHqD6GM5GiEqkbWAc6aaZQp3ba93X"),
|
||||||
assertEqual(wallet.getPrivateKeyForAddress("mnXiDR4MKsFxcKJEZjx4353oXvo55iuptn"),
|
wallet.getExternalAccount().derive(1).priv
|
||||||
wallet.getInternalAccount().derive(0).priv)
|
)
|
||||||
|
assertEqual(
|
||||||
|
wallet.getPrivateKeyForAddress("mnXiDR4MKsFxcKJEZjx4353oXvo55iuptn"),
|
||||||
|
wallet.getInternalAccount().derive(0).priv
|
||||||
|
)
|
||||||
})
|
})
|
||||||
|
|
||||||
it('raises an error when address is not found', function(){
|
it('raises an error when address is not found', function(){
|
||||||
|
@ -463,7 +467,10 @@ describe('Wallet', function() {
|
||||||
var to = 'mt7MyTVVEWnbwpF5hBn6fgnJcv95Syk2ue'
|
var to = 'mt7MyTVVEWnbwpF5hBn6fgnJcv95Syk2ue'
|
||||||
var wallet = new Wallet(seed, {network: 'testnet'})
|
var wallet = new Wallet(seed, {network: 'testnet'})
|
||||||
var tx = wallet.createTx(to, value)
|
var tx = wallet.createTx(to, value)
|
||||||
|
|
||||||
assert.equal(tx.outs.length, 1)
|
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(){
|
it('should allow custom changeAddress', function(){
|
||||||
var wallet = new Wallet(seed, {network: 'testnet'})
|
var wallet = new Wallet(seed, {network: 'testnet'})
|
||||||
var address = wallet.generateAddress()
|
var address = wallet.generateAddress()
|
||||||
utxo = {
|
|
||||||
"hash":"b3c5fde139dc0a3bba2729bfd5b9e16f5894131dc3dc46a91151da3053e7e3a5",
|
wallet.setUnspentOutputs([{
|
||||||
"outputIndex": 0,
|
hash: fakeTxHash(0),
|
||||||
"address" : address,
|
outputIndex: 0,
|
||||||
"value": 100000
|
address: address,
|
||||||
}
|
value: value
|
||||||
var to = "mt7MyTVVEWnbwpF5hBn6fgnJcv95Syk2ue"
|
}])
|
||||||
|
assert.equal(wallet.getBalance(), value)
|
||||||
|
|
||||||
var changeAddress = 'mfrFjnKZUvTcvdAK2fUX5D8v1Epu5H8JCk'
|
var changeAddress = 'mfrFjnKZUvTcvdAK2fUX5D8v1Epu5H8JCk'
|
||||||
wallet.setUnspentOutputs([utxo])
|
var to = 'mt7MyTVVEWnbwpF5hBn6fgnJcv95Syk2ue'
|
||||||
var tx = wallet.createTx(to, 10000, 1000, changeAddress)
|
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.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…
Add table
Add a link
Reference in a new issue