Skip to content

Commit

Permalink
sdk/node: prevent v1.x params for transacting
Browse files Browse the repository at this point in the history
Closes #3335

Author: Dominic Dagradi <[email protected]>
Date: Mon Apr 16 10:44:18 2018 -0700
upstream:63da10b71a6fcb4ba7cd33a0f22fa174fb17212c
  • Loading branch information
dominic authored and iampogo committed Apr 16, 2018
1 parent 6e970c9 commit 4b29efd
Show file tree
Hide file tree
Showing 4 changed files with 94 additions and 11 deletions.
11 changes: 5 additions & 6 deletions src/api/transactions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -61,6 +62,7 @@ export class TransactionBuilder {
tokenTags?: object
actionTags?: object
}) {
validate(params, 'IssueActionSchema')
this.actions.push(Object.assign({}, params, { type: 'issue' }))
}

Expand All @@ -84,6 +86,7 @@ export class TransactionBuilder {
filterParams?: object
actionTags?: object
}) {
validate(params, 'RetireActionSchema')
this.actions.push(Object.assign({}, params, { type: 'retire' }))
}

Expand All @@ -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.
Expand All @@ -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}.
*
Expand Down
6 changes: 1 addition & 5 deletions src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
46 changes: 46 additions & 0 deletions src/validate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
)
42 changes: 42 additions & 0 deletions test/validateSchema.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'")
})
})
})

})

0 comments on commit 4b29efd

Please sign in to comment.