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: WalletAccount non-breaking temp solution #1259

Merged
merged 9 commits into from
Nov 18, 2024
58 changes: 45 additions & 13 deletions src/wallet/account.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import type {
Signature,
AccountChangeEventHandler,
AddStarknetChainParameters,
NetworkChangeEventHandler,
Signature,
WatchAssetParameters,
} from 'starknet-types-07';

import { Account, AccountInterface } from '../account';
import { StarknetChainId } from '../constants';
import { ProviderInterface } from '../provider';
import {
AllowArray,
Expand Down Expand Up @@ -34,20 +36,35 @@ import {
watchAsset,
} from './connect';
import { StarknetWalletProvider } from './types';
import { StarknetChainId } from '../constants';

// TODO: Remove non address constructor in next major version
// Represent 'Selected Active' Account inside Connected Wallet
export class WalletAccount extends Account implements AccountInterface {
public address: string = '';

public walletProvider: StarknetWalletProvider;

/**
* @deprecated Use static method WalletAccount.connect or WalletAccount.connectSilent instead. Constructor {@link WalletAccount.(format:2)}.
*/
constructor(
providerOrOptions: ProviderOptions | ProviderInterface,
walletProvider: StarknetWalletProvider,
cairoVersion?: CairoVersion
);
constructor(
providerOrOptions: ProviderOptions | ProviderInterface,
walletProvider: StarknetWalletProvider,
cairoVersion?: CairoVersion,
address?: string
);
constructor(
providerOrOptions: ProviderOptions | ProviderInterface,
walletProvider: StarknetWalletProvider,
cairoVersion?: CairoVersion,
address: string = ''
) {
super(providerOrOptions, '', '', cairoVersion); // At this point unknown address
super(providerOrOptions, address, '', cairoVersion); // At this point unknown address
this.walletProvider = walletProvider;

// Update Address on change
Expand All @@ -64,17 +81,14 @@ export class WalletAccount extends Account implements AccountInterface {
this.channel.setChainId(res as StarknetChainId);
});

// Get and Set Address !!! Post constructor initial empty string
walletProvider
.request({
type: 'wallet_requestAccounts',
params: {
silent_mode: false,
},
})
.then((res) => {
this.address = res[0].toLowerCase();
if (!address.length) {
console.warn(
'@deprecated Use static method WalletAccount.connect or WalletAccount.connectSilent instead. Constructor {@link WalletAccount.(format:2)}.'
);
requestAccounts(this.walletProvider).then(([accountAddress]) => {
this.address = accountAddress.toLowerCase();
});
}
}

/**
Expand Down Expand Up @@ -170,5 +184,23 @@ export class WalletAccount extends Account implements AccountInterface {
return signMessage(this.walletProvider, typedData);
}

static async connect(
provider: ProviderInterface,
walletProvider: StarknetWalletProvider,
cairoVersion?: CairoVersion,
silentMode: boolean = false
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like my implementation better. Separating "connect" and "connectSilent" is a better approach imo, then passing a boolean here

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK will add connectSilent as alias on fixed connect, I think these best compromise on this.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

) {
const [accountAddress] = await requestAccounts(walletProvider, silentMode);
return new WalletAccount(provider, walletProvider, cairoVersion, accountAddress);
}

static async connectSilent(
provider: ProviderInterface,
walletProvider: StarknetWalletProvider,
cairoVersion?: CairoVersion
) {
return WalletAccount.connect(provider, walletProvider, cairoVersion, true);
}

// TODO: MISSING ESTIMATES
}