From a521ccedf997f42bbcd82cd4fb4f6a5d3d10b26b Mon Sep 17 00:00:00 2001 From: lenkan Date: Tue, 16 Jul 2024 10:56:59 +0200 Subject: [PATCH] pull in fixes from #267 --- .../integration-scripts/delegation.test.ts | 41 ++++++++++-------- src/keri/app/aiding.ts | 43 ++++++++++++++----- src/keri/app/clienting.ts | 9 ++++ src/keri/app/delegating.ts | 34 +++++++++++++++ 4 files changed, 97 insertions(+), 30 deletions(-) create mode 100644 src/keri/app/delegating.ts diff --git a/examples/integration-scripts/delegation.test.ts b/examples/integration-scripts/delegation.test.ts index 9d0f4a09..1e374d11 100644 --- a/examples/integration-scripts/delegation.test.ts +++ b/examples/integration-scripts/delegation.test.ts @@ -6,7 +6,7 @@ import { resolveOobi, waitOperation, } from './utils/test-util'; -import { getOrCreateContact } from './utils/test-setup'; +import { step } from './utils/test-step'; const { url, bootUrl } = resolveEnvironment(); @@ -79,16 +79,16 @@ test('delegation', async () => { console.log('Delegate waiting for approval...'); // Client 1 approves deletation - const anchor = { - i: delegatePrefix, - s: '0', - d: delegatePrefix, - }; - const ixnResult1 = await client1 - .identifiers() - .interact('delegator', anchor); - await waitOperation(client1, await ixnResult1.op()); - console.log('Delegator approved delegation'); + await step('delegator approves delegation', async () => { + const anchor = { + i: delegatePrefix, + s: '0', + d: delegatePrefix, + }; + const result = await client1.delegations().approve('delegator', anchor); + await waitOperation(client1, await result.op()); + expect(result.serder.ked.a[0]).toEqual(anchor); + }); let op3 = await client2.keyStates().query(aid1.prefix, '1'); await waitOperation(client2, op3); @@ -106,12 +106,15 @@ test('delegation', async () => { await waitOperation(client2, await rpyResult2.op()); const oobis = await client2.oobis().get('delegate'); - console.log(oobis); - const res = await getOrCreateContact( - client1, - 'delegate', - oobis.oobis[0].split('/agent/')[0] - ); - console.log(res); - // console.log(await client2.) + const oobi = oobis.oobis[0]; //.split('/agent/')[0]; + assert(typeof oobi === 'string'); + + const oobiOperation = await client1.oobis().resolve(oobi); + const oobiResult = await client1 + .operations() + .wait(oobiOperation, { signal: AbortSignal.timeout(10000) }); + + expect(oobiResult.response).toMatchObject({ + i: aid2.prefix, + }); }, 60000); diff --git a/src/keri/app/aiding.ts b/src/keri/app/aiding.ts index 9ad08bd6..5fb6d4ad 100644 --- a/src/keri/app/aiding.ts +++ b/src/keri/app/aiding.ts @@ -7,6 +7,7 @@ import { MtrDex } from '../core/matter'; import { Serder } from '../core/serder'; import { parseRangeHeaders } from '../core/httping'; import { KeyManager } from '../core/keeping'; +import { Operation } from './coring'; import { HabState } from '../core/state'; /** Arguments required to create an identfier */ @@ -233,8 +234,14 @@ export class Identifier { icp: serder.ked, sigs: sigs, proxy: proxy, - smids: states, - rmids: rstates ?? states, + smids: + states != undefined + ? states.map((state) => state.i) + : undefined, + rmids: + rstates != undefined + ? rstates.map((state) => state.i) + : undefined, }; jsondata[algo] = keeper.params(); @@ -251,6 +258,20 @@ export class Identifier { * @returns {Promise} A promise to the interaction event result */ async interact(name: string, data?: any): Promise { + let { serder, sigs, jsondata } = await this.createInteract(name, data); + + const res = await this.client.fetch( + '/identifiers/' + name + '?type=ixn', + 'POST', + jsondata + ); + return new EventResult(serder, sigs, res); + } + + async createInteract( + name: string, + data?: any + ): Promise<{ serder: any; sigs: any; jsondata: any }> { const hab = await this.get(name); const pre: string = hab.prefix; @@ -276,13 +297,7 @@ export class Identifier { sigs: sigs, }; jsondata[keeper.algo] = keeper.params(); - - const res = await this.client.fetch( - '/identifiers/' + name + '/events', - 'POST', - jsondata - ); - return new EventResult(serder, sigs, res); + return { serder, sigs, jsondata }; } /** @@ -364,8 +379,14 @@ export class Identifier { const jsondata: any = { rot: serder.ked, sigs: sigs, - smids: states, - rmids: rstates, + smids: + states != undefined + ? states.map((state) => state.i) + : undefined, + rmids: + rstates != undefined + ? rstates.map((state) => state.i) + : undefined, }; jsondata[keeper.algo] = keeper.params(); diff --git a/src/keri/app/clienting.ts b/src/keri/app/clienting.ts index 4c7d5c0f..9da24ecd 100644 --- a/src/keri/app/clienting.ts +++ b/src/keri/app/clienting.ts @@ -8,6 +8,7 @@ import { Contacts, Challenges } from './contacting'; import { Agent, Controller } from './controller'; import { Oobis, Operations, KeyEvents, KeyStates } from './coring'; import { Credentials, Ipex, Registries, Schemas } from './credentialing'; +import { Delegations } from './delegating'; import { Escrows } from './escrowing'; import { Exchanges } from './exchanging'; import { Groups } from './grouping'; @@ -469,4 +470,12 @@ export class SignifyClient { exchanges(): Exchanges { return new Exchanges(this); } + + /** + * Get delegations resource + * @returns {Delegations} + */ + delegations(): Delegations { + return new Delegations(this); + } } diff --git a/src/keri/app/delegating.ts b/src/keri/app/delegating.ts new file mode 100644 index 00000000..fac38973 --- /dev/null +++ b/src/keri/app/delegating.ts @@ -0,0 +1,34 @@ +import { EventResult } from './aiding'; +import { SignifyClient } from './clienting'; +import { Operation } from './coring'; + +export class Delegations { + public client: SignifyClient; + /** + * Delegations + * @param {SignifyClient} client + */ + constructor(client: SignifyClient) { + this.client = client; + } + + /** + * Approve the delegation via interaction event + * @async + * @param {string} name Name or alias of the identifier + * @param {any} [data] The anchoring interaction event + * @returns {Promise} A promise to the delegated approval result + */ + async approve(name: string, data?: any): Promise { + let { serder, sigs, jsondata } = await this.client + .identifiers() + .createInteract(name, data); + + const res = await this.client.fetch( + '/identifiers/' + name + '/delegation', + 'POST', + jsondata + ); + return new EventResult(serder, sigs, res); + } +}