-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
203 additions
and
144 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,129 @@ | ||
import { truncate } from "fs"; | ||
import { IdentifierMetadataRecord } from "../records/identifierMetadataRecord"; | ||
import { EventService } from "./eventService"; | ||
import { DelegationService } from "./delegationService"; | ||
|
||
const identifiersGetMock = jest.fn(); | ||
let identifiersCreateMock = jest.fn(); | ||
const identifiersInteractMock = jest.fn(); | ||
|
||
const queryKeyStateMock = jest.fn(); | ||
|
||
const signifyClient = jest.mocked({ | ||
identifiers: () => ({ | ||
get: identifiersGetMock, | ||
create: identifiersCreateMock, | ||
interact: identifiersInteractMock, | ||
}), | ||
keyStates: () => ({ | ||
query: queryKeyStateMock, | ||
}), | ||
}); | ||
|
||
const identifierStorage = jest.mocked({ | ||
getIdentifierMetadata: jest.fn(), | ||
getAllIdentifierMetadata: jest.fn(), | ||
getKeriIdentifiersMetadata: jest.fn(), | ||
updateIdentifierMetadata: jest.fn(), | ||
createIdentifierMetadataRecord: jest.fn(), | ||
}); | ||
|
||
const agentServicesProps = { | ||
basicStorage: {} as any, | ||
signifyClient: signifyClient as any, | ||
eventService: new EventService(), | ||
identifierStorage: identifierStorage as any, | ||
credentialStorage: {} as any, | ||
}; | ||
|
||
const delegationService = new DelegationService(agentServicesProps); | ||
|
||
describe("Delegation sig service of agent", () => { | ||
beforeEach(() => { | ||
jest.resetAllMocks(); | ||
}); | ||
|
||
test("should call signify.createDelegationIdentifier with the correct parameters and return the result", async () => { | ||
const aid = "newIdentifierAid"; | ||
const displayName = "newDisplayName"; | ||
const signifyName = "newUuidHere"; | ||
identifiersCreateMock = jest.fn().mockResolvedValue({ | ||
identifier: aid, | ||
signifyName, | ||
serder: { | ||
ked: { | ||
i: "i", | ||
}, | ||
}, | ||
}); | ||
expect( | ||
await delegationService.createDelegatedIdentifier( | ||
{ | ||
displayName, | ||
theme: 0, | ||
}, | ||
"delegationPrefix" | ||
) | ||
).toEqual({ | ||
identifier: "i", | ||
signifyName: expect.any(String), | ||
}); | ||
expect(identifiersCreateMock).toBeCalled(); | ||
expect(identifierStorage.createIdentifierMetadataRecord).toBeCalledTimes(1); | ||
}); | ||
|
||
test("should call the interactDelegation method of the signify module with the given arguments", async () => { | ||
const signifyName = "exampleSignifyName"; | ||
const delegatePrefix = "exampleDelegatePrefix"; | ||
identifiersInteractMock.mockResolvedValue({ | ||
op: jest.fn().mockResolvedValue({ | ||
done: truncate, | ||
}), | ||
}); | ||
await delegationService.approveDelegation(signifyName, delegatePrefix); | ||
expect(identifiersInteractMock).toHaveBeenCalledWith(signifyName, { | ||
d: delegatePrefix, | ||
i: delegatePrefix, | ||
s: "0", | ||
}); | ||
}); | ||
test("should call signify.checkDelegationSuccess and update metadata isPending property to false", async () => { | ||
const metadata = { | ||
id: "123456", | ||
displayName: "John Doe", | ||
isPending: true, | ||
signifyOpName: "op123", | ||
signifyName: "john_doe", | ||
theme: 4, | ||
} as IdentifierMetadataRecord; | ||
identifiersGetMock.mockResolvedValue({ | ||
state: { | ||
id: metadata.id, | ||
}, | ||
}); | ||
queryKeyStateMock.mockResolvedValue({ | ||
done: true, | ||
}); | ||
expect(await delegationService.checkDelegationSuccess(metadata)).toEqual( | ||
true | ||
); | ||
expect(identifierStorage.updateIdentifierMetadata).toHaveBeenCalledWith( | ||
metadata.id, | ||
{ isPending: false } | ||
); | ||
}); | ||
test("should call signify.checkDelegationSuccess with isPending is false and return true", async () => { | ||
const metadata = { | ||
id: "123456", | ||
displayName: "John Doe", | ||
|
||
isPending: false, | ||
signifyOpName: "op123", | ||
signifyName: "john_doe", | ||
theme: 4, | ||
} as IdentifierMetadataRecord; | ||
expect(await delegationService.checkDelegationSuccess(metadata)).toEqual( | ||
true | ||
); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
import { v4 as uuidv4 } from "uuid"; | ||
import { CreateIdentifierResult } from "../agent.types"; | ||
import { | ||
IdentifierMetadataRecord, | ||
IdentifierMetadataRecordProps, | ||
} from "../records"; | ||
import { AgentService } from "./agentService"; | ||
import { waitAndGetDoneOp } from "./utils"; | ||
|
||
class DelegationService extends AgentService { | ||
async createDelegatedIdentifier( | ||
metadata: Omit< | ||
IdentifierMetadataRecordProps, | ||
"id" | "createdAt" | "isArchived" | "signifyName" | ||
>, | ||
delegatorPrefix: string | ||
): Promise<CreateIdentifierResult> { | ||
const signifyName = uuidv4(); | ||
const operation = await this.signifyClient | ||
.identifiers() | ||
.create(signifyName, { delpre: delegatorPrefix }); | ||
const identifier = operation.serder.ked.i; | ||
await this.identifierStorage.createIdentifierMetadataRecord({ | ||
id: identifier, | ||
...metadata, | ||
signifyName: signifyName, | ||
isPending: true, | ||
}); | ||
return { identifier, signifyName }; | ||
} | ||
|
||
async approveDelegation( | ||
signifyName: string, | ||
delegatePrefix: string | ||
): Promise<void> { | ||
const anchor = { | ||
i: delegatePrefix, | ||
s: "0", | ||
d: delegatePrefix, | ||
}; | ||
const ixnResult = await this.signifyClient | ||
.identifiers() | ||
.interact(signifyName, anchor); | ||
const operation = await ixnResult.op(); | ||
await waitAndGetDoneOp(this.signifyClient, operation); | ||
return operation.done; | ||
} | ||
|
||
async checkDelegationSuccess( | ||
metadata: IdentifierMetadataRecord | ||
): Promise<boolean> { | ||
if (!metadata.isPending) { | ||
return true; | ||
} | ||
const identifier = await this.signifyClient | ||
.identifiers() | ||
.get(metadata.signifyName); | ||
const operation = await this.signifyClient | ||
.keyStates() | ||
.query(identifier.state.di, "1"); | ||
await waitAndGetDoneOp(this.signifyClient, operation); | ||
const isDone = operation.done; | ||
if (isDone) { | ||
await this.identifierStorage.updateIdentifierMetadata(metadata.id, { | ||
isPending: false, | ||
}); | ||
} | ||
return isDone; | ||
} | ||
} | ||
|
||
export { DelegationService }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters