Skip to content

Commit

Permalink
feat: improve WalletAccount class instantiation
Browse files Browse the repository at this point in the history
  • Loading branch information
dhruvkelawala committed Oct 10, 2024
1 parent f546d0e commit 541615f
Showing 1 changed file with 32 additions and 15 deletions.
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
}

0 comments on commit 541615f

Please sign in to comment.