Skip to content

Commit 8ac836f

Browse files
committed
feat(ocap-kernel): Add makeOcapUrl endowment
1 parent 7d618d0 commit 8ac836f

File tree

2 files changed

+51
-0
lines changed

2 files changed

+51
-0
lines changed

packages/ocap-kernel/src/endowments/factories/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1+
import * as makeOcapUrl from './make-ocap-url.ts';
12
import * as makeRevoker from './make-revoker.ts';
23
import * as scry from './scry.ts';
34
import type { EndowmentDefinition } from '../types.ts';
45

56
const endowmentDefinitions = {
7+
makeOcapUrl,
68
makeRevoker,
79
scry,
810
} as const satisfies Record<string, EndowmentDefinition>;
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import { makePromiseKit } from '@endo/promise-kit';
2+
import { waitUntilQuiescent } from '@metamask/kernel-utils';
3+
4+
import type { EndowmentContext } from '../types.ts';
5+
6+
/**
7+
* An ocap url.
8+
*/
9+
export type OcapUrl = `ocap://${string}`;
10+
11+
/**
12+
* A function that makes an ocap url for a given object.
13+
*/
14+
export type MakeOcapUrl = (object: unknown) => Promise<OcapUrl>;
15+
16+
/**
17+
* Make a function that makes an ocap url for a given object. Intended to be
18+
* used as a user code endowment.
19+
*
20+
* @param context - The context in which the endowment is created.
21+
* @returns A function that makes an ocap url for a given object.
22+
*/
23+
export function factory(context: EndowmentContext): MakeOcapUrl {
24+
const { toRef } = context;
25+
26+
/**
27+
* Make an ocap url for a given object.
28+
*
29+
* @param object - The object to make an ocap url for.
30+
* @returns A promise that resolves with the ocap url.
31+
*/
32+
const makeOcapUrl = async (object: unknown): Promise<OcapUrl> => {
33+
const { promise, resolve } = makePromiseKit<OcapUrl>();
34+
// This vpid can be sent to the kernel as the resolution target.
35+
const vpid = toRef(promise);
36+
const vref = toRef(object);
37+
console.log('makeOcapUrl', vpid, vref);
38+
// XXX this is a placeholder for the actual implementation which sends the
39+
// kernel a request to resolve the promise with an ocap url for the given
40+
// vref.
41+
waitUntilQuiescent()
42+
.catch((error) => console.error('wait until quiescent failed:', error))
43+
.then(() => resolve(`ocap://${vref}`))
44+
.catch((error) => console.error('resolve failed:', error));
45+
return promise;
46+
};
47+
48+
return harden(makeOcapUrl);
49+
}

0 commit comments

Comments
 (0)