How to correctly parse view_state in Javascript

Viewed 74

I'm using the view_state RPC method and parsing the response via JavaScript. However, it appears most of the returned results are very useable. Is there an example of clearly printable text or another way to parse the data?

const contract = await this.props.near.connection.provider.sendJsonRpc('query', { request_type: 'view_state', finality: 'final', account_id: 'blazenet', prefix_base64: '' });

let contractParsed = contract.values.map((data, i) => {
  return { 'key': atob(data.key).replace(/[^\x20-\x7E]/g, ""), 'val': atob(data.value).replace(/[^\x20-\x7E]/g, "") };
});

Parsed response:

0: {key: "STATE", val: "blaze.betanet!as+vGj"JV0hxX_/b@zHQXz{Q?!p^/b@zHduiukuv"}
1: {key: "ui ]qU2s7AG5|c7`-?", val: ""}
2: {key: "ui bJ+e,-,[y]CsKUB", val: ""}
3: {key: "ui *EDfC*,#,/", val: ""}
4: {key: "ui ZB\[`VR\/a", val: ""}
5: {key: "ui _|XW==. 0M$l", val: ""}
6: {key: "ui \;>bR?{Ut7/Q", val: ""}
7: {key: "uk", val: " ZB\[`VR\/a"}
8: {key: "uk", val: " \;>bR?{Ut7/Q"}
9: {key: "uk", val: " ]qU2s7AG5|c7`-?"}
10: {key: "uk", val: " *EDfC*,#,/"}
11: {key: "uk", val: " _|XW==. 0M$l"}
12: {key: "uk", val: " bJ+e,-,[y]CsKUB"}
13: {key: "uv", val: "i\s1"}
14: {key: "uv", val: "z-:HYc:"}
15: {key: "uv", val: "Z|"}
16: {key: "uv", val: "Vk/"}
17: {key: "uv", val: "ESQlVA"}
18: {key: "uv", val: "`>(u{|{"}
1 Answers

Both key and value are encoded in base64.

Keys and Values might be serialized with Borsh, in order to deserialize it you need to know the schema of each key and value. Read more about https://borsh.io

Value under the key STATE serializes the main structure of the contract.

Related