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

Refactor Account create/import APIs #282

Merged
merged 3 commits into from
Nov 28, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,14 @@ $ pod install

### Getting Started

Create an instance of `EthereumAccount` with a `EthereumKeyStorage` provider. This provides a wrapper around your key for web3.swift to use. **NOTE** We recommend you implement your own KeyStorage provider, instead of relying on the provided `EthereumKeyLocalStorage` class. This is provided as an example for conformity to the `EthereumKeyStorageProtocol`.
Create an instance of `EthereumAccount` with a `EthereumKeyStorage` provider. This provides a wrapper around your key for web3.swift to use. **NOTE We recommend you implement your own KeyStorage provider, instead of relying on the provided `EthereumKeyLocalStorage` class. This is provided as an example for conformity to the `EthereumSingleKeyStorageProtocol`.**

```swift
import web3


// This is just an example. EthereumKeyLocalStorage should not be used in production code
let keyStorage = EthereumKeyLocalStorage()
let account = try? EthereumAccount.create(keyStorage: keyStorage, keystorePassword: "MY_PASSWORD")
let account = try? EthereumAccount.create(replacing: keyStorage, keystorePassword: "MY_PASSWORD")
```

Create an instance of `EthereumHttpClient` or `EthereumWebSocketClient`. This will then provide you access to a set of functions for interacting with the Blockchain.
Expand Down
24 changes: 12 additions & 12 deletions web3sTests/Account/EthereumAccountTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,41 +27,41 @@ class EthereumAccountTests: XCTestCase {
}

func testCreateAccount() {
let storage = EthereumKeyLocalStorage() as EthereumKeyStorageProtocol
let account = try? EthereumAccount.create(keyStorage: storage, keystorePassword: "PASSWORD")
let storage = EthereumKeyLocalStorage()
let account = try? EthereumAccount.create(replacing: storage, keystorePassword: "PASSWORD")
XCTAssertNotNil(account, "Failed to create account. Ensure key is valid in TestConfig.swift")
}

func testCreateAccountMultiple() {
let storage = EthereumKeyLocalStorage() as EthereumMultipleKeyStorageProtocol
let account = try? EthereumAccount.create(keyStorage: storage, keystorePassword: "PASSWORD")
let storage = EthereumKeyLocalStorage()
let account = try? EthereumAccount.create(addingTo: storage, keystorePassword: "PASSWORD")
XCTAssertNotNil(account, "Failed to create account. Ensure key is valid in TestConfig.swift")
}

func testImportAccount() {
let storage = EthereumKeyLocalStorage() as EthereumKeyStorageProtocol
let account = try! EthereumAccount.importAccount(keyStorage: storage, privateKey: "0x2639f727ded571d584643895d43d02a7a190f8249748a2c32200cfc12dde7173", keystorePassword: "PASSWORD")
let storage = EthereumKeyLocalStorage()
let account = try! EthereumAccount.importAccount(replacing: storage, privateKey: "0x2639f727ded571d584643895d43d02a7a190f8249748a2c32200cfc12dde7173", keystorePassword: "PASSWORD")

XCTAssertEqual(account.address.value, "0x675f5810feb3b09528e5cd175061b4eb8de69075")
}

func testImportAccountMultiple() {
let storage = EthereumKeyLocalStorage() as EthereumMultipleKeyStorageProtocol
let account = try! EthereumAccount.importAccount(keyStorage: storage, privateKey: "0x2639f727ded571d584643895d43d02a7a190f8249748a2c32200cfc12dde7173", keystorePassword: "PASSWORD")
let storage = EthereumKeyLocalStorage()
let account = try! EthereumAccount.importAccount(addingTo: storage, privateKey: "0x2639f727ded571d584643895d43d02a7a190f8249748a2c32200cfc12dde7173", keystorePassword: "PASSWORD")

XCTAssertEqual(account.address.value, "0x675f5810feb3b09528e5cd175061b4eb8de69075")
}

func testFetchAccounts() {
let storage = EthereumKeyLocalStorage() as EthereumMultipleKeyStorageProtocol
let account = try! EthereumAccount.importAccount(keyStorage: storage, privateKey: "0x2639f727ded571d584643895d43d02a7a190f8249748a2c32200cfc12dde7173", keystorePassword: "PASSWORD")
let storage = EthereumKeyLocalStorage()
let account = try! EthereumAccount.importAccount(addingTo: storage, privateKey: "0x2639f727ded571d584643895d43d02a7a190f8249748a2c32200cfc12dde7173", keystorePassword: "PASSWORD")
let accounts = try! storage.fetchAccounts()
XCTAssertTrue(accounts.contains(account.address))
}

func testDeleteAccount() {
let storage = EthereumKeyLocalStorage() as EthereumMultipleKeyStorageProtocol
let account = try! EthereumAccount.importAccount(keyStorage: storage, privateKey: "0x2639f727ded571d584643895d43d02a7a190f8249748a2c32200cfc12dde7173", keystorePassword: "PASSWORD")
let storage = EthereumKeyLocalStorage()
let account = try! EthereumAccount.importAccount(addingTo: storage, privateKey: "0x2639f727ded571d584643895d43d02a7a190f8249748a2c32200cfc12dde7173", keystorePassword: "PASSWORD")
let ethereumAddress = EthereumAddress("0x675f5810feb3b09528e5cd175061b4eb8de69075")
try! storage.deletePrivateKey(for: ethereumAddress)
let accounts = try! storage.fetchAccounts()
Expand Down
10 changes: 5 additions & 5 deletions web3sTests/Account/EthereumKeyStorageTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ class EthereumKeyStorageTests: XCTestCase {

func testEncryptAndStorePrivateKey() {
let randomData = Data.randomOfLength(256)!
let keyStorage = EthereumKeyLocalStorage() as EthereumKeyStorageProtocol
let keyStorage = EthereumKeyLocalStorage() as EthereumSingleKeyStorageProtocol
let password = "myP4ssw0rD"

do {
Expand Down Expand Up @@ -73,11 +73,11 @@ class EthereumKeyStorageTests: XCTestCase {
}

func testDeleteAllPrivateKeys() {
let keyStorage = EthereumKeyLocalStorage() as EthereumMultipleKeyStorageProtocol
let keyStorage = EthereumKeyLocalStorage()
do {
_ = try EthereumAccount.create(keyStorage: keyStorage, keystorePassword: "PASSWORD")
_ = try EthereumAccount.create(keyStorage: keyStorage, keystorePassword: "PASSWORD")
_ = try EthereumAccount.create(keyStorage: keyStorage, keystorePassword: "PASSWORD")
_ = try EthereumAccount.create(addingTo: keyStorage, keystorePassword: "PASSWORD")
_ = try EthereumAccount.create(addingTo: keyStorage, keystorePassword: "PASSWORD")
_ = try EthereumAccount.create(addingTo: keyStorage, keystorePassword: "PASSWORD")
try keyStorage.deleteAllKeys()
let countAfterDeleting = try keyStorage.fetchAccounts()
XCTAssertEqual(countAfterDeleting.count, 0)
Expand Down
2 changes: 1 addition & 1 deletion web3sTests/Mocks/TestEthereumKeyStorage.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import Foundation
@testable import web3

class TestEthereumKeyStorage: EthereumKeyStorageProtocol {
class TestEthereumKeyStorage: EthereumSingleKeyStorageProtocol {

private var privateKey: String

Expand Down
12 changes: 6 additions & 6 deletions web3swift/src/Account/EthereumAccount.swift
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public class EthereumAccount: EthereumAccountProtocol {
return KeyUtil.generateAddress(from: self.publicKeyData)
}()

required public init(keyStorage: EthereumKeyStorageProtocol, keystorePassword password: String, logger: Logger? = nil) throws {
required public init(keyStorage: EthereumSingleKeyStorageProtocol, keystorePassword password: String, logger: Logger? = nil) throws {
self.logger = logger ?? Logger(label: "web3.swift.eth-account")
do {
let decodedKey = try keyStorage.loadAndDecryptPrivateKey(keystorePassword: password)
Expand All @@ -49,7 +49,7 @@ public class EthereumAccount: EthereumAccountProtocol {
}
}

required public init(keyStorage: EthereumKeyStorageProtocol, logger: Logger? = nil) throws {
required public init(keyStorage: EthereumSingleKeyStorageProtocol, logger: Logger? = nil) throws {
self.logger = logger ?? Logger(label: "web3.swift.eth-account")
do {
let data = try keyStorage.loadPrivateKey()
Expand Down Expand Up @@ -85,7 +85,7 @@ public class EthereumAccount: EthereumAccountProtocol {
}
}

public static func create(keyStorage: EthereumMultipleKeyStorageProtocol, keystorePassword password: String) throws -> EthereumAccount {
public static func create(addingTo keyStorage: EthereumMultipleKeyStorageProtocol, keystorePassword password: String) throws -> EthereumAccount {
guard let privateKey = KeyUtil.generatePrivateKeyData() else {
throw EthereumAccountError.createAccountError
}
Expand All @@ -100,7 +100,7 @@ public class EthereumAccount: EthereumAccountProtocol {
}
}

public static func create(keyStorage: EthereumKeyStorageProtocol, keystorePassword password: String) throws -> EthereumAccount {
public static func create(replacing keyStorage: EthereumSingleKeyStorageProtocol, keystorePassword password: String) throws -> EthereumAccount {
guard let privateKey = KeyUtil.generatePrivateKeyData() else {
throw EthereumAccountError.createAccountError
}
Expand All @@ -113,7 +113,7 @@ public class EthereumAccount: EthereumAccountProtocol {
}
}

public static func importAccount(keyStorage: EthereumMultipleKeyStorageProtocol, privateKey: String, keystorePassword password: String) throws -> EthereumAccount {
public static func importAccount(addingTo keyStorage: EthereumMultipleKeyStorageProtocol, privateKey: String, keystorePassword password: String) throws -> EthereumAccount {
guard let privateKey = privateKey.web3.hexData else {
throw EthereumAccountError.importAccountError
}
Expand All @@ -127,7 +127,7 @@ public class EthereumAccount: EthereumAccountProtocol {
}
}

public static func importAccount(keyStorage: EthereumKeyStorageProtocol, privateKey: String, keystorePassword password: String) throws -> EthereumAccount {
public static func importAccount(replacing keyStorage: EthereumSingleKeyStorageProtocol, privateKey: String, keystorePassword password: String) throws -> EthereumAccount {
guard let privateKey = privateKey.web3.hexData else {
throw EthereumAccountError.importAccountError
}
Expand Down
2 changes: 1 addition & 1 deletion web3swift/src/Account/EthereumKeyStorage+Password.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

import Foundation

public extension EthereumKeyStorageProtocol {
public extension EthereumSingleKeyStorageProtocol {
func encryptAndStorePrivateKey(key: Data, keystorePassword: String) throws {
let encodedKey = try KeystoreUtil.encode(privateKey: key, password: keystorePassword)
try storePrivateKey(key: encodedKey)
Expand Down
4 changes: 2 additions & 2 deletions web3swift/src/Account/EthereumKeyStorage.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

import Foundation

public protocol EthereumKeyStorageProtocol {
public protocol EthereumSingleKeyStorageProtocol {
func storePrivateKey(key: Data) throws
func loadPrivateKey() throws -> Data
}
Expand All @@ -25,7 +25,7 @@ public enum EthereumKeyStorageError: Error {
case failedToDelete
}

public class EthereumKeyLocalStorage: EthereumKeyStorageProtocol {
public class EthereumKeyLocalStorage: EthereumSingleKeyStorageProtocol {

public init() {}

Expand Down