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

Closed
wants to merge 10 commits into 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 @@ -602,4 +612,55 @@ 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.registerIdentity(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
}
}
}
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
38 changes: 38 additions & 0 deletions Tests/XMTPTests/ClientTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -455,4 +455,42 @@ 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
)
)
XCTAssertEqual(alix.addresses.count, 1)

let alixWallet2 = try PrivateKey.generate()
try await alix.addWallet(account: alixWallet2)
XCTAssertEqual(alix.addresses.count, 2)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

@neekolas what would be a good thing to test on a client to see if a wallet was added? Is there something I can expose from libxmtp to show me all the wallets attached?

Copy link
Contributor

Choose a reason for hiding this comment

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

The one we already expose is group.members() which will show all the account addresses for each member

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 can for sure use that in the test. But what if I want to see all the addresses I have on my account so I can revoke some? Should we also expose addresses on client?

Copy link
Contributor

@neekolas neekolas Aug 20, 2024

Choose a reason for hiding this comment

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

Easy enough to add. We have all the information in the local DB. Happy to pick that one up, should be a small task.

Would definitely be helpful for revocation, where you need to see all your own account addresses and installations.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

When we talk about revoking a wallet that is different than revoking installations correct? Revoking a wallet would just revoke all the installations for that address?

}

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"
)
)
XCTAssertEqual(alix.addresses.count, 1)

let alixWallet2 = try PrivateKey.generate()
try await alix.addWallet(account: alixWallet2)
XCTAssertEqual(alix.addresses.count, 2)
}

}
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" : "0b2d92a8da6caa2e5f16a2ea060b099278354d74",
"version" : "0.5.7-beta3"
"revision" : "65e9a6eed3db394c1afad7d895b89a0c1cae914b",
"version" : "0.5.7-beta0"
}
},
{
Expand Down
Loading