-
Notifications
You must be signed in to change notification settings - Fork 21
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
19 changed files
with
744 additions
and
5 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 |
---|---|---|
|
@@ -3,4 +3,4 @@ | |
zkeys/ | ||
proofs/ | ||
tally.json | ||
|
||
session-keys.json |
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
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,17 @@ | ||
import { genPimlicoRPCUrl } from "../accountAbstraction"; | ||
import { ErrorCodes } from "../errors"; | ||
|
||
describe("common", () => { | ||
describe("genPimlicoRPCUrl", () => { | ||
test("should return the correct RPCUrl", () => { | ||
const rpcUrl = genPimlicoRPCUrl("optimism-sepolia"); | ||
expect(rpcUrl).toBeDefined(); | ||
expect(rpcUrl).toContain("https://api.pimlico.io/v2/optimism-sepolia/rpc"); | ||
}); | ||
|
||
test("should throw when PIMLICO_API_KEY is not set", () => { | ||
delete process.env.PIMLICO_API_KEY; | ||
expect(() => genPimlicoRPCUrl("optimism-sepolia")).toThrow(ErrorCodes.PIMLICO_API_KEY_NOT_SET); | ||
}); | ||
}); | ||
}); |
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,20 @@ | ||
import dotenv from "dotenv"; | ||
|
||
import { ErrorCodes } from "./errors"; | ||
|
||
dotenv.config(); | ||
|
||
/** | ||
* Generate the RPCUrl for Pimlico based on the chain we need to interact with | ||
* @param network - the network we want to interact with | ||
* @returns the RPCUrl for the network | ||
*/ | ||
export const genPimlicoRPCUrl = (network: string): string => { | ||
const pimlicoAPIKey = process.env.PIMLICO_API_KEY; | ||
|
||
if (!pimlicoAPIKey) { | ||
throw new Error(ErrorCodes.PIMLICO_API_KEY_NOT_SET); | ||
} | ||
|
||
return `https://api.pimlico.io/v2/${network}/rpc?apikey=${pimlicoAPIKey}`; | ||
}; |
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
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
57 changes: 57 additions & 0 deletions
57
packages/coordinator/ts/sessionKeys/__tests__/sessionKeys.controller.test.ts
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,57 @@ | ||
import { Test } from "@nestjs/testing"; | ||
import { zeroAddress } from "viem"; | ||
|
||
import type { IGenerateSessionKeyReturn } from "../types"; | ||
|
||
import { SessionKeysController } from "../sessionKeys.controller"; | ||
import { SessionKeysService } from "../sessionKeys.service"; | ||
|
||
describe("SessionKeysController", () => { | ||
let sessionKeysController: SessionKeysController; | ||
|
||
const mockSessionKeysService = { | ||
generateSessionKey: jest.fn(), | ||
deactivateSessionKey: jest.fn(), | ||
}; | ||
|
||
const defaultGenerateSessionKeyReturn: IGenerateSessionKeyReturn = { | ||
sessionKeyAddress: zeroAddress, | ||
}; | ||
|
||
beforeEach(async () => { | ||
const app = await Test.createTestingModule({ | ||
controllers: [SessionKeysController], | ||
}) | ||
.useMocker((token) => { | ||
if (token === SessionKeysService) { | ||
mockSessionKeysService.generateSessionKey.mockResolvedValue(defaultGenerateSessionKeyReturn); | ||
return mockSessionKeysService; | ||
} | ||
|
||
return jest.fn(); | ||
}) | ||
.compile(); | ||
|
||
sessionKeysController = app.get<SessionKeysController>(SessionKeysController); | ||
}); | ||
|
||
afterEach(() => { | ||
jest.clearAllMocks(); | ||
}); | ||
|
||
describe("v1/session-keys/generate", () => { | ||
test("should return a session key address", async () => { | ||
const data = await sessionKeysController.generateSessionKey(); | ||
expect(data).toStrictEqual(defaultGenerateSessionKeyReturn); | ||
}); | ||
}); | ||
|
||
describe("v1/session-keys/delete", () => { | ||
test("should delete a session key", () => { | ||
sessionKeysController.deactivateSessionKey({ | ||
sessionKeyAddress: zeroAddress, | ||
}); | ||
expect(mockSessionKeysService.deactivateSessionKey).toHaveBeenCalledWith(zeroAddress); | ||
}); | ||
}); | ||
}); |
79 changes: 79 additions & 0 deletions
79
packages/coordinator/ts/sessionKeys/__tests__/sessionKeys.service.test.ts
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,79 @@ | ||
import dotenv from "dotenv"; | ||
import { ZeroAddress } from "ethers"; | ||
import { zeroAddress } from "viem"; | ||
import { optimismSepolia } from "viem/chains"; | ||
|
||
import { KeyLike } from "crypto"; | ||
|
||
import { ErrorCodes } from "../../common"; | ||
import { CryptoService } from "../../crypto/crypto.service"; | ||
import { FileService } from "../../file/file.service"; | ||
import { SessionKeysService } from "../sessionKeys.service"; | ||
|
||
import { mockSessionKeyApproval } from "./utils"; | ||
|
||
dotenv.config(); | ||
|
||
describe("SessionKeysService", () => { | ||
afterEach(() => { | ||
jest.clearAllMocks(); | ||
}); | ||
|
||
const cryptoService = new CryptoService(); | ||
const fileService = new FileService(); | ||
const sessionKeysService = new SessionKeysService(cryptoService, fileService); | ||
let publicKey: KeyLike; | ||
|
||
beforeAll(async () => { | ||
publicKey = (await fileService.getPublicKey()).publicKey; | ||
}); | ||
|
||
describe("generateSessionKey", () => { | ||
test("should generate and store a session key", () => { | ||
const sessionKeyAddress = sessionKeysService.generateSessionKey(); | ||
expect(sessionKeyAddress).toBeDefined(); | ||
expect(sessionKeyAddress).not.toEqual(ZeroAddress); | ||
|
||
const sessionKey = fileService.getSessionKey(sessionKeyAddress.sessionKeyAddress); | ||
expect(sessionKey).toBeDefined(); | ||
}); | ||
}); | ||
|
||
describe("deactivateSessionKey", () => { | ||
test("should delete a session key", () => { | ||
const sessionKeyAddress = sessionKeysService.generateSessionKey(); | ||
expect(sessionKeyAddress).toBeDefined(); | ||
expect(sessionKeyAddress).not.toEqual(ZeroAddress); | ||
|
||
const sessionKey = fileService.getSessionKey(sessionKeyAddress.sessionKeyAddress); | ||
expect(sessionKey).toBeDefined(); | ||
|
||
sessionKeysService.deactivateSessionKey(sessionKeyAddress.sessionKeyAddress); | ||
const sessionKeyDeleted = fileService.getSessionKey(sessionKeyAddress.sessionKeyAddress); | ||
expect(sessionKeyDeleted).toBeUndefined(); | ||
}); | ||
}); | ||
|
||
describe("generateClientFromSessionKey", () => { | ||
test("should fail to generate a client with an invalid approval", async () => { | ||
const sessionKeyAddress = sessionKeysService.generateSessionKey(); | ||
const approval = await mockSessionKeyApproval(sessionKeyAddress.sessionKeyAddress); | ||
const encryptedApproval = cryptoService.encrypt(publicKey, approval); | ||
await expect( | ||
sessionKeysService.generateClientFromSessionKey( | ||
sessionKeyAddress.sessionKeyAddress, | ||
encryptedApproval, | ||
optimismSepolia, | ||
), | ||
).rejects.toThrow(ErrorCodes.INVALID_APPROVAL); | ||
}); | ||
|
||
test("should throw when given a non existent session key address", async () => { | ||
const approval = await mockSessionKeyApproval(zeroAddress); | ||
const encryptedApproval = cryptoService.encrypt(publicKey, approval); | ||
await expect( | ||
sessionKeysService.generateClientFromSessionKey(zeroAddress, encryptedApproval, optimismSepolia), | ||
).rejects.toThrow(ErrorCodes.SESSION_KEY_NOT_FOUND); | ||
}); | ||
}); | ||
}); |
Oops, something went wrong.