Skip to content

Commit

Permalink
Merge pull request #19 from provenance-io/add-java-key-conversion
Browse files Browse the repository at this point in the history
add conversion from PrivateKey -> Java Private Key
  • Loading branch information
celloman authored Dec 6, 2021
2 parents 915b6c2 + ba939a7 commit 07711e7
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 1 deletion.
22 changes: 22 additions & 0 deletions ec/src/main/kotlin/io/provenance/hdwallet/ec/Curve.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,14 @@ import io.provenance.hdwallet.ec.bc.toCurve
import io.provenance.hdwallet.ec.bc.toCurvePoint
import org.bouncycastle.crypto.ec.CustomNamedCurves
import org.bouncycastle.crypto.params.ECDomainParameters
import org.bouncycastle.jcajce.provider.asymmetric.util.EC5Util
import org.bouncycastle.math.ec.ECCurve
import org.bouncycastle.math.ec.ECPoint
import org.bouncycastle.math.ec.FixedPointCombMultiplier
import java.math.BigInteger
import java.security.spec.ECParameterSpec
import org.bouncycastle.jce.spec.ECParameterSpec as BCECParameterSpec
import java.security.spec.EllipticCurve

class CurvePoint(val ecPoint: ECPoint) {
val x: BigInteger = ecPoint.xCoord.toBigInteger()
Expand All @@ -18,6 +22,12 @@ class CurvePoint(val ecPoint: ECPoint) {
fun mul(n: BigInteger): CurvePoint = ecPoint.multiply(n).toCurvePoint()
fun add(n: CurvePoint): CurvePoint = ecPoint.add(n.ecPoint).toCurvePoint()
fun normalize(): CurvePoint = ecPoint.normalize().toCurvePoint()

fun toJavaECPoint(): java.security.spec.ECPoint =
java.security.spec.ECPoint(x, y)

fun toBCECPoint(curve: ECCurve): ECPoint =
EC5Util.convertPoint(curve, toJavaECPoint())
}

data class Curve(val n: BigInteger, val g: CurvePoint, private val ecCurve: ECCurve) {
Expand All @@ -32,6 +42,12 @@ data class Curve(val n: BigInteger, val g: CurvePoint, private val ecCurve: ECCu
return BigInteger(1, encoded.copyOfRange(1, encoded.size))
}

fun toJavaEllipticCurve(): EllipticCurve =
EC5Util.convertCurve(ecCurve, ecDomainParameters.seed)

fun toBCEllipticCurve(): ECCurve =
EC5Util.convertCurve(toJavaEllipticCurve())

private fun fpcMul(pk: BigInteger): CurvePoint {
val postProcessedPrivateKey =
if (pk.bitLength() > n.bitLength()) pk.mod(n)
Expand All @@ -49,3 +65,9 @@ val secp256r1Curve = Curve.lookup("secp256r1")

// Provenance defaults to the secp256k1 EC curve for keys and signatures.
val CURVE = secp256k1Curve

val Curve.ecParameterSpec: ECParameterSpec
get() = ECParameterSpec(toJavaEllipticCurve(), g.toJavaECPoint(), n, ecDomainParameters.h.toInt())

val Curve.bcecParameterSpec: BCECParameterSpec
get() = toBCEllipticCurve().let { bcCurve -> BCECParameterSpec(bcCurve, g.toBCECPoint(bcCurve), n, ecDomainParameters.h) }
8 changes: 8 additions & 0 deletions ec/src/main/kotlin/io/provenance/hdwallet/ec/JavaKeys.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@ package io.provenance.hdwallet.ec
import io.provenance.hdwallet.ec.bc.toCurvePoint
import org.bouncycastle.jcajce.provider.asymmetric.ec.BCECPrivateKey
import org.bouncycastle.jcajce.provider.asymmetric.ec.BCECPublicKey
import org.bouncycastle.jce.ECNamedCurveTable
import org.bouncycastle.jce.spec.ECParameterSpec
import org.bouncycastle.jce.spec.ECPrivateKeySpec
import java.security.KeyFactory
import java.security.PrivateKey as JavaPrivateKey
import java.security.PublicKey as JavaPublicKey

Expand All @@ -18,3 +21,8 @@ fun JavaPrivateKey.toECPrivateKey(): PrivateKey {
val bcec = requireNotNull(this as? BCECPrivateKey) { "key type invalid" }
return PrivateKey(bcec.d, bcec.parameters.toCurve())
}

fun PrivateKey.toJavaPrivateKey(): JavaPrivateKey {
val keySpec = ECPrivateKeySpec(key, this.curve.bcecParameterSpec)
return KeyFactory.getInstance("EC").generatePrivate(keySpec)
}
2 changes: 1 addition & 1 deletion hdwallet/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
dependencies {
listOf(":base58", ":bech32", ":bip32", ":bip39", ":bip44", ":common", ":ec", ":signer")
.map { implementation(project(it)) }
.map { api(project(it)) }
}

0 comments on commit 07711e7

Please sign in to comment.