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

Commit

Permalink
Extract getPathFind validation
Browse files Browse the repository at this point in the history
  • Loading branch information
Chris Clark committed Apr 2, 2015
1 parent 3e4f5ad commit 91f3d68
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 50 deletions.
1 change: 1 addition & 0 deletions api/lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ function renameCounterpartyToIssuer(amount) {
amount.issuer = amount.counterparty;
delete amount.counterparty;
}
return amount;
}

function dropsToXrp(drops) {
Expand Down
31 changes: 31 additions & 0 deletions api/lib/validate.js
Original file line number Diff line number Diff line change
Expand Up @@ -315,6 +315,36 @@ function validatePayment(payment) {
}
}

function validatePathFind(pathfind) {
if (!pathfind.source_account) {
throw error(
'Missing parameter: source_account. Must be a valid Ripple address');
}

if (!pathfind.destination_account) {
throw error('Missing parameter: destination_account. '
+ 'Must be a valid Ripple address');
}

if (!isValidAddress(pathfind.source_account)) {
throw error('Parameter is not a valid Ripple address: account');
}

if (!isValidAddress(pathfind.destination_account)) {
throw error('Parameter is not a valid Ripple address: destination_account');
}

if (!pathfind.destination_amount) {
throw error('Missing parameter: destination_amount. '
+ 'Must be an amount string in the form value+currency+issuer');
}

if (!validator.isValid(pathfind.destination_amount, 'Amount')) {
throw error('Invalid parameter: destination_amount. '
+ 'Must be an amount string in the form value+currency+issuer');
}
}

function validateTrustline(trustline) {
validateSchema(trustline, 'Trustline');
}
Expand Down Expand Up @@ -358,6 +388,7 @@ module.exports = createValidators({
client_resource_id: validateClientResourceID,
last_ledger_sequence: validateLastLedgerSequence,
payment: validatePayment,
pathfind: validatePathFind,
trustline: validateTrustline,
validated: validateValidated
});
58 changes: 8 additions & 50 deletions api/payments.js
Original file line number Diff line number Diff line change
Expand Up @@ -383,58 +383,16 @@ function getAccountPayments(account, source_account, destination_account,
function getPathFind(source_account, destination_account,
destination_amount_string, source_currency_strings, callback) {
var self = this;
if (!source_account) {
callback(new InvalidRequestError(
'Missing parameter: source_account. Must be a valid Ripple address'));
return;
}

if (!destination_account) {
callback(new InvalidRequestError('Missing parameter: destination_account. '
+ 'Must be a valid Ripple address'));
return;
}

if (!ripple.UInt160.is_valid(source_account)) {
callback(new errors.InvalidRequestError(
'Parameter is not a valid Ripple address: account'));
return;
}

if (!ripple.UInt160.is_valid(destination_account)) {
callback(new errors.InvalidRequestError(
'Parameter is not a valid Ripple address: destination_account'));
return;
}

// Parse destination amount
if (!destination_amount_string) {
callback(new InvalidRequestError('Missing parameter: destination_amount. '
+ 'Must be an amount string in the form value+currency+issuer'));
return;
}
var destination_amount = utils.renameCounterpartyToIssuer(
utils.parseCurrencyQuery(destination_amount_string || ''));

var _destination_amount = utils.parseCurrencyQuery(destination_amount_string);
var destination_amount = _.omit(_destination_amount, 'counterparty');
destination_amount.issuer = _destination_amount.counterparty;

if (!ripple.UInt160.is_valid(source_account)) {
callback(new InvalidRequestError(
'Invalid parameter: source_account. Must be a valid Ripple address'));
return;
}

if (!ripple.UInt160.is_valid(destination_account)) {
callback(new InvalidRequestError('Invalid parameter: destination_account. '
+ 'Must be a valid Ripple address'));
return;
}

if (!validator.isValid(destination_amount, 'Amount')) {
callback(new InvalidRequestError('Invalid parameter: destination_amount. '
+ 'Must be an amount string in the form value+currency+issuer'));
return;
}
validate.pathfind({
source_account: source_account,
destination_account: destination_account,
destination_amount: destination_amount,
source_currency_strings: source_currency_strings
});

var source_currencies = [];
// Parse source currencies
Expand Down

0 comments on commit 91f3d68

Please sign in to comment.