- React
- React Native
import { useEthereum } from "@phantom/react-sdk";
function SignTypedData() {
const { ethereum } = useEthereum();
const sign = async () => {
const accounts = await ethereum.getAccounts();
const typedData = {
types: {
EIP712Domain: [
{ name: "name", type: "string" },
{ name: "version", type: "string" },
{ name: "chainId", type: "uint256" },
{ name: "verifyingContract", type: "address" },
],
Order: [
{ name: "from", type: "address" },
{ name: "to", type: "address" },
{ name: "amount", type: "uint256" },
{ name: "nonce", type: "uint256" },
],
},
primaryType: "Order",
domain: {
name: "My App",
version: "1",
chainId: 1,
verifyingContract: "0xCcCCccccCCCCcCCCCCCcCcCccCcCCCcCcccccccC",
},
message: {
from: accounts[0],
to: "0x742D35cC6634C0532925a3b8d4C8Db86fB5C4A7e",
amount: "1000000000000000000",
nonce: 1,
},
};
const signature = await ethereum.signTypedData(typedData, accounts[0]);
console.log("Signature:", signature);
return signature;
};
return <button onClick={sign}>Sign Order</button>;
}
Ethereum methods aren’t available on React Native yet. The React Native SDK connects only through embedded wallets (Google and Apple social login), and EVM support for embedded wallets is coming soon. See Chain support for current availability.
import { useEthereum } from "@phantom/react-native-sdk";
import { View, Button, Alert, StyleSheet } from "react-native";
function SignTypedData() {
const { ethereum } = useEthereum();
const sign = async () => {
try {
const accounts = await ethereum.getAccounts();
const typedData = {
types: {
EIP712Domain: [
{ name: "name", type: "string" },
{ name: "version", type: "string" },
{ name: "chainId", type: "uint256" },
{ name: "verifyingContract", type: "address" },
],
Order: [
{ name: "from", type: "address" },
{ name: "to", type: "address" },
{ name: "amount", type: "uint256" },
{ name: "nonce", type: "uint256" },
],
},
primaryType: "Order",
domain: {
name: "My App",
version: "1",
chainId: 1,
verifyingContract: "0xCcCCccccCCCCcCCCCCCcCcCccCcCCCcCcccccccC",
},
message: {
from: accounts[0],
to: "0x742D35cC6634C0532925a3b8d4C8Db86fB5C4A7e",
amount: "1000000000000000000",
nonce: 1,
},
};
const signature = await ethereum.signTypedData(typedData, accounts[0]);
Alert.alert("Success", `Signature: ${signature.slice(0, 20)}...`);
return signature;
} catch (error) {
Alert.alert("Error", error instanceof Error ? error.message : String(error));
throw error;
}
};
return (
<View style={styles.container}>
<Button title="Sign Order" onPress={sign} />
</View>
);
}
const styles = StyleSheet.create({
container: {
padding: 20,
},
});
Verify with viem
import { verifyTypedData } from "viem";
const isValid = await verifyTypedData({
address: signerAddress,
domain: typedData.domain,
types: typedData.types,
primaryType: typedData.primaryType,
message: typedData.message,
signature,
});