Skip to content

Commit c4907d4

Browse files
[TRON]: Add transaction memo (#4099)
* [TRON]: Add transaction memo * [TRON]: Skip empty memo JSON serialization
1 parent 5071656 commit c4907d4

File tree

5 files changed

+49
-1
lines changed

5 files changed

+49
-1
lines changed

src/Tron/Protobuf/TronInternal.proto

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import "google/protobuf/any.proto";
44

55
package protocol;
66

7+
// https://github.com/tronprotocol/protocol/blob/2a678934da3992b1a67f975769bbb2d31989451f/core/Tron.proto#L336
78
message Transaction {
89
message Contract {
910
enum ContractType {
@@ -33,6 +34,8 @@ message Transaction {
3334
int64 ref_block_num = 3;
3435
bytes ref_block_hash = 4;
3536
int64 expiration = 8;
37+
// transaction memo
38+
bytes data = 10;
3639
//only support size = 1, repeated list here for extension
3740
repeated Contract contract = 11;
3841
int64 timestamp = 14;

src/Tron/Serialization.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -291,6 +291,9 @@ json raw_dataJSON(const protocol::Transaction::raw& raw) {
291291
}
292292
raw_dataJSON["timestamp"] = raw.timestamp();
293293
raw_dataJSON["expiration"] = raw.expiration();
294+
if (!raw.data().empty()) {
295+
raw_dataJSON["data"] = hex(raw.data());
296+
}
294297
raw_dataJSON["contract"] = json::array({contractJSON(raw.contract(0))});
295298

296299
return raw_dataJSON;

src/Tron/Signer.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -386,6 +386,10 @@ protocol::Transaction buildTransaction(const Proto::SigningInput& input) noexcep
386386
*contract->mutable_parameter() = any;
387387
}
388388

389+
if (!input.transaction().memo().empty()) {
390+
tx.mutable_raw_data()->set_data(input.transaction().memo());
391+
}
392+
389393
tx.mutable_raw_data()->set_timestamp(input.transaction().timestamp());
390394
tx.mutable_raw_data()->set_expiration(input.transaction().expiration());
391395
tx.mutable_raw_data()->set_fee_limit(input.transaction().fee_limit());

src/proto/Tron.proto

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -228,9 +228,12 @@ message Transaction {
228228
// Transaction block header.
229229
BlockHeader block_header = 3;
230230

231-
// Transaction fee limit
231+
// Transaction fee limit.
232232
int64 fee_limit = 4;
233233

234+
// Transaction memo.
235+
string memo = 5;
236+
234237
// Contract.
235238
oneof contract_oneof {
236239
TransferContract transfer = 10;

tests/chains/Tron/SignerTests.cpp

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,40 @@ TEST(TronSigner, SignTransfer) {
8989
ASSERT_EQ(hex(output.signature()), "ede769f6df28aefe6a846be169958c155e23e7e5c9621d2e8dce1719b4d952b63e8a8bf9f00e41204ac1bf69b1a663dacdf764367e48e4a5afcd6b055a747fb200");
9090
}
9191

92+
TEST(TronSigner, SignTransferWithMemo) {
93+
// Successfully broadcasted https://tronscan.org/#/transaction/20321755964d6ec5bcfc9ebfb15faeb043787ae599fff44442962e12e1c357f1
94+
auto input = Proto::SigningInput();
95+
auto& transaction = *input.mutable_transaction();
96+
97+
auto& transfer = *transaction.mutable_transfer();
98+
transfer.set_owner_address("TFnYQCt892UNjn67pjAULTSTkB7YvqsnPp");
99+
transfer.set_to_address("TBUCzgc29vykkvFaEG2mgRtxKvaKe6skwX");
100+
transfer.set_amount(100000);
101+
102+
transaction.set_timestamp(1730827017000);
103+
transaction.set_expiration(1730827017000 + 10 * 60 * 60 * 1000);
104+
transaction.set_memo("Test memo");
105+
106+
auto& blockHeader = *transaction.mutable_block_header();
107+
blockHeader.set_timestamp(1730827017000);
108+
const auto txTrieRoot = parse_hex("a94f115089893f37336baf32dbf6cb7d06adc13cf6bf046d9bc22748bd72e7a6");
109+
blockHeader.set_tx_trie_root(txTrieRoot.data(), txTrieRoot.size());
110+
const auto parentHash = parse_hex("0000000003fa27db7d67f93920f64733532412ab6a71eb4089dc48c8ff5e182c");
111+
blockHeader.set_parent_hash(parentHash.data(), parentHash.size());
112+
blockHeader.set_number(66725852);
113+
const auto witnessAddress = parse_hex("4167e39013be3cdd3814bed152d7439fb5b6791409");
114+
blockHeader.set_witness_address(witnessAddress.data(), witnessAddress.size());
115+
blockHeader.set_version(30);
116+
117+
const auto privateKey = PrivateKey(parse_hex("7c2108a30f6f69f8dce72a7df897eabadfe9810eee6976b43bdf8c0b0d35337d"));
118+
input.set_private_key(privateKey.bytes.data(), privateKey.bytes.size());
119+
120+
const auto output = Signer::sign(input);
121+
122+
EXPECT_EQ(hex(output.id()), "20321755964d6ec5bcfc9ebfb15faeb043787ae599fff44442962e12e1c357f1");
123+
EXPECT_EQ(hex(output.signature()), "6fcee79c61f660ec689299f77924f32b5020b4c41593056052ef07d640cc799325103fab130c8691e8a224c96cd0704a698ac356ff789a543c284605668bf38000");
124+
}
125+
92126
TEST(TronSigner, SignFreezeBalanceV2) {
93127
// Successfully broadcasted https://nile.tronscan.org/#/transaction/3a46321487ce1fd115da38b3431006ea529f65ef2507f19233f5a23c05abd01d
94128
auto input = Proto::SigningInput();
@@ -514,4 +548,5 @@ TEST(TronSigner, SignTransferTrc20Contract) {
514548
ASSERT_EQ(hex(output.id()), "0d644290e3cf554f6219c7747f5287589b6e7e30e1b02793b48ba362da6a5058");
515549
ASSERT_EQ(hex(output.signature()), "bec790877b3a008640781e3948b070740b1f6023c29ecb3f7b5835433c13fc5835e5cad3bd44360ff2ddad5ed7dc9d7dee6878f90e86a40355b7697f5954b88c01");
516550
}
551+
517552
} // namespace TW::Tron

0 commit comments

Comments
 (0)