Skip to content

Commit

Permalink
Support wallet mode (#227)
Browse files Browse the repository at this point in the history
Co-authored-by: upalinski <ulad@cere.io>
  • Loading branch information
upalinski and upalinski authored Jan 21, 2025
1 parent d0f9c47 commit 8320211
Show file tree
Hide file tree
Showing 14 changed files with 70 additions and 26 deletions.
12 changes: 6 additions & 6 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 packages/communication/src/wallet/channels.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import type { Context, NetworkConfig } from '@cere/embed-wallet';
import type { PermissionRequest, BiconomyOptions } from '@cere-wallet/wallet-engine';

import { createChannel, CreateChannelOptions } from './createChannel';
import { AuthMethod } from '@cere/torus-embed';
import { AuthMethod, WalletMode } from '@cere/torus-embed';

export type UserInfo = {
email: string;
Expand Down Expand Up @@ -44,6 +44,7 @@ export type InitChannelIn = {
network: NetworkInterface;
biconomy?: BiconomyOptions;
authMethod?: AuthMethod;
mode?: WalletMode;
};
};

Expand Down
2 changes: 1 addition & 1 deletion packages/embed-wallet-inject/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@cere/embed-wallet-inject",
"version": "0.23.2",
"version": "0.23.3",
"sideEffects": false,
"type": "module",
"types": "./dist/types/index.d.ts",
Expand Down
4 changes: 2 additions & 2 deletions packages/embed-wallet/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@cere/embed-wallet",
"version": "0.23.2",
"version": "0.23.3",
"description": "Cere Wallet SDK to integrate the wallet into a web application.",
"sideEffects": false,
"main": "dist/bundle.umd.js",
Expand All @@ -23,7 +23,7 @@
},
"license": "Apache-2.0",
"dependencies": {
"@cere/torus-embed": "0.2.9",
"@cere/torus-embed": "0.2.10",
"@types/bn.js": "^5.1.1",
"@types/readable-stream": "^2.3.15",
"bn.js": "^5.2.1"
Expand Down
2 changes: 2 additions & 0 deletions packages/embed-wallet/src/EmbedWallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,7 @@ export class EmbedWallet {
context,
biconomy,
authMethod,
mode,
popupMode = 'modal',
connectOptions = {},
appId = this.options.appId,
Expand Down Expand Up @@ -188,6 +189,7 @@ export class EmbedWallet {
check: false,
version: clientVersion,
},
mode,
});

this.proxyProvider.setTarget(this.torus.provider);
Expand Down
3 changes: 2 additions & 1 deletion packages/embed-wallet/src/types.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { AuthMethod, NetworkInterface } from '@cere/torus-embed';
import type { AuthMethod, NetworkInterface, WalletMode } from '@cere/torus-embed';
import BN from 'bn.js';

// App context
Expand Down Expand Up @@ -89,6 +89,7 @@ export type WalletOptions = {
sessionNamespace?: string;
clientVersion?: string;
env?: WalletEnvironment;
mode?: WalletMode;
};

export type BiconomyOptions = {
Expand Down
9 changes: 8 additions & 1 deletion packages/wallet-engine/src/engine/polkadot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,14 @@ export const createPolkadotEngine = ({ getPrivateKey, polkadotRpc }: PolkadotEng

const secretKeyCurve25519 = convertSecretKey(secretKey);

res.result = u8aToHex(nacl.box(new TextEncoder().encode(u8aToHex(dek)), new Uint8Array(24), theirPublicKeyCurve25519, secretKeyCurve25519));
res.result = u8aToHex(
nacl.box(
new TextEncoder().encode(u8aToHex(dek)),
new Uint8Array(24),
theirPublicKeyCurve25519,
secretKeyCurve25519,
),
);
}),

ed25519_getBalance: createAsyncMiddleware(async (req, res) => {
Expand Down
11 changes: 11 additions & 0 deletions packages/wallet-engine/src/engine/provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import type { EthereumEngineOptions } from './ethereum';
import type { PolkadotEngineOptions } from './polkadot';
import type { AccountsEngineOptions } from './accounts';
import type { SolanaEngineOptions } from './solana';
import { WalletMode } from '@cere/torus-embed';

export type ProviderEngineOptions = WalletEngineOptions &
AccountsEngineOptions &
Expand Down Expand Up @@ -53,6 +54,10 @@ class ChainEngine extends Engine {
return createPolkadotEngine(options);
});

if (options.mode == 'light') {
return;
}

this.pushEngine(
import(/* webpackChunkName: "accountsEngine" */ './solana').then(({ createSolanaEngine }) =>
createSolanaEngine(options),
Expand All @@ -74,9 +79,11 @@ class ChainEngine extends Engine {
export class ProviderEngine extends Engine {
readonly provider: Provider;
readonly unsafeProvider;
private readonly mode?: WalletMode;

constructor(options: ProviderEngineOptions) {
super();
this.mode = options.mode;

const unsafeEngine = new Engine();
const walletEngine = createWalletEngine(options);
Expand All @@ -103,6 +110,10 @@ export class ProviderEngine extends Engine {
async updateAccounts() {
const [eth, ed25519] = await this.provider.request({ method: 'wallet_updateAccounts' });

if (this.mode == 'light') {
return;
}

/**
* TODO: Move balance subscriptions to the Wallet SDK to make them lazy started
*/
Expand Down
2 changes: 2 additions & 0 deletions packages/wallet-engine/src/engine/wallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@ import { createScaffoldMiddleware, createAsyncMiddleware } from 'json-rpc-engine

import { Engine } from './engine';
import { Account, ChainConfig } from '../types';
import { WalletMode } from '@cere/torus-embed';

export type WalletEngineOptions = {
getAccounts: () => Account[];
getPrivateKey: () => string | undefined;
chainConfig: ChainConfig;
mode?: WalletMode;
};

export const createWalletEngine = ({ chainConfig, getAccounts, getPrivateKey }: WalletEngineOptions) => {
Expand Down
2 changes: 1 addition & 1 deletion src/stores/CollectiblesStore/CollectionStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export class CollectiblesStore {
reaction(
() => wallet.account?.address,
async (address) => {
if (address) {
if (address && wallet.mode !== 'light') {
await this.updateNfts(address);
} else {
this._nfts = [];
Expand Down
28 changes: 19 additions & 9 deletions src/stores/EmbededWalletStore/EmbeddedWalletStore.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { randomBytes } from 'crypto';
import { providers } from 'ethers';
import { makeAutoObservable, reaction, toJS, when } from 'mobx';
import { makeAutoObservable, reaction, toJS, when, runInAction } from 'mobx';
import { createWalletEngine, WalletEngine, BiconomyOptions } from '@cere-wallet/wallet-engine';
import { createWalletConnection, createRpcConnection, WalletConnection } from '@cere-wallet/communication';

Expand All @@ -20,9 +20,11 @@ import { BICONOMY_API_KEY, CERE_NETWORK_RPC, RPC_POLLING_INTERVAL } from '~/cons
import { ApplicationsStore } from '../ApplicationsStore';
import { SessionStore } from '../SessionStore';
import { PermissionsStore } from '../PermissionsStore';
import { WalletMode } from '@cere/torus-embed';

type InitOptions = {
biconomy?: BiconomyOptions;
mode?: WalletMode;
};

export class EmbeddedWalletStore implements Wallet {
Expand Down Expand Up @@ -151,6 +153,10 @@ export class EmbeddedWalletStore implements Wallet {
return this.accountStore.accounts;
}

get mode() {
return this.options.mode;
}

async init() {
await this.setupWalletConnection();
await this.setupRpcConnection();
Expand All @@ -160,19 +166,23 @@ export class EmbeddedWalletStore implements Wallet {
this.walletConnection = createWalletConnection({
logger: console,

onInit: async ({ chainConfig, context, biconomy, authMethod }) => {
onInit: async ({ chainConfig, context, biconomy, authMethod, mode }) => {
this.networkStore.network = chainConfig;
this.appContextStore.context = context;
if (authMethod) {
this.appContextStore.authMethod = authMethod;
}

/**
* Configure the wallet with the init options
*/
if (biconomy) {
this.options.biconomy = biconomy;
}
runInAction(() => {
/**
* Configure the wallet with the init options
*/
if (biconomy) {
this.options.biconomy = biconomy;
}

this.options.mode = mode || 'default';
});

return true;
},
Expand Down Expand Up @@ -273,10 +283,10 @@ export class EmbeddedWalletStore implements Wallet {
await when(() => !!this.networkStore.network);

this.engine = createWalletEngine({
pollingInterval: RPC_POLLING_INTERVAL,
chainConfig: this.networkStore.network!,
polkadotRpc: CERE_NETWORK_RPC,
biconomy: this.options.biconomy,
mode: this.options.mode,
getPrivateKey: () => this.accountStore.privateKey,
getAccounts: () => toJS(this.accountStore.accounts),
onUpdateAccounts: (keyPairs) => this.accountStore.updateAccounts(keyPairs),
Expand Down
14 changes: 11 additions & 3 deletions src/stores/ExchangeRatesStore/ExchangeRatesStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,20 @@ export class ExchangeRatesStore {
this.interval = DEFAULT_INTERVAL;
when(
() => this.assetStore.commonList.length > 0,
() => this.updateExchangeRates(),
() => {
if (wallet.mode !== 'light') {
this.updateExchangeRates();
}
},
);

when(
() => !!wallet.network?.chainId,
() => this.updateExchangeRates(),
() => !!wallet.network?.chainId && !!wallet.mode,
() => {
if (wallet.mode !== 'light') {
this.updateExchangeRates();
}
},
);
}

Expand Down
2 changes: 1 addition & 1 deletion src/stores/NetworkStore/NetworkStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export class NetworkStore {
makeAutoObservable(this);

autorun(async () => {
if (wallet.isReady()) {
if (wallet.isReady() && wallet.mode !== 'light') {
this.gasPrice = await this.wallet.provider?.getGasPrice()!;
} else {
this.gasPrice = undefined;
Expand Down
2 changes: 2 additions & 0 deletions src/stores/types.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { ChainConfig, Account, WalletEngine } from '@cere-wallet/wallet-engine';
import { ethers } from 'ethers';
import { ReactElement } from 'react';
import { WalletMode } from '@cere/torus-embed';

export type PriceData = {
amount: number;
Expand Down Expand Up @@ -58,6 +59,7 @@ export interface Wallet {
readonly engine?: WalletEngine;
readonly account?: Account;
readonly accounts: Account[];
readonly mode?: WalletMode;

/**
* Provider instance with blockchain requests protected by confirmation modals
Expand Down

0 comments on commit 8320211

Please sign in to comment.