Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add additional wallets to a client #383

Open
wants to merge 10 commits into
base: main
Choose a base branch
from
69 changes: 65 additions & 4 deletions Sources/XMTPiOS/Client.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,17 @@ public typealias PreEventCallback = () async throws -> Void
public enum ClientError: Error, CustomStringConvertible, LocalizedError {
case creationError(String)
case noV3Client(String)
case addWalletError(String)


public var description: String {
switch self {
case .creationError(let err):
return "ClientError.creationError: \(err)"
case .noV3Client(let err):
return "ClientError.noV3Client: \(err)"
case .addWalletError(let err):
return "ClientError.addWalletError: \(err)"
}
}

Expand Down Expand Up @@ -65,6 +69,8 @@ public struct ClientOptions {
public var dbEncryptionKey: Data?
public var dbDirectory: String?
public var historySyncUrl: String?
public var chainRPCUrl: String?


public init(
api: Api = Api(),
Expand All @@ -75,7 +81,8 @@ public struct ClientOptions {
enableV3: Bool = false,
encryptionKey: Data? = nil,
dbDirectory: String? = nil,
historySyncUrl: String? = nil
historySyncUrl: String? = nil,
chainRPCUrl: String? = nil
) {
self.api = api
self.codecs = codecs
Expand All @@ -85,6 +92,7 @@ public struct ClientOptions {
self.enableV3 = enableV3
self.dbEncryptionKey = encryptionKey
self.dbDirectory = dbDirectory
self.chainRPCUrl = chainRPCUrl
if (historySyncUrl == nil) {
switch api.env {
case .production:
Expand Down Expand Up @@ -118,6 +126,7 @@ public final class Client {
public let dbPath: String
public let installationID: String
public let inboxID: String
public let chainRPCUrl: String

/// Access ``Conversations`` for this Client.
public lazy var conversations: Conversations = .init(client: self)
Expand Down Expand Up @@ -241,7 +250,7 @@ public final class Client {
inboxId: inboxId
)

let client = try Client(address: account.address, privateKeyBundleV1: privateKeyBundleV1, apiClient: apiClient, v3Client: v3Client, dbPath: dbPath, installationID: v3Client?.installationId().toHex ?? "", inboxID: v3Client?.inboxId() ?? inboxId)
let client = try Client(address: account.address, privateKeyBundleV1: privateKeyBundleV1, apiClient: apiClient, v3Client: v3Client, dbPath: dbPath, installationID: v3Client?.installationId().toHex ?? "", inboxID: v3Client?.inboxId() ?? inboxId, chainRPCUrl: options?.chainRPCUrl ?? "")
let conversations = client.conversations
let contacts = client.contacts
try await client.ensureUserContactPublished()
Expand Down Expand Up @@ -359,7 +368,7 @@ public final class Client {
rustClient: client
)

let result = try Client(address: address, privateKeyBundleV1: v1Bundle, apiClient: apiClient, v3Client: v3Client, dbPath: dbPath, installationID: v3Client?.installationId().toHex ?? "", inboxID: v3Client?.inboxId() ?? inboxId)
let result = try Client(address: address, privateKeyBundleV1: v1Bundle, apiClient: apiClient, v3Client: v3Client, dbPath: dbPath, installationID: v3Client?.installationId().toHex ?? "", inboxID: v3Client?.inboxId() ?? inboxId, chainRPCUrl: options.chainRPCUrl ?? "")
let conversations = result.conversations
let contacts = result.contacts
for codec in options.codecs {
Expand All @@ -369,14 +378,15 @@ public final class Client {
return result
}

init(address: String, privateKeyBundleV1: PrivateKeyBundleV1, apiClient: ApiClient, v3Client: LibXMTP.FfiXmtpClient?, dbPath: String = "", installationID: String, inboxID: String) throws {
init(address: String, privateKeyBundleV1: PrivateKeyBundleV1, apiClient: ApiClient, v3Client: LibXMTP.FfiXmtpClient?, dbPath: String = "", installationID: String, inboxID: String, chainRPCUrl: String) throws {
self.address = address
self.privateKeyBundleV1 = privateKeyBundleV1
self.apiClient = apiClient
self.v3Client = v3Client
self.dbPath = dbPath
self.installationID = installationID
self.inboxID = inboxID
self.chainRPCUrl = chainRPCUrl
}

public var privateKeyBundle: PrivateKeyBundle {
Expand Down Expand Up @@ -604,6 +614,57 @@ public final class Client {
try await client.requestHistorySync()
}

public func addWallet(account: SigningKey) async throws {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

After adding the wallet to their identity what benefits do they get/ how can the interact with the wallet?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This would allow someone else to enter any one of the added wallet addresses and reach the same inbox.

And for an app that has access to any of the added wallets to create a new installation for that inbox.

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

do {
let signatureRequest = try await client.addWallet(existingWalletAddress: self.address, newWalletAddress: account.address)

let signedData = try await account.sign(message: signatureRequest.signatureText())

if account.isSmartContractWallet {
guard !chainRPCUrl.isEmpty else {
throw ClientError.addWalletError("ChainRPCUrl required to add smart contract wallet")
}
guard isValidAccountID(account.address) else {
throw ClientError.addWalletError("Account address must conform to CAIP format")
}

try await signatureRequest.addScwSignature(
signatureBytes: signedData.rawData,
address: account.address,
chainRpcUrl: chainRPCUrl
)
} else {
try await signatureRequest.addEcdsaSignature(signatureBytes: signedData.rawData)
}

try await client.applySignatureRequest(signatureRequest: signatureRequest)
} catch {
throw ClientError.addWalletError("Failed to sign the message: \(error.localizedDescription)")
}
}

// See for more details https://github.com/ChainAgnostic/CAIPs/blob/main/CAIPs/caip-10.md
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
}
}

public func revokeAllOtherInstallations(signingKey: SigningKey) async throws {
guard let client = v3Client else {
throw ClientError.noV3Client("Error: No V3 client initialized")
Expand Down
2 changes: 1 addition & 1 deletion Sources/XMTPiOS/Messages/PrivateKey.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ enum PrivateKeyError: Error, CustomStringConvertible {
}
}

extension PrivateKey: SigningKey {
extension PrivateKey: SigningKey {
public var address: String {
walletAddress
}
Expand Down
7 changes: 7 additions & 0 deletions Sources/XMTPiOS/SigningKey.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ import LibXMTP
public protocol SigningKey {
/// A wallet address for this key
var address: String { get }

/// If this signing key is a smart contract wallet
var isSmartContractWallet: Bool { get }
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also pass a chainId


/// Sign the data and return a secp256k1 compact recoverable signature.
func sign(_ data: Data) async throws -> Signature
Expand All @@ -29,6 +32,10 @@ public protocol SigningKey {
}

extension SigningKey {
public var isSmartContractWallet: Bool {
return false
}

func createIdentity(_ identity: PrivateKey, preCreateIdentityCallback: PreEventCallback? = nil) async throws -> AuthorizedIdentity {
var slimKey = PublicKey()
slimKey.timestamp = UInt64(Date().millisecondsSinceEpoch)
Expand Down
75 changes: 75 additions & 0 deletions Tests/XMTPTests/ClientTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -456,6 +456,81 @@ class ClientTests: XCTestCase {
XCTAssertEqual(inboxId, alixClient.inboxID)
}

func testAddAdditionalEOAWallets() async throws {
let key = try Crypto.secureRandomBytes(count: 32)
let alixWallet1 = try PrivateKey.generate()
let alix = try await Client.create(
account: alixWallet1,
options: .init(
api: .init(env: .local, isSecure: false),
enableV3: true,
encryptionKey: key
)
)

let group = try await alix.conversations.newGroup(with: [])
try await group.sync()
XCTAssertEqual(try group.members[0].addresses.count, 1)

let alixWallet2 = try PrivateKey.generate()
try await alix.addWallet(account: alixWallet2)
try await group.sync()
XCTAssertEqual(try group.members[0].addresses.count, 2)
}

func testAddAdditionalSCWWallets() async throws {
let key = try Crypto.secureRandomBytes(count: 32)
let alixWallet1 = try PrivateKey.generate()
let alix = try await Client.create(
account: alixWallet1,
options: .init(
api: .init(env: .local, isSecure: false),
enableV3: true,
encryptionKey: key,
chainRPCUrl: "https://mainnet.infura.io/v3/YOUR_INFURA_PROJECT_ID"
)
)
let group = try await alix.conversations.newGroup(with: [])
try await group.sync()
XCTAssertEqual(try group.members[0].addresses.count, 1)

let alixWallet2 = try FakeSCWWallet.generate()
try await alix.addWallet(account: alixWallet2)
try await group.sync()
XCTAssertEqual(try group.members[0].addresses.count, 2)
}

public struct FakeSCWWallet: SigningKey {
public static func generate() throws -> FakeWallet {
let key = try PrivateKey.generate()
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't know if there is something simple like this for generating a test SCW... seems unlikely. But I can atleast test the account format and isSmartContractWallet.

return FakeWallet(key)
}

public var address: String {
"eip155:1:\(key.walletAddress)"
}

public var isSmartContractWallet: Bool {
true
}

public func sign(_ data: Data) async throws -> XMTPiOS.Signature {
let signature = try await key.sign(data)
return signature
}

public func sign(message: String) async throws -> XMTPiOS.Signature {
let signature = try await key.sign(message: message)
return signature
}

public var key: PrivateKey

public init(_ key: PrivateKey) {
self.key = key
}
}

func testRevokesAllOtherInstallations() async throws {
let key = try Crypto.secureRandomBytes(count: 32)
let alix = try PrivateKey.generate()
Expand Down
Loading