Skip to content

Commit

Permalink
Integrate Yttrium methods in WalletKit
Browse files Browse the repository at this point in the history
  • Loading branch information
jakubuid committed Oct 31, 2024
1 parent a4f9ed2 commit 14d07ea
Show file tree
Hide file tree
Showing 11 changed files with 66 additions and 39 deletions.
2 changes: 1 addition & 1 deletion buildSrc/src/main/kotlin/Versions.kt
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ const val NOTIFY_VERSION = "1.0.1"
const val WALLETKIT_VERSION = "1.0.1"
const val APPKIT_VERSION = "1.0.1"
const val MODAL_CORE_VERSION = "1.0.1"
const val YTTRIUM_VERSION = "0.2.5"
const val YTTRIUM_VERSION = "0.2.6"

//Artifact ids
const val ANDROID_BOM = "android-bom"
Expand Down
12 changes: 1 addition & 11 deletions product/walletkit/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -69,29 +69,20 @@ tasks.register("downloadYttriumArtifacts") {
val tagName = YTTRIUM_VERSION
val downloadUrl = "https://github.com/reown-com/yttrium/releases/download/$tagName/kotlin-artifacts.zip"
val outputFile = file("${file(layout.buildDirectory)}/kotlin-artifacts.zip")


outputFile.parentFile.mkdirs()

println("kobe: downloading $downloadUrl to: $outputFile")

try {
// Download the kotlin-artifacts.zip from GitHub Releases
URL(downloadUrl).openStream().use { input ->
println("kobe: input: $input")
outputFile.outputStream().use { output ->
println("kobe: output: $output")
input.copyTo(output)
}
}
} catch (e: Exception) {
println("kobe: Failed to download $downloadUrl error: $e")
println("Failed to download $downloadUrl error: $e")
throw e
}



println("kobe: extracting from $outputFile: to: ${file(layout.buildDirectory)}")
// Extract the kotlin-artifacts.zip to the build directory
copy {
from(zipTree(outputFile))
Expand All @@ -101,7 +92,6 @@ tasks.register("downloadYttriumArtifacts") {
// Delete the zip file after extraction
if (outputFile.exists()) {
outputFile.delete()
println("Deleted $outputFile")
} else {
println("File $outputFile does not exist")
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ package com.reown.walletkit.client

import com.reown.android.internal.common.signing.cacao.CacaoType
import com.reown.sign.client.Sign
import uniffi.uniffi_yttrium.OwnerSignature
import uniffi.uniffi_yttrium.PreparedSendTransaction
import uniffi.uniffi_yttrium.Transaction

@JvmSynthetic
internal fun Map<String, Wallet.Model.Namespace.Session>.toSign(): Map<String, Sign.Model.Namespace.Session> =
Expand Down Expand Up @@ -277,6 +280,15 @@ internal fun Sign.Model.Cacao.toWallet(): Wallet.Model.Cacao = with(this) {

@JvmSynthetic
internal fun Sign.Model.ConnectionState.Reason.toWallet(): Wallet.Model.ConnectionState.Reason = when (this) {
is Sign.Model.ConnectionState.Reason.ConnectionClosed -> Wallet.Model.ConnectionState.Reason.ConnectionClosed(this.message)
is Sign.Model.ConnectionState.Reason.ConnectionFailed -> Wallet.Model.ConnectionState.Reason.ConnectionFailed(this.throwable)
}
is Sign.Model.ConnectionState.Reason.ConnectionClosed -> Wallet.Model.ConnectionState.Reason.ConnectionClosed(this.message)
is Sign.Model.ConnectionState.Reason.ConnectionFailed -> Wallet.Model.ConnectionState.Reason.ConnectionFailed(this.throwable)
}

@JvmSynthetic
internal fun PreparedSendTransaction.toWallet(): Wallet.Model.PreparedSendTransaction = Wallet.Model.PreparedSendTransaction(hash, doSendTransactionParams)

@JvmSynthetic
internal fun Wallet.Params.Transaction.toYttrium(): Transaction = Transaction(to, data, value)

@JvmSynthetic
internal fun Wallet.Params.OwnerSignature.toYttrium(): OwnerSignature = OwnerSignature(address, signature)
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ object Wallet {
}

sealed class Params {
data class Init(val core: CoreInterface) : Params()
data class Init(val core: CoreInterface, val pimlicoApiKey: String? = null) : Params()

data class Pair(val uri: String) : Params()

Expand Down Expand Up @@ -59,6 +59,12 @@ object Wallet {
}

data class DecryptMessage(val topic: String, val encryptedMessage: String) : Params()

data class Transaction(val to: String, val value: String, val data: String) : Params()

data class Account(val address: String) : Params()

data class OwnerSignature(val address: String, val signature: String) : Params()
}

sealed class Model {
Expand Down Expand Up @@ -309,5 +315,10 @@ object Wallet {
) : Message()
}
}

data class PreparedSendTransaction(
var hash: String,
var doSendTransactionParams: String
) : Model()
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.reown.walletkit.client

import accountClient
import com.reown.android.Core
import com.reown.android.CoreInterface
import com.reown.android.internal.common.scope
Expand Down Expand Up @@ -100,8 +99,10 @@ object WalletKit {
@Throws(IllegalStateException::class)
fun initialize(params: Wallet.Params.Init, onSuccess: () -> Unit = {}, onError: (Wallet.Model.Error) -> Unit) {
coreClient = params.core

//todo: init safeInteractor
if (params.pimlicoApiKey != null) {
println("kobe: SafeInteractor initialized")
safeInteractor = SafeInteractor(params.pimlicoApiKey)
}

SignClient.initialize(Sign.Params.Init(params.core), onSuccess = onSuccess) { error ->
if (error.throwable is SignClientAlreadyInitializedException) {
Expand Down Expand Up @@ -276,20 +277,36 @@ object WalletKit {
}

//Yttrium
fun prepareSendTransactions() {
@Throws(IllegalStateException::class)
fun getSmartAccount(owner: Wallet.Params.Account): String {
check(::safeInteractor.isInitialized) { "Smart Accounts are not enabled" }

val client = safeInteractor.getOrCreate(owner.address)
return runBlocking { client.getAddress() }
}

fun doSendTransaction() {
@Throws(IllegalStateException::class)
fun prepareSendTransactions(transactions: List<Wallet.Params.Transaction>, owner: Wallet.Params.Account): Wallet.Model.PreparedSendTransaction {
check(::safeInteractor.isInitialized) { "Smart Accounts are not enabled" }

val client = safeInteractor.getOrCreate(owner.address)
return runBlocking { client.prepareSendTransactions(transactions.map { it.toYttrium() }).toWallet() }
}

fun getSmartAccount() {
@Throws(IllegalStateException::class)
fun doSendTransactions(owner: Wallet.Params.Account, signatures: List<Wallet.Params.OwnerSignature>, doSendTransactionParams: String): String {
check(::safeInteractor.isInitialized) { "Smart Accounts are not enabled" }

val client = safeInteractor.getOrCreate(owner.address)
return runBlocking { client.doSendTransactions(signatures.map { it.toYttrium() }, doSendTransactionParams) }
}

fun waitForUserOperationReceipt() {
@Throws(IllegalStateException::class)
fun waitForUserOperationReceipt(owner: Wallet.Params.Account, userOperationHash: String): String {
check(::safeInteractor.isInitialized) { "Smart Accounts are not enabled" }

val client = safeInteractor.getOrCreate(owner.address)
return runBlocking { client.waitForUserOperationReceipt(userOperationHash) }
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class SafeInteractor(private val pimlicoApiKey: String) {


private fun createSafeAccount(owner: String): AccountClient {
val (namespace: String, reference: String, _) = owner.split(":")
val (namespace: String, reference: String, address: String) = owner.split(":")
val pimlicoUrl = "https://api.pimlico.io/v2/$reference/rpc?apikey=$pimlicoApiKey"
val endpoints = Endpoints(
rpc = Endpoint(baseUrl = "https://rpc.walletconnect.com/v1?chainId=$namespace:$reference&projectId=$projectId", apiKey = ""),
Expand All @@ -34,13 +34,14 @@ class SafeInteractor(private val pimlicoApiKey: String) {
val config = Config(endpoints)

val accountConfig = AccountClientConfig(
ownerAddress = owner,
ownerAddress = address,
chainId = reference.toULong(),
config = config,
privateKey = "", //todo: remove
privateKey = "ff89825a799afce0d5deaa079cdde227072ec3f62973951683ac8cc033092156", //todo: remove sign service
safe = true,
signerType = "PrivateKey" //todo: remove
signerType = "PrivateKey" //todo: remove sign service
)
println("kobe: init Safe Account")
return AccountClient(accountConfig)
}
}
1 change: 1 addition & 0 deletions sample/wallet/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ android {
useSupportLibrary = true
}
buildConfigField("String", "PROJECT_ID", "\"${System.getenv("WC_CLOUD_PROJECT_ID") ?: ""}\"")
buildConfigField("String", "PIMLICO_API_KEY", "\"${System.getenv("PIMLICO_API_KEY") ?: ""}\"")
buildConfigField("String", "BOM_VERSION", "\"${BOM_VERSION}\"")
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ class WalletKitApplication : Application() {

println("Account: ${EthAccountDelegate.account}")

WalletKit.initialize(Wallet.Params.Init(core = CoreClient),
WalletKit.initialize(Wallet.Params.Init(core = CoreClient, pimlicoApiKey = BuildConfig.PIMLICO_API_KEY),
onSuccess = { println("Web3Wallet initialized") },
onError = { error ->
Firebase.crashlytics.recordException(error.throwable)
Expand All @@ -81,12 +81,8 @@ class WalletKitApplication : Application() {

FirebaseAppDistribution.getInstance().updateIfNewReleaseAvailable()

// //Yttrium Account Client init
// accountClient = AccountClient(accountConfig)
// scope.launch {
// smartAccountAddress = accountClient.getAddress()
// println("kobe: SmartAccountAddress: $smartAccountAddress")
// }
val smartAccountAddress = WalletKit.getSmartAccount(owner = Wallet.Params.Account(EthAccountDelegate.ethAddress))
println("kobe: SmartAccountAddress: $smartAccountAddress")

NotifyClient.initialize(
init = Notify.Params.Init(CoreClient)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.reown.sample.wallet.ui.routes.dialog_routes.session_proposal

import smartAccountAddress
import com.reown.sample.wallet.domain.ACCOUNTS_1_EIP155_ADDRESS
import com.reown.sample.wallet.ui.common.peer.PeerContextUI
import com.reown.sample.wallet.ui.common.peer.PeerUI
import com.reown.walletkit.client.Wallet
Expand Down Expand Up @@ -48,7 +48,7 @@ val walletMetaData = WalletMetaData(
),
events = listOf("chainChanged", "accountsChanged", "connect", "disconnect"),
accounts = listOf(
"eip155:11155111:$smartAccountAddress"
"eip155:11155111:$ACCOUNTS_1_EIP155_ADDRESS"
)
// "eip155:1:$ACCOUNTS_1_EIP155_ADDRESS",
// "eip155:1:$ACCOUNTS_2_EIP155_ADDRESS",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ package com.reown.sample.wallet.ui.routes.dialog_routes.session_proposal
import androidx.lifecycle.ViewModel
import com.google.firebase.crashlytics.ktx.crashlytics
import com.google.firebase.ktx.Firebase
import com.reown.sample.wallet.domain.ACCOUNTS_1_EIP155_ADDRESS
import com.reown.sample.wallet.domain.WCDelegate
import smartAccountAddress
import com.reown.sample.wallet.ui.common.peer.PeerUI
import com.reown.sample.wallet.ui.common.peer.toPeerUI
import com.reown.walletkit.client.Wallet
Expand All @@ -20,7 +20,7 @@ class SessionProposalViewModel : ViewModel() {
Timber.d("Approving session proposal: $proposalPublicKey")
val sessionNamespaces = WalletKit.generateApprovedNamespaces(sessionProposal = proposal, supportedNamespaces = walletMetaData.namespaces)

val capability = "{\"$smartAccountAddress\":{\"0xaa36a7\":{\"atomicBatch\":{\"supported\":true}}}}"
val capability = "{\"eip155:11155111:$ACCOUNTS_1_EIP155_ADDRESS\":{\"0xaa36a7\":{\"atomicBatch\":{\"supported\":true}}}}"

// "[{\"from\":\"$account\",\"to\":\"0x70012948c348CBF00806A3C79E3c5DAdFaAa347B\",\"data\":\"0x\",\"gasLimit\":\"0x5208\",\"gasPrice\":\"0x0649534e00\",\"value\":\"0x01\",\"nonce\":\"0x07\"}]"
val sessionProperties = mapOf(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import com.reown.android.utils.cacao.sign
import com.reown.sample.common.Chains
import com.reown.sample.wallet.domain.EthAccountDelegate
import com.reown.sample.wallet.domain.WCDelegate
import accountClient
import com.reown.sample.wallet.ui.common.peer.PeerUI
import com.reown.sample.wallet.ui.common.peer.toPeerUI
import com.reown.util.hexToBytes
Expand Down

0 comments on commit 14d07ea

Please sign in to comment.