Skip to content

Commit

Permalink
fix: walletconnect getAccount and getKey (#177)
Browse files Browse the repository at this point in the history
  • Loading branch information
codingki authored Nov 26, 2024
2 parents b98ca08 + fa9d64a commit 6209154
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 31 deletions.
2 changes: 1 addition & 1 deletion example/starter/src/ui/layout/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ export const Layout = ({ children }: { children: ReactNode }) => {
0 0 40px #b7fcc2, 0 0 50px #b7fcc2`,
animation: `${glowAnimation} 3s infinite linear alternate`,
}}
w="container.md"
w={["full", "full", "container.md"]}
>
<HStack justifyContent="space-between">
{isConnected ? (
Expand Down
2 changes: 1 addition & 1 deletion packages/graz/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "graz",
"description": "React hooks for Cosmos",
"version": "0.1.28",
"version": "0.1.29",
"author": "Griko Nibras <[email protected]>",
"repository": "https://github.com/graz-sh/graz.git",
"homepage": "https://github.com/graz-sh/graz",
Expand Down
2 changes: 2 additions & 0 deletions packages/graz/src/actions/account.ts
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,8 @@ export const reconnect = async (args?: ReconnectArgs) => {
try {
const isWalletReady = checkWallet(_reconnectConnector || undefined);
if (recentChains && isWalletReady && _reconnectConnector) {
const isWC = isWalletConnect(_reconnectConnector);
if (isWC) return;
const key = await connect({
chainId: recentChains,
walletType: _reconnectConnector,
Expand Down
50 changes: 21 additions & 29 deletions packages/graz/src/actions/wallet/wallet-connect/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -305,24 +305,12 @@ export const getWalletConnect = (params?: GetWalletConnectParams): Wallet => {
};

const getAccount = async (chainId: string): Promise<AccountData> => {
const { wcSignClients } = useGrazSessionStore.getState();
const wcSignClient = wcSignClients.get(walletType);
if (!wcSignClient) throw new Error("walletConnect.signClient is not defined");
const topic = getSession([chainId])?.topic;
if (!topic) throw new Error("No wallet connect session");
const result: { address: string; algo: string; pubkey: string }[] = await wcSignClient.request({
topic,
chainId: `cosmos:${chainId}`,
request: {
method: "cosmos_getAccounts",
params: {},
},
});
if (!result[0]) throw new Error("No wallet connect account");
const key = await getKey(chainId);

return {
address: result[0].address,
algo: result[0].algo as Algo,
pubkey: new Uint8Array(Buffer.from(result[0].pubkey, encoding)),
address: key.bech32Address,
algo: key.algo as Algo,
pubkey: key.pubKey,
};
};

Expand All @@ -339,7 +327,10 @@ export const getWalletConnect = (params?: GetWalletConnectParams): Wallet => {
const key = keys.find((x) => x.chainId === chainId);
if (!key) throw new Error(`No wallet connect key for chainId ${chainId}`);

return key;
return {
...key,
pubKey: Buffer.from(String(key.pubKey), encoding) as unknown as Uint8Array,
};
};

const wcSignDirect = async (...args: SignDirectParams) => {
Expand All @@ -354,24 +345,23 @@ export const getWalletConnect = (params?: GetWalletConnectParams): Wallet => {

if (!signDoc.bodyBytes) throw new Error("No bodyBytes");
if (!signDoc.authInfoBytes) throw new Error("No authInfoBytes");
const req = {
redirectToApp();
const result: WalletConnectSignDirectResponse = await wcSignClient.request({
topic,
chainId: `cosmos:${chainId}`,
request: {
method: "cosmos_signDirect",
params: {
signerAddress: signer,
signDoc: {
...signDoc,
bodyBytes: Buffer.from(signDoc.bodyBytes).toString(encoding),
authInfoBytes: Buffer.from(signDoc.authInfoBytes).toString(encoding),
chainId: signDoc.chainId,
accountNumber: signDoc.accountNumber?.toString(),
bodyBytes: signDoc.bodyBytes ? Buffer.from(signDoc.bodyBytes).toString(encoding) : null,
authInfoBytes: signDoc.authInfoBytes ? Buffer.from(signDoc.authInfoBytes).toString(encoding) : null,
},
},
},
};
redirectToApp();
const result: WalletConnectSignDirectResponse = await wcSignClient.request(req);
});
return result;
};

Expand All @@ -380,10 +370,12 @@ export const getWalletConnect = (params?: GetWalletConnectParams): Wallet => {
const { signature, signed } = await wcSignDirect(chainId, signer, signDoc);
return {
signed: {
chainId: signed.chainId,
accountNumber: Long.fromString(signed.accountNumber, false),
authInfoBytes: new Uint8Array(Buffer.from(signed.authInfoBytes, encoding)),
bodyBytes: new Uint8Array(Buffer.from(signed.bodyBytes, encoding)),
chainId: signed.chainId ?? "",
accountNumber: signed.accountNumber ? Long.fromString(signed.accountNumber) : new Long(0),
authInfoBytes: signed.authInfoBytes
? new Uint8Array(Buffer.from(signed.authInfoBytes, encoding))
: new Uint8Array([]),
bodyBytes: signed.bodyBytes ? new Uint8Array(Buffer.from(signed.bodyBytes, encoding)) : new Uint8Array([]),
},
signature,
};
Expand Down

0 comments on commit 6209154

Please sign in to comment.