From 4b29efdb981ab83147f7e9ecebe392e4049f531b Mon Sep 17 00:00:00 2001 From: Dominic Dagradi Date: Mon, 16 Apr 2018 17:44:27 +0000 Subject: [PATCH] sdk/node: prevent v1.x params for transacting Closes #3335 Author: Dominic Dagradi Date: Mon Apr 16 10:44:18 2018 -0700 upstream:63da10b71a6fcb4ba7cd33a0f22fa174fb17212c --- src/api/transactions.ts | 11 +++++----- src/client.ts | 6 +----- src/validate.ts | 46 +++++++++++++++++++++++++++++++++++++++++ test/validateSchema.js | 42 +++++++++++++++++++++++++++++++++++++ 4 files changed, 94 insertions(+), 11 deletions(-) diff --git a/src/api/transactions.ts b/src/api/transactions.ts index d54f3e3..dcccd52 100644 --- a/src/api/transactions.ts +++ b/src/api/transactions.ts @@ -2,6 +2,7 @@ import { Client } from '../client' import { Page } from '../page' import { Query } from '../query' import { QueryParams } from '../types' +import { validate } from '../validate' /** * A blockchain consists of an immutable set of cryptographically linked @@ -61,6 +62,7 @@ export class TransactionBuilder { tokenTags?: object actionTags?: object }) { + validate(params, 'IssueActionSchema') this.actions.push(Object.assign({}, params, { type: 'issue' })) } @@ -84,6 +86,7 @@ export class TransactionBuilder { filterParams?: object actionTags?: object }) { + validate(params, 'RetireActionSchema') this.actions.push(Object.assign({}, params, { type: 'retire' })) } @@ -94,6 +97,7 @@ export class TransactionBuilder { * @param {Object} params Action parameters * @param {String} params.sourceAccountId - Account ID specifying the account * controlling the flavor. + * @param {String} params.flavorId - ID of flavor to be transferred. * @param {Integer} params.amount - Amount of the flavor to be transferred. * @param {String} params.destinationAccountId - Account ID specifying the * account controlling the flavor. @@ -112,16 +116,11 @@ export class TransactionBuilder { tokenTags?: object actionTags?: object }) { + validate(params, 'TransferActionSchema') this.actions.push(Object.assign({}, params, { type: 'transfer' })) } } -export interface TransactionQueryParameters extends QueryParams { - startTime?: number - endTime?: number - timeout?: number -} - /** * API for interacting with {@link Transaction transactions}. * diff --git a/src/client.ts b/src/client.ts index 1963f2c..8299633 100644 --- a/src/client.ts +++ b/src/client.ts @@ -6,11 +6,7 @@ import { flavorsAPI } from './api/flavors' import { keysAPI } from './api/keys' import { statsAPI } from './api/stats' import { tokensAPI, TokenSumParams } from './api/tokens' -import { - TransactionBuilder, - TransactionQueryParameters, - transactionsAPI, -} from './api/transactions' +import { TransactionBuilder, transactionsAPI } from './api/transactions' import { Connection } from './connection' import { Page } from './page' import { Query } from './query' diff --git a/src/validate.ts b/src/validate.ts index 1102b04..3040ac5 100644 --- a/src/validate.ts +++ b/src/validate.ts @@ -67,3 +67,49 @@ ajv.addSchema( }, 'QueryParamsSchema' ) + +ajv.addSchema( + { + properties: { + actionTags: { type: 'object' }, + amount: { type: 'number' }, + destinationAccountId: { type: 'string' }, + filter: { type: 'string' }, + filterParams: { type: 'array' }, + flavorId: { type: 'string' }, + sourceAccountId: { type: 'string' }, + tokenTags: { type: 'object' }, + }, + additionalProperties: false, + }, + 'TransferActionSchema' +) + +ajv.addSchema( + { + properties: { + actionTags: { type: 'object' }, + amount: { type: 'number' }, + destinationAccountId: { type: 'string' }, + flavorId: { type: 'string' }, + tokenTags: { type: 'object' }, + }, + additionalProperties: false, + }, + 'IssueActionSchema' +) + +ajv.addSchema( + { + properties: { + actionTags: { type: 'object' }, + amount: { type: 'number' }, + filter: { type: 'string' }, + filterParams: { type: 'array' }, + flavorId: { type: 'string' }, + sourceAccountId: { type: 'string' }, + }, + additionalProperties: false, + }, + 'RetireActionSchema' +) diff --git a/test/validateSchema.js b/test/validateSchema.js index 13a2302..635c974 100644 --- a/test/validateSchema.js +++ b/test/validateSchema.js @@ -105,4 +105,46 @@ describe('Schemas', () => { assert(false, 'should not accept `startTime` field') }) }) + + describe('Transaction', () => { + it('rejects issue with destinationAccountAlias', () => { + return client.transactions.transact(builder => { + builder.issue({ + flavorId: 'foo', + amount: 1, + destinationAccountAlias: 'foo', + }) + }).then(() => assert(false, 'should not accept `destinationAccountAlias` field')) + .catch(err => { + expect(err.message).to.contain("should NOT have additional properties '.destinationAccountAlias'") + }) + }) + + it('rejects transfer with destinationAccountAlias', () => { + return client.transactions.transact(builder => { + builder.transfer({ + flavorId: 'foo', + amount: 1, + destinationAccountAlias: 'foo', + }) + }).then(() => assert(false, 'should not accept `destinationAccountAlias` field')) + .catch(err => { + expect(err.message).to.contain("should NOT have additional properties '.destinationAccountAlias'") + }) + }) + + it('rejects retire with sourceAccountAlias', () => { + return client.transactions.transact(builder => { + builder.retire({ + flavorId: 'foo', + amount: 1, + sourceAccountAlias: 'foo', + }) + }).then(() => assert(false, 'should not accept `sourceAccountAlias` field')) + .catch(err => { + expect(err.message).to.contain("should NOT have additional properties '.sourceAccountAlias'") + }) + }) + }) + })