Skip to content
This repository has been archived by the owner on Aug 2, 2022. It is now read-only.

Adding regex to validate name during serialization (release/21.0.x branch) #748

Merged
merged 3 commits into from
Jun 9, 2020
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
4 changes: 4 additions & 0 deletions src/eosjs-serialize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,10 @@ export class SerialBuffer { // tslint:disable-line max-classes-per-file
if (typeof s !== 'string') {
throw new Error('Expected string containing name');
}
const regex = new RegExp(/^[.1-5a-z]{1,12}[.1-5a-j]?$/);
if (!regex.test(s)) {
throw new Error('Name should be less than 13 characters, or less than 14 if last character is between 1-5 or a-j, and only contain the following symbols .12345abcdefghijklmnopqrstuvwxyz'); // tslint:disable-line
}
function charToSymbol(c: number) {
if (c >= 'a'.charCodeAt(0) && c <= 'z'.charCodeAt(0)) {
return (c - 'a'.charCodeAt(0)) + 6;
Expand Down
49 changes: 49 additions & 0 deletions src/tests/eosjs-serialize.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,55 @@ describe('Serialize', () => {
});
});

describe('name', () => {
let serialBuffer: SerialBuffer;
const invalidNameErrorMessage = 'Name should be less than 13 characters, or less than 14 if last character is between 1-5 or a-j, and only contain the following symbols .12345abcdefghijklmnopqrstuvwxyz'; //tslint:disable-line

beforeEach(() => {
serialBuffer = new SerialBuffer({
textEncoder: new TextEncoder(),
textDecoder: new TextDecoder()
});
});

it('should be able to push name with a valid account name', () => {
const name = '.12345abcdefg';

serialBuffer.pushName(name);

expect(serialBuffer.getName()).toEqual(name);
});

it('should remove the `.` character from the end of the account name', () => {
const name = 'abcd......';
const expectedName = 'abcd';

serialBuffer.pushName(name);
expect(serialBuffer.getName()).toEqual(expectedName);
});

it('should not be able to push name with an account name too short', () => {
const name = '';

const shouldFail = () => serialBuffer.pushName(name);
expect(shouldFail).toThrowError(invalidNameErrorMessage);
});

it('should not be able to push name with an account name too long', () => {
const name = 'abcdabcdabcdab';

const shouldFail = () => serialBuffer.pushName(name);
expect(shouldFail).toThrowError(invalidNameErrorMessage);
});

it('should not be able to push name with an account name with invalid characters', () => {
const name = '6789$/,';

const shouldFail = () => serialBuffer.pushName(name);
expect(shouldFail).toThrowError(invalidNameErrorMessage);
});
});

describe('bool', () => {
let boolType: Type;
let mockedBuffer: SerialBuffer;
Expand Down
Loading