From d82fdd7b9d21b771ad99befe6e188cf6db87e9f7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Vants?= Date: Wed, 2 Feb 2022 15:52:27 -0300 Subject: [PATCH 1/7] Add account struct --- Sources/WalletConnect/Account.swift | 42 +++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 Sources/WalletConnect/Account.swift diff --git a/Sources/WalletConnect/Account.swift b/Sources/WalletConnect/Account.swift new file mode 100644 index 000000000..b498ec45a --- /dev/null +++ b/Sources/WalletConnect/Account.swift @@ -0,0 +1,42 @@ +/** + + */ +public struct Account { + + /// + public let namespace: String + + /// + public let reference: String + + /// + public let address: String + + /// + public var chainIdentifier: String { + "\(namespace):\(reference)" + } + + /// + public var absoluteString: String { + "\(namespace):\(reference):\(address)" + } + + public var isCAIP10Conformant: Bool { + return String.conformsToCAIP10(absoluteString) + } + + public init(chainNamespace: String, chainReference: String, address: String) { + self.namespace = chainNamespace + self.reference = chainReference + self.address = address + } + + public init?(string: String) { + guard String.conformsToCAIP10(string) else { return nil } + let splits = string.split(separator: ":") + self.namespace = String(splits[0]) + self.reference = String(splits[1]) + self.address = String(splits[2]) + } +} From 47ac3ae5b43bb72e294e10fc3c72b5723f89b429 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Vants?= Date: Wed, 2 Feb 2022 16:04:12 -0300 Subject: [PATCH 2/7] Account type unit tests --- Sources/WalletConnect/Account.swift | 12 +-------- Tests/WalletConnectTests/AccountTests.swift | 27 +++++++++++++++++++++ 2 files changed, 28 insertions(+), 11 deletions(-) create mode 100644 Tests/WalletConnectTests/AccountTests.swift diff --git a/Sources/WalletConnect/Account.swift b/Sources/WalletConnect/Account.swift index b498ec45a..80f1ee8e2 100644 --- a/Sources/WalletConnect/Account.swift +++ b/Sources/WalletConnect/Account.swift @@ -13,7 +13,7 @@ public struct Account { public let address: String /// - public var chainIdentifier: String { + public var blockchainIdentifier: String { "\(namespace):\(reference)" } @@ -22,16 +22,6 @@ public struct Account { "\(namespace):\(reference):\(address)" } - public var isCAIP10Conformant: Bool { - return String.conformsToCAIP10(absoluteString) - } - - public init(chainNamespace: String, chainReference: String, address: String) { - self.namespace = chainNamespace - self.reference = chainReference - self.address = address - } - public init?(string: String) { guard String.conformsToCAIP10(string) else { return nil } let splits = string.split(separator: ":") diff --git a/Tests/WalletConnectTests/AccountTests.swift b/Tests/WalletConnectTests/AccountTests.swift new file mode 100644 index 000000000..7181899ad --- /dev/null +++ b/Tests/WalletConnectTests/AccountTests.swift @@ -0,0 +1,27 @@ +import XCTest +@testable import WalletConnect + +final class AccountTests: XCTestCase { + + func testInit() { + // Valid accounts + XCTAssertNotNil(Account(string: "std:0:0")) + XCTAssertNotNil(Account(string: "chainstd:8c3444cf8970a9e41a706fab93e7a6c4:6d9b0b4b9994e8a6afbd3dc3ed983cd51c755afb27cd1dc7825ef59c134a39f7")) + + // Invalid accounts + XCTAssertNil(Account(string: "std:0:$")) + XCTAssertNil(Account(string: "std:$:0")) + XCTAssertNil(Account(string: "st:0:0")) + } + + func testBlockchainIdentifier() { + let account = Account(string: "eip155:1:0xab16a96d359ec26a11e2c2b3d8f8b8942d5bfcdb") + XCTAssertEqual(account?.blockchainIdentifier, "eip155:1") + } + + func testAbsoluteString() { + let accountString = "eip155:1:0xab16a96d359ec26a11e2c2b3d8f8b8942d5bfcdb" + let account = Account(string: accountString) + XCTAssertEqual(account?.absoluteString, accountString) + } +} From 877482ad7016a159133d2685031599d9a90259e5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Vants?= Date: Wed, 2 Feb 2022 17:20:37 -0300 Subject: [PATCH 3/7] Account type documentation --- Sources/WalletConnect/Account.swift | 36 ++++++++++++++++++++++------- 1 file changed, 28 insertions(+), 8 deletions(-) diff --git a/Sources/WalletConnect/Account.swift b/Sources/WalletConnect/Account.swift index 80f1ee8e2..3e37d1e41 100644 --- a/Sources/WalletConnect/Account.swift +++ b/Sources/WalletConnect/Account.swift @@ -1,27 +1,47 @@ /** + A value that identifies an account in any given blockchain. + This structure parses account IDs according to [CAIP-10]. + Account IDs are prefixed with a [CAIP-2] blockchain ID, delimited by a `':'` character, followed by the account address. + + Specifying a blockchain account by using a chain-agnostic identifier is useful to allow interoperability between multiple + chains when using both wallets and decentralized applications. + + [CAIP-2]:https://github.com/ChainAgnostic/CAIPs/blob/master/CAIPs/caip-2.md + [CAIP-10]:https://github.com/ChainAgnostic/CAIPs/blob/master/CAIPs/caip-10.md */ public struct Account { - /// - public let namespace: String + /// A blockchain namespace. Usually describes an ecosystem or standard. + public var namespace: String - /// - public let reference: String + /// A reference string that identifies a blockchain within a given namespace. + public var reference: String - /// - public let address: String + /// The account's address specific to the blockchain. + public var address: String - /// + /// The CAIP-2 blockchain identifier of the account. public var blockchainIdentifier: String { "\(namespace):\(reference)" } - /// + /// The CAIP-10 account identifier absolute string. public var absoluteString: String { "\(namespace):\(reference):\(address)" } + /// Returns whether the account conforms to CAIP-10. + public var isCAIP10Conformant: Bool { + String.conformsToCAIP10(absoluteString) + } + + /** + Creates an account instance from the provided string. + + This initializer returns nil if the string doesn't represent a valid account id in conformance with + [CAIP-10](https://github.com/ChainAgnostic/CAIPs/blob/master/CAIPs/caip-10.md). + */ public init?(string: String) { guard String.conformsToCAIP10(string) else { return nil } let splits = string.split(separator: ":") From 8545daff75d6e4a773ab5a12b6870100269ab051 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Vants?= Date: Wed, 2 Feb 2022 19:06:33 -0300 Subject: [PATCH 4/7] Add conformance to utility protocols --- Sources/WalletConnect/Account.swift | 27 +++++++++++++++++++-- Tests/WalletConnectTests/AccountTests.swift | 25 ++++++++++++------- 2 files changed, 41 insertions(+), 11 deletions(-) diff --git a/Sources/WalletConnect/Account.swift b/Sources/WalletConnect/Account.swift index 3e37d1e41..c83865116 100644 --- a/Sources/WalletConnect/Account.swift +++ b/Sources/WalletConnect/Account.swift @@ -10,7 +10,7 @@ [CAIP-2]:https://github.com/ChainAgnostic/CAIPs/blob/master/CAIPs/caip-2.md [CAIP-10]:https://github.com/ChainAgnostic/CAIPs/blob/master/CAIPs/caip-10.md */ -public struct Account { +public struct Account: Equatable { /// A blockchain namespace. Usually describes an ecosystem or standard. public var namespace: String @@ -42,7 +42,7 @@ public struct Account { This initializer returns nil if the string doesn't represent a valid account id in conformance with [CAIP-10](https://github.com/ChainAgnostic/CAIPs/blob/master/CAIPs/caip-10.md). */ - public init?(string: String) { + public init?(_ string: String) { guard String.conformsToCAIP10(string) else { return nil } let splits = string.split(separator: ":") self.namespace = String(splits[0]) @@ -50,3 +50,26 @@ public struct Account { self.address = String(splits[2]) } } + +extension Account: LosslessStringConvertible { + public var description: String { + return absoluteString + } +} + +extension Account: Codable { + + public init(from decoder: Decoder) throws { + let container = try decoder.singleValueContainer() + let absoluteString = try container.decode(String.self) + guard let account = Account(absoluteString) else { + throw DecodingError.dataCorruptedError(in: container, debugDescription: "Malformed CAIP-10 account identifier.") + } + self = account + } + + public func encode(to encoder: Encoder) throws { + var container = encoder.singleValueContainer() + try container.encode(absoluteString) + } +} diff --git a/Tests/WalletConnectTests/AccountTests.swift b/Tests/WalletConnectTests/AccountTests.swift index 7181899ad..528a0e71d 100644 --- a/Tests/WalletConnectTests/AccountTests.swift +++ b/Tests/WalletConnectTests/AccountTests.swift @@ -5,23 +5,30 @@ final class AccountTests: XCTestCase { func testInit() { // Valid accounts - XCTAssertNotNil(Account(string: "std:0:0")) - XCTAssertNotNil(Account(string: "chainstd:8c3444cf8970a9e41a706fab93e7a6c4:6d9b0b4b9994e8a6afbd3dc3ed983cd51c755afb27cd1dc7825ef59c134a39f7")) + XCTAssertNotNil(Account("std:0:0")) + XCTAssertNotNil(Account("chainstd:8c3444cf8970a9e41a706fab93e7a6c4:6d9b0b4b9994e8a6afbd3dc3ed983cd51c755afb27cd1dc7825ef59c134a39f7")) // Invalid accounts - XCTAssertNil(Account(string: "std:0:$")) - XCTAssertNil(Account(string: "std:$:0")) - XCTAssertNil(Account(string: "st:0:0")) + XCTAssertNil(Account("std:0:$")) + XCTAssertNil(Account("std:$:0")) + XCTAssertNil(Account("st:0:0")) } func testBlockchainIdentifier() { - let account = Account(string: "eip155:1:0xab16a96d359ec26a11e2c2b3d8f8b8942d5bfcdb") - XCTAssertEqual(account?.blockchainIdentifier, "eip155:1") + let account = Account("eip155:1:0xab16a96d359ec26a11e2c2b3d8f8b8942d5bfcdb")! + XCTAssertEqual(account.blockchainIdentifier, "eip155:1") } func testAbsoluteString() { let accountString = "eip155:1:0xab16a96d359ec26a11e2c2b3d8f8b8942d5bfcdb" - let account = Account(string: accountString) - XCTAssertEqual(account?.absoluteString, accountString) + let account = Account(accountString)! + XCTAssertEqual(account.absoluteString, accountString) + } + + func testCodable() throws { + let account = Account("eip155:1:0xab16a96d359ec26a11e2c2b3d8f8b8942d5bfcdb")! + let encoded = try JSONEncoder().encode(account) + let decoded = try JSONDecoder().decode(Account.self, from: encoded) + XCTAssertEqual(account, decoded) } } From 6c254a0990f13042efbb1d6d3b8cba23c0218ea6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Vants?= Date: Thu, 3 Feb 2022 23:02:21 -0300 Subject: [PATCH 5/7] Replaced update calls string params with account type --- Example/DApp/ClientDelegate.swift | 2 +- .../Responder/ResponderViewController.swift | 6 +++--- Sources/WalletConnect/Account.swift | 2 +- .../WalletConnect/WalletConnectClient.swift | 19 ++++++++++--------- .../WalletConnectClientDelegate.swift | 2 +- Tests/IntegrationTests/ClientDelegate.swift | 4 ++-- Tests/IntegrationTests/ClientTest.swift | 10 +++++----- 7 files changed, 23 insertions(+), 22 deletions(-) diff --git a/Example/DApp/ClientDelegate.swift b/Example/DApp/ClientDelegate.swift index 2fecc1d15..f64f674dd 100644 --- a/Example/DApp/ClientDelegate.swift +++ b/Example/DApp/ClientDelegate.swift @@ -34,7 +34,7 @@ class ClientDelegate: WalletConnectClientDelegate { onSessionResponse?(sessionResponse) } - func didUpdate(sessionTopic: String, accounts: Set) { + func didUpdate(sessionTopic: String, accounts: Set) { } func didUpgrade(sessionTopic: String, permissions: Session.Permissions) { diff --git a/Example/ExampleApp/Responder/ResponderViewController.swift b/Example/ExampleApp/Responder/ResponderViewController.swift index c5e662ca6..7b8bf90c0 100644 --- a/Example/ExampleApp/Responder/ResponderViewController.swift +++ b/Example/ExampleApp/Responder/ResponderViewController.swift @@ -151,8 +151,8 @@ extension ResponderViewController: SessionViewControllerDelegate { print("[RESPONDER] Approving session...") let proposal = currentProposal! currentProposal = nil - let accounts = proposal.permissions.blockchains.map {$0+":\(account)"} - client.approve(proposal: proposal, accounts: Set(accounts)) + let accounts = Set(proposal.permissions.blockchains.compactMap { Account($0+":\(account)") }) + client.approve(proposal: proposal, accounts: accounts) } func didRejectSession() { @@ -197,7 +197,7 @@ extension ResponderViewController: WalletConnectClientDelegate { } - func didUpdate(sessionTopic: String, accounts: Set) { + func didUpdate(sessionTopic: String, accounts: Set) { } diff --git a/Sources/WalletConnect/Account.swift b/Sources/WalletConnect/Account.swift index c83865116..ff258dea5 100644 --- a/Sources/WalletConnect/Account.swift +++ b/Sources/WalletConnect/Account.swift @@ -10,7 +10,7 @@ [CAIP-2]:https://github.com/ChainAgnostic/CAIPs/blob/master/CAIPs/caip-2.md [CAIP-10]:https://github.com/ChainAgnostic/CAIPs/blob/master/CAIPs/caip-10.md */ -public struct Account: Equatable { +public struct Account: Equatable, Hashable { /// A blockchain namespace. Usually describes an ecosystem or standard. public var namespace: String diff --git a/Sources/WalletConnect/WalletConnectClient.swift b/Sources/WalletConnect/WalletConnectClient.swift index ddb239ff8..786c1e78c 100644 --- a/Sources/WalletConnect/WalletConnectClient.swift +++ b/Sources/WalletConnect/WalletConnectClient.swift @@ -140,8 +140,8 @@ public final class WalletConnectClient { /// - Parameters: /// - proposal: Session Proposal received from peer client in a WalletConnect delegate function: `didReceive(sessionProposal: Session.Proposal)` /// - accounts: A Set of accounts that the dapp will be allowed to request methods executions on. - public func approve(proposal: Session.Proposal, accounts: Set) { - sessionEngine.approve(proposal: proposal.proposal, accounts: accounts) + public func approve(proposal: Session.Proposal, accounts: Set) { + sessionEngine.approve(proposal: proposal.proposal, accounts: Set(accounts.map { $0.absoluteString })) } /// For the responder to reject a session proposal. @@ -156,12 +156,13 @@ public final class WalletConnectClient { /// - Parameters: /// - topic: Topic of the session that is intended to be updated. /// - accounts: Set of accounts that will be allowed to be used by the session after the update. - public func update(topic: String, accounts: Set) { - do { - try sessionEngine.update(topic: topic, accounts: accounts) - } catch { - print("Error on session update call: \(error)") - } + public func update(topic: String, accounts: Set) throws { + try sessionEngine.update(topic: topic, accounts: Set(accounts.map { $0.absoluteString })) +// do { +// try sessionEngine.update(topic: topic, accounts: accounts) +// } catch { +// print("Error on session update call: \(error)") +// } } /// For the responder to upgrade session permissions @@ -308,7 +309,7 @@ public final class WalletConnectClient { delegate?.didUpgrade(sessionTopic: topic, permissions: upgradedPermissions) } sessionEngine.onSessionUpdate = { [unowned self] topic, accounts in - delegate?.didUpdate(sessionTopic: topic, accounts: accounts) + delegate?.didUpdate(sessionTopic: topic, accounts: Set(accounts.compactMap { Account($0) })) } sessionEngine.onNotificationReceived = { [unowned self] topic, notification in delegate?.didReceive(notification: notification, sessionTopic: topic) diff --git a/Sources/WalletConnect/WalletConnectClientDelegate.swift b/Sources/WalletConnect/WalletConnectClientDelegate.swift index a9df37c0a..b235d0320 100644 --- a/Sources/WalletConnect/WalletConnectClientDelegate.swift +++ b/Sources/WalletConnect/WalletConnectClientDelegate.swift @@ -36,7 +36,7 @@ public protocol WalletConnectClientDelegate: AnyObject { /// Tells the delegate that extra accounts has been included in session sequence /// /// Function is executed on controller and non-controller client when both communicating peers have successfully included new accounts requested by the controller client. - func didUpdate(sessionTopic: String, accounts: Set) + func didUpdate(sessionTopic: String, accounts: Set) /// Tells the delegate that the client has settled a session. /// diff --git a/Tests/IntegrationTests/ClientDelegate.swift b/Tests/IntegrationTests/ClientDelegate.swift index f5eacaeb0..523beb6f5 100644 --- a/Tests/IntegrationTests/ClientDelegate.swift +++ b/Tests/IntegrationTests/ClientDelegate.swift @@ -12,7 +12,7 @@ class ClientDelegate: WalletConnectClientDelegate { var onSessionRejected: ((String, Reason)->())? var onSessionDelete: (()->())? var onSessionUpgrade: ((String, Session.Permissions)->())? - var onSessionUpdate: ((String, Set)->())? + var onSessionUpdate: ((String, Set)->())? var onNotificationReceived: ((Session.Notification, String)->())? var onPairingUpdate: ((String, AppMetadata)->())? @@ -42,7 +42,7 @@ class ClientDelegate: WalletConnectClientDelegate { func didUpgrade(sessionTopic: String, permissions: Session.Permissions) { onSessionUpgrade?(sessionTopic, permissions) } - func didUpdate(sessionTopic: String, accounts: Set) { + func didUpdate(sessionTopic: String, accounts: Set) { onSessionUpdate?(sessionTopic, accounts) } func didReceive(notification: Session.Notification, sessionTopic: String) { diff --git a/Tests/IntegrationTests/ClientTest.swift b/Tests/IntegrationTests/ClientTest.swift index ecf9a5a83..4d348b7b2 100644 --- a/Tests/IntegrationTests/ClientTest.swift +++ b/Tests/IntegrationTests/ClientTest.swift @@ -57,7 +57,7 @@ final class ClientTests: XCTestCase { func testNewSession() { let proposerSettlesSessionExpectation = expectation(description: "Proposer settles session") let responderSettlesSessionExpectation = expectation(description: "Responder settles session") - let account = "0x022c0c42a80bd19EA4cF0F94c4F9F96645759716" + let account = Account("eip155:1:0xab16a96d359ec26a11e2c2b3d8f8b8942d5bfcdb")! let permissions = Session.Permissions.stub() let uri = try! proposer.client.connect(sessionPermissions: permissions)! @@ -245,7 +245,7 @@ final class ClientTests: XCTestCase { func testSuccessfulSessionUpgrade() { let proposerSessionUpgradeExpectation = expectation(description: "Proposer upgrades session on responder request") let responderSessionUpgradeExpectation = expectation(description: "Responder upgrades session on proposer response") - let account = "0x022c0c42a80bd19EA4cF0F94c4F9F96645759716" + let account = Account("eip155:1:0xab16a96d359ec26a11e2c2b3d8f8b8942d5bfcdb")! let permissions = Session.Permissions.stub() let upgradePermissions = Session.Permissions(blockchains: ["eip155:42"], methods: ["eth_sendTransaction"]) let uri = try! proposer.client.connect(sessionPermissions: permissions)! @@ -272,8 +272,8 @@ final class ClientTests: XCTestCase { func testSuccessfulSessionUpdate() { let proposerSessionUpdateExpectation = expectation(description: "Proposer updates session on responder request") let responderSessionUpdateExpectation = expectation(description: "Responder updates session on proposer response") - let account = "eip155:42:0x022c0c42a80bd19EA4cF0F94c4F9F96645759716" - let updateAccounts: Set = ["eip155:1:0xab16a96d359ec26a11e2c2b3d8f8b8942d5bfcdb"] + let account = Account("eip155:1:0xab16a96d359ec26a11e2c2b3d8f8b8942d5bfcdb")! + let updateAccounts: Set = [Account("eip155:1:0xab16a96d359ec26a11e2c2b3d8f8b8942d5bfcdf")!] let permissions = Session.Permissions.stub() let uri = try! proposer.client.connect(sessionPermissions: permissions)! try! responder.client.pair(uri: uri) @@ -281,7 +281,7 @@ final class ClientTests: XCTestCase { self.responder.client.approve(proposal: proposal, accounts: [account]) } responder.onSessionSettled = { [unowned self] sessionSettled in - responder.client.update(topic: sessionSettled.topic, accounts: updateAccounts) + try? responder.client.update(topic: sessionSettled.topic, accounts: updateAccounts) } responder.onSessionUpdate = { _, accounts in XCTAssertEqual(accounts, updateAccounts) From 36621567f0af942d9331cf918ea785c23e83cfb5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Vants?= Date: Wed, 9 Feb 2022 18:34:47 -0300 Subject: [PATCH 6/7] Removed unused commented code --- Sources/WalletConnect/WalletConnectClient.swift | 5 ----- 1 file changed, 5 deletions(-) diff --git a/Sources/WalletConnect/WalletConnectClient.swift b/Sources/WalletConnect/WalletConnectClient.swift index 6b36b3121..be8d586ba 100644 --- a/Sources/WalletConnect/WalletConnectClient.swift +++ b/Sources/WalletConnect/WalletConnectClient.swift @@ -155,11 +155,6 @@ public final class WalletConnectClient { /// - accounts: Set of accounts that will be allowed to be used by the session after the update. public func update(topic: String, accounts: Set) throws { try sessionEngine.update(topic: topic, accounts: Set(accounts.map { $0.absoluteString })) -// do { -// try sessionEngine.update(topic: topic, accounts: accounts) -// } catch { -// print("Error on session update call: \(error)") -// } } /// For the responder to upgrade session permissions From dc241adbf0e7c645b00836de25c47f7fa180795f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Vants?= Date: Thu, 10 Feb 2022 15:00:19 -0300 Subject: [PATCH 7/7] New alternate initializers for Account type --- Sources/WalletConnect/Account.swift | 31 +++++++++++++++++---- Tests/WalletConnectTests/AccountTests.swift | 20 ++++++++++++- 2 files changed, 44 insertions(+), 7 deletions(-) diff --git a/Sources/WalletConnect/Account.swift b/Sources/WalletConnect/Account.swift index ff258dea5..60dfcd1aa 100644 --- a/Sources/WalletConnect/Account.swift +++ b/Sources/WalletConnect/Account.swift @@ -13,13 +13,13 @@ public struct Account: Equatable, Hashable { /// A blockchain namespace. Usually describes an ecosystem or standard. - public var namespace: String + public let namespace: String /// A reference string that identifies a blockchain within a given namespace. - public var reference: String + public let reference: String /// The account's address specific to the blockchain. - public var address: String + public let address: String /// The CAIP-2 blockchain identifier of the account. public var blockchainIdentifier: String { @@ -45,9 +45,28 @@ public struct Account: Equatable, Hashable { public init?(_ string: String) { guard String.conformsToCAIP10(string) else { return nil } let splits = string.split(separator: ":") - self.namespace = String(splits[0]) - self.reference = String(splits[1]) - self.address = String(splits[2]) + self.init(namespace: String(splits[0]), reference: String(splits[1]), address: String(splits[2])) + } + + /** + Creates an account instance from a chain ID and an address. + + This initializer returns nil if the `chainIdentifier` parameter doesn't represent a valid chain id in conformance with + [CAIP-2](https://github.com/ChainAgnostic/CAIPs/blob/master/CAIPs/caip-2.md) or if the `address` format is invalid. + */ + public init?(chainIdentifier: String, address: String) { + self.init("\(chainIdentifier):\(address)") + } + + /** + Creates an account instance directly from the base components. + + This initializer bypass any checks to CAIP conformance, make sure to pass valid values as parameters. + */ + public init(namespace: String, reference: String, address: String) { + self.namespace = namespace + self.reference = reference + self.address = address } } diff --git a/Tests/WalletConnectTests/AccountTests.swift b/Tests/WalletConnectTests/AccountTests.swift index 528a0e71d..006cf9df4 100644 --- a/Tests/WalletConnectTests/AccountTests.swift +++ b/Tests/WalletConnectTests/AccountTests.swift @@ -3,7 +3,7 @@ import XCTest final class AccountTests: XCTestCase { - func testInit() { + func testInitFromString() { // Valid accounts XCTAssertNotNil(Account("std:0:0")) XCTAssertNotNil(Account("chainstd:8c3444cf8970a9e41a706fab93e7a6c4:6d9b0b4b9994e8a6afbd3dc3ed983cd51c755afb27cd1dc7825ef59c134a39f7")) @@ -14,6 +14,24 @@ final class AccountTests: XCTestCase { XCTAssertNil(Account("st:0:0")) } + func testInitFromChainAndAddress() { + // Valid accounts + XCTAssertNotNil(Account(chainIdentifier: "std:0", address: "0")) + XCTAssertNotNil(Account(chainIdentifier: "chainstd:8c3444cf8970a9e41a706fab93e7a6c4", address: "6d9b0b4b9994e8a6afbd3dc3ed983cd51c755afb27cd1dc7825ef59c134a39f7")) + + // Invalid accounts + XCTAssertNil(Account(chainIdentifier: "std:0", address: "")) + XCTAssertNil(Account(chainIdentifier: "std", address: "0")) + } + + func testInitCAIP10Conformance() { + XCTAssertTrue(Account(namespace: "std", reference: "0", address: "0").isCAIP10Conformant) + + XCTAssertFalse(Account(namespace: "st", reference: "0", address: "0").isCAIP10Conformant) + XCTAssertFalse(Account(namespace: "std", reference: "", address: "0").isCAIP10Conformant) + XCTAssertFalse(Account(namespace: "std", reference: "0", address: "").isCAIP10Conformant) + } + func testBlockchainIdentifier() { let account = Account("eip155:1:0xab16a96d359ec26a11e2c2b3d8f8b8942d5bfcdb")! XCTAssertEqual(account.blockchainIdentifier, "eip155:1")