Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: add typings for contacts #275

Merged
merged 3 commits into from
Oct 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 7 additions & 9 deletions examples/integration-scripts/challenge.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,11 +97,9 @@ test('challenge', async () => {

// List Client 1 contacts
let contacts1 = await client1.contacts().list();
let bobContact = contacts1.find(
(contact: { alias: string }) => contact.alias === 'bob'
);
expect(bobContact.alias).toEqual('bob');
expect(bobContact.challenges).toHaveLength(0);
let bobContact = contacts1.find((contact) => contact.alias === 'bob');
expect(bobContact?.alias).toEqual('bob');
expect(bobContact?.challenges).toHaveLength(0);

// Bob responds to Alice challenge
await client2.challenges().respond('bob', aid1.i, challenge1_small.words);
Expand All @@ -125,10 +123,10 @@ test('challenge', async () => {

// Check Bob's challenge in conctats
contacts1 = await client1.contacts().list();
bobContact = contacts1.find(
(contact: { alias: string }) => contact.alias === 'bob'
);
expect(bobContact.challenges[0].authenticated).toEqual(true);
bobContact = contacts1.find((contact) => contact.alias === 'bob');

assert(Array.isArray(bobContact?.challenges));
expect(bobContact?.challenges[0].authenticated).toBe(true);

await assertOperations(client1, client2);
}, 30000);
50 changes: 33 additions & 17 deletions src/keri/app/contacting.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,23 @@
import { SignifyClient } from './clienting';
import { Operation } from './coring';

export interface Contact {
alias: string;
oobi: string;
id: string;
[key: string]: unknown;
}

export interface ContactInfo {
[key: string]: unknown;
}

/**
* Contacts
*/
export class Contacts {
client: SignifyClient;

/**
* Contacts
* @param {SignifyClient} client
Expand All @@ -26,7 +38,7 @@ export class Contacts {
group?: string,
filterField?: string,
filterValue?: string
): Promise<any> {
): Promise<Contact[]> {
const params = new URLSearchParams();
if (group !== undefined) {
params.append('group', group);
Expand All @@ -48,7 +60,7 @@ export class Contacts {
* @param {string} pre Prefix of the contact
* @returns {Promise<any>} A promise to the contact
*/
async get(pre: string): Promise<any> {
async get(pre: string): Promise<Contact> {
const path = `/contacts/` + pre;
const method = 'GET';
const res = await this.client.fetch(path, method, null);
Expand All @@ -58,11 +70,11 @@ export class Contacts {
/**
* Add a contact
* @async
* @param {string} pre Prefix of the contact
* @param {any} info Information about the contact
* @returns {Promise<any>} A promise to the result of the addition
* @param pre Prefix of the contact
* @param info Information about the contact
* @returns A promise to the result of the addition
*/
async add(pre: string, info: any): Promise<any> {
async add(pre: string, info: ContactInfo): Promise<Contact> {
const path = `/contacts/` + pre;
const method = 'POST';

Expand Down Expand Up @@ -90,7 +102,7 @@ export class Contacts {
* @param {any} info Updated information about the contact
* @returns {Promise<any>} A promise to the result of the update
*/
async update(pre: string, info: any): Promise<any> {
async update(pre: string, info: ContactInfo): Promise<Contact> {
const path = `/contacts/` + pre;
const method = 'PUT';

Expand All @@ -99,6 +111,10 @@ export class Contacts {
}
}

export interface Challenge {
words: string[];
}

/**
* Challenges
*/
Expand All @@ -118,7 +134,7 @@ export class Challenges {
* @param {number} strength Integer representing the strength of the challenge. Typically 128 or 256
* @returns {Promise<any>} A promise to the list of random words
*/
async generate(strength: number = 128): Promise<any> {
async generate(strength: number = 128): Promise<Challenge> {
const path = `/challenges?strength=${strength.toString()}`;
const method = 'GET';
const res = await this.client.fetch(path, method, null);
Expand All @@ -128,16 +144,16 @@ export class Challenges {
/**
* Respond to a challenge by signing a message with the list of words
* @async
* @param {string} name Name or alias of the identifier
* @param {string} recipient Prefix of the recipient of the response
* @param {Array<string>} words List of words to embed in the signed response
* @returns {Promise<Response>} A promise to the result of the response
* @param name Name or alias of the identifier
* @param recipient Prefix of the recipient of the response
* @param words List of words to embed in the signed response
* @returns A promise to the result of the response
*/
async respond(
name: string,
recipient: string,
words: string[]
): Promise<Response> {
): Promise<unknown> {
const hab = await this.client.identifiers().get(name);
const exchanges = this.client.exchanges();
const resp = await exchanges.send(
Expand All @@ -154,8 +170,8 @@ export class Challenges {

/**
* Ask Agent to verify a given sender signed the provided words
* @param {string} source Prefix of the identifier that was challenged
* @param {Array<string>} words List of challenge words to check for
* @param source Prefix of the identifier that was challenged
* @param words List of challenge words to check for
* @returns A promise to the long running operation
*/
async verify(source: string, words: string[]): Promise<Operation<unknown>> {
Expand All @@ -171,8 +187,8 @@ export class Challenges {

/**
* Mark challenge response as signed and accepted
* @param {string} source Prefix of the identifier that was challenged
* @param {string} said qb64 AID of exn message representing the signed response
* @param source Prefix of the identifier that was challenged
* @param said qb64 AID of exn message representing the signed response
* @returns {Promise<Response>} A promise to the result
*/
async responded(source: string, said: string): Promise<Response> {
Expand Down
42 changes: 1 addition & 41 deletions test/app/contacting.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,44 +71,6 @@ const mockGetAID = {
windexes: [],
};

const mockCredential = {
sad: {
v: 'ACDC10JSON000197_',
d: 'EMwcsEMUEruPXVwPCW7zmqmN8m0I3CihxolBm-RDrsJo',
i: 'EMQQpnSkgfUOgWdzQTWfrgiVHKIDAhvAZIPQ6z3EAfz1',
ri: 'EGK216v1yguLfex4YRFnG7k1sXRjh3OKY7QqzdKsx7df',
s: 'EBfdlu8R27Fbx-ehrqwImnK-8Cm79sqbAQ4MmvEAYqao',
a: {
d: 'EK0GOjijKd8_RLYz9qDuuG29YbbXjU8yJuTQanf07b6P',
i: 'EKvn1M6shPLnXTb47bugVJblKMuWC0TcLIePP8p98Bby',
dt: '2023-08-23T15:16:07.553000+00:00',
LEI: '5493001KJTIIGC8Y1R17',
},
},
pre: 'EMQQpnSkgfUOgWdzQTWfrgiVHKIDAhvAZIPQ6z3EAfz1',
sadsigers: [
{
path: '-',
pre: 'EMQQpnSkgfUOgWdzQTWfrgiVHKIDAhvAZIPQ6z3EAfz1',
sn: 0,
d: 'EMQQpnSkgfUOgWdzQTWfrgiVHKIDAhvAZIPQ6z3EAfz1',
},
],
sadcigars: [],
chains: [],
status: {
v: 'KERI10JSON000135_',
i: 'EMwcsEMUEruPXVwPCW7zmqmN8m0I3CihxolBm-RDrsJo',
s: '0',
d: 'ENf3IEYwYtFmlq5ZzoI-zFzeR7E3ZNRN2YH_0KAFbdJW',
ri: 'EGK216v1yguLfex4YRFnG7k1sXRjh3OKY7QqzdKsx7df',
ra: {},
a: { s: 2, d: 'EIpgyKVF0z0Pcn2_HgbWhEKmJhOXFeD4SA62SrxYXOLt' },
dt: '2023-08-23T15:16:07.553000+00:00',
et: 'iss',
},
};

fetchMock.mockResponse((req) => {
if (req.url.startsWith(url + '/agent')) {
return Promise.resolve({ body: mockConnect, init: { status: 202 } });
Expand Down Expand Up @@ -143,9 +105,7 @@ fetchMock.mockResponse((req) => {
req.method,
requrl.pathname.split('?')[0]
);
const body = req.url.startsWith(url + '/identifiers/aid1/credentials')
? mockCredential
: mockGetAID;
const body = mockGetAID;

return Promise.resolve({
body: JSON.stringify(body),
Expand Down
Loading