Skip to content

Commit

Permalink
Add draft social login logic
Browse files Browse the repository at this point in the history
  • Loading branch information
OKendigelyan committed Jan 22, 2025
1 parent b12890a commit f81f02e
Show file tree
Hide file tree
Showing 7 changed files with 75 additions and 37 deletions.
26 changes: 25 additions & 1 deletion apps/mobile/app/home.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,33 @@
import { View, Text, StyleSheet } from "react-native";
import AsyncStorage from "@react-native-async-storage/async-storage";
import { useCurrentAccount, useGetAccountBalance } from "@umami/state";
import { router } from "expo-router";
import * as SecureStore from "expo-secure-store";
import { StyleSheet, Text, View } from "react-native";
import { Button } from "tamagui";

import { persistor } from "../store/store";

export default function HomeScreen() {
const currentAccount = useCurrentAccount();
const getBalance = useGetAccountBalance();
const balance = getBalance(currentAccount!.address.pkh);

return (
<View style={styles.container}>
<Text style={styles.title}>Welcome to the Home Screen!</Text>
<Text>{currentAccount?.address.pkh}</Text>
<Text>{currentAccount?.label}</Text>
<Text>Balance: {balance}</Text>
<Button
onPress={async () => {
persistor.pause();
await AsyncStorage.clear();
await SecureStore.deleteItemAsync("authToken");
router.replace("/onboarding");
}}
>
<Button.Text>Logout</Button.Text>
</Button>
</View>
);
}
Expand Down
15 changes: 11 additions & 4 deletions apps/mobile/app/index.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
import { OnboardingScreen } from "../screens/Onboarding";
import { useCurrentAccount } from "@umami/state";

import HomeScreen from "./home";
import Onboarding from "./onboarding";

export default function MainStack() {
return (
<OnboardingScreen />
)
const currentAccount = useCurrentAccount();
console.log("currentAccount", currentAccount);

if (!currentAccount) {
return <Onboarding />;
}
return <HomeScreen />;
}
5 changes: 5 additions & 0 deletions apps/mobile/app/onboarding.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { OnboardingScreen } from "../screens/Onboarding";

export default function Onboarding() {
return <OnboardingScreen />;
}
1 change: 1 addition & 0 deletions apps/mobile/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
"@react-navigation/native": "^7.0.0",
"@tamagui/babel-plugin": "^1.121.5",
"@tamagui/config": "^1.116.15",
"@taquito/utils": "^21.0.0",
"@tezos-core-tools/crypto-utils": "^0.0.7",
"@umami/core": "workspace:^",
"@umami/crypto": "workspace:^",
Expand Down
39 changes: 21 additions & 18 deletions apps/mobile/screens/Onboarding/useOnboardingData.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { TezosToolkit } from "@taquito/taquito";
import { hex2buf } from "@taquito/utils";
import { b58cencode, prefix } from "@taquito/utils";
// @ts-ignore
import * as tezosCrypto from "@tezos-core-tools/crypto-utils";
import { useRestoreSocial } from "@umami/state";
import { MAINNET, getPublicKeyPairFromSk, makeToolkit } from "@umami/tezos";
import * as Linking from "expo-linking";
import { useRouter } from "expo-router";
import { useCallback, useEffect } from "react";
Expand All @@ -27,10 +27,10 @@ type Web3AuthLoginResponse = {
verifierId: string;
};


export const useOnboardingData = () => {
const router = useRouter();
const web3auth = createWeb3AuthInstance();
const restoreSocial = useRestoreSocial();

useEffect(() => {
const handleUrl = (event: any) => {
Expand Down Expand Up @@ -63,23 +63,29 @@ export const useOnboardingData = () => {
}

try {
const web3authResponse = await web3auth.login({ loginProvider }) as unknown as Web3AuthLoginResponse;
const web3authResponse = (await web3auth.login({
loginProvider,
})) as unknown as Web3AuthLoginResponse;

if (web3auth.connected) {
const userInfo = web3auth.userInfo();
console.log("userInfo", userInfo);
const tezos = new TezosToolkit("https://ithacanet.ecadinfra.com");

const web3authProvider = web3auth.provider;
const privateKey = await web3authProvider?.request({ method: "private_key" }) as string;
console.log('private key', privateKey);
const privateKey = (await web3authProvider?.request({ method: "private_key" })) as string;
const secretKey = b58cencode(privateKey, prefix.spsk);
const tezos = await makeToolkit({ type: "social", secretKey, network: MAINNET });

console.log("private key", privateKey);

const keyPair = tezosCrypto.utils.seedToKeyPair(hex2buf(privateKey));
const account = keyPair?.pkh;
console.log('account', account);
// const keyPair = tezosCrypto.utils.seedToKeyPair(hex2buf(privateKey));
// const account = keyPair?.pkh;
const { pk, pkh } = await getPublicKeyPairFromSk(secretKey);

const balance = await tezos.tz.getBalance(account);
console.log('balance', balance);
restoreSocial(pk, pkh, userInfo?.email || userInfo?.name || "Social account", "google");
console.log("account", pkh);

const balance = await tezos.tz.getBalance(pkh);
console.log("balance", balance);

if (web3authResponse.idToken) {
await saveToken("authToken", web3authResponse.idToken);
Expand All @@ -102,10 +108,7 @@ export const useOnboardingData = () => {
const onRedditLogin = createLoginHandler("reddit");
const onAppleLogin = createLoginHandler("apple");

const openTerms = useCallback(
() => openBrowser("https://umamiwallet.com/tos.html"),
[]
);
const openTerms = useCallback(() => openBrowser("https://umamiwallet.com/tos.html"), []);

const openPrivacy = useCallback(
() => openBrowser("https://umamiwallet.com/privacypolicy.html"),
Expand Down
9 changes: 5 additions & 4 deletions apps/mobile/services/web3AuthFactory.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import Web3Auth, { WEB3AUTH_NETWORK } from "@web3auth/react-native-sdk";
import { CommonPrivateKeyProvider } from "@web3auth/base-provider";
import { CHAIN_CONFIG, WEB3_AUTH_CLIENT_ID } from "../constants";
import * as WebBrowser from "expo-web-browser";
import * as SecureStore from "expo-secure-store";
import Web3Auth, { WEB3AUTH_NETWORK } from "@web3auth/react-native-sdk";
import { makeRedirectUri } from "expo-auth-session";
import * as SecureStore from "expo-secure-store";
import * as WebBrowser from "expo-web-browser";

import { CHAIN_CONFIG, WEB3_AUTH_CLIENT_ID } from "../constants";

export const createWeb3AuthInstance = () => {
const privateKeyProvider = new CommonPrivateKeyProvider({
Expand Down
17 changes: 7 additions & 10 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit f81f02e

Please sign in to comment.