forked from cerc-io/laconic-wallet
* Sign message using signDirect method with cosmos accounts * Add explaination for signDirect method * Use existing utility function to convert hex string to uint8array * Handle review changes
80 lines
2.7 KiB
TypeScript
80 lines
2.7 KiB
TypeScript
// Taken from https://medium.com/walletconnect/how-to-build-a-wallet-in-react-native-with-the-web3wallet-sdk-b6f57bf02f9a
|
|
|
|
import { formatJsonRpcError, formatJsonRpcResult } from '@json-rpc-tools/utils';
|
|
import { SignClientTypes } from '@walletconnect/types';
|
|
import { getSdkError } from '@walletconnect/utils';
|
|
|
|
import { EIP155_SIGNING_METHODS } from './EIP155Lib';
|
|
import { signDirectMessage, signEthMessage } from '../sign-message';
|
|
import { Account } from '../../types';
|
|
import { getCosmosAccounts, getMnemonic, getPathKey } from '../utils';
|
|
|
|
export async function approveWalletConnectRequest(
|
|
requestEvent: SignClientTypes.EventArguments['session_request'],
|
|
account: Account,
|
|
network: string,
|
|
message: string,
|
|
) {
|
|
const { params, id } = requestEvent;
|
|
const { request } = params;
|
|
|
|
const path = (await getPathKey(network, account.counterId)).path;
|
|
const mnemonic = await getMnemonic();
|
|
const cosmosAccount = await getCosmosAccounts(mnemonic, path);
|
|
const address = cosmosAccount.data.address;
|
|
|
|
switch (request.method) {
|
|
case EIP155_SIGNING_METHODS.PERSONAL_SIGN:
|
|
const ethSignature = await signEthMessage(message, account.counterId);
|
|
return formatJsonRpcResult(id, ethSignature);
|
|
|
|
case 'cosmos_signDirect':
|
|
// Reference: https://github.com/confio/cosmjs-types/blob/66e52711914fccd2a9d1a03e392d3628fdf499e2/src/cosmos/tx/v1beta1/tx.ts#L51
|
|
// According above doc, in the signDoc interface 'bodyBytes' and 'authInfoBytes' have Uint8Array type
|
|
const bodyBytesArray = Uint8Array.from(
|
|
Buffer.from(request.params.signDoc.bodyBytes, 'hex'),
|
|
);
|
|
const authInfoBytesArray = Uint8Array.from(
|
|
Buffer.from(request.params.signDoc.authInfoBytes, 'hex'),
|
|
);
|
|
|
|
const cosmosDirectSignature = await signDirectMessage(
|
|
network,
|
|
account.counterId,
|
|
{
|
|
...request.params.signDoc,
|
|
bodyBytes: bodyBytesArray,
|
|
authInfoBytes: authInfoBytesArray,
|
|
},
|
|
);
|
|
|
|
return formatJsonRpcResult(id, {
|
|
signature: cosmosDirectSignature,
|
|
});
|
|
|
|
case 'cosmos_signAmino':
|
|
const cosmosAminoSignature = await cosmosAccount.cosmosWallet.signAmino(
|
|
address,
|
|
request.params.signDoc,
|
|
);
|
|
|
|
if (!cosmosAminoSignature) {
|
|
throw new Error('Error signing message');
|
|
}
|
|
|
|
return formatJsonRpcResult(id, {
|
|
signature: cosmosAminoSignature.signature.signature,
|
|
});
|
|
default:
|
|
throw new Error(getSdkError('INVALID_METHOD').message);
|
|
}
|
|
}
|
|
|
|
export function rejectEIP155Request(
|
|
request: SignClientTypes.EventArguments['session_request'],
|
|
) {
|
|
const { id } = request;
|
|
|
|
return formatJsonRpcError(id, getSdkError('USER_REJECTED_METHODS').message);
|
|
}
|