From 42eb121e19a7d93371b73a5a180dedb8491d423e Mon Sep 17 00:00:00 2001 From: emperorhan Date: Mon, 13 Apr 2020 16:08:25 +0900 Subject: [PATCH] Add deserialize struct type --- src/eosjs-serialize.ts | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/src/eosjs-serialize.ts b/src/eosjs-serialize.ts index 756b198b9..c7e2139bf 100644 --- a/src/eosjs-serialize.ts +++ b/src/eosjs-serialize.ts @@ -1143,3 +1143,17 @@ export function deserializeAction(contract: Contract, account: string, name: str data: deserializeActionData(contract, account, name, data, textEncoder, textDecoder), }; } + +/** Deserialize struct type. If `data` is a `string`, then it's assumed to be in hex. */ +export function deserializeTypeData(contract: Contract, account: string, structName: string, data: string | Uint8Array, textEncoder: TextEncoder, textDecoder: TextDecoder): any { + const type = contract.types.get(structName); + if (typeof data === "string") { + data = hexToUint8Array(data); + } + if(!type) { + throw new Error(`Unknown type ${structName} in contract ${account}`); + } + const buffer = new SerialBuffer({textDecoder, textEncoder}); + buffer.pushArray(data); + return type.deserialize(buffer); +} \ No newline at end of file