diff --git a/src/common/index.js b/src/common/index.js index 815d3b6e..934c578f 100644 --- a/src/common/index.js +++ b/src/common/index.js @@ -17,5 +17,6 @@ module.exports = { convertKeysFromSnakeCaseToCamelCase: utils.convertKeysFromSnakeCaseToCamelCase, iso8601ToRippleTime: utils.iso8601ToRippleTime, - rippleTimeToISO8601: utils.rippleTimeToISO8601 + rippleTimeToISO8601: utils.rippleTimeToISO8601, + isValidSecret: utils.isValidSecret }; diff --git a/src/common/schema-validator.js b/src/common/schema-validator.js index d247586d..e622aa8b 100644 --- a/src/common/schema-validator.js +++ b/src/common/schema-validator.js @@ -6,79 +6,93 @@ const assert = require('assert'); const Ajv = require('ajv'); const ValidationError = require('./errors').ValidationError; const {isValidAddress} = require('ripple-address-codec'); +const {isValidSecret} = require('./utils'); function loadSchemas() { // listed explicitly for webpack (instead of scanning schemas directory) const schemas = [ - require('./schemas/objects/address.json'), - require('./schemas/objects/adjustment.json'), - require('./schemas/objects/amount.json'), - require('./schemas/objects/amount-base.json'), - require('./schemas/objects/balance.json'), - require('./schemas/objects/blob.json'), - require('./schemas/objects/currency.json'), - require('./schemas/output/get-account-info.json'), - require('./schemas/output/get-balances.json'), - require('./schemas/output/get-balance-sheet'), - require('./schemas/input/balance-sheet-options.json'), - require('./schemas/output/get-ledger.json'), - require('./schemas/output/get-orderbook.json'), - require('./schemas/output/get-orders.json'), - require('./schemas/output/get-paths.json'), - require('./schemas/output/get-server-info.json'), - require('./schemas/output/get-settings.json'), - require('./schemas/output/get-transaction.json'), - require('./schemas/output/get-transactions.json'), - require('./schemas/output/get-trustlines.json'), + require('./schemas/objects/tx-json.json'), require('./schemas/objects/hash128.json'), require('./schemas/objects/hash256.json'), - require('./schemas/input/instructions.json'), + require('./schemas/objects/sequence.json'), require('./schemas/objects/issue.json'), - require('./schemas/input/ledger-options.json'), require('./schemas/objects/ledgerversion.json'), require('./schemas/objects/max-adjustment.json'), require('./schemas/objects/memo.json'), - require('./schemas/output/order-cancellation-transaction.json'), - require('./schemas/input/order-cancellation.json'), - require('./schemas/output/order-change.json'), - require('./schemas/output/order-transaction.json'), - require('./schemas/input/order.json'), - require('./schemas/output/orderbook-orders.json'), - require('./schemas/input/orderbook.json'), - require('./schemas/input/orders-options.json'), - require('./schemas/output/outcome.json'), - require('./schemas/input/pathfind.json'), - require('./schemas/output/payment-transaction.json'), - require('./schemas/input/payment.json'), - require('./schemas/objects/quality.json'), - require('./schemas/input/api-options.json'), - require('./schemas/objects/sequence.json'), - require('./schemas/input/settings-options.json'), - require('./schemas/output/settings-transaction.json'), - require('./schemas/input/settings.json'), - require('./schemas/output/sign.json'), - require('./schemas/objects/signed-value.json'), - require('./schemas/output/submit.json'), - require('./schemas/input/suspended-payment-cancellation.json'), - require('./schemas/input/suspended-payment-execution.json'), - require('./schemas/input/suspended-payment-creation.json'), - require('./schemas/input/transaction-options.json'), - require('./schemas/input/transactions-options.json'), - require('./schemas/output/trustline-transaction.json'), - require('./schemas/input/trustline.json'), - require('./schemas/input/trustlines-options.json'), - require('./schemas/objects/tx-json.json'), require('./schemas/objects/uint32.json'), require('./schemas/objects/value.json'), - require('./schemas/output/prepare.json'), - require('./schemas/output/ledger-closed.json'), require('./schemas/objects/source-adjustment.json'), require('./schemas/objects/destination-adjustment.json'), require('./schemas/objects/tag.json'), require('./schemas/objects/lax-amount.json'), require('./schemas/objects/lax-lax-amount.json'), require('./schemas/objects/min-adjustment.json'), - require('./schemas/objects/lax-adjustment.json') + require('./schemas/objects/lax-adjustment.json'), + require('./schemas/objects/tx-hash.json'), + require('./schemas/objects/address.json'), + require('./schemas/objects/adjustment.json'), + require('./schemas/objects/quality.json'), + require('./schemas/objects/amount.json'), + require('./schemas/objects/amount-base.json'), + require('./schemas/objects/balance.json'), + require('./schemas/objects/blob.json'), + require('./schemas/objects/currency.json'), + require('./schemas/objects/signed-value.json'), + require('./schemas/objects/orderbook.json'), + require('./schemas/objects/instructions.json'), + require('./schemas/specifications/settings.json'), + require('./schemas/specifications/payment.json'), + require('./schemas/specifications/suspended-payment-cancellation.json'), + require('./schemas/specifications/order-cancellation.json'), + require('./schemas/specifications/order.json'), + require('./schemas/specifications/suspended-payment-execution.json'), + require('./schemas/specifications/suspended-payment-creation.json'), + require('./schemas/specifications/trustline.json'), + require('./schemas/output/sign.json'), + require('./schemas/output/submit.json'), + require('./schemas/output/get-account-info.json'), + require('./schemas/output/get-balances.json'), + require('./schemas/output/get-balance-sheet'), + require('./schemas/output/get-ledger.json'), + require('./schemas/output/get-orderbook.json'), + require('./schemas/output/settings-transaction.json'), + require('./schemas/output/get-orders.json'), + require('./schemas/output/order-change.json'), + require('./schemas/output/order-cancellation-transaction.json'), + require('./schemas/output/prepare.json'), + require('./schemas/output/ledger-closed.json'), + require('./schemas/output/order-transaction.json'), + require('./schemas/output/get-paths.json'), + require('./schemas/output/trustline-transaction.json'), + require('./schemas/output/get-server-info.json'), + require('./schemas/output/get-settings.json'), + require('./schemas/output/orderbook-orders.json'), + require('./schemas/output/payment-transaction.json'), + require('./schemas/output/outcome.json'), + require('./schemas/output/get-transaction.json'), + require('./schemas/output/get-transactions.json'), + require('./schemas/output/get-trustlines.json'), + require('./schemas/input/get-balance-sheet.json'), + require('./schemas/input/get-ledger.json'), + require('./schemas/input/get-orders.json'), + require('./schemas/input/get-orderbook.json'), + require('./schemas/input/get-paths.json'), + require('./schemas/input/api-options.json'), + require('./schemas/input/get-settings.json'), + require('./schemas/input/get-transaction.json'), + require('./schemas/input/get-transactions.json'), + require('./schemas/input/get-trustlines.json'), + require('./schemas/input/prepare-payment.json'), + require('./schemas/input/prepare-order.json'), + require('./schemas/input/prepare-trustline.json'), + require('./schemas/input/prepare-order-cancellation.json'), + require('./schemas/input/prepare-settings.json'), + require('./schemas/input/prepare-suspended-payment-creation.json'), + require('./schemas/input/prepare-suspended-payment-cancellation.json'), + require('./schemas/input/prepare-suspended-payment-execution.json'), + require('./schemas/input/compute-ledger-hash'), + require('./schemas/input/sign'), + require('./schemas/input/submit') ]; const titles = _.map(schemas, schema => schema.title); const duplicates = _.keys(_.pick(_.countBy(titles), count => count > 1)); @@ -86,6 +100,7 @@ function loadSchemas() { const ajv = new Ajv(); _.forEach(schemas, schema => ajv.addSchema(schema, schema.title)); ajv.addFormat('address', isValidAddress); + ajv.addFormat('secret', isValidSecret); return ajv; } @@ -99,5 +114,6 @@ function schemaValidate(schemaName: string, object: any): void { } module.exports = { - schemaValidate + schemaValidate, + isValidSecret }; diff --git a/src/common/schemas/input/balance-sheet-options.json b/src/common/schemas/input/balance-sheet-options.json deleted file mode 100644 index 85a8547c..00000000 --- a/src/common/schemas/input/balance-sheet-options.json +++ /dev/null @@ -1,15 +0,0 @@ -{ - "$schema": "http://json-schema.org/draft-04/schema#", - "title": "balance-sheet-options", - "description": "Options for getBalanceSheet", - "type": "object", - "properties": { - "excludeAddresses": { - "type": "array", - "items": {"$ref": "address"}, - "uniqueItems": true - }, - "ledgerVersion": {"$ref": "ledgerVersion"} - }, - "additionalProperties": false -} diff --git a/src/common/schemas/input/compute-ledger-hash.json b/src/common/schemas/input/compute-ledger-hash.json new file mode 100644 index 00000000..fc76e6cb --- /dev/null +++ b/src/common/schemas/input/compute-ledger-hash.json @@ -0,0 +1,10 @@ +{ + "$schema": "http://json-schema.org/draft-04/schema#", + "title": "computeLedgerHashParameters", + "type": "object", + "properties": { + "ledger": {"$ref": "getLedger"} + }, + "additionalProperties": false, + "required": ["ledger"] +} diff --git a/src/common/schemas/input/get-balance-sheet.json b/src/common/schemas/input/get-balance-sheet.json new file mode 100644 index 00000000..61f3d465 --- /dev/null +++ b/src/common/schemas/input/get-balance-sheet.json @@ -0,0 +1,22 @@ +{ + "$schema": "http://json-schema.org/draft-04/schema#", + "title": "getBalanceSheetParameters", + "description": "Parameters for getBalanceSheet", + "type": "object", + "properties": { + "address": {"$ref": "address"}, + "options": { + "properties": { + "excludeAddresses": { + "type": "array", + "items": {"$ref": "address"}, + "uniqueItems": true + }, + "ledgerVersion": {"$ref": "ledgerVersion"} + }, + "additionalProperties": false + } + }, + "additionalProperties": false, + "required": ["address"] +} diff --git a/src/common/schemas/input/get-ledger.json b/src/common/schemas/input/get-ledger.json new file mode 100644 index 00000000..edffbca9 --- /dev/null +++ b/src/common/schemas/input/get-ledger.json @@ -0,0 +1,18 @@ +{ + "$schema": "http://json-schema.org/draft-04/schema#", + "title": "getLedgerParameters", + "description": "Parameters for getLedger", + "type": "object", + "properties": { + "options": { + "properties": { + "ledgerVersion": {"$ref": "ledgerVersion"}, + "includeAllData": {"type": "boolean"}, + "includeTransactions": {"type": "boolean"}, + "includeState": {"type": "boolean"} + }, + "additionalProperties": false + } + }, + "additionalProperties": false +} diff --git a/src/common/schemas/input/get-orderbook.json b/src/common/schemas/input/get-orderbook.json new file mode 100644 index 00000000..62245a2b --- /dev/null +++ b/src/common/schemas/input/get-orderbook.json @@ -0,0 +1,25 @@ +{ + "$schema": "http://json-schema.org/draft-04/schema#", + "title": "getOrderbookParameters", + "description": "Parameters for getOrderbook", + "type": "object", + "properties": { + "address": { + "$ref": "address", + "description": "Address of an account to use as point-of-view. (This affects which unfunded offers are returned.)" + }, + "orderbook": {"$ref": "orderbook"}, + "options": { + "properties": { + "limit": { + "type": "integer", + "minimum": 1 + }, + "ledgerVersion": {"$ref": "ledgerVersion"} + }, + "additionalProperties": false + } + }, + "required": ["address", "orderbook"], + "additionalProperties": false +} diff --git a/src/common/schemas/input/get-orders.json b/src/common/schemas/input/get-orders.json new file mode 100644 index 00000000..ec70572d --- /dev/null +++ b/src/common/schemas/input/get-orders.json @@ -0,0 +1,21 @@ +{ + "$schema": "http://json-schema.org/draft-04/schema#", + "title": "getOrdersParameters", + "description": "Parameters for getOrders", + "type": "object", + "properties": { + "address": {"$ref": "address"}, + "options": { + "properties": { + "limit": { + "type": "integer", + "minimum": 1 + }, + "ledgerVersion": {"$ref": "ledgerVersion"} + }, + "additionalProperties": false + } + }, + "required": ["address"], + "additionalProperties": false +} diff --git a/src/common/schemas/input/get-paths.json b/src/common/schemas/input/get-paths.json new file mode 100644 index 00000000..edad40c6 --- /dev/null +++ b/src/common/schemas/input/get-paths.json @@ -0,0 +1,49 @@ +{ + "$schema": "http://json-schema.org/draft-04/schema#", + "title": "getPathsParameters", + "type": "object", + "properties": { + "pathfind": { + "properties": { + "source": { + "type": "object", + "properties": { + "address": {"$ref": "address"}, + "amount": {"$ref": "laxAmount"}, + "currencies": { + "type": "array", + "items": { + "type": "object", + "properties": { + "currency": {"$ref": "currency"}, + "counterparty": {"$ref": "address"} + }, + "required": ["currency"], + "additionalProperties": false + }, + "uniqueItems": true + } + }, + "not": { + "required": ["amount", "currencies"] + }, + "additionalProperties": false, + "required": ["address"] + }, + "destination": { + "type": "object", + "properties": { + "address": {"$ref": "address"}, + "amount": {"$ref": "laxLaxAmount"} + }, + "required": ["address", "amount"], + "additionalProperties": false + } + }, + "required": ["source", "destination"], + "additionalProperties": false + } + }, + "additionalProperties": false, + "required": ["pathfind"] +} diff --git a/src/common/schemas/input/get-settings.json b/src/common/schemas/input/get-settings.json new file mode 100644 index 00000000..1c29722e --- /dev/null +++ b/src/common/schemas/input/get-settings.json @@ -0,0 +1,17 @@ +{ + "$schema": "http://json-schema.org/draft-04/schema#", + "title": "getSettingsParameters", + "description": "Parameters for getSettings and getAccountInfo", + "type": "object", + "properties": { + "address": {"$ref": "address"}, + "options": { + "properties": { + "ledgerVersion": {"$ref": "ledgerVersion"} + }, + "additionalProperties": false + } + }, + "required": ["address"], + "additionalProperties": false +} diff --git a/src/common/schemas/input/get-transaction.json b/src/common/schemas/input/get-transaction.json new file mode 100644 index 00000000..82bb07b1 --- /dev/null +++ b/src/common/schemas/input/get-transaction.json @@ -0,0 +1,18 @@ +{ + "$schema": "http://json-schema.org/draft-04/schema#", + "title": "getTransactionParameters", + "description": "Parameters for getTransaction", + "type": "object", + "properties": { + "identifier": {"$ref": "identifier"}, + "options": { + "properties": { + "minLedgerVersion": {"$ref": "ledgerVersion"}, + "maxLedgerVersion": {"$ref": "ledgerVersion"} + }, + "additionalProperties": false + } + }, + "additionalProperties": false, + "required": ["identifier"] +} diff --git a/src/common/schemas/input/get-transactions.json b/src/common/schemas/input/get-transactions.json new file mode 100644 index 00000000..0a694b1d --- /dev/null +++ b/src/common/schemas/input/get-transactions.json @@ -0,0 +1,46 @@ +{ + "$schema": "http://json-schema.org/draft-04/schema#", + "title": "getTransactionsParameters", + "description": "Parameters for getTransactions", + "type": "object", + "properties": { + "address": {"$ref": "address"}, + "options": { + "properties": { + "start": {"$ref": "hash256"}, + "limit": { + "type": "integer", + "minimum": 1 + }, + "minLedgerVersion": {"$ref": "ledgerVersion"}, + "maxLedgerVersion": {"$ref": "ledgerVersion"}, + "earliestFirst": {"type": "boolean"}, + "excludeFailures": {"type": "boolean"}, + "initiated": {"type": "boolean"}, + "counterparty": {"$ref": "address"}, + "types": { + "type": "array", + "items": { + "enum": [ + "payment", + "trustline", + "order", + "orderCancellation", + "settings" + ] + } + }, + "binary": {"type": "boolean"} + }, + "additionalProperties": false, + "not": { + "anyOf": [ + {"required": ["start", "minLedgerVersion"]}, + {"required": ["start", "maxLedgerVersion"]} + ] + } + } + }, + "additionalProperties": false, + "required": ["address"] +} diff --git a/src/common/schemas/input/get-trustlines.json b/src/common/schemas/input/get-trustlines.json new file mode 100644 index 00000000..2c6e3b04 --- /dev/null +++ b/src/common/schemas/input/get-trustlines.json @@ -0,0 +1,23 @@ +{ + "$schema": "http://json-schema.org/draft-04/schema#", + "title": "getTrustlinesParameters", + "description": "Parameters for getTrustlines and getBalances", + "type": "object", + "properties": { + "address": {"$ref": "address"}, + "options": { + "properties": { + "counterparty": {"$ref": "address"}, + "currency": {"$ref": "currency"}, + "limit": { + "type": "integer", + "minimum": 1 + }, + "ledgerVersion": {"$ref": "ledgerVersion"} + }, + "additionalProperties": false + } + }, + "additionalProperties": false, + "required": ["address"] +} diff --git a/src/common/schemas/input/ledger-options.json b/src/common/schemas/input/ledger-options.json deleted file mode 100644 index f534c613..00000000 --- a/src/common/schemas/input/ledger-options.json +++ /dev/null @@ -1,13 +0,0 @@ -{ - "$schema": "http://json-schema.org/draft-04/schema#", - "title": "ledger-options", - "description": "Options for getLedger", - "type": "object", - "properties": { - "ledgerVersion": {"$ref": "ledgerVersion"}, - "includeAllData": {"type": "boolean"}, - "includeTransactions": {"type": "boolean"}, - "includeState": {"type": "boolean"} - }, - "additionalProperties": false -} diff --git a/src/common/schemas/input/orders-options.json b/src/common/schemas/input/orders-options.json deleted file mode 100644 index d95045a0..00000000 --- a/src/common/schemas/input/orders-options.json +++ /dev/null @@ -1,14 +0,0 @@ -{ - "$schema": "http://json-schema.org/draft-04/schema#", - "title": "orders-options", - "description": "Options for getOrders and getOrderbook", - "type": "object", - "properties": { - "limit": { - "type": "integer", - "minimum": 1 - }, - "ledgerVersion": {"$ref": "ledgerVersion"} - }, - "additionalProperties": false -} diff --git a/src/common/schemas/input/pathfind.json b/src/common/schemas/input/pathfind.json deleted file mode 100644 index 4a37f6e2..00000000 --- a/src/common/schemas/input/pathfind.json +++ /dev/null @@ -1,43 +0,0 @@ -{ - "$schema": "http://json-schema.org/draft-04/schema#", - "title": "pathfind", - "type": "object", - "properties": { - "source": { - "type": "object", - "properties": { - "address": {"$ref": "address"}, - "amount": {"$ref": "laxAmount"}, - "currencies": { - "type": "array", - "items": { - "type": "object", - "properties": { - "currency": {"$ref": "currency"}, - "counterparty": {"$ref": "address"} - }, - "required": ["currency"], - "additionalProperties": false - }, - "uniqueItems": true - }, - "not": { - "required": ["amount", "currencies"] - } - }, - "additionalProperties": false, - "required": ["address"] - }, - "destination": { - "type": "object", - "properties": { - "address": {"$ref": "address"}, - "amount": {"$ref": "laxLaxAmount"} - }, - "required": ["address", "amount"], - "additionalProperties": false - } - }, - "required": ["source", "destination"], - "additionalProperties": false -} diff --git a/src/common/schemas/input/prepare-order-cancellation.json b/src/common/schemas/input/prepare-order-cancellation.json new file mode 100644 index 00000000..88110b36 --- /dev/null +++ b/src/common/schemas/input/prepare-order-cancellation.json @@ -0,0 +1,12 @@ +{ + "$schema": "http://json-schema.org/draft-04/schema#", + "title": "prepareOrderCancellationParameters", + "type": "object", + "properties": { + "address": {"$ref": "address"}, + "sequence": {"$ref": "sequence"}, + "instructions": {"$ref": "instructions"} + }, + "additionalProperties": false, + "required": ["address", "sequence"] +} diff --git a/src/common/schemas/input/prepare-order.json b/src/common/schemas/input/prepare-order.json new file mode 100644 index 00000000..9755fbb2 --- /dev/null +++ b/src/common/schemas/input/prepare-order.json @@ -0,0 +1,12 @@ +{ + "$schema": "http://json-schema.org/draft-04/schema#", + "title": "prepareOrderParameters", + "type": "object", + "properties": { + "address": {"$ref": "address"}, + "order": {"$ref": "order"}, + "instructions": {"$ref": "instructions"} + }, + "additionalProperties": false, + "required": ["address", "order"] +} diff --git a/src/common/schemas/input/prepare-payment.json b/src/common/schemas/input/prepare-payment.json new file mode 100644 index 00000000..7f5f076c --- /dev/null +++ b/src/common/schemas/input/prepare-payment.json @@ -0,0 +1,12 @@ +{ + "$schema": "http://json-schema.org/draft-04/schema#", + "title": "preparePaymentParameters", + "type": "object", + "properties": { + "address": {"$ref": "address"}, + "payment": {"$ref": "payment"}, + "instructions": {"$ref": "instructions"} + }, + "additionalProperties": false, + "required": ["address", "payment"] +} diff --git a/src/common/schemas/input/prepare-settings.json b/src/common/schemas/input/prepare-settings.json new file mode 100644 index 00000000..0ac20a29 --- /dev/null +++ b/src/common/schemas/input/prepare-settings.json @@ -0,0 +1,12 @@ +{ + "$schema": "http://json-schema.org/draft-04/schema#", + "title": "prepareSettingsParameters", + "type": "object", + "properties": { + "address": {"$ref": "address"}, + "settings": {"$ref": "settings"}, + "instructions": {"$ref": "instructions"} + }, + "additionalProperties": false, + "required": ["address", "settings"] +} diff --git a/src/common/schemas/input/prepare-suspended-payment-cancellation.json b/src/common/schemas/input/prepare-suspended-payment-cancellation.json new file mode 100644 index 00000000..770b61df --- /dev/null +++ b/src/common/schemas/input/prepare-suspended-payment-cancellation.json @@ -0,0 +1,12 @@ +{ + "$schema": "http://json-schema.org/draft-04/schema#", + "title": "prepareSuspendedPaymentCancellationParameters", + "type": "object", + "properties": { + "address": {"$ref": "address"}, + "suspendedPaymentCancellation": {"$ref": "suspendedPaymentCancellation"}, + "instructions": {"$ref": "instructions"} + }, + "additionalProperties": false, + "required": ["address", "suspendedPaymentCancellation"] +} diff --git a/src/common/schemas/input/prepare-suspended-payment-creation.json b/src/common/schemas/input/prepare-suspended-payment-creation.json new file mode 100644 index 00000000..27a4192e --- /dev/null +++ b/src/common/schemas/input/prepare-suspended-payment-creation.json @@ -0,0 +1,12 @@ +{ + "$schema": "http://json-schema.org/draft-04/schema#", + "title": "prepareSuspendedPaymentCreationParameters", + "type": "object", + "properties": { + "address": {"$ref": "address"}, + "suspendedPaymentCreation": {"$ref": "suspendedPaymentCreation"}, + "instructions": {"$ref": "instructions"} + }, + "additionalProperties": false, + "required": ["address", "suspendedPaymentCreation"] +} diff --git a/src/common/schemas/input/prepare-suspended-payment-execution.json b/src/common/schemas/input/prepare-suspended-payment-execution.json new file mode 100644 index 00000000..32a15ebd --- /dev/null +++ b/src/common/schemas/input/prepare-suspended-payment-execution.json @@ -0,0 +1,12 @@ +{ + "$schema": "http://json-schema.org/draft-04/schema#", + "title": "prepareSuspendedPaymentExecutionParameters", + "type": "object", + "properties": { + "address": {"$ref": "address"}, + "suspendedPaymentExecution": {"$ref": "suspendedPaymentExecution"}, + "instructions": {"$ref": "instructions"} + }, + "additionalProperties": false, + "required": ["address", "suspendedPaymentExecution"] +} diff --git a/src/common/schemas/input/prepare-trustline.json b/src/common/schemas/input/prepare-trustline.json new file mode 100644 index 00000000..a73fc612 --- /dev/null +++ b/src/common/schemas/input/prepare-trustline.json @@ -0,0 +1,12 @@ +{ + "$schema": "http://json-schema.org/draft-04/schema#", + "title": "prepareTrustlineParameters", + "type": "object", + "properties": { + "address": {"$ref": "address"}, + "trustline": {"$ref": "trustline"}, + "instructions": {"$ref": "instructions"} + }, + "additionalProperties": false, + "required": ["address", "trustline"] +} diff --git a/src/common/schemas/input/settings-options.json b/src/common/schemas/input/settings-options.json deleted file mode 100644 index eca8136c..00000000 --- a/src/common/schemas/input/settings-options.json +++ /dev/null @@ -1,10 +0,0 @@ -{ - "$schema": "http://json-schema.org/draft-04/schema#", - "title": "settings-options", - "description": "Options for getSettings and getAccountInfo", - "type": "object", - "properties": { - "ledgerVersion": {"$ref": "ledgerVersion"} - }, - "additionalProperties": false -} diff --git a/src/common/schemas/input/sign.json b/src/common/schemas/input/sign.json new file mode 100644 index 00000000..4e9d6db9 --- /dev/null +++ b/src/common/schemas/input/sign.json @@ -0,0 +1,18 @@ +{ + "$schema": "http://json-schema.org/draft-04/schema#", + "title": "signParameters", + "type": "object", + "properties": { + "txJSON": { + "type": "string", + "description": "Transaction represented as a JSON string in rippled format." + }, + "secret": { + "type": "string", + "format": "secret", + "description": "The secret of the account that is initiating the transaction." + } + }, + "additionalProperties": false, + "required": ["txJSON", "secret"] +} diff --git a/src/common/schemas/input/submit.json b/src/common/schemas/input/submit.json new file mode 100644 index 00000000..99a1579c --- /dev/null +++ b/src/common/schemas/input/submit.json @@ -0,0 +1,10 @@ +{ + "$schema": "http://json-schema.org/draft-04/schema#", + "title": "submitParameters", + "type": "object", + "properties": { + "txBlob": {"$ref": "blob"} + }, + "additionalProperties": false, + "required": ["txBlob"] +} diff --git a/src/common/schemas/input/transaction-options.json b/src/common/schemas/input/transaction-options.json deleted file mode 100644 index c05c1ec4..00000000 --- a/src/common/schemas/input/transaction-options.json +++ /dev/null @@ -1,11 +0,0 @@ -{ - "$schema": "http://json-schema.org/draft-04/schema#", - "title": "transaction-options", - "description": "Options for getTransaction", - "type": "object", - "properties": { - "minLedgerVersion": {"$ref": "ledgerVersion"}, - "maxLedgerVersion": {"$ref": "ledgerVersion"} - }, - "additionalProperties": false -} diff --git a/src/common/schemas/input/transactions-options.json b/src/common/schemas/input/transactions-options.json deleted file mode 100644 index 7dd60be6..00000000 --- a/src/common/schemas/input/transactions-options.json +++ /dev/null @@ -1,40 +0,0 @@ -{ - "$schema": "http://json-schema.org/draft-04/schema#", - "title": "transactions-options", - "description": "Options for getTransactions", - "type": "object", - "properties": { - "start": {"$ref": "hash256"}, - "limit": { - "type": "integer", - "minimum": 1 - }, - "minLedgerVersion": {"$ref": "ledgerVersion"}, - "maxLedgerVersion": {"$ref": "ledgerVersion"}, - "earliestFirst": {"type": "boolean"}, - "excludeFailures": {"type": "boolean"}, - "initiated": {"type": "boolean"}, - "counterparty": {"$ref": "address"}, - "types": { - "type": "array", - "items": { - "enum": [ - "payment", - "trustline", - "order", - "orderCancellation", - "settings" - ] - } - }, - "binary": {"type": "boolean"} - }, - "additionalProperties": false, - "not": { - "anyOf": [ - {"required": ["incoming", "outgoing"]}, - {"required": ["start", "minLedgerVersion"]}, - {"required": ["start", "maxLedgerVersion"]} - ] - } -} diff --git a/src/common/schemas/input/trustlines-options.json b/src/common/schemas/input/trustlines-options.json deleted file mode 100644 index 705f6169..00000000 --- a/src/common/schemas/input/trustlines-options.json +++ /dev/null @@ -1,16 +0,0 @@ -{ - "$schema": "http://json-schema.org/draft-04/schema#", - "title": "trustlines-options", - "description": "Options for getTrustlines and getBalances", - "type": "object", - "properties": { - "counterparty": {"$ref": "address"}, - "currency": {"$ref": "currency"}, - "limit": { - "type": "integer", - "minimum": 1 - }, - "ledgerVersion": {"$ref": "ledgerVersion"} - }, - "additionalProperties": false -} diff --git a/src/common/schemas/objects/address.json b/src/common/schemas/objects/address.json index afd3efee..a490e6a5 100644 --- a/src/common/schemas/objects/address.json +++ b/src/common/schemas/objects/address.json @@ -4,5 +4,6 @@ "description": "A Ripple account address", "type": "string", "format": "address", + "link": "ripple-address", "pattern": "^r[1-9A-HJ-NP-Za-km-z]{25,34}$" } diff --git a/src/common/schemas/objects/amount.json b/src/common/schemas/objects/amount.json index c027f6b5..de91eae5 100644 --- a/src/common/schemas/objects/amount.json +++ b/src/common/schemas/objects/amount.json @@ -1,6 +1,7 @@ { "$schema": "http://json-schema.org/draft-04/schema#", "title": "amount", + "link": "amount", "description": "An Amount on the Ripple Protocol, used also for XRP in the ripple-rest API", "allOf": [ {"$ref": "amountbase"}, diff --git a/src/common/schemas/objects/balance.json b/src/common/schemas/objects/balance.json index 71ee34cf..afd0a36f 100644 --- a/src/common/schemas/objects/balance.json +++ b/src/common/schemas/objects/balance.json @@ -2,6 +2,7 @@ "$schema": "http://json-schema.org/draft-04/schema#", "title": "balance", "description": "Balance amount", + "link": "amount", "type": "object", "properties": { "value": { diff --git a/src/common/schemas/objects/currency.json b/src/common/schemas/objects/currency.json index 2d8ecf8b..828b0e18 100644 --- a/src/common/schemas/objects/currency.json +++ b/src/common/schemas/objects/currency.json @@ -3,5 +3,6 @@ "title": "currency", "description": "The three-character code or hex string used to denote currencies", "type": "string", + "link": "currency", "pattern": "^([a-zA-Z0-9<>(){}[\\]|?!@#$%^&*]{3}|[A-F0-9]{40})$" } diff --git a/src/common/schemas/input/instructions.json b/src/common/schemas/objects/instructions.json similarity index 96% rename from src/common/schemas/input/instructions.json rename to src/common/schemas/objects/instructions.json index 5d342c4f..f922a92f 100644 --- a/src/common/schemas/input/instructions.json +++ b/src/common/schemas/objects/instructions.json @@ -1,6 +1,7 @@ { "$schema": "http://json-schema.org/draft-04/schema#", "title": "instructions", + "link": "transaction-instructions", "description": "Instructions for executing a transaction", "type": "object", "properties": { diff --git a/src/common/schemas/objects/lax-amount.json b/src/common/schemas/objects/lax-amount.json index 51a3eeef..861d282b 100644 --- a/src/common/schemas/objects/lax-amount.json +++ b/src/common/schemas/objects/lax-amount.json @@ -2,6 +2,7 @@ "$schema": "http://json-schema.org/draft-04/schema#", "title": "laxAmount", "description": "Amount where counterparty is optional", + "link": "amount", "type": "object", "properties": { "currency": {"$ref": "currency"}, diff --git a/src/common/schemas/objects/lax-lax-amount.json b/src/common/schemas/objects/lax-lax-amount.json index 0b27a766..4ef5933e 100644 --- a/src/common/schemas/objects/lax-lax-amount.json +++ b/src/common/schemas/objects/lax-lax-amount.json @@ -2,6 +2,7 @@ "$schema": "http://json-schema.org/draft-04/schema#", "title": "laxLaxAmount", "description": "Amount where counterparty and value are optional", + "link": "amount", "type": "object", "properties": { "currency": {"$ref": "currency"}, diff --git a/src/common/schemas/input/orderbook.json b/src/common/schemas/objects/orderbook.json similarity index 100% rename from src/common/schemas/input/orderbook.json rename to src/common/schemas/objects/orderbook.json diff --git a/src/common/schemas/objects/tx-hash.json b/src/common/schemas/objects/tx-hash.json new file mode 100644 index 00000000..80638528 --- /dev/null +++ b/src/common/schemas/objects/tx-hash.json @@ -0,0 +1,8 @@ +{ + "$schema": "http://json-schema.org/draft-04/schema#", + "title": "identifier", + "link": "transaction-identifier", + "description": "A hash of a transaction used to identify the transaction, represented in hexadecimal.", + "type": "string", + "pattern": "^[A-F0-9]{64}$" +} diff --git a/src/common/schemas/objects/tx-json.json b/src/common/schemas/objects/tx-json.json index c59be65b..bb46377a 100644 --- a/src/common/schemas/objects/tx-json.json +++ b/src/common/schemas/objects/tx-json.json @@ -1,6 +1,7 @@ { "$schema": "http://json-schema.org/draft-04/schema#", "title": "tx", + "link": "https://ripple.com/build/transactions/", "description": "An object in rippled txJSON format", "type": "object", "properties": { diff --git a/src/common/schemas/output/get-server-info.json b/src/common/schemas/output/get-server-info.json index de565f1f..4da6dfe3 100644 --- a/src/common/schemas/output/get-server-info.json +++ b/src/common/schemas/output/get-server-info.json @@ -3,47 +3,117 @@ "title": "getServerInfo", "type": "object", "properties": { - "buildVersion": {"type": "string"}, - "completeLedgers": {"type": "string", "pattern": "[0-9,-]+"}, - "hostid": {"type": "string"}, - "ioLatencyMs": {"type": "number"}, + "buildVersion": { + "type": "string", + "description": "The version number of the running rippled version." + }, + "completeLedgers": { + "type": "string", + "pattern": "[0-9,-]+", + "description": "Range expression indicating the sequence numbers of the ledger versions the local rippled has in its database. It is possible to be a disjoint sequence, e.g. “2500-5000,32570-7695432”." + }, + "hostid": { + "type": "string", + "description": "On an admin request, returns the hostname of the server running the rippled instance; otherwise, returns a unique four letter word." + }, + "ioLatencyMs": { + "type": "number", + "description": "Amount of time spent waiting for I/O operations to be performed, in milliseconds. If this number is not very, very low, then the rippled server is probably having serious load issues." + }, "load": { "type": "object", + "description": "*(Admin only)* Detailed information about the current load state of the server.", "properties": { "jobTypes": { "type": "array", + "description": "*(Admin only)* Information about the rate of different types of jobs being performed by the server and how much time it spends on each.", "items": {"type": "object"} }, - "threads": {"type": "number"} - } + "threads": { + "type": "number", + "description": "*(Admin only)* The number of threads in the server’s main job pool, performing various Ripple Network operations." + } + }, + "required": ["jobTypes", "threads"] }, "lastClose": { "type": "object", + "description": "Information about the last time the server closed a ledger.", "properties": { - "convergeTimeS": {"type": "number"}, - "proposers": {"type": "integer", "minimum": 0} - } + "convergeTimeS": { + "type": "number", + "description": "The time it took to reach a consensus for the last ledger closing." + }, + "proposers": { + "type": "integer", + "minimum": 0, + "description": "Number of trusted validators participating in the ledger closing."} + }, + "required": ["convergeTimeS", "proposers"] + }, + "loadFactor": { + "type": "number", + "description": "The load factor the server is currently enforcing, as a multiplier on the base transaction fee. The load factor is determined by the highest of the individual server’s load factor, cluster’s load factor, and the overall network’s load factor." + }, + "peers": { + "type": "integer", + "minimum": 0, + "description": "How many other rippled servers the node is currently connected to." + }, + "pubkeyNode": { + "type": "string", + "description": "Public key used to verify this node for internal communications; this key is automatically generated by the server the first time it starts up. (If deleted, the node can just create a new pair of keys.)" + }, + "pubkeyValidator": { + "type": "string", + "description": "*(Admin only)* Public key used by this node to sign ledger validations." }, - "loadFactor": {"type": "number"}, - "peers": {"type": "integer", "minimum": 0}, - "pubkeyNode": {"type": "string"}, - "pubkeyValidator": {"type": "string"}, "serverState": { "type": "string", + "description": "A string indicating to what extent the server is participating in the network. See [Possible Server States](https://ripple.com/build/rippled-apis/#possible-server-states) for more details.", "enum": ["disconnected", "connected", "syncing", "tracking", "full", "validating", "proposing"] }, "validatedLedger": { "type": "object", + "description": "Information about the fully-validated ledger with the highest sequence number (the most recent).", "properties": { - "age": {"type": "integer", "minimum": 0}, - "baseFeeXrp": {"type": "number"}, - "hash": {"$ref": "hash256"}, - "reserveBaseXrp": {"type": "integer", "minimum": 0}, - "reserveIncXrp": {"type": "integer", "minimum": 0}, - "seq": {"type": "integer", "minimum": 0} - } + "age": { + "type": "integer", + "minimum": 0, + "description": "The time since the ledger was closed, in seconds." + }, + "baseFeeXrp": { + "type": "number", + "description": "Base fee, in XRP. This may be represented in scientific notation such as 1e-05 for 0.00005." + }, + "hash": { + "$ref": "hash256", + "description": "Unique hash for the ledger, as hex." + }, + "reserveBaseXrp": { + "type": "integer", + "minimum": 0, + "description": "Minimum amount of XRP (not drops) necessary for every account to keep in reserve." + }, + "reserveIncXrp": { + "type": "integer", + "minimum": 0, + "description": "Amount of XRP (not drops) added to the account reserve for each object an account is responsible for in the ledger." + }, + "seq": { + "type": "integer", + "minimum": 0, + "description": "Identifying sequence number of this ledger version." + } + }, + "additionalProperties": false, + "required": ["age", "baseFeeXrp", "hash", "reserveBaseXrp", "reserveIncXrp", "seq"] }, - "validationQuorum": {"type": "number"} + "validationQuorum": { + "type": "number", + "description": "Minimum number of trusted validations required in order to validate a ledger version. Some circumstances may cause the server to require more validations." + } }, + "required": ["buildVersion", "completeLedgers", "hostid", "ioLatencyMs", "lastClose", "loadFactor", "peers", "pubkeyNode", "serverState", "validatedLedger", "validationQuorum"], "additionalProperties": false } diff --git a/src/common/schemas/output/get-settings.json b/src/common/schemas/output/get-settings.json index 16c44ece..b2381b3e 100644 --- a/src/common/schemas/output/get-settings.json +++ b/src/common/schemas/output/get-settings.json @@ -1,6 +1,7 @@ { "$schema": "http://json-schema.org/draft-04/schema#", "title": "getSettings", + "link": "settings-specification", "type": "object", "properties": { "passwordSpent": {"type": "boolean"}, diff --git a/src/common/schemas/output/outcome.json b/src/common/schemas/output/outcome.json index a02452ab..58d60d9b 100644 --- a/src/common/schemas/output/outcome.json +++ b/src/common/schemas/output/outcome.json @@ -2,6 +2,7 @@ "$schema": "http://json-schema.org/draft-04/schema#", "title": "outcome", "type": "object", + "link": "transaction-outcome", "properties": { "result": {"type": "string"}, "timestamp": {"type": "string", "format": "date-time"}, diff --git a/src/common/schemas/output/settings-transaction.json b/src/common/schemas/output/settings-transaction.json index d55959f3..680bbd40 100644 --- a/src/common/schemas/output/settings-transaction.json +++ b/src/common/schemas/output/settings-transaction.json @@ -10,6 +10,6 @@ "address": {"$ref": "address"}, "sequence": {"$ref": "sequence"} }, - "required": ["type", "id", "address", "sequence", "specification"], + "required": ["type", "id", "address", "sequence", "specification", "outcome"], "additionalProperties": false } diff --git a/src/common/schemas/input/order-cancellation.json b/src/common/schemas/specifications/order-cancellation.json similarity index 84% rename from src/common/schemas/input/order-cancellation.json rename to src/common/schemas/specifications/order-cancellation.json index 0521c3ec..3b110f1b 100644 --- a/src/common/schemas/input/order-cancellation.json +++ b/src/common/schemas/specifications/order-cancellation.json @@ -1,6 +1,7 @@ { "$schema": "http://json-schema.org/draft-04/schema#", "title": "orderCancellation", + "link": "order-cancellation-specification", "type": "object", "properties": { "orderSequence": {"$ref": "sequence"} diff --git a/src/common/schemas/input/order.json b/src/common/schemas/specifications/order.json similarity index 96% rename from src/common/schemas/input/order.json rename to src/common/schemas/specifications/order.json index f11f044c..a4ec5ba7 100644 --- a/src/common/schemas/input/order.json +++ b/src/common/schemas/specifications/order.json @@ -1,6 +1,7 @@ { "$schema": "http://json-schema.org/draft-04/schema#", "title": "order", + "link": "order-specification", "type": "object", "properties": { "direction": { diff --git a/src/common/schemas/input/payment.json b/src/common/schemas/specifications/payment.json similarity index 97% rename from src/common/schemas/input/payment.json rename to src/common/schemas/specifications/payment.json index 40ec6480..71f6d46b 100644 --- a/src/common/schemas/input/payment.json +++ b/src/common/schemas/specifications/payment.json @@ -1,6 +1,7 @@ { "$schema": "http://json-schema.org/draft-04/schema#", "title": "payment", + "link": "payment-specification", "type": "object", "properties": { "source": {"$ref": "sourceAdjustment"}, diff --git a/src/common/schemas/input/settings.json b/src/common/schemas/specifications/settings.json similarity index 85% rename from src/common/schemas/input/settings.json rename to src/common/schemas/specifications/settings.json index 93e20469..044d423f 100644 --- a/src/common/schemas/input/settings.json +++ b/src/common/schemas/specifications/settings.json @@ -1,6 +1,7 @@ { "$schema": "http://json-schema.org/draft-04/schema#", "title": "settings", + "link": "settings-specification", "allOf": [ { "$ref": "getSettings" diff --git a/src/common/schemas/input/suspended-payment-cancellation.json b/src/common/schemas/specifications/suspended-payment-cancellation.json similarity index 77% rename from src/common/schemas/input/suspended-payment-cancellation.json rename to src/common/schemas/specifications/suspended-payment-cancellation.json index 30e8e445..82c1373c 100644 --- a/src/common/schemas/input/suspended-payment-cancellation.json +++ b/src/common/schemas/specifications/suspended-payment-cancellation.json @@ -1,6 +1,7 @@ { "$schema": "http://json-schema.org/draft-04/schema#", - "title": "suspended-payment-cancellation", + "title": "suspendedPaymentCancellation", + "link": "suspended-payment-cancellation-specification", "type": "object", "properties": { "memos": { diff --git a/src/common/schemas/input/suspended-payment-creation.json b/src/common/schemas/specifications/suspended-payment-creation.json similarity index 84% rename from src/common/schemas/input/suspended-payment-creation.json rename to src/common/schemas/specifications/suspended-payment-creation.json index e575653d..4fa48dff 100644 --- a/src/common/schemas/input/suspended-payment-creation.json +++ b/src/common/schemas/specifications/suspended-payment-creation.json @@ -1,6 +1,7 @@ { "$schema": "http://json-schema.org/draft-04/schema#", - "title": "suspended-payment-creation", + "title": "suspendedPaymentCreation", + "link": "suspended-payment-creation-specification", "type": "object", "properties": { "source": {"$ref": "maxAdjustment"}, diff --git a/src/common/schemas/input/suspended-payment-execution.json b/src/common/schemas/specifications/suspended-payment-execution.json similarity index 83% rename from src/common/schemas/input/suspended-payment-execution.json rename to src/common/schemas/specifications/suspended-payment-execution.json index cde068a5..3dd380a8 100644 --- a/src/common/schemas/input/suspended-payment-execution.json +++ b/src/common/schemas/specifications/suspended-payment-execution.json @@ -1,6 +1,7 @@ { "$schema": "http://json-schema.org/draft-04/schema#", - "title": "suspended-payment-execution", + "title": "suspendedPaymentExecution", + "link": "suspended-payment-execution-specification", "type": "object", "properties": { "memos": { diff --git a/src/common/schemas/input/trustline.json b/src/common/schemas/specifications/trustline.json similarity index 93% rename from src/common/schemas/input/trustline.json rename to src/common/schemas/specifications/trustline.json index 6c00419c..6194e8e0 100644 --- a/src/common/schemas/input/trustline.json +++ b/src/common/schemas/specifications/trustline.json @@ -1,6 +1,7 @@ { "$schema": "http://json-schema.org/draft-04/schema#", "title": "trustline", + "link": "trustline-specification", "type": "object", "properties": { "currency": {"$ref": "currency"}, diff --git a/src/common/utils.js b/src/common/utils.js index 0ce92cd3..3b964419 100644 --- a/src/common/utils.js +++ b/src/common/utils.js @@ -4,9 +4,19 @@ const _ = require('lodash'); const BigNumber = require('bignumber.js'); const errors = require('./errors'); const keypairs = require('ripple-keypairs'); +const {deriveKeypair} = require('ripple-keypairs'); import type {Amount, RippledAmount} from './types.js'; +function isValidSecret(secret: string): boolean { + try { + deriveKeypair(secret); + return true; + } catch (err) { + return false; + } +} + function dropsToXrp(drops: string): string { return (new BigNumber(drops)).dividedBy(1000000.0).toString(); } @@ -96,5 +106,6 @@ module.exports = { convertKeysFromSnakeCaseToCamelCase, removeUndefined, rippleTimeToISO8601, - iso8601ToRippleTime + iso8601ToRippleTime, + isValidSecret }; diff --git a/src/common/validate.js b/src/common/validate.js index fc9ac1f6..26a5d76a 100644 --- a/src/common/validate.js +++ b/src/common/validate.js @@ -1,7 +1,6 @@ /* @flow */ 'use strict'; const _ = require('lodash'); -const deriveKeypair = require('ripple-keypairs').deriveKeypair; const ValidationError = require('./errors').ValidationError; const schemaValidate = require('./schema-validator').schemaValidate; @@ -9,27 +8,8 @@ function error(text) { return new ValidationError(text); } -function isValidSecret(secret) { - try { - deriveKeypair(secret); - return true; - } catch (err) { - return false; - } -} - -function validateSecret(secret: string): void { - if (!secret) { - throw error('Parameter missing: secret'); - } - if (typeof secret !== 'string' || secret[0] !== 's' - || !isValidSecret(secret)) { - throw error('Invalid parameter: secret'); - } -} - function validateLedgerRange(options) { - if (!_.isUndefined(options.minLedgerVersion) + if (!_.isUndefined(options) && !_.isUndefined(options.minLedgerVersion) && !_.isUndefined(options.maxLedgerVersion)) { if (Number(options.minLedgerVersion) > Number(options.maxLedgerVersion)) { throw error('minLedgerVersion must not be greater than maxLedgerVersion'); @@ -37,43 +17,38 @@ function validateLedgerRange(options) { } } -function validateOptions(schema, options) { - schemaValidate(schema, options); - validateLedgerRange(options); +function validateOptions(schema, instance) { + schemaValidate(schema, instance); + validateLedgerRange(instance.options); } module.exports = { - address: _.partial(schemaValidate, 'address'), - secret: validateSecret, - currency: _.partial(schemaValidate, 'currency'), - identifier: _.partial(schemaValidate, 'hash256'), - ledgerVersion: _.partial(schemaValidate, 'ledgerVersion'), - sequence: _.partial(schemaValidate, 'sequence'), - order: _.partial(schemaValidate, 'order'), - orderbook: _.partial(schemaValidate, 'orderbook'), - payment: _.partial(schemaValidate, 'payment'), - suspendedPaymentCreation: - _.partial(schemaValidate, 'suspended-payment-creation'), - suspendedPaymentExecution: - _.partial(schemaValidate, 'suspended-payment-execution'), - suspendedPaymentCancellation: - _.partial(schemaValidate, 'suspended-payment-cancellation'), - pathfind: _.partial(schemaValidate, 'pathfind'), - settings: _.partial(schemaValidate, 'settings'), - trustline: _.partial(schemaValidate, 'trustline'), - txJSON: _.partial(schemaValidate, 'tx'), - blob: _.partial(schemaValidate, 'blob'), - getTransactionsOptions: _.partial(validateOptions, 'transactions-options'), - getSettingsOptions: _.partial(validateOptions, 'settings-options'), - getAccountInfoOptions: _.partial(validateOptions, 'settings-options'), - getTrustlinesOptions: _.partial(validateOptions, 'trustlines-options'), - getBalancesOptions: _.partial(validateOptions, 'trustlines-options'), - getBalanceSheetOptions: _.partial(validateOptions, 'balance-sheet-options'), - getOrdersOptions: _.partial(validateOptions, 'orders-options'), - getOrderbookOptions: _.partial(validateOptions, 'orders-options'), - getTransactionOptions: _.partial(validateOptions, 'transaction-options'), - getLedgerOptions: _.partial(validateOptions, 'ledger-options'), - options: _.partial(validateOptions, 'options'), + getPaths: _.partial(schemaValidate, 'getPathsParameters'), + getTransactions: _.partial(validateOptions, 'getTransactionsParameters'), + getSettings: _.partial(validateOptions, 'getSettingsParameters'), + getAccountInfo: _.partial(validateOptions, 'getSettingsParameters'), + getTrustlines: _.partial(validateOptions, 'getTrustlinesParameters'), + getBalances: _.partial(validateOptions, 'getTrustlinesParameters'), + getBalanceSheet: _.partial(validateOptions, 'getBalanceSheetParameters'), + getOrders: _.partial(validateOptions, 'getOrdersParameters'), + getOrderbook: _.partial(validateOptions, 'getOrderbookParameters'), + getTransaction: _.partial(validateOptions, 'getTransactionParameters'), + getLedger: _.partial(validateOptions, 'getLedgerParameters'), + preparePayment: _.partial(schemaValidate, 'preparePaymentParameters'), + prepareOrder: _.partial(schemaValidate, 'prepareOrderParameters'), + prepareOrderCancellation: + _.partial(schemaValidate, 'prepareOrderCancellationParameters'), + prepareTrustline: _.partial(schemaValidate, 'prepareTrustlineParameters'), + prepareSettings: _.partial(schemaValidate, 'prepareSettingsParameters'), + prepareSuspendedPaymentCreation: _.partial(schemaValidate, + 'prepareSuspendedPaymentCreationParameters'), + prepareSuspendedPaymentCancellation: _.partial(schemaValidate, + 'prepareSuspendedPaymentCancellationParameters'), + prepareSuspendedPaymentExecution: _.partial(schemaValidate, + 'prepareSuspendedPaymentExecutionParameters'), + sign: _.partial(schemaValidate, 'signParameters'), + submit: _.partial(schemaValidate, 'submitParameters'), + computeLedgerHash: _.partial(schemaValidate, 'computeLedgerHashParameters'), apiOptions: _.partial(schemaValidate, 'api-options'), instructions: _.partial(schemaValidate, 'instructions') }; diff --git a/src/ledger/accountinfo.js b/src/ledger/accountinfo.js index efd64bdd..61eb9d75 100644 --- a/src/ledger/accountinfo.js +++ b/src/ledger/accountinfo.js @@ -50,14 +50,13 @@ function formatAccountInfo(response: AccountDataResponse) { }); } -function getAccountInfo(account: string, options: AccountInfoOptions = {} +function getAccountInfo(address: string, options: AccountInfoOptions = {} ): Promise { - validate.address(account); - validate.getAccountInfoOptions(options); + validate.getAccountInfo({address, options}); const request = { command: 'account_info', - account: account, + account: address, ledger_index: options.ledgerVersion || 'validated' }; diff --git a/src/ledger/balance-sheet.js b/src/ledger/balance-sheet.js index f451ec52..f761a0d5 100644 --- a/src/ledger/balance-sheet.js +++ b/src/ledger/balance-sheet.js @@ -49,8 +49,7 @@ function formatBalanceSheet(balanceSheet): GetBalanceSheet { function getBalanceSheet(address: string, options: BalanceSheetOptions = {} ): Promise { - validate.address(address); - validate.getBalanceSheetOptions(options); + validate.getBalanceSheet({address, options}); return utils.ensureLedgerVersion.call(this, options).then(_options => { const request = { diff --git a/src/ledger/balances.js b/src/ledger/balances.js index 96cd25d7..69a20b82 100644 --- a/src/ledger/balances.js +++ b/src/ledger/balances.js @@ -48,16 +48,15 @@ function getLedgerVersionHelper(connection: Connection, optionValue?: number return connection.getLedgerVersion(); } -function getBalances(account: string, options: TrustlinesOptions = {} +function getBalances(address: string, options: TrustlinesOptions = {} ): Promise { - validate.address(account); - validate.getBalancesOptions(options); + validate.getTrustlines({address, options}); return Promise.all([ getLedgerVersionHelper(this.connection, options.ledgerVersion).then( ledgerVersion => - utils.getXRPBalance(this.connection, account, ledgerVersion)), - this.getTrustlines(account, options) + utils.getXRPBalance(this.connection, address, ledgerVersion)), + this.getTrustlines(address, options) ]).then(results => formatBalances(options, {xrp: results[0], trustlines: results[1]})); } diff --git a/src/ledger/ledger.js b/src/ledger/ledger.js index 6088ce02..299ccbdc 100644 --- a/src/ledger/ledger.js +++ b/src/ledger/ledger.js @@ -14,7 +14,7 @@ type LedgerOptions = { function getLedger(options: LedgerOptions = {}): Promise { - validate.getLedgerOptions(options); + validate.getLedger({options}); const request = { command: 'ledger', diff --git a/src/ledger/orderbook.js b/src/ledger/orderbook.js index 1222b922..9ffa9b62 100644 --- a/src/ledger/orderbook.js +++ b/src/ledger/orderbook.js @@ -91,14 +91,12 @@ function formatBidsAndAsks(orderbook: Orderbook, offers) { return {bids, asks}; } -function getOrderbook(account: string, orderbook: Orderbook, +function getOrderbook(address: string, orderbook: Orderbook, options: OrdersOptions = {} ): Promise { - validate.address(account); - validate.orderbook(orderbook); - validate.getOrderbookOptions(options); + validate.getOrderbook({address, orderbook, options}); - const getter = _.partial(getBookOffers, this.connection, account, + const getter = _.partial(getBookOffers, this.connection, address, options.ledgerVersion, options.limit); const getOffers = _.partial(getter, orderbook.base, orderbook.counter); const getReverseOffers = _.partial(getter, orderbook.counter, orderbook.base); diff --git a/src/ledger/orders.js b/src/ledger/orders.js index 6b399eb2..d921cc9a 100644 --- a/src/ledger/orders.js +++ b/src/ledger/orders.js @@ -26,13 +26,12 @@ function requestAccountOffers(connection: Connection, address: string, }); } -function getOrders(account: string, options: OrdersOptions = {} +function getOrders(address: string, options: OrdersOptions = {} ): Promise { - validate.address(account); - validate.getOrdersOptions(options); + validate.getOrders({address, options}); return utils.ensureLedgerVersion.call(this, options).then(_options => { - const getter = _.partial(requestAccountOffers, this.connection, account, + const getter = _.partial(requestAccountOffers, this.connection, address, _options.ledgerVersion); return utils.getRecursive(getter, _options.limit).then(orders => _.sortBy(orders, (order) => order.properties.sequence)); diff --git a/src/ledger/pathfind.js b/src/ledger/pathfind.js index 17380bfb..1d1f93ed 100644 --- a/src/ledger/pathfind.js +++ b/src/ledger/pathfind.js @@ -111,7 +111,7 @@ function formatResponse(pathfind: PathFind, paths: RippledPathsResponse) { } function getPaths(pathfind: PathFind): Promise { - validate.pathfind(pathfind); + validate.getPaths({pathfind}); const address = pathfind.source.address; return requestPathFind(this.connection, pathfind).then(paths => diff --git a/src/ledger/settings.js b/src/ledger/settings.js index 87705ce0..959a21d8 100644 --- a/src/ledger/settings.js +++ b/src/ledger/settings.js @@ -48,14 +48,13 @@ function formatSettings(response) { return _.assign({}, parsedFlags, parsedFields); } -function getSettings(account: string, options: SettingsOptions = {} +function getSettings(address: string, options: SettingsOptions = {} ): Promise { - validate.address(account); - validate.getSettingsOptions(options); + validate.getSettings({address, options}); const request = { command: 'account_info', - account: account, + account: address, ledger_index: options.ledgerVersion || 'validated' }; diff --git a/src/ledger/transaction.js b/src/ledger/transaction.js index fbd42249..5e072fcd 100644 --- a/src/ledger/transaction.js +++ b/src/ledger/transaction.js @@ -78,8 +78,7 @@ function formatResponse(options: TransactionOptions, tx: TransactionType function getTransaction(identifier: string, options: TransactionOptions = {} ): Promise { - validate.identifier(identifier); - validate.getTransactionOptions(options); + validate.getTransaction({identifier, options}); const request = { command: 'tx', diff --git a/src/ledger/transactions.js b/src/ledger/transactions.js index 63ee1b39..fe805b08 100644 --- a/src/ledger/transactions.js +++ b/src/ledger/transactions.js @@ -163,10 +163,9 @@ function getTransactionsInternal(connection: Connection, address: string, return utils.getRecursive(getter, options.limit).then(format); } -function getTransactions(account: string, options: TransactionsOptions = {} +function getTransactions(address: string, options: TransactionsOptions = {} ): Promise { - validate.address(account); - validate.getTransactionsOptions(options); + validate.getTransactions({address, options}); const defaults = {maxLedgerVersion: -1}; if (options.start) { @@ -175,11 +174,11 @@ function getTransactions(account: string, options: TransactionsOptions = {} const bound = options.earliestFirst ? {minLedgerVersion: ledgerVersion} : {maxLedgerVersion: ledgerVersion}; const newOptions = _.assign(defaults, options, {startTx: tx}, bound); - return getTransactionsInternal(this.connection, account, newOptions); + return getTransactionsInternal(this.connection, address, newOptions); }); } const newOptions = _.assign(defaults, options); - return getTransactionsInternal(this.connection, account, newOptions); + return getTransactionsInternal(this.connection, address, newOptions); } module.exports = getTransactions; diff --git a/src/ledger/trustlines.js b/src/ledger/trustlines.js index 42078077..ae99af70 100644 --- a/src/ledger/trustlines.js +++ b/src/ledger/trustlines.js @@ -38,13 +38,12 @@ function getAccountLines(connection: Connection, address: string, return connection.request(request).then(_.partial(formatResponse, options)); } -function getTrustlines(account: string, options: TrustlinesOptions = {} +function getTrustlines(address: string, options: TrustlinesOptions = {} ): Promise { - validate.address(account); - validate.getTrustlinesOptions(options); + validate.getTrustlines({address, options}); return this.getLedgerVersion().then(ledgerVersion => { - const getter = _.partial(getAccountLines, this.connection, account, + const getter = _.partial(getAccountLines, this.connection, address, options.ledgerVersion || ledgerVersion, options); return utils.getRecursive(getter, options.limit); }); diff --git a/src/transaction/order.js b/src/transaction/order.js index a266e72b..b4da99e3 100644 --- a/src/transaction/order.js +++ b/src/transaction/order.js @@ -7,9 +7,6 @@ import type {Instructions, Prepare} from './types.js'; import type {Order} from '../ledger/transaction-types.js'; function createOrderTransaction(account: string, order: Order): Object { - validate.address(account); - validate.order(order); - const takerPays = utils.common.toRippledAmount(order.direction === 'buy' ? order.quantity : order.totalPrice); const takerGets = utils.common.toRippledAmount(order.direction === 'buy' ? @@ -40,10 +37,11 @@ function createOrderTransaction(account: string, order: Order): Object { return txJSON; } -function prepareOrder(account: string, order: Order, +function prepareOrder(address: string, order: Order, instructions: Instructions = {} ): Promise { - const txJSON = createOrderTransaction(account, order); + validate.prepareOrder({address, order, instructions}); + const txJSON = createOrderTransaction(address, order); return utils.prepareTransaction(txJSON, this, instructions); } diff --git a/src/transaction/ordercancellation.js b/src/transaction/ordercancellation.js index 3eb74bb3..a59c463a 100644 --- a/src/transaction/ordercancellation.js +++ b/src/transaction/ordercancellation.js @@ -7,9 +7,6 @@ import type {Instructions, Prepare} from './types.js'; function createOrderCancellationTransaction(account: string, sequence: number ): Object { - validate.address(account); - validate.sequence(sequence); - return { TransactionType: 'OfferCancel', Account: account, @@ -17,10 +14,11 @@ function createOrderCancellationTransaction(account: string, }; } -function prepareOrderCancellation(account: string, sequence: number, +function prepareOrderCancellation(address: string, sequence: number, instructions: Instructions = {} ): Promise { - const txJSON = createOrderCancellationTransaction(account, sequence); + validate.prepareOrderCancellation({address, sequence, instructions}); + const txJSON = createOrderCancellationTransaction(address, sequence); return utils.prepareTransaction(txJSON, this, instructions); } diff --git a/src/transaction/payment.js b/src/transaction/payment.js index 54cc815b..e9ff4336 100644 --- a/src/transaction/payment.js +++ b/src/transaction/payment.js @@ -65,12 +65,14 @@ function createMaximalAmount(amount: Amount): Amount { return _.assign(amount, {value: maxValue}); } -function createPaymentTransaction(account: string, paymentArgument: Payment +function createPaymentTransaction(address: string, paymentArgument: Payment ): Object { const payment = _.cloneDeep(paymentArgument); applyAnyCounterpartyEncoding(payment); - validate.address(account); - validate.payment(payment); + + if (address !== payment.source.address) { + throw new ValidationError('address must match payment.source.address'); + } if ((payment.source.maxAmount && payment.destination.minAmount) || (payment.source.amount && payment.destination.amount)) { @@ -141,10 +143,11 @@ function createPaymentTransaction(account: string, paymentArgument: Payment return txJSON; } -function preparePayment(account: string, payment: Payment, - instructions: Instructions +function preparePayment(address: string, payment: Payment, + instructions: Instructions = {} ): Promise { - const txJSON = createPaymentTransaction(account, payment); + validate.preparePayment({address, payment, instructions}); + const txJSON = createPaymentTransaction(address, payment); return utils.prepareTransaction(txJSON, this, instructions); } diff --git a/src/transaction/settings.js b/src/transaction/settings.js index 341d118b..54d58c94 100644 --- a/src/transaction/settings.js +++ b/src/transaction/settings.js @@ -71,9 +71,6 @@ function convertTransferRate(transferRate: number | string): number | string { function createSettingsTransaction(account: string, settings: Settings ): Object { - validate.address(account); - validate.settings(settings); - if (settings.regularKey) { return { TransactionType: 'SetRegularKey', @@ -95,10 +92,11 @@ function createSettingsTransaction(account: string, settings: Settings return txJSON; } -function prepareSettings(account: string, settings: Settings, +function prepareSettings(address: string, settings: Settings, instructions: Instructions = {} ): Promise { - const txJSON = createSettingsTransaction(account, settings); + validate.prepareSettings({address, settings, instructions}); + const txJSON = createSettingsTransaction(address, settings); return utils.prepareTransaction(txJSON, this, instructions); } diff --git a/src/transaction/sign.js b/src/transaction/sign.js index 82832eaa..aeba262c 100644 --- a/src/transaction/sign.js +++ b/src/transaction/sign.js @@ -13,12 +13,11 @@ function computeSignature(txJSON, privateKey) { function sign(txJSON: string, secret: string ): {signedTransaction: string; id: string} { - const tx = JSON.parse(txJSON); - validate.txJSON(tx); + validate.sign({txJSON, secret}); // we can't validate that the secret matches the account because // the secret could correspond to the regular key - validate.secret(secret); + const tx = JSON.parse(txJSON); const keypair = keypairs.deriveKeypair(secret); if (tx.SigningPubKey === undefined) { tx.SigningPubKey = keypair.publicKey; diff --git a/src/transaction/submit.js b/src/transaction/submit.js index d0d96081..69a696b9 100644 --- a/src/transaction/submit.js +++ b/src/transaction/submit.js @@ -31,7 +31,7 @@ function formatResponse(response) { } function submit(txBlob: string): Promise { - validate.blob(txBlob); + validate.submit({txBlob}); const request = { command: 'submit', tx_blob: txBlob diff --git a/src/transaction/suspended-payment-cancellation.js b/src/transaction/suspended-payment-cancellation.js index 1ae38718..b710fc3e 100644 --- a/src/transaction/suspended-payment-cancellation.js +++ b/src/transaction/suspended-payment-cancellation.js @@ -15,9 +15,6 @@ type SuspendedPaymentCancellation = { function createSuspendedPaymentCancellationTransaction(account: string, payment: SuspendedPaymentCancellation ): Object { - validate.address(account); - validate.suspendedPaymentCancellation(payment); - const txJSON: Object = { TransactionType: 'SuspendedPaymentCancel', Account: account, @@ -30,11 +27,14 @@ function createSuspendedPaymentCancellationTransaction(account: string, return txJSON; } -function prepareSuspendedPaymentCancellation(account: string, - payment: SuspendedPaymentCancellation, instructions: Instructions = {} +function prepareSuspendedPaymentCancellation(address: string, + suspendedPaymentCancellation: SuspendedPaymentCancellation, + instructions: Instructions = {} ): Promise { - const txJSON = - createSuspendedPaymentCancellationTransaction(account, payment); + validate.prepareSuspendedPaymentCancellation( + {address, suspendedPaymentCancellation, instructions}); + const txJSON = createSuspendedPaymentCancellationTransaction( + address, suspendedPaymentCancellation); return utils.prepareTransaction(txJSON, this, instructions); } diff --git a/src/transaction/suspended-payment-creation.js b/src/transaction/suspended-payment-creation.js index eca99f32..81f97311 100644 --- a/src/transaction/suspended-payment-creation.js +++ b/src/transaction/suspended-payment-creation.js @@ -18,9 +18,6 @@ type SuspendedPaymentCreation = { function createSuspendedPaymentCreationTransaction(account: string, payment: SuspendedPaymentCreation ): Object { - validate.address(account); - validate.suspendedPaymentCreation(payment); - const txJSON: Object = { TransactionType: 'SuspendedPaymentCreate', Account: account, @@ -49,10 +46,14 @@ function createSuspendedPaymentCreationTransaction(account: string, return txJSON; } -function prepareSuspendedPaymentCreation(account: string, - payment: SuspendedPaymentCreation, instructions: Instructions = {} +function prepareSuspendedPaymentCreation(address: string, + suspendedPaymentCreation: SuspendedPaymentCreation, + instructions: Instructions = {} ): Promise { - const txJSON = createSuspendedPaymentCreationTransaction(account, payment); + validate.prepareSuspendedPaymentCreation( + {address, suspendedPaymentCreation, instructions}); + const txJSON = createSuspendedPaymentCreationTransaction( + address, suspendedPaymentCreation); return utils.prepareTransaction(txJSON, this, instructions); } diff --git a/src/transaction/suspended-payment-execution.js b/src/transaction/suspended-payment-execution.js index 4070ad37..8aa444a6 100644 --- a/src/transaction/suspended-payment-execution.js +++ b/src/transaction/suspended-payment-execution.js @@ -18,9 +18,6 @@ type SuspendedPaymentExecution = { function createSuspendedPaymentExecutionTransaction(account: string, payment: SuspendedPaymentExecution ): Object { - validate.address(account); - validate.suspendedPaymentExecution(payment); - const txJSON: Object = { TransactionType: 'SuspendedPaymentFinish', Account: account, @@ -43,10 +40,14 @@ function createSuspendedPaymentExecutionTransaction(account: string, return txJSON; } -function prepareSuspendedPaymentExecution(account: string, - payment: SuspendedPaymentExecution, instructions: Instructions = {} +function prepareSuspendedPaymentExecution(address: string, + suspendedPaymentExecution: SuspendedPaymentExecution, + instructions: Instructions = {} ): Promise { - const txJSON = createSuspendedPaymentExecutionTransaction(account, payment); + validate.prepareSuspendedPaymentExecution( + {address, suspendedPaymentExecution, instructions}); + const txJSON = createSuspendedPaymentExecutionTransaction( + address, suspendedPaymentExecution); return utils.prepareTransaction(txJSON, this, instructions); } diff --git a/src/transaction/trustline.js b/src/transaction/trustline.js index e6058e4c..b9ba7ad0 100644 --- a/src/transaction/trustline.js +++ b/src/transaction/trustline.js @@ -15,9 +15,6 @@ function convertQuality(quality) { function createTrustlineTransaction(account: string, trustline: TrustLineSpecification ): Object { - validate.address(account); - validate.trustline(trustline); - const limit = { currency: trustline.currency, issuer: trustline.counterparty, @@ -50,10 +47,11 @@ function createTrustlineTransaction(account: string, return txJSON; } -function prepareTrustline(account: string, +function prepareTrustline(address: string, trustline: TrustLineSpecification, instructions: Instructions = {} ): Promise { - const txJSON = createTrustlineTransaction(account, trustline); + validate.prepareTrustline({address, trustline, instructions}); + const txJSON = createTrustlineTransaction(address, trustline); return utils.prepareTransaction(txJSON, this, instructions); } diff --git a/test/api-test.js b/test/api-test.js index 5f99d0e4..11a18bf7 100644 --- a/test/api-test.js +++ b/test/api-test.js @@ -834,26 +834,32 @@ describe('RippleAPI', function() { minLedgerVersion: 20000, maxLedgerVersion: 10000 }; - assert.throws(_.partial(validate.getTransactionsOptions, options), - this.api.errors.ValidationError); - assert.throws(_.partial(validate.getTransactionsOptions, options), + const thunk = _.partial(validate.getTransactions, + {address, options}); + assert.throws(thunk, this.api.errors.ValidationError); + assert.throws(thunk, /minLedgerVersion must not be greater than maxLedgerVersion/); }); it('secret', function() { - assert.doesNotThrow(_.partial(validate.secret, + function validateSecret(secret) { + validate.sign({txJSON: '', secret}); + } + assert.doesNotThrow(_.partial(validateSecret, 'shzjfakiK79YQdMjy4h8cGGfQSV6u')); - assert.throws(_.partial(validate.secret, 1), - /Invalid parameter/); - assert.throws(_.partial(validate.secret, ''), + assert.throws(_.partial(validateSecret, + 'shzjfakiK79YQdMjy4h8cGGfQSV6v'), this.api.errors.ValidationError); + assert.throws(_.partial(validateSecret, 1), this.api.errors.ValidationError); - assert.throws(_.partial(validate.secret, 's!!!'), + assert.throws(_.partial(validateSecret, ''), this.api.errors.ValidationError); - assert.throws(_.partial(validate.secret, 'passphrase'), + assert.throws(_.partial(validateSecret, 's!!!'), + this.api.errors.ValidationError); + assert.throws(_.partial(validateSecret, 'passphrase'), this.api.errors.ValidationError); // 32 0s is a valid hex repr of seed bytes const hex = new Array(33).join('0'); - assert.throws(_.partial(validate.secret, hex), + assert.throws(_.partial(validateSecret, hex), this.api.errors.ValidationError); }); diff --git a/test/fixtures/responses/get-server-info.json b/test/fixtures/responses/get-server-info.json index 71daea0c..6a765d30 100644 --- a/test/fixtures/responses/get-server-info.json +++ b/test/fixtures/responses/get-server-info.json @@ -2,6 +2,7 @@ "buildVersion": "0.24.0-rc1", "completeLedgers": "32570-6595042", "hostid": "ARTS", + "ioLatencyMs": 1, "lastClose": { "convergeTimeS": 2.007, "proposers": 4 diff --git a/test/fixtures/rippled/server-info.json b/test/fixtures/rippled/server-info.json index 913c43be..55ce67d0 100644 --- a/test/fixtures/rippled/server-info.json +++ b/test/fixtures/rippled/server-info.json @@ -7,6 +7,7 @@ "build_version": "0.24.0-rc1", "complete_ledgers": "32570-6595042", "hostid": "ARTS", + "io_latency_ms": 1, "last_close": { "converge_time_s": 2.007, "proposers": 4 diff --git a/test/integration/integration-test.js b/test/integration/integration-test.js index bb7b36c3..43730185 100644 --- a/test/integration/integration-test.js +++ b/test/integration/integration-test.js @@ -4,10 +4,11 @@ const _ = require('lodash'); const assert = require('assert'); const errors = require('../../src/common/errors'); -const validate = require('../../src/common').validate; const wallet = require('./wallet'); const requests = require('../fixtures/requests'); const RippleAPI = require('../../src').RippleAPI; +const {isValidAddress} = require('ripple-address-codec'); +const {isValidSecret} = require('../../src/common'); const TIMEOUT = 30000; // how long before each test case times out @@ -292,8 +293,8 @@ describe('integration tests', function() { it('generateWallet', function() { const newWallet = this.api.generateAddress(); assert(newWallet && newWallet.address && newWallet.secret); - validate.address(newWallet.address); - validate.secret(newWallet.secret); + assert(isValidAddress(newWallet.address)); + assert(isValidSecret(newWallet.secret)); }); });