Add schema validation and assertion, create ECSignature schema
This commit is contained in:
parent
d23dbf53fd
commit
0b8ddc1ee1
@ -33,6 +33,7 @@
|
||||
"devDependencies": {
|
||||
"@types/bignumber.js": "^4.0.2",
|
||||
"@types/chai": "^3.5.2",
|
||||
"@types/jsonschema": "^1.1.1",
|
||||
"@types/mocha": "^2.2.41",
|
||||
"@types/node": "^7.0.22",
|
||||
"awesome-typescript-loader": "^3.1.3",
|
||||
@ -53,6 +54,7 @@
|
||||
"dependencies": {
|
||||
"bignumber.js": "^4.0.2",
|
||||
"ethereumjs-util": "^5.1.1",
|
||||
"jsonschema": "^1.1.1",
|
||||
"web3": "^0.19.0"
|
||||
}
|
||||
}
|
||||
|
@ -1,6 +1,7 @@
|
||||
import * as BigNumber from 'bignumber.js';
|
||||
import * as ethUtil from 'ethereumjs-util';
|
||||
import {assert} from './utils/assert';
|
||||
import {ECSignatureSchema} from './schemas/ec_signature_schema';
|
||||
|
||||
/**
|
||||
* Elliptic Curve signature
|
||||
@ -18,7 +19,7 @@ export class ZeroEx {
|
||||
*/
|
||||
public static isValidSignature(data: string, signature: ECSignature, signer: ETHAddressHex): boolean {
|
||||
assert.isString('data', data);
|
||||
assert.isObject('signature', signature);
|
||||
assert.doesConformToSchema('signature', signature, ECSignatureSchema);
|
||||
assert.isETHAddressHex('signer', signer);
|
||||
|
||||
const dataBuff = ethUtil.toBuffer(data);
|
||||
|
4
src/ts/globals.d.ts
vendored
4
src/ts/globals.d.ts
vendored
@ -2,6 +2,10 @@ declare type ETHPublicKey = string;
|
||||
declare type ETHAddressHex = string;
|
||||
declare type ETHAddressBuff = Buffer;
|
||||
|
||||
declare interface Schema {
|
||||
id: string;
|
||||
}
|
||||
|
||||
declare module 'ethereumjs-util' {
|
||||
const toBuffer: (data: string) => Buffer;
|
||||
const hashPersonalMessage: (msg: Buffer) => Buffer;
|
||||
|
10
src/ts/schemas/ec_signature_schema.ts
Normal file
10
src/ts/schemas/ec_signature_schema.ts
Normal file
@ -0,0 +1,10 @@
|
||||
export const ECSignatureSchema = {
|
||||
id: '/ECSignature',
|
||||
properties: {
|
||||
v: {type: 'number'},
|
||||
r: {type: 'string'},
|
||||
s: {type: 'string'},
|
||||
},
|
||||
required: ['v', 'r', 's'],
|
||||
type: 'object',
|
||||
};
|
@ -1,6 +1,7 @@
|
||||
import * as _ from 'lodash';
|
||||
import * as BigNumber from 'bignumber.js';
|
||||
import Web3 = require('web3');
|
||||
import {SchemaValidator} from './schema_validator';
|
||||
|
||||
export const assert = {
|
||||
isBigNumber(variableName: string, value: BigNumber.BigNumber) {
|
||||
@ -14,12 +15,16 @@ export const assert = {
|
||||
const web3 = new Web3();
|
||||
this.assert(web3.isAddress(value), this.typeAssertionMessage(variableName, 'ETHAddressHex', value));
|
||||
},
|
||||
isObject(variableName: string, value: object) {
|
||||
this.assert(_.isObject(value), this.typeAssertionMessage(variableName, 'object', value));
|
||||
},
|
||||
isNumber(variableName: string, value: number) {
|
||||
this.assert(_.isFinite(value), this.typeAssertionMessage(variableName, 'number', value));
|
||||
},
|
||||
doesConformToSchema(variableName: string, value: object, schema: Schema) {
|
||||
const schemaValidator = new SchemaValidator();
|
||||
const validationResult = schemaValidator.validate(value, schema);
|
||||
const hasValidationErrors = validationResult.errors.length > 0;
|
||||
const assertMsg = `Expected ${variableName} to conform to schema ${schema.id}, encountered: $value`;
|
||||
this.assert(!hasValidationErrors, assertMsg);
|
||||
},
|
||||
assert(condition: boolean, message: string) {
|
||||
if (!condition) {
|
||||
throw new Error(message);
|
||||
|
13
src/ts/utils/schema_validator.ts
Normal file
13
src/ts/utils/schema_validator.ts
Normal file
@ -0,0 +1,13 @@
|
||||
import {Validator as V, ValidatorResult} from 'jsonschema';
|
||||
import {ECSignatureSchema} from '../schemas/ec_signature_schema';
|
||||
|
||||
export class SchemaValidator {
|
||||
private v: V;
|
||||
constructor() {
|
||||
this.v = new V();
|
||||
this.v.addSchema(ECSignatureSchema, ECSignatureSchema.id);
|
||||
}
|
||||
public validate(instance: object, schema: Schema): ValidatorResult {
|
||||
return this.v.validate(instance, schema);
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user