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 1 commit
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
15 changes: 12 additions & 3 deletions bip39-lib/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ plugins {
id("signing")
}

val enableNative = project.property("NATIVE_TARGETS_ENABLED").toString().toBoolean()
val nativeTargets = if (enableNative) arrayOf(
val enableNonJvm = project.property("NONJVM_TARGETS_ENABLED").toString().toBoolean()
val nativeTargets = if (enableNonJvm) arrayOf(
"linuxX64",
"macosX64", "macosArm64",
"iosArm64", "iosX64", "iosSimulatorArm64",
Expand All @@ -32,6 +32,12 @@ kotlin {
useJUnitPlatform()
}
}
if (enableNonJvm) {
js(IR) {
browser() // to compile for the web
nodejs() // to compile against node
}
}
for (target in nativeTargets) {
targets.add(presets.getByName(target).createTarget(target))
}
Expand All @@ -58,13 +64,16 @@ kotlin {
implementation(libs.kotest.runner.junit5)
}
}
if (enableNative) {
if (enableNonJvm) {
val nonJvmMain by creating {
dependsOn(commonMain)
dependencies {
implementation(libs.com.squareup.okio)
}
}
val jsMain by getting {
dependsOn(nonJvmMain)
}
val mingwMain by creating {
dependsOn(nonJvmMain)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package cash.z.ecc.android.random

import kotlinx.browser.window
import org.w3c.dom.get

actual class SecureRandom {

var crypto: dynamic = null

init {
crypto = try {
// browser
window["crypto"]
} catch (_: Throwable) {
null
}
if (crypto == null) {
try {
// node
crypto = js("""require("crypto")""").webcrypto
} catch (_: Throwable) {
}
}
requireNotNull(crypto) { "Could not get crypto from window or node" }
}

actual fun nextBytes(bytes: ByteArray) {
crypto.getRandomValues(bytes)
}
}
4 changes: 4 additions & 0 deletions bip39-lib/webpack.config.d/resolve.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
config.resolve.fallback = {
...config.resolve.fallback,
crypto: false,
};
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ org.gradle.parallel=true
org.gradle.vfs.watch=true

kotlin.mpp.stability.nowarn=true
NATIVE_TARGETS_ENABLED=true
NONJVM_TARGETS_ENABLED=true

BIP39_IS_TREAT_WARNINGS_AS_ERRORS=true
BIP39_IS_COVERAGE_ENABLED=true
Expand Down
Loading