Skip to content
This repository has been archived by the owner on Mar 7, 2023. It is now read-only.

Commit

Permalink
Merge pull request #169 from blockchain/transaction-error-messages
Browse files Browse the repository at this point in the history
Transaction: localizable error messages
  • Loading branch information
Pernas committed Mar 10, 2016
2 parents acc0d13 + 5b0512a commit 19cb352
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 6 deletions.
12 changes: 8 additions & 4 deletions src/transaction.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ var randomBytes = require('randombytes');
var Helpers = require('./helpers');
var Buffer = require('buffer').Buffer;

// Error messages that can be seen by the user should take the form of:
// {error: "NOT_GOOD", some_param: 1}
// Error messages that should only appear during development can be any string.

var Transaction = function (unspentOutputs, toAddresses, amounts, fee, feePerKb, changeAddress, listener) {

if (!Array.isArray(toAddresses) && toAddresses != null) {toAddresses = [toAddresses];}
Expand All @@ -26,8 +30,8 @@ var Transaction = function (unspentOutputs, toAddresses, amounts, fee, feePerKb,
feePerKb = Helpers.isNumber(feePerKb) ? feePerKb : 10000;

assert(toAddresses.length == amounts.length, 'The number of destiny addresses and destiny amounts should be the same.');
assert(this.amount > BITCOIN_DUST, this.amount + ' must be above dust threshold (' + BITCOIN_DUST + ' Satoshis)');
assert(unspentOutputs && unspentOutputs.length > 0, 'Missing coins to spend');
assert(this.amount > BITCOIN_DUST, {error: 'BELOW_DUST_THRESHOLD', amount: this.amount, threshold: BITCOIN_DUST});
assert(unspentOutputs && unspentOutputs.length > 0, {error: 'NO_UNSPENT_OUTPUTS'});

var transaction = new Bitcoin.Transaction();
// add all outputs
Expand All @@ -52,9 +56,9 @@ var Transaction = function (unspentOutputs, toAddresses, amounts, fee, feePerKb,
// Generate address from output script and add to private list so we can check if the private keys match the inputs later

var script = Bitcoin.Script.fromHex(output.script);
assert.notEqual(Bitcoin.scripts.classifyOutput(script), 'nonstandard', 'Strange Script');
assert.notEqual(Bitcoin.scripts.classifyOutput(script), 'nonstandard', {error: 'STRANGE_SCRIPT'});
var address = Bitcoin.Address.fromOutputScript(script).toString();
assert(address, 'Unable to decode output address from transaction hash ' + output.tx_hash);
assert(address, {error: 'CANNOT_DECODE_OUTPUT_ADDRESS', tx_hash: output.tx_hash});
this.addressesOfInputs.push(address);

// Add to list of needed private keys
Expand Down
4 changes: 2 additions & 2 deletions tests/transaction_spend_spec.js.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ describe "Transaction", ->
new Transaction(null, data.to, data.amount, data.fee, data.feePerKb, data.from, null)
catch e
expect(e.name).toBe('AssertionError')
expect(e.message).toBe('Missing coins to spend')
expect(e.message.error).toBe('NO_UNSPENT_OUTPUTS')

it "should fail without amount lower than dust threshold", ->

Expand All @@ -115,7 +115,7 @@ describe "Transaction", ->
new Transaction(data.unspentMock, data.to, data.amount, data.fee, data.feePerKb, data.from, null)
catch e
expect(e.name).toBe('AssertionError')
expect(e.message).toContain('dust threshold')
expect(e.message.error).toBe('BELOW_DUST_THRESHOLD')

it "should initialize with good data", ->

Expand Down

0 comments on commit 19cb352

Please sign in to comment.