Skip to content

Commit

Permalink
Do not nullify entropy when its returned as a part of Mnemonic (#95)
Browse files Browse the repository at this point in the history
* Do not nullify entropy when its returned as a part of Mnemonic

* Bump version
  • Loading branch information
valentunn authored Sep 3, 2024
1 parent eb9e234 commit c43c1cf
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 7 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
buildscript {
ext {
// App version
versionName = '2.1.3'
versionName = '2.1.4'
versionCode = 1

// SDK and tools
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import io.novasama.substrate_sdk_android.exceptions.Bip39Exception
import java.security.SecureRandom
import java.text.Normalizer
import java.text.Normalizer.normalize
import java.util.Arrays
import kotlin.math.floor

private val DELIMITER_REGEX = "[\\s,]+".toRegex()
Expand All @@ -21,11 +20,15 @@ object MnemonicCreator {
SecureRandom().nextBytes(entropy)
MnemonicGenerator(EnglishWordList.INSTANCE)
.createMnemonic(entropy, secure::append)
Arrays.fill(entropy, 0.toByte())

val words = secure.toStringAble().toString()
val normalizedWords = normalizeWords(words)

fromWords(words)
Mnemonic(
words = normalizedWords,
wordList = toWordList(normalizedWords),
entropy = entropy
)
}

fun fromWords(words: String): Mnemonic {
Expand Down Expand Up @@ -61,11 +64,10 @@ object MnemonicCreator {
}

private fun generateWords(entropy: ByteArray): String {
SecureCharBuffer().use { secure ->
return SecureCharBuffer().use { secure ->
MnemonicGenerator(EnglishWordList.INSTANCE)
.createMnemonic(entropy, secure::append)
Arrays.fill(entropy, 0.toByte())
return secure.toStringAble().toString()
secure.toStringAble().toString()
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ class MnemonicCreatorTest {
fun `should generate mnemonic of different length`() {
val mnemonic = MnemonicCreator.randomMnemonic(Mnemonic.Length.TWELVE)
assertEquals(12, mnemonic.wordList.size)
assertFalse(mnemonic.entropy.all { it.toInt() == 0 })

val mnemonic2 = MnemonicCreator.randomMnemonic(Mnemonic.Length.TWENTY_ONE)
assertEquals(21, mnemonic2.wordList.size)
Expand All @@ -29,6 +30,7 @@ class MnemonicCreatorTest {
val mnemonic = MnemonicCreator.fromEntropy(expectedEntropyHex.fromHex())

assertEquals(expectedMnemonicWords, mnemonic.words)
assertArrayEquals(expectedEntropyHex.fromHex(), mnemonic.entropy)
}

@Test
Expand Down

0 comments on commit c43c1cf

Please sign in to comment.