Compare commits
3 Commits
main
...
sk-embed-s
Author | SHA1 | Date | |
---|---|---|---|
|
2d3242aad3 | ||
|
616ead7ae5 | ||
|
817aa95530 |
@ -3,3 +3,4 @@ REACT_APP_DEFAULT_GAS_PRICE=0.025
|
|||||||
# Reference: https://github.com/cosmos/cosmos-sdk/issues/16020
|
# Reference: https://github.com/cosmos/cosmos-sdk/issues/16020
|
||||||
REACT_APP_GAS_ADJUSTMENT=2
|
REACT_APP_GAS_ADJUSTMENT=2
|
||||||
REACT_APP_LACONICD_RPC_URL=https://laconicd-sapo.laconic.com
|
REACT_APP_LACONICD_RPC_URL=https://laconicd-sapo.laconic.com
|
||||||
|
REACT_APP_AUTH_SECRET=
|
||||||
|
@ -35,6 +35,7 @@ import { COSMOS_METHODS } from "./utils/wallet-connect/COSMOSData";
|
|||||||
import styles from "./styles/stylesheet";
|
import styles from "./styles/stylesheet";
|
||||||
import { Header } from "./components/Header";
|
import { Header } from "./components/Header";
|
||||||
import { WalletEmbed } from "./screens/WalletEmbed";
|
import { WalletEmbed } from "./screens/WalletEmbed";
|
||||||
|
import { AutoSignIn } from "./screens/AutoSignIn";
|
||||||
|
|
||||||
const Stack = createStackNavigator<StackParamsList>();
|
const Stack = createStackNavigator<StackParamsList>();
|
||||||
|
|
||||||
@ -321,6 +322,13 @@ const App = (): React.JSX.Element => {
|
|||||||
header: () => <></>,
|
header: () => <></>,
|
||||||
}}
|
}}
|
||||||
/>
|
/>
|
||||||
|
<Stack.Screen
|
||||||
|
name="auto-sign-in"
|
||||||
|
component={AutoSignIn}
|
||||||
|
options={{
|
||||||
|
header: () => <></>,
|
||||||
|
}}
|
||||||
|
/>
|
||||||
</Stack.Navigator>
|
</Stack.Navigator>
|
||||||
<PairingModal
|
<PairingModal
|
||||||
visible={modalVisible}
|
visible={modalVisible}
|
||||||
|
99
src/screens/AutoSignIn.tsx
Normal file
99
src/screens/AutoSignIn.tsx
Normal file
@ -0,0 +1,99 @@
|
|||||||
|
import React, { useEffect, useCallback } from 'react';
|
||||||
|
|
||||||
|
import { createWallet, retrieveAccounts } from '../utils/accounts';
|
||||||
|
import { useNetworks } from '../context/NetworksContext';
|
||||||
|
import { Account } from '../types';
|
||||||
|
import { signMessage } from '../utils/sign-message';
|
||||||
|
import { EIP155 } from '../utils/constants';
|
||||||
|
|
||||||
|
export const AutoSignIn = () => {
|
||||||
|
const { networksData } = useNetworks();
|
||||||
|
|
||||||
|
const getAccountsData = useCallback(async (chainId: string): Promise<Account[]> => {
|
||||||
|
const targetNetwork = networksData.find(network => network.chainId === chainId);
|
||||||
|
|
||||||
|
if (!targetNetwork) {
|
||||||
|
return [];
|
||||||
|
}
|
||||||
|
|
||||||
|
const accounts = await retrieveAccounts(targetNetwork);
|
||||||
|
|
||||||
|
if (!accounts || accounts.length === 0) {
|
||||||
|
return [];
|
||||||
|
}
|
||||||
|
|
||||||
|
return accounts
|
||||||
|
}, [networksData]);
|
||||||
|
|
||||||
|
const sendMessage = (
|
||||||
|
source: Window | null,
|
||||||
|
type: string,
|
||||||
|
data: any,
|
||||||
|
origin: string
|
||||||
|
): void => {
|
||||||
|
source?.postMessage({ type, data }, origin);
|
||||||
|
};
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
const handleSignIn = async (event: MessageEvent) => {
|
||||||
|
if (event.data.type !== 'AUTO_SIGN_IN') return;
|
||||||
|
|
||||||
|
const accountsData = await getAccountsData(event.data.chainId);
|
||||||
|
|
||||||
|
if (!accountsData.length) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
const signature = await signMessage({ message: event.data.message, accountId: accountsData[0].index, chainId: event.data.chainId, namespace: EIP155 })
|
||||||
|
|
||||||
|
sendMessage(event.source as Window, 'SIGN_IN_RESPONSE', { message: event.data.message, signature }, event.origin);
|
||||||
|
};
|
||||||
|
|
||||||
|
window.addEventListener('message', handleSignIn);
|
||||||
|
|
||||||
|
return () => {
|
||||||
|
window.removeEventListener('message', handleSignIn);
|
||||||
|
};
|
||||||
|
}, [networksData, getAccountsData]);
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
const getAccountAddress = async (event: MessageEvent) => {
|
||||||
|
if (event.data.type !== 'GET_ACCOUNT_ADDRESS') return;
|
||||||
|
let isNewUser = false;
|
||||||
|
|
||||||
|
if (event.data.secret !== process.env.REACT_APP_AUTH_SECRET) {
|
||||||
|
console.log('Unauthorized app.');
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
let accountsData = await getAccountsData(event.data.chainId);
|
||||||
|
|
||||||
|
if (accountsData.length === 0) {
|
||||||
|
console.log("Accounts not found, creating wallet...");
|
||||||
|
await createWallet(networksData);
|
||||||
|
|
||||||
|
isNewUser = true
|
||||||
|
|
||||||
|
// Re-fetch newly created accounts
|
||||||
|
accountsData = await getAccountsData(event.data.chainId);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!accountsData.length) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
sendMessage(event.source as Window, 'ACCOUNT_ADDRESS_RESPONSE', { address: accountsData[0].address, isNewUser }, event.origin);
|
||||||
|
};
|
||||||
|
|
||||||
|
window.addEventListener('message', getAccountAddress);
|
||||||
|
|
||||||
|
return () => {
|
||||||
|
window.removeEventListener('message', getAccountAddress);
|
||||||
|
};
|
||||||
|
}, [networksData, getAccountsData]);
|
||||||
|
|
||||||
|
return (
|
||||||
|
<>
|
||||||
|
</>
|
||||||
|
)
|
||||||
|
};
|
@ -37,6 +37,7 @@ export type StackParamsList = {
|
|||||||
requestSessionData: SessionTypes.Struct;
|
requestSessionData: SessionTypes.Struct;
|
||||||
};
|
};
|
||||||
"wallet-embed": undefined;
|
"wallet-embed": undefined;
|
||||||
|
"auto-sign-in": undefined;
|
||||||
};
|
};
|
||||||
|
|
||||||
export type Account = {
|
export type Account = {
|
||||||
|
Loading…
Reference in New Issue
Block a user