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

Update the crypto libs and clean up a test case to match #10

Merged
merged 1 commit into from
Dec 22, 2023
Merged
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
13 changes: 8 additions & 5 deletions javatest/src/test/java/me/uma/javatest/UmaTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertTrue;

import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
Expand All @@ -27,8 +28,8 @@

public class UmaTest {
UmaProtocolHelper umaProtocolHelper = new UmaProtocolHelper(new InMemoryPublicKeyCache(), new TestUmaRequester());
private static String PUBKEY_HEX = "02061e5634646e60cfbe2ca42e2be920b4deb749f0159ed7c428cdd8e3ea69c133";
private static String PRIVKEY_HEX = "0e120a3c9ff18d295c6452cbb7ee3bb0f3d9c34f4db9e62293d2773f338a3b9d";
private static String PUBKEY_HEX = "04f2998ab056897ddb91b5e6fad1e4bb6c4b7dda427409f667d0f4694b553e4feeeb08936c2993f7b931f6a3fa7e846f11165fae222de5e4a55c12def21a7c9fcf";
private static String PRIVKEY_HEX = "10fbbee8f689b207bb22df2dfa27827ae9ae02e265980ea09ef5101ed5fb508f";


@Test
Expand All @@ -49,13 +50,15 @@ public void testFetchPublicKeyFuture() throws Exception {
public void testGetLnurlpRequest() throws Exception {
String lnurlpUrl = umaProtocolHelper.getSignedLnurlpRequestUrl(
privateKeyBytes(),
"[email protected]",
"https://vasp.com",
true);
"[email protected]",
/* senderVaspDomain */ "myvasp.com",
/* isSubjectToTravelRule */ true
);
assertNotNull(lnurlpUrl);
System.out.println(lnurlpUrl);
LnurlpRequest request = umaProtocolHelper.parseLnurlpRequest(lnurlpUrl);
assertNotNull(request);
assertTrue(umaProtocolHelper.verifyUmaLnurlpQuerySignature(request, new PubKeyResponse(publicKeyBytes(), publicKeyBytes())));
System.out.println(request);
}

Expand Down
52 changes: 23 additions & 29 deletions uma-sdk/src/commonMain/kotlin/me/uma/crypto/internal/UmaCrypto.kt
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ open class RustBuffer : Structure() {
companion object {
internal fun alloc(size: Int = 0) =
rustCall { status ->
_UniFFILib.INSTANCE.ffi_uma_crypto_d9b9_rustbuffer_alloc(size, status).also {
_UniFFILib.INSTANCE.ffi_uma_crypto_b9a_rustbuffer_alloc(size, status).also {
if (it.data == null) {
throw RuntimeException("RustBuffer.alloc() returned null data pointer (size=$size)")
}
Expand All @@ -55,7 +55,7 @@ open class RustBuffer : Structure() {

internal fun free(buf: RustBuffer.ByValue) =
rustCall { status ->
_UniFFILib.INSTANCE.ffi_uma_crypto_d9b9_rustbuffer_free(buf, status)
_UniFFILib.INSTANCE.ffi_uma_crypto_b9a_rustbuffer_free(buf, status)
}
}

Expand Down Expand Up @@ -273,64 +273,64 @@ internal interface _UniFFILib : Library {
}
}

fun ffi_uma_crypto_d9b9_KeyPair_object_free(
fun ffi_uma_crypto_b9a_KeyPair_object_free(
`ptr`: Pointer,
_uniffi_out_err: RustCallStatus,
): Unit

fun uma_crypto_d9b9_KeyPair_get_public_key(
fun uma_crypto_b9a_KeyPair_get_public_key(
`ptr`: Pointer,
_uniffi_out_err: RustCallStatus,
): RustBuffer.ByValue

fun uma_crypto_d9b9_KeyPair_get_private_key(
fun uma_crypto_b9a_KeyPair_get_private_key(
`ptr`: Pointer,
_uniffi_out_err: RustCallStatus,
): RustBuffer.ByValue

fun uma_crypto_d9b9_sign_ecdsa(
fun uma_crypto_b9a_sign_ecdsa(
`msg`: RustBuffer.ByValue,
`privateKeyBytes`: RustBuffer.ByValue,
_uniffi_out_err: RustCallStatus,
): RustBuffer.ByValue

fun uma_crypto_d9b9_verify_ecdsa(
fun uma_crypto_b9a_verify_ecdsa(
`msg`: RustBuffer.ByValue,
`signatureBytes`: RustBuffer.ByValue,
`publicKeyBytes`: RustBuffer.ByValue,
_uniffi_out_err: RustCallStatus,
): Byte

fun uma_crypto_d9b9_encrypt_ecies(
fun uma_crypto_b9a_encrypt_ecies(
`msg`: RustBuffer.ByValue,
`publicKeyBytes`: RustBuffer.ByValue,
_uniffi_out_err: RustCallStatus,
): RustBuffer.ByValue

fun uma_crypto_d9b9_decrypt_ecies(
fun uma_crypto_b9a_decrypt_ecies(
`cipherText`: RustBuffer.ByValue,
`privateKeyBytes`: RustBuffer.ByValue,
_uniffi_out_err: RustCallStatus,
): RustBuffer.ByValue

fun uma_crypto_d9b9_generate_keypair(_uniffi_out_err: RustCallStatus): Pointer
fun uma_crypto_b9a_generate_keypair(_uniffi_out_err: RustCallStatus): Pointer

fun ffi_uma_crypto_d9b9_rustbuffer_alloc(
fun ffi_uma_crypto_b9a_rustbuffer_alloc(
`size`: Int,
_uniffi_out_err: RustCallStatus,
): RustBuffer.ByValue

fun ffi_uma_crypto_d9b9_rustbuffer_from_bytes(
fun ffi_uma_crypto_b9a_rustbuffer_from_bytes(
`bytes`: ForeignBytes.ByValue,
_uniffi_out_err: RustCallStatus,
): RustBuffer.ByValue

fun ffi_uma_crypto_d9b9_rustbuffer_free(
fun ffi_uma_crypto_b9a_rustbuffer_free(
`buf`: RustBuffer.ByValue,
_uniffi_out_err: RustCallStatus,
): Unit

fun ffi_uma_crypto_d9b9_rustbuffer_reserve(
fun ffi_uma_crypto_b9a_rustbuffer_reserve(
`buf`: RustBuffer.ByValue,
`additional`: Int,
_uniffi_out_err: RustCallStatus,
Expand Down Expand Up @@ -444,6 +444,7 @@ public object FfiConverterString : FfiConverter<String, RustBuffer.ByValue> {
// helper method to execute a block and destroy the object at the end.
interface Disposable {
fun destroy()

companion object {
fun destroy(vararg args: Any?) {
args.filterIsInstance<Disposable>()
Expand Down Expand Up @@ -614,14 +615,14 @@ class KeyPair(
*/
protected override fun freeRustArcPtr() {
rustCall { status ->
_UniFFILib.INSTANCE.ffi_uma_crypto_d9b9_KeyPair_object_free(this.pointer, status)
_UniFFILib.INSTANCE.ffi_uma_crypto_b9a_KeyPair_object_free(this.pointer, status)
}
}

override fun `getPublicKey`(): List<UByte> =
callWithPointer {
rustCall { _status ->
_UniFFILib.INSTANCE.uma_crypto_d9b9_KeyPair_get_public_key(it, _status)
_UniFFILib.INSTANCE.uma_crypto_b9a_KeyPair_get_public_key(it, _status)
}
}.let {
FfiConverterSequenceUByte.lift(it)
Expand All @@ -630,7 +631,7 @@ class KeyPair(
override fun `getPrivateKey`(): List<UByte> =
callWithPointer {
rustCall { _status ->
_UniFFILib.INSTANCE.uma_crypto_d9b9_KeyPair_get_private_key(it, _status)
_UniFFILib.INSTANCE.uma_crypto_b9a_KeyPair_get_private_key(it, _status)
}
}.let {
FfiConverterSequenceUByte.lift(it)
Expand Down Expand Up @@ -667,8 +668,6 @@ sealed class CryptoException(message: String) : Exception(message) {
// Flat enums carries a string error message, so no special implementation is necessary.
class Secp256k1Exception(message: String) : CryptoException(message)

class RustSecp256k1Exception(message: String) : CryptoException(message)

companion object ErrorHandler : CallStatusErrorHandler<CryptoException> {
override fun lift(error_buf: RustBuffer.ByValue): CryptoException = FfiConverterTypeCryptoError.lift(error_buf)
}
Expand All @@ -678,7 +677,6 @@ public object FfiConverterTypeCryptoError : FfiConverterRustBuffer<CryptoExcepti
override fun read(buf: ByteBuffer): CryptoException {
return when (buf.getInt()) {
1 -> CryptoException.Secp256k1Exception(FfiConverterString.read(buf))
2 -> CryptoException.RustSecp256k1Exception(FfiConverterString.read(buf))
else -> throw RuntimeException("invalid error enum value, something is very wrong!!")
}
}
Expand All @@ -696,10 +694,6 @@ public object FfiConverterTypeCryptoError : FfiConverterRustBuffer<CryptoExcepti
buf.putInt(1)
Unit
}
is CryptoException.RustSecp256k1Exception -> {
buf.putInt(2)
Unit
}
}.let { /* this makes the `when` an expression, which ensures it is exhaustive */ }
}
}
Expand Down Expand Up @@ -736,7 +730,7 @@ fun `signEcdsa`(
): List<UByte> {
return FfiConverterSequenceUByte.lift(
rustCallWithError(CryptoException) { _status ->
_UniFFILib.INSTANCE.uma_crypto_d9b9_sign_ecdsa(
_UniFFILib.INSTANCE.uma_crypto_b9a_sign_ecdsa(
FfiConverterSequenceUByte.lower(`msg`),
FfiConverterSequenceUByte.lower(`privateKeyBytes`),
_status,
Expand All @@ -753,7 +747,7 @@ fun `verifyEcdsa`(
): Boolean {
return FfiConverterBoolean.lift(
rustCallWithError(CryptoException) { _status ->
_UniFFILib.INSTANCE.uma_crypto_d9b9_verify_ecdsa(
_UniFFILib.INSTANCE.uma_crypto_b9a_verify_ecdsa(
FfiConverterSequenceUByte.lower(`msg`),
FfiConverterSequenceUByte.lower(`signatureBytes`),
FfiConverterSequenceUByte.lower(`publicKeyBytes`),
Expand All @@ -770,7 +764,7 @@ fun `encryptEcies`(
): List<UByte> {
return FfiConverterSequenceUByte.lift(
rustCallWithError(CryptoException) { _status ->
_UniFFILib.INSTANCE.uma_crypto_d9b9_encrypt_ecies(
_UniFFILib.INSTANCE.uma_crypto_b9a_encrypt_ecies(
FfiConverterSequenceUByte.lower(`msg`),
FfiConverterSequenceUByte.lower(`publicKeyBytes`),
_status,
Expand All @@ -786,7 +780,7 @@ fun `decryptEcies`(
): List<UByte> {
return FfiConverterSequenceUByte.lift(
rustCallWithError(CryptoException) { _status ->
_UniFFILib.INSTANCE.uma_crypto_d9b9_decrypt_ecies(
_UniFFILib.INSTANCE.uma_crypto_b9a_decrypt_ecies(
FfiConverterSequenceUByte.lower(`cipherText`),
FfiConverterSequenceUByte.lower(`privateKeyBytes`),
_status,
Expand All @@ -799,7 +793,7 @@ fun `decryptEcies`(
fun `generateKeypair`(): KeyPair {
return FfiConverterTypeKeyPair.lift(
rustCallWithError(CryptoException) { _status ->
_UniFFILib.INSTANCE.uma_crypto_d9b9_generate_keypair(_status)
_UniFFILib.INSTANCE.uma_crypto_b9a_generate_keypair(_status)
},
)
}
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.