Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] Web3Core Signing #6

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ dependencies {
implementation("io.ktor:ktor-client-android:2.3.4")

implementation("io.github.funkatronics:multimult:0.2.0")
implementation("com.solanamobile:web3-solana:0.2.2")
implementation("com.solanamobile:web3-solana:0.3.0-beta2")
implementation("com.solanamobile:rpc-core:0.2.3")
implementation("com.solanamobile:mobile-wallet-adapter-clientlib-ktx:2.0.0")

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,14 @@ import com.example.solanakotlincomposescaffold.usecase.RequestAirdropUseCase
import com.funkatronics.encoders.Base58
import com.solana.publickey.SolanaPublicKey
import com.solana.mobilewalletadapter.clientlib.ActivityResultSender
import com.solana.mobilewalletadapter.clientlib.AdapterOperations
import com.solana.mobilewalletadapter.clientlib.MobileWalletAdapter
import com.solana.mobilewalletadapter.clientlib.TransactionResult
import com.solana.mobilewalletadapter.clientlib.protocol.MobileWalletAdapterClient
import com.solana.mobilewalletadapter.clientlib.successPayload
import com.solana.publickey.PublicKey
import com.solana.signer.Signer
import com.solana.transaction.Transaction
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.MutableStateFlow
Expand Down Expand Up @@ -152,19 +157,52 @@ class MainViewModel @Inject constructor(
}
}

// temporary stuff
val AdapterOperations.signer: Signer
get() = object : Signer {
override val ownerLength = 32
override val signatureLength = 64
override val publicKey: PublicKey
get() = TODO("Not yet implemented")

override suspend fun signPayload(payload: ByteArray): ByteArray {
return runCatching { Transaction.from(payload) }.getOrNull()?.let {
val result = [email protected](arrayOf(payload))
result.signedPayloads.first()
} ?: [email protected](arrayOf(payload), arrayOf(publicKey.bytes)).messages.first().signatures.first()
}
}

suspend fun AdapterOperations.signTransactions(vararg transactions: Transaction): List<Transaction> {
val serializedTxns = runCatching {
transactions.map { it.serialize() }.toTypedArray()
}.getOrElse {
throw IllegalArgumentException("Transactions could not be serialized", it)
}
return signTransactions(serializedTxns).signedPayloads.map {
Transaction.from(it)
}
}
suspend fun AdapterOperations.signAndSendTransactions(vararg transactions: Transaction): MobileWalletAdapterClient.SignAndSendTransactionsResult =
signAndSendTransactions(
runCatching {
transactions.map { it.serialize() }.toTypedArray()
}.getOrElse {
throw IllegalArgumentException("Transactions could not be serialized", it)
}
)

fun signTransaction(sender: ActivityResultSender ) {
viewModelScope.launch {
val result = walletAdapter.transact(sender) { authResult ->
val account = SolanaPublicKey(authResult.accounts.first().publicKey)
val memoTx = MemoTransactionUseCase(rpcUri, account, "Hello Solana!");
signTransactions(arrayOf(
memoTx.serialize(),
));
val memoTx = MemoTransactionUseCase(rpcUri, account, "Hello Solana!")
signTransactions(memoTx)
}

_state.value = when (result) {
is TransactionResult.Success -> {
val signedTxBytes = result.successPayload?.signedPayloads?.first()
val signedTxBytes = result.successPayload?.first()?.serialize()
signedTxBytes?.let {
println("Memo publish signature: " + Base58.encodeToString(signedTxBytes))
}
Expand All @@ -188,8 +226,8 @@ class MainViewModel @Inject constructor(
viewModelScope.launch {
val result = walletAdapter.transact(sender) { authResult ->
val account = SolanaPublicKey(authResult.accounts.first().publicKey)
val memoTx = MemoTransactionUseCase(rpcUri, account, memoText);
signAndSendTransactions(arrayOf(memoTx.serialize()));
val memoTx = MemoTransactionUseCase(rpcUri, account, memoText)
signAndSendTransactions(memoTx)
}

_state.value = when (result) {
Expand Down