Skip to content

Commit

Permalink
updates for initial libp2p integration
Browse files Browse the repository at this point in the history
  • Loading branch information
n8fr8 committed Jun 27, 2024
1 parent e2d9be1 commit 7a0c0e9
Show file tree
Hide file tree
Showing 17 changed files with 441 additions and 73 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -537,7 +537,7 @@ class CameraFragment : BaseFragment<FragmentCameraBinding>(R.layout.fragment_cam
.build() else CameraSelector.DEFAULT_BACK_CAMERA
proofModeCamera = localCameraProvider.bindToLifecycle(
viewLifecycleOwner, // current lifecycle owner
hdrCameraSelector ?: selector, // either front or back facing
selector, // either front or back facing
preview, // camera preview use case
imageCapture // image capture use case
).apply {
Expand Down
6 changes: 3 additions & 3 deletions android-libproofmode/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,9 @@ dependencies {
implementation 'com.jakewharton.timber:timber:5.0.1'
implementation 'androidx.multidex:multidex:2.0.1'

implementation 'org.bouncycastle:bcpkix-jdk15to18:1.72'
implementation 'org.bouncycastle:bcprov-jdk15to18:1.72'
api 'org.bouncycastle:bcpg-jdk15to18:1.71'
implementation 'org.bouncycastle:bcpkix-jdk18on:1.76'
implementation 'org.bouncycastle:bcprov-jdk18on:1.76'
api 'org.bouncycastle:bcpg-jdk18on:1.76'
implementation 'androidx.core:core-ktx:1.12.0'

// Required -- JUnit 4 framework
Expand Down
6 changes: 3 additions & 3 deletions android-opentimestamps/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,9 @@ dependencies {
implementation 'com.jakewharton.timber:timber:5.0.1'
implementation 'androidx.multidex:multidex:2.0.1'

implementation 'org.bouncycastle:bcpkix-jdk15to18:1.72'
implementation 'org.bouncycastle:bcprov-jdk15to18:1.72'
api 'org.bouncycastle:bcpg-jdk15to18:1.71'
implementation 'org.bouncycastle:bcpkix-jdk18on:1.76'
implementation 'org.bouncycastle:bcprov-jdk18on:1.76'
api 'org.bouncycastle:bcpg-jdk18on:1.76'
implementation 'androidx.core:core-ktx:1.12.0'

implementation 'com.google.guava:listenablefuture:9999.0-empty-to-avoid-conflict-with-guava'
Expand Down
9 changes: 7 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ android {
}
packagingOptions {
resources {
excludes += ['org.bitcoin.production.checkpoints', 'org.bitcoin.test.checkpoints', 'org/bitcoinj/crypto/cacerts', 'org/bitcoinj/crypto/mnemonic/wordlist/english.txt', 'lib/x86_64/darwin/libscrypt.dylib', 'com/google/thirdparty/publicsuffix/PublicSuffixType.gwt.xml', 'com/google/thirdparty/publicsuffix/PublicSuffixPatterns.gwt.xml', 'com/google/thirdparty/publicsuffix/PublicSuffixPatterns.gwt.xml', 'org/apache/commons/cli/AlreadySelectedException.class']
excludes += ['org.bitcoin.production.checkpoints', 'org.bitcoin.test.checkpoints', 'org/bitcoinj/crypto/cacerts', 'org/bitcoinj/crypto/mnemonic/wordlist/english.txt', 'lib/x86_64/darwin/libscrypt.dylib', 'com/google/thirdparty/publicsuffix/PublicSuffixType.gwt.xml', 'com/google/thirdparty/publicsuffix/PublicSuffixPatterns.gwt.xml', 'com/google/thirdparty/publicsuffix/PublicSuffixPatterns.gwt.xml', 'org/apache/commons/cli/AlreadySelectedException.class','META-INF/INDEX.LIST','META-INF/io.netty.versions.properties']
}
}
lint {
Expand Down Expand Up @@ -101,7 +101,10 @@ android {
}

dependencies {
// implementation files('src/main/libs/Web3StorageMobile.aar')
implementation 'androidx.navigation:navigation-fragment-ktx:2.7.7'
implementation 'androidx.navigation:navigation-ui-ktx:2.7.7'

// implementation files('src/main/libs/Web3StorageMobile.aar')
testImplementation 'junit:junit:4.13.2'

implementation project(':android-libproofmode')
Expand Down Expand Up @@ -183,6 +186,8 @@ dependencies {
// Kotlin + coroutines
implementation "androidx.work:work-runtime-ktx:$work_version"

//libp2p
implementation 'io.libp2p:jvm-libp2p:1.1.1-RELEASE'



Expand Down
119 changes: 56 additions & 63 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,57 +2,52 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="org.witness.proofmode">

<!-- A device that has any camera be it front, back or external will work. -->
<uses-feature
android:name="android.hardware.camera.any"
android:required="false" />
<!-- Required only if your app needs to access images or photos
that other apps created. -->
<!--
Required only if your app needs to access images or photos
that other apps created.
-->
<uses-permission android:name="android.permission.READ_MEDIA_IMAGES" />

<!-- Required only if your app needs to access videos
that other apps created. -->
<!--
Required only if your app needs to access videos
that other apps created.
-->
<uses-permission android:name="android.permission.READ_MEDIA_VIDEO" />

<!-- Required only if your app needs to access audio files
that other apps created. -->
<!--
Required only if your app needs to access audio files
that other apps created.
-->
<uses-permission android:name="android.permission.READ_MEDIA_AUDIO" />

<!-- If your app doesn't need to access media files that other apps created,
set the "maxSdkVersion" attribute to "28" instead. -->
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"
<!--
If your app doesn't need to access media files that other apps created,
set the "maxSdkVersion" attribute to "28" instead.
-->
<uses-permission
android:name="android.permission.READ_EXTERNAL_STORAGE"
android:maxSdkVersion="32" />

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"
tools:remove="android:maxSdkVersion"
/>

<uses-permission android:name="android.permission.ACCESS_MEDIA_LOCATION"
/>

<!-- SDK 33 and up -->
<uses-permission android:name="android.permission.POST_NOTIFICATIONS"/>


<uses-permission
android:name="android.permission.WRITE_EXTERNAL_STORAGE"
tools:remove="android:maxSdkVersion" />
<uses-permission android:name="android.permission.ACCESS_MEDIA_LOCATION" /> <!-- SDK 33 and up -->
<uses-permission android:name="android.permission.POST_NOTIFICATIONS" />
<uses-permission
android:name="android.permission.CAMERA"
tools:remove="android:maxSdkVersion" />
<uses-permission android:name="android.permission.RECORD_AUDIO" />


<uses-permission
android:name="android.permission.ACCESS_COARSE_LOCATION"
tools:remove="android:maxSdkVersion" />
<uses-permission
android:name="android.permission.ACCESS_FINE_LOCATION"
tools:remove="android:maxSdkVersion" />

<!--
<uses-permission
android:name="android.permission.ACCESS_BACKGROUND_LOCATION"
tools:remove="android:maxSdkVersion" />-->

tools:remove="android:maxSdkVersion" />
-->
<uses-permission
android:name="android.permission.ACCESS_NETWORK_STATE"
tools:remove="android:maxSdkVersion" />
Expand All @@ -62,21 +57,23 @@
<uses-permission
android:name="android.permission.READ_PHONE_STATE"
tools:remove="android:maxSdkVersion" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE"/>

<uses-permission android:name="android.permission.GET_ACCOUNTS"/>
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
<uses-permission android:name="android.permission.GET_ACCOUNTS" />

<application
android:name=".ProofModeApp"
android:allowBackup="false"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:largeHeap="true"
android:supportsRtl="true"
android:theme="@style/AppTheme"
android:requestLegacyExternalStorage="true"
>

android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity
android:name=".org.witness.proofmode.share.p2p.P2PShareActivity"
android:exported="false"
android:label="@string/title_activity_p2_pshare"
android:theme="@style/AppTheme" />
<activity android:name=".PermissionActivity" />
<activity
android:name=".onboarding.OnboardingActivity"
Expand All @@ -85,10 +82,10 @@
android:theme="@style/FullscreenTheme" />
<activity
android:name=".MainActivity"
android:exported="true"
android:label="@string/app_name"
android:screenOrientation="portrait"
android:theme="@style/AppTheme.NoActionBar"
android:exported="true">
android:theme="@style/AppTheme.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

Expand Down Expand Up @@ -133,9 +130,9 @@

<activity
android:name=".ShareProofActivity"
android:exported="true"
android:label="@string/share_proof_action"
android:theme="@style/AppTheme.NoActionBar"
android:exported="true">
android:theme="@style/AppTheme.NoActionBar">
<intent-filter>
<action
android:name="android.intent.action.SEND"
Expand Down Expand Up @@ -169,24 +166,26 @@
android:label="@string/share_proof_action" />

<category android:name="android.intent.category.DEFAULT" />
<data android:mimeType="audio/*"/>

<data android:mimeType="audio/*" />
<data android:mimeType="image/*" />
<data android:mimeType="video/*" />
</intent-filter>
<intent-filter>
<action
android:name="org.witness.proofmode.action.SHARE_PROOF"
android:label="@string/share_proof_action" />

<category android:name="android.intent.category.DEFAULT" />

<data android:mimeType="*/*" />
</intent-filter>
</activity>

<activity
android:name=".VerifyProofActivity"
android:exported="true"
android:label="@string/verify_proof"
android:theme="@style/AppTheme.NoActionBar"
android:exported="true">
android:theme="@style/AppTheme.NoActionBar">
<intent-filter>
<action
android:name="android.intent.action.SEND"
Expand All @@ -208,45 +207,41 @@
android:permission="android.permission.BIND_JOB_SERVICE" />
<service
android:name=".ProofModeTileService"
android:exported="false"
android:icon="@drawable/ic_stat_proofmodewhite"
android:label="@string/app_name"
android:permission="android.permission.BIND_QUICK_SETTINGS_TILE"
android:exported="false">
android:permission="android.permission.BIND_QUICK_SETTINGS_TILE">
<intent-filter>
<action android:name="android.service.quicksettings.action.QS_TILE" />
</intent-filter>
</service>

<receiver
android:name="org.witness.proofmode.service.CameraEventReceiver"
android:exported="true"
android:enabled="true" >

android:name=".service.CameraEventReceiver"
android:enabled="true"
android:exported="true">
<intent-filter android:priority="10000">
<action android:name="com.android.camera.NEW_PICTURE" />

<category android:name="android.intent.category.DEFAULT" />

<data android:mimeType="image/*" />
</intent-filter>


<intent-filter android:priority="10000">
<action android:name="com.android.camera.NEW_VIDEO" />

<category android:name="android.intent.category.DEFAULT" />

<data android:mimeType="video/*" />
</intent-filter>

</receiver>

<receiver
android:name="org.witness.proofmode.ProofEventReceiver"
android:exported="true"
android:name=".ProofEventReceiver"
android:enabled="true"
>

android:exported="true">
<intent-filter android:priority="10000">
<action android:name="org.witness.proofmode.PROOF_GENERATED" />
</intent-filter>

</receiver>

<provider
Expand All @@ -258,12 +253,10 @@
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/provider_paths" />
</provider>

<provider
android:name="androidx.startup.InitializationProvider"
android:authorities="${applicationId}.androidx-startup"
tools:node="remove">
</provider>
tools:node="remove"></provider>
</application>

</manifest>
8 changes: 8 additions & 0 deletions app/src/main/java/org/witness/proofmode/ProofModeApp.kt
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,12 @@ import org.witness.proofmode.notaries.OpenTimestampsNotarizationProvider
import org.witness.proofmode.notaries.SafetyNetCheck
import org.witness.proofmode.notarization.NotarizationProvider
import org.witness.proofmode.org.witness.proofmode.ProofBackgroundWorker
import org.witness.proofmode.org.witness.proofmode.share.p2p.P2PNotarizationProvider
import timber.log.Timber
import java.io.IOException
import java.util.concurrent.Executors
import kotlin.random.Random
import java.util.concurrent.CompletableFuture.runAsync

private var mPgpUtils: PgpUtils? = null
private lateinit var mPrefs: SharedPreferences
Expand Down Expand Up @@ -256,6 +258,12 @@ class ProofModeApp : MultiDexApplication() {
//notarize and then write proof so we can include notarization response
val gProvider = GoogleSafetyNetNotarizationProvider(this)
ProofMode.addNotarizationProvider(this, gProvider)

runAsync {
val p2pProvider = P2PNotarizationProvider()
ProofMode.addNotarizationProvider(this, p2pProvider)
}

} catch (ce: ClassNotFoundException) {
//SafetyNet API not available
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package org.witness.proofmode.org.witness.proofmode.share.p2p

import io.libp2p.core.PeerId
import io.libp2p.core.Stream
import io.libp2p.core.multistream.ProtocolId
import io.libp2p.core.multistream.StrictProtocolBinding
import io.libp2p.etc.types.toByteBuf
import io.libp2p.protocol.ProtocolHandler
import io.libp2p.protocol.ProtocolMessageHandler
import io.netty.buffer.ByteBuf
import java.nio.charset.Charset
import java.util.concurrent.CompletableFuture

interface ChatController {
fun send(message: String)
}

typealias OnChatMessage = (PeerId, String) -> Unit

class Chat(chatCallback: OnChatMessage) : ChatBinding(ChatProtocol(chatCallback))

const val protocolId: ProtocolId = "/example/chat/0.1.0"

open class ChatBinding(echo: ChatProtocol) : StrictProtocolBinding<ChatController>(protocolId, echo)

open class ChatProtocol(
private val chatCallback: OnChatMessage
) : ProtocolHandler<ChatController>(Long.MAX_VALUE, Long.MAX_VALUE) {

override fun onStartInitiator(stream: Stream) = onStart(stream)
override fun onStartResponder(stream: Stream) = onStart(stream)

private fun onStart(stream: Stream): CompletableFuture<ChatController> {
val ready = CompletableFuture<Void>()
val handler = Chatter(chatCallback, ready)
stream.pushHandler(handler)
return ready.thenApply { handler }
}

open inner class Chatter(
private val chatCallback: OnChatMessage,
val ready: CompletableFuture<Void>
) : ProtocolMessageHandler<ByteBuf>, ChatController {
lateinit var stream: Stream

override fun onActivated(stream: Stream) {
this.stream = stream
ready.complete(null)
}

override fun onMessage(stream: Stream, msg: ByteBuf) {
val msgStr = msg.toString(Charset.defaultCharset())
chatCallback(stream.remotePeerId(), msgStr)
}

override fun send(message: String) {
val data = message.toByteArray(Charset.defaultCharset())
stream.writeAndFlush(data.toByteBuf())
}
}
}
Loading

0 comments on commit 7a0c0e9

Please sign in to comment.