Skip to content
This repository has been archived by the owner on Jun 11, 2024. It is now read-only.

Add validation of custom transaction - Closes #3877 #3880

Merged
merged 13 commits into from
Jul 9, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions elements/lisk-transactions/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ import {
signRawTransaction,
signTransaction,
stringEndsWith,
transactionInterface,
validateAddress,
validateFee,
validateKeysgroup,
Expand Down Expand Up @@ -115,6 +116,7 @@ export {
TransactionResponse,
TransactionJSON,
TransactionError,
transactionInterface,
convertToAssetError,
constants,
exposedUtils as utils,
Expand Down
1 change: 1 addition & 0 deletions elements/lisk-transactions/src/utils/validation/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,4 @@ export {
} from './validation';
export { validateTransaction } from './validate_transaction';
export { validator } from './validator';
export { transactionInterface } from './schema';
58 changes: 58 additions & 0 deletions elements/lisk-transactions/src/utils/validation/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,64 @@ export const transaction = {
},
};

export const transactionInterface = {
required: [
'toJSON',
'isReady',
'getBytes',
'validate',
'verifyAgainstOtherTransactions',
'apply',
'undo',
'prepare',
'addMultisignature',
'addVerifiedMultisignature',
'isExpired',
'fromSync',
],
properties: {
toJSON: {
typeof: 'function',
shuse2 marked this conversation as resolved.
Show resolved Hide resolved
},
isReady: {
typeof: 'function',
},
getBytes: {
typeof: 'function',
},
validate: {
typeof: 'function',
},
verifyAgainstOtherTransactions: {
typeof: 'function',
},
apply: {
typeof: 'function',
},
undo: {
typeof: 'function',
},
prepare: {
typeof: 'function',
},
addMultisignature: {
typeof: 'function',
},
addVerifiedMultisignature: {
typeof: 'function',
},
processMultisignatures: {
typeof: 'function',
},
isExpired: {
typeof: 'function',
},
fromSync: {
typeof: 'function',
},
},
};

// TODO: Add senderId and recipientId to required once deprecated functions relying on this schema are removed
export const baseTransaction = {
$id: 'lisk/base-transaction',
Expand Down
15 changes: 11 additions & 4 deletions framework/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions framework/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
"@liskhq/lisk-transaction-pool": "0.1.0",
"@liskhq/lisk-transactions": "2.1.0",
"ajv": "6.7.0",
"ajv-keywords": "3.4.0",
"async": "2.6.1",
"bignumber.js": "8.0.2",
"bluebird": "3.5.3",
Expand Down
7 changes: 6 additions & 1 deletion framework/src/controller/application.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ const {
DelegateTransaction,
VoteTransaction,
MultisignatureTransaction,
transactionInterface,
} = require('@liskhq/lisk-transactions');
const randomstring = require('randomstring');
const _ = require('lodash');
Expand Down Expand Up @@ -233,10 +234,14 @@ class Application {
);

assert(
!Object.keys(this.getTransactions()).includes(Transaction.TYPE),
!Object.keys(this.getTransactions()).includes(
Transaction.TYPE.toString()
),
`A transaction type "${Transaction.TYPE}" is already registered.`
);

validator.validate(transactionInterface, Transaction.prototype);

if (matcher) {
Object.defineProperty(Transaction.prototype, 'matcher', {
get: () => matcher,
Expand Down
5 changes: 5 additions & 0 deletions framework/src/controller/validator/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ const _ = require('lodash');
* Custom Lisk Framework Validator implemented on top of Ajv (https://github.com/epoberezkin/ajv)
*/
const Ajv = require('ajv');
const ajvKeywords = require('ajv-keywords');
const { SchemaValidationError } = require('../../errors');
const formats = require('./formats');
const ZSchema = require('./z_schema');
Expand All @@ -33,13 +34,17 @@ const validator = new Ajv({
$data: true,
});

ajvKeywords(validator);

const parserAndValidator = new Ajv({
allErrors: true,
schemaId: 'auto',
useDefaults: false,
$data: true,
});

ajvKeywords(parserAndValidator);

parserAndValidator.addKeyword('env', envKeyword);
parserAndValidator.addKeyword('arg', argKeyword);

Expand Down
1 change: 0 additions & 1 deletion framework/src/errors.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ class SchemaValidationError extends FrameworkError {
*/
constructor(errors) {
super('Schema validation error');
this.message = JSON.stringify(errors, null, 2);
this.errors = errors;
this.message = JSON.stringify(errors, null, 2);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,6 @@ Object {
"broadcasts": Object {
"active": true,
"broadcastInterval": 5000,
"broadcastLimit": 25,
"parallelLimit": 20,
"relayLimit": 3,
"releaseLimit": 25,
},
Expand All @@ -75,12 +73,15 @@ Object {
"inertTransactions": Array [],
"multisignatures": Array [],
"precedent": Object {
"disableDappTransaction": 0,
"disableDappTransfer": 0,
},
"recipientExceedingUint64": Object {},
"recipientLeadingZero": Object {},
"roundVotes": Array [],
"rounds": Object {},
"senderPublicKey": Array [],
"signSignature": Array [],
"signatures": Array [],
"transactionWithNullByte": Array [],
"votes": Array [],
Expand Down Expand Up @@ -497,28 +498,14 @@ Object {
},
"loading": Object {
"loadPerIteration": 5000,
"rebuildUpToRound": 0,
"rebuildUpToRound": null,
},
"network": Object {
"access": Object {
"blackList": Array [],
},
"address": "0.0.0.0",
"enabled": true,
"list": Array [
Object {
"ip": "127.0.0.1",
"wsPort": 5000,
},
],
"options": Object {
"broadhashConsensusCalculationInterval": 5000,
"httpHeadersTimeout": 5000,
"httpServerSetTimeout": 20000,
"timeout": 5000,
"wsEngine": "ws",
},
"wsPort": 5000,
"registeredTransactions": Object {
"0": [Function],
"1": [Function],
"2": [Function],
"3": [Function],
"4": [Function],
},
"syncing": Object {
"active": true,
Expand Down Expand Up @@ -574,6 +561,22 @@ Object {
},
"trustProxy": false,
},
"network": Object {
"ackTimeout": 20000,
"address": "0.0.0.0",
"blacklistedPeers": Array [],
"connectTimeout": 5000,
"discoveryInterval": 30000,
"emitPeerLimit": 25,
"seedPeers": Array [
Object {
"ip": "127.0.0.1",
"wsPort": 5000,
},
],
"wsEngine": "ws",
"wsPort": 5000,
},
},
}
`;
Loading