Merge branch 'feat/solana-signMessage' into main

This commit is contained in:
Ben Kremer 2022-03-15 14:30:20 +01:00
commit a99f7d82c8
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 getSolanaActions = (): AccountAction[] => {
const wrapRpcRequest = const wrapRpcRequest =
(rpcRequest: (account: string) => Promise<IFormattedRpcResponse>) => (rpcRequest: (account: string) => Promise<IFormattedRpcResponse>) =>
@ -168,6 +209,10 @@ export default function App() {
method: SolanaRpcMethod.SOL_SIGN_TRANSACTION, method: SolanaRpcMethod.SOL_SIGN_TRANSACTION,
callback: wrapRpcRequest(testSignTransaction), 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 { export enum SolanaRpcMethod {
SOL_SIGN_TRANSACTION = "sol_signTransaction", SOL_SIGN_TRANSACTION = "sol_signTransaction",
SOL_SIGN_MESSAGE = "sol_signMessage",
} }
interface IContext { interface IContext {
client: Client | undefined; client: Client | undefined;
@ -181,7 +182,9 @@ export function ClientContextProvider({ children }: { children: ReactNode | Reac
const _session = await client.connect({ const _session = await client.connect({
permissions: { permissions: {
blockchain: { chains: [caipChainId] }, blockchain: { chains: [caipChainId] },
jsonrpc: { methods: [SolanaRpcMethod.SOL_SIGN_TRANSACTION] }, jsonrpc: {
methods: [SolanaRpcMethod.SOL_SIGN_TRANSACTION, SolanaRpcMethod.SOL_SIGN_MESSAGE],
},
}, },
}); });
onSessionConnected(_session); onSessionConnected(_session);