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

Add deserialize struct type #718

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
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
20 changes: 20 additions & 0 deletions src/eosjs-serialize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1143,3 +1143,23 @@ 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);
}