Skip to content

Commit

Permalink
🔥 A bit of cleanup post merge
Browse files Browse the repository at this point in the history
  • Loading branch information
KONFeature committed Oct 14, 2024
1 parent a59d7c3 commit c661b5e
Show file tree
Hide file tree
Showing 9 changed files with 18 additions and 130 deletions.
2 changes: 2 additions & 0 deletions iac/Backend.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ function elysiaBackend(
postgres,
sessionEncryptionKey,
jwtSecret,
jwtSdkSecret,
vapidPrivateKey,
vapidPublicKey,
coinGeckoApiKey,
Expand Down Expand Up @@ -108,6 +109,7 @@ function elysiaBackend(
// some secrets
sessionEncryptionKey,
jwtSecret,
jwtSdkSecret,
masterSecretId,
// mongo
mongoExampleUri,
Expand Down
4 changes: 4 additions & 0 deletions knip.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ const config: KnipConfig = {
"packages/shared": {
entry: "**/*.{ts,tsx}",
},
"packages/wallet": {
entry: ["src/**/*.tsx", "src/app/service-worker.ts"],
project: ["src/**/*.{ts,tsx}"],
},
"packages/backend-elysia": {
entry: "src/index.ts",
project: "src/**/*.ts",
Expand Down
2 changes: 2 additions & 0 deletions packages/backend-elysia/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@
"name": "@frak-labs/backend-elysia",
"description": "Elysia Backend for the frak ecosystem",
"version": "0.0.2",
"private": true,
"type": "module",
"license": "GNU GPL 3.0",
"scripts": {
"lint": "biome lint .",
"format:check": "biome check .",
Expand Down
13 changes: 4 additions & 9 deletions packages/wallet/src/context/wallet/action/registerOptions.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
"use client";

import {
defaultUsername,
rpId,
rpName,
} from "@/context/wallet/smartWallet/webAuthN";
import { WebAuthN } from "@frak-labs/app-essentials";
import { generateRegistrationOptions } from "@simplewebauthn/server";
import type {
AuthenticatorTransportFuture,
Expand Down Expand Up @@ -34,16 +29,16 @@ export async function getRegisterOptions({
const year = date.getFullYear().toString();

// Get the username
const username = `${defaultUsername}-${day}-${month}-${year}`;
const username = `${WebAuthN.defaultUsername}-${day}-${month}-${year}`;

// Get the user id
const randomBytes = window.crypto.getRandomValues(new Uint8Array(16));
const userId = keccak256(concatHex([toHex(username), toHex(randomBytes)]));

// Generate the registration options
return await generateRegistrationOptions({
rpName,
rpID: rpId,
rpName: WebAuthN.rpName,
rpID: WebAuthN.rpId,
userID: fromHex(userId, "bytes"),
userName: username,
userDisplayName: username,
Expand Down
4 changes: 2 additions & 2 deletions packages/wallet/src/context/wallet/action/sign.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"use server";

import { getAuthenticatorRepository } from "@/context/wallet/repository/AuthenticatorRepository";
import { rpId } from "@/context/wallet/smartWallet/webAuthN";
import { WebAuthN } from "@frak-labs/app-essentials";
import { generateAuthenticationOptions } from "@simplewebauthn/server";
import type { Hex } from "viem";

Expand All @@ -25,7 +25,7 @@ export async function getSignOptions({

// Build the options
return await generateAuthenticationOptions({
rpID: rpId,
rpID: WebAuthN.rpId,
allowCredentials: [
{
id: authenticator._id,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import type {
AuthenticatorTransportFuture,
CredentialDeviceType,
} from "@simplewebauthn/types";
import type { Binary } from "mongodb";
import type { Address } from "viem";

/**
Expand All @@ -18,7 +19,7 @@ export type AuthenticatorDocument = Readonly<{
// The extracted pub key
publicKey: P256PubKey;
// The authenticator stuff
credentialPublicKey: string;
credentialPublicKey: Binary;
counter: number;
credentialDeviceType: CredentialDeviceType;
credentialBackedUp: boolean;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { getMongoDb } from "@/context/common/mongoDb";
import type { AuthenticatorDocument } from "@/context/wallet/dto/AuthenticatorDocument";
import type { Collection } from "mongodb";
import { memo } from "radash";
import type { Address } from "viem";

/**
* Repository used to access the authenticator collection
Expand All @@ -21,55 +20,6 @@ class AuthenticatorRepository {
): Promise<AuthenticatorDocument | null> {
return this.collection.findOne({ _id: credentialId });
}

/**
* Create a new authenticator for the given user
* @param authenticator
*/
public async createAuthenticator(
authenticator: AuthenticatorDocument
): Promise<void> {
// Ensure no other credential exist with the same id
const existing = await this.getByCredentialId(authenticator._id);
if (existing) {
throw new Error("Credential already exists");
}

await this.collection.insertOne(authenticator);
}

/**
* Update the counter for the given authenticator
* @param authenticatorId
* @param counter
*/
public async updateCounter({
credentialId,
counter,
}: { credentialId: string; counter: number }): Promise<void> {
await this.collection.updateOne(
{ _id: credentialId },
{ $set: { counter } }
);
}

/**
* Set the smart wallet address for the given credential
* @param credentialId
* @param smartWalletAddress
*/
public async updateSmartWalletAddress({
credentialId,
smartWalletAddress,
}: {
credentialId: string;
smartWalletAddress: Address;
}): Promise<void> {
await this.collection.updateOne(
{ _id: credentialId },
{ $set: { smartWalletAddress } }
);
}
}

export const getAuthenticatorRepository = memo(
Expand Down
66 changes: 0 additions & 66 deletions packages/wallet/src/context/wallet/smartWallet/webAuthN.ts
Original file line number Diff line number Diff line change
@@ -1,77 +1,11 @@
import { appUrl } from "@/context/common/env";
import type { P256Signature, WebAuthNSignature } from "@/types/WebAuthN";
import { isRunningLocally } from "@frak-labs/app-essentials";
import { ECDSASigValue } from "@peculiar/asn1-ecc";
import { AsnParser } from "@peculiar/asn1-schema";
import { base64URLStringToBuffer } from "@simplewebauthn/browser";
import { decodeCredentialPublicKey } from "@simplewebauthn/server/helpers";
import type { AuthenticationResponseJSON } from "@simplewebauthn/types";
import { toHex } from "viem";
import { polygon, polygonMumbai } from "viem/chains";

/**
* The RP ID for the webauthn
*/
export const rpName = "Nexus by Frak";
export const rpId = isRunningLocally ? "localhost" : "frak.id";
export const rpOrigin = appUrl;

/**
* The default user name
*/
export const defaultUsername = "Frak Wallet";

/**
* Decode the public key from the attestation object
* @param credentialPubKey
*/
export function decodePublicKey({
credentialPubKey,
}: { credentialPubKey: Uint8Array }) {
const publicKey = decodeCredentialPublicKey(
credentialPubKey
) as unknown as {
get(key: DecodedPubKeyIndexes.kty): number | undefined;
get(key: DecodedPubKeyIndexes.alg): number | undefined;
get(key: DecodedPubKeyIndexes.crv): DecodedPubKeyCrv | undefined;
get(key: DecodedPubKeyIndexes.x): Uint8Array | undefined;
get(key: DecodedPubKeyIndexes.y): Uint8Array | undefined;
};

const x = toHex(
publicKey.get(DecodedPubKeyIndexes.x) ?? Uint8Array.from([])
);
const y = toHex(
publicKey.get(DecodedPubKeyIndexes.y) ?? Uint8Array.from([])
);

return { x, y };
}

/**
* The indexes zhere zhen can find each value in the decoded public key (matching the COSE curve)
*/
enum DecodedPubKeyIndexes {
kty = 1,
alg = 3,
crv = -1,
x = -2,
y = -3,
n = -1,
e = -2,
}

/**
* The different type of curves we knoz about the public key
*/
enum DecodedPubKeyCrv {
P256 = 1,
P384 = 2,
P521 = 3,
ED25519 = 6,
SECP256K1 = 8,
}

/**
* Verify a webauthn signature internally, and format it for blockchain transaction
*/
Expand Down
4 changes: 2 additions & 2 deletions packages/wallet/src/module/authentication/hook/useLogin.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import type { PreviousAuthenticatorModel } from "@/context/common/dexie/PreviousAuthenticatorModel";
import { rpId } from "@/context/wallet/smartWallet/webAuthN";
import { addLastAuthenticationAtom } from "@/module/authentication/atoms/lastAuthenticator";
import { sessionAtom } from "@/module/common/atoms/session";
import type { Session } from "@/types/Session";
import { WebAuthN } from "@frak-labs/app-essentials";
import { backendApi } from "@frak-labs/shared/context/server";
import { startAuthentication } from "@simplewebauthn/browser";
import { generateAuthenticationOptions } from "@simplewebauthn/server";
Expand Down Expand Up @@ -51,7 +51,7 @@ export function useLogin(

// Get the authenticate options
const authenticationOptions = await generateAuthenticationOptions({
rpID: rpId,
rpID: WebAuthN.rpId,
userVerification: "required",
allowCredentials,
// timeout in ms (3min, can be useful for mobile phone linking)
Expand Down

0 comments on commit c661b5e

Please sign in to comment.