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 private key validation #6728

Merged
merged 1 commit into from
Oct 12, 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
Original file line number Diff line number Diff line change
Expand Up @@ -45,70 +45,67 @@ class RestorePrivateKeyViewModel(
inputState = null
return try {
accountType(text)
} catch (e: Throwable) {
} catch (e: Exception) {
inputState = DataState.Error(
Exception(Translator.getString(R.string.Restore_PrivateKey_InvalidKey))
)
null
}
}

@Throws(Exception::class)
private fun accountType(text: String): AccountType {
val textCleaned = text.trim()

if (textCleaned.isEmpty()) {
throw EmptyText
}

if (!isValidEthereumPrivateKey(textCleaned)) {
throw NoValidKey
if (isValidEthereumPrivateKey(textCleaned)) {
val privateKey = Signer.privateKey(textCleaned)
return AccountType.EvmPrivateKey(privateKey)
}

try {
val extendedKey = HDExtendedKey(textCleaned)
if (!extendedKey.isPublic) {
when (extendedKey.derivedType) {
HDExtendedKey.DerivedType.Master,
HDExtendedKey.DerivedType.Account -> {
return AccountType.HdExtendedKey(extendedKey.serializePrivate())
}
else -> throw NotSupportedDerivedType
}
} else {
if (extendedKey.isPublic) {
throw NonPrivateKey
}
} catch (e: Throwable) {
//do nothing
}
when (extendedKey.derivedType) {
HDExtendedKey.DerivedType.Master,
HDExtendedKey.DerivedType.Account -> {
return AccountType.HdExtendedKey(extendedKey.serializePrivate())
}

try {
val privateKey = Signer.privateKey(text)
return AccountType.EvmPrivateKey(privateKey)
else -> throw NotSupportedDerivedType
}
} catch (e: Throwable) {
//do nothing
throw NoValidKey
}

throw NoValidKey
}

fun isValidEthereumPrivateKey(privateKeyHex: String): Boolean {
//key should be 32 bytes long
privateKeyHex.hexToByteArray().let {
if (it.size != 32) {
return false
private fun isValidEthereumPrivateKey(privateKeyHex: String): Boolean {
try {
//key should be 32 bytes long
privateKeyHex.hexToByteArray().let {
if (it.size != 32) {
return false
}
}
}

// Convert the hex private key to a BigInteger
val privateKeyBigInt = BigInteger(privateKeyHex, 16)
// Convert the hex private key to a BigInteger
val privateKeyBigInt = BigInteger(privateKeyHex, 16)

// Define the order of the secp256k1 curve (n)
val secp256k1Order = BigInteger(
"fffffffffffffffffffffffffffffffebaaedce6af48a03bbfd25e8cd0364141",
16
)
// Define the order of the secp256k1 curve (n)
val secp256k1Order = BigInteger(
"fffffffffffffffffffffffffffffffebaaedce6af48a03bbfd25e8cd0364141",
16
)

// Check if the private key is greater than zero and less than the order
return privateKeyBigInt > BigInteger.ZERO && privateKeyBigInt < secp256k1Order
// Check if the private key is greater than zero and less than the order
return privateKeyBigInt > BigInteger.ZERO && privateKeyBigInt < secp256k1Order
} catch (e: NumberFormatException) {
return false
}
}
}