diff --git a/src/commands/generate-lattice-keys.js b/src/commands/generate-lattice-keys.js index d4833bd..8db3018 100644 --- a/src/commands/generate-lattice-keys.js +++ b/src/commands/generate-lattice-keys.js @@ -178,15 +178,14 @@ class Lattice extends Command { } // set the fee to default or flag - let fee = 0 // default fee 100 Shor + let fee = 0 // default fee 0 Shor if (flags.fee) { const passedFee = parseInt(flags.fee, 10) - if (passedFee) { - fee = passedFee - } else { + if (Number.isNaN(passedFee) || passedFee < 0) { this.log(`${red('⨉')} Fee is invalid`) this.exit(1) } + fee = passedFee } // create the keys @@ -497,7 +496,7 @@ Lattice.flags = { fee: flags.string({ char: 'f', required: false, - description: '(default: 100) QRL (f)ee for transaction in Shor' + description: '(default: 0) QRL (f)ee for transaction in Shor' }), otsindex: flags.string({ diff --git a/src/commands/notarize.js b/src/commands/notarize.js index 76ad2d3..f87db25 100644 --- a/src/commands/notarize.js +++ b/src/commands/notarize.js @@ -200,12 +200,11 @@ class Notarise extends Command { let fee = 0 // default fee 0 Shor if (flags.fee) { const passedFee = parseInt(flags.fee, 10) - if (passedFee) { - fee = passedFee - } else { + if (Number.isNaN(passedFee) || passedFee < 0) { spinner.fail(`${black().bgRed(`Fee is invalid...`)}` ) this.exit(1) } + fee = passedFee } // sign and send transaction diff --git a/src/commands/send-message.js b/src/commands/send-message.js index 26cd652..d012224 100644 --- a/src/commands/send-message.js +++ b/src/commands/send-message.js @@ -177,15 +177,14 @@ class SendMessage extends Command { } } // set the fee to default or flag - let fee = 0 // default fee 100 Shor + let fee = 0 // default fee 0 Shor if (flags.fee) { const passedFee = parseInt(flags.fee, 10) - if (passedFee) { - fee = passedFee - } else { + if (Number.isNaN(passedFee) || passedFee < 0) { this.log(`${red('⨉')} Fee is invalid`) this.exit(1) } + fee = passedFee } const spinner = ora({ text: 'Sending Message to network...' }).start() @@ -387,7 +386,7 @@ SendMessage.flags = { fee: flags.string({ char: 'f', required: false, - description: 'QRL (f)ee for transaction in Shor (defaults to 100 Shor)' + description: 'QRL (f)ee for transaction in Shor (defaults to 0 Shor)' }), otsindex: flags.string({ diff --git a/src/commands/send.js b/src/commands/send.js index 7b0eaec..7fd2097 100644 --- a/src/commands/send.js +++ b/src/commands/send.js @@ -390,15 +390,14 @@ class Send extends Command { this.exit(1) } } - let fee = 0 // default fee 100 Shor + let fee = 0 // default fee 0 Shor if (flags.fee) { const passedFee = parseInt(flags.fee, 10) - if (passedFee) { - fee = passedFee - } else { + if (Number.isNaN(passedFee) || passedFee < 0) { this.log(`${red('⨉')} Fee is invalid`) this.exit(1) } + fee = passedFee } const thisAddressesTo = [] const thisAmounts = [] @@ -756,7 +755,7 @@ Send.flags = { fee: flags.string({ char: 'f', required: false, - description: 'Fee for transaction in Shor (defaults to 100 Shor)' + description: 'Fee for transaction in Shor (defaults to 0 Shor)' }), file: flags.string({ diff --git a/src/commands/sign-tx-offline.js b/src/commands/sign-tx-offline.js index e90557a..3a48542 100644 --- a/src/commands/sign-tx-offline.js +++ b/src/commands/sign-tx-offline.js @@ -323,12 +323,11 @@ class SignTxOffline extends Command { let fee = 100 // default fee 100 Shor if (flags.fee) { const passedFee = parseInt(flags.fee, 10) - if (passedFee) { - fee = passedFee - } else { + if (Number.isNaN(passedFee) || passedFee < 0) { this.log(`${red('⨉')} Fee is invalid`) this.exit(1) } + fee = passedFee } const thisAddressesTo = [] const thisAmounts = [] diff --git a/test/commands/send.test.js b/test/commands/send.test.js index fba1223..519f0b7 100644 --- a/test/commands/send.test.js +++ b/test/commands/send.test.js @@ -682,9 +682,43 @@ describe('send #3a', () => { }) }) +// an explicit fee of 0 (the default) must be accepted, not rejected as invalid +describe('send #3c', () => { + const fs = require('fs') // eslint-disable-line global-require + const outFile = `${testSetup.sendTXOfflineFile}.zerofee` + let exitCode + before(done => { + const args = [ + 'send', + '1', + '-r', 'Q000200ecffb27f3d7b11ccd048eb559277d64bb52bfda998341e66a9f11b2d07f6b2ee4f62c408', + '-h', 'aback filled atop regal town opaque gloss send cheek ten fisher cow once home remain module aye salt chord before bunch stiff heel won attend reduce heroic oak shrug midday king fit islam appear', + '-i', '10', + '-f', '0', + '-t', + '-T', outFile, + ] + const process = spawn('./bin/run', args, processFlags) + process.on('exit', code => { + exitCode = code + done() + }) + }) + after(() => { + try { fs.unlinkSync(outFile) } catch (e) { /* ignore */ } + }) + it('exit code should be 0 when --fee 0 is passed explicitly', () => { + assert.strictEqual(exitCode, 0) + }) + it('writes a transaction carrying a zero fee', () => { + const tx = JSON.parse(fs.readFileSync(outFile)) + assert.strictEqual(tx.tx.fee, '0') + }) +}) + -// load from a file +// load from a file describe('send #3b', () => { let exitCode before(done => {