Skip to content

Commit

Permalink
🚧 Base smart wallet supporting privy
Browse files Browse the repository at this point in the history
  • Loading branch information
KONFeature committed Dec 18, 2024
1 parent 32d9e9d commit 891d2e4
Show file tree
Hide file tree
Showing 14 changed files with 360 additions and 212 deletions.
5 changes: 4 additions & 1 deletion packages/app-essentials/src/blockchain/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ export {
type GetTokenMetadataParams,
} from "./actions/getTokenMetadata";
export { getTokenBalances } from "./actions/getTokenBalances";
export {
KernelWallet
} from "./wallet"
// Abis
export {
campaignFactoryAbi,
Expand Down Expand Up @@ -59,4 +62,4 @@ export { getExecutionAbi, mintAbi } from "./abis/custom";
export {
sendInteractionsSelector,
sendInteractionSelector,
} from "./abis/selectors";
} from "./abis/selectors";
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,10 @@ const webAuthNValidatorEnablingLayout = [
* @param authenticatorId
* @param signerPubKey
*/
export function getWebAuthNSmartWalletInitCode({
authenticatorIdHash,
signerPubKey,
}: {
function getWebAuthNSmartWalletInitCode({
authenticatorIdHash,
signerPubKey,
}: {
authenticatorIdHash: Hex;
signerPubKey: { x: Hex; y: Hex };
}): Hex {
Expand All @@ -78,3 +78,34 @@ export function getWebAuthNSmartWalletInitCode({
args: [kernelAddresses.accountLogic, initialisationData, 0n],
}) as Hex;
}

/**
* Get the account initialization code for a fallback smart account (using a regular ecdsa key)
* @param ecdsaAddress
*/
function getFallbackWalletInitCode({
ecdsaAddress,
}: {
ecdsaAddress: Hex;
}): Hex {
if (!ecdsaAddress) throw new Error("Owner account not found");

// Build the account initialization data
const initialisationData = encodeFunctionData({
abi: KernelInitAbi,
functionName: "initialize",
args: [kernelAddresses.ecdsaValidator, ecdsaAddress],
});

// Build the account init code
return encodeFunctionData({
abi: createAccountAbi,
functionName: "createAccount",
args: [kernelAddresses.accountLogic, initialisationData, 0n],
}) as Hex;
}

export const KernelWallet = {
getWebAuthNSmartWalletInitCode,
getFallbackWalletInitCode,
}
2 changes: 2 additions & 0 deletions packages/app-essentials/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ export {
productRoles,
interactionValidatorRoles,
type ProductRolesKey,
// Wallet
KernelWallet,
// Abis
campaignFactoryAbi,
interactionCampaignAbi,
Expand Down
2 changes: 0 additions & 2 deletions packages/app-essentials/src/webauthn/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { isRunningInProd, isRunningLocally } from "../utils";
import { getWebAuthNSmartWalletInitCode } from "./kernel";

/**
* The RP ID for the webauthn
Expand All @@ -22,5 +21,4 @@ export const WebAuthN = {
rpName,
rpOrigin,
defaultUsername,
getWebAuthNSmartWalletInitCode,
};
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ import {
mongoDbContext,
sessionContext,
} from "@backend-common";
import { WebAuthN, kernelAddresses } from "@frak-labs/app-essentials";
import {
type AuthenticationResponseJSON,
verifyAuthenticationResponse,
} from "@simplewebauthn/server";
import {WebAuthN, kernelAddresses, KernelWallet} from "@frak-labs/app-essentials";
import { Elysia } from "elysia";
import { getSenderAddress } from "permissionless/actions";
import { type Hex, concatHex, keccak256, toHex } from "viem";
Expand Down Expand Up @@ -40,7 +40,7 @@ export const webAuthNService = new Elysia({ name: "Service.webAuthN" })
}: { authenticatorId: string; pubKey: { x: Hex; y: Hex } }) {
// Compute base stuff to fetch the smart wallet address
const authenticatorIdHash = keccak256(toHex(authenticatorId));
const initCode = WebAuthN.getWebAuthNSmartWalletInitCode({
const initCode = KernelWallet.getWebAuthNSmartWalletInitCode({
authenticatorIdHash,
signerPubKey: pubKey,
});
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { baseFrakWallet } from "@/context/wallet/smartWallet/baseFrakWallet";
import type { SmartAccountV06 } from "@/context/wallet/smartWallet/utils";
import { KernelWallet } from "@frak-labs/app-essentials";
import type { Address, Chain, Client, Hex, Transport } from "viem";

export type FrakFallbackWalelt = SmartAccountV06;

/**
* Build a kernel smart account from a private key, that use the ECDSA signer behind the scene
* @param client
* @param authenticatorId
* @param signatureProvider
*/
export async function frakFallbackWalletSmartAccount<
TTransport extends Transport,
TChain extends Chain,
>(
client: Client<TTransport, TChain>,
{
ecdsaAddress,
signatureProvider,
preDeterminedAccountAddress,
}: {
ecdsaAddress: Address;
signatureProvider: (args: { hash: Hex }) => Promise<Hex>;
preDeterminedAccountAddress?: Address;
}
): Promise<FrakFallbackWalelt> {
return baseFrakWallet(client, {
getSignature: signatureProvider,
getStubSignature: () =>
"0xfffffffffffffffffffffffffffffff0000000000000000000000000000000007aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa1c",
generateInitCode: () =>
KernelWallet.getFallbackWalletInitCode({
ecdsaAddress,
}),
preDeterminedAccountAddress,
});
}
Loading

0 comments on commit 891d2e4

Please sign in to comment.