feat(with-solana): adds sol_signMessage request example

This commit is contained in:
Ben Kremer 2022-03-15 11:56:40 +01:00
parent f676fd0d05
commit c419b83ee5
2 changed files with 49 additions and 1 deletions

View File

@ -146,6 +146,47 @@ export default function App() {
}
};
const testSignMessage = async (account: string): Promise<IFormattedRpcResponse> => {
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<IFormattedRpcResponse>) =>
@ -168,6 +209,10 @@ export default function App() {
method: SolanaRpcMethod.SOL_SIGN_TRANSACTION,
callback: wrapRpcRequest(testSignTransaction),
},
{
method: SolanaRpcMethod.SOL_SIGN_MESSAGE,
callback: wrapRpcRequest(testSignMessage),
},
];
};

View File

@ -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);