Skip to content

Commit

Permalink
particle fixes, changeset
Browse files Browse the repository at this point in the history
  • Loading branch information
jordaaash committed Jan 4, 2024
1 parent cf3ccf5 commit f8f6f9f
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 91 deletions.
5 changes: 5 additions & 0 deletions .changeset/popular-pants-cheat.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@solana/wallet-adapter-particle': patch
---

Update Particle adapter
1 change: 1 addition & 0 deletions packages/wallets/particle/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
"@solana/wallet-adapter-base": "workspace:^"
},
"devDependencies": {
"@metamask/eth-sig-util": "^7.0.1",
"@solana/web3.js": "^1.77.3",
"shx": "^0.3.4"
}
Expand Down
68 changes: 19 additions & 49 deletions packages/wallets/particle/src/adapter.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { Config, ParticleNetwork, SolanaWallet } from '@particle-network/solana-wallet';
import type { ParticleNetwork, SolanaWallet } from '@particle-network/solana-wallet';
import type { WalletName } from '@solana/wallet-adapter-base';
import {
BaseMessageSignerWalletAdapter,
Expand All @@ -15,19 +15,10 @@ 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?: NestedConfig;
preferredAuthType?: string;
config?: ConstructorParameters<typeof ParticleNetwork>[0];
login?: Parameters<SolanaWallet['connect']>[0];
}

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

private _particleNetwork: ParticleNetwork | null = null;
private _particle: ParticleNetwork | null = null;

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

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,
projectId: '',
clientKey: '',
appId: '',
...config.config,
chainId: config.config?.chainId ?? 101,
chainName: config.config?.chainName ?? 'solana',
},
login: config.login,
};
}

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

get publicKey() {
Expand All @@ -106,7 +83,6 @@ 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 @@ -115,25 +91,19 @@ export class ParticleAdapter extends BaseMessageSignerWalletAdapter {
throw new WalletLoadError(error?.message, error);
}

let particleNetwork: ParticleNetwork;

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

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

let wallet: SolanaWallet;
try {
wallet = new WalletClass(particleNetwork.auth);
wallet = new WalletClass(particle.auth);
} catch (error: any) {
throw new WalletConfigError(error?.message, error);
}
Expand All @@ -148,8 +118,8 @@ export class ParticleAdapter extends BaseMessageSignerWalletAdapter {
throw new WalletPublicKeyError(error?.message, error);
}

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

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

0 comments on commit f8f6f9f

Please sign in to comment.