Skip to content

Commit 1b747f8

Browse files
authored
chore: add typings for contacts (#275)
* add typings for contacts * fix test * fix test
1 parent 8597038 commit 1b747f8

File tree

3 files changed

+41
-67
lines changed

3 files changed

+41
-67
lines changed

examples/integration-scripts/challenge.test.ts

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -97,11 +97,9 @@ test('challenge', async () => {
9797

9898
// List Client 1 contacts
9999
let contacts1 = await client1.contacts().list();
100-
let bobContact = contacts1.find(
101-
(contact: { alias: string }) => contact.alias === 'bob'
102-
);
103-
expect(bobContact.alias).toEqual('bob');
104-
expect(bobContact.challenges).toHaveLength(0);
100+
let bobContact = contacts1.find((contact) => contact.alias === 'bob');
101+
expect(bobContact?.alias).toEqual('bob');
102+
expect(bobContact?.challenges).toHaveLength(0);
105103

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

126124
// Check Bob's challenge in conctats
127125
contacts1 = await client1.contacts().list();
128-
bobContact = contacts1.find(
129-
(contact: { alias: string }) => contact.alias === 'bob'
130-
);
131-
expect(bobContact.challenges[0].authenticated).toEqual(true);
126+
bobContact = contacts1.find((contact) => contact.alias === 'bob');
127+
128+
assert(Array.isArray(bobContact?.challenges));
129+
expect(bobContact?.challenges[0].authenticated).toBe(true);
132130

133131
await assertOperations(client1, client2);
134132
}, 30000);

src/keri/app/contacting.ts

Lines changed: 33 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,23 @@
11
import { SignifyClient } from './clienting';
22
import { Operation } from './coring';
33

4+
export interface Contact {
5+
alias: string;
6+
oobi: string;
7+
id: string;
8+
[key: string]: unknown;
9+
}
10+
11+
export interface ContactInfo {
12+
[key: string]: unknown;
13+
}
14+
415
/**
516
* Contacts
617
*/
718
export class Contacts {
819
client: SignifyClient;
20+
921
/**
1022
* Contacts
1123
* @param {SignifyClient} client
@@ -26,7 +38,7 @@ export class Contacts {
2638
group?: string,
2739
filterField?: string,
2840
filterValue?: string
29-
): Promise<any> {
41+
): Promise<Contact[]> {
3042
const params = new URLSearchParams();
3143
if (group !== undefined) {
3244
params.append('group', group);
@@ -48,7 +60,7 @@ export class Contacts {
4860
* @param {string} pre Prefix of the contact
4961
* @returns {Promise<any>} A promise to the contact
5062
*/
51-
async get(pre: string): Promise<any> {
63+
async get(pre: string): Promise<Contact> {
5264
const path = `/contacts/` + pre;
5365
const method = 'GET';
5466
const res = await this.client.fetch(path, method, null);
@@ -58,11 +70,11 @@ export class Contacts {
5870
/**
5971
* Add a contact
6072
* @async
61-
* @param {string} pre Prefix of the contact
62-
* @param {any} info Information about the contact
63-
* @returns {Promise<any>} A promise to the result of the addition
73+
* @param pre Prefix of the contact
74+
* @param info Information about the contact
75+
* @returns A promise to the result of the addition
6476
*/
65-
async add(pre: string, info: any): Promise<any> {
77+
async add(pre: string, info: ContactInfo): Promise<Contact> {
6678
const path = `/contacts/` + pre;
6779
const method = 'POST';
6880

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

@@ -99,6 +111,10 @@ export class Contacts {
99111
}
100112
}
101113

114+
export interface Challenge {
115+
words: string[];
116+
}
117+
102118
/**
103119
* Challenges
104120
*/
@@ -118,7 +134,7 @@ export class Challenges {
118134
* @param {number} strength Integer representing the strength of the challenge. Typically 128 or 256
119135
* @returns {Promise<any>} A promise to the list of random words
120136
*/
121-
async generate(strength: number = 128): Promise<any> {
137+
async generate(strength: number = 128): Promise<Challenge> {
122138
const path = `/challenges?strength=${strength.toString()}`;
123139
const method = 'GET';
124140
const res = await this.client.fetch(path, method, null);
@@ -128,16 +144,16 @@ export class Challenges {
128144
/**
129145
* Respond to a challenge by signing a message with the list of words
130146
* @async
131-
* @param {string} name Name or alias of the identifier
132-
* @param {string} recipient Prefix of the recipient of the response
133-
* @param {Array<string>} words List of words to embed in the signed response
134-
* @returns {Promise<Response>} A promise to the result of the response
147+
* @param name Name or alias of the identifier
148+
* @param recipient Prefix of the recipient of the response
149+
* @param words List of words to embed in the signed response
150+
* @returns A promise to the result of the response
135151
*/
136152
async respond(
137153
name: string,
138154
recipient: string,
139155
words: string[]
140-
): Promise<Response> {
156+
): Promise<unknown> {
141157
const hab = await this.client.identifiers().get(name);
142158
const exchanges = this.client.exchanges();
143159
const resp = await exchanges.send(
@@ -154,8 +170,8 @@ export class Challenges {
154170

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

172188
/**
173189
* Mark challenge response as signed and accepted
174-
* @param {string} source Prefix of the identifier that was challenged
175-
* @param {string} said qb64 AID of exn message representing the signed response
190+
* @param source Prefix of the identifier that was challenged
191+
* @param said qb64 AID of exn message representing the signed response
176192
* @returns {Promise<Response>} A promise to the result
177193
*/
178194
async responded(source: string, said: string): Promise<Response> {

test/app/contacting.test.ts

Lines changed: 1 addition & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -71,44 +71,6 @@ const mockGetAID = {
7171
windexes: [],
7272
};
7373

74-
const mockCredential = {
75-
sad: {
76-
v: 'ACDC10JSON000197_',
77-
d: 'EMwcsEMUEruPXVwPCW7zmqmN8m0I3CihxolBm-RDrsJo',
78-
i: 'EMQQpnSkgfUOgWdzQTWfrgiVHKIDAhvAZIPQ6z3EAfz1',
79-
ri: 'EGK216v1yguLfex4YRFnG7k1sXRjh3OKY7QqzdKsx7df',
80-
s: 'EBfdlu8R27Fbx-ehrqwImnK-8Cm79sqbAQ4MmvEAYqao',
81-
a: {
82-
d: 'EK0GOjijKd8_RLYz9qDuuG29YbbXjU8yJuTQanf07b6P',
83-
i: 'EKvn1M6shPLnXTb47bugVJblKMuWC0TcLIePP8p98Bby',
84-
dt: '2023-08-23T15:16:07.553000+00:00',
85-
LEI: '5493001KJTIIGC8Y1R17',
86-
},
87-
},
88-
pre: 'EMQQpnSkgfUOgWdzQTWfrgiVHKIDAhvAZIPQ6z3EAfz1',
89-
sadsigers: [
90-
{
91-
path: '-',
92-
pre: 'EMQQpnSkgfUOgWdzQTWfrgiVHKIDAhvAZIPQ6z3EAfz1',
93-
sn: 0,
94-
d: 'EMQQpnSkgfUOgWdzQTWfrgiVHKIDAhvAZIPQ6z3EAfz1',
95-
},
96-
],
97-
sadcigars: [],
98-
chains: [],
99-
status: {
100-
v: 'KERI10JSON000135_',
101-
i: 'EMwcsEMUEruPXVwPCW7zmqmN8m0I3CihxolBm-RDrsJo',
102-
s: '0',
103-
d: 'ENf3IEYwYtFmlq5ZzoI-zFzeR7E3ZNRN2YH_0KAFbdJW',
104-
ri: 'EGK216v1yguLfex4YRFnG7k1sXRjh3OKY7QqzdKsx7df',
105-
ra: {},
106-
a: { s: 2, d: 'EIpgyKVF0z0Pcn2_HgbWhEKmJhOXFeD4SA62SrxYXOLt' },
107-
dt: '2023-08-23T15:16:07.553000+00:00',
108-
et: 'iss',
109-
},
110-
};
111-
11274
fetchMock.mockResponse((req) => {
11375
if (req.url.startsWith(url + '/agent')) {
11476
return Promise.resolve({ body: mockConnect, init: { status: 202 } });
@@ -143,9 +105,7 @@ fetchMock.mockResponse((req) => {
143105
req.method,
144106
requrl.pathname.split('?')[0]
145107
);
146-
const body = req.url.startsWith(url + '/identifiers/aid1/credentials')
147-
? mockCredential
148-
: mockGetAID;
108+
const body = mockGetAID;
149109

150110
return Promise.resolve({
151111
body: JSON.stringify(body),

0 commit comments

Comments
 (0)