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

JsonToRawAbi, inverse of rawAbiToJson #641

Merged
merged 2 commits into from
Jan 3, 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
13 changes: 13 additions & 0 deletions src/eosjs-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,19 @@ export class Api {
return this.abiTypes.get('abi_def').deserialize(buffer);
}

/** Encodes a json abi as Uint8Array. */
public jsonToRawAbi(jsonAbi: Abi): Uint8Array {
const buffer = new ser.SerialBuffer({
textEncoder: this.textEncoder,
textDecoder: this.textDecoder,
});
this.abiTypes.get('abi_def').serialize(buffer, jsonAbi);
if (!ser.supportedAbiVersion(buffer.getString())) {
throw new Error('Unsupported abi version');
}
return buffer.asUint8Array();
}

/** Get abi in both binary and structured forms. Fetch when needed. */
public async getCachedAbi(accountName: string, reload = false): Promise<CachedAbi> {
if (!reload && this.cachedAbis.get(accountName)) {
Expand Down
31 changes: 30 additions & 1 deletion src/tests/eosjs-api.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ const deserializedActions = [

describe('eosjs-api', () => {
let api: any;
let rpc: any;
const fetch = async (input: any, init: any): Promise<any> => ({
ok: true,
json: async () => {
Expand All @@ -142,7 +143,7 @@ describe('eosjs-api', () => {
});

beforeEach(() => {
const rpc = new JsonRpc('', { fetch });
rpc = new JsonRpc('', { fetch });
const signatureProvider = new JsSignatureProvider(['5JtUScZK2XEp3g9gh7F8bwtPTRAkASmNrrftmx4AxDKD5K4zDnr']);
const chainId = '038f4b0fc8ff18a4f0842a8f0564611f6e96e8535901dd45e43ac8691a1c4dca';
api = new Api({
Expand Down Expand Up @@ -191,4 +192,32 @@ describe('eosjs-api', () => {

expect(response).toEqual(true);
});

it('rawAbiToJson returns correct Json from raw Abi', async () => {
const expected = await api.getAbi('testeostoken');
const response = await rpc.getRawAbi('testeostoken');
const actual = api.rawAbiToJson(response.abi);

expect(actual).toEqual(expected);
});

it('jsonToRawAbi returns correct raw Abi from Json', async () => {
const response = await rpc.getRawAbi('testeostoken');
const expected = response.abi;
const jsonAbi = await api.getAbi('testeostoken');
const actual = api.jsonToRawAbi(jsonAbi);

expect(actual).toEqual(expected);
});

it('confirms jsonToRawAbi and rawAbiToJson are reciprocal', async () => {
const expectedJsonAbi = await api.getAbi('testeostoken');
const response = await rpc.getRawAbi('testeostoken');
const expectedRawAbi = response.abi;
const jsonAbi = api.rawAbiToJson(api.jsonToRawAbi(expectedJsonAbi));
const rawAbi = api.jsonToRawAbi(api.rawAbiToJson(expectedRawAbi));

expect(rawAbi).toEqual(expectedRawAbi);
expect(jsonAbi).toEqual(expectedJsonAbi);
});
});