diff --git a/src/channel/rpc_0_6.ts b/src/channel/rpc_0_6.ts index ec2eb24c1..c7754fbb2 100644 --- a/src/channel/rpc_0_6.ts +++ b/src/channel/rpc_0_6.ts @@ -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, diff --git a/src/channel/rpc_0_7.ts b/src/channel/rpc_0_7.ts index 97fb0c811..7e74e0fa5 100644 --- a/src/channel/rpc_0_7.ts +++ b/src/channel/rpc_0_7.ts @@ -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, diff --git a/src/index.ts b/src/index.ts index c5e9dde23..3a2c4576e 100644 --- a/src/index.ts +++ b/src/index.ts @@ -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'; diff --git a/src/account/wallet.ts b/src/wallet/default.ts similarity index 77% rename from src/account/wallet.ts rename to src/wallet/default.ts index 06d83f6b5..20770b476 100644 --- a/src/account/wallet.ts +++ b/src/wallet/default.ts @@ -1,5 +1,4 @@ -import { StarknetChainId } from '../constants'; -import { buildUDCCall } from '../utils/transaction'; +import { Account, AccountInterface } from '../account'; import { AccountChangeEventHandler, AddDeclareTransactionResult, @@ -7,29 +6,30 @@ import { 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; @@ -39,8 +39,8 @@ type RpcCall = Omit; 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; @@ -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()); }); + + // Network change Event Listeners + this.walletProvider.on('networkChanged', (res) => { + if (!res) return; + console.log('Setting new network', res.toLowerCase()); + 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(); + }); } /** @@ -92,6 +109,17 @@ export class WalletAccount extends Account { return this.walletProvider.request(rpcCall) as Promise; } + /** + * Request Permission for wallet account + * @returns allowed accounts addresses + */ + public getPermissions() { + const rpcCall: RpcCall = { + type: 'wallet_getPermissions', + }; + return this.walletProvider.request(rpcCall) as Promise; + } + /** * Request Wallet Network change * @param chainId StarknetChainId @@ -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 diff --git a/src/wallet/index.ts b/src/wallet/index.ts new file mode 100644 index 000000000..acced897e --- /dev/null +++ b/src/wallet/index.ts @@ -0,0 +1 @@ +export * from './default';