Skip to content

Commit

Permalink
Merge branch 'feat/privy-polishup' into dev
Browse files Browse the repository at this point in the history
  • Loading branch information
KONFeature committed Jan 3, 2025
2 parents 1b9b357 + 0c945f8 commit 5ef28d5
Show file tree
Hide file tree
Showing 35 changed files with 622 additions and 176 deletions.
Binary file modified bun.lockb
Binary file not shown.
4 changes: 4 additions & 0 deletions example/news-interactions/sst-env.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@ declare module "sst" {
"type": "sst.sst.Secret"
"value": string
}
"PRIVY_APP_ID": {
"type": "sst.sst.Secret"
"value": string
}
"SESSION_ENCRYPTION_KEY": {
"type": "sst.sst.Secret"
"value": string
Expand Down
4 changes: 4 additions & 0 deletions example/vanilla-js/sst-env.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@ declare module "sst" {
"type": "sst.sst.Secret"
"value": string
}
"PRIVY_APP_ID": {
"type": "sst.sst.Secret"
"value": string
}
"SESSION_ENCRYPTION_KEY": {
"type": "sst.sst.Secret"
"value": string
Expand Down
4 changes: 4 additions & 0 deletions example/wallet-ethcc/sst-env.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@ declare module "sst" {
"type": "sst.sst.Secret"
"value": string
}
"PRIVY_APP_ID": {
"type": "sst.sst.Secret"
"value": string
}
"SESSION_ENCRYPTION_KEY": {
"type": "sst.sst.Secret"
"value": string
Expand Down
1 change: 1 addition & 0 deletions infra/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ export const vapidPublicKey = new sst.Secret("VAPID_PUBLIC_KEY");
export const umamiWalletWebsiteId = new sst.Secret("UMAMI_WALLET_WEBSITE_ID");
export const sessionEncryptionKy = new sst.Secret("SESSION_ENCRYPTION_KEY");
export const mongoBusinessDb = new sst.Secret("MONGODB_BUSINESS_URI");
export const privyAppId = new sst.Secret("PRIVY_APP_ID");

export const postgresHost = new sst.Secret("POSTGRES_HOST");
export const postgresPassword = new sst.Secret("POSTGRES_PASSWORD");
2 changes: 2 additions & 0 deletions infra/wallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
isProd,
nexusRpcSecret,
pimlicoApiKey,
privyAppId,
umamiWalletWebsiteId,
vapidPublicKey,
} from "./config";
Expand Down Expand Up @@ -37,5 +38,6 @@ new sst.aws.StaticSite("Wallet", {
NEXUS_RPC_SECRET: nexusRpcSecret.value,
VAPID_PUBLIC_KEY: vapidPublicKey.value,
UMAMI_WALLET_WEBSITE_ID: umamiWalletWebsiteId.value,
PRIVY_APP_ID: privyAppId.value,
},
});
4 changes: 4 additions & 0 deletions packages/app-essentials/sst-env.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@ declare module "sst" {
"type": "sst.sst.Secret"
"value": string
}
"PRIVY_APP_ID": {
"type": "sst.sst.Secret"
"value": string
}
"SESSION_ENCRYPTION_KEY": {
"type": "sst.sst.Secret"
"value": string
Expand Down
4 changes: 4 additions & 0 deletions packages/backend-elysia/sst-env.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@ declare module "sst" {
"type": "sst.sst.Secret"
"value": string
}
"PRIVY_APP_ID": {
"type": "sst.sst.Secret"
"value": string
}
"SESSION_ENCRYPTION_KEY": {
"type": "sst.sst.Secret"
"value": string
Expand Down
4 changes: 4 additions & 0 deletions packages/dashboard/sst-env.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@ declare module "sst" {
"type": "sst.sst.Secret"
"value": string
}
"PRIVY_APP_ID": {
"type": "sst.sst.Secret"
"value": string
}
"SESSION_ENCRYPTION_KEY": {
"type": "sst.sst.Secret"
"value": string
Expand Down
4 changes: 4 additions & 0 deletions packages/shared/sst-env.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@ declare module "sst" {
"type": "sst.sst.Secret"
"value": string
}
"PRIVY_APP_ID": {
"type": "sst.sst.Secret"
"value": string
}
"SESSION_ENCRYPTION_KEY": {
"type": "sst.sst.Secret"
"value": string
Expand Down
14 changes: 0 additions & 14 deletions packages/wallet/app/context/blockchain/privy-cross-client.ts

This file was deleted.

1 change: 1 addition & 0 deletions packages/wallet/app/context/blockchain/privy.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const isPrivyEnabled = false;
39 changes: 22 additions & 17 deletions packages/wallet/app/context/wallet/smartWallet/connector.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import { crossAppClient } from "@/context/blockchain/privy-cross-client";
import { currentChain } from "@/context/blockchain/provider";
import { getSmartAccountProvider } from "@/context/wallet/smartWallet/provider";
import type { SmartAccountV06 } from "@/context/wallet/smartWallet/utils";
import { type Hex, type Transport, isAddressEqual } from "viem";
import type { Address, Hex, Transport } from "viem";
import { createConnector } from "wagmi";

smartAccountConnector.type = "frakSmartAccountConnector" as const;
Expand All @@ -26,8 +25,18 @@ export function smartAccountConnector<
// The current provider
let provider: Provider | undefined;

// The current privy signer
let privySigner:
| ((args: { hash: Hex; address: Address }) => Promise<Hex>)
| undefined = undefined;

// Create the wagmi connector itself
return createConnector<Provider>((config) => ({
return createConnector<
Provider,
{
setPrivySigner: (signer: typeof privySigner) => void;
}
>((config) => ({
id: "frak-wallet-connector",
name: "Frak Smart Account",
type: smartAccountConnector.type,
Expand Down Expand Up @@ -145,22 +154,11 @@ export function smartAccountConnector<
},

async signViaEcdsa(message, address) {
const currentWallet = crossAppClient.address;
if (
!(
currentWallet &&
isAddressEqual(currentWallet, address)
)
) {
throw new Error("No current wallet");
if (!privySigner) {
throw new Error("No privy signer");
}

// Send the request to the cross app client
const response = await crossAppClient.sendRequest(
"personal_sign",
[message, currentWallet]
);
return response as Hex;
return privySigner({ hash: message, address });
},
});
}
Expand All @@ -175,5 +173,12 @@ export function smartAccountConnector<
onDisconnect() {
config.emitter.emit("disconnect");
},
/**
* Update the current privy signer
* @param signer
*/
setPrivySigner(signer: typeof privySigner) {
privySigner = signer;
},
}));
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { usePrivyCrossAppAuthenticate } from "@/module/common/hook/crossAppPrivyHooks";
import { usePrivyLogin } from "@/module/authentication/hook/usePrivyLogin";
import { Button } from "@module/component/Button";
import { useTranslation } from "react-i18next";

Expand All @@ -9,11 +9,15 @@ import { useTranslation } from "react-i18next";
* @constructor
*/
export function EcdsaLogin() {
const { mutate: logIn } = usePrivyCrossAppAuthenticate();
const { privyLogin } = usePrivyLogin();
const { t } = useTranslation();

return (
<Button type={"button"} onClick={() => logIn()} variant={"primary"}>
<Button
type={"button"}
onClick={() => privyLogin()}
variant={"primary"}
>
{t("wallet.login.privy")}
</Button>
);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { crossAppClient } from "@/context/blockchain/privy-cross-client";
import { sdkSessionAtom, sessionAtom } from "@/module/common/atoms/session";
import { Panel } from "@/module/common/component/Panel";
import { jotaiStore } from "@module/atoms/store";
Expand Down Expand Up @@ -30,6 +29,7 @@ export function Logout() {
const { t } = useTranslation();
const navigate = useNavigate();
const queryClient = useQueryClient();
// const { logout: privyLogout } = usePrivyContext();

return (
<Panel size={"none"} variant={"invisible"}>
Expand All @@ -39,7 +39,8 @@ export function Logout() {
align={"left"}
onClick={async () => {
// Privy logout
crossAppClient.clearConnection();
// todo: to reput when privy enabled
// await privyLogout();
// Session deletion
jotaiStore.set(sessionAtom, RESET);
jotaiStore.set(sdkSessionAtom, RESET);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,57 +1,34 @@
import { crossAppClient } from "@/context/blockchain/privy-cross-client";
import { authenticatedBackendApi } from "@/context/common/backendClient";
import { sdkSessionAtom, sessionAtom } from "@/module/common/atoms/session";
import { usePrivyContext } from "@/module/common/provider/PrivyProvider";
import type { Session } from "@/types/Session";
import { jotaiStore } from "@module/atoms/store";
import { trackEvent } from "@module/utils/trackEvent";
import {
type UseMutationOptions,
useMutation,
useQueryClient,
} from "@tanstack/react-query";
import { type Address, type Hex, stringToHex } from "viem";
import { useMutation } from "@tanstack/react-query";
import type { UseMutationOptions } from "@tanstack/react-query";
import { type Address, stringToHex } from "viem";
import { generatePrivateKey } from "viem/accounts";

export const crossAppWalletQuery = {
queryKey: ["privy-cross-app", "wallet"],
queryFn() {
return crossAppClient.address;
},
} as const;

export const isCrossAppWalletLoggedInQuery = {
queryKey: ["privy-cross-app", "isLoggedIn"],
queryFn() {
return crossAppClient.address;
},
} as const;

/**
* Function used to trigger a privy external authentication
* Hook that handle the registration process
*/
export function usePrivyCrossAppAuthenticate(
opts?: Omit<
UseMutationOptions<Session, Error>,
"mutationFn" | "mutationKey"
>
) {
const queryClient = useQueryClient();
return useMutation({
...opts,
mutationKey: ["privy-cross-app", "authenticate"],
async mutationFn() {
let wallet = crossAppClient.address;
if (!wallet) {
// If we don't have a wallet, request a connection
await crossAppClient.requestConnection();
await queryClient.invalidateQueries({
queryKey: ["privy-cross-app"],
exact: false,
});
wallet = crossAppClient.address;
}
export function usePrivyLogin(options?: UseMutationOptions<Session>) {
const { login, signMessage } = usePrivyContext();

// If we still don't have a wallet, throw an error
// The mutation that will be used to perform the privy login process
const {
isPending: isLoading,
isSuccess,
isError,
error,
mutate: privyLogin,
mutateAsync: privyLoginAsync,
} = useMutation({
...options,
mutationKey: ["privy-login"],
mutationFn: async () => {
// Do the initial privy login stuff
const wallet = await login();
if (!wallet) {
throw new Error("No wallet selected");
}
Expand All @@ -62,11 +39,11 @@ export function usePrivyCrossAppAuthenticate(
// Build the message to sign
const message = `I want to connect to Frak and I accept the CGU.\n Verification code:${challenge}`;

// Launch the signature
const signature = (await crossAppClient.sendRequest(
"personal_sign",
[stringToHex(message), wallet]
)) as Hex | undefined;
// Sign the message
const signature = await signMessage({
hash: stringToHex(message),
address: wallet,
});
if (!signature) {
console.warn("No signature");
throw new Error("No signature returned");
Expand Down Expand Up @@ -98,4 +75,13 @@ export function usePrivyCrossAppAuthenticate(
return session;
},
});

return {
isLoading,
isSuccess,
isError,
error,
privyLogin,
privyLoginAsync,
};
}
Loading

0 comments on commit 5ef28d5

Please sign in to comment.