Skip to content

Commit

Permalink
Don't set empty paths
Browse files Browse the repository at this point in the history
"The Paths field must not be an empty array, nor an array whose members are all
empty arrays."
https://ripple.com/build/transactions/#paths

Fix test file lint errors
  • Loading branch information
Alan Cohen committed Aug 7, 2015
1 parent 9270d0a commit 83874ec
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 53 deletions.
8 changes: 6 additions & 2 deletions src/core/transaction.js
Expand Up @@ -344,8 +344,8 @@ Transaction.prototype._computeFee = function() {
}

switch (fees.length) {
case 0: return undefined;
case 1: return String(fees[0]);
case 0: return undefined;
case 1: return String(fees[0]);
}

fees.sort(function ascending(a, b) {
Expand Down Expand Up @@ -1181,6 +1181,10 @@ Transaction.prototype.paths = function(paths) {
throw new Error('Paths must be an array');
}

if (paths.length === 0) {
return this;
}

this.tx_json.Paths = [];
paths.forEach(this.addPath, this);

Expand Down
123 changes: 72 additions & 51 deletions test/transaction-test.js
Expand Up @@ -649,26 +649,26 @@ describe('Transaction', function() {
Paths: [
[
{
account: 'rMwjYedjc7qqtKYVLiAccJSmCwih4LnE2q',
currency: 'USD',
issuer: 'rMwjYedjc7qqtKYVLiAccJSmCwih4LnE2q',
type: 49,
type_hex: '0000000000000031'
},
{
currency: 'LTC',
issuer: 'rfYv1TXnwgDDK4WQNbFALykYuEBnrR4pDX',
type: 48,
type_hex: '0000000000000030'
},
{
account: 'rfYv1TXnwgDDK4WQNbFALykYuEBnrR4pDX',
currency: 'LTC',
issuer: 'rfYv1TXnwgDDK4WQNbFALykYuEBnrR4pDX',
type: 49,
type_hex: '0000000000000031'
}
]
account: 'rMwjYedjc7qqtKYVLiAccJSmCwih4LnE2q',
currency: 'USD',
issuer: 'rMwjYedjc7qqtKYVLiAccJSmCwih4LnE2q',
type: 49,
type_hex: '0000000000000031'
},
{
currency: 'LTC',
issuer: 'rfYv1TXnwgDDK4WQNbFALykYuEBnrR4pDX',
type: 48,
type_hex: '0000000000000030'
},
{
account: 'rfYv1TXnwgDDK4WQNbFALykYuEBnrR4pDX',
currency: 'LTC',
issuer: 'rfYv1TXnwgDDK4WQNbFALykYuEBnrR4pDX',
type: 49,
type_hex: '0000000000000031'
}
]
],
SendMax: {
currency: 'USD',
Expand Down Expand Up @@ -778,26 +778,26 @@ describe('Transaction', function() {
Paths: [
[
{
account: 'rMwjYedjc7qqtKYVLiAccJSmCwih4LnE2q',
currency: 'USD',
issuer: 'rMwjYedjc7qqtKYVLiAccJSmCwih4LnE2q',
type: 49,
type_hex: '0000000000000031'
},
{
currency: 'LTC',
issuer: 'rfYv1TXnwgDDK4WQNbFALykYuEBnrR4pDX',
type: 48,
type_hex: '0000000000000030'
},
{
account: 'rfYv1TXnwgDDK4WQNbFALykYuEBnrR4pDX',
currency: 'LTC',
issuer: 'rfYv1TXnwgDDK4WQNbFALykYuEBnrR4pDX',
type: 49,
type_hex: '0000000000000031'
}
]
account: 'rMwjYedjc7qqtKYVLiAccJSmCwih4LnE2q',
currency: 'USD',
issuer: 'rMwjYedjc7qqtKYVLiAccJSmCwih4LnE2q',
type: 49,
type_hex: '0000000000000031'
},
{
currency: 'LTC',
issuer: 'rfYv1TXnwgDDK4WQNbFALykYuEBnrR4pDX',
type: 48,
type_hex: '0000000000000030'
},
{
account: 'rfYv1TXnwgDDK4WQNbFALykYuEBnrR4pDX',
currency: 'LTC',
issuer: 'rfYv1TXnwgDDK4WQNbFALykYuEBnrR4pDX',
type: 49,
type_hex: '0000000000000031'
}
]
],
SendMax: {
currency: 'USD',
Expand Down Expand Up @@ -1136,17 +1136,17 @@ describe('Transaction', function() {

const paths = [
[{
account: 'rP51ycDJw5ZhgvdKiRjBYZKYjsyoCcHmnY',
issuer: 'rsLEU1TPdCJPPysqhWYw9jD97xtG5WqSJm',
test: 1,
currency: 'USD'
}],
[{
account: 'rP51ycDJw5ZhgvdKiRjBYZKYjsyoCcHmnY',
issuer: 'rsLEU1TPdCJPPysqhWYw9jD97xtG5WqSJm',
test: 2,
currency: 'USD'
}]
account: 'rP51ycDJw5ZhgvdKiRjBYZKYjsyoCcHmnY',
issuer: 'rsLEU1TPdCJPPysqhWYw9jD97xtG5WqSJm',
test: 1,
currency: 'USD'
}],
[{
account: 'rP51ycDJw5ZhgvdKiRjBYZKYjsyoCcHmnY',
issuer: 'rsLEU1TPdCJPPysqhWYw9jD97xtG5WqSJm',
test: 2,
currency: 'USD'
}]
];

// assert.throws(function() {
Expand Down Expand Up @@ -1184,6 +1184,27 @@ describe('Transaction', function() {
]);
});

it('Does not add empty transaction paths', function() {
const transaction = new Transaction();

const paths = [];

assert.strictEqual(transaction.tx_json.Paths, undefined);

transaction.setType('Payment');

assert.throws(function() {
transaction.paths(1);
}, /Error: Paths must be an array/);
assert.throws(function() {
transaction.setPaths(1);
}, /Error: Paths must be an array/);

transaction.setPaths(paths);

assert.strictEqual(transaction.tx_json.Paths, undefined);
});

it('Set secret', function() {
const transaction = new Transaction();
transaction.secret('shHXjwp9m3MDQNcUrTekXcdzFsCjM');
Expand Down

0 comments on commit 83874ec

Please sign in to comment.