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

Add deserialize struct type #717

Closed
wants to merge 12 commits into from
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ Documentation can be found [here](https://eosio.github.io/eosjs)

The official distribution package can be found at [npm](https://www.npmjs.com/package/eosjs).

### NodeJS Dependency
### Add dependency to your project

`yarn add eosjs`

Expand All @@ -38,15 +38,15 @@ Clone this repository locally then run `yarn build-web`. The browser distributi

### ES Modules

Importing using ES6 module syntax in the browser is supported if you have a transpiler, such as Babel.
Importing using ESM syntax is supported using TypeScript, [webpack](https://webpack.js.org/api/module-methods), or [Node.js with `--experimental-modules` flag](https://nodejs.org/api/esm.html)
```js
import { Api, JsonRpc, RpcError } from 'eosjs';
import { JsSignatureProvider } from 'eosjs/dist/eosjs-jssig'; // development only
```

### CommonJS

Importing using commonJS syntax is supported by NodeJS out of the box.
Importing using commonJS syntax is supported by Node.js out of the box.
```js
const { Api, JsonRpc, RpcError } = require('eosjs');
const { JsSignatureProvider } = require('eosjs/dist/eosjs-jssig'); // development only
Expand All @@ -69,7 +69,7 @@ const signatureProvider = new JsSignatureProvider([defaultPrivateKey]);

### JSON-RPC

Open a connection to JSON-RPC, include `fetch` when on NodeJS.
Open a connection to JSON-RPC, include `fetch` when on Node.js.
```js
const rpc = new JsonRpc('http://127.0.0.1:8888', { fetch });
```
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
"test": "jest src/tests/*eosjs*",
"test-node": "jest src/tests/*node*",
"test-all": "yarn test && yarn test-node && yarn cypress",
"build": "tsc -p ./tsconfig.json && cp src/ripemd.es5.js dist/ripemd.js",
"build": "tsc -p ./tsconfig.json && node scripts/copy-ripe-md.js",
"build-web": "webpack --config webpack.prod.js && webpack --config webpack.debug.js",
"build-production": "yarn build && yarn build-web && yarn test-all",
"clean": "rm -rf dist",
Expand Down
8 changes: 8 additions & 0 deletions scripts/copy-ripe-md.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
var fs = require('fs');
var path = require('path');
var root = __dirname.replace('scripts', '');

if(!fs.existsSync(path.join(root + 'dist')))
fs.mkdirSync(path.join(root + 'dist'));

fs.copyFileSync(path.join(root + 'src/ripemd.es5.js'), path.join(root + 'dist/ripemd.js'));
14 changes: 14 additions & 0 deletions src/eosjs-serialize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}