Skip to content

Commit

Permalink
update the signers
Browse files Browse the repository at this point in the history
  • Loading branch information
nplasterer committed Oct 18, 2024
1 parent d1f509d commit abe6af8
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 19 deletions.
2 changes: 1 addition & 1 deletion Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ let package = Package(
.package(url: "https://github.com/1024jp/GzipSwift", from: "5.2.0"),
.package(url: "https://github.com/bufbuild/connect-swift", exact: "0.12.0"),
.package(url: "https://github.com/apple/swift-docc-plugin.git", from: "1.0.0"),
.package(url: "https://github.com/xmtp/libxmtp-swift.git", exact: "0.5.9-beta2"),
.package(url: "https://github.com/xmtp/libxmtp-swift.git", exact: "0.5.9-beta3"),
],
targets: [
// Targets are the basic building blocks of a package. A target can define a module or a test suite.
Expand Down
57 changes: 43 additions & 14 deletions Sources/XMTPiOS/Client.swift
Original file line number Diff line number Diff line change
Expand Up @@ -161,39 +161,67 @@ public final class Client {
}
}

// This is a V3 only feature
public static func createOrBuild(account: SigningKey, options: ClientOptions) async throws -> Client {
let inboxId = try await getOrCreateInboxId(options: options, address: account.address)

private static func initializeClient(
accountAddress: String,
options: ClientOptions,
signingKey: SigningKey?,
inboxId: String
) async throws -> Client {
let (libxmtpClient, dbPath) = try await initV3Client(
accountAddress: account.address,
accountAddress: accountAddress,
options: options,
privateKeyBundleV1: nil,
signingKey: account,
signingKey: signingKey,
inboxId: inboxId
)

guard let v3Client = libxmtpClient else {
throw ClientError.noV3Client("Error no V3 client initialized")
}

let client = try Client(
address: account.address,
address: accountAddress,
v3Client: v3Client,
dbPath: dbPath,
installationID: v3Client.installationId().toHex,
inboxID: v3Client.inboxId(),
environment: options.api.env
)

let conversations = client.conversations
let contacts = client.contacts

for codec in (options.codecs) {
// Register codecs
for codec in options.codecs {
client.register(codec: codec)
}

return client
}

public static func createV3(account: SigningKey, options: ClientOptions) async throws -> Client {
let accountAddress = if(account.isSmartContractWallet) { "eip155:\(String(describing: account.chainId)):\(account.address.lowercased())" } else { account.address }


let inboxId = try await getOrCreateInboxId(options: options, address: accountAddress)

return try await initializeClient(
accountAddress: accountAddress,
options: options,
signingKey: account,
inboxId: inboxId
)
}

public static func buildV3(address: String, scwChainId: Int64?, options: ClientOptions) async throws -> Client {
let accountAddress = if(scwChainId != nil) { "eip155:\(String(describing: scwChainId)):\(address.lowercased())" } else { address }
let inboxId = try await getOrCreateInboxId(options: options, address: accountAddress)

return try await initializeClient(
accountAddress: accountAddress,
options: options,
signingKey: nil,
inboxId: inboxId
)
}

static func initV3Client(
accountAddress: String,
options: ClientOptions?,
Expand Down Expand Up @@ -224,7 +252,7 @@ public final class Client {
let alias = "xmtp-\(options?.api.env.rawValue ?? "")-\(inboxId).db3"
let dbURL = directoryURL.appendingPathComponent(alias).path

var encryptionKey = options?.dbEncryptionKey
let encryptionKey = options?.dbEncryptionKey
if (encryptionKey == nil) {
throw ClientError.creationError("No encryption key passed for the database. Please store and provide a secure encryption key.")
}
Expand All @@ -246,14 +274,15 @@ public final class Client {
if let signatureRequest = v3Client.signatureRequest() {
if let signingKey = signingKey {
do {
let signedData = try await signingKey.sign(message: signatureRequest.signatureText())
if signingKey.isSmartContractWallet {
try await signatureRequest.addScwSignature(signatureBytes: signedData.rawData,
let signedData = try await signingKey.signSCW(message: signatureRequest.signatureText())
try await signatureRequest.addScwSignature(signatureBytes: signedData,
address: signingKey.address,
chainId: UInt64(signingKey.chainId),
blockNumber: signingKey.blockNumber.flatMap { $0 >= 0 ? UInt64($0) : nil })

} else {
let signedData = try await signingKey.sign(message: signatureRequest.signatureText())
try await signatureRequest.addEcdsaSignature(signatureBytes: signedData.rawData)
}
try await v3Client.registerIdentity(signatureRequest: signatureRequest)
Expand Down
3 changes: 3 additions & 0 deletions Sources/XMTPiOS/SigningKey.swift
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ public protocol SigningKey {
/// Pass a personal Ethereum signed message string text to be signed, returning
/// a secp256k1 compact recoverable signature. You can use ``Signature.ethPersonalMessage`` to generate this text.
func sign(message: String) async throws -> Signature

/// Pass a personal Ethereum signed message string text to be signed, return bytes to be verified
func signSCW(message: String) async throws -> Data
}

extension SigningKey {
Expand Down
4 changes: 2 additions & 2 deletions XMTP.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ Pod::Spec.new do |spec|
#

spec.name = "XMTP"
spec.version = "0.15.1-alpha1"
spec.version = "0.15.1-alpha2"
spec.summary = "XMTP SDK Cocoapod"

# This description is used to generate tags and improve search results.
Expand Down Expand Up @@ -44,5 +44,5 @@ Pod::Spec.new do |spec|
spec.dependency "web3.swift"
spec.dependency "GzipSwift"
spec.dependency "Connect-Swift", "= 0.12.0"
spec.dependency 'LibXMTP', '= 0.5.9-beta2'
spec.dependency 'LibXMTP', '= 0.5.9-beta3'
end
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,8 @@
"kind" : "remoteSourceControl",
"location" : "https://github.com/xmtp/libxmtp-swift.git",
"state" : {
"revision" : "d5d5134ccd177bbad3d74d8ebb6bd2cdd7d2f197",
"version" : "0.5.9-beta2"
"revision" : "a103132088fb9657b03c25006fe6a6686fa4d082",
"version" : "0.5.9-beta3"
}
},
{
Expand Down

0 comments on commit abe6af8

Please sign in to comment.