import { Alert, Button, View } from "react-native";
import { Buffer } from "buffer";
import { useTurnkey } from "@turnkey/react-native-wallet-kit";
export function SignRawPayloadButton() {
const { httpClient, wallets } = useTurnkey();
const doSignRawPayload = async () => {
try {
const signWith = wallets?.[0]?.accounts?.[0]?.address;
if (!signWith) {
Alert.alert("No account", "Create a wallet and account first");
return;
}
const message = "Hello, Turnkey!";
const payload = Buffer.from(message, "utf8").toString("hex");
const response = await httpClient.signRawPayload({
signWith,
payload,
encoding: "PAYLOAD_ENCODING_HEXADECIMAL",
hashFunction: "HASH_FUNCTION_NOT_APPLICABLE",
});
const { r, s, v } = response;
console.log("Message signed:", { r, s, v });
} catch (error) {
console.error("Error signing message:", error);
Alert.alert("Error", "Failed to sign message");
}
};
return (
<View style={{ padding: 16 }}>
<Button title="Sign Message" onPress={doSignRawPayload} />
</View>
);
}