-
Notifications
You must be signed in to change notification settings - Fork 3
Refactor Ethereum txId logic and update tests #80
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
Draft
lmcmz
wants to merge
1
commit into
main
Choose a base branch
from
fix-prehash
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
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
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
256 changes: 256 additions & 0 deletions
256
Android/wallet/src/androidTest/java/com/flow/wallet/keys/SeedPhraseKeyProviderTest.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,256 @@ | ||
| package com.flow.wallet.keys | ||
|
|
||
| import com.flow.wallet.errors.WalletError | ||
| import com.flow.wallet.crypto.ChaChaPolyCipher | ||
| import com.flow.wallet.storage.StorageProtocol | ||
| import com.flow.wallet.storage.InMemoryStorage | ||
| import junit.framework.TestCase.assertEquals | ||
| import junit.framework.TestCase.assertNotNull | ||
| import junit.framework.TestCase.assertTrue | ||
| import kotlinx.coroutines.runBlocking | ||
| import org.junit.Before | ||
| import org.junit.Test | ||
| import org.junit.runner.RunWith | ||
| import org.mockito.Mock | ||
| import org.mockito.Mockito.`when` | ||
| import org.mockito.Mockito.verify | ||
| import org.mockito.MockitoAnnotations | ||
| import org.mockito.junit.MockitoJUnitRunner | ||
| import org.mockito.kotlin.any | ||
| import org.onflow.flow.models.HashingAlgorithm | ||
| import org.onflow.flow.models.SigningAlgorithm | ||
| import kotlin.test.assertFailsWith | ||
| import kotlin.test.assertFalse | ||
|
|
||
| @RunWith(MockitoJUnitRunner::class) | ||
| class SeedPhraseKeyProviderTest { | ||
|
|
||
| @Mock | ||
| private lateinit var mockStorage: StorageProtocol | ||
|
|
||
| private lateinit var seedPhraseKeyProvider: SeedPhraseKeyProvider | ||
| private val validSeedPhrase = "abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon about" | ||
|
|
||
| @Before | ||
| fun setup() { | ||
| MockitoAnnotations.openMocks(this) | ||
| seedPhraseKeyProvider = SeedPhraseKey(validSeedPhrase, "", "m/44'/539'/0'/0/0", mockStorage) | ||
| } | ||
|
|
||
| @Test | ||
| fun `test key derivation`() { | ||
| val derivedKey = seedPhraseKeyProvider.deriveKey(0) | ||
| assertNotNull(derivedKey) | ||
| assertTrue(derivedKey is PrivateKey) | ||
| } | ||
|
|
||
| @Test | ||
| fun `test key derivation with different indices`() { | ||
| val key1 = seedPhraseKeyProvider.deriveKey(0) | ||
| val key2 = seedPhraseKeyProvider.deriveKey(1) | ||
|
|
||
| assertNotNull(key1) | ||
| assertNotNull(key2) | ||
| assertTrue(key1 is PrivateKey) | ||
| assertTrue(key2 is PrivateKey) | ||
|
|
||
| // Different indices should produce different keys | ||
| assertTrue(!key1.secret.contentEquals(key2.secret)) | ||
| } | ||
|
|
||
| @Test | ||
| fun `test key creation with default options`() { | ||
| runBlocking { | ||
| val key = seedPhraseKeyProvider.create(mockStorage) | ||
| assertNotNull(key) | ||
| assertEquals(KeyType.SEED_PHRASE, key.keyType) | ||
| } | ||
| } | ||
|
|
||
| @Test | ||
| fun `test key creation with advanced options`() { | ||
| runBlocking { | ||
| val key = seedPhraseKeyProvider.create(Unit, mockStorage) | ||
| assertNotNull(key) | ||
| assertEquals(KeyType.SEED_PHRASE, key.keyType) | ||
| } | ||
| } | ||
|
|
||
| @Test | ||
| fun `test key storage and retrieval`() { | ||
| runBlocking { | ||
| val testId = "test_key" | ||
| val testPassword = "test_password" | ||
| val encryptedData = "encrypted_data".toByteArray() | ||
|
|
||
| `when`(mockStorage.get(testId)).thenReturn(encryptedData) | ||
|
|
||
| val key = seedPhraseKeyProvider.createAndStore(testId, testPassword, mockStorage) | ||
| assertNotNull(key) | ||
| assertEquals(KeyType.SEED_PHRASE, key.keyType) | ||
| verify(mockStorage).set(testId, any()) | ||
| } | ||
| } | ||
|
|
||
| @Test | ||
| fun `test storage failure scenarios`() { | ||
| runBlocking { | ||
| val testId = "test_key" | ||
| val testPassword = "test_password" | ||
|
|
||
| `when`(mockStorage.set(any(), any())).thenThrow(RuntimeException("Storage error")) | ||
|
|
||
| assertFailsWith<WalletError> { | ||
| seedPhraseKeyProvider.createAndStore(testId, testPassword, mockStorage) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| @Test | ||
| fun `test key retrieval with invalid password`() { | ||
| runBlocking { | ||
| val testId = "test_key" | ||
| val testPassword = "invalid_password" | ||
| val encryptedData = "encrypted_data".toByteArray() | ||
|
|
||
| `when`(mockStorage.get(testId)).thenReturn(encryptedData) | ||
|
|
||
| assertFailsWith<WalletError> { | ||
| seedPhraseKeyProvider.get(testId, testPassword, mockStorage) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| @Test | ||
| fun `test key restoration`() { | ||
| runBlocking { | ||
| val secret = "test_secret".toByteArray() | ||
| val restoredKey = seedPhraseKeyProvider.restore(secret, mockStorage) | ||
| assertNotNull(restoredKey) | ||
| assertEquals(KeyType.SEED_PHRASE, restoredKey.keyType) | ||
| } | ||
| } | ||
|
|
||
| @Test | ||
| fun `test key restoration with invalid data`() { | ||
| runBlocking { | ||
| val invalidSecret = ByteArray(32) { it.toByte() } | ||
| assertFailsWith<WalletError> { | ||
| seedPhraseKeyProvider.restore(invalidSecret, mockStorage) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| @Test | ||
| fun `test public key retrieval for different algorithms`() { | ||
| val p256Key = seedPhraseKeyProvider.publicKey(SigningAlgorithm.ECDSA_P256) | ||
| val secp256k1Key = seedPhraseKeyProvider.publicKey(SigningAlgorithm.ECDSA_secp256k1) | ||
|
|
||
| assertNotNull(p256Key) | ||
| assertNotNull(secp256k1Key) | ||
| if (p256Key != null) { | ||
| assertTrue(p256Key.isNotEmpty()) | ||
| } | ||
| if (secp256k1Key != null) { | ||
| assertTrue(secp256k1Key.isNotEmpty()) | ||
| } | ||
| } | ||
|
|
||
| @Test | ||
| fun `test signing and verification`() { | ||
| runBlocking { | ||
| val message = "test message".toByteArray() | ||
| val signature = seedPhraseKeyProvider.sign(message, SigningAlgorithm.ECDSA_P256, HashingAlgorithm.SHA2_256) | ||
|
|
||
| assertTrue(signature.isNotEmpty()) | ||
| assertTrue(seedPhraseKeyProvider.isValidSignature(signature, message, SigningAlgorithm.ECDSA_P256, HashingAlgorithm.SHA2_256)) | ||
| } | ||
| } | ||
|
|
||
| @Test | ||
| fun `test signing with different hashing algorithms`() { | ||
| runBlocking { | ||
| val message = "test message".toByteArray() | ||
|
|
||
| val sha2_256 = seedPhraseKeyProvider.sign(message, SigningAlgorithm.ECDSA_P256, HashingAlgorithm.SHA2_256) | ||
| val sha3_256 = seedPhraseKeyProvider.sign(message, SigningAlgorithm.ECDSA_P256, HashingAlgorithm.SHA3_256) | ||
|
|
||
| assertTrue(sha2_256.isNotEmpty()) | ||
| assertTrue(sha3_256.isNotEmpty()) | ||
| } | ||
| } | ||
|
|
||
| @Test | ||
| fun `test invalid signature verification`() { | ||
| val message = "test message".toByteArray() | ||
| val invalidSignature = "invalid signature".toByteArray() | ||
|
|
||
| assertFalse(seedPhraseKeyProvider.isValidSignature(invalidSignature, message, SigningAlgorithm.ECDSA_P256, HashingAlgorithm.SHA2_256)) | ||
| } | ||
|
|
||
| @Test | ||
| fun `test key removal`() { | ||
| runBlocking { | ||
| val testId = "test_key" | ||
| seedPhraseKeyProvider.remove(testId) | ||
| verify(mockStorage).remove(testId) | ||
| } | ||
| } | ||
|
|
||
| @Test | ||
| fun `test getting all keys`() { | ||
| val testKeys = listOf("key1", "key2") | ||
| `when`(mockStorage.allKeys).thenReturn(testKeys) | ||
|
|
||
| val allKeys = seedPhraseKeyProvider.allKeys() | ||
| assertEquals(testKeys, allKeys) | ||
| verify(mockStorage).allKeys | ||
| } | ||
|
|
||
| @Test | ||
| fun `test hardware backed property`() { | ||
| assertFalse(seedPhraseKeyProvider.isHardwareBacked) | ||
| } | ||
|
|
||
| @Test | ||
| fun `legacy keydata with length field is still readable`() { | ||
| runBlocking { | ||
| val storage = InMemoryStorage() | ||
| val password = "test_password" | ||
| val testId = "legacy_seed" | ||
|
|
||
| // Simulate old app writing a JSON blob that includes an extra "length" field. | ||
| // Cover both numeric and string enum representations to be safe. | ||
| val legacyJsonNumeric = """ | ||
| { | ||
| "mnemonic": "$validSeedPhrase", | ||
| "passphrase": "", | ||
| "path": "m/44'/539'/0'/0/0", | ||
| "length": 12 | ||
| } | ||
| """.trimIndent().toByteArray() | ||
| val legacyJsonStringEnum = """ | ||
| { | ||
| "mnemonic": "$validSeedPhrase", | ||
| "passphrase": "", | ||
| "path": "m/44'/539'/0'/0/0", | ||
| "length": "TWELVE" | ||
| } | ||
| """.trimIndent().toByteArray() | ||
|
|
||
| val cipher = ChaChaPolyCipher(password) | ||
| val encryptedNumeric = cipher.encrypt(legacyJsonNumeric) | ||
| storage.set(testId, encryptedNumeric) | ||
| val restoredNumeric = seedPhraseKeyProvider.get(testId, password, storage) as SeedPhraseKey | ||
| assertEquals(validSeedPhrase, restoredNumeric.mnemonic.joinToString(" ")) | ||
| assertEquals("m/44'/539'/0'/0/0", restoredNumeric.derivationPath) | ||
|
|
||
| val encryptedEnum = cipher.encrypt(legacyJsonStringEnum) | ||
| storage.set(testId, encryptedEnum) | ||
| val restoredEnum = seedPhraseKeyProvider.get(testId, password, storage) as SeedPhraseKey | ||
| assertEquals(validSeedPhrase, restoredEnum.mnemonic.joinToString(" ")) | ||
| assertEquals("m/44'/539'/0'/0/0", restoredEnum.derivationPath) | ||
|
|
||
| } | ||
| } | ||
| } |
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
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
TODOimplementation should not return a hardcodedByteArray(1). This could cause runtime issues if this method is actually called during testing.