Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: revamp particleAdapter #854

Merged
merged 10 commits into from
Jan 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/wallets/particle/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
"@solana/web3.js": "^1.77.3"
},
"dependencies": {
"@particle-network/solana-wallet": "^0.5.6",
"@particle-network/solana-wallet": "1.2.1",
"@solana/wallet-adapter-base": "workspace:^"
},
"devDependencies": {
Expand Down
66 changes: 59 additions & 7 deletions packages/wallets/particle/src/adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import {
BaseMessageSignerWalletAdapter,
WalletAccountError,
WalletConfigError,
WalletConnectionError,
WalletDisconnectionError,
WalletLoadError,
WalletNotConnectedError,
Expand All @@ -16,9 +15,19 @@ import {
} from '@solana/wallet-adapter-base';
import type { Transaction } from '@solana/web3.js';
import { PublicKey } from '@solana/web3.js';
import { LoginOptions } from '@particle-network/auth';

interface NestedConfig {
chainId?: number;
chainName?: string;
projectId: string;
clientKey: string;
appId: string;
}

export interface ParticleAdapterConfig {
config?: Config;
config?: NestedConfig;
preferredAuthType?: string;
}

export const ParticleName = 'Particle' as WalletName<'Particle'>;
Expand All @@ -37,12 +46,43 @@ export class ParticleAdapter extends BaseMessageSignerWalletAdapter {
private _readyState: WalletReadyState =
typeof window === 'undefined' ? WalletReadyState.Unsupported : WalletReadyState.Loadable;

private _particleNetwork: ParticleNetwork | null = null;

constructor(config: ParticleAdapterConfig = {}) {
super();
this._connecting = false;
this._publicKey = null;
this._wallet = null;
this._config = config;

const defaultNestedConfig: NestedConfig = {
projectId: '',
clientKey: '',
appId: '',
};

const nestedConfig: NestedConfig = {
...defaultNestedConfig,
...config.config,
};

const chainId = nestedConfig.chainId !== undefined ? nestedConfig.chainId : 101;
const chainName = nestedConfig.chainName !== undefined ? nestedConfig.chainName : 'solana';

this._config = {
...config,
config: {
...nestedConfig,
chainId,
chainName,
projectId: nestedConfig.projectId,
clientKey: nestedConfig.clientKey,
appId: nestedConfig.appId,
},
};
}

public get particle(): ParticleNetwork | null {
return this._particleNetwork;
}

get publicKey() {
Expand All @@ -66,6 +106,7 @@ export class ParticleAdapter extends BaseMessageSignerWalletAdapter {

let ParticleClass: typeof ParticleNetwork;
let WalletClass: typeof SolanaWallet;

try {
({ ParticleNetwork: ParticleClass, SolanaWallet: WalletClass } = await import(
'@particle-network/solana-wallet'
Expand All @@ -74,17 +115,27 @@ export class ParticleAdapter extends BaseMessageSignerWalletAdapter {
throw new WalletLoadError(error?.message, error);
}

let wallet: SolanaWallet;
let particleNetwork: ParticleNetwork;

const authOptions: LoginOptions = {};
if (this._config.preferredAuthType) {
authOptions.preferredAuthType = this._config.preferredAuthType as LoginOptions['preferredAuthType'];
}

try {
wallet = new WalletClass(new ParticleClass(this._config?.config).auth);
particleNetwork = new ParticleClass(this._config?.config as Config);
if (!particleNetwork.auth.isLogin()) {
await particleNetwork.auth.login(authOptions);
}
} catch (error: any) {
throw new WalletConfigError(error?.message, error);
}

let wallet: SolanaWallet;
try {
await wallet.connect();
wallet = new WalletClass(particleNetwork.auth);
} catch (error: any) {
throw new WalletConnectionError(error?.message, error);
throw new WalletConfigError(error?.message, error);
}

const account = wallet.publicKey;
Expand All @@ -98,6 +149,7 @@ export class ParticleAdapter extends BaseMessageSignerWalletAdapter {
}

this._wallet = wallet;
this._particleNetwork = particleNetwork;
this._publicKey = publicKey;

this.emit('connect', publicKey);
Expand Down
Loading