|
| 1 | +package org.xmtp.android.library.frames |
| 2 | + |
| 3 | +import android.util.Base64 |
| 4 | +import org.xmtp.android.library.Client |
| 5 | +import org.xmtp.android.library.XMTPException |
| 6 | +import org.xmtp.android.library.frames.FramesConstants.PROTOCOL_VERSION |
| 7 | +import org.xmtp.android.library.messages.PrivateKeyBuilder |
| 8 | +import org.xmtp.android.library.messages.Signature |
| 9 | +import org.xmtp.android.library.messages.getPublicKeyBundle |
| 10 | +import org.xmtp.proto.message.contents.PublicKeyOuterClass.SignedPublicKeyBundle |
| 11 | +import java.security.MessageDigest |
| 12 | +import org.xmtp.proto.message.contents.Frames.FrameActionBody |
| 13 | +import org.xmtp.proto.message.contents.Frames.FrameAction |
| 14 | +import java.util.Date |
| 15 | + |
| 16 | +class FramesClient(private val xmtpClient: Client, var proxy: OpenFramesProxy = OpenFramesProxy()) { |
| 17 | + |
| 18 | + suspend fun signFrameAction(inputs: FrameActionInputs): FramePostPayload { |
| 19 | + val opaqueConversationIdentifier = buildOpaqueIdentifier(inputs) |
| 20 | + val frameUrl = inputs.frameUrl |
| 21 | + val buttonIndex = inputs.buttonIndex |
| 22 | + val inputText = inputs.inputText |
| 23 | + val state = inputs.state |
| 24 | + val now = Date().time * 1_000_000 |
| 25 | + val frameActionBuilder = FrameActionBody.newBuilder().also { frame -> |
| 26 | + frame.frameUrl = frameUrl |
| 27 | + frame.buttonIndex = buttonIndex |
| 28 | + frame.opaqueConversationIdentifier = opaqueConversationIdentifier |
| 29 | + frame.timestamp = now |
| 30 | + frame.unixTimestamp = now.toInt() |
| 31 | + if (inputText != null) { |
| 32 | + frame.inputText = inputText |
| 33 | + } |
| 34 | + if (state != null) { |
| 35 | + frame.state = state |
| 36 | + } |
| 37 | + } |
| 38 | + |
| 39 | + val toSign = frameActionBuilder.build() |
| 40 | + val signedAction = Base64.encodeToString(buildSignedFrameAction(toSign), Base64.NO_WRAP) |
| 41 | + |
| 42 | + val untrustedData = FramePostUntrustedData(frameUrl, now, buttonIndex, inputText, state, xmtpClient.address, opaqueConversationIdentifier, now.toInt()) |
| 43 | + val trustedData = FramePostTrustedData(signedAction) |
| 44 | + |
| 45 | + return FramePostPayload("xmtp@$PROTOCOL_VERSION", untrustedData, trustedData) |
| 46 | + } |
| 47 | + |
| 48 | + private suspend fun signDigest(digest: ByteArray): Signature { |
| 49 | + val signedPrivateKey = xmtpClient.keys.identityKey |
| 50 | + val privateKey = PrivateKeyBuilder.buildFromSignedPrivateKey(signedPrivateKey) |
| 51 | + return PrivateKeyBuilder(privateKey).sign(digest) |
| 52 | + } |
| 53 | + |
| 54 | + private fun getPublicKeyBundle(): SignedPublicKeyBundle { |
| 55 | + return xmtpClient.keys.getPublicKeyBundle() |
| 56 | + } |
| 57 | + |
| 58 | + private suspend fun buildSignedFrameAction(actionBodyInputs: FrameActionBody): ByteArray { |
| 59 | + val digest = sha256(actionBodyInputs.toByteArray()) |
| 60 | + val signature = signDigest(digest) |
| 61 | + |
| 62 | + val publicKeyBundle = getPublicKeyBundle() |
| 63 | + val frameAction = FrameAction.newBuilder().also { |
| 64 | + it.actionBody = actionBodyInputs.toByteString() |
| 65 | + it.signature = signature |
| 66 | + it.signedPublicKeyBundle = publicKeyBundle |
| 67 | + }.build() |
| 68 | + |
| 69 | + return frameAction.toByteArray() |
| 70 | + } |
| 71 | + |
| 72 | + private fun buildOpaqueIdentifier(inputs: FrameActionInputs): String { |
| 73 | + return when (inputs.conversationInputs) { |
| 74 | + is ConversationActionInputs.Group -> { |
| 75 | + val groupInputs = inputs.conversationInputs.inputs |
| 76 | + val combined = groupInputs.groupId + groupInputs.groupSecret |
| 77 | + val digest = sha256(combined) |
| 78 | + Base64.encodeToString(digest, Base64.NO_WRAP) |
| 79 | + } |
| 80 | + is ConversationActionInputs.Dm -> { |
| 81 | + val dmInputs = inputs.conversationInputs.inputs |
| 82 | + val conversationTopic = dmInputs.conversationTopic ?: throw XMTPException("No conversation topic") |
| 83 | + val combined = (conversationTopic.lowercase() + dmInputs.participantAccountAddresses.map { it.lowercase() }.sorted().joinToString("")).toByteArray() |
| 84 | + val digest = sha256(combined) |
| 85 | + Base64.encodeToString(digest, Base64.NO_WRAP) |
| 86 | + } |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + private fun sha256(input: ByteArray): ByteArray { |
| 91 | + val digest = MessageDigest.getInstance("SHA-256") |
| 92 | + return digest.digest(input) |
| 93 | + } |
| 94 | +} |
0 commit comments