Skip to content

Commit 9a59170

Browse files
committed
fix: switch current account set
1 parent 4f65b2d commit 9a59170

File tree

3 files changed

+46
-85
lines changed

3 files changed

+46
-85
lines changed

app/src/main/java/com/flowfoundation/wallet/manager/account/AccountManager.kt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -480,11 +480,12 @@ object AccountManager {
480480
return@ioScope
481481
}
482482
isSwitching = true
483-
currentAccount = account
484-
logd(TAG, "Account switched. Current account is now: $currentAccount")
483+
logd(TAG, "Starting account switch to: ${account.userInfo.username}")
485484
switchAccount(account) { isSuccess ->
486485
if (isSuccess) {
487486
isSwitching = false
487+
currentAccount = account
488+
logd(TAG, "Account switch successful. Current account updated to: $currentAccount")
488489
accounts.forEach {
489490
it.isActive = it.userInfo.username == account.userInfo.username
490491
}

app/src/main/java/com/flowfoundation/wallet/network/UserRegisterUtils.kt

Lines changed: 1 addition & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -558,63 +558,10 @@ private suspend fun registerServer(username: String, prefix: String): RegisterRe
558558
firebaseJwt
559559
)
560560
)
561-
// Create EVMAccountInfo for registration
562-
// IMPORTANT: EVM key must be derived from the MNEMONIC, not from the P256 private key
563-
// The extension derives EVM from mnemonic with BIP44 path m/44'/60'/0'/0/0
564-
val evmAccountInfo = try {
565-
// Generate and store mnemonic globally for potential future EOA support
566-
val mnemonic = BIP39.generate(BIP39.SeedPhraseLength.TWELVE)
567-
logd(TAG, "Generated new 12-word mnemonic for backup support")
568-
569-
val passwordMap = try {
570-
val pref = readWalletPassword()
571-
if (pref.isBlank()) {
572-
HashMap<String, String>()
573-
} else {
574-
Gson().fromJson(pref, object : TypeToken<HashMap<String, String>>() {}.type)
575-
}
576-
} catch (_: Exception) {
577-
HashMap()
578-
}
579-
580-
// Store mnemonic globally (available for future EOA enablement if user chooses)
581-
storeWalletPassword(Gson().toJson(passwordMap.apply { put("global", mnemonic) }))
582-
logd(TAG, "Stored mnemonic globally for backup support")
583-
// Use Trust Wallet Core to derive EVM key from mnemonic
584-
val hdWallet = wallet.core.jni.HDWallet(mnemonic, "")
585-
val evmDerivationPath = "m/44'/60'/0'/0/0" // Standard Ethereum BIP44 path
586-
587-
// Get private key for EVM using secp256k1 curve
588-
val evmPrivateKey = hdWallet.getKeyByCurve(wallet.core.jni.Curve.SECP256K1, evmDerivationPath)
589-
val evmPublicKey = evmPrivateKey.getPublicKeySecp256k1(false) // uncompressed
590-
591-
// Derive EVM address from public key
592-
val evmAddress = wallet.core.jni.AnyAddress(evmPublicKey, wallet.core.jni.CoinType.ETHEREUM).description()
593-
logd(TAG, "Derived EVM address from mnemonic: $evmAddress")
594-
595-
// Sign keccak256(idToken) for EVM - NO domain tag, same as extension
596-
val jwtBytes = firebaseJwt.toByteArray(Charsets.UTF_8)
597-
val jwtHash = Hash.keccak256(jwtBytes)
598-
599-
// Sign the digest with secp256k1
600-
val signatureData = evmPrivateKey.sign(jwtHash, wallet.core.jni.Curve.SECP256K1)
601-
602-
val evmSignature = "0x" + signatureData.joinToString("") { "%02x".format(it) }
603-
logd(TAG, "Generated EVM signature from mnemonic, length: ${evmSignature.length}")
604-
605-
EvmAccountInfo(
606-
eoaAddress = evmAddress,
607-
signature = evmSignature
608-
)
609-
} catch (e: Exception) {
610-
logd(TAG, "Error creating EVM account info from mnemonic: ${e.message}")
611-
e.printStackTrace()
612-
null
613-
}
614561

615562
val request = RegisterRequest(
616563
flowAccountInfo = flowAccountInfo,
617-
evmAccountInfo = evmAccountInfo,
564+
evmAccountInfo = null,
618565
username = username,
619566
deviceInfo = deviceInfoRequest
620567
)

app/src/main/java/com/flowfoundation/wallet/page/restore/keystore/viewmodel/KeyStoreRestoreViewModel.kt

Lines changed: 42 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1256,39 +1256,52 @@ class KeyStoreRestoreViewModel : ViewModel() {
12561256
// Create EVMAccountInfo for keystore registration
12571257
val evmAccountInfo = try {
12581258
val storage = getStorage()
1259-
val privateKeyHex = cryptoProvider.getPrivateKey()
1260-
val key = PrivateKey.create(storage).apply {
1261-
val keyBytes = privateKeyHex.removePrefix("0x").hexToBytes()
1262-
importPrivateKey(keyBytes, KeyFormat.RAW)
1263-
}
1259+
if (currentMnemonic.isNullOrBlank().not()) {
1260+
val hdWallet = wallet.core.jni.HDWallet(currentMnemonic, "")
1261+
val evmDerivationPath = "m/44'/60'/0'/0/0"
1262+
val evmPrivateKey = hdWallet.getKeyByCurve(wallet.core.jni.Curve.SECP256K1, evmDerivationPath)
1263+
val evmPublicKey = evmPrivateKey.getPublicKeySecp256k1(false)
1264+
val evmAddress = wallet.core.jni.AnyAddress(evmPublicKey, wallet.core.jni.CoinType.ETHEREUM).description()
1265+
val jwtHash = Hash.keccak256(firebaseJwt.toByteArray(Charsets.UTF_8))
1266+
val signatureData = evmPrivateKey.sign(jwtHash, wallet.core.jni.Curve.SECP256K1)
1267+
val evmSignature = "0x" + signatureData.joinToString("") { "%02x".format(it) }
1268+
EvmAccountInfo(eoaAddress = evmAddress, signature = evmSignature)
1269+
} else {
1270+
val privateKeyHex = cryptoProvider.getPrivateKey()
1271+
val key = PrivateKey.create(storage).apply {
1272+
val keyBytes = privateKeyHex.removePrefix("0x").hexToBytes()
1273+
importPrivateKey(keyBytes, KeyFormat.RAW)
1274+
}
12641275

1265-
// Get secp256k1 public key for EVM address derivation
1266-
val evmPublicKeyBytes = key.publicKey(SigningAlgorithm.ECDSA_secp256k1)
1267-
if (evmPublicKeyBytes != null) {
1268-
// Derive EVM address from public key using Keccak256
1269-
val publicKeyForHash = if (evmPublicKeyBytes.size == 65 && evmPublicKeyBytes[0] == 0x04.toByte()) {
1270-
evmPublicKeyBytes.copyOfRange(1, evmPublicKeyBytes.size)
1276+
// Get secp256k1 public key for EVM address derivation
1277+
val evmPublicKeyBytes = key.publicKey(SigningAlgorithm.ECDSA_secp256k1)
1278+
if (evmPublicKeyBytes != null) {
1279+
// Derive EVM address from public key using Keccak256
1280+
val publicKeyForHash = if (evmPublicKeyBytes.size == 65 && evmPublicKeyBytes[0] == 0x04.toByte()) {
1281+
evmPublicKeyBytes.copyOfRange(1, evmPublicKeyBytes.size)
1282+
} else {
1283+
evmPublicKeyBytes
1284+
}
1285+
val addressHash = Hash.keccak256(publicKeyForHash)
1286+
val evmAddress = "0x" + addressHash.copyOfRange(12, 32).joinToString("") { "%02x".format(it) }
1287+
logd("KeyStoreRestoreViewModel", "Derived EVM address: $evmAddress")
1288+
1289+
// Sign Firebase JWT for EVM with secp256k1 key
1290+
val dataToSign = DomainTag.User.bytes + firebaseJwt.toByteArray(Charsets.UTF_8)
1291+
val evmSignatureBytes = key.sign(dataToSign, SigningAlgorithm.ECDSA_secp256k1, HashingAlgorithm.SHA2_256)
1292+
val evmSignature = evmSignatureBytes.joinToString("") { "%02x".format(it) }
1293+
logd("KeyStoreRestoreViewModel", "Generated EVM signature, length: ${evmSignature.length}")
1294+
1295+
EvmAccountInfo(
1296+
eoaAddress = evmAddress,
1297+
signature = evmSignature
1298+
)
12711299
} else {
1272-
evmPublicKeyBytes
1300+
logd("KeyStoreRestoreViewModel", "Could not derive secp256k1 public key, skipping EVM account")
1301+
null
12731302
}
1274-
val addressHash = Hash.keccak256(publicKeyForHash)
1275-
val evmAddress = "0x" + addressHash.copyOfRange(12, 32).joinToString("") { "%02x".format(it) }
1276-
logd("KeyStoreRestoreViewModel", "Derived EVM address: $evmAddress")
1277-
1278-
// Sign Firebase JWT for EVM with secp256k1 key
1279-
val dataToSign = DomainTag.User.bytes + firebaseJwt.toByteArray(Charsets.UTF_8)
1280-
val evmSignatureBytes = key.sign(dataToSign, SigningAlgorithm.ECDSA_secp256k1, HashingAlgorithm.SHA2_256)
1281-
val evmSignature = evmSignatureBytes.joinToString("") { "%02x".format(it) }
1282-
logd("KeyStoreRestoreViewModel", "Generated EVM signature, length: ${evmSignature.length}")
1283-
1284-
EvmAccountInfo(
1285-
eoaAddress = evmAddress,
1286-
signature = evmSignature
1287-
)
1288-
} else {
1289-
logd("KeyStoreRestoreViewModel", "Could not derive secp256k1 public key, skipping EVM account")
1290-
null
12911303
}
1304+
12921305
} catch (e: Exception) {
12931306
logd("KeyStoreRestoreViewModel", "Error creating EVM account info: ${e.message}")
12941307
null

0 commit comments

Comments
 (0)