Skip to content

Commit

Permalink
feat(ton): Fix C++ interface
Browse files Browse the repository at this point in the history
* Add Kotlin, Swift tests
  • Loading branch information
satoshiotomakan committed Aug 2, 2024
1 parent aaa336b commit 048e7f0
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package com.trustwallet.core.app.utils

import com.trustwallet.core.app.utils.toHexBytes
import com.trustwallet.core.app.utils.toHex
import org.junit.Assert.*
import org.junit.Test
import wallet.core.jni.*

class TestCryptoBox {
init {
System.loadLibrary("TrustWalletCore")
}

@Test
fun testEncryptDecryptEasy() {
val mySecret = CryptoBoxSecretKey()
val myPubkey = mySecret.publicKey

val otherSecret = CryptoBoxSecretKey()
val otherPubkey = otherSecret.publicKey

val message = "Well done is better than well said. -Benjamin Franklin"
val encrypted = CryptoBox.encryptEasy(mySecret, otherPubkey, message.toByteArray())

// Step 2. Make sure the Box can be decrypted by the other side.
val decrypted = CryptoBox.decryptEasy(otherSecret, myPubkey, encrypted)
assertEquals(decrypted.toString(Charsets.UTF_8), message)
}
}
2 changes: 1 addition & 1 deletion include/TrustWalletCore/TWCryptoBoxPublicKey.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
TW_EXTERN_C_BEGIN

/// Public key used in `crypto_box` cryptography.
TW_EXPORT_STRUCT
TW_EXPORT_CLASS
struct TWCryptoBoxPublicKey;

/// Determines if the given public key is valid or not.
Expand Down
2 changes: 1 addition & 1 deletion include/TrustWalletCore/TWCryptoBoxSecretKey.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,6 @@ void TWCryptoBoxSecretKeyDelete(struct TWCryptoBoxSecretKey* _Nonnull key);
/// \param key *non-null* pointer to the private key.
/// \return *non-null* pointer to the corresponding public key.
TW_EXPORT_METHOD
struct TWCryptoBoxPublicKey* TWCryptoBoxSecretKeyGetPublicKey(struct TWCryptoBoxSecretKey* _Nonnull key);
struct TWCryptoBoxPublicKey* _Nonnull TWCryptoBoxSecretKeyGetPublicKey(struct TWCryptoBoxSecretKey* _Nonnull key);

TW_EXTERN_C_END
24 changes: 24 additions & 0 deletions swift/Tests/CryptoBoxTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// SPDX-License-Identifier: Apache-2.0
//
// Copyright © 2017 Trust Wallet.

import WalletCore
import XCTest

class CryptoBoxTests: XCTestCase {
func testEncryptDecryptEasy() {
let mySecret = CryptoBoxSecretKey()
let myPubkey = mySecret.getPublicKey()

let otherSecret = CryptoBoxSecretKey()
let otherPubkey = otherSecret.getPublicKey()

let message = "Well done is better than well said. -Benjamin Franklin"
let encrypted = CryptoBox.encryptEasy(mySecret: mySecret, otherPubkey: otherPubkey, message: Data(message.utf8))

// Step 2. Make sure the Box can be decrypted by the other side.
let decrypted = CryptoBox.decryptEasy(mySecret: otherSecret, otherPubkey: myPubkey, encrypted: encrypted)!
let decryptedStr = String(bytes: decrypted, encoding: .utf8)
XCTAssertEqual(decryptedStr, message)
}
}

0 comments on commit 048e7f0

Please sign in to comment.