-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[WalletConnect/BNB]: Add
TWWalletConnectRequestParse
C FFI
* Improve WalletConnect.proto interface
- Loading branch information
1 parent
77a917e
commit cb61cea
Showing
13 changed files
with
146 additions
and
53 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
// Copyright © 2017-2024 Trust Wallet. | ||
// | ||
// This file is part of Trust. The full Trust copyright notice, including | ||
// terms governing use, modification, and redistribution, is contained in the | ||
// file LICENSE at the root of the source code distribution tree. | ||
|
||
#pragma once | ||
|
||
#include "TWBase.h" | ||
#include "TWCoinType.h" | ||
#include "TWData.h" | ||
|
||
TW_EXTERN_C_BEGIN | ||
|
||
/// Represents a WalletConnect signing request. | ||
TW_EXPORT_CLASS | ||
struct TWWalletConnectRequest; | ||
|
||
/// Parses the WalletConnect signing request as a `SigningInput`. | ||
/// | ||
/// \param coin The given coin type to plan the transaction for. | ||
/// \param input The serialized data of a `WalletConnect::Proto::ParseRequestInput` proto object. | ||
/// \return The serialized data of `WalletConnect::Proto::ParseRequestOutput` proto object. | ||
TW_EXPORT_STATIC_METHOD | ||
TWData* _Nonnull TWWalletConnectRequestParse(enum TWCoinType coin, TWData* _Nonnull input); | ||
|
||
TW_EXTERN_C_END |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
// Copyright © 2017-2024 Trust Wallet. | ||
// | ||
// This file is part of Trust. The full Trust copyright notice, including | ||
// terms governing use, modification, and redistribution, is contained in the | ||
// file LICENSE at the root of the source code distribution tree. | ||
|
||
#include <TrustWalletCore/TWWalletConnectRequest.h> | ||
|
||
#include "rust/Wrapper.h" | ||
|
||
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; | ||
|
||
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
// Copyright © 2017-2023 Trust Wallet. | ||
// | ||
// This file is part of Trust. The full Trust copyright notice, including | ||
// terms governing use, modification, and redistribution, is contained in the | ||
// file LICENSE at the root of the source code distribution tree. | ||
|
||
#include "HexCoding.h" | ||
#include "proto/Binance.pb.h" | ||
#include "proto/WalletConnect.pb.h" | ||
#include "Coin.h" | ||
#include <TrustWalletCore/TWAnySigner.h> | ||
#include <TrustWalletCore/TWWalletConnectRequest.h> | ||
|
||
#include "TestUtilities.h" | ||
#include <gtest/gtest.h> | ||
|
||
namespace TW::Binance { | ||
|
||
TEST(TWWalletConnectSign, SendOrder) { | ||
auto private_key = parse_hex("95949f757db1f57ca94a5dff23314accbe7abee89597bf6a3c7382c84d7eb832"); | ||
const auto payload = R"({"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"}})"; | ||
|
||
WalletConnect::Proto::ParseRequestInput parsingInput; | ||
parsingInput.set_method(WalletConnect::Proto::Method::CosmosSignAmino); | ||
parsingInput.set_payload(payload); | ||
|
||
const auto parsinginputData = parsingInput.SerializeAsString(); | ||
const auto parsingInputDataPtr = WRAPD(TWDataCreateWithBytes(reinterpret_cast<const uint8_t *>(parsinginputData.c_str()), parsinginputData.size())); | ||
|
||
const auto outputDataPtr = WRAPD(TWWalletConnectRequestParse(TWCoinTypeBinance, parsingInputDataPtr.get())); | ||
|
||
WalletConnect::Proto::ParseRequestOutput parsingOutput; | ||
parsingOutput.ParseFromArray( | ||
TWDataBytes(outputDataPtr.get()), | ||
static_cast<int>(TWDataSize(outputDataPtr.get())) | ||
); | ||
|
||
EXPECT_EQ(parsingOutput.error(), Common::Proto::SigningError::OK); | ||
|
||
// Step 2: Set missing fields. | ||
ASSERT_TRUE(parsingOutput.has_binance()); | ||
Proto::SigningInput signingInput = parsingOutput.binance(); | ||
|
||
signingInput.set_private_key(private_key.data(), private_key.size()); | ||
|
||
Proto::SigningOutput output; | ||
ANY_SIGN(signingInput, TWCoinTypeBinance); | ||
|
||
EXPECT_EQ(output.error(), Common::Proto::SigningError::OK); | ||
EXPECT_EQ(hex(output.signature()), "3c24c784c6bbf99d54ffabb153edcb6d3c4a774936df5c72a5d32897256f8e062f320fb4753302fb0a96f08c475974d20edfd1a27bbeeda73587f58ddc958975"); | ||
EXPECT_EQ(output.signature_json(), R"({"pub_key":{"type":"tendermint/PubKeySecp256k1","value":"Amo1kgCI2Yw4iMpoxT38k/RWRgJgbLuH8P5e5TPbOOUC"},"signature":"PCTHhMa7+Z1U/6uxU+3LbTxKd0k231xypdMolyVvjgYvMg+0dTMC+wqW8IxHWXTSDt/Ronu+7ac1h/WN3JWJdQ=="})"); | ||
} | ||
|
||
} // namespace TW::Binance |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters