Skip to content
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

Add JS target #191

Open
wants to merge 18 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
147 changes: 87 additions & 60 deletions bip39-lib/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import java.util.Base64
import java.util.*

plugins {
// https://github.com/gradle/gradle/issues/20084#issuecomment-1060822638
id(libs.plugins.kotlin.multiplatform.get().pluginId)
alias(libs.plugins.dokka)
alias(libs.plugins.kotest)
id("bip39.kotlin-multiplatform-conventions")
id("bip39.dependency-conventions")

Expand All @@ -17,23 +16,51 @@ plugins {
}

val enableNative = project.property("NATIVE_TARGETS_ENABLED").toString().toBoolean()
val nativeTargets = if (enableNative) arrayOf(
"linuxX64",
"macosX64", "macosArm64",
"iosArm64", "iosX64", "iosSimulatorArm64",
"tvosArm64", "tvosX64", "tvosSimulatorArm64",
"watchosArm32", "watchosArm64", "watchosX64", "watchosSimulatorArm64",
"mingwX64"
) else arrayOf()
val enableJs = project.property("JS_TARGET_ENABLED").toString().toBoolean()
val nativeTargets =
if (enableNative) {
arrayOf(
"linuxX64",
"macosX64", "macosArm64",
"iosArm64", "iosX64", "iosSimulatorArm64",
"tvosArm64", "tvosX64", "tvosSimulatorArm64",
"watchosArm32", "watchosArm64", "watchosX64", "watchosSimulatorArm64",
"mingwX64",
)
} else {
arrayOf()
}

kotlin {
jvm {
testRuns["test"].executionTask.configure {
useJUnitPlatform()
}
}
for (target in nativeTargets) {
targets.add(presets.getByName(target).createTarget(target))
if (enableJs) {
js(IR) {
browser {
testTask {
useMocha {
// Needed due to: https://github.com/square/okio/issues/1206
timeout = "60s"
}
}
}
nodejs {
testTask {
useMocha {
// Needed due to: https://github.com/square/okio/issues/1206
timeout = "60s"
}
}
}
}
}
if (enableNative) {
for (target in nativeTargets) {
targets.add(presets.getByName(target).createTarget(target))
}
}

sourceSets {
Expand All @@ -44,44 +71,40 @@ kotlin {
val commonTest by getting {
dependencies {
implementation(kotlin("test"))
implementation(libs.kotest.framework.engine)
implementation(libs.kotest.assertion)
implementation(libs.kotest.property)
}
}
@Suppress("UnusedPrivateProperty")
val jvmMain by getting {
dependencies {
}
}
@Suppress("UnusedPrivateProperty")
val jvmTest by getting {
dependencies {
implementation(libs.kotest.runner.junit5)
}
}
if (enableNative) {

if (enableNative || enableJs) {
val nonJvmMain by creating {
dependsOn(commonMain)
dependencies {
implementation(libs.com.squareup.okio)
}
}
val mingwMain by creating {
dependsOn(nonJvmMain)
}
val unixMain by creating {
dependsOn(nonJvmMain)

if (enableJs) {
jsMain {
dependsOn(nonJvmMain)
}
}
for (target in nativeTargets) {
when (target) {
"mingwX64" ->
getByName("${target}Main").dependsOn(mingwMain)

else ->
getByName("${target}Main").dependsOn(unixMain)
if (enableNative) {
val mingwMain by creating {
dependsOn(nonJvmMain)
}
val unixMain by creating {
dependsOn(nonJvmMain)
}
for (target in nativeTargets) {
when (target) {
"mingwX64" ->
getByName("${target}Main").dependsOn(mingwMain)

else ->
getByName("${target}Main").dependsOn(unixMain)
}
getByName("${target}Test").dependsOn(commonTest)
}
getByName("${target}Test").dependsOn(commonTest)
}
}
}
Expand All @@ -94,9 +117,10 @@ tasks {
outputDirectory.set(dokkaOutputDir)
}

val deleteDokkaOutputDir = register<Delete>("deleteDokkaOutputDirectory") {
delete(dokkaOutputDir)
}
val deleteDokkaOutputDir =
register<Delete>("deleteDokkaOutputDirectory") {
delete(dokkaOutputDir)
}

register<Jar>("javadocJar") {
dependsOn(deleteDokkaOutputDir, dokkaHtml)
Expand All @@ -119,11 +143,12 @@ publishing {
// platform specific suffixes. Doing a partial replacement is the way to rename the artifact.
artifactId = artifactId.replace(project.name, myArtifactId)
groupId = "cash.z.ecc.android"
version = if (isSnapshot) {
"$myVersion-SNAPSHOT"
} else {
myVersion
}
version =
if (isSnapshot) {
"$myVersion-SNAPSHOT"
} else {
myVersion
}

pom {
name.set("Kotlin BIP-39")
Expand Down Expand Up @@ -153,11 +178,12 @@ publishing {
}
}
repositories {
val mavenUrl = if (isSnapshot) {
project.property("ZCASH_MAVEN_PUBLISH_SNAPSHOT_URL").toString()
} else {
project.property("ZCASH_MAVEN_PUBLISH_RELEASE_URL").toString()
}
val mavenUrl =
if (isSnapshot) {
project.property("ZCASH_MAVEN_PUBLISH_SNAPSHOT_URL").toString()
} else {
project.property("ZCASH_MAVEN_PUBLISH_RELEASE_URL").toString()
}
val mavenPublishUsername = project.property("ZCASH_MAVEN_PUBLISH_USERNAME").toString()
val mavenPublishPassword = project.property("ZCASH_MAVEN_PUBLISH_PASSWORD").toString()

Expand All @@ -178,15 +204,16 @@ signing {
// Maven Central requires signing for non-snapshots
isRequired = !isSnapshot

val signingKey = run {
val base64EncodedKey = project.property("ZCASH_ASCII_GPG_KEY").toString()
if (base64EncodedKey.isNotEmpty()) {
val keyBytes = Base64.getDecoder().decode(base64EncodedKey)
String(keyBytes)
} else {
""
val signingKey =
run {
val base64EncodedKey = project.property("ZCASH_ASCII_GPG_KEY").toString()
if (base64EncodedKey.isNotEmpty()) {
val keyBytes = Base64.getDecoder().decode(base64EncodedKey)
String(keyBytes)
} else {
""
}
}
}

if (signingKey.isNotEmpty()) {
useInMemoryPgpKeys(signingKey, "")
Expand Down
278 changes: 67 additions & 211 deletions bip39-lib/gradle.lockfile

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -43,17 +43,17 @@ object Mnemonics {
Closeable, Iterable<String> {
constructor(
phrase: String,
languageCode: String = DEFAULT_LANGUAGE_CODE
languageCode: String = DEFAULT_LANGUAGE_CODE,
) : this(phrase.toCharArray(), languageCode)

constructor(
entropy: ByteArray,
languageCode: String = DEFAULT_LANGUAGE_CODE
languageCode: String = DEFAULT_LANGUAGE_CODE,
) : this(computeSentence(entropy), languageCode)

constructor(
wordCount: WordCount,
languageCode: String = DEFAULT_LANGUAGE_CODE
languageCode: String = DEFAULT_LANGUAGE_CODE,
) : this(computeSentence(wordCount.toEntropy()), languageCode)

override fun close() = clear()
Expand Down Expand Up @@ -215,7 +215,7 @@ object Mnemonics {
*/
private fun computeSentence(
entropy: ByteArray,
languageCode: String = DEFAULT_LANGUAGE_CODE
languageCode: String = DEFAULT_LANGUAGE_CODE,
): CharArray {
// initialize state
var index = 0
Expand All @@ -226,7 +226,7 @@ object Mnemonics {
// Note: the excess bits of the checksum are intentionally ignored, per BIP-39
fun processBit(
bit: Boolean,
chars: ArrayList<Char>
chars: ArrayList<Char>,
) {
// update the index
index = index shl 1
Expand Down Expand Up @@ -268,7 +268,8 @@ object Mnemonics {
COUNT_15(15),
COUNT_18(18),
COUNT_21(21),
COUNT_24(24);
COUNT_24(24),
;

/**
* The bit length of the entropy necessary to create a mnemonic with the given word count.
Expand All @@ -294,7 +295,7 @@ object Mnemonics {

object ChecksumException :
RuntimeException(
"Error: The checksum failed. Verify that none of the words have been transposed."
"Error: The checksum failed. Verify that none of the words have been transposed.",
)

class WordCountException(count: Int) :
Expand Down Expand Up @@ -332,7 +333,7 @@ object Mnemonics {
fun MnemonicCode.toSeed(
// expect: UTF-8 normalized with NFKD
passphrase: CharArray = charArrayOf(),
validate: Boolean = true
validate: Boolean = true,
): ByteArray {
// we can skip validation when we know for sure that the code is valid
// such as when it was just generated from new/correct entropy (common case for new seeds)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ class WordList internal constructor(val languageCode: String = DEFAULT_LANGUAGE_
fun validate(languageCode: String) {
if (!isSupported(languageCode)) {
throw UnsupportedOperationException(
"The language <$languageCode> is not currently supported"
"The language <$languageCode> is not currently supported",
)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,6 @@ internal expect object Pbkdf2Sha512 {
p: CharArray,
s: ByteArray,
c: Int,
dkLen: Int
dkLen: Int,
): ByteArray
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ internal expect class SecretKeyFactoryCommon {

fun getInstance(
algorithm: String,
provider: FallbackProvider
provider: FallbackProvider,
): SecretKeyFactoryCommon
}
}
Loading