Skip to content

Commit

Permalink
Added context in coincommunity
Browse files Browse the repository at this point in the history
  • Loading branch information
JoepMulder committed Apr 26, 2024
1 parent f12e802 commit 5022c6b
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@ import nl.tudelft.ipv8.peerdiscovery.strategy.RandomWalk
import nl.tudelft.ipv8.sqldelight.Database
import nl.tudelft.ipv8.util.hexToBytes
import nl.tudelft.ipv8.util.toHex
import nl.tudelft.trustchain.foc.community.FOCCommunity
import nl.tudelft.trustchain.app.service.TrustChainService
import nl.tudelft.trustchain.common.DemoCommunity
import nl.tudelft.trustchain.common.MarketCommunity
Expand All @@ -62,6 +61,7 @@ import nl.tudelft.trustchain.common.eurotoken.TransactionRepository
import nl.tudelft.trustchain.currencyii.CoinCommunity
import nl.tudelft.trustchain.eurotoken.community.EuroTokenCommunity
import nl.tudelft.trustchain.eurotoken.db.TrustStore
import nl.tudelft.trustchain.foc.community.FOCCommunity
import nl.tudelft.trustchain.musicdao.core.dao.DaoCommunity
import nl.tudelft.trustchain.musicdao.core.ipv8.MusicCommunity
import nl.tudelft.trustchain.valuetransfer.community.IdentityCommunity
Expand All @@ -87,6 +87,7 @@ class TrustChainApplication : Application() {
}
defaultCryptoProvider = AndroidCryptoProvider

appContext = applicationContext
// Only start IPv8 here if we are on Android 11 or below.
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.S) {
initIPv8()
Expand Down Expand Up @@ -323,9 +324,8 @@ class TrustChainApplication : Application() {
private fun createCoinCommunity(): OverlayConfiguration<CoinCommunity> {
val randomWalk = RandomWalk.Factory()
val nsd = NetworkServiceDiscovery.Factory(getSystemService()!!)

return OverlayConfiguration(
Overlay.Factory(CoinCommunity::class.java),
CoinCommunity.Factory(appContext),
listOf(randomWalk, nsd)
)
}
Expand Down Expand Up @@ -414,5 +414,6 @@ class TrustChainApplication : Application() {
private const val PREF_ID_METADATA_RANGE_18PLUS_KEY = "id_metadata_range_18plus"
private const val BLOCK_TYPE = "demo_block"
private const val FIRST_RUN = "first_run"
lateinit var appContext: Context
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import android.app.Activity
import android.content.Context
import android.util.Log
import nl.tudelft.ipv8.Community
import nl.tudelft.ipv8.Overlay
import nl.tudelft.ipv8.Peer
import nl.tudelft.ipv8.android.IPv8Android
import nl.tudelft.ipv8.attestation.trustchain.TrustChainBlock
Expand All @@ -12,14 +13,40 @@ import nl.tudelft.ipv8.attestation.trustchain.TrustChainTransaction
import nl.tudelft.ipv8.messaging.Packet
import nl.tudelft.ipv8.util.hexToBytes
import nl.tudelft.ipv8.util.toHex
import nl.tudelft.trustchain.currencyii.payload.*
import nl.tudelft.trustchain.currencyii.sharedWallet.*
import nl.tudelft.trustchain.currencyii.coin.WalletManagerAndroid
import nl.tudelft.trustchain.currencyii.payload.AlivePayload
import nl.tudelft.trustchain.currencyii.payload.ElectedPayload
import nl.tudelft.trustchain.currencyii.payload.ElectionPayload
import nl.tudelft.trustchain.currencyii.payload.SignPayload
import nl.tudelft.trustchain.currencyii.sharedWallet.SWJoinBlockTD
import nl.tudelft.trustchain.currencyii.sharedWallet.SWJoinBlockTransactionData
import nl.tudelft.trustchain.currencyii.sharedWallet.SWResponseNegativeSignatureBlockTD
import nl.tudelft.trustchain.currencyii.sharedWallet.SWResponseNegativeSignatureTransactionData
import nl.tudelft.trustchain.currencyii.sharedWallet.SWResponseSignatureBlockTD
import nl.tudelft.trustchain.currencyii.sharedWallet.SWResponseSignatureTransactionData
import nl.tudelft.trustchain.currencyii.sharedWallet.SWSignatureAskBlockTD
import nl.tudelft.trustchain.currencyii.sharedWallet.SWSignatureAskTransactionData
import nl.tudelft.trustchain.currencyii.sharedWallet.SWTransferDoneTransactionData
import nl.tudelft.trustchain.currencyii.sharedWallet.SWTransferFundsAskBlockTD
import nl.tudelft.trustchain.currencyii.sharedWallet.SWTransferFundsAskTransactionData
import nl.tudelft.trustchain.currencyii.util.DAOCreateHelper
import nl.tudelft.trustchain.currencyii.util.DAOJoinHelper
import nl.tudelft.trustchain.currencyii.util.DAOTransferFundsHelper

@Suppress("UNCHECKED_CAST")
open class CoinCommunity constructor(serviceId: String = "02313685c1912a141279f8248fc8db5899c5df5b") : Community() {
open class CoinCommunity constructor(
private val context: Context,
serviceId: String = "02313685c1912a141279f8248fc8db5899c5df5b",
) : Community() {

class Factory(
private val context: Context,
) : Overlay.Factory<CoinCommunity>(CoinCommunity::class.java) {
override fun create(): CoinCommunity {
return CoinCommunity(context)
}
}

override val serviceId = serviceId
private var currentLeader: HashMap<String, Peer?> = HashMap()
private var candidates: HashMap<String, ArrayList<Peer>> = HashMap()
Expand Down Expand Up @@ -275,19 +302,20 @@ open class CoinCommunity constructor(serviceId: String = "02313685c1912a141279f8
peer: Peer,
payload: SignPayload
) {
// TODO: Implement adding to the wallet without a Context
// try {
// joinBitcoinWallet(
// payload.mostRecentSWBlock.transaction,
// payload.proposeBlockData,
// payload.signatures
// )
// // Add new nonceKey after joining a DAO
// WalletManagerAndroid.getInstance()
// .addNewNonceKey(payload.proposeBlockData.SW_UNIQUE_ID)
// } catch (t: Throwable) {
// Log.e("Coin", "Joining failed. ${t.message ?: "No further information"}.")
// }
//TODO: Implement adding to the wallet without a Context
try {
joinBitcoinWallet(
payload.mostRecentSWBlock.transaction,
payload.proposeBlockData,
payload.signatures,
this.context
)
// Add new nonceKey after joining a DAO
WalletManagerAndroid.getInstance()
.addNewNonceKey(payload.proposeBlockData.SW_UNIQUE_ID, this.context)
} catch (t: Throwable) {
Log.e("Coin", "Joining failed. ${t.message ?: "No further information"}.")
}
}

fun onAliveResponse(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,6 @@ class JoinDAOFragment : BaseFragment(R.layout.fragment_join_network) {
signatures,
latestHash
)

try {
getCoinCommunity().joinBitcoinWallet(
mostRecentSWBlock.transaction,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,14 @@ import nl.tudelft.trustchain.currencyii.CoinCommunity.Companion.SIGNATURE_AGREEM
import nl.tudelft.trustchain.currencyii.CoinCommunity.Companion.SIGNATURE_ASK_BLOCK
import nl.tudelft.trustchain.currencyii.TrustChainHelper
import nl.tudelft.trustchain.currencyii.coin.WalletManagerAndroid
import nl.tudelft.trustchain.currencyii.sharedWallet.*
import nl.tudelft.trustchain.currencyii.sharedWallet.SWJoinBlockTD
import nl.tudelft.trustchain.currencyii.sharedWallet.SWJoinBlockTransactionData
import nl.tudelft.trustchain.currencyii.sharedWallet.SWResponseNegativeSignatureTransactionData
import nl.tudelft.trustchain.currencyii.sharedWallet.SWResponseSignatureBlockTD
import nl.tudelft.trustchain.currencyii.sharedWallet.SWResponseSignatureTransactionData
import nl.tudelft.trustchain.currencyii.sharedWallet.SWSignatureAskBlockTD
import nl.tudelft.trustchain.currencyii.sharedWallet.SWSignatureAskTransactionData
import nl.tudelft.trustchain.currencyii.sharedWallet.SWUtil
import nl.tudelft.trustchain.currencyii.util.taproot.CTransaction
import nl.tudelft.trustchain.currencyii.util.taproot.MuSig
import org.bitcoinj.core.Coin
Expand Down

0 comments on commit 5022c6b

Please sign in to comment.