Skip to content

Commit

Permalink
fix(wallet): Switch chain when required for base
Browse files Browse the repository at this point in the history
  • Loading branch information
RezaRahemtola committed Oct 29, 2024
1 parent 853b866 commit 932e5c2
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 3 deletions.
15 changes: 14 additions & 1 deletion src/utils/aleph-persistent-storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,13 @@ import {
import { AuthenticatedAlephHttpClient } from '@aleph-sdk/client';
import { ItemType } from '@aleph-sdk/message';
import { getAccountFromProvider as getSolAccountFromProvider, SOLAccount } from '@aleph-sdk/solana';
import { type Config, getConnectorClient, signMessage as signWagmiMessage } from '@wagmi/core';
import {
type Config,
getConnections,
getConnectorClient,
signMessage as signWagmiMessage,
switchChain,
} from '@wagmi/core';
import { base } from '@wagmi/vue/chains';
import { PrivateKey } from 'eciesjs';
import { providers } from 'ethers';
Expand Down Expand Up @@ -155,6 +161,13 @@ export class AlephPersistentStorage {

private static async getAlephAccount(chain: AccountChain): Promise<BaseAccount | SOLAccount> {
if (chain === 'base') {
const connections = getConnections(config);
if (connections.length === 0) {
throw Error('No wallet connection active');
}
if (connections[0].chainId !== env.WAGMI_BASE_ID) {
await switchChain(config, { chainId: env.WAGMI_BASE_ID });
}
const provider = await getEthersProvider(config);
return getBaseAccountFromProvider(provider);
} else {
Expand Down
4 changes: 2 additions & 2 deletions src/utils/encryption.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,15 @@ export const decrypt = (encryptedText: string, key: Buffer, iv: Buffer): string

export const encryptFile = async (file: File, key: Buffer, iv: Buffer): Promise<Buffer> => {
const arrayBuffer = await file.arrayBuffer();
const bufferString = Buffer.from(arrayBuffer).toString('hex');
const bufferString = Buffer.from(arrayBuffer).toString(BUFFER_ENCODING);
const encryptedString = encrypt(bufferString, key, iv);
return Buffer.from(encryptedString);
};

export const decryptFile = (arrayBuffer: ArrayBuffer, key: Buffer, iv: Buffer): Buffer => {
const fileBufferString = Buffer.from(arrayBuffer).toString();
const decryptedString = decrypt(fileBufferString, key, iv);
return Buffer.from(decryptedString, 'hex');
return Buffer.from(decryptedString, BUFFER_ENCODING);
};

export const generateKey = () => Buffer.from(randomBytes(16).toString('hex'));
Expand Down

0 comments on commit 932e5c2

Please sign in to comment.