Skip to content

Commit

Permalink
update the signer
Browse files Browse the repository at this point in the history
  • Loading branch information
nplasterer committed Nov 20, 2024
1 parent 874fe6d commit 4e23584
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 34 deletions.
73 changes: 41 additions & 32 deletions Sources/XMTPiOS/Frames/FramesClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,11 @@ public class FramesClient {
self.proxy = proxy ?? OpenFramesProxy()
}

public func signFrameAction(inputs: FrameActionInputs) async throws -> FramePostPayload {
let opaqueConversationIdentifier = try self.buildOpaqueIdentifier(inputs: inputs)
public func signFrameAction(inputs: FrameActionInputs) async throws
-> FramePostPayload
{
let opaqueConversationIdentifier = try self.buildOpaqueIdentifier(
inputs: inputs)
let frameUrl = inputs.frameUrl
let buttonIndex = inputs.buttonIndex
let inputText = inputs.inputText ?? ""
Expand All @@ -39,55 +42,55 @@ public class FramesClient {
var toSign = FrameActionBody()
toSign.frameURL = frameUrl
toSign.buttonIndex = buttonIndex
toSign.opaqueConversationIdentifier = opaqueConversationIdentifier
toSign.opaqueConversationIdentifier = opaqueConversationIdentifier
toSign.timestamp = UInt64(timestamp)
toSign.inputText = inputText
toSign.unixTimestamp = UInt32(now)
toSign.state = state

let signedAction = try await self.buildSignedFrameAction(actionBodyInputs: toSign)
let signedAction = try await self.buildSignedFrameAction(
actionBodyInputs: toSign)

let untrustedData = FramePostUntrustedData(
url: frameUrl, timestamp: UInt64(now), buttonIndex: buttonIndex, inputText: inputText, state: state, walletAddress: self.xmtpClient.address, opaqueConversationIdentifier: opaqueConversationIdentifier, unixTimestamp: UInt32(now)
url: frameUrl, timestamp: UInt64(now), buttonIndex: buttonIndex,
inputText: inputText, state: state,
walletAddress: self.xmtpClient.address,
opaqueConversationIdentifier: opaqueConversationIdentifier,
unixTimestamp: UInt32(now)
)


let trustedData = FramePostTrustedData(messageBytes: signedAction.base64EncodedString())
let trustedData = FramePostTrustedData(
messageBytes: signedAction.base64EncodedString())

let payload = FramePostPayload(
clientProtocol: "xmtp@\(PROTOCOL_VERSION)", untrustedData: untrustedData, trustedData: trustedData
clientProtocol: "xmtp@\(PROTOCOL_VERSION)",
untrustedData: untrustedData, trustedData: trustedData
)

return payload
}

private func signDigest(digest: Data) async throws -> Signature {
let key = try self.xmtpClient.keys.identityKey
let privateKey = try PrivateKey(key)
let signature = try await privateKey.sign(Data(digest))
return signature

private func signDigest(message: String) async throws -> Data {
return try self.xmtpClient.signWithInstallationKey(message: message)
}

private func getPublicKeyBundle() async throws -> PublicKeyBundle {
let bundleBytes = try self.xmtpClient.publicKeyBundle;
return try PublicKeyBundle(bundleBytes);
}

private func buildSignedFrameAction(actionBodyInputs: FrameActionBody) async throws -> Data {

let digest = sha256(input: try actionBodyInputs.serializedData())
let signature = try await self.signDigest(digest: digest)

let publicKeyBundle = try await self.getPublicKeyBundle()

private func buildSignedFrameAction(actionBodyInputs: FrameActionBody)
async throws -> Data
{
let digest = sha256(input: try actionBodyInputs.serializedData()).toHex
let signature = try await self.signDigest(message: digest)

var frameAction = FrameAction()
frameAction.actionBody = try actionBodyInputs.serializedData()
frameAction.signature = signature
frameAction.signedPublicKeyBundle = try SignedPublicKeyBundle(publicKeyBundle)
frameAction.installationSignature = signature
frameAction.installationID = self.xmtpClient.installationID.hexToData

return try frameAction.serializedData()
}

private func buildOpaqueIdentifier(inputs: FrameActionInputs) throws -> String {

private func buildOpaqueIdentifier(inputs: FrameActionInputs) throws
-> String
{
switch inputs.conversationInputs {
case .group(let groupInputs):
let combined = groupInputs.groupId + groupInputs.groupSecret
Expand All @@ -97,7 +100,13 @@ public class FramesClient {
guard let conversationTopic = dmInputs.conversationTopic else {
throw FramesClientError.missingConversationTopic
}
guard let combined = (conversationTopic.lowercased() + dmInputs.participantAccountAddresses.map { $0.lowercased() }.sorted().joined()).data(using: .utf8) else {
guard
let combined =
(conversationTopic.lowercased()
+ dmInputs.participantAccountAddresses.map {
$0.lowercased()
}.sorted().joined()).data(using: .utf8)
else {
throw FramesClientError.missingConversationTopic
}
let digest = sha256(input: combined)
Expand Down
3 changes: 1 addition & 2 deletions Tests/XMTPTests/FramesTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@ final class FramesTests: XCTestCase {
account: bo,
options: .init(
api: .init(env: .local, isSecure: false),
enableV3: true,
encryptionKey: key
dbEncryptionKey: key
)
)

Expand Down

0 comments on commit 4e23584

Please sign in to comment.