Skip to content

Commit

Permalink
Merge pull request #385 from xmtp/ar/update-beta-05-20-24
Browse files Browse the repository at this point in the history
chore: update beta
  • Loading branch information
alexrisch authored May 20, 2024
2 parents d36fc8c + de5c8a3 commit b142e66
Show file tree
Hide file tree
Showing 14 changed files with 229 additions and 51 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ import org.xmtp.android.library.push.XMTPPush
import org.xmtp.android.library.toHex
import org.xmtp.proto.keystore.api.v1.Keystore.TopicMap.TopicData
import org.xmtp.proto.message.api.v1.MessageApiOuterClass
import org.xmtp.proto.message.contents.Invitation.ConsentProofPayload
import org.xmtp.proto.message.contents.PrivateKeyOuterClass
import uniffi.xmtpv3.GroupPermissions
import java.io.File
Expand Down Expand Up @@ -712,11 +713,25 @@ class XMTPModule : Module() {
}
}

AsyncFunction("createConversation") Coroutine { clientAddress: String, peerAddress: String, contextJson: String ->
AsyncFunction("createConversation") Coroutine { clientAddress: String, peerAddress: String, contextJson: String, consentProofPayload: List<Int> ->
withContext(Dispatchers.IO) {
logV("createConversation: $contextJson")
val client = clients[clientAddress] ?: throw XMTPException("No client")
val context = JsonParser.parseString(contextJson).asJsonObject

var consentProof: ConsentProofPayload? = null
if (consentProofPayload.isNotEmpty()) {
val consentProofDataBytes = consentProofPayload.foldIndexed(ByteArray(consentProofPayload.size)) { i, a, v ->
a.apply {
set(
i,
v.toByte()
)
}
}
consentProof = ConsentProofPayload.parseFrom(consentProofDataBytes)
}

val conversation = client.conversations.newConversation(
peerAddress,
context = InvitationV1ContextBuilder.buildFromConversation(
Expand All @@ -733,11 +748,15 @@ class XMTPModule : Module() {

else -> mapOf()
},
)
),
consentProof
)
if (conversation.keyMaterial == null) {
logV("Null key material before encode conversation")
}
if (conversation.consentProof == null) {
logV("Null consent before encode conversation")
}
ConversationWrapper.encode(client, conversation)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import org.xmtp.android.library.Conversation
class ConversationWrapper {

companion object {
fun encodeToObj(client: Client, conversation: Conversation): Map<String, Any> {
fun encodeToObj(client: Client, conversation: Conversation): Map<String, Any?> {
val context = when (conversation.version) {
Conversation.Version.V2 -> mapOf<String, Any>(
"conversationID" to (conversation.conversationId ?: ""),
Expand All @@ -26,7 +26,8 @@ class ConversationWrapper {
"peerAddress" to conversation.peerAddress,
"version" to "DIRECT",
"conversationID" to (conversation.conversationId ?: ""),
"keyMaterial" to (conversation.keyMaterial?.let { Base64.encodeToString(it, Base64.NO_WRAP) } ?: "")
"keyMaterial" to (conversation.keyMaterial?.let { Base64.encodeToString(it, Base64.NO_WRAP) } ?: ""),
"consentProof" to if (conversation.consentProof != null) Base64.encodeToString(conversation.consentProof?.toByteArray(), Base64.NO_WRAP) else null
)
}

Expand Down
2 changes: 1 addition & 1 deletion example/ios/Podfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -769,4 +769,4 @@ SPEC CHECKSUMS:

PODFILE CHECKSUM: 95d6ace79946933ecf80684613842ee553dd76a2

COCOAPODS: 1.14.2
COCOAPODS: 1.15.2
1 change: 1 addition & 0 deletions example/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
"@thirdweb-dev/react-native": "^0.6.2",
"@thirdweb-dev/react-native-compat": "^0.6.2",
"@xmtp/frames-client": "^0.3.2",
"@xmtp/proto": "3.54.0",
"ethers": "^5.7.2",
"expo": "~48.0.18",
"expo-document-picker": "^11.5.4",
Expand Down
61 changes: 60 additions & 1 deletion example/src/tests/tests.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { FramesClient } from '@xmtp/frames-client'
import { content, keystore } from '@xmtp/proto'
import { content, invitation } from '@xmtp/proto'
import { createHmac } from 'crypto'
import { ethers } from 'ethers'
import ReactNativeBlobUtil from 'react-native-blob-util'
import Config from 'react-native-config'
import { TextEncoder, TextDecoder } from 'text-encoding'
Expand Down Expand Up @@ -1790,3 +1791,61 @@ test('can handle complex streaming setup with messages from self', async () => {

return true
})

test('can send and receive consent proofs', async () => {
const alixWallet = await ethers.Wallet.createRandom()
const boWallet = await ethers.Wallet.createRandom()
const bo = await Client.create(boWallet, { env: 'local' })
await delayToPropogate()
const alix = await Client.create(alixWallet, { env: 'local' })
await delayToPropogate()

const timestamp = Date.now()
const consentMessage =
'XMTP : Grant inbox consent to sender\n' +
'\n' +
`Current Time: ${new Date(timestamp).toUTCString()}\n` +
`From Address: ${bo.address}\n` +
'\n' +
'For more info: https://xmtp.org/signatures/'
const sig = await alixWallet.signMessage(consentMessage)
const consentProof = invitation.ConsentProofPayload.fromPartial({
payloadVersion:
invitation.ConsentProofPayloadVersion.CONSENT_PROOF_PAYLOAD_VERSION_1,
signature: sig,
timestamp,
})

const boConvo = await bo.conversations.newConversation(
alix.address,
undefined,
consentProof
)
await delayToPropogate()
assert(!!boConvo?.consentProof, 'bo consentProof should exist')
const convos = await alix.conversations.list()
const alixConvo = convos.find((convo) => convo.topic === boConvo.topic)
await delayToPropogate()
assert(!!alixConvo?.consentProof, ' alix consentProof should not exist')
await delayToPropogate()
await alix.contacts.refreshConsentList()
const isAllowed = await alix.contacts.isAllowed(bo.address)
assert(isAllowed, 'bo should be allowed')
return true
})

test('can start conversations without consent proofs', async () => {
const bo = await Client.createRandom({ env: 'local' })
await delayToPropogate()
const alix = await Client.createRandom({ env: 'local' })
await delayToPropogate()

const boConvo = await bo.conversations.newConversation(alix.address)
await delayToPropogate()
assert(!boConvo.consentProof, 'consentProof should not exist')
const alixConvo = (await alix.conversations.list())[0]
await delayToPropogate()
assert(!alixConvo.consentProof, 'consentProof should not exist')
await delayToPropogate()
return true
})
43 changes: 39 additions & 4 deletions example/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -4215,7 +4215,7 @@

"@ethersproject/shims@^5.7.0":
version "5.7.0"
resolved "https://registry.npmjs.org/@ethersproject/shims/-/shims-5.7.0.tgz"
resolved "https://registry.yarnpkg.com/@ethersproject/shims/-/shims-5.7.0.tgz#ee32e543418595774029c5ea6123ea8995e7e154"
integrity sha512-WeDptc6oAprov5CCN2LJ/6/+dC9gTonnkdAtLepm/7P5Z+3PRxS5NpfVWmOMs1yE4Vitl2cU8bOPWC0GvGSbVg==

"@ethersproject/[email protected]", "@ethersproject/signing-key@^5.7.0":
Expand Down Expand Up @@ -6843,6 +6843,16 @@
rxjs "^7.8.0"
undici "^5.8.1"

"@xmtp/[email protected]":
version "3.54.0"
resolved "https://registry.yarnpkg.com/@xmtp/proto/-/proto-3.54.0.tgz#1b6be9fa2268a51b730bf484af3bf0ebc2621505"
integrity sha512-X0jDRR19/tH0qRB8mM/H/vBueQAK22VZF4QUnDN7TgnbNaOYL5DvSmPfXFH+xzeGKQ5S0zgwc+qFJbI4xoKNHw==
dependencies:
long "^5.2.0"
protobufjs "^7.0.0"
rxjs "^7.8.0"
undici "^5.8.1"

JSONStream@^1.3.5:
version "1.3.5"
resolved "https://registry.npmjs.org/JSONStream/-/JSONStream-1.3.5.tgz"
Expand Down Expand Up @@ -14498,7 +14508,16 @@ strict-uri-encode@^2.0.0:
resolved "https://registry.npmjs.org/strict-uri-encode/-/strict-uri-encode-2.0.0.tgz"
integrity sha512-QwiXZgpRcKkhTj2Scnn++4PKtWsH0kpzZ62L2R6c/LUVYv7hVnZqcg2+sMuT6R7Jusu1vviK/MFsu6kNJfWlEQ==

"string-width-cjs@npm:string-width@^4.2.0", string-width@^4.1.0, string-width@^4.2.0, string-width@^4.2.3:
"string-width-cjs@npm:string-width@^4.2.0":
version "4.2.3"
resolved "https://registry.npmjs.org/string-width/-/string-width-4.2.3.tgz"
integrity sha512-wKyQRQpjJ0sIp62ErSZdGsjMJWsap5oRNihHhu6G7JVO/9jIB6UyevL+tXuOqrng8j/cxKTWyWUwvSTriiZz/g==
dependencies:
emoji-regex "^8.0.0"
is-fullwidth-code-point "^3.0.0"
strip-ansi "^6.0.1"

string-width@^4.1.0, string-width@^4.2.0, string-width@^4.2.3:
version "4.2.3"
resolved "https://registry.npmjs.org/string-width/-/string-width-4.2.3.tgz"
integrity sha512-wKyQRQpjJ0sIp62ErSZdGsjMJWsap5oRNihHhu6G7JVO/9jIB6UyevL+tXuOqrng8j/cxKTWyWUwvSTriiZz/g==
Expand Down Expand Up @@ -14572,7 +14591,7 @@ string_decoder@~1.1.1:
dependencies:
safe-buffer "~5.1.0"

"strip-ansi-cjs@npm:strip-ansi@^6.0.1", strip-ansi@^6.0.0, strip-ansi@^6.0.1:
"strip-ansi-cjs@npm:strip-ansi@^6.0.1":
version "6.0.1"
resolved "https://registry.npmjs.org/strip-ansi/-/strip-ansi-6.0.1.tgz"
integrity sha512-Y38VPSHcqkFrCpFnQ9vuSXmquuv5oXOKpGeT6aGrr3o3Gc9AlVa6JBfUSOCnbxGGZF+/0ooI7KrPuUSztUdU5A==
Expand All @@ -14586,6 +14605,13 @@ strip-ansi@^5.0.0, strip-ansi@^5.2.0:
dependencies:
ansi-regex "^4.1.0"

strip-ansi@^6.0.0, strip-ansi@^6.0.1:
version "6.0.1"
resolved "https://registry.npmjs.org/strip-ansi/-/strip-ansi-6.0.1.tgz"
integrity sha512-Y38VPSHcqkFrCpFnQ9vuSXmquuv5oXOKpGeT6aGrr3o3Gc9AlVa6JBfUSOCnbxGGZF+/0ooI7KrPuUSztUdU5A==
dependencies:
ansi-regex "^5.0.1"

strip-ansi@^7.0.1:
version "7.1.0"
resolved "https://registry.npmjs.org/strip-ansi/-/strip-ansi-7.1.0.tgz"
Expand Down Expand Up @@ -15856,7 +15882,7 @@ word-wrap@~1.2.3:
resolved "https://registry.npmjs.org/word-wrap/-/word-wrap-1.2.5.tgz"
integrity sha512-BN22B5eaMMI9UMtjrGd5g5eCYPpCPDUy0FJXbYsaT5zYxjFOckS53SQDE3pWkVoWpHXVb3BrYcEN4Twa55B5cA==

"wrap-ansi-cjs@npm:wrap-ansi@^7.0.0", wrap-ansi@^7.0.0:
"wrap-ansi-cjs@npm:wrap-ansi@^7.0.0":
version "7.0.0"
resolved "https://registry.npmjs.org/wrap-ansi/-/wrap-ansi-7.0.0.tgz"
integrity sha512-YVGIj2kamLSTxw6NsZjoBxfSwsn0ycdesmc4p+Q21c5zPuZ1pl+NfxVdxPtdHvmNVOQ6XSYG4AUtyt/Fi7D16Q==
Expand All @@ -15874,6 +15900,15 @@ wrap-ansi@^6.2.0:
string-width "^4.1.0"
strip-ansi "^6.0.0"

wrap-ansi@^7.0.0:
version "7.0.0"
resolved "https://registry.npmjs.org/wrap-ansi/-/wrap-ansi-7.0.0.tgz"
integrity sha512-YVGIj2kamLSTxw6NsZjoBxfSwsn0ycdesmc4p+Q21c5zPuZ1pl+NfxVdxPtdHvmNVOQ6XSYG4AUtyt/Fi7D16Q==
dependencies:
ansi-styles "^4.0.0"
string-width "^4.1.0"
strip-ansi "^6.0.0"

wrap-ansi@^8.1.0:
version "8.1.0"
resolved "https://registry.npmjs.org/wrap-ansi/-/wrap-ansi-8.1.0.tgz"
Expand Down
7 changes: 6 additions & 1 deletion ios/Wrappers/ConversationWrapper.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ struct ConversationWrapper {
"metadata": cv2.context.metadata,
]
}
var consentProof: String? = nil
if (conversation.consentProof != nil) {
consentProof = try conversation.consentProof?.serializedData().base64EncodedString()
}
return [
"clientAddress": client.address,
"topic": conversation.topic,
Expand All @@ -25,7 +29,8 @@ struct ConversationWrapper {
"peerAddress": conversation.peerAddress,
"version": "DIRECT",
"conversationID": conversation.conversationID ?? "",
"keyMaterial": conversation.keyMaterial?.base64EncodedString() ?? ""
"keyMaterial": conversation.keyMaterial?.base64EncodedString() ?? "",
"consentProof": consentProof ?? ""
]
}

Expand Down
22 changes: 17 additions & 5 deletions ios/XMTPModule.swift
Original file line number Diff line number Diff line change
Expand Up @@ -604,18 +604,30 @@ public class XMTPModule: Module {
return prepared.messageID
}

AsyncFunction("createConversation") { (clientAddress: String, peerAddress: String, contextJson: String) -> String in
AsyncFunction("createConversation") { (clientAddress: String, peerAddress: String, contextJson: String, consentProofBytes: [UInt8]) -> String in
guard let client = await clientsManager.getClient(key: clientAddress) else {
throw Error.noClient
}

do {
let contextData = contextJson.data(using: .utf8)!
let contextObj = (try? JSONSerialization.jsonObject(with: contextData) as? [String: Any]) ?? [:]
let conversation = try await client.conversations.newConversation(with: peerAddress, context: .init(
conversationID: contextObj["conversationID"] as? String ?? "",
metadata: contextObj["metadata"] as? [String: String] ?? [:] as [String: String]
))
var consentProofData:ConsentProofPayload?
if consentProofBytes.count != 0 {
do {
consentProofData = try ConsentProofPayload(serializedData: Data(consentProofBytes))
} catch {
print("Error: \(error)")
}
}
let conversation = try await client.conversations.newConversation(
with: peerAddress,
context: .init(
conversationID: contextObj["conversationID"] as? String ?? "",
metadata: contextObj["metadata"] as? [String: String] ?? [:] as [String: String]
),
consentProofPayload:consentProofData
)

return try ConversationWrapper.encode(conversation, client: client)
} catch {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
"@ethersproject/bytes": "^5.7.0",
"@msgpack/msgpack": "^3.0.0-beta2",
"@noble/hashes": "^1.3.3",
"@xmtp/proto": "^3.44.0",
"@xmtp/proto": "3.54.0",
"buffer": "^6.0.3",
"text-encoding": "^0.7.0"
},
Expand Down
13 changes: 10 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { content, keystore } from '@xmtp/proto'
import { content, invitation, keystore } from '@xmtp/proto'
import { EventEmitter, NativeModulesProxy } from 'expo-modules-core'

import { Client } from '.'
Expand Down Expand Up @@ -430,15 +430,22 @@ export async function createConversation<
>(
client: Client<ContentTypes>,
peerAddress: string,
context?: ConversationContext
context?: ConversationContext,
consentProofPayload?: invitation.ConsentProofPayload
): Promise<Conversation<ContentTypes>> {
const consentProofData = consentProofPayload
? Array.from(
invitation.ConsentProofPayload.encode(consentProofPayload).finish()
)
: []
return new Conversation(
client,
JSON.parse(
await XMTPModule.createConversation(
client.address,
getAddress(peerAddress),
JSON.stringify(context || {})
JSON.stringify(context || {}),
consentProofData
)
)
)
Expand Down
Loading

0 comments on commit b142e66

Please sign in to comment.