Skip to content

Commit

Permalink
Merge pull request #209 from xmtp/np/fix-custom-content-type-bug
Browse files Browse the repository at this point in the history
Fix Custom Content Type Decryption
  • Loading branch information
cameronvoell committed Jan 11, 2024
2 parents d4619ec + ed58e51 commit d5ac040
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,6 @@ class ContentJson(
Client.register(RemoteAttachmentCodec())
Client.register(ReplyCodec())
Client.register(ReadReceiptCodec())
// TODO:
//Client.register(CompositeCodec())
}

fun fromJsonObject(obj: JsonObject): ContentJson {
Expand Down Expand Up @@ -121,8 +119,8 @@ class ContentJson(
return fromJsonObject(obj);
}

fun bytesFrom64(bytes64: String): ByteArray = Base64.decode(bytes64, Base64.DEFAULT)
fun bytesTo64(bytes: ByteArray): String = Base64.encodeToString(bytes, Base64.DEFAULT)
fun bytesFrom64(bytes64: String): ByteArray = Base64.decode(bytes64, Base64.NO_WRAP)
fun bytesTo64(bytes: ByteArray): String = Base64.encodeToString(bytes, Base64.NO_WRAP)
}

fun toJsonMap(): Map<String, Any> {
Expand Down Expand Up @@ -186,6 +184,8 @@ class ContentJson(
json.addProperty("fallback", encodedContent.fallback)
json.add("parameters", JsonParser.parseString(parameters))
json.add("type", typeJson)
json.addProperty("content", bytesTo64(encodedContent.content.toByteArray()))

}
val encodedContentJSON = json.toString()
if (encodedContentJSON.isNotBlank()) {
Expand Down
3 changes: 2 additions & 1 deletion example/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@
"react-native-screens": "~3.20.0",
"react-native-svg": "^13.9.0",
"react-native-url-polyfill": "^2.0.0",
"react-query": "^3.39.3"
"react-query": "^3.39.3",
"text-encoding": "^0.7.0"
},
"devDependencies": {
"@babel/core": "^7.20.0",
Expand Down
40 changes: 28 additions & 12 deletions example/src/tests.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { content } from '@xmtp/proto'
import ReactNativeBlobUtil from 'react-native-blob-util'
import { TextEncoder, TextDecoder } from 'text-encoding'
import { DecodedMessage } from 'xmtp-react-native-sdk/lib/DecodedMessage'

import {
Expand All @@ -24,25 +25,37 @@ const ContentTypeNumber: ContentTypeId = {
versionMinor: 0,
}

class NumberCodec implements JSContentCodec<number> {
export type NumberRef = {
topNumber: {
bottomNumber: number
}
}

class NumberCodec implements JSContentCodec<NumberRef> {
contentType = ContentTypeNumber

// a completely absurd way of encoding number values
encode(content: number): EncodedContent {
encode(content: NumberRef): EncodedContent {
return {
type: ContentTypeNumber,
parameters: {
number: JSON.stringify(content),
test: 'test',
},
content: new Uint8Array(),
content: new TextEncoder().encode(JSON.stringify(content)),
}
}

decode(encodedContent: EncodedContent): number {
return JSON.parse(encodedContent.parameters.number) as number
decode(encodedContent: EncodedContent): NumberRef {
if (encodedContent.parameters.test !== 'test') {
throw new Error(`parameters should parse ${encodedContent.parameters}`)
}
const contentReceived = JSON.parse(
new TextDecoder().decode(encodedContent.content)
) as NumberRef
return contentReceived
}

fallback(content: number): string | undefined {
fallback(content: NumberRef): string | undefined {
return 'a billion'
}
}
Expand Down Expand Up @@ -772,20 +785,23 @@ test('register and use custom content types when preparing message', async () =>
const bobConvo = await bob.conversations.newConversation(alice.address)
const aliceConvo = await alice.conversations.newConversation(bob.address)

const prepped = await bobConvo.prepareMessage(12, {
contentType: ContentTypeNumber,
})
const prepped = await bobConvo.prepareMessage(
{ topNumber: { bottomNumber: 12 } },
{
contentType: ContentTypeNumber,
}
)

await bobConvo.sendPreparedMessage(prepped)

const messages = await aliceConvo.messages()
assert(messages.length === 1, 'did not get messages')

const message = messages[0]
const messageContent = message.content()
const messageContent = message.content() as NumberRef

assert(
messageContent === 12,
messageContent.topNumber.bottomNumber === 12,
'did not get content properly: ' + JSON.stringify(messageContent)
)

Expand Down
5 changes: 5 additions & 0 deletions example/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -8458,6 +8458,11 @@ terser@^5.15.0:
commander "^2.20.0"
source-map-support "~0.5.20"

text-encoding@^0.7.0:
version "0.7.0"
resolved "https://registry.yarnpkg.com/text-encoding/-/text-encoding-0.7.0.tgz#f895e836e45990624086601798ea98e8f36ee643"
integrity sha512-oJQ3f1hrOnbRLOcwKz0Liq2IcrvDeZRHXhd9RgLrsT+DjWY/nty1Hi7v3dtkaEYbPYe0mUoOfzRrMwfXXwgPUA==

text-table@^0.2.0:
version "0.2.0"
resolved "https://registry.npmjs.org/text-table/-/text-table-0.2.0.tgz"
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
"dependencies": {
"@msgpack/msgpack": "^3.0.0-beta2",
"@xmtp/proto": "^3.25.0",
"buffer": "^6.0.3",
"ethers": "^5.7.2"
},
"devDependencies": {
Expand Down
4 changes: 4 additions & 0 deletions src/lib/DecodedMessage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
} from './ContentCodec'
import { ReplyCodec } from './NativeCodecs/ReplyCodec'
import { TextCodec } from './NativeCodecs/TextCodec'
import { Buffer } from 'buffer'

export class DecodedMessage<ContentTypes = any> {
client: Client<ContentTypes>
Expand Down Expand Up @@ -91,6 +92,9 @@ export class DecodedMessage<ContentTypes = any> {
`no content type found ${JSON.stringify(this.contentTypeId)}`
)
}
if (encoded.content) {
encoded.content = new Uint8Array(Buffer.from(encoded.content, 'base64'))
}
return codec.decode(encoded)
} else {
for (const codec of Object.values(this.client.codecRegistry)) {
Expand Down

0 comments on commit d5ac040

Please sign in to comment.