Skip to content

Commit

Permalink
fix: wallet circular dependency fix
Browse files Browse the repository at this point in the history
  • Loading branch information
tabaktoni committed Mar 14, 2024
1 parent 318b006 commit 621ae2d
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 23 deletions.
4 changes: 4 additions & 0 deletions src/channel/rpc_0_6.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,10 @@ export class RpcChannel {
this.requestId = 0;
}

public setChainId(chainId: StarknetChainId) {
this.chainId = chainId;
}

public fetch(method: string, params?: object, id: string | number = 0) {
const rpcRequestBody: JRPC.RequestBody = {
id,
Expand Down
4 changes: 4 additions & 0 deletions src/channel/rpc_0_7.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,10 @@ export class RpcChannel {
this.requestId = 0;
}

public setChainId(chainId: StarknetChainId) {
this.chainId = chainId;
}

public fetch(method: string, params?: object, id: string | number = 0) {
const rpcRequestBody: JRPC.RequestBody = {
id,
Expand Down
3 changes: 1 addition & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
/**
* Main
*/
export * from './wallet';
export * from './account';
export * from './contract';
export * from './provider';
export * from './signer';
export * from './channel';

export * from './account/wallet';

// TODO: decide on final export style
export * from './types';
export * as types from './types';
Expand Down
70 changes: 49 additions & 21 deletions src/account/wallet.ts → src/wallet/default.ts
Original file line number Diff line number Diff line change
@@ -1,35 +1,35 @@
import { StarknetChainId } from '../constants';
import { buildUDCCall } from '../utils/transaction';
import { Account, AccountInterface } from '../account';
import {
AccountChangeEventHandler,
AddDeclareTransactionResult,
AddDeployAccountTransactionResult,
AddInvokeTransactionResult,
AddStarknetChainParameters,
NetworkChangeEventHandler,
Permission,
RpcMessage,
StarknetWindowObject,
WatchAssetParameters,
} from './getst/main';
// eslint-disable-next-line import/no-cycle
} from '../account/getst/main';
import { StarknetChainId } from '../constants';
import { ProviderInterface } from '../provider';
import {
Account,
AllowArray,
ArraySignatureType,
CairoVersion,
Call,
CallData,
CompiledSierra,
DeclareContractPayload,
DeployAccountContractPayload,
MultiDeployContractResponse,
ProviderInterface,
ProviderOptions,
TypedData,
UniversalDeployerContractPayload,
extractContractHashes,
json,
} from '..';
} from '../types';
import { CallData } from '../utils/calldata';
import { extractContractHashes } from '../utils/contract';
import { stringify } from '../utils/json';
import { buildUDCCall } from '../utils/transaction';

// ---- TT Request Handler
type RpcCall = Omit<RpcMessage, 'result'>;
Expand All @@ -39,8 +39,8 @@ type RpcCall = Omit<RpcMessage, 'result'>;
interface StarknetWalletProvider extends StarknetWindowObject {}

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

public walletProvider: StarknetWalletProvider;

Expand All @@ -49,17 +49,34 @@ export class WalletAccount extends Account {
walletProvider: StarknetWalletProvider,
cairoVersion?: CairoVersion
) {
// if (!walletProvider.isConnected) throw Error('StarknetWalletProvider should be connected');
const address = '0x0'; // walletProvider.selectedAddress;
super(providerOrOptions, address, '', cairoVersion);
super(providerOrOptions, '', '', cairoVersion); // At this point unknown address
this.walletProvider = walletProvider;
this.address = address.toLowerCase();

// Event Listeners
this.walletProvider.on('accountsChanged', (data) => {
console.log('data', data);
// this.address = walletProvider.selectedAddress;
// Address change Event Listeners
this.walletProvider.on('accountsChanged', (res) => {
if (!res) return;
this.address = res[0].toLowerCase();
console.log('Setting new address', res[0].toLowerCase());

Check warning on line 59 in src/wallet/default.ts

View workflow job for this annotation

GitHub Actions / Run test on rpc-devnet / Run tests

Unexpected console statement
});

// Network change Event Listeners
this.walletProvider.on('networkChanged', (res) => {
if (!res) return;
console.log('Setting new network', res.toLowerCase());

Check warning on line 65 in src/wallet/default.ts

View workflow job for this annotation

GitHub Actions / Run test on rpc-devnet / Run tests

Unexpected console statement
throw Error('WalletAccount doest support switching chains');
});

// Get and Set Address !!! Post constructor initial it is ''
walletProvider
.request({
type: 'wallet_requestAccounts',
params: {
silentMode: false,
},
})
.then((res) => {
this.address = res[0].toLowerCase();
});
}

/**
Expand Down Expand Up @@ -92,6 +109,17 @@ export class WalletAccount extends Account {
return this.walletProvider.request(rpcCall) as Promise<string[]>;
}

/**
* Request Permission for wallet account
* @returns allowed accounts addresses
*/
public getPermissions() {
const rpcCall: RpcCall = {
type: 'wallet_getPermissions',
};
return this.walletProvider.request(rpcCall) as Promise<Permission[]>;
}

/**
* Request Wallet Network change
* @param chainId StarknetChainId
Expand Down Expand Up @@ -163,7 +191,7 @@ export class WalletAccount extends Account {
const pContract = payload.contract as CompiledSierra;
const cairo1Contract = {
...pContract,
abi: json.stringify(pContract.abi),
abi: stringify(pContract.abi),
};

// Check FIx
Expand Down
1 change: 1 addition & 0 deletions src/wallet/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './default';

0 comments on commit 621ae2d

Please sign in to comment.