Initial commit

This initial commit includes HUSH specific changes starting at this commit:
d14637012c
This commit is contained in:
fekt
2022-11-29 20:49:44 -05:00
commit 4adbc901a0
355 changed files with 31799 additions and 0 deletions

1
mnemonic/.gitignore vendored Normal file
View File

@@ -0,0 +1 @@
/build

14
mnemonic/build.gradle Normal file
View File

@@ -0,0 +1,14 @@
import cash.z.ecc.android.Deps
apply plugin: 'kotlin'
dependencies {
implementation Deps.JavaX.INJECT
implementation Deps.Kotlin.STDLIB
// Zcash
implementation Deps.Zcash.ANDROID_WALLET_PLUGINS
implementation Deps.Zcash.KOTLIN_BIP39
testImplementation Deps.Test.JUNIT
}

View File

@@ -0,0 +1,18 @@
package cash.z.ecc.kotlin.mnemonic
import java.util.*
/**
* Clears out the given char array in memory, for security purposes.
*/
fun CharArray.clear() {
Arrays.fill(this, '0')
}
/**
* Clears out the given byte array in memory, for security purposes.
*/
fun ByteArray.clear() {
Arrays.fill(this, 0.toByte())
}

View File

@@ -0,0 +1,25 @@
package cash.z.ecc.kotlin.mnemonic
import cash.z.android.plugin.MnemonicPlugin
import cash.z.ecc.android.bip39.Mnemonics
import cash.z.ecc.android.bip39.Mnemonics.MnemonicCode
import cash.z.ecc.android.bip39.Mnemonics.WordCount
import cash.z.ecc.android.bip39.toEntropy
import cash.z.ecc.android.bip39.toSeed
import java.util.*
import java.util.Locale.ENGLISH
class Mnemonics : MnemonicPlugin {
override fun fullWordList(languageCode: String) = Mnemonics.getCachedWords(Locale.ENGLISH.language)
override fun nextEntropy(): ByteArray = WordCount.COUNT_24.toEntropy()
override fun nextMnemonic(): CharArray = MnemonicCode(WordCount.COUNT_24).chars
override fun nextMnemonic(entropy: ByteArray): CharArray = MnemonicCode(entropy).chars
override fun nextMnemonicList(): List<CharArray> = MnemonicCode(WordCount.COUNT_24).words
override fun nextMnemonicList(entropy: ByteArray): List<CharArray> = MnemonicCode(entropy).words
override fun toSeed(mnemonic: CharArray): ByteArray = MnemonicCode(mnemonic).toSeed()
override fun toWordList(mnemonic: CharArray): List<CharArray> = MnemonicCode(mnemonic).words
fun validate(mnemonic: CharArray) {
MnemonicCode(mnemonic).validate()
}
}

View File

@@ -0,0 +1,90 @@
package cash.z.ecc.android.util
import cash.z.android.plugin.MnemonicPlugin
import cash.z.ecc.kotlin.mnemonic.Mnemonics
import org.junit.Assert.assertEquals
import org.junit.Before
import org.junit.Test
import java.lang.Math.max
import java.util.*
class MnemonicTest {
lateinit var mnemonics: MnemonicPlugin
@Before
fun start() {
mnemonics = Mnemonics()
}
@Test
fun testSeed_fromMnemonic() {
val seed = mnemonics.run {
toSeed(nextMnemonic())
}
assertEquals(64, seed.size)
}
@Test
fun testMnemonic_create() {
val words = String(mnemonics.nextMnemonic()).split(' ')
assertEquals(24, words.size)
validate(words)
}
@Test
fun testMnemonic_createList() {
val words = mnemonics.nextMnemonicList()
assertEquals(24, words.size)
validate(words.map { String(it) })
}
@Test
fun testMnemonic_toList() {
val words = mnemonics.run {
toWordList(nextMnemonic())
}
assertEquals(24, words.size)
validate(words.map { String(it) })
}
@Test
fun testMnemonic_longestWord() {
var max = 0
val englishWordList = mnemonics.fullWordList(Locale.ENGLISH.language)
repeat(2048) {
max = max(max, englishWordList[it].length)
}
assertEquals(8, max)
}
private fun validate(words: List<String>) {
val englishWordList = mnemonics.fullWordList(Locale.ENGLISH.language)
// return or crash!
words.forEach { word ->
var i = 0
while (true) {
if (englishWordList[i++] == word) {
println(word)
break
}
}
}
}
}
private fun CharSequence.toWords(): List<CharSequence> {
return mutableListOf<CharSequence>().let { result ->
var index = 0
repeat(length) {
if (this[it] == ' ') {
result.add(subSequence(index, it))
index = it + 1
}
}
result.add(subSequence(index, length))
result
}
}