clean up mnemonic inputs of extra spaces

This commit is contained in:
ivan grachev 2023-11-02 21:07:16 +03:00
parent fc78b1125f
commit 0635215d80
2 changed files with 13 additions and 5 deletions

View File

@ -49,4 +49,10 @@ extension String {
return prefix + String(value, radix: 16)
}
var singleSpaced: String {
let trimmedString = self.trimmingCharacters(in: .whitespacesAndNewlines)
let singleSpacedString = trimmedString.replacingOccurrences(of: "\\s+", with: " ", options: .regularExpression, range: nil)
return singleSpacedString
}
}

View File

@ -26,9 +26,10 @@ final class WalletsManager {
}
func validateWalletInput(_ input: String) -> InputValidationResult {
if Mnemonic.isValid(mnemonic: input) {
let trimmedInput = input.singleSpaced
if Mnemonic.isValid(mnemonic: trimmedInput) {
return .valid
} else if let data = Data(hexString: input) {
} else if let data = Data(hexString: trimmedInput) {
return PrivateKey.isValid(data: data, curve: CoinType.ethereum.curve) ? .valid : .invalid
} else {
return input.maybeJSON ? .requiresPassword : .invalid
@ -48,9 +49,10 @@ final class WalletsManager {
guard let password = keychain.password else { throw Error.keychainAccessFailure }
let name = defaultWalletName
let defaultCoin = CoinType.ethereum
if Mnemonic.isValid(mnemonic: input) {
return try importMnemonic(input, name: name, encryptPassword: password)
} else if let data = Data(hexString: input), PrivateKey.isValid(data: data, curve: defaultCoin.curve), let privateKey = PrivateKey(data: data) {
let trimmedInput = input.singleSpaced
if Mnemonic.isValid(mnemonic: trimmedInput) {
return try importMnemonic(trimmedInput, name: name, encryptPassword: password)
} else if let data = Data(hexString: trimmedInput), PrivateKey.isValid(data: data, curve: defaultCoin.curve), let privateKey = PrivateKey(data: data) {
return try importPrivateKey(privateKey, name: name, password: password, coin: defaultCoin, onlyToKeychain: false)
} else if input.maybeJSON, let inputPassword = inputPassword, let json = input.data(using: .utf8) {
return try importJSON(json, name: name, password: inputPassword, newPassword: password, coin: defaultCoin, onlyToKeychain: false)