Skip to content

Commit

Permalink
Merge pull request #4 from MKuijpers/add_new_page
Browse files Browse the repository at this point in the history
Add our page to the app
  • Loading branch information
brian2509 authored Mar 3, 2020
2 parents 9dbd266 + 6349e92 commit 57f5fe7
Show file tree
Hide file tree
Showing 11 changed files with 271 additions and 17 deletions.
7 changes: 1 addition & 6 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,12 +1,7 @@
*.iml
.gradle
/local.properties
/.idea/caches
/.idea/libraries
/.idea/modules.xml
/.idea/workspace.xml
/.idea/navEditor.xml
/.idea/assetWizardSettings.xml
/.idea/*
.DS_Store
/build
/captures
Expand Down
5 changes: 3 additions & 2 deletions demo-android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ android {


defaultConfig {
applicationId "nl.tudelft.ipv8.android.demo"
applicationId "nl.tudelft.ipv8.android.demo_currency"
minSdkVersion 26
targetSdkVersion 29
versionCode 1
Expand Down Expand Up @@ -80,13 +80,14 @@ dependencies {
implementation 'com.mattskala:itemadapter:0.2'

// Testing
implementation 'androidx.legacy:legacy-support-v4:1.0.0'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test.ext:junit:1.1.1'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
}

tasks.withType(org.jetbrains.kotlin.gradle.tasks.KotlinCompile).all {
kotlinOptions.freeCompilerArgs += [
"-Xuse-experimental=kotlin.Experimental,kotlin.ExperimentalUnsignedTypes"
"-Xuse-experimental=kotlin.Experimental,kotlin.ExperimentalUnsignedTypes"
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package nl.tudelft.ipv8.android.demo
import nl.tudelft.ipv8.Address
import nl.tudelft.ipv8.Community
import nl.tudelft.ipv8.IPv8
import nl.tudelft.ipv8.android.IPv8Android
import nl.tudelft.ipv8.attestation.trustchain.*
import nl.tudelft.ipv8.attestation.trustchain.store.TrustChainStore
import nl.tudelft.ipv8.keyvault.Key
import java.util.*

class CoinCommunity: Community() {
override val serviceId = "0000bitcoin0000community0000"

public val discoveredAddressesContacted: MutableMap<Address, Date> = mutableMapOf()

protected val trustchain: TrustChainHelper by lazy {
TrustChainHelper(getTrustChainCommunity())
}

override fun walkTo(address: Address) {
super.walkTo(address)
discoveredAddressesContacted[address] = Date()
}

protected fun getTrustChainCommunity(): TrustChainCommunity {
return getIpv8().getOverlay() ?: throw IllegalStateException("TrustChainCommunity is not configured")
}

protected fun getIpv8(): IPv8 {
return IPv8Android.getInstance()
}

public fun sendCurrency(amount: Double, toPublicKey: ByteArray = myPeer.publicKey.keyToBin()) {
val message = "Transaction amount: $amount bitcoins"
trustchain.createProposalBlock(message, toPublicKey, BLOCK_TYPE)
}

companion object {
private const val BLOCK_TYPE = "coin_block"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ class DemoApplication : Application() {
val config = IPv8Configuration(overlays = listOf(
createDiscoveryCommunity(),
createTrustChainCommunity(),
createDemoCommunity()
createDemoCommunity(),
createCoinCommunity()
), walkerInterval = 1.0)

IPv8Android.Factory(this)
Expand Down Expand Up @@ -93,6 +94,15 @@ class DemoApplication : Application() {
)
}

private fun createCoinCommunity(): OverlayConfiguration<CoinCommunity> {
val randomWalk = RandomWalk.Factory()
val nsd = NetworkServiceDiscovery.Factory(getSystemService()!!)
return OverlayConfiguration(
Overlay.Factory(CoinCommunity::class.java),
listOf(randomWalk, nsd)
)
}

private fun createDemoCommunity(): OverlayConfiguration<DemoCommunity> {
val randomWalk = RandomWalk.Factory()
val nsd = NetworkServiceDiscovery.Factory(getSystemService()!!)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,7 @@ class TrustChainHelper(
/**
* Creates a new proposal block, using a text message as the transaction content.
*/
fun createProposalBlock(message: String, publicKey: ByteArray) {
val blockType = "demo_block"
fun createProposalBlock(message: String, publicKey: ByteArray, blockType: String = "demo_block") {
val transaction = mapOf("message" to message)
trustChainCommunity.createProposalBlock(blockType, transaction, publicKey)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import androidx.annotation.LayoutRes
import androidx.fragment.app.Fragment
import nl.tudelft.ipv8.IPv8
import nl.tudelft.ipv8.android.IPv8Android
import nl.tudelft.ipv8.android.demo.CoinCommunity
import nl.tudelft.ipv8.android.demo.DemoCommunity

import nl.tudelft.ipv8.android.demo.TrustChainHelper
Expand All @@ -25,4 +26,8 @@ abstract class BaseFragment(@LayoutRes contentLayoutId: Int = 0) : Fragment(cont
protected fun getDemoCommunity(): DemoCommunity {
return getIpv8().getOverlay() ?: throw IllegalStateException("DemoCommunity is not configured")
}

protected fun getCoinCommunity(): CoinCommunity {
return getIpv8().getOverlay() ?: throw IllegalStateException("CoinCommunity is not configured")
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package nl.tudelft.ipv8.android.demo.ui.bitcoin

import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import kotlinx.android.synthetic.main.fragment_bitcoin.*
import nl.tudelft.ipv8.android.demo.R
import nl.tudelft.ipv8.android.demo.ui.BaseFragment
import nl.tudelft.ipv8.util.hexToBytes

/**
* A simple [Fragment] subclass.
* Use the [BitcoinFragment.newInstance] factory method to
* create an instance of this fragment.
*/
class BitcoinFragment : BaseFragment(R.layout.fragment_bitcoin) {
private var publicKeyReceiver: String = ""
private var bitcoinPrivateKey: String = ""
private var transactionAmount: Double = 0.0

override fun onActivityCreated(savedInstanceState: Bundle?) {
super.onActivityCreated(savedInstanceState)

button3.setOnClickListener {
publicKeyReceiver = pk_receiver.text.toString()
transactionAmount = tx_amount.text.toString().toDouble()
outputTextView.text = "PK Receiver: $publicKeyReceiver, Amount: $transactionAmount"

getCoinCommunity().sendCurrency(transactionAmount, publicKeyReceiver.hexToBytes())
}
}

override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_bitcoin, container, false)
}

companion object {
/**
* Use this factory method to create a new instance of
* this fragment using the provided parameters.
*
* @return A new instance of fragment bitcoinFragment.
*/
@JvmStatic
fun newInstance() = BitcoinFragment()
}
}
133 changes: 133 additions & 0 deletions demo-android/src/main/res/layout/fragment_bitcoin.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".ui.bitcoin.BitcoinFragment">

<!-- TODO: Update blank fragment layout -->

<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="20px">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/sending_bitcoins"
android:textAppearance="@style/TextAppearance.AppCompat.Large" />

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">

<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="PK of Receiver:" />

<EditText
android:id="@+id/pk_receiver"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="textPersonName" />
</LinearLayout>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">

<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Bitcoin amount:" />

<EditText
android:id="@+id/tx_amount"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:ems="10"
android:inputType="numberDecimal" />
</LinearLayout>

<Button
android:id="@+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/send_bitcoin" />
<TextView
android:id="@+id/outputTextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="" />

<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:baselineAligned="false"
android:gravity="bottom"
android:orientation="vertical">

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">

<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">

<TextView
android:id="@+id/textView7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Balance:" />

<TextView
android:id="@+id/textView6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="10.3 BtC"
android:textAlignment="center" />
</LinearLayout>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">

<TextView
android:id="@+id/textView5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Public Key:" />

<TextView
android:id="@+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="1BvBMSEYstWetqTFn5Au4m4GFg7xJaNVN2"
android:textAlignment="center" />
</LinearLayout>

</LinearLayout>

</LinearLayout>

</LinearLayout>

</FrameLayout>
12 changes: 8 additions & 4 deletions demo-android/src/main/res/menu/bottom_navigation_menu.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@
android:id="@+id/peersFragment"
android:title="Peers"
android:icon="@drawable/ic_device_hub_black_24dp" />
<item
android:id="@+id/bitcoinFragment"
android:title="@string/bitcoin"
android:icon="@drawable/ic_device_hub_black_24dp" />
<item
android:id="@+id/usersFragment"
android:title="Chains"
Expand All @@ -18,8 +22,8 @@
android:id="@+id/myChainFragment"
android:title="My Chain"
android:icon="@drawable/ic_person_black_24dp" />
<item
android:id="@+id/debugFragment"
android:title="Debug"
android:icon="@drawable/ic_bug_report_black_24dp" />
<!-- <item-->
<!-- android:id="@+id/debugFragment"-->
<!-- android:title="Debug"-->
<!-- android:icon="@drawable/ic_bug_report_black_24dp" />-->
</menu>
9 changes: 8 additions & 1 deletion demo-android/src/main/res/navigation/nav_graph.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
<?xml version="1.0" encoding="utf-8"?>
<navigation xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto" android:id="@+id/nav_graph"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/nav_graph"
app:startDestination="@id/peersFragment">

<fragment
Expand Down Expand Up @@ -47,4 +49,9 @@
android:id="@+id/latestBlocksFragment"
android:name="nl.tudelft.ipv8.android.demo.ui.blocks.LatestBlocksFragment"
android:label="All Blocks" />
<fragment
android:id="@+id/bitcoinFragment"
android:name="nl.tudelft.ipv8.android.demo.ui.bitcoin.BitcoinFragment"
android:label="fragment_bitcoin"
tools:layout="@layout/fragment_bitcoin" />
</navigation>
8 changes: 7 additions & 1 deletion demo-android/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<resources>
<string name="app_name">TrustChain Explorer</string>
<string name="app_name">TrustChain Currency</string>

<plurals name="x_peers">
<item quantity="one">%d peer</item>
Expand All @@ -16,4 +16,10 @@

<string name="discovered"><![CDATA[<b>Discovered:</b> %s]]></string>
<string name="stored"><![CDATA[<b>Stored:</b> %s]]></string>

<!-- TODO: Remove or change this placeholder text -->
<string name="sending_bitcoins">Sending Bitcoins</string>
<string name="send_bitcoin">Send Bitcoin</string>
<string name="enter_private_key">Enter private key</string>
<string name="bitcoin">Bitcoin</string>
</resources>

0 comments on commit 57f5fe7

Please sign in to comment.