Skip to content

Commit

Permalink
Fix private key validation
Browse files Browse the repository at this point in the history
  • Loading branch information
rafaelekol committed Oct 12, 2023
1 parent 45a6d93 commit 1a1aae9
Showing 1 changed file with 33 additions and 36 deletions.
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
}
}
}

0 comments on commit 1a1aae9

Please sign in to comment.