Add iframe component for signing messages (#25)
Part of https://www.notion.so/Gentx-like-attestation-mechanism-to-add-validators-at-genesis-time-19da6b22d47280ecbf1fe657c241ff59 Co-authored-by: IshaVenikar <ishavenikar7@gmail.com> Co-authored-by: Shreerang Kale <shreerangkale@gmail.com> Co-authored-by: AdityaSalunkhe21 <adityasalunkhe2204@gmail.com> Reviewed-on: LaconicNetwork/laconic-wallet-web#25 Co-authored-by: ishavenikar <ishavenikar@noreply.git.vdb.to> Co-committed-by: ishavenikar <ishavenikar@noreply.git.vdb.to>
This commit is contained in:
parent
6d5fcf798d
commit
713f8bc0bb
@ -5,4 +5,5 @@ REACT_APP_DEFAULT_GAS_PRICE=0.025
|
||||
REACT_APP_GAS_ADJUSTMENT=2
|
||||
REACT_APP_LACONICD_RPC_URL=https://laconicd-sapo.laconic.com
|
||||
|
||||
REACT_APP_DEPLOY_APP_URL=
|
||||
# Example: https://example-url-1.com,https://example-url-2.com
|
||||
REACT_APP_ALLOWED_URLS=
|
||||
|
@ -41,6 +41,7 @@ import { AutoSignIn } from "./screens/AutoSignIn";
|
||||
import { checkSufficientFunds, getPathKey, sendMessage } from "./utils/misc";
|
||||
import useAccountsData from "./hooks/useAccountsData";
|
||||
import { useWebViewHandler } from "./hooks/useWebViewHandler";
|
||||
import SignMessageEmbed from "./screens/SignMessageEmbed";
|
||||
|
||||
const Stack = createStackNavigator<StackParamsList>();
|
||||
|
||||
@ -390,6 +391,13 @@ const App = (): React.JSX.Element => {
|
||||
header: () => <></>,
|
||||
}}
|
||||
/>
|
||||
<Stack.Screen
|
||||
name="sign-request-embed"
|
||||
component={SignMessageEmbed}
|
||||
options={{
|
||||
header: () => <Header title="Wallet" />,
|
||||
}}
|
||||
/>
|
||||
</Stack.Navigator>
|
||||
<PairingModal
|
||||
visible={modalVisible}
|
||||
|
@ -6,6 +6,8 @@ import useAccountsData from "./useAccountsData";
|
||||
import { useNetworks } from "../context/NetworksContext";
|
||||
import { useAccounts } from "../context/AccountsContext";
|
||||
|
||||
const REACT_APP_ALLOWED_URLS = process.env.REACT_APP_ALLOWED_URLS;
|
||||
|
||||
const useGetOrCreateAccounts = () => {
|
||||
const { networksData } = useNetworks();
|
||||
const { getAccountsData } = useAccountsData();
|
||||
@ -31,6 +33,18 @@ const useGetOrCreateAccounts = () => {
|
||||
const handleCreateAccounts = async (event: MessageEvent) => {
|
||||
if (event.data.type !== 'REQUEST_CREATE_OR_GET_ACCOUNTS') return;
|
||||
|
||||
if (!REACT_APP_ALLOWED_URLS) {
|
||||
console.log('allowed URLs are not set.');
|
||||
return;
|
||||
}
|
||||
|
||||
const allowedUrls = REACT_APP_ALLOWED_URLS.split(',').map(url => url.trim());
|
||||
|
||||
if (!allowedUrls.includes(event.origin)) {
|
||||
console.log('Unauthorized app.');
|
||||
return;
|
||||
}
|
||||
|
||||
const accountsData = await getOrCreateAccountsForChain(event.data.chainId);
|
||||
|
||||
sendMessage(
|
||||
@ -42,7 +56,7 @@ const useGetOrCreateAccounts = () => {
|
||||
|
||||
const autoCreateAccounts = async () => {
|
||||
const defaultChainId = networksData[0]?.chainId;
|
||||
|
||||
|
||||
if (!defaultChainId) {
|
||||
console.log('useGetOrCreateAccounts: No default chainId found');
|
||||
return;
|
||||
@ -60,7 +74,7 @@ const useGetOrCreateAccounts = () => {
|
||||
window.addEventListener('message', handleCreateAccounts);
|
||||
|
||||
const isAndroidWebView = !!(window.Android);
|
||||
|
||||
|
||||
if (isAndroidWebView) {
|
||||
autoCreateAccounts();
|
||||
}
|
||||
|
@ -40,7 +40,7 @@ export const useWebViewHandler = () => {
|
||||
const path = `/sign/${selectedNetwork.namespace}/${selectedNetwork.chainId}/${currentAccount.address}/${encodeURIComponent(message)}`;
|
||||
const pathRegex = /^\/sign\/(eip155|cosmos)\/(.+)\/(.+)\/(.+)$/;
|
||||
const match = path.match(pathRegex);
|
||||
|
||||
|
||||
if (!match) {
|
||||
window.Android?.onSignatureError?.('Invalid signing path');
|
||||
return;
|
||||
|
@ -7,6 +7,8 @@ import { sendMessage } from '../utils/misc';
|
||||
import useAccountsData from '../hooks/useAccountsData';
|
||||
import useGetOrCreateAccounts from '../hooks/useGetOrCreateAccounts';
|
||||
|
||||
const REACT_APP_ALLOWED_URLS = process.env.REACT_APP_ALLOWED_URLS;
|
||||
|
||||
export const AutoSignIn = () => {
|
||||
const { networksData } = useNetworks();
|
||||
|
||||
@ -16,7 +18,14 @@ export const AutoSignIn = () => {
|
||||
const handleSignIn = async (event: MessageEvent) => {
|
||||
if (event.data.type !== 'AUTO_SIGN_IN') return;
|
||||
|
||||
if (event.origin !== process.env.REACT_APP_DEPLOY_APP_URL) {
|
||||
if (!REACT_APP_ALLOWED_URLS) {
|
||||
console.log('allowed URLs are not set.');
|
||||
return;
|
||||
}
|
||||
|
||||
const allowedUrls = REACT_APP_ALLOWED_URLS.split(',').map(url => url.trim());
|
||||
|
||||
if (!allowedUrls.includes(event.origin)) {
|
||||
console.log('Unauthorized app.');
|
||||
return;
|
||||
}
|
||||
|
191
src/screens/SignMessageEmbed.tsx
Normal file
191
src/screens/SignMessageEmbed.tsx
Normal file
@ -0,0 +1,191 @@
|
||||
import React, { useEffect, useState } from 'react';
|
||||
import { ScrollView, View } from 'react-native';
|
||||
import { ActivityIndicator, Button, Text, Appbar } from 'react-native-paper';
|
||||
|
||||
import { useNavigation } from '@react-navigation/native';
|
||||
import {
|
||||
NativeStackNavigationProp,
|
||||
NativeStackScreenProps,
|
||||
} from '@react-navigation/native-stack';
|
||||
import { getHeaderTitle } from '@react-navigation/elements';
|
||||
|
||||
import { Account, StackParamsList } from '../types';
|
||||
import AccountDetails from '../components/AccountDetails';
|
||||
import styles from '../styles/stylesheet';
|
||||
import { getCosmosAccounts, retrieveSingleAccount } from '../utils/accounts';
|
||||
import { getMnemonic, getPathKey, sendMessage } from '../utils/misc';
|
||||
import { COSMOS } from '../utils/constants';
|
||||
|
||||
const REACT_APP_ALLOWED_URLS = process.env.REACT_APP_ALLOWED_URLS;
|
||||
|
||||
type SignRequestProps = NativeStackScreenProps<StackParamsList, 'sign-request-embed'>;
|
||||
|
||||
const SignMessageEmbed = ({ route }: SignRequestProps) => {
|
||||
const [displayAccount, setDisplayAccount] = useState<Account>();
|
||||
const [message, setMessage] = useState<string>('');
|
||||
const [chainId, setChainId] = useState<string>('');
|
||||
const [signDoc, setSignDoc] = useState<any>(null);
|
||||
const [signerAddress, setSignerAddress] = useState<string>('');
|
||||
const [origin, setOrigin] = useState<string>('');
|
||||
const [sourceWindow, setSourceWindow] = useState<Window | null>(null);
|
||||
const [isLoading, setIsLoading] = useState(true);
|
||||
const [isApproving, setIsApproving] = useState(false);
|
||||
|
||||
const navigation =
|
||||
useNavigation<NativeStackNavigationProp<StackParamsList>>();
|
||||
|
||||
const signMessageHandler = async () => {
|
||||
if (!signDoc || !signerAddress || !sourceWindow) return;
|
||||
|
||||
setIsApproving(true);
|
||||
try {
|
||||
const requestAccount = await retrieveSingleAccount(COSMOS, chainId, signerAddress);
|
||||
const path = (await getPathKey(`${COSMOS}:${chainId}`, requestAccount!.index)).path;
|
||||
const mnemonic = await getMnemonic();
|
||||
const cosmosAccount = await getCosmosAccounts(mnemonic, path, 'zenith');
|
||||
|
||||
const cosmosAminoSignature = await cosmosAccount.cosmosWallet.signAmino(
|
||||
signerAddress,
|
||||
signDoc,
|
||||
);
|
||||
|
||||
const signature = cosmosAminoSignature.signature.signature;
|
||||
|
||||
sendMessage(
|
||||
sourceWindow,
|
||||
'ZENITH_SIGNED_MESSAGE',
|
||||
{ signature },
|
||||
origin,
|
||||
);
|
||||
|
||||
navigation.navigate('Home');
|
||||
} catch (err) {
|
||||
console.error('Signing failed:', err);
|
||||
sendMessage(
|
||||
sourceWindow!,
|
||||
'ZENITH_SIGNED_MESSAGE',
|
||||
{ error: err },
|
||||
origin,
|
||||
);
|
||||
} finally {
|
||||
setIsApproving(false);
|
||||
}
|
||||
};
|
||||
|
||||
const rejectRequestHandler = async () => {
|
||||
if (sourceWindow && origin) {
|
||||
sendMessage(
|
||||
sourceWindow,
|
||||
'ZENITH_SIGNED_MESSAGE',
|
||||
{ error: 'User rejected the request' },
|
||||
origin,
|
||||
);
|
||||
}
|
||||
navigation.navigate('Home');
|
||||
};
|
||||
|
||||
useEffect(() => {
|
||||
const handleCosmosSignMessage = async (event: MessageEvent) => {
|
||||
if (event.data.type !== 'SIGN_ZENITH_MESSAGE') return;
|
||||
|
||||
|
||||
if (!REACT_APP_ALLOWED_URLS) {
|
||||
console.log('allowed URLs are not set.');
|
||||
return;
|
||||
}
|
||||
|
||||
const allowedUrls = REACT_APP_ALLOWED_URLS.split(',').map(url => url.trim());
|
||||
|
||||
if (!allowedUrls.includes(event.origin)) {
|
||||
console.log('Unauthorized app.');
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
const { signerAddress, signDoc } = event.data.params;
|
||||
|
||||
setSignerAddress(signerAddress);
|
||||
setSignDoc(signDoc);
|
||||
setMessage(signDoc.memo || '');
|
||||
setOrigin(event.origin);
|
||||
setSourceWindow(event.source as Window);
|
||||
setChainId(event.data.chainId);
|
||||
|
||||
const requestAccount = await retrieveSingleAccount(
|
||||
COSMOS,
|
||||
event.data.chainId,
|
||||
signerAddress,
|
||||
);
|
||||
|
||||
setDisplayAccount(requestAccount);
|
||||
setIsLoading(false);
|
||||
} catch (err) {
|
||||
console.error('Error preparing sign request:', err);
|
||||
setIsLoading(false);
|
||||
}
|
||||
};
|
||||
|
||||
window.addEventListener('message', handleCosmosSignMessage);
|
||||
return () => window.removeEventListener('message', handleCosmosSignMessage);
|
||||
}, []);
|
||||
|
||||
useEffect(() => {
|
||||
navigation.setOptions({
|
||||
// eslint-disable-next-line react/no-unstable-nested-components
|
||||
header: ({ options, back }) => {
|
||||
const title = getHeaderTitle(options, 'Sign Message');
|
||||
|
||||
return (
|
||||
<Appbar.Header>
|
||||
{back && (
|
||||
<Appbar.BackAction
|
||||
onPress={async () => {
|
||||
await rejectRequestHandler();
|
||||
navigation.navigate('Home');
|
||||
}}
|
||||
/>
|
||||
)}
|
||||
<Appbar.Content title={title} />
|
||||
</Appbar.Header>
|
||||
);
|
||||
},
|
||||
});
|
||||
// eslint-disable-next-line react-hooks/exhaustive-deps
|
||||
}, [navigation]);
|
||||
|
||||
return (
|
||||
<>
|
||||
{isLoading ? (
|
||||
<View style={styles.spinnerContainer}>
|
||||
<ActivityIndicator size="large" color="#0000ff" />
|
||||
</View>
|
||||
) : (
|
||||
<>
|
||||
<ScrollView contentContainerStyle={styles.appContainer}>
|
||||
<AccountDetails account={displayAccount} />
|
||||
<View style={styles.requestMessage}>
|
||||
<Text variant="bodyLarge">{message}</Text>
|
||||
</View>
|
||||
</ScrollView>
|
||||
<View style={styles.buttonContainer}>
|
||||
<Button
|
||||
mode="contained"
|
||||
onPress={signMessageHandler}
|
||||
loading={isApproving}
|
||||
disabled={isApproving}>
|
||||
Yes
|
||||
</Button>
|
||||
<Button
|
||||
mode="contained"
|
||||
onPress={rejectRequestHandler}
|
||||
buttonColor="#B82B0D">
|
||||
No
|
||||
</Button>
|
||||
</View>
|
||||
</>
|
||||
)}
|
||||
</>
|
||||
);
|
||||
};
|
||||
|
||||
export default SignMessageEmbed;
|
@ -40,6 +40,7 @@ export type StackParamsList = {
|
||||
};
|
||||
"wallet-embed": undefined;
|
||||
"auto-sign-in": undefined;
|
||||
"sign-request-embed": undefined;
|
||||
};
|
||||
|
||||
export type Account = {
|
||||
|
@ -18,6 +18,18 @@ export const DEFAULT_NETWORKS: NetworksFormData[] = [
|
||||
gasPrice: '0.001',
|
||||
isDefault: true,
|
||||
},
|
||||
{
|
||||
chainId: 'zenith-testnet',
|
||||
networkName: 'zenithd testnet',
|
||||
namespace: COSMOS,
|
||||
rpcUrl: 'https://zenith-node-rpc.com',
|
||||
blockExplorerUrl: '',
|
||||
nativeDenom: 'znt',
|
||||
addressPrefix: 'zenith',
|
||||
coinType: '118',
|
||||
gasPrice: '0.01',
|
||||
isDefault: true,
|
||||
},
|
||||
{
|
||||
chainId: 'laconic_9000-1',
|
||||
networkName: 'laconicd',
|
||||
|
@ -10,7 +10,7 @@ services:
|
||||
CERC_DEFAULT_GAS_PRICE: ${CERC_DEFAULT_GAS_PRICE:-0.025}
|
||||
CERC_GAS_ADJUSTMENT: ${CERC_GAS_ADJUSTMENT:-2}
|
||||
CERC_LACONICD_RPC_URL: ${CERC_LACONICD_RPC_URL:-https://laconicd.laconic.com}
|
||||
CERC_DEPLOY_APP_URL: ${CERC_DEPLOY_APP_URL}
|
||||
CERC_ALLOWED_URLS: ${CERC_ALLOWED_URLS}
|
||||
command: ["bash", "/scripts/run.sh"]
|
||||
volumes:
|
||||
- ../config/app/run.sh:/scripts/run.sh
|
||||
|
@ -10,14 +10,14 @@ echo "WALLET_CONNECT_ID: ${WALLET_CONNECT_ID}"
|
||||
echo "CERC_DEFAULT_GAS_PRICE: ${CERC_DEFAULT_GAS_PRICE}"
|
||||
echo "CERC_GAS_ADJUSTMENT: ${CERC_GAS_ADJUSTMENT}"
|
||||
echo "CERC_LACONICD_RPC_URL: ${CERC_LACONICD_RPC_URL}"
|
||||
echo "CERC_DEPLOY_APP_URL: ${CERC_DEPLOY_APP_URL}"
|
||||
echo "CERC_ALLOWED_URLS: ${CERC_ALLOWED_URLS}"
|
||||
|
||||
# Build with required env
|
||||
REACT_APP_WALLET_CONNECT_PROJECT_ID=$WALLET_CONNECT_ID \
|
||||
REACT_APP_DEFAULT_GAS_PRICE=$CERC_DEFAULT_GAS_PRICE \
|
||||
REACT_APP_GAS_ADJUSTMENT=$CERC_GAS_ADJUSTMENT \
|
||||
REACT_APP_LACONICD_RPC_URL=$CERC_LACONICD_RPC_URL \
|
||||
REACT_APP_DEPLOY_APP_URL=$CERC_DEPLOY_APP_URL \
|
||||
REACT_APP_ALLOWED_URLS=$CERC_ALLOWED_URLS \
|
||||
yarn build
|
||||
|
||||
# Define the directory and file path
|
||||
|
@ -49,6 +49,9 @@ Instructions for running the `laconic-wallet-web` using [laconic-so](https://git
|
||||
# WalletConnect project ID, same should be used in the laconic-wallet
|
||||
WALLET_CONNECT_ID=
|
||||
|
||||
# Allowed urls is a comma separated list of allowed urls
|
||||
CERC_ALLOWED_URLS=
|
||||
|
||||
# Optional
|
||||
|
||||
# WalletConnect code for hostname verification
|
||||
@ -63,10 +66,6 @@ Instructions for running the `laconic-wallet-web` using [laconic-so](https://git
|
||||
|
||||
# RPC endpoint of laconicd node (default: https://laconicd.laconic.com)
|
||||
CERC_LACONICD_RPC_URL=
|
||||
|
||||
# Deploy app URL used for checking origin of the messages for auto-sign-in route
|
||||
# Deploy app repo: https://git.vdb.to/cerc-io/snowballtools-base
|
||||
CERC_DEPLOY_APP_URL=
|
||||
```
|
||||
|
||||
## Start the deployment
|
||||
|
Loading…
Reference in New Issue
Block a user