Skip to content

Commit

Permalink
Merge branch 'develop' into #17-engine-responses
Browse files Browse the repository at this point in the history
  • Loading branch information
André Vants committed Feb 10, 2022
2 parents eed465c + 35c010d commit 5e7694a
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 12 deletions.
31 changes: 25 additions & 6 deletions Sources/WalletConnect/Account.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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
}
}

Expand Down
5 changes: 0 additions & 5 deletions Sources/WalletConnect/WalletConnectClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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<Account>) 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
Expand Down
20 changes: 19 additions & 1 deletion Tests/WalletConnectTests/AccountTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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"))
Expand All @@ -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")
Expand Down

0 comments on commit 5e7694a

Please sign in to comment.