-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #85 from xmtp/daniel-remote-attachment
feat: support remote attachments
- Loading branch information
Showing
26 changed files
with
1,457 additions
and
111 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
32 changes: 32 additions & 0 deletions
32
android/src/main/java/expo/modules/xmtpreactnativesdk/wrappers/DecryptedLocalAttachment.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package expo.modules.xmtpreactnativesdk.wrappers | ||
|
||
import com.google.gson.GsonBuilder | ||
import com.google.gson.JsonObject | ||
import com.google.gson.JsonParser | ||
|
||
/** | ||
* Refers to a decrypted attachment that is stored locally on the device. | ||
*/ | ||
class DecryptedLocalAttachment( | ||
val fileUri: String, | ||
val mimeType: String, | ||
) { | ||
companion object { | ||
fun fromJsonObject(obj: JsonObject) = DecryptedLocalAttachment( | ||
obj.get("fileUri").asString, | ||
obj.get("mimeType").asString, | ||
) | ||
|
||
fun fromJson(json: String): DecryptedLocalAttachment { | ||
val obj = JsonParser.parseString(json).asJsonObject | ||
return fromJsonObject(obj); | ||
} | ||
} | ||
|
||
fun toJsonMap(): Map<String, Any> = mapOf( | ||
"fileUri" to fileUri, | ||
"mimeType" to mimeType, | ||
) | ||
|
||
fun toJson(): String = GsonBuilder().create().toJson(toJsonMap()) | ||
} |
66 changes: 66 additions & 0 deletions
66
...oid/src/main/java/expo/modules/xmtpreactnativesdk/wrappers/EncryptedAttachmentMetadata.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
package expo.modules.xmtpreactnativesdk.wrappers | ||
|
||
import com.facebook.common.util.Hex | ||
import com.google.gson.JsonObject | ||
import com.google.protobuf.ByteString | ||
import com.google.protobuf.kotlin.toByteString | ||
import org.xmtp.android.library.codecs.Attachment | ||
import org.xmtp.android.library.codecs.EncryptedEncodedContent | ||
import org.xmtp.android.library.codecs.RemoteAttachment | ||
|
||
/** | ||
* Describes the metadata for an encrypted attachment used to encrypt/decrypt the payload. | ||
*/ | ||
class EncryptedAttachmentMetadata( | ||
val filename: String, | ||
val secret: ByteString, | ||
val salt: ByteString, | ||
val nonce: ByteString, | ||
val contentDigest: String, | ||
val contentLength: Int, | ||
) { | ||
companion object { | ||
fun fromAttachment( | ||
attachment: Attachment, | ||
encrypted: EncryptedEncodedContent | ||
) = EncryptedAttachmentMetadata( | ||
attachment.filename, | ||
encrypted.secret, | ||
encrypted.salt, | ||
encrypted.nonce, | ||
encrypted.contentDigest, | ||
attachment.data.size(), | ||
) | ||
|
||
fun fromRemoteAttachment( | ||
remoteAttachment: RemoteAttachment, | ||
) = EncryptedAttachmentMetadata( | ||
remoteAttachment.filename ?: "", | ||
remoteAttachment.secret, | ||
remoteAttachment.salt, | ||
remoteAttachment.nonce, | ||
remoteAttachment.contentDigest, | ||
remoteAttachment.contentLength ?: 0, | ||
) | ||
|
||
fun fromJsonObj( | ||
obj: JsonObject, | ||
) = EncryptedAttachmentMetadata( | ||
obj.get("filename").asString, | ||
Hex.hexStringToByteArray(obj.get("secret").asString).toByteString(), | ||
Hex.hexStringToByteArray(obj.get("salt").asString).toByteString(), | ||
Hex.hexStringToByteArray(obj.get("nonce").asString).toByteString(), | ||
obj.get("contentDigest").asString, | ||
Integer.parseInt(obj.get("contentLength").asString), | ||
) | ||
} | ||
|
||
fun toJsonMap(): Map<String, Any> = mapOf( | ||
"filename" to filename, | ||
"secret" to Hex.encodeHex(secret.toByteArray(), false), | ||
"salt" to Hex.encodeHex(salt.toByteArray(), false), | ||
"nonce" to Hex.encodeHex(nonce.toByteArray(), false), | ||
"contentDigest" to contentDigest, | ||
"contentLength" to contentLength.toString(), | ||
) | ||
} |
48 changes: 48 additions & 0 deletions
48
android/src/main/java/expo/modules/xmtpreactnativesdk/wrappers/EncryptedLocalAttachment.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package expo.modules.xmtpreactnativesdk.wrappers | ||
|
||
import android.net.Uri | ||
import com.google.gson.GsonBuilder | ||
import com.google.gson.JsonObject | ||
import com.google.gson.JsonParser | ||
import org.xmtp.android.library.codecs.Attachment | ||
import org.xmtp.android.library.codecs.EncryptedEncodedContent | ||
|
||
/** | ||
* Refers to an encrypted attachment that is stored locally on the device | ||
* alongside the metadata that can be used to encrypt/decrypt it. | ||
*/ | ||
class EncryptedLocalAttachment( | ||
val encryptedLocalFileUri: String, | ||
val metadata: EncryptedAttachmentMetadata, | ||
) { | ||
companion object { | ||
fun from( | ||
attachment: Attachment, | ||
encrypted: EncryptedEncodedContent, | ||
encryptedFile: Uri, | ||
) = EncryptedLocalAttachment( | ||
encryptedFile.toString(), | ||
EncryptedAttachmentMetadata.fromAttachment( | ||
attachment, | ||
encrypted | ||
) | ||
) | ||
|
||
fun fromJsonObject(obj: JsonObject) = EncryptedLocalAttachment( | ||
obj.get("encryptedLocalFileUri").asString, | ||
EncryptedAttachmentMetadata.fromJsonObj(obj.get("metadata").asJsonObject), | ||
) | ||
|
||
fun fromJson(json: String): EncryptedLocalAttachment { | ||
val obj = JsonParser.parseString(json).asJsonObject | ||
return fromJsonObject(obj); | ||
} | ||
} | ||
|
||
fun toJsonMap(): Map<String, Any> = mapOf( | ||
"encryptedLocalFileUri" to encryptedLocalFileUri, | ||
"metadata" to metadata.toJsonMap() | ||
) | ||
|
||
fun toJson(): String = GsonBuilder().create().toJson(toJsonMap()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
data/ |
Oops, something went wrong.