-
Notifications
You must be signed in to change notification settings - Fork 180
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
* ClientIdStorage implementation * Storage key changed
- Loading branch information
Showing
6 changed files
with
65 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,27 +1,29 @@ | ||
import Foundation | ||
|
||
protocol KeychainServiceProtocol { | ||
public protocol KeychainServiceProtocol { | ||
func add(_ attributes: CFDictionary, _ result: UnsafeMutablePointer<CFTypeRef?>?) -> OSStatus | ||
func copyMatching(_ query: CFDictionary, _ result: UnsafeMutablePointer<CFTypeRef?>?) -> OSStatus | ||
func update(_ query: CFDictionary, _ attributesToUpdate: CFDictionary) -> OSStatus | ||
func delete(_ query: CFDictionary) -> OSStatus | ||
} | ||
|
||
final class KeychainServiceWrapper: KeychainServiceProtocol { | ||
public final class KeychainServiceWrapper: KeychainServiceProtocol { | ||
|
||
func add(_ attributes: CFDictionary, _ result: UnsafeMutablePointer<CFTypeRef?>?) -> OSStatus { | ||
public init() { } | ||
|
||
public func add(_ attributes: CFDictionary, _ result: UnsafeMutablePointer<CFTypeRef?>?) -> OSStatus { | ||
return SecItemAdd(attributes, result) | ||
} | ||
|
||
func copyMatching(_ query: CFDictionary, _ result: UnsafeMutablePointer<CFTypeRef?>?) -> OSStatus { | ||
public func copyMatching(_ query: CFDictionary, _ result: UnsafeMutablePointer<CFTypeRef?>?) -> OSStatus { | ||
return SecItemCopyMatching(query, result) | ||
} | ||
|
||
func update(_ query: CFDictionary, _ attributesToUpdate: CFDictionary) -> OSStatus { | ||
public func update(_ query: CFDictionary, _ attributesToUpdate: CFDictionary) -> OSStatus { | ||
return SecItemUpdate(query, attributesToUpdate) | ||
} | ||
|
||
func delete(_ query: CFDictionary) -> OSStatus { | ||
public func delete(_ query: CFDictionary) -> OSStatus { | ||
return SecItemDelete(query) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import Foundation | ||
import XCTest | ||
import TestingUtils | ||
import WalletConnectKMS | ||
@testable import WalletConnectRelay | ||
|
||
final class ClientIdStorageTests: XCTestCase { | ||
|
||
func testGetOrCreate() async throws { | ||
let keychain = KeychainStorageMock() | ||
let storage = ClientIdStorage(keychain: keychain) | ||
|
||
XCTAssertThrowsError(try keychain.read(key: "com.walletconnect.iridium.client_id") as SigningPrivateKey) | ||
|
||
let saved = try await storage.getOrCreateKeyPair() | ||
XCTAssertEqual(saved, try keychain.read(key: "com.walletconnect.iridium.client_id")) | ||
|
||
let restored = try await storage.getOrCreateKeyPair() | ||
XCTAssertEqual(saved, restored) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,33 +1,37 @@ | ||
import Foundation | ||
@testable import WalletConnectKMS | ||
|
||
final class KeychainStorageMock: KeychainStorageProtocol { | ||
public final class KeychainStorageMock: KeychainStorageProtocol { | ||
|
||
var storage: [String: Data] = [:] | ||
public var storage: [String: Data] | ||
|
||
private(set) var didCallAdd = false | ||
private(set) var didCallRead = false | ||
private(set) var didCallDelete = false | ||
|
||
func add<T>(_ item: T, forKey key: String) throws where T: GenericPasswordConvertible { | ||
public init(storage: [String: Data] = [:]) { | ||
self.storage = storage | ||
} | ||
|
||
public func add<T>(_ item: T, forKey key: String) throws where T: GenericPasswordConvertible { | ||
didCallAdd = true | ||
storage[key] = item.rawRepresentation | ||
} | ||
|
||
func read<T>(key: String) throws -> T where T: GenericPasswordConvertible { | ||
public func read<T>(key: String) throws -> T where T: GenericPasswordConvertible { | ||
didCallRead = true | ||
if let data = storage[key] { | ||
return try T(rawRepresentation: data) | ||
} | ||
throw KeychainError(errSecItemNotFound) | ||
} | ||
|
||
func delete(key: String) throws { | ||
public func delete(key: String) throws { | ||
didCallDelete = true | ||
storage[key] = nil | ||
} | ||
|
||
func deleteAll() throws { | ||
public func deleteAll() throws { | ||
storage = [:] | ||
} | ||
} |