From a01781c66a31e88bd8b8c8bdfadec4c5053f584b Mon Sep 17 00:00:00 2001 From: BlockyJ Date: Sat, 2 Nov 2024 03:39:19 +0100 Subject: [PATCH] feat: add account deploy logic (#120) * add account deploy logic * updates to deploy contract logic --- .../example/walletapp/utils/StarknetClient.kt | 31 ++++++++++++++++--- 1 file changed, 27 insertions(+), 4 deletions(-) diff --git a/wallet_app/app/src/main/java/com/example/walletapp/utils/StarknetClient.kt b/wallet_app/app/src/main/java/com/example/walletapp/utils/StarknetClient.kt index ff3401c6..13a93447 100644 --- a/wallet_app/app/src/main/java/com/example/walletapp/utils/StarknetClient.kt +++ b/wallet_app/app/src/main/java/com/example/walletapp/utils/StarknetClient.kt @@ -2,6 +2,7 @@ package com.example.walletapp.utils import com.example.walletapp.BuildConfig import com.swmansion.starknet.account.StandardAccount +import com.swmansion.starknet.data.ContractAddressCalculator import com.swmansion.starknet.data.types.Call import com.swmansion.starknet.data.types.Felt import com.swmansion.starknet.data.types.Uint256 @@ -9,6 +10,7 @@ import com.swmansion.starknet.provider.rpc.JsonRpcProvider import com.swmansion.starknet.signer.StarkCurveSigner import com.swmansion.starknet.account.Account import kotlinx.coroutines.future.await +import java.security.SecureRandom import java.math.BigDecimal import android.util.Log @@ -24,6 +26,7 @@ import java.math.BigInteger import java.security.GeneralSecurityException const val ETH_ERC20_ADDRESS = "0x049d36570d4e46f48e99674bd3fcc84644ddd6b96f7c741b1562b82f9e004dc7" +const val ACCOUNT_CLASS_HASH = "0x04c6d6cf894f8bc96bb9c525e6853e5483177841f7388f74a46cfda6f028c755" class StarknetClient(private val rpcUrl: String) { @@ -35,26 +38,46 @@ class StarknetClient(private val rpcUrl: String) { suspend fun deployAccount() { // Predefined values for account creation - val randomPrivateKey = StandardAccount.generatePrivateKey() keystore.storeData(randomPrivateKey.value.toString()) // save the key generated val data = keystore.retrieveData() // retrieve it to generate public key val privateKey = BigInteger(data).toFelt - val accountAddress = StarknetCurve.getPublicKey(privateKey) + val publicKey = StarknetCurve.getPublicKey(privateKey) val signer = StarkCurveSigner(privateKey) + val chainId = provider.getChainId().sendAsync().await() + + val salt = BigInteger(250, SecureRandom()).toFelt + + val accountContractClassHash = Felt.fromHex(ACCOUNT_CLASS_HASH) + + val address = ContractAddressCalculator.calculateAddressFromHash( + classHash = accountContractClassHash, + calldata = listOf(publicKey), + salt = salt, + ) + val account = StandardAccount( - address = accountAddress, + address = address, signer = signer, provider = provider, chainId = chainId, cairoVersion = CairoVersion.ONE, ) - // TODO(#99): add account deployment logic + // Fund address first + // how to approach this, we need the user to fund the address + val payload = account.signDeployAccountV1( + classHash = accountContractClassHash, + calldata = listOf(publicKey), + salt = salt, + // 10*fee from estimate deploy account fee + maxFee = Felt.fromHex("0x11fcc58c7f7000"), + ) + provider.deployAccount(payload).send() } // TODO(#107): change name to getBalance, support getting balance for any token