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

Add feature for renaming and deleting aids #241

Open
wants to merge 16 commits into
base: main
Choose a base branch
from
15 changes: 15 additions & 0 deletions examples/integration-scripts/salty.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,21 @@ test('salty', async () => {
assert.equal(serder.pre, ixn.pre);
assert.equal(serder.ked['d'], ixn.ked['d']);

const renameResult = await client1
.identifiers()
.rename('aid1', 'aidRenamed');
assert.equal(renameResult.name, 'aidRenamed');
aids = await client1.identifiers().list();
assert.equal(aids.aids.length, 3);
aid = aids.aids.pop();
assert.equal(aid.name, 'aidRenamed');

await client1.identifiers().delete('aidRenamed');
aids = await client1.identifiers().list();
assert.equal(aids.aids.length, 2);
aid = aids.aids.pop();
assert.equal(aid.name, 'aid3');

await assertOperations(client1);

console.log('Salty test passed');
Expand Down
38 changes: 34 additions & 4 deletions src/keri/app/aiding.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,36 @@ export class Identifier {
return await res.json();
}

/**
* Get information for a managed identifier
* @async
* @param {string} name Name or alias of the identifier
* @param {string} newName New name or alias of the identifier
* @returns {Promise<any>} A promise to the identifier information
*/
async rename(name: string, newName: string): Promise<any> {
const path = `/identifiers/${name}`;
const data = { name: newName };
const method = 'PUT';
const res = await this.client.fetch(path, method, data);
console.log(res.status);
lenkan marked this conversation as resolved.
Show resolved Hide resolved
return res.json();
}

/**
* Delete a managed identifier
* @async
* @param {string} name Name or alias of the identifier
* @returns {Promise<any>} A promise to the identifier information
*/
async delete(name: string): Promise<any> {
const path = `/identifiers/${name}`;
const method = 'DELETE';
const res = await this.client.fetch(path, method, null);
console.log(res.status);
lenkan marked this conversation as resolved.
Show resolved Hide resolved
return;
}

/**
* Create a managed identifier
* @async
Expand Down Expand Up @@ -277,8 +307,8 @@ export class Identifier {
jsondata[keeper.algo] = keeper.params();

const res = await this.client.fetch(
'/identifiers/' + name + '?type=ixn',
'PUT',
'/identifiers/' + name + '/events',
'POST',
jsondata
);
return new EventResult(serder, sigs, res);
Expand Down Expand Up @@ -375,8 +405,8 @@ export class Identifier {
jsondata[keeper.algo] = keeper.params();

const res = await this.client.fetch(
'/identifiers/' + name,
'PUT',
'/identifiers/' + name + '/events',
'POST',
jsondata
);
return new EventResult(serder, sigs, res);
Expand Down
46 changes: 38 additions & 8 deletions test/app/aiding.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -160,8 +160,8 @@ describe('Aiding', () => {

await client.identifiers().rotate('aid1');
const lastCall = client.getLastMockRequest();
assert.equal(lastCall.path, '/identifiers/aid1');
assert.equal(lastCall.method, 'PUT');
assert.equal(lastCall.path, '/identifiers/aid1/events');
assert.equal(lastCall.method, 'POST');
assert.deepEqual(lastCall.body.rot, {
v: 'KERI10JSON000160_',
t: 'rot',
Expand Down Expand Up @@ -206,8 +206,8 @@ describe('Aiding', () => {

await client.identifiers().rotate('aid1');
const lastCall = client.getLastMockRequest();
assert.equal(lastCall.path, '/identifiers/aid1');
assert.equal(lastCall.method, 'PUT');
assert.equal(lastCall.path, '/identifiers/aid1/events');
assert.equal(lastCall.method, 'POST');
expect(lastCall.body.rot).toMatchObject({
v: 'KERI10JSON000160_',
t: 'rot',
Expand All @@ -232,8 +232,8 @@ describe('Aiding', () => {

const lastCall = client.getLastMockRequest();

expect(lastCall.path).toEqual('/identifiers/aid1?type=ixn');
expect(lastCall.method).toEqual('PUT');
expect(lastCall.path).toEqual('/identifiers/aid1/events');
expect(lastCall.method).toEqual('POST');
expect(lastCall.body.ixn).toMatchObject({
v: 'KERI10JSON000138_',
t: 'ixn',
Expand Down Expand Up @@ -274,8 +274,8 @@ describe('Aiding', () => {

const lastCall = client.getLastMockRequest();

expect(lastCall.path).toEqual('/identifiers/aid1?type=ixn');
expect(lastCall.method).toEqual('PUT');
expect(lastCall.path).toEqual('/identifiers/aid1/events');
expect(lastCall.method).toEqual('POST');
expect(lastCall.body.ixn).toMatchObject({
s: 'b',
a: data,
Expand Down Expand Up @@ -431,4 +431,34 @@ describe('Aiding', () => {
args !== null; // avoids TS6133
});
});

it('Can rename salty identifier', async () => {
const aid1 = await createMockIdentifierState('aid1', bran, {});
client.fetch.mockResolvedValueOnce(Response.json(aid1));
client.fetch.mockResolvedValueOnce(Response.json({}));

const aidRenamed = await client
.identifiers()
.rename('aid1', 'aidRenamed');
client.fetch.mockResolvedValueOnce(Response.json(aidRenamed));
client.fetch.mockResolvedValueOnce(Response.json({}));
const lastCall = client.getLastMockRequest();
assert.equal(lastCall.path, '/identifiers/aid1');
assert.equal(lastCall.method, 'PUT');
assert.deepEqual(lastCall.body, {
name: 'aidRenamed',
});
});

it('Can delete salty identifier', async () => {
const aid1 = await createMockIdentifierState('aid1', bran, {});
client.fetch.mockResolvedValueOnce(Response.json(aid1));
client.fetch.mockResolvedValueOnce(Response.json({}));

await client.identifiers().delete('aid1');
client.fetch.mockResolvedValueOnce(Response.json({}));
const lastCall = client.getLastMockRequest();
assert.equal(lastCall.path, '/identifiers/aid1');
assert.equal(lastCall.method, 'DELETE');
});
});
Loading