-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcapTPTool.js
48 lines (39 loc) · 1.65 KB
/
capTPTool.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
// @ts-check
import '@endo/init';
import { E, makeCapTP } from '@endo/captp';
import { nodeFetch } from './vendor/rchain-api/curl.js';
import { RNode } from './vendor/rchain-api/rnode.js';
import { makePeer } from './messageQueue.js';
import { makeAccount } from './vendor/rchain-api/proxy.js';
async function main({ https, setTimeout, clock, env }) {
const fetch = nodeFetch({ http: https });
const node = RNode(fetch);
const validator = node.validator('https://rnodeapi.rhobot.net');
const observer = node.observer('https://rnodeapi.rhobot.net');
const { BOOTSTRAP_ACCOUNT: pkHex, VAT_ID: vatId } = env;
if (!pkHex) throw Error('$BOOTSTRAP_ACCOUNT');
const account = makeAccount(pkHex, observer, { setTimeout, clock }, {});
const peer = makePeer(validator, observer);
if (!vatId) throw Error('$VAT_ID');
const myconn = peer.makeConnection(account, vatId);
const myBootstrap = {};
// Create a message dispatcher and bootstrap.
// Messages on myconn are exchanged with JSON-able objects.
const { dispatch, getBootstrap, abort } = makeCapTP(
'myid',
myconn.send,
myBootstrap,
);
myconn.onReceive((obj) => dispatch(obj));
// Get the remote's bootstrap object and call a remote method.
const boot = await getBootstrap(); // TODO: validator.propose()
await E(boot)
.lookup('stuff')
.then((res) => console.log('got res', res));
// Tear down the CapTP connection if it fails (e.g. connection is closed).
// abort(Error('Connection aborted by user.'));
}
(async () => {
const https = await import('https');
await main({ https, setTimeout, clock: () => Date.now(), env: process.env });
})().catch((err) => console.error(err));