@ -1,3 +1,5 @@
/* global describe, it, beforeEach, afterEach */
var assert = require ( 'assert' )
var bufferutils = require ( '../src/bufferutils' )
var crypto = require ( '../src/crypto' )
@ -111,8 +113,8 @@ describe('Wallet', function() {
it ( 'generate receiving addresses' , function ( ) {
var wallet = new Wallet ( seed , networks . testnet )
var expectedAddresses = [
"n1GyUANZand9Kw6hGSV9837cCC9FFUQzQa" ,
"n2fiWrHqD6GM5GiEqkbWAc6aaZQp3ba93X"
'n1GyUANZand9Kw6hGSV9837cCC9FFUQzQa' ,
'n2fiWrHqD6GM5GiEqkbWAc6aaZQp3ba93X'
]
assert . equal ( wallet . generateAddress ( ) , expectedAddresses [ 0 ] )
@ -122,14 +124,9 @@ describe('Wallet', function() {
} )
describe ( 'generateChangeAddress' , function ( ) {
var wallet
beforeEach ( function ( ) {
wallet = new Wallet ( seed )
} )
it ( 'generates change addresses' , function ( ) {
var wallet = new Wallet ( seed , networks . testnet )
var expectedAddresses = [ "mnXiDR4MKsFxcKJEZjx4353oXvo55iuptn" ]
var expectedAddresses = [ 'mnXiDR4MKsFxcKJEZjx4353oXvo55iuptn' ]
assert . equal ( wallet . generateChangeAddress ( ) , expectedAddresses [ 0 ] )
assert . deepEqual ( wallet . changeAddresses , expectedAddresses )
@ -137,11 +134,6 @@ describe('Wallet', function() {
} )
describe ( 'getPrivateKey' , function ( ) {
var wallet
beforeEach ( function ( ) {
wallet = new Wallet ( seed )
} )
it ( 'returns the private key at the given index of external account' , function ( ) {
var wallet = new Wallet ( seed , networks . testnet )
@ -151,11 +143,6 @@ describe('Wallet', function() {
} )
describe ( 'getInternalPrivateKey' , function ( ) {
var wallet
beforeEach ( function ( ) {
wallet = new Wallet ( seed )
} )
it ( 'returns the private key at the given index of internal account' , function ( ) {
var wallet = new Wallet ( seed , networks . testnet )
@ -165,11 +152,6 @@ describe('Wallet', function() {
} )
describe ( 'getPrivateKeyForAddress' , function ( ) {
var wallet
beforeEach ( function ( ) {
wallet = new Wallet ( seed )
} )
it ( 'returns the private key for the given address' , function ( ) {
var wallet = new Wallet ( seed , networks . testnet )
wallet . generateChangeAddress ( )
@ -177,11 +159,11 @@ describe('Wallet', function() {
wallet . generateAddress ( )
assertEqual (
wallet . getPrivateKeyForAddress ( "n2fiWrHqD6GM5GiEqkbWAc6aaZQp3ba93X" ) ,
wallet . getPrivateKeyForAddress ( 'n2fiWrHqD6GM5GiEqkbWAc6aaZQp3ba93X' ) ,
wallet . getExternalAccount ( ) . derive ( 1 ) . privKey
)
assertEqual (
wallet . getPrivateKeyForAddress ( "mnXiDR4MKsFxcKJEZjx4353oXvo55iuptn" ) ,
wallet . getPrivateKeyForAddress ( 'mnXiDR4MKsFxcKJEZjx4353oXvo55iuptn' ) ,
wallet . getInternalAccount ( ) . derive ( 0 ) . privKey
)
} )
@ -190,23 +172,23 @@ describe('Wallet', function() {
var wallet = new Wallet ( seed , networks . testnet )
assert . throws ( function ( ) {
wallet . getPrivateKeyForAddress ( "n2fiWrHqD6GM5GiEqkbWAc6aaZQp3ba93X" )
wallet . getPrivateKeyForAddress ( 'n2fiWrHqD6GM5GiEqkbWAc6aaZQp3ba93X' )
} , /Unknown address. Make sure the address is from the keychain and has been generated/ )
} )
} )
describe ( 'Unspent Outputs' , function ( ) {
var utxo , expectedOutputKey
var utxo
var wallet
beforeEach ( function ( ) {
utxo = {
"address" : "1AZpKpcfCzKDUeTFBQUL4MokQai3m3HMXv" ,
"confirmations" : 1 ,
"index" : 0 ,
"txId" : fakeTxId ( 6 ) ,
"value" : 20000 ,
"pending" : false
'address' : '1AZpKpcfCzKDUeTFBQUL4MokQai3m3HMXv' ,
'confirmations' : 1 ,
'index' : 0 ,
'txId' : fakeTxId ( 6 ) ,
'value' : 20000 ,
'pending' : false
}
} )
@ -272,7 +254,6 @@ describe('Wallet', function() {
describe ( 'setUnspentOutputs' , function ( ) {
var utxo
var expectedOutputKey
var wallet
beforeEach ( function ( ) {
@ -296,7 +277,7 @@ describe('Wallet', function() {
describe ( 'required fields' , function ( ) {
[ 'index' , 'address' , 'hash' , 'value' ] . forEach ( function ( field ) {
it ( "throws an error when " + field + " is missing" , function ( ) {
it ( 'throws an error when ' + field + ' is missing' , function ( ) {
delete utxo [ field ]
assert . throws ( function ( ) {
@ -326,15 +307,15 @@ describe('Wallet', function() {
tx = Transaction . fromHex ( fixtureTx1Hex )
} )
describe ( "processPendingTx" , function ( ) {
it ( "incoming: sets the pending flag on output" , function ( ) {
describe ( 'processPendingTx' , function ( ) {
it ( 'incoming: sets the pending flag on output' , function ( ) {
wallet . addresses = [ addresses [ 0 ] ]
wallet . processPendingTx ( tx )
verifyOutputAdded ( 0 , true )
} )
describe ( "when tx ins outpoint contains a known txhash:i" , function ( ) {
describe ( 'when tx ins outpoint contains a known txhash:i' , function ( ) {
var spendTx
beforeEach ( function ( ) {
wallet . addresses = [ addresses [ 0 ] ]
@ -372,7 +353,7 @@ describe('Wallet', function() {
} )
describe ( "when tx outs contains an address owned by the wallet, an 'output' gets added to wallet.unspentMap" , function ( ) {
it ( "works for receive address" , function ( ) {
it ( 'works for receive address' , function ( ) {
var totalOuts = outputCount ( )
wallet . addresses = [ addresses [ 0 ] ]
@ -382,7 +363,7 @@ describe('Wallet', function() {
verifyOutputAdded ( 0 , false )
} )
it ( "works for change address" , function ( ) {
it ( 'works for change address' , function ( ) {
var totalOuts = outputCount ( )
wallet . changeAddresses = [ addresses [ 1 ] ]
@ -397,7 +378,7 @@ describe('Wallet', function() {
}
} )
describe ( "when tx ins contains a known txhash:i" , function ( ) {
describe ( 'when tx ins contains a known txhash:i' , function ( ) {
var spendTx
beforeEach ( function ( ) {
wallet . addresses = [ addresses [ 0 ] ] // the address fixtureTx2 used as input
@ -406,7 +387,7 @@ describe('Wallet', function() {
spendTx = Transaction . fromHex ( fixtureTx2Hex )
} )
it ( "does not add to wallet.unspentMap" , function ( ) {
it ( 'does not add to wallet.unspentMap' , function ( ) {
wallet . processConfirmedTx ( spendTx )
assert . deepEqual ( wallet . unspentMap , { } )
} )
@ -424,16 +405,15 @@ describe('Wallet', function() {
} )
} )
it ( "does nothing when none of the involved addresses belong to the wallet" , function ( ) {
it ( 'does nothing when none of the involved addresses belong to the wallet' , function ( ) {
wallet . processConfirmedTx ( tx )
assert . deepEqual ( wallet . unspentMap , { } )
} )
function verifyOutputAdded ( index , pending ) {
var txOut = tx . outs [ index ]
var key = tx . getId ( ) + ":" + index
var key = tx . getId ( ) + ':' + index
var output = wallet . unspentMap [ key ]
assert . deepEqual ( output . txHash , tx . getHash ( ) )
assert . equal ( output . value , txOut . value )
@ -453,28 +433,28 @@ describe('Wallet', function() {
to = 'mt7MyTVVEWnbwpF5hBn6fgnJcv95Syk2ue'
value = 500000
address1 = "n1GyUANZand9Kw6hGSV9837cCC9FFUQzQa"
address2 = "n2fiWrHqD6GM5GiEqkbWAc6aaZQp3ba93X"
address1 = 'n1GyUANZand9Kw6hGSV9837cCC9FFUQzQa'
address2 = 'n2fiWrHqD6GM5GiEqkbWAc6aaZQp3ba93X'
// set up 3 utxos
var utxos = [
{
"txId" : fakeTxId ( 1 ) ,
"index" : 0 ,
"address" : address1 ,
"value" : 400000 // not enough for value
'txId' : fakeTxId ( 1 ) ,
'index' : 0 ,
'address' : address1 ,
'value' : 400000 // not enough for value
} ,
{
"txId" : fakeTxId ( 2 ) ,
"index" : 1 ,
"address" : address1 ,
"value" : 500000 // enough for only value
'txId' : fakeTxId ( 2 ) ,
'index' : 1 ,
'address' : address1 ,
'value' : 500000 // enough for only value
} ,
{
"txId" : fakeTxId ( 3 ) ,
"index" : 0 ,
"address" : address2 ,
"value" : 510000 // enough for value and fee
'txId' : fakeTxId ( 3 ) ,
'index' : 0 ,
'address' : address2 ,
'value' : 510000 // enough for value and fee
}
]
@ -487,7 +467,9 @@ describe('Wallet', function() {
describe ( 'transaction fee' , function ( ) {
it ( 'allows fee to be specified' , function ( ) {
var fee = 30000
var tx = wallet . createTx ( to , value , { fixedFee : fee } )
var tx = wallet . createTx ( to , value , {
fixedFee : fee
} )
assert . equal ( getFee ( wallet , tx ) , fee )
} )
@ -495,7 +477,9 @@ describe('Wallet', function() {
it ( 'allows fee to be set to zero' , function ( ) {
value = 510000
var fee = 0
var tx = wallet . createTx ( to , value , { fixedFee : fee } )
var tx = wallet . createTx ( to , value , {
fixedFee : fee
} )
assert . equal ( getFee ( wallet , tx ) , fee )
} )
@ -504,7 +488,7 @@ describe('Wallet', function() {
var utxo = {
txId : fakeTxId ( 0 ) ,
index : 0 ,
address : "LeyySKbQrRRwodKEj1W4a8y3YQupPLw5os" ,
address : 'LeyySKbQrRRwodKEj1W4a8y3YQupPLw5os' ,
value : 500000
}
@ -608,7 +592,9 @@ describe('Wallet', function() {
var fee = 0
wallet . generateChangeAddress ( )
wallet . generateChangeAddress ( )
var tx = wallet . createTx ( to , value , { fixedFee : fee } )
var tx = wallet . createTx ( to , value , {
fixedFee : fee
} )
assert . equal ( tx . outs . length , 2 )
var out = tx . outs [ 1 ]
@ -622,7 +608,9 @@ describe('Wallet', function() {
var fee = 0
assert . equal ( wallet . changeAddresses . length , 0 )
var tx = wallet . createTx ( to , value , { fixedFee : fee } )
var tx = wallet . createTx ( to , value , {
fixedFee : fee
} )
assert . equal ( wallet . changeAddresses . length , 1 )
var out = tx . outs [ 1 ]
@ -649,15 +637,18 @@ describe('Wallet', function() {
it ( 'signs the inputs with respective keys' , function ( ) {
var fee = 30000
sinon . spy ( TransactionBuilder . prototype , "sign" )
sinon . spy ( TransactionBuilder . prototype , 'sign' )
wallet . createTx ( to , value , { fixedFee : fee } )
wallet . createTx ( to , value , {
fixedFee : fee
} )
var priv1 = wallet . getPrivateKeyForAddress ( address1 )
var priv2 = wallet . getPrivateKeyForAddress ( address2 )
// FIXME: boo (required) side effects
priv1 . pub . Q . affineX , priv2 . pub . Q . affineX
// FIXME: boo, toString invokes reqiuired affine coordinate side effects
priv1 . pub . Q . toString ( )
priv2 . pub . Q . toString ( )
assert ( TransactionBuilder . prototype . sign . calledWith ( 0 , priv2 ) )
assert ( TransactionBuilder . prototype . sign . calledWith ( 1 , priv1 ) )