Skip to content

Commit

Permalink
wip: Wagmi convert to Ethers provider and use with aleph-sdk-ts
Browse files Browse the repository at this point in the history
  • Loading branch information
RezaRahemtola committed Aug 30, 2024
1 parent 88c7147 commit 7d97fd0
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 35 deletions.
45 changes: 13 additions & 32 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@
},
"dependencies": {
"@aleph-sdk/client": "^1.0.6",
"@aleph-sdk/ethereum": "^1.0.3",
"@aleph-sdk/ethereum": "file:../aleph-sdk-ts/packages/ethereum/aleph-sdk-ethereum-1.0.3.tgz",
"@aleph-sdk/evm": "file:../aleph-sdk-ts/packages/evm/aleph-sdk-evm-1.0.4.tgz",
"@aleph-sdk/message": "^1.0.7",
"@libertai/libertai-js": "0.0.10-beta",
"@quasar/extras": "^1.16.12",
Expand Down
31 changes: 29 additions & 2 deletions src/utils/aleph-persistent-storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@ import { AuthenticatedAlephHttpClient } from '@aleph-sdk/client';
import { ETHAccount, getAccountFromProvider, importAccountFromPrivateKey } from '@aleph-sdk/ethereum';
import web3 from 'web3';
import { ItemType } from '@aleph-sdk/message';
import { signMessage } from '@wagmi/core';
import { type Config, getClient, signMessage } from '@wagmi/core';
import { config } from 'src/config/wagmi';
import type { Chain, Client, Transport } from 'viem';
import { SignMessageReturnType } from 'viem';
import {
KnowledgeBase,
Expand All @@ -14,6 +15,7 @@ import {
import { decrypt, encrypt, generateIv, generateKey } from 'src/utils/encryption';
import { PrivateKey } from 'eciesjs';
import { decryptKnowledgeBaseIdentifiers, encryptKnowledgeBaseIdentifiers } from 'src/utils/knowledge/encryption';
import { providers } from 'ethers';

// Aleph keys and channels settings
const SECURITY_AGGREGATE_KEY = 'security';
Expand All @@ -23,6 +25,29 @@ const LIBERTAI_SETTINGS_KEY = `${LIBERTAI_CHANNEL}-settings`;
const LIBERTAI_KNOWLEDGE_BASE_IDENTIFIERS_KEY = `${LIBERTAI_CHANNEL}-knowledge-base-identifiers-test-12`;
const LIBERTAI_KNOWLEDGE_BASE_POST_TYPE = `${LIBERTAI_CHANNEL}-knowledge-base-test-12`;

export function clientToProvider(client: Client<Transport, Chain>) {
const { chain, transport } = client;
const network = {
chainId: chain.id,
name: chain.name,
ensAddress: chain.contracts?.ensRegistry?.address,
};
if (transport.type === 'fallback')
return new providers.FallbackProvider(
(transport.transports as ReturnType<Transport>[]).map(
({ value }) => new providers.JsonRpcProvider(value?.url, network),
),
);
return new providers.JsonRpcProvider(transport.url, network);
}

/** Action to convert a viem Public Client to an ethers.js Provider. */
export function getEthersProvider(config: Config, { chainId }: { chainId?: number } = {}) {
const client = getClient(config, { chainId });
if (!client) return;
return clientToProvider(client);
}

export class AlephPersistentStorage {
constructor(
/* eslint-disable-next-line no-unused-vars */
Expand All @@ -47,7 +72,9 @@ export class AlephPersistentStorage {
const encryptionPrivateKey = PrivateKey.fromHex(privateKey);

const subAccount = importAccountFromPrivateKey(privateKey);
const account = await getAccountFromProvider(window.ethereum);
const provider = getEthersProvider(config);

const account = await getAccountFromProvider(provider as unknown as any);
const accountClient = new AuthenticatedAlephHttpClient(account, process.env.ALEPH_API_URL);
const subAccountClient = new AuthenticatedAlephHttpClient(subAccount, process.env.ALEPH_API_URL);

Expand Down

0 comments on commit 7d97fd0

Please sign in to comment.