From c419b83ee5c2bf7556e869d3baaed91ab7556666 Mon Sep 17 00:00:00 2001 From: Ben Kremer Date: Tue, 15 Mar 2022 11:56:40 +0100 Subject: [PATCH] feat(with-solana): adds `sol_signMessage` request example --- .../src/App.tsx | 45 +++++++++++++++++++ .../src/contexts/ClientContext.tsx | 5 ++- 2 files changed, 49 insertions(+), 1 deletion(-) diff --git a/dapps/react-dapp-v2-with-solana-web3js/src/App.tsx b/dapps/react-dapp-v2-with-solana-web3js/src/App.tsx index 3eef66d..5ad7c29 100644 --- a/dapps/react-dapp-v2-with-solana-web3js/src/App.tsx +++ b/dapps/react-dapp-v2-with-solana-web3js/src/App.tsx @@ -146,6 +146,47 @@ export default function App() { } }; + const testSignMessage = async (account: string): Promise => { + if (!client || !publicKeys || !session) { + throw new Error("WalletConnect Client not initialized properly."); + } + + const address = account.split(":").pop(); + + if (!address) { + throw new Error(`Could not derive Solana address from CAIP account: ${account}`); + } + + const senderPublicKey = publicKeys[address]; + + // Encode message to `UInt8Array` first via `TextEncoder` so we can pass it to `bs58.encode`. + const message = bs58.encode( + new TextEncoder().encode(`This is an example message to be signed - ${Date.now()}`), + ); + + try { + const result = await client.request({ + topic: session.topic, + request: { + method: SolanaRpcMethod.SOL_SIGN_MESSAGE, + params: { + pubkey: senderPublicKey.toBase58(), + message, + }, + }, + }); + + return { + method: SolanaRpcMethod.SOL_SIGN_MESSAGE, + address, + valid: true, + result: result.signature, + }; + } catch (error: any) { + throw new Error(error); + } + }; + const getSolanaActions = (): AccountAction[] => { const wrapRpcRequest = (rpcRequest: (account: string) => Promise) => @@ -168,6 +209,10 @@ export default function App() { method: SolanaRpcMethod.SOL_SIGN_TRANSACTION, callback: wrapRpcRequest(testSignTransaction), }, + { + method: SolanaRpcMethod.SOL_SIGN_MESSAGE, + callback: wrapRpcRequest(testSignMessage), + }, ]; }; diff --git a/dapps/react-dapp-v2-with-solana-web3js/src/contexts/ClientContext.tsx b/dapps/react-dapp-v2-with-solana-web3js/src/contexts/ClientContext.tsx index 42d60da..b0e9e84 100644 --- a/dapps/react-dapp-v2-with-solana-web3js/src/contexts/ClientContext.tsx +++ b/dapps/react-dapp-v2-with-solana-web3js/src/contexts/ClientContext.tsx @@ -23,6 +23,7 @@ import { AccountBalances, ChainNamespaces, getAllChainNamespaces } from "../help export enum SolanaRpcMethod { SOL_SIGN_TRANSACTION = "sol_signTransaction", + SOL_SIGN_MESSAGE = "sol_signMessage", } interface IContext { client: Client | undefined; @@ -181,7 +182,9 @@ export function ClientContextProvider({ children }: { children: ReactNode | Reac const _session = await client.connect({ permissions: { blockchain: { chains: [caipChainId] }, - jsonrpc: { methods: [SolanaRpcMethod.SOL_SIGN_TRANSACTION] }, + jsonrpc: { + methods: [SolanaRpcMethod.SOL_SIGN_TRANSACTION, SolanaRpcMethod.SOL_SIGN_MESSAGE], + }, }, }); onSessionConnected(_session);