Skip to content

Commit

Permalink
Fetch all keys from wallet instead of transforming bech32 (#158)
Browse files Browse the repository at this point in the history
  • Loading branch information
codingki authored May 10, 2024
2 parents 85a758b + a721d30 commit 67d728e
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 51 deletions.
33 changes: 10 additions & 23 deletions packages/graz/src/actions/account.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { fromBech32, toBech32 } from "@cosmjs/encoding";
import type { OfflineDirectSigner } from "@cosmjs/proto-signing";
import type { ChainInfo, Key, OfflineAminoSigner } from "@keplr-wallet/types";

Expand Down Expand Up @@ -67,17 +66,11 @@ export const connect = async (args?: ConnectArgs): Promise<ConnectResult> => {
const _resAcc = useGrazSessionStore.getState().accounts;
useGrazSessionStore.setState({ status: "connecting" });

const key = await wallet.getKey(chainIds[0]!);
const resultAcccounts: Record<string, Key> = {};
chainIds.forEach((chainId) => {
resultAcccounts[chainId] = {
...key,
bech32Address: toBech32(
chains!.find((x) => x.chainId === chainId)!.bech32Config.bech32PrefixAccAddr,
fromBech32(key.bech32Address).data,
),
};
});
const resultAcccounts = Object.fromEntries(
await Promise.all(
chainIds.map(async (chainId): Promise<[string, Key]> => [chainId, await wallet.getKey(chainId)]),
),
);
useGrazSessionStore.setState((prev) => ({
accounts: { ...(prev.accounts || {}), ...resultAcccounts },
}));
Expand Down Expand Up @@ -105,17 +98,11 @@ export const connect = async (args?: ConnectArgs): Promise<ConnectResult> => {
return { accounts: _resAcc!, walletType: currentWalletType, chains: connectedChains };
}
if (!isWalletConnect(currentWalletType)) {
const key = await wallet.getKey(chainIds[0]!);
const resultAcccounts: Record<string, Key> = {};
chainIds.forEach((chainId) => {
resultAcccounts[chainId] = {
...key,
bech32Address: toBech32(
chains!.find((x) => x.chainId === chainId)!.bech32Config.bech32PrefixAccAddr,
fromBech32(key.bech32Address).data,
),
};
});
const resultAcccounts = Object.fromEntries(
await Promise.all(
chainIds.map(async (chainId): Promise<[string, Key]> => [chainId, await wallet.getKey(chainId)]),
),
);
useGrazSessionStore.setState((prev) => ({
accounts: { ...(prev.accounts || {}), ...resultAcccounts },
}));
Expand Down
37 changes: 20 additions & 17 deletions packages/graz/src/actions/wallet/capsule.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { AminoSignResponse } from "@cosmjs/amino";
import { fromBech32, toBech32 } from "@cosmjs/encoding";
import { fromBech32 } from "@cosmjs/encoding";
import type { DirectSignResponse } from "@cosmjs/proto-signing";
import type { Keplr, Key } from "@keplr-wallet/types";

Expand Down Expand Up @@ -40,22 +40,25 @@ export const getCapsule = (): Wallet => {
await client.enable();
const chainIds = useGrazInternalStore.getState().capsuleState?.chainId;
if (!chainIds) throw new Error("Chain ids are not set");
const key = await client.getAccount(chainIds[0]!);
const resultAcccounts: Record<string, Key> = {};
chainIds.forEach((chainId) => {
resultAcccounts[chainId] = {
address: fromBech32(key.address).data,
bech32Address: toBech32(
chains.find((x) => x.chainId === chainId)!.bech32Config.bech32PrefixAccAddr,
fromBech32(key.address).data,
),
algo: key.algo,
name: key.username || "",
pubKey: key.pubkey,
isKeystone: false,
isNanoLedger: false,
};
});
const resultAcccounts = Object.fromEntries(
await Promise.all(
chainIds.map(async (chainId): Promise<[string, Key]> => {
const account = await client.getAccount(chainId);
return [
chainId,
{
address: fromBech32(account.address).data,
bech32Address: account.address,
algo: account.algo,
name: account.username || "",
pubKey: account.pubkey,
isKeystone: false,
isNanoLedger: false,
},
];
}),
),
);
useGrazSessionStore.setState((prev) => ({
accounts: { ...(prev.accounts || {}), ...resultAcccounts },
}));
Expand Down
14 changes: 3 additions & 11 deletions packages/graz/src/actions/wallet/wallet-connect/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -297,17 +297,9 @@ export const getWalletConnect = (params?: GetWalletConnectParams): Wallet => {
try {
await promiseWithTimeout(
(async () => {
const wcAccount = await getKey(chainId[0]!);
const resultAcccounts: Record<string, Key> = {};
chainId.forEach((x) => {
resultAcccounts[x] = {
...wcAccount,
bech32Address: toBech32(
chains!.find((y) => y.chainId === x)!.bech32Config.bech32PrefixAccAddr,
fromBech32(wcAccount.bech32Address).data,
),
};
});
const resultAcccounts = Object.fromEntries(
await Promise.all(chainId.map(async (c): Promise<[string, Key]> => [c, await getKey(c)])),
);
useGrazSessionStore.setState({
accounts: resultAcccounts,
});
Expand Down

0 comments on commit 67d728e

Please sign in to comment.