Skip to content

Commit

Permalink
add implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
nplasterer committed Sep 20, 2024
1 parent 2f9cca8 commit 4e43786
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 3 deletions.
27 changes: 26 additions & 1 deletion Sources/XMTPiOS/Client.swift
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,14 @@ public final class Client {
if let signingKey = signingKey {
do {
let signedData = try await signingKey.sign(message: signatureRequest.signatureText())
try await signatureRequest.addEcdsaSignature(signatureBytes: signedData.rawData)
if signingKey.isSmartContractWallet {
guard isValidAccountID(signingKey.address) else {
throw ClientError.creationError("Account address must conform to CAIP format")
}
try await signatureRequest.addScwSignature(signatureBytes: signedData.rawData, address: signingKey.address, chainId: signingKey.chainId, blockNumber: signingKey.blockNumber)
} else {
try await signatureRequest.addEcdsaSignature(signatureBytes: signedData.rawData)
}
try await v3Client.registerIdentity(signatureRequest: signatureRequest)
} catch {
throw ClientError.creationError("Failed to sign the message: \(error.localizedDescription)")
Expand Down Expand Up @@ -696,4 +703,22 @@ public final class Client {
}
return InboxState(ffiInboxState: try await client.inboxState(refreshFromNetwork: refreshFromNetwork))
}

// See for more details https://github.com/ChainAgnostic/CAIPs/blob/main/CAIPs/caip-10.md
public func isValidAccountID(_ accountAddress: String) -> Bool {
// Define the regular expressions for chain_id and account_address
let chainIDPattern = "[-a-z0-9]{3,8}:[-_a-zA-Z0-9]{1,32}"
let accountAddressPattern = "[-.%a-zA-Z0-9]{1,128}"

// Combine them to match the entire account_id format
let accountIDPattern = "^\(chainIDPattern):\(accountAddressPattern)$"

let regex = try? NSRegularExpression(pattern: accountIDPattern)

if let match = regex?.firstMatch(in: accountAddress, options: [], range: NSRange(location: 0, length: accountAddress.utf16.count)) {
return match.range.location != NSNotFound
} else {
return false
}
}
}
11 changes: 9 additions & 2 deletions Sources/XMTPiOS/SigningKey.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,10 @@ public protocol SigningKey {
var isSmartContractWallet: Bool { get }

/// The name of the chainId for example "eip155:1"
var chainId: String { get }
var chainId: UInt64 { get }

/// The blockNumber of the chain for example "1"
var blockNumber: UInt64 { get }

/// Sign the data and return a secp256k1 compact recoverable signature.
func sign(_ data: Data) async throws -> Signature
Expand All @@ -39,7 +42,11 @@ extension SigningKey {
return false
}

public var chainId: String {
public var chainId: UInt64 {
return "eip155:1"
}

public var blockNumber: UInt64 {
return "eip155:1"
}

Expand Down

0 comments on commit 4e43786

Please sign in to comment.