Skip to content

Commit

Permalink
feat: add account deploy logic (#120)
Browse files Browse the repository at this point in the history
* add account deploy logic

* updates to deploy contract logic
  • Loading branch information
JoE11-y authored Nov 2, 2024
1 parent e68ef7d commit a01781c
Showing 1 changed file with 27 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@ 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
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
Expand All @@ -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) {

Expand All @@ -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
Expand Down

0 comments on commit a01781c

Please sign in to comment.