Skip to content

Commit

Permalink
feat: rename managed identifier (#283)
Browse files Browse the repository at this point in the history
* feat: rename identifier

* feat: updated interface and await before return

* refactor: prettier run

* test: update unit test with new interface
  • Loading branch information
iFergal authored Oct 17, 2024
1 parent 32ceda1 commit 36bed7b
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 4 deletions.
9 changes: 9 additions & 0 deletions examples/integration-scripts/salty.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -168,5 +168,14 @@ test('salty', async () => {

await assertOperations(client1);

aid = await client1.identifiers().update('aid3', { name: 'aid4' });
assert.equal(aid.name, 'aid4');
aid = await client1.identifiers().get('aid4');
assert.equal(aid.name, 'aid4');
aids = await client1.identifiers().list(2, 2);
assert.equal(aids.aids.length, 1);
aid = aids.aids[0];
assert.equal(aid.name, 'aid4');

console.log('Salty test passed');
}, 30000);
28 changes: 24 additions & 4 deletions src/keri/app/aiding.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ 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 @@ -67,6 +66,13 @@ export interface IdentifierDeps {
manager: KeyManager | null;
}

/**
* Updatable information for a managed identifier
*/
export interface IdentifierInfo {
name: string;
}

/** Identifier */
export class Identifier {
public client: IdentifierDeps;
Expand Down Expand Up @@ -110,8 +116,8 @@ export class Identifier {
/**
* Get information for a managed identifier
* @async
* @param {string} name Name or alias of the identifier
* @returns {Promise<any>} A promise to the identifier information
* @param {string} name Prefix or alias of the identifier
* @returns {Promise<HabState>} A promise to the identifier information
*/
async get(name: string): Promise<HabState> {
const path = `/identifiers/${encodeURIComponent(name)}`;
Expand All @@ -121,6 +127,20 @@ export class Identifier {
return await res.json();
}

/**
* Update managed identifier
* @async
* @param {string} name Prefix or alias of the identifier
* @param {IdentifierInfo} info Information to update for the given identifier
* @returns {Promise<HabState>} A promise to the identifier information after updating
*/
async update(name: string, info: IdentifierInfo): Promise<HabState> {
const path = `/identifiers/${name}`;
const method = 'PUT';
const res = await this.client.fetch(path, method, info);
return await res.json();
}

/**
* Create a managed identifier
* @async
Expand Down Expand Up @@ -253,7 +273,7 @@ export class Identifier {
/**
* Generate an interaction event in a managed identifier
* @async
* @param {string} name Name or alias of the identifier
* @param {string} name Prefix or alias of the identifier
* @param {any} [data] Option data to be anchored in the interaction event
* @returns {Promise<EventResult>} A promise to the interaction event result
*/
Expand Down
9 changes: 9 additions & 0 deletions test/app/aiding.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -369,6 +369,15 @@ describe('Aiding', () => {
assert.deepEqual(lastCall.body.randy.transferable, true);
});

it('Can rename identifier', async () => {
client.fetch.mockResolvedValue(Response.json({}));
await client.identifiers().update('aid1', { name: 'aid2' });
const lastCall = client.getLastMockRequest();
assert.equal(lastCall.path, '/identifiers/aid1');
assert.equal(lastCall.method, 'PUT');
assert.equal(lastCall.body.name, 'aid2');
});

describe('Group identifiers', () => {
it('Can Rotate group', async () => {
const member1 = await createMockIdentifierState(
Expand Down

0 comments on commit 36bed7b

Please sign in to comment.