Skip to content

Commit

Permalink
[WalletConnect/BNB]: Add Android test
Browse files Browse the repository at this point in the history
  • Loading branch information
satoshiotomakan committed Jan 8, 2024
1 parent 2492dfc commit 3c65772
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 9 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package com.trustwallet.core.app.blockchains.binance

import com.google.protobuf.ByteString
import com.trustwallet.core.app.utils.toHexBytes
import org.junit.Assert.assertEquals
import org.junit.Test
import wallet.core.jni.proto.Binance.SigningOutput
import wallet.core.jni.proto.WalletConnect
import wallet.core.jni.*
import wallet.core.jni.CoinType.BINANCE
import wallet.core.java.AnySigner
import wallet.core.jni.proto.Common

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

@Test
fun testSignBinanceTransactionFromWalletConnectRequest() {
val parsingInput = WalletConnect.ParseRequestInput.newBuilder().apply {
method = WalletConnect.Method.CosmosSignAmino
payload = "{\"signerAddress\":\"bnb1grpf0955h0ykzq3ar5nmum7y6gdfl6lxfn46h2\",\"signDoc\":{\"account_number\":\"19\",\"chain_id\":\"chain-bnb\",\"memo\":\"\",\"data\":null,\"msgs\":[{\"inputs\":[{\"address\":\"bnb1grpf0955h0ykzq3ar5nmum7y6gdfl6lxfn46h2\",\"coins\":[{\"amount\":1001000000,\"denom\":\"BNB\"}]}],\"outputs\":[{\"address\":\"bnb13zeh6hs97d5eu2s5qerguhv8ewwue6u4ywa6yf\",\"coins\":[{\"amount\":1001000000,\"denom\":\"BNB\"}]}]}],\"sequence\":\"23\",\"source\":\"1\"}}"
}.build()

val parsingOutputBytes = WalletConnectRequest.parse(BINANCE, parsingInput.toByteArray())
val parsingOutput = WalletConnect.ParseRequestOutput.parseFrom(parsingOutputBytes)

assertEquals(parsingOutput.error, Common.SigningError.OK)

val inpu = parsingOutput.binance.toBuilder().apply {
privateKey = ByteString.copyFrom("95949f757db1f57ca94a5dff23314accbe7abee89597bf6a3c7382c84d7eb832".toHexBytes())
}.build()

// sign and encode resulting input
val output = AnySigner.sign(inpu, BINANCE, SigningOutput.parser())

assertEquals(output.error, Common.SigningError.OK)
assertEquals(output.signatureJson, "{\"pub_key\":{\"type\":\"tendermint/PubKeySecp256k1\",\"value\":\"Amo1kgCI2Yw4iMpoxT38k/RWRgJgbLuH8P5e5TPbOOUC\"},\"signature\":\"PCTHhMa7+Z1U/6uxU+3LbTxKd0k231xypdMolyVvjgYvMg+0dTMC+wqW8IxHWXTSDt/Ronu+7ac1h/WN3JWJdQ==\"}")
}
}
14 changes: 5 additions & 9 deletions src/interface/TWWalletConnectRequest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,10 @@
using namespace TW;

TWData* _Nonnull TWWalletConnectRequestParse(enum TWCoinType coin, TWData* _Nonnull input) {
try {
const Data& inputData = *reinterpret_cast<const Data*>(input);
Rust::TWDataWrapper twInputData = inputData;
const Data& inputData = *reinterpret_cast<const Data*>(input);
Rust::TWDataWrapper twInputData = inputData;

Rust::TWDataWrapper twOutputData = Rust::tw_wallet_connect_request_parse(static_cast<uint32_t>(coin), twInputData.get());
auto outputData = twOutputData.toDataOrDefault();
return TWDataCreateWithBytes(outputData.data(), outputData.size());
} catch (...) {
return nullptr;
}
Rust::TWDataWrapper twOutputData = Rust::tw_wallet_connect_request_parse(static_cast<uint32_t>(coin), twInputData.get());
auto outputData = twOutputData.toDataOrDefault();
return TWDataCreateWithBytes(outputData.data(), outputData.size());
}

0 comments on commit 3c65772

Please sign in to comment.