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

Fix import wallet account instead of creating account? #111 #173

Merged
merged 1 commit into from
Feb 8, 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: 6 additions & 0 deletions web3sTests/Account/EthereumAccountTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,12 @@ class EthereumAccountTests: XCTestCase {
let account = try? EthereumAccount.create(keyStorage: EthereumKeyLocalStorage(), keystorePassword: "PASSWORD")
XCTAssertNotNil(account, "Failed to create account. Ensure key is valid in TestConfig.swift")
}

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

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

func testSignMessage() {
let account = try! EthereumAccount(keyStorage: TestEthereumKeyStorage(privateKey: "0x2639f727ded571d584643895d43d02a7a190f8249748a2c32200cfc12dde7173"))
Expand Down
16 changes: 15 additions & 1 deletion web3swift/src/Account/EthereumAccount.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ public protocol EthereumAccountProtocol {

public enum EthereumAccountError: Error {
case createAccountError
case importAccountError
case loadAccountError
case signError
}
Expand Down Expand Up @@ -76,7 +77,20 @@ public class EthereumAccount: EthereumAccountProtocol {
throw EthereumAccountError.createAccountError
}
}


public static func importAccount(keyStorage: EthereumKeyStorageProtocol, privateKey: String, keystorePassword password: String) throws -> EthereumAccount {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you change privateKey to be Data, then you'll avoid the throw below.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you change privateKey to be Data, then you'll avoid the throw below.

About changing the privateKey as Data I dont agree with that. Its better to create an overloaded method to accept also Data as param instead of accepting only Data.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, can be either way

guard let privateKey = privateKey.web3.hexData else {
throw EthereumAccountError.importAccountError
}
do {
let encodedData = try KeystoreUtil.encode(privateKey: privateKey, password: password)
try keyStorage.storePrivateKey(key: encodedData)
return try self.init(keyStorage: keyStorage, keystorePassword: password)
} catch {
throw EthereumAccountError.importAccountError
}
}

public func sign(data: Data) throws -> Data {
return try KeyUtil.sign(message: data, with: self.privateKeyData, hashing: true)
}
Expand Down