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

Allow struct serialization from array input #806

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
Open
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: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,12 @@
"@blockone/eslint-config-blockone": "^3.0.0",
"@types/elliptic": "^6.4.12",
"@types/jest": "^26.0.14",
"@types/node": "^14.11.5",
"@types/node": "^14.11.8",
"@types/pako": "^1.0.1",
"cypress": "^4.12.1",
"eosjs-ecc": "^4.0.7",
"eslint": "^6.8.0",
"jest": "^26.5.2",
"jest": "^26.5.3",
"jest-fetch-mock": "^3.0.3",
"ts-jest": "^26.4.1",
"ts-loader": "^7.0.5",
Expand Down
10 changes: 7 additions & 3 deletions src/eosjs-serialize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -695,16 +695,20 @@ function serializeStruct(
if (typeof data !== 'object') {
throw new Error('expected object containing data: ' + JSON.stringify(data));
}
const inputIsArray = Array.isArray(data);
if (this.base) {
if (inputIsArray) {
throw new Error('input arrays are currently only allowed for structs without a base (type=' + this.name + ', base=' + this.baseName + ')');
}
this.base.serialize(buffer, data, state, allowExtensions);
}
for (const field of this.fields) {
if (field.name in data) {
for (const [i, field] of this.fields.entries()) {
if (field.name in data || (inputIsArray && i < data.length)) {
if (state.skippedBinaryExtension) {
throw new Error('unexpected ' + this.name + '.' + field.name);
}
field.type.serialize(
buffer, data[field.name], state, allowExtensions && field === this.fields[this.fields.length - 1]);
buffer, data[inputIsArray ? i : field.name], state, allowExtensions && field === this.fields[this.fields.length - 1]);
} else {
if (allowExtensions && field.type.extensionOf) {
state.skippedBinaryExtension = true;
Expand Down
51 changes: 50 additions & 1 deletion src/tests/eosjs-serialize.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
const { TextEncoder, TextDecoder } = require('util');
import { ec } from 'elliptic';

import { createInitialTypes, Type, SerialBuffer } from '../eosjs-serialize';
import { createInitialTypes, getType, getTypesFromAbi, Type, SerialBuffer } from '../eosjs-serialize';
import { Abi } from '../eosjs-rpc-interfaces';

describe('Serialize', () => {
let types: Map<string, Type>;
Expand Down Expand Up @@ -195,4 +196,52 @@ describe('Serialize', () => {
shouldNotThrowErrorForValue(dataValue);
});
});

describe('struct', () => {
let serialBuffer: SerialBuffer;

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

beforeAll(() => {
const mockStructAbi = {
version: 'eosio::abi/1.1',
structs: [{
name: 'extension',
base: '',
fields: [{
name: 'type',
type: 'uint16'
}, {
name: 'data',
type: 'bytes'
}]
}]
} as Abi;
types = getTypesFromAbi(types, mockStructAbi);
});

it('should serialize corresponding object', () => {
const mockType: Type = getType(types, 'extension');
const expectedBuffer = Buffer.from([1, 1, 1, 1]);
mockType.serialize(serialBuffer, {
type: 257,
data: new Uint8Array([1])
});

expect(expectedBuffer.equals(serialBuffer.asUint8Array())).toEqual(true);
});

it('should serialize array input', () => {
const mockType: Type = getType(types, 'extension');
const expectedBuffer = Buffer.from([1, 1, 1, 1]);
mockType.serialize(serialBuffer, [257, [1]]);

expect(expectedBuffer.equals(serialBuffer.asUint8Array())).toEqual(true);
});
});
});
Loading