-
Notifications
You must be signed in to change notification settings - Fork 4
feat: add multi-EOA support with BIP44 derivation #85
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
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
12379b6
feat: add multi-EOA support with BIP44 derivation
lmcmz 1c9d9ad
feat: replace eoaAddress Set with index→address Map
lmcmz 41c4870
feat: persist eoaAddressMap to storage
lmcmz f92863f
fix: use key ID in eoaMapCacheId to avoid collision between wallets
lmcmz File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
196 changes: 196 additions & 0 deletions
196
Android/wallet/src/androidTest/java/com/flow/wallet/wallet/EOAAccountTest.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,196 @@ | ||
| package com.flow.wallet.wallet | ||
|
|
||
| import androidx.test.ext.junit.runners.AndroidJUnit4 | ||
| import com.flow.wallet.NativeLibraryManager | ||
| import com.flow.wallet.crypto.HasherImpl | ||
| import com.flow.wallet.keys.SeedPhraseKey | ||
| import com.flow.wallet.storage.InMemoryStorage | ||
| import kotlinx.coroutines.runBlocking | ||
| import org.junit.Assert.* | ||
| import org.junit.Before | ||
| import org.junit.Test | ||
| import org.junit.runner.RunWith | ||
|
|
||
| @RunWith(AndroidJUnit4::class) | ||
| class EOAAccountTest { | ||
|
|
||
| private val testMnemonic = "abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon about" | ||
| // Standard BIP44 m/44'/60'/0'/0/0 address for this mnemonic | ||
| private val expectedIndex0Address = "0x9858EfFD232B4033E47d90003D41EC34EcaEda94" | ||
|
|
||
| private lateinit var storage: InMemoryStorage | ||
| private lateinit var seedPhraseKey: SeedPhraseKey | ||
|
|
||
| @Before | ||
| fun setup() { | ||
| NativeLibraryManager.ensureLibraryLoaded() | ||
| storage = InMemoryStorage() | ||
| seedPhraseKey = SeedPhraseKey( | ||
| testMnemonic, "", SeedPhraseKey.DEFAULT_DERIVATION_PATH, storage | ||
| ) | ||
| } | ||
|
|
||
| // MARK: - Multi EOA Derivation | ||
|
|
||
| @Test | ||
| fun testDeriveMultipleEOAAccounts() { | ||
| val accounts = listOf(0, 1, 2).map { index -> | ||
| EOAAccount( | ||
| address = seedPhraseKey.ethAddress(index), | ||
| index = index, | ||
| publicKey = seedPhraseKey.ethPublicKey(index), | ||
| key = seedPhraseKey | ||
| ) | ||
| } | ||
|
|
||
| assertEquals(3, accounts.size) | ||
| // Each account should have a unique address | ||
| val addresses = accounts.map { it.address }.toSet() | ||
| assertEquals("Each derivation index should produce a unique address", 3, addresses.size) | ||
|
|
||
| // Verify indexes match | ||
| assertEquals(0, accounts[0].index) | ||
| assertEquals(1, accounts[1].index) | ||
| assertEquals(2, accounts[2].index) | ||
|
|
||
| // Index 0 should match known address | ||
| assertEquals(expectedIndex0Address, accounts[0].address) | ||
| } | ||
|
|
||
| @Test | ||
| fun testEOAAccountSignDigest() { | ||
| val account = EOAAccount( | ||
| address = seedPhraseKey.ethAddress(0), | ||
| index = 0, | ||
| publicKey = seedPhraseKey.ethPublicKey(0), | ||
| key = seedPhraseKey | ||
| ) | ||
|
|
||
| val digest = HasherImpl.keccak256("hello world".toByteArray()) | ||
| val signature = account.signDigest(digest) | ||
|
|
||
| assertEquals(65, signature.size) | ||
| val v = signature[64].toInt() and 0xFF | ||
| assertTrue("v should be 27 or 28", v == 27 || v == 28) | ||
|
|
||
| // Should match direct key signing | ||
| val directSignature = seedPhraseKey.ethSignDigest(digest, 0) | ||
| assertArrayEquals(signature, directSignature) | ||
| } | ||
|
|
||
| @Test | ||
| fun testEOAAccountPersonalSign() { | ||
| val account = EOAAccount( | ||
| address = seedPhraseKey.ethAddress(0), | ||
| index = 0, | ||
| publicKey = seedPhraseKey.ethPublicKey(0), | ||
| key = seedPhraseKey | ||
| ) | ||
|
|
||
| val message = "Flow Wallet".toByteArray() | ||
| val signature = account.signPersonalMessage(message) | ||
|
|
||
| assertEquals(65, signature.size) | ||
|
|
||
| // Manually compute expected: prefix + keccak256 + sign | ||
| val prefix = "\u0019Ethereum Signed Message:\n${message.size}".toByteArray(Charsets.UTF_8) | ||
| val payload = prefix + message | ||
| val digest = HasherImpl.keccak256(payload) | ||
| val directSignature = seedPhraseKey.ethSignDigest(digest, 0) | ||
| assertArrayEquals(signature, directSignature) | ||
| } | ||
|
|
||
| @Test | ||
| fun testDifferentIndexesProduceDifferentSignatures() { | ||
| val account0 = EOAAccount( | ||
| address = seedPhraseKey.ethAddress(0), index = 0, | ||
| publicKey = seedPhraseKey.ethPublicKey(0), key = seedPhraseKey | ||
| ) | ||
| val account1 = EOAAccount( | ||
| address = seedPhraseKey.ethAddress(1), index = 1, | ||
| publicKey = seedPhraseKey.ethPublicKey(1), key = seedPhraseKey | ||
| ) | ||
|
|
||
| val digest = HasherImpl.keccak256("test message".toByteArray()) | ||
| val sig0 = account0.signDigest(digest) | ||
| val sig1 = account1.signDigest(digest) | ||
|
|
||
| assertFalse("Different derivation indexes should produce different signatures", sig0.contentEquals(sig1)) | ||
| } | ||
|
|
||
| @Test | ||
| fun testEOAAccountPublicKey() { | ||
| val account = EOAAccount( | ||
| address = seedPhraseKey.ethAddress(0), index = 0, | ||
| publicKey = seedPhraseKey.ethPublicKey(0), key = seedPhraseKey | ||
| ) | ||
|
|
||
| // Uncompressed secp256k1 public key is 65 bytes (04 prefix + 32x + 32y) | ||
| assertEquals(65, account.publicKey.size) | ||
| assertEquals(0x04.toByte(), account.publicKey[0]) | ||
| } | ||
|
|
||
| @Test | ||
| fun testEOAAccountPrivateKey() { | ||
| val account0 = EOAAccount( | ||
| address = seedPhraseKey.ethAddress(0), index = 0, | ||
| publicKey = seedPhraseKey.ethPublicKey(0), key = seedPhraseKey | ||
| ) | ||
| val account1 = EOAAccount( | ||
| address = seedPhraseKey.ethAddress(1), index = 1, | ||
| publicKey = seedPhraseKey.ethPublicKey(1), key = seedPhraseKey | ||
| ) | ||
|
|
||
| val pk0 = account0.privateKeyData() | ||
| val pk1 = account1.privateKeyData() | ||
|
|
||
| assertEquals(32, pk0.size) | ||
| assertEquals(32, pk1.size) | ||
| assertFalse(pk0.contentEquals(pk1)) | ||
| } | ||
|
|
||
| @Test | ||
| fun testLargeDerivationIndex() { | ||
| val account = EOAAccount( | ||
| address = seedPhraseKey.ethAddress(100), index = 100, | ||
| publicKey = seedPhraseKey.ethPublicKey(100), key = seedPhraseKey | ||
| ) | ||
|
|
||
| assertEquals(100, account.index) | ||
| assertTrue(account.address.startsWith("0x")) | ||
| assertEquals(42, account.address.length) | ||
| } | ||
|
|
||
| // MARK: - Wallet integration (via KeyWallet) | ||
|
|
||
| @Test | ||
| fun testKeyWalletGetEOAAccounts() = runBlocking { | ||
| val wallet = KeyWallet( | ||
| key = seedPhraseKey, | ||
| networks = mutableSetOf(), | ||
| storage = storage | ||
| ) | ||
|
|
||
| val accounts = wallet.getEOAAccounts(listOf(0, 1, 2)) | ||
|
|
||
| assertEquals(3, accounts.size) | ||
| assertEquals(expectedIndex0Address, accounts[0].address) | ||
| assertEquals(0, accounts[0].index) | ||
| assertEquals(1, accounts[1].index) | ||
| assertEquals(2, accounts[2].index) | ||
| } | ||
|
|
||
| @Test | ||
| fun testKeyWalletGetEOAAccountsDefaultIndex() = runBlocking { | ||
| val wallet = KeyWallet( | ||
| key = seedPhraseKey, | ||
| networks = mutableSetOf(), | ||
| storage = storage | ||
| ) | ||
|
|
||
| val accounts = wallet.getEOAAccounts() | ||
|
|
||
| assertEquals(1, accounts.size) | ||
| assertEquals(0, accounts[0].index) | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
64 changes: 64 additions & 0 deletions
64
Android/wallet/src/main/java/com/flow/wallet/wallet/EOAAccount.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,64 @@ | ||
| package com.flow.wallet.wallet | ||
|
|
||
| import com.flow.wallet.crypto.HasherImpl | ||
| import com.flow.wallet.errors.WalletError | ||
| import com.flow.wallet.keys.EthereumKeyProtocol | ||
| import com.flow.wallet.keys.EthereumSignatureUtils | ||
| import com.google.protobuf.ByteString | ||
| import wallet.core.java.AnySigner | ||
| import wallet.core.jni.CoinType | ||
| import wallet.core.jni.EthereumAbi | ||
| import wallet.core.jni.proto.Ethereum | ||
|
|
||
| /** | ||
| * Represents a single Ethereum EOA derived from a BIP44 path. | ||
| * Bundles the address, derivation index, public key, and signing capabilities. | ||
| * | ||
| * @property address EIP-55 checksummed Ethereum address | ||
| * @property index BIP44 derivation index (m/44'/60'/0'/0/{index}) | ||
| * @property publicKey Uncompressed secp256k1 public key bytes | ||
| */ | ||
| class EOAAccount( | ||
| val address: String, | ||
| val index: Int, | ||
| val publicKey: ByteArray, | ||
| private val key: EthereumKeyProtocol | ||
| ) { | ||
| /** Sign a pre-hashed 32-byte digest. Returns [r(32)|s(32)|v(1)]. */ | ||
| fun signDigest(digest: ByteArray): ByteArray { | ||
| return key.ethSignDigest(digest, index) | ||
| } | ||
|
|
||
| /** EIP-191 personal_sign. */ | ||
| fun signPersonalMessage(message: ByteArray): ByteArray { | ||
| val prefix = "\u0019Ethereum Signed Message:\n${message.size}".toByteArray(Charsets.UTF_8) | ||
| val payload = prefix + message | ||
| val digest = HasherImpl.keccak256(payload) | ||
| return signDigest(digest) | ||
| } | ||
|
|
||
| /** EIP-712 signTypedData. */ | ||
| fun signTypedData(json: String): ByteArray { | ||
| val digest = EthereumAbi.encodeTyped(json) | ||
| if (digest.size != 32) { | ||
| throw WalletError.InvalidEthereumTypedData | ||
| } | ||
| return signDigest(digest) | ||
| } | ||
|
|
||
| /** Sign an Ethereum transaction via WalletCore AnySigner. */ | ||
| fun signTransaction(input: Ethereum.SigningInput): Ethereum.SigningOutput { | ||
| val privateKey = key.ethPrivateKey(index) | ||
| val builder = input.toBuilder() | ||
| builder.privateKey = ByteString.copyFrom(privateKey) | ||
| return try { | ||
| AnySigner.sign(builder.build(), CoinType.ETHEREUM, Ethereum.SigningOutput.parser()) | ||
| } finally { | ||
| builder.clearPrivateKey() | ||
| privateKey.fill(0) | ||
| } | ||
| } | ||
|
|
||
| /** Raw 32-byte secp256k1 private key. Caller is responsible for secure handling. */ | ||
| fun privateKeyData(): ByteArray = key.ethPrivateKey(index) | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The
privateKeyis cleared from the builder but the localprivateKeyvariable is not cleared from memory after use. Consider usingArrays.fill(privateKey, 0.toByte())or similar to ensure the private key is securely wiped from memory.