From 54a99f0bc1a6f3e8db723e02f4d46294f9fe558c Mon Sep 17 00:00:00 2001 From: xumoyan <13381205621@163.cm> Date: Fri, 25 Aug 2023 16:55:43 +0800 Subject: [PATCH] set ----> OrderSet --- .../xcschemes/WalletConnect-Package.xcscheme | 70 +++++++++++++++++++ Package.resolved | 9 +++ Package.swift | 5 +- .../WalletConnectModal.swift | 3 +- Sources/WalletConnectSign/Namespace.swift | 30 ++++---- .../AutoNamespacesValidationTests.swift | 31 ++++---- 6 files changed, 116 insertions(+), 32 deletions(-) diff --git a/.swiftpm/xcode/xcshareddata/xcschemes/WalletConnect-Package.xcscheme b/.swiftpm/xcode/xcshareddata/xcschemes/WalletConnect-Package.xcscheme index 14b8eeb45..4a1696230 100644 --- a/.swiftpm/xcode/xcshareddata/xcschemes/WalletConnect-Package.xcscheme +++ b/.swiftpm/xcode/xcshareddata/xcschemes/WalletConnect-Package.xcscheme @@ -482,6 +482,76 @@ ReferencedContainer = "container:"> + + + + + + + + + + + + + + + + + + + + = ["eth_sendTransaction", "personal_sign", "eth_signTypedData"] let events: Set = ["chainChanged", "accountsChanged"] - let blockchains: Set = [Blockchain("eip155:1")!] + let blockchains: OrderedSet = [Blockchain("eip155:1")!] let namespaces: [String: ProposalNamespace] = [ "eip155": ProposalNamespace( chains: blockchains, diff --git a/Sources/WalletConnectSign/Namespace.swift b/Sources/WalletConnectSign/Namespace.swift index 6de73e1cc..d64077bb3 100644 --- a/Sources/WalletConnectSign/Namespace.swift +++ b/Sources/WalletConnectSign/Namespace.swift @@ -1,3 +1,5 @@ +import OrderedCollections + public enum AutoNamespacesError: Error { case requiredChainsNotSatisfied case requiredAccountsNotSatisfied @@ -7,11 +9,11 @@ public enum AutoNamespacesError: Error { public struct ProposalNamespace: Equatable, Codable { - public let chains: Set? + public let chains: OrderedSet? public let methods: Set public let events: Set - public init(chains: Set? = nil, methods: Set, events: Set) { + public init(chains: OrderedSet? = nil, methods: Set, events: Set) { self.chains = chains self.methods = methods self.events = events @@ -19,19 +21,19 @@ public struct ProposalNamespace: Equatable, Codable { } public struct SessionNamespace: Equatable, Codable { - public var chains: Set? - public var accounts: Set + public var chains: OrderedSet? + public var accounts: OrderedSet public var methods: Set public var events: Set - public init(chains: Set? = nil, accounts: Set, methods: Set, events: Set) { + public init(chains: OrderedSet? = nil, accounts: OrderedSet, methods: Set, events: Set) { self.chains = chains self.accounts = accounts self.methods = methods self.events = events } - static func accountsAreCompliant(_ accounts: Set, toChains chains: Set) -> Bool { + static func accountsAreCompliant(_ accounts: OrderedSet, toChains chains: OrderedSet) -> Bool { for chain in chains { guard accounts.contains(where: { $0.blockchain == chain }) else { return false @@ -186,8 +188,8 @@ public enum AutoNamespaces { } let sessionNamespace = SessionNamespace( - chains: sessionChains, - accounts: Set(accounts).filter { sessionChains.contains($0.blockchain) }, + chains: OrderedSet(sessionChains), + accounts: OrderedSet(accounts.filter { sessionChains.contains($0.blockchain) }), methods: sessionMethods, events: sessionEvents ) @@ -229,8 +231,8 @@ public enum AutoNamespaces { } let sessionNamespace = SessionNamespace( - chains: Set([Blockchain(namespace: network, reference: chain)!]), - accounts: Set(accounts).filter { $0.blockchain == Blockchain(namespace: network, reference: chain)! }, + chains: OrderedSet([Blockchain(namespace: network, reference: chain)!]), + accounts: OrderedSet(accounts.filter { $0.blockchain == Blockchain(namespace: network, reference: chain)! }), methods: sessionMethods, events: sessionEvents ) @@ -269,8 +271,8 @@ public enum AutoNamespaces { let sessionEvents = Set(proposalNamespace.events).intersection(Set(events)) let sessionNamespace = SessionNamespace( - chains: sessionChains, - accounts: Set(accounts).filter { sessionChains.contains($0.blockchain) }, + chains: OrderedSet(sessionChains), + accounts: OrderedSet(accounts.filter { sessionChains.contains($0.blockchain) }), methods: sessionMethods, events: sessionEvents ) @@ -304,8 +306,8 @@ public enum AutoNamespaces { let sessionEvents = Set(proposalNamespace.events).intersection(Set(events)) let sessionNamespace = SessionNamespace( - chains: Set([Blockchain(namespace: network, reference: chain)!]), - accounts: Set(accounts).filter { $0.blockchain == Blockchain(namespace: network, reference: chain)! }, + chains: OrderedSet([Blockchain(namespace: network, reference: chain)!]), + accounts: OrderedSet(accounts.filter { $0.blockchain == Blockchain(namespace: network, reference: chain)! }), methods: sessionMethods, events: sessionEvents ) diff --git a/Tests/WalletConnectSignTests/AutoNamespacesValidationTests.swift b/Tests/WalletConnectSignTests/AutoNamespacesValidationTests.swift index 5dd2e2d58..16fa731a5 100644 --- a/Tests/WalletConnectSignTests/AutoNamespacesValidationTests.swift +++ b/Tests/WalletConnectSignTests/AutoNamespacesValidationTests.swift @@ -1,5 +1,6 @@ import XCTest @testable import WalletConnectSign +@testable import OrderedCollections final class AutoNamespacesValidationTests: XCTestCase { func testAutoNamespacesSameChainRequiredAndOptional() async { @@ -37,7 +38,7 @@ final class AutoNamespacesValidationTests: XCTestCase { let expectedNamespaces: [String: SessionNamespace] = [ "eip155": SessionNamespace( chains: [Blockchain("eip155:1")!], - accounts: Set(accounts), + accounts: OrderedSet(accounts), methods: ["personal_sign", "eth_sendTransaction"], events: ["chainChanged"] ) @@ -84,7 +85,7 @@ final class AutoNamespacesValidationTests: XCTestCase { let expectedNamespaces: [String: SessionNamespace] = [ "eip155": SessionNamespace( chains: [Blockchain("eip155:1")!, Blockchain("eip155:2")!], - accounts: Set(accounts), + accounts: OrderedSet(accounts), methods: ["personal_sign", "eth_sendTransaction"], events: ["chainChanged"] ) @@ -129,7 +130,7 @@ final class AutoNamespacesValidationTests: XCTestCase { let expectedNamespaces: [String: SessionNamespace] = [ "eip155": SessionNamespace( chains: [Blockchain("eip155:1")!, Blockchain("eip155:2")!], - accounts: Set(accounts), + accounts: OrderedSet(accounts), methods: ["personal_sign", "eth_sendTransaction"], events: ["chainChanged"] ) @@ -179,7 +180,7 @@ final class AutoNamespacesValidationTests: XCTestCase { let expectedNamespaces: [String: SessionNamespace] = [ "eip155": SessionNamespace( chains: [Blockchain("eip155:1")!, Blockchain("eip155:2")!, Blockchain("eip155:3")!], - accounts: Set(accounts), + accounts: OrderedSet(accounts), methods: ["personal_sign", "eth_sendTransaction"], events: ["chainChanged"] ) @@ -234,7 +235,7 @@ final class AutoNamespacesValidationTests: XCTestCase { let expectedNamespaces: [String: SessionNamespace] = [ "eip155": SessionNamespace( chains: [Blockchain("eip155:1")!, Blockchain("eip155:2")!, Blockchain("eip155:3")!, Blockchain("eip155:4")!], - accounts: Set(accounts), + accounts: OrderedSet(accounts), methods: ["personal_sign", "eth_sendTransaction"], events: ["chainChanged"] ) @@ -287,7 +288,7 @@ final class AutoNamespacesValidationTests: XCTestCase { let expectedNamespaces: [String: SessionNamespace] = [ "eip155": SessionNamespace( chains: [Blockchain("eip155:1")!, Blockchain("eip155:2")!], - accounts: Set(accounts), + accounts: OrderedSet(accounts), methods: ["personal_sign", "eth_sendTransaction"], events: ["chainChanged"] ) @@ -341,7 +342,7 @@ final class AutoNamespacesValidationTests: XCTestCase { let expectedNamespaces: [String: SessionNamespace] = [ "eip155": SessionNamespace( chains: [Blockchain("eip155:1")!, Blockchain("eip155:2")!, Blockchain("eip155:4")!], - accounts: Set(accounts), + accounts: OrderedSet(accounts), methods: ["personal_sign", "eth_sendTransaction"], events: ["chainChanged"] ) @@ -390,7 +391,7 @@ final class AutoNamespacesValidationTests: XCTestCase { let expectedNamespaces: [String: SessionNamespace] = [ "eip155": SessionNamespace( chains: [Blockchain("eip155:1")!, Blockchain("eip155:2")!], - accounts: Set(accounts), + accounts: OrderedSet(accounts), methods: ["personal_sign", "eth_sendTransaction", "eth_signTransaction"], events: ["chainChanged"] ) @@ -439,7 +440,7 @@ final class AutoNamespacesValidationTests: XCTestCase { let expectedNamespaces: [String: SessionNamespace] = [ "eip155": SessionNamespace( chains: [Blockchain("eip155:1")!, Blockchain("eip155:2")!], - accounts: Set(accounts), + accounts: OrderedSet(accounts), methods: ["personal_sign", "eth_sendTransaction", "eth_signTransaction"], events: ["chainChanged", "accountsChanged"] ) @@ -544,7 +545,7 @@ final class AutoNamespacesValidationTests: XCTestCase { let expectedNamespaces: [String: SessionNamespace] = [ "eip155": SessionNamespace( chains: [Blockchain("eip155:1")!, Blockchain("eip155:2")!], - accounts: Set([ + accounts: OrderedSet([ Account(blockchain: Blockchain("eip155:1")!, address: "0x57f48fAFeC1d76B27e3f29b8d277b6218CDE6092")!, Account(blockchain: Blockchain("eip155:2")!, address: "0x57f48fAFeC1d76B27e3f29b8d277b6218CDE6092")! ]), @@ -836,7 +837,7 @@ final class AutoNamespacesValidationTests: XCTestCase { let expectedNamespaces: [String: SessionNamespace] = [ "eip155": SessionNamespace( chains: [Blockchain("eip155:1")!], - accounts: Set([ + accounts: OrderedSet([ Account(blockchain: Blockchain("eip155:1")!, address: "0x57f48fAFeC1d76B27e3f29b8d277b6218CDE6092")! ]), methods: ["eth_signTypedData_v4", "eth_signTransaction", "eth_signTypedData", "eth_sign", "personal_sign", "eth_sendTransaction"], @@ -883,7 +884,7 @@ final class AutoNamespacesValidationTests: XCTestCase { let expectedNamespaces: [String: SessionNamespace] = [ "eip155": SessionNamespace( chains: [Blockchain("eip155:1")!], - accounts: Set([ + accounts: OrderedSet([ Account(blockchain: Blockchain("eip155:1")!, address: "0x57f48fAFeC1d76B27e3f29b8d277b6218CDE6092")! ]), methods: ["eth_signTypedData_v4", "eth_signTransaction", "eth_signTypedData", "eth_sign", "personal_sign", "eth_sendTransaction"], @@ -930,7 +931,7 @@ final class AutoNamespacesValidationTests: XCTestCase { let expectedNamespaces: [String: SessionNamespace] = [ "eip155": SessionNamespace( chains: [Blockchain("eip155:1")!], - accounts: Set([ + accounts: OrderedSet([ Account(blockchain: Blockchain("eip155:1")!, address: "0x57f48fAFeC1d76B27e3f29b8d277b6218CDE6092")! ]), methods: ["eth_signTypedData_v4", "eth_signTransaction", "eth_signTypedData", "eth_sign", "personal_sign", "eth_sendTransaction"], @@ -982,7 +983,7 @@ final class AutoNamespacesValidationTests: XCTestCase { let expectedNamespaces: [String: SessionNamespace] = [ "eip155": SessionNamespace( chains: [Blockchain("eip155:1")!], - accounts: Set([ + accounts: OrderedSet([ Account(blockchain: Blockchain("eip155:1")!, address: "0x57f48fAFeC1d76B27e3f29b8d277b6218CDE6092")! ]), methods: ["eth_signTypedData_v4", "eth_signTransaction", "eth_signTypedData", "eth_sign", "personal_sign", "eth_sendTransaction"], @@ -990,7 +991,7 @@ final class AutoNamespacesValidationTests: XCTestCase { ), "solana": SessionNamespace( chains: [Blockchain("solana:4sGjMW1sUnHzSxGspuhpqLDx6wiyjNtZ")!], - accounts: Set([ + accounts: OrderedSet([ Account(blockchain: Blockchain("solana:4sGjMW1sUnHzSxGspuhpqLDx6wiyjNtZ")!, address: "4sGjMW1sUnHzSxGspuhpqLDx6wiyjNtZ")!, ]), methods: ["solana_signMessage"],