Skip to content

Commit

Permalink
[BNB]: Remove TransactionCompilerBuildInput
Browse files Browse the repository at this point in the history
  • Loading branch information
satoshiotomakan committed Dec 11, 2023
1 parent b867158 commit b26aabe
Show file tree
Hide file tree
Showing 10 changed files with 30 additions and 98 deletions.
17 changes: 0 additions & 17 deletions include/TrustWalletCore/TWTransactionCompiler.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,23 +18,6 @@ TW_EXTERN_C_BEGIN
TW_EXPORT_STRUCT
struct TWTransactionCompiler;

/// Builds a coin-specific SigningInput (proto object) from a simple transaction.
///
/// \deprecated `TWTransactionCompilerBuildInput` will be removed soon.
/// \param coin coin type.
/// \param from sender of the transaction.
/// \param to receiver of the transaction.
/// \param amount transaction amount in string
/// \param asset optional asset name, like "BNB"
/// \param memo optional memo
/// \param chainId optional chainId to override default
/// \return serialized data of the SigningInput proto object.
TW_EXPORT_STATIC_METHOD
TWData* _Nonnull TWTransactionCompilerBuildInput(enum TWCoinType coinType, TWString* _Nonnull from,
TWString* _Nonnull to, TWString* _Nonnull amount,
TWString* _Nonnull asset, TWString* _Nonnull memo,
TWString* _Nonnull chainId);

/// Obtains pre-signing hashes of a transaction.
///
/// We provide a default `PreSigningOutput` in TransactionCompiler.proto.
Expand Down
19 changes: 0 additions & 19 deletions samples/go/core/transactionHelper.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,25 +6,6 @@ package core
import "C"
import "tw/types"

func BuildInput(c CoinType, from, to string, amount string, asset string, memo string, chainId string) []byte {
fromStr := types.TWStringCreateWithGoString(from)
defer C.TWStringDelete(fromStr)
toStr := types.TWStringCreateWithGoString(to)
defer C.TWStringDelete(toStr)
amountStr := types.TWStringCreateWithGoString(amount)
defer C.TWStringDelete(amountStr)
assetStr := types.TWStringCreateWithGoString(asset)
defer C.TWStringDelete(assetStr)
memoStr := types.TWStringCreateWithGoString(memo)
defer C.TWStringDelete(memoStr)
chainIdStr := types.TWStringCreateWithGoString(chainId)
defer C.TWStringDelete(chainIdStr)

result := C.TWTransactionCompilerBuildInput(C.enum_TWCoinType(c), fromStr, toStr, amountStr, assetStr, memoStr, chainIdStr)
defer C.TWDataDelete(result)
return types.TWDataGoBytes(result)
}

func PreImageHashes(c CoinType, txInputData []byte) []byte {
input := types.TWDataCreateWithGoBytes(txInputData)
defer C.TWDataDelete(input)
Expand Down
39 changes: 30 additions & 9 deletions samples/go/sample/external_signing.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,37 @@ func SignExternalBinanceDemo() {

coin := core.CoinTypeBinance

// bnb1grpf0955h0ykzq3ar5nmum7y6gdfl6lxfn46h2
fromAddress, _ := hex.DecodeString("40c2979694bbc961023d1d27be6fc4d21a9febe6")
// bnb1hlly02l6ahjsgxw9wlcswnlwdhg4xhx38yxpd5
toAddress, _ := hex.DecodeString("bffe47abfaede50419c577f1074fee6dd1535cd1")

inOutToken := binance.SendOrder_Token {
Denom: "BNB",
Amount: 1,
}

orderInput := binance.SendOrder_Input {
Address: fromAddress,
Coins: []*binance.SendOrder_Token{&inOutToken},
}
orderOutput := binance.SendOrder_Output {
Address: toAddress,
Coins: []*binance.SendOrder_Token{&inOutToken},
}

input := binance.SigningInput {
ChainId: "Binance-Chain-Nile",
OrderOneof: &binance.SigningInput_SendOrder {
SendOrder: &binance.SendOrder {
Inputs: []*binance.SendOrder_Input{&orderInput},
Outputs: []*binance.SendOrder_Output{&orderOutput},
},
},
}

fmt.Println("\n==> Step 1: Prepare transaction input (protobuf)")
txInputData := core.BuildInput(
coin,
"bnb1grpf0955h0ykzq3ar5nmum7y6gdfl6lxfn46h2", // from
"bnb1hlly02l6ahjsgxw9wlcswnlwdhg4xhx38yxpd5", // to
"1", // amount
"BNB", // asset
"", // memo
"", // chainId
)
txInputData, _ := proto.Marshal(&input)
fmt.Println("txInputData len: ", len(txInputData))

fmt.Println("\n==> Step 2: Obtain preimage hash")
Expand Down
6 changes: 0 additions & 6 deletions src/Coin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -323,12 +323,6 @@ void TW::anyCoinCompileWithSignatures(TWCoinType coinType, const Data& txInputDa
dispatcher->compile(coinType, txInputData, signatures, publicKeys, txOutputOut);
}

Data TW::anyCoinBuildTransactionInput(TWCoinType coinType, const std::string& from, const std::string& to, const uint256_t& amount, const std::string& asset, const std::string& memo, const std::string& chainId) {
auto* dispatcher = coinDispatcher(coinType);
assert(dispatcher != nullptr);
return dispatcher->buildTransactionInput(coinType, from, to, amount, asset, memo, chainId);
}

// Coin info accessors

extern const CoinInfo getCoinInfo(TWCoinType coin); // in generated CoinInfoData.cpp file
Expand Down
2 changes: 0 additions & 2 deletions src/Coin.h
Original file line number Diff line number Diff line change
Expand Up @@ -126,8 +126,6 @@ Data anyCoinPreImageHashes(TWCoinType coinType, const Data& txInputData);

void anyCoinCompileWithSignatures(TWCoinType coinType, const Data& txInputData, const std::vector<Data>& signatures, const std::vector<PublicKey>& publicKeys, Data& txOutputOut);

Data anyCoinBuildTransactionInput(TWCoinType coinType, const std::string& from, const std::string& to, const uint256_t& amount, const std::string& asset, const std::string& memo, const std::string& chainId);

// Describes a derivation: path + optional format + optional name
struct Derivation {
TWDerivation name = TWDerivationDefault;
Expand Down
3 changes: 0 additions & 3 deletions src/CoinEntry.h
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,6 @@ class CoinEntry {
virtual Data preImageHashes([[maybe_unused]] TWCoinType coin, [[maybe_unused]] const Data& txInputData) const { return {}; }
// Optional method for compiling a transaction with externally-supplied signatures & pubkeys.
virtual void compile([[maybe_unused]] TWCoinType coin, [[maybe_unused]] const Data& txInputData, [[maybe_unused]] const std::vector<Data>& signatures, [[maybe_unused]] const std::vector<PublicKey>& publicKeys, [[maybe_unused]] Data& dataOut) const {}
// Optional helper to prepare a SigningInput from simple parameters.
// Not suitable for UTXO chains. Some parameters, like chain-specific fee/gas paraemters, may need to be set in the SigningInput.
virtual Data buildTransactionInput([[maybe_unused]] TWCoinType coinType, [[maybe_unused]] const std::string& from, [[maybe_unused]] const std::string& to, [[maybe_unused]] const uint256_t& amount, [[maybe_unused]] const std::string& asset, [[maybe_unused]] const std::string& memo, [[maybe_unused]] const std::string& chainId) const { return Data(); }
};

// In each coin's Entry.cpp the specific types of the coin are used, this template enforces the Signer implement:
Expand Down
7 changes: 0 additions & 7 deletions src/TransactionCompiler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,6 @@

using namespace TW;


Data TransactionCompiler::buildInput(TWCoinType coinType, const std::string& from, const std::string& to, const std::string& amount, const std::string& asset, const std::string& memo, const std::string& chainId) {
// parse amount
uint256_t amount256 { amount };
return anyCoinBuildTransactionInput(coinType, from, to, amount256, asset, memo, chainId);
}

Data TransactionCompiler::preImageHashes(TWCoinType coinType, const Data& txInputData) {
return anyCoinPreImageHashes(coinType, txInputData);
}
Expand Down
3 changes: 0 additions & 3 deletions src/TransactionCompiler.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,6 @@ namespace TW {
/// Non-core transaction utility methods, like building a transaction using an external signature
class TransactionCompiler {
public:
/// Build a coin-specific SigningInput protobuf transaction input, from simple transaction parameters
static Data buildInput(TWCoinType coinType, const std::string& from, const std::string& to, const std::string& amount, const std::string& asset, const std::string& memo, const std::string& chainId);

/// Obtain pre-signing hash of a transaction.
/// It will return a proto object named `PreSigningOutput` which will include hash.
/// We provide a default `PreSigningOutput` in TransactionCompiler.proto.
Expand Down
17 changes: 0 additions & 17 deletions src/interface/TWTransactionCompiler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,23 +14,6 @@

using namespace TW;


TWData *_Nonnull TWTransactionCompilerBuildInput(enum TWCoinType coinType, TWString *_Nonnull from, TWString *_Nonnull to, TWString *_Nonnull amount, TWString *_Nonnull asset, TWString *_Nonnull memo, TWString *_Nonnull chainId) {
Data result;
try {
result = TransactionCompiler::buildInput(
coinType,
std::string(TWStringUTF8Bytes(from)),
std::string(TWStringUTF8Bytes(to)),
std::string(TWStringUTF8Bytes(amount)),
std::string(TWStringUTF8Bytes(asset)),
std::string(TWStringUTF8Bytes(memo)),
std::string(TWStringUTF8Bytes(chainId))
);
} catch (...) {} // return empty
return TWDataCreateWithBytes(result.data(), result.size());
}

static std::vector<Data> createFromTWDataVector(const struct TWDataVector* _Nonnull dataVector) {
std::vector<Data> ret;
const auto n = TWDataVectorSize(dataVector);
Expand Down
15 changes: 0 additions & 15 deletions tests/chains/Ethereum/TransactionCompilerTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -108,18 +108,3 @@ TEST(EthereumCompiler, CompileWithSignatures) {
EXPECT_EQ(output.error(), Common::Proto::Error_signatures_count);
}
}

TEST(EthereumCompiler, BuildTransactionInputShouldFail) {
const auto coin = TWCoinTypeEthereum;
const auto txInputData0 =
TransactionCompiler::buildInput(coin,
"0x9d8A62f656a8d1615C1294fd71e9CFb3E4855A4F", // from
"0x3535353535353535353535353535353535353535", // to
"1000000000000000000", // amount
"ETH", // asset
"Memo", // memo
"05" // chainId
);
// Ethereum doesn't support `buildInput`.
EXPECT_TRUE(txInputData0.empty());
}

0 comments on commit b26aabe

Please sign in to comment.