Skip to content

Commit

Permalink
pull in fixes from #267
Browse files Browse the repository at this point in the history
  • Loading branch information
lenkan committed Jul 16, 2024
1 parent fdddbee commit a521cce
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 30 deletions.
41 changes: 22 additions & 19 deletions examples/integration-scripts/delegation.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down Expand Up @@ -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');

Check warning on line 93 in examples/integration-scripts/delegation.test.ts

View workflow job for this annotation

GitHub Actions / Build, lint, and test on Node 18 and macOS-latest

'op3' is never reassigned. Use 'const' instead

Check warning on line 93 in examples/integration-scripts/delegation.test.ts

View workflow job for this annotation

GitHub Actions / Build, lint, and test on Node 20 and macOS-latest

'op3' is never reassigned. Use 'const' instead

Check warning on line 93 in examples/integration-scripts/delegation.test.ts

View workflow job for this annotation

GitHub Actions / Build, lint, and test on Node 18 and ubuntu-latest

'op3' is never reassigned. Use 'const' instead

Check warning on line 93 in examples/integration-scripts/delegation.test.ts

View workflow job for this annotation

GitHub Actions / Build, lint, and test on Node 20 and ubuntu-latest

'op3' is never reassigned. Use 'const' instead
await waitOperation(client2, op3);
Expand All @@ -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);
43 changes: 32 additions & 11 deletions src/keri/app/aiding.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand Down Expand Up @@ -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();

Expand All @@ -251,6 +258,20 @@ export class Identifier {
* @returns {Promise<EventResult>} A promise to the interaction event result
*/
async interact(name: string, data?: any): Promise<EventResult> {
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;

Expand All @@ -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 };
}

/**
Expand Down Expand Up @@ -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();

Expand Down
9 changes: 9 additions & 0 deletions src/keri/app/clienting.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -469,4 +470,12 @@ export class SignifyClient {
exchanges(): Exchanges {
return new Exchanges(this);
}

/**
* Get delegations resource
* @returns {Delegations}
*/
delegations(): Delegations {
return new Delegations(this);
}
}
34 changes: 34 additions & 0 deletions src/keri/app/delegating.ts
Original file line number Diff line number Diff line change
@@ -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<EventResult>} A promise to the delegated approval result
*/
async approve(name: string, data?: any): Promise<EventResult> {
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);
}
}

0 comments on commit a521cce

Please sign in to comment.