import { PasskeyEmbeddedWallet } from "./passkey-wallet"; import { IStorage } from "./embedded-wallet"; import { http, HttpResponse } from "msw"; import { setupServer } from "msw/node"; import { server } from "./mock/node" import { CredentialCreationOptionsRequest, PasskeyAuthenticate, RegistrationResponseJSON, State, } from "./types"; /*const prepareRegistrationOptions = (config: {rpName: string, rpId: string}, displayName: string): GenerateRegistrationOptionsOpts => { return { rpName: config.rpName, rpID: config.rpId, userID: displayName, userName: displayName, // Don't prompt users for additional information about the authenticator // (Recommended for smoother UX) attestationType: 'none', //: TODO only enabled on test, should be "direct" in production // Prevent users from re-registering existing authenticators (in this example we allow the user to register only one authenticator so we don't provide the list of the existing ones) // excludeCredentials, authenticatorSelection: { residentKey: 'required', userVerification: 'required', }, /!** * Support the two most common algorithms: ES256, and RS256 *!/ supportedAlgorithmIDs: [-7, -257], }; }*/ describe("Wallet", () => { beforeAll(async () => { server.listen(); }); afterAll(() => { server.close(); }); test("register should throw an error if wallet is already registered", async () => { // Arrange // Act & Assert //await expect(wallet.register('JohnDoe')).rejects.toThrow("Wallet already registered"); }); test("register should create a new wallet and passkey", async () => { const createFn = jest.fn(); const getFn = jest.fn(); const passkeyAuthenticate = { create: createFn, get: getFn, } satisfies PasskeyAuthenticate; const wallet = new PasskeyEmbeddedWallet(passkeyAuthenticate, undefined, "JohnDoe"); // Arrange expect(wallet.isAvailable()).toBeFalsy(); createFn.mockResolvedValue({ id: "MMHjh_KhgG4ToxY81JBYRGiulXI_p5JbyjW9z5IfMXk", rawId: "MMHjh_KhgG4ToxY81JBYRGiulXI_p5JbyjW9z5IfMXk", response: { clientDataJSON: "eyJ0eXBlIjoid2ViYXV0aG4uY3JlYXRlIiwiY2hhbGxlbmdlIjoiMVBocEo3d3RiR3N2ZUUzaFdTTFFPQ2hZU3BWbnR2UjlCbTQtaDlNR1JLayIsIm9yaWdpbiI6Imh0dHBzOi8vdHJ5LXdlYmF1dGhuLmFwcHNwb3QuY29tIiwiY3Jvc3NPcmlnaW4iOmZhbHNlfQ", attestationObject: "o2NmbXRkbm9uZWdhdHRTdG10oGhhdXRoRGF0YViBDXX8xWP9p3nbLjQ-6kiYiHWLeFSdSTpP2-oc2WqjHMRFAAAAAQECAwQFBgcIAQIDBAUGBwgAIDDB44fyoYBuE6MWPNSQWERorpVyP6eSW8o1vc-SHzF5pAEBAycgBiFYIAnvJqNiUog6V2QKvaFQ9n6D7QX-UBvf6_lVPCa7HDoR", transports: ["internal"], }, type: "public-key", clientExtensionResults: { credProps: { rk: false, }, }, }); getFn.mockResolvedValue({ id: "MMHjh_KhgG4ToxY81JBYRGiulXI_p5JbyjW9z5IfMXk", rawId: "MMHjh_KhgG4ToxY81JBYRGiulXI_p5JbyjW9z5IfMXk", response: { authenticatorData: "DXX8xWP9p3nbLjQ-6kiYiHWLeFSdSTpP2-oc2WqjHMQFAAAAAg", clientDataJSON: "eyJ0eXBlIjoid2ViYXV0aG4uZ2V0IiwiY2hhbGxlbmdlIjoiOFd4aW91dUVvWkJmTXJGZUNXWTJwMzlYY0p5b1FmRmFrT0VWRTg4ajI0YyIsIm9yaWdpbiI6Imh0dHBzOi8vdHJ5LXdlYmF1dGhuLmFwcHNwb3QuY29tIiwiY3Jvc3NPcmlnaW4iOmZhbHNlLCJvdGhlcl9rZXlzX2Nhbl9iZV9hZGRlZF9oZXJlIjoiZG8gbm90IGNvbXBhcmUgY2xpZW50RGF0YUpTT04gYWdhaW5zdCBhIHRlbXBsYXRlLiBTZWUgaHR0cHM6Ly9nb28uZ2wveWFiUGV4In0", signature: "iKzGnnTA16GHTaqQHxjQMQbs7SkEPoaIOy8BkuRXXBK8NprhUBY_NDVCUsCFTFpBX1OdHnpUkI5kmf0yUw8XDg", }, type: "public-key", clientExtensionResults: {}, }); const createCallback = (wallet: { state: State; password: string }) => { globalThis.passwd = wallet.password; }; // register await wallet.register("JohnDoe", { walletCreateCallback: createCallback }); const walletPersist = await wallet.persistent(); const restoredWallet = new PasskeyEmbeddedWallet( passkeyAuthenticate, JSON.parse(walletPersist) as State, "JohnDoe" ); expect(await restoredWallet.unlockWallet(globalThis.passwd)).toBeTruthy(); // console.log(walletPersist) // authenticate const authWallet = new PasskeyEmbeddedWallet( passkeyAuthenticate, JSON.parse(walletPersist) as State, "JohnDoe" ); await authWallet.initialize(); expect(authWallet.isAvailable()).toBeTruthy(); }); // Add more tests as needed to cover other branches and edge cases });