From 809d981987a2890fac3a73a40a05c598b9040334 Mon Sep 17 00:00:00 2001 From: Chris Clark Date: Mon, 30 Nov 2015 14:54:15 -0800 Subject: [PATCH] Fix generateAddress docs and add error event listener to boilerplate --- docs/index.md | 18 +++++++++--- docs/src/boilerplate.md.ejs | 7 +++++ docs/src/events.md.ejs | 2 +- docs/src/generateAddress.md.ejs | 5 ++-- src/api.js | 3 +- src/common/schema-validator.js | 5 ++-- .../schemas/input/generate-address.json | 29 +++++++++++++++++++ src/common/utils.js | 19 ------------ src/common/validate.js | 1 + src/offline/generate-address.js | 24 +++++++++++++++ 10 files changed, 83 insertions(+), 30 deletions(-) create mode 100644 src/common/schemas/input/generate-address.json create mode 100644 src/offline/generate-address.js diff --git a/docs/index.md b/docs/index.md index 1fa7a01c..8e625d3b 100644 --- a/docs/index.md +++ b/docs/index.md @@ -86,6 +86,9 @@ const {RippleAPI} = require('ripple-lib'); const api = new RippleAPI({ server: 'wss://s1.ripple.com' // Public rippled server hosted by Ripple, Inc. }); +api.on('error', (errorCode, errorMessage) => { + console.log(errorCode + ': ' + errorMessage); +}); api.connect().then(() => { /* insert code here */ }).then(() => { @@ -105,6 +108,10 @@ All the code snippets in this documentation assume that you have surrounded them If you omit the "catch" section, errors may not be visible. + + ### Parameters The RippleAPI constructor optionally takes one argument, an object with the following options: @@ -3355,7 +3362,11 @@ Generate a new Ripple address and corresponding secret. ### Parameters -This method has no parameters. +Name | Type | Description +---- | ---- | ----------- +options | object | *Optional* Options to control how the address and secret are generated. +*options.* algorithm | string | *Optional* The digital signature algorithm to generate an address for. Can be `ecdsa-secp256k1` (default) or `ed25519`. +*options.* entropy | array\ | *Optional* The entropy to use to generate the seed. ### Return Value @@ -3369,8 +3380,7 @@ secret | secret string | The secret corresponding to the `address`. ### Example ```javascript -return api.generateAddress() - .then(result => {/* ... */}); +return api.generateAddress(); ``` @@ -3482,7 +3492,7 @@ api.on('ledger', ledger => { ## error -This event is emitted when there is an error on the connection to the server. +This event is emitted when there is an error on the connection to the server that cannot be associated to a specific request. ### Return Value diff --git a/docs/src/boilerplate.md.ejs b/docs/src/boilerplate.md.ejs index 974aa9a3..12cead0e 100644 --- a/docs/src/boilerplate.md.ejs +++ b/docs/src/boilerplate.md.ejs @@ -8,6 +8,9 @@ const {RippleAPI} = require('ripple-lib'); const api = new RippleAPI({ server: 'wss://s1.ripple.com' // Public rippled server hosted by Ripple, Inc. }); +api.on('error', (errorCode, errorMessage) => { + console.log(errorCode + ': ' + errorMessage); +}); api.connect().then(() => { /* insert code here */ }).then(() => { @@ -27,6 +30,10 @@ All the code snippets in this documentation assume that you have surrounded them If you omit the "catch" section, errors may not be visible. + + ### Parameters The RippleAPI constructor optionally takes one argument, an object with the following options: diff --git a/docs/src/events.md.ejs b/docs/src/events.md.ejs index 0f18811c..6353f4b9 100644 --- a/docs/src/events.md.ejs +++ b/docs/src/events.md.ejs @@ -20,7 +20,7 @@ api.on('ledger', ledger => { ## error -This event is emitted when there is an error on the connection to the server. +This event is emitted when there is an error on the connection to the server that cannot be associated to a specific request. ### Return Value diff --git a/docs/src/generateAddress.md.ejs b/docs/src/generateAddress.md.ejs index 2232e204..b39808d6 100644 --- a/docs/src/generateAddress.md.ejs +++ b/docs/src/generateAddress.md.ejs @@ -6,7 +6,7 @@ Generate a new Ripple address and corresponding secret. ### Parameters -This method has no parameters. +<%- renderSchema('input/generate-address.json') %> ### Return Value @@ -17,8 +17,7 @@ This method returns an object with the following structure: ### Example ```javascript -return api.generateAddress() - .then(result => {/* ... */}); +return api.generateAddress(); ``` <%- renderFixture('responses/generate-address.json') %> diff --git a/src/api.js b/src/api.js index 24613bff..ce6c6365 100644 --- a/src/api.js +++ b/src/api.js @@ -46,7 +46,8 @@ const prepareSettings = require('./transaction/settings'); const sign = require('./transaction/sign'); const submit = require('./transaction/submit'); const errors = require('./common').errors; -const generateAddress = common.generateAddressAPI; +const generateAddress = + require('./offline/generate-address').generateAddressAPI; const computeLedgerHash = require('./offline/ledgerhash'); const getLedger = require('./ledger/ledger'); diff --git a/src/common/schema-validator.js b/src/common/schema-validator.js index 5885c771..abf45948 100644 --- a/src/common/schema-validator.js +++ b/src/common/schema-validator.js @@ -92,8 +92,9 @@ function loadSchemas() { 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') + require('./schemas/input/sign.json'), + require('./schemas/input/submit.json'), + require('./schemas/input/generate-address.json') ]; const titles = _.map(schemas, schema => schema.title); const duplicates = _.keys(_.pick(_.countBy(titles), count => count > 1)); diff --git a/src/common/schemas/input/generate-address.json b/src/common/schemas/input/generate-address.json new file mode 100644 index 00000000..64902356 --- /dev/null +++ b/src/common/schemas/input/generate-address.json @@ -0,0 +1,29 @@ +{ + "$schema": "http://json-schema.org/draft-04/schema#", + "title": "generateAddressParameters", + "type": "object", + "properties": { + "options": { + "type": "object", + "description": "Options to control how the address and secret are generated.", + "properties": { + "entropy": { + "type": "array", + "items": { + "type": "integer", + "minimum": 0, + "maximum": 255 + }, + "description": "The entropy to use to generate the seed." + }, + "algorithm": { + "type": "string", + "enum": ["ecdsa-secp256k1", "ed25519"], + "description": "The digital signature algorithm to generate an address for. Can be `ecdsa-secp256k1` (default) or `ed25519`." + } + }, + "additionalProperties": false + } + }, + "additionalProperties": false +} diff --git a/src/common/utils.js b/src/common/utils.js index 3b964419..c73fa56c 100644 --- a/src/common/utils.js +++ b/src/common/utils.js @@ -2,8 +2,6 @@ 'use strict'; 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'; @@ -37,21 +35,6 @@ function toRippledAmount(amount: Amount): RippledAmount { }; } -function generateAddress(options?: Object): Object { - const secret = keypairs.generateSeed(options); - const keypair = keypairs.deriveKeypair(secret); - const address = keypairs.deriveAddress(keypair.publicKey); - return {secret, address}; -} - -function generateAddressAPI(options?: Object): Object { - try { - return generateAddress(options); - } catch (error) { - throw new errors.UnexpectedError(error.message); - } -} - const FINDSNAKE = /([a-zA-Z]_[a-zA-Z])/g; function convertKeysFromSnakeCaseToCamelCase(obj: any): any { if (typeof obj === 'object') { @@ -101,8 +84,6 @@ module.exports = { dropsToXrp, xrpToDrops, toRippledAmount, - generateAddress, - generateAddressAPI, convertKeysFromSnakeCaseToCamelCase, removeUndefined, rippleTimeToISO8601, diff --git a/src/common/validate.js b/src/common/validate.js index fceed930..bff6620b 100644 --- a/src/common/validate.js +++ b/src/common/validate.js @@ -49,6 +49,7 @@ module.exports = { sign: _.partial(schemaValidate, 'signParameters'), submit: _.partial(schemaValidate, 'submitParameters'), computeLedgerHash: _.partial(schemaValidate, 'computeLedgerHashParameters'), + generateAddress: _.partial(schemaValidate, 'generateAddressParameters'), apiOptions: _.partial(schemaValidate, 'api-options'), instructions: _.partial(schemaValidate, 'instructions') }; diff --git a/src/offline/generate-address.js b/src/offline/generate-address.js new file mode 100644 index 00000000..f8c7e0de --- /dev/null +++ b/src/offline/generate-address.js @@ -0,0 +1,24 @@ +'use strict'; +const keypairs = require('ripple-keypairs'); +const common = require('../common'); +const {errors, validate} = common; + +function generateAddress(options?: Object): Object { + const secret = keypairs.generateSeed(options); + const keypair = keypairs.deriveKeypair(secret); + const address = keypairs.deriveAddress(keypair.publicKey); + return {secret, address}; +} + +function generateAddressAPI(options?: Object): Object { + validate.generateAddress({options}); + try { + return generateAddress(options); + } catch (error) { + throw new errors.UnexpectedError(error.message); + } +} + +module.exports = { + generateAddressAPI +};