@agoric/client-utils 0.1.1-dev-b2bff24.0 → 0.1.1-dev-4054171.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/package.json +9 -7
  2. package/src/vstorage.js +78 -12
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@agoric/client-utils",
3
- "version": "0.1.1-dev-b2bff24.0+b2bff24",
3
+ "version": "0.1.1-dev-4054171.0+4054171",
4
4
  "description": "Utilities for building Agoric clients",
5
5
  "license": "Apache-2.0",
6
6
  "publishConfig": {
@@ -27,13 +27,15 @@
27
27
  "ts-blank-space": "^0.4.4"
28
28
  },
29
29
  "dependencies": {
30
- "@agoric/casting": "0.4.3-dev-b2bff24.0+b2bff24",
31
- "@agoric/ertp": "0.16.3-dev-b2bff24.0+b2bff24",
32
- "@agoric/internal": "0.3.3-dev-b2bff24.0+b2bff24",
33
- "@agoric/smart-wallet": "0.5.4-dev-b2bff24.0+b2bff24",
34
- "@agoric/vats": "0.15.2-dev-b2bff24.0+b2bff24",
30
+ "@agoric/casting": "0.4.3-dev-4054171.0+4054171",
31
+ "@agoric/cosmic-proto": "0.4.1-dev-4054171.0+4054171",
32
+ "@agoric/ertp": "0.16.3-dev-4054171.0+4054171",
33
+ "@agoric/internal": "0.3.3-dev-4054171.0+4054171",
34
+ "@agoric/smart-wallet": "0.5.4-dev-4054171.0+4054171",
35
+ "@agoric/vats": "0.15.2-dev-4054171.0+4054171",
35
36
  "@cosmjs/stargate": "^0.33.0",
36
37
  "@cosmjs/tendermint-rpc": "^0.33.0",
38
+ "@endo/base64": "^1.0.9",
37
39
  "@endo/common": "^1.2.10",
38
40
  "@endo/errors": "^1.2.10",
39
41
  "@endo/marshal": "^1.6.4",
@@ -58,5 +60,5 @@
58
60
  ],
59
61
  "timeout": "20m"
60
62
  },
61
- "gitHead": "b2bff2406b336cd5e1dcd865c295bae93629e6c1"
63
+ "gitHead": "4054171a6a562a8bad1824b0151d103b8fe2f502"
62
64
  }
package/src/vstorage.js CHANGED
@@ -1,26 +1,81 @@
1
- /* global Buffer */
1
+ import {
2
+ QueryChildrenRequest,
3
+ QueryChildrenResponse,
4
+ QueryDataRequest,
5
+ QueryDataResponse,
6
+ } from '@agoric/cosmic-proto/agoric/vstorage/query.js';
7
+ import { decodeBase64 } from '@endo/base64';
8
+ import { encodeHex } from '@agoric/internal/src/hex.js';
2
9
 
3
10
  /**
4
11
  * @import {MinimalNetworkConfig} from './network-config.js';
5
12
  */
6
13
 
14
+ const codecs = {
15
+ children: {
16
+ rpc: '/agoric.vstorage.Query/Children',
17
+ request: QueryChildrenRequest,
18
+ response: QueryChildrenResponse,
19
+ },
20
+ data: {
21
+ rpc: '/agoric.vstorage.Query/Data',
22
+ request: QueryDataRequest,
23
+ response: QueryDataResponse,
24
+ },
25
+ };
26
+
27
+ /** @typedef {(typeof codecs)[keyof typeof codecs]['response']} ResponseCodec */
28
+
7
29
  /**
8
30
  * @param {object} powers
9
31
  * @param {typeof window.fetch} powers.fetch
10
32
  * @param {MinimalNetworkConfig} config
11
33
  */
12
34
  export const makeVStorage = ({ fetch }, config) => {
13
- /** @param {string} path */
14
- const getJSON = path => {
35
+ /**
36
+ * @param {string} path
37
+ */
38
+ const getJSON = async path => {
15
39
  const url = config.rpcAddrs[0] + path;
16
40
  // console.warn('fetching', url);
17
- return fetch(url, { keepalive: true }).then(res => res.json());
41
+ const res = await fetch(url, { keepalive: true });
42
+ return res.json();
43
+ };
44
+
45
+ /**
46
+ * @template {keyof codecs} T
47
+ * @param {string} [path]
48
+ * @param {object} [opts]
49
+ * @param {T} [opts.kind]
50
+ * @param {number | bigint} [opts.height] 0 is the same as omitting height and implies the highest block
51
+ */
52
+ const url = (
53
+ path = 'published',
54
+ { kind = /** @type {T} */ ('children'), height = 0 } = {},
55
+ ) => {
56
+ const codec = codecs[kind];
57
+ if (!codec) {
58
+ throw Error(`unknown rpc kind: ${kind}`);
59
+ }
60
+
61
+ const { rpc, request } = codec;
62
+ const buf = request.toProto({ path });
63
+
64
+ const hexData = encodeHex(buf);
65
+ return `/abci_query?path=%22${rpc}%22&data=0x${hexData}&height=${height}`;
18
66
  };
19
- // height=0 is the same as omitting height and implies the highest block
20
- const url = (path = 'published', { kind = 'children', height = 0 } = {}) =>
21
- `/abci_query?path=%22/custom/vstorage/${kind}/${path}%22&height=${height}`;
22
67
 
23
- const readStorage = (path = 'published', { kind = 'children', height = 0 }) =>
68
+ /**
69
+ * @template {keyof codecs} T
70
+ * @param {string} [path]
71
+ * @param {object} [opts]
72
+ * @param {T} [opts.kind]
73
+ * @param {number | bigint} [opts.height] 0 is the same as omitting height and implies the highest block
74
+ */
75
+ const readStorage = (
76
+ path = 'published',
77
+ { kind = /** @type {T} */ ('children'), height = 0 } = {},
78
+ ) =>
24
79
  getJSON(url(path, { kind, height }))
25
80
  .catch(err => {
26
81
  throw Error(`cannot read ${kind} of ${path}: ${err.message}`);
@@ -38,18 +93,29 @@ export const makeVStorage = ({ fetch }, config) => {
38
93
  err.codespace = response?.codespace;
39
94
  throw err;
40
95
  }
41
- return data;
96
+
97
+ const codec = codecs[kind];
98
+ if (!codec) {
99
+ throw Error(`unknown codec for kind: ${kind}`);
100
+ }
101
+ return { ...data, codec: codec.response };
42
102
  });
43
103
 
44
104
  const vstorage = {
45
105
  url,
46
- decode({ result: { response } }) {
106
+ /**
107
+ * @param {{ result: { response: { value: string, code: number } }, codec?: ResponseCodec }} param0
108
+ */
109
+ decode({ result: { response }, codec }) {
47
110
  const { code } = response;
48
111
  if (code !== 0) {
49
112
  throw response;
50
113
  }
51
- const { value } = response;
52
- return Buffer.from(value, 'base64').toString();
114
+ const { value: b64Value } = response;
115
+ if (!codec) {
116
+ return atob(b64Value);
117
+ }
118
+ return JSON.stringify(codec.decode(decodeBase64(b64Value)));
53
119
  },
54
120
  /**
55
121
  *