Skip to content

Commit

Permalink
refactor(rn): clean things up so they're more digestable
Browse files Browse the repository at this point in the history
  • Loading branch information
moldy530 committed Nov 6, 2024
1 parent 4dcf02b commit 30d8d27
Show file tree
Hide file tree
Showing 14 changed files with 379 additions and 6,534 deletions.
4 changes: 3 additions & 1 deletion .eslintignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,6 @@ site/.vitepress/cache/**/*

account-kit/rn-signer/lib/*
**/android/generated/*
**/ios/generated/*
**/ios/generated/*
**/build/*
**/.cxx/*
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ dist
node_modules
tsconfig*.tsbuildinfo
.idea/
.nx

# local env files
.env
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package com.accountkit.reactnativesigner

import com.google.crypto.tink.CleartextKeysetHandle
import com.google.crypto.tink.InsecureSecretKeyAccess
import com.google.crypto.tink.KeysetHandle
import com.google.crypto.tink.hybrid.HpkeParameters
import com.google.crypto.tink.hybrid.HpkePrivateKey
import com.google.crypto.tink.hybrid.HpkePublicKey
import com.google.crypto.tink.subtle.EllipticCurves
import com.google.crypto.tink.util.Bytes
import com.google.crypto.tink.util.SecretBytes
import java.security.interfaces.ECPublicKey
import javax.xml.bind.DatatypeConverter
import com.google.crypto.tink.proto.HpkePrivateKey as ProtoHpkePrivateKey
import com.google.crypto.tink.proto.HpkePublicKey as ProtoHpkePublicKey

// Keyset Handle Extensions
fun KeysetHandle.toHpkePublicKey(hpkeParameters: HpkeParameters): HpkePublicKey {
val keySet = CleartextKeysetHandle.getKeyset(this.publicKeysetHandle)
val protoKey = ProtoHpkePublicKey.parseFrom(keySet.keyList[0].keyData.value)

return HpkePublicKey.create(
hpkeParameters,
Bytes.copyFrom(protoKey.publicKey.toByteArray()),
null
)
}

fun KeysetHandle.toHpkePrivateKey(hpkeParams: HpkeParameters): HpkePrivateKey {
val publicKey = this.toHpkePublicKey(hpkeParams)
val pkKs = CleartextKeysetHandle.getKeyset(this)
val pkKeyData = pkKs.keyList[0].keyData
if (pkKeyData.typeUrl != "type.googleapis.com/google.crypto.tink.HpkePrivateKey") {
throw Error("invalid key type")
}

return HpkePrivateKey.create(
HpkePublicKey.create(
hpkeParams,
Bytes.copyFrom(publicKey.toByteArray()),
null
),
SecretBytes.copyFrom(
ProtoHpkePrivateKey.parseFrom(pkKeyData.value).privateKey.toByteArray(),
InsecureSecretKeyAccess.get()
)
)
}

// HPKE Public Key Extensions
fun HpkePublicKey.toHex(): String {
return this.toByteArray().toHex()
}

fun HpkePublicKey.toByteArray(): ByteArray {
return this.publicKeyBytes.toByteArray()
}

// ECPublicKey Extensions
fun ECPublicKey.toBytes(
pfType: EllipticCurves.PointFormatType
): ByteArray {
return EllipticCurves.pointEncode(
this.params.curve,
pfType,
this.w
)
}

// Conversions from Hex <-> byte[]
fun String.fromHex(): ByteArray {
return DatatypeConverter.parseHexBinary(this)
}

fun ByteArray.toHex(): String {
return DatatypeConverter.printHexBinary(this)
}
Loading

0 comments on commit 30d8d27

Please sign in to comment.