| 1 | import {
|
| 2 | canonicalHash,
|
| 3 | signObject,
|
| 4 | type InteractionEvent,
|
| 5 | type KeyPair,
|
| 6 | type TrustEnvelope,
|
| 7 | } from '../../protocol/src/index.js';
|
| 8 | export { createIdentity } from '../../core/src/index.js';
|
| 9 | export * from '../../protocol/src/index.js';
|
| 10 |
|
| 11 | export class OpenClaspClient {
|
| 12 | constructor(readonly baseUrl = 'http://localhost:3100/v0.1') {}
|
| 13 | private async request<T>(path: string, init?: RequestInit): Promise<T> {
|
| 14 | const response = await fetch(`${this.baseUrl}${path}`, {
|
| 15 | ...init,
|
| 16 | headers: { 'content-type': 'application/json', ...init?.headers },
|
| 17 | });
|
| 18 | const body = (await response.json()) as any;
|
| 19 | if (!response.ok) throw new Error(body.error ?? `OpenClasp request failed: ${response.status}`);
|
| 20 | return body as T;
|
| 21 | }
|
| 22 | registerAgent(identity: unknown) {
|
| 23 | return this.request('/agents', { method: 'POST', body: JSON.stringify(identity) });
|
| 24 | }
|
| 25 | getProfile(agentId: string, version: string, taskCategory: string) {
|
| 26 | return this.request(
|
| 27 | `/profiles/${encodeURIComponent(agentId)}?version=${encodeURIComponent(version)}&taskCategory=${encodeURIComponent(taskCategory)}`,
|
| 28 | );
|
| 29 | }
|
| 30 | saveContract(contract: unknown) {
|
| 31 | return this.request('/interactions/contracts', {
|
| 32 | method: 'POST',
|
| 33 | body: JSON.stringify(contract),
|
| 34 | });
|
| 35 | }
|
| 36 | assess(input: unknown) {
|
| 37 | return this.request('/risk/assess', { method: 'POST', body: JSON.stringify(input) });
|
| 38 | }
|
| 39 | recordEvent(event: unknown) {
|
| 40 | return this.request('/events', { method: 'POST', body: JSON.stringify(event) });
|
| 41 | }
|
| 42 | checkClaim(claim: string) {
|
| 43 | return this.request('/claims/check', { method: 'POST', body: JSON.stringify({ claim }) });
|
| 44 | }
|
| 45 | submitReceipt(receipt: unknown) {
|
| 46 | return this.request('/receipts', { method: 'POST', body: JSON.stringify(receipt) });
|
| 47 | }
|
| 48 | verifyReceipt(receipt: unknown) {
|
| 49 | return this.request('/receipts/verify', { method: 'POST', body: JSON.stringify(receipt) });
|
| 50 | }
|
| 51 | submitFeedback(feedback: unknown) {
|
| 52 | return this.request('/feedback', { method: 'POST', body: JSON.stringify(feedback) });
|
| 53 | }
|
| 54 | setContributionConsent(agentId: string, enabled: boolean) {
|
| 55 | return this.request('/contributions/consent', {
|
| 56 | method: 'POST',
|
| 57 | body: JSON.stringify({ agentId, enabled }),
|
| 58 | });
|
| 59 | }
|
| 60 | }
|
| 61 |
|
| 62 | export function createSignedEvent(
|
| 63 | input: Omit<InteractionEvent, 'payloadHash' | 'signature'>,
|
| 64 | key: KeyPair,
|
| 65 | ): InteractionEvent {
|
| 66 | return signObject(
|
| 67 | { ...input, payloadHash: canonicalHash(input.payload) },
|
| 68 | key,
|
| 69 | ) as InteractionEvent;
|
| 70 | }
|
| 71 |
|
| 72 | export function createSignedEnvelope(
|
| 73 | input: Omit<TrustEnvelope, 'signature'>,
|
| 74 | key: KeyPair,
|
| 75 | ): TrustEnvelope {
|
| 76 | return signObject(input, key) as TrustEnvelope;
|
| 77 | }
|