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: improve WalletAccount class instantiation #1245

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
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
47 changes: 32 additions & 15 deletions src/wallet/account.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,16 +38,15 @@ import { StarknetChainId } from '../constants';

// Represent 'Selected Active' Account inside Connected Wallet
export class WalletAccount extends Account implements AccountInterface {
public address: string = '';

public walletProvider: StarknetWalletProvider;

constructor(
providerOrOptions: ProviderOptions | ProviderInterface,
address: string,
walletProvider: StarknetWalletProvider,
cairoVersion?: CairoVersion
) {
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 @@ -63,18 +62,6 @@ export class WalletAccount extends Account implements AccountInterface {
// At the moment channel is stateless but it could change
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();
});
}

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

static async connect(
provider: ProviderInterface,
walletProvider: StarknetWalletProvider,
cairoVersion?: CairoVersion
) {
const [accountAddress] = await walletProvider.request({
type: 'wallet_requestAccounts',
params: {
silent_mode: false,
},
});

return new WalletAccount(provider, accountAddress, walletProvider, cairoVersion);
}

static async connectSilent(
provider: ProviderInterface,
walletProvider: StarknetWalletProvider,
cairoVersion?: CairoVersion
) {
const [accountAddress] = await walletProvider.request({
type: 'wallet_requestAccounts',
params: {
silent_mode: true,
},
});

return new WalletAccount(provider, accountAddress, walletProvider, cairoVersion);
}

// TODO: MISSING ESTIMATES
}