|
1 |
| -import { RpcMessage } from 'get-starknet-core'; |
| 1 | +import { |
| 2 | + AccountChangeEventHandler, |
| 3 | + AddDeclareTransactionResult, |
| 4 | + AddDeployAccountTransactionResult, |
| 5 | + AddInvokeTransactionResult, |
| 6 | + AddStarknetChainParameters, |
| 7 | + ConnectedStarknetWindowObject, |
| 8 | + NetworkChangeEventHandler, |
| 9 | + RpcMessage, |
| 10 | + WatchAssetParameters, |
| 11 | +} from 'get-starknet-core'; |
2 | 12 |
|
3 |
| -import { AllowArray, Call } from '..'; |
| 13 | +import { StarknetChainId } from '../constants'; |
| 14 | +// eslint-disable-next-line import/no-cycle |
| 15 | +import { |
| 16 | + AllowArray, |
| 17 | + ArraySignatureType, |
| 18 | + Call, |
| 19 | + CallData, |
| 20 | + CompiledSierra, |
| 21 | + DeclareContractPayload, |
| 22 | + DeployAccountContractPayload, |
| 23 | + MultiDeployContractResponse, |
| 24 | + TypedData, |
| 25 | + UniversalDeployerContractPayload, |
| 26 | + extractContractHashes, |
| 27 | + json, |
| 28 | +} from '..'; |
4 | 29 |
|
5 | 30 | // ---- TT Request Handler
|
6 | 31 | type RpcCall = Omit<RpcMessage, 'result'>;
|
7 | 32 |
|
| 33 | +// -- TT Better naming |
| 34 | +// This is provider object expected by WalletAccount to communicate with wallet |
| 35 | +interface StarknetWalletProvider extends ConnectedStarknetWindowObject {} |
| 36 | + |
| 37 | +// Represent 'Selected Active' Account inside Connected Wallet |
8 | 38 | export class WalletAccount /* implements AccountInterface */ {
|
9 |
| - public address?: string; |
| 39 | + public address: string; |
| 40 | + |
| 41 | + public provider: StarknetWalletProvider; |
| 42 | + |
| 43 | + constructor(provider: StarknetWalletProvider) { |
| 44 | + if (!provider.isConnected) throw Error('StarknetWalletProvider should be connected'); |
| 45 | + this.provider = provider; |
| 46 | + this.address = provider.selectedAddress; |
10 | 47 |
|
11 |
| - public starknetWindowObject?: StarknetWindowObject; // Should be ConnectedStarknetWindowObject |
| 48 | + this.provider.on('accountsChanged', () => { |
| 49 | + this.address = provider.selectedAddress; |
| 50 | + }); |
| 51 | + } |
| 52 | + |
| 53 | + /** |
| 54 | + * WALLET EVENTS |
| 55 | + */ |
| 56 | + public onAccountChange(callback: AccountChangeEventHandler) { |
| 57 | + this.provider.on('accountsChanged', callback); |
| 58 | + } |
12 | 59 |
|
13 |
| - public async connect(options?: ConnectOptions) { |
14 |
| - const starknetWindowObject = await connect(options); |
15 |
| - if (!starknetWindowObject) { |
16 |
| - throw Error('StarknetWindowObject is null'); |
17 |
| - } |
18 |
| - if (!starknetWindowObject.isConnected) { |
19 |
| - throw Error('StarknetWindowObject need to be connected to the wallet'); |
20 |
| - } |
| 60 | + public onNetworkChanged(callback: NetworkChangeEventHandler) { |
| 61 | + this.provider.on('networkChanged', callback); |
| 62 | + } |
| 63 | + |
| 64 | + /** |
| 65 | + * WALLET SPECIFIC METHODS |
| 66 | + */ |
| 67 | + |
| 68 | + /** |
| 69 | + * Request Permission for wallet account, return addresses that's allowed by user |
| 70 | + * @param silentMode false: request user interaction allowance. true: return only pre-allowed |
| 71 | + * @returns allowed accounts addresses |
| 72 | + */ |
| 73 | + public requestAccounts(silentMode = false) { |
| 74 | + const rpcCall: RpcCall = { |
| 75 | + type: 'wallet_requestAccounts', |
| 76 | + params: { |
| 77 | + silentMode, |
| 78 | + }, |
| 79 | + }; |
| 80 | + return this.provider.request(rpcCall) as Promise<string[]>; |
| 81 | + } |
| 82 | + |
| 83 | + /** |
| 84 | + * Request Wallet Network change |
| 85 | + * @param chainId StarknetChainId |
| 86 | + * @returns boolean |
| 87 | + */ |
| 88 | + public switchStarknetChain(chainId: StarknetChainId) { |
| 89 | + const rpcCall: RpcCall = { |
| 90 | + type: 'wallet_switchStarknetChain', |
| 91 | + params: { |
| 92 | + chainId, |
| 93 | + }, |
| 94 | + }; |
| 95 | + return this.provider.request(rpcCall) as Promise<boolean>; |
| 96 | + } |
| 97 | + |
| 98 | + /** |
| 99 | + * Request adding ERC20 Token to Wallet List |
| 100 | + * @param asset WatchAssetParameters |
| 101 | + * @returns boolean |
| 102 | + */ |
| 103 | + public watchAsset(asset: WatchAssetParameters) { |
| 104 | + const rpcCall: RpcCall = { |
| 105 | + type: 'wallet_watchAsset', |
| 106 | + params: asset, |
| 107 | + }; |
| 108 | + return this.provider.request(rpcCall) as Promise<boolean>; |
| 109 | + } |
21 | 110 |
|
22 |
| - this.starknetWindowObject = starknetWindowObject; |
| 111 | + /** |
| 112 | + * Request adding custom Starknet chain |
| 113 | + * @param chain AddStarknetChainParameters |
| 114 | + * @returns boolean |
| 115 | + */ |
| 116 | + public addStarknetChain(chain: AddStarknetChainParameters) { |
| 117 | + // Can this set custom RPC endpoint ? |
| 118 | + const rpcCall: RpcCall = { |
| 119 | + type: 'wallet_addStarknetChain', |
| 120 | + params: chain, |
| 121 | + }; |
| 122 | + return this.provider.request(rpcCall) as Promise<boolean>; |
23 | 123 | }
|
24 | 124 |
|
| 125 | + /** |
| 126 | + * ACCOUNT METHODS |
| 127 | + */ |
| 128 | + |
25 | 129 | public async execute(calls: AllowArray<Call>) {
|
26 |
| - const req2: RpcCall = { |
| 130 | + const rpcCall: RpcCall = { |
27 | 131 | type: 'starknet_addInvokeTransaction',
|
28 | 132 | params: {
|
29 |
| - calls, |
| 133 | + calls: [].concat(calls as any), |
30 | 134 | },
|
31 | 135 | };
|
32 |
| - return this.starknetWindowObject.request(req2); |
| 136 | + return this.provider.request(rpcCall) as Promise<AddInvokeTransactionResult>; |
33 | 137 | }
|
| 138 | + |
| 139 | + public async declare(payload: DeclareContractPayload) { |
| 140 | + const declareContractPayload = extractContractHashes(payload); |
| 141 | + |
| 142 | + // DISCUSS: HOTFIX: Adapt Abi format |
| 143 | + const pContract = payload.contract as CompiledSierra; |
| 144 | + const cairo1Contract = { |
| 145 | + ...pContract, |
| 146 | + abi: json.stringify(pContract.abi), |
| 147 | + }; |
| 148 | + |
| 149 | + const rpcCall: RpcCall = { |
| 150 | + type: 'starknet_addDeclareTransaction', |
| 151 | + params: { |
| 152 | + compiled_class_hash: declareContractPayload.compiledClassHash, |
| 153 | + contract_class: cairo1Contract, |
| 154 | + }, |
| 155 | + }; |
| 156 | + return this.provider.request(rpcCall) as Promise<AddDeclareTransactionResult>; |
| 157 | + } |
| 158 | + |
| 159 | + public async deploy( |
| 160 | + payload: UniversalDeployerContractPayload | UniversalDeployerContractPayload[] |
| 161 | + ): Promise<MultiDeployContractResponse> { |
| 162 | + // TODO: Create UDC PRocedure using invoke() |
| 163 | + return new Promise((e) => false); |
| 164 | + } |
| 165 | + |
| 166 | + public async deployAccount(payload: DeployAccountContractPayload) { |
| 167 | + const rpcCall: RpcCall = { |
| 168 | + type: 'starknet_addDeployAccountTransaction', |
| 169 | + params: { |
| 170 | + contract_address_salt: payload.addressSalt?.toString(), |
| 171 | + constructor_calldata: CallData.compile(payload.constructorCalldata), |
| 172 | + class_hash: payload.classHash, |
| 173 | + }, |
| 174 | + }; |
| 175 | + return this.provider.request(rpcCall) as Promise<AddDeployAccountTransactionResult>; |
| 176 | + } |
| 177 | + |
| 178 | + public async signMessage(typedData: TypedData) { |
| 179 | + const rpcCall: RpcCall = { |
| 180 | + type: 'starknet_signTypedData', |
| 181 | + params: typedData, |
| 182 | + }; |
| 183 | + return this.provider.request(rpcCall) as Promise<ArraySignatureType>; |
| 184 | + } |
| 185 | + |
| 186 | + // MISSING ESTIMATES |
34 | 187 | }
|
0 commit comments