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

chore: refactoring #3

Merged
merged 5 commits into from
Jun 30, 2023
Merged
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
36 changes: 15 additions & 21 deletions Sources/Web3Core/KeystoreManager/BIP32Keystore.swift
Original file line number Diff line number Diff line change
Expand Up @@ -169,45 +169,40 @@ public class BIP32Keystore: AbstractKeystore {
throw AbstractKeystoreError.encryptionError("Failed to deserialize a root node")
}

let prefixPath = self.rootPrefix
var pathAppendix: String?
let prefixPath = rootPrefix
var pathAppendix = path

if path.hasPrefix(prefixPath) {
if let upperIndex = (path.range(of: prefixPath)?.upperBound), upperIndex < path.endIndex {
pathAppendix = String(path[path.index(after: upperIndex)..<path.endIndex])
} else {
throw AbstractKeystoreError.encryptionError("out of bounds")
}

guard let modifiedAppendix = pathAppendix else {
throw AbstractKeystoreError.encryptionError("Derivation depth mismatch")
}
if modifiedAppendix.hasPrefix("/") {
pathAppendix = modifiedAppendix.trimmingCharacters(in: CharacterSet.init(charactersIn: "/"))
}
} else if path.hasPrefix("/") {
pathAppendix = path.trimmingCharacters(in: CharacterSet.init(charactersIn: "/"))
}

guard let pathAppendix,
rootNode.depth == prefixPath.components(separatedBy: "/").count - 1 else {
if pathAppendix.hasPrefix("/") {
pathAppendix = pathAppendix.trimmingCharacters(in: .init(charactersIn: "/"))
}
guard rootNode.depth == prefixPath.components(separatedBy: "/").count - 1 else {
throw AbstractKeystoreError.encryptionError("Derivation depth mismatch")
}

guard let newNode = rootNode.derive(path: pathAppendix, derivePrivateKey: true),
let newAddress = Utilities.publicToAddress(newNode.publicKey) else {
guard let newNode = rootNode.derive(path: pathAppendix, derivePrivateKey: true) else {
throw AbstractKeystoreError.keyDerivationError
}
guard let newAddress = Utilities.publicToAddress(newNode.publicKey) else {
throw AbstractKeystoreError.keyDerivationError
}

var newPath: String
let newPath: String
Copy link
Owner

Choose a reason for hiding this comment

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

will it be working ?

Copy link
Author

Choose a reason for hiding this comment

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

Yes, absolutely.

if newNode.isHardened {
newPath = prefixPath + "/" + pathAppendix.trimmingCharacters(in: CharacterSet.init(charactersIn: "'")) + "'"
newPath = prefixPath + "/" + pathAppendix.trimmingCharacters(in: .init(charactersIn: "'")) + "'"
} else {
newPath = prefixPath + "/" + pathAppendix
}

addressStorage.add(address: newAddress, for: newPath)
guard let serializedRootNode = rootNode.serialize(serializePublic: false) else {throw AbstractKeystoreError.keyDerivationError}
guard let serializedRootNode = rootNode.serialize(serializePublic: false) else {
throw AbstractKeystoreError.keyDerivationError
}
try encryptDataToStorage(password, data: serializedRootNode, aesMode: keystoreParams.crypto.cipher)
}

Expand All @@ -222,7 +217,6 @@ public class BIP32Keystore: AbstractKeystore {
let rootNode = HDNode(decryptedRootNode) else {
throw AbstractKeystoreError.encryptionError("Failed to decrypt a keystore")
}

return try [UInt](0..<number).compactMap({ number in
guard rootNode.depth == rootPrefix.components(separatedBy: "/").count - 1,
let newNode = rootNode.derive(path: "\(number)", derivePrivateKey: true) else {
Expand Down