8000 [Feat] Add offline transaction methods by rudolf-del · Pull Request #150 · andelf/tronpy · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

[Feat] Add offline transaction methods #150

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: master
Choose a base branch
from

Conversation

rudolf-del
Copy link

PR Type

Enhancement


Description

  • Add offline transaction building methods for TRX transfers

  • Add offline TRC-20 token transfer transaction building

  • Refactor timestamp and block reference utilities

  • Add Protocol Buffer definitions for transaction structures


Changes diagram

flowchart LR
  A["Client Code"] --> B["Transaction.build_offline()"]
  A --> C["Transaction.build_trc20_transfer_offline()"]
  B --> D["create_transaction_offline()"]
  C --> E["create_smart_contract_transaction_offline()"]
  D --> F["Protocol Buffer Transaction"]
  E --> F
  F --> G["JSON Transaction Dict"]
  G --> H["Unsigned Transaction"]
Loading

Changes walkthrough 📝

Relevant files
Tests
1 files
test_offline.py
Add comprehensive tests for offline transaction methods   
+93/-0   
Enhancement
4 files
async_tron.py
Refactor to use shared utilities and constants                     
+10/-13 
transaction.py
Core offline transaction building implementation                 
+263/-0 
tron.py
Add offline transaction class methods and refactor utilities
+161/-14
utils.py
Extract shared utility functions for timestamps and blocks
+37/-0   
Configuration changes
3 files
defaults.py
Add SIXTY_SECONDS constant for transaction expiration       
+5/-0     
.flake8
Update linting configuration with exclusions                         
+12/-0   
Makefile
Add Protocol Buffer generation target                                       
+8/-0     
Dependencies
4 files
tron_pb2.py
Generated Protocol Buffer Python bindings                               
+55/-0   
pyproject.toml
Add development dependencies for protobuf and testing       
+2/-0     
tron.proto
Protocol Buffer schema for TRON transactions                         
+153/-0 
tron_pb2.pyi
Type stubs for generated Protocol Buffer code                       
+375/-0 
Miscellaneous
1 files
types.py
Add TAddress type alias                                                                   
+1/-0     

@rudolf-del rudolf-del force-pushed the feat-add-offline-transaction-methods branch from 33eb470 to a868703 Compare July 4, 2025 12:32
@rudolf-del
Copy link
Author

Why this PR matters
Enterprise custodians, exchanges, and HSM vendors must build / sign TRON transactions on air-gapped machines. Today tronpy always calls a full node (/wallet/createtransaction, /wallet/triggersmartcontract, …) before signing, forcing shops to re-implement tron_pb2.Transaction logic

  • Brings tronpy to feature-parity with Java-SDK / TronWeb.
  • Enables cold-storage signing
  • Offloads trivial assembly work from public full nodes.

Compatibility & risk
All additions are purely additive; signing/broadcasting paths untouched. New code is type-checked and >90 % covered by tests. This closes a critical security gap without increasing tronpy’s surface area.

@MrNaif2018
Copy link
Collaborator

Hi! Wow, this is impressive! I think the best well-written PR I've seen in a long time!
I did indeed use tron protobuf before for e.g. calculating transaction sizes at my lib trontxsize
So from what I see it uses protobuf to construct the json mostly, but some JSON fields are still manually added on top of that
Would you say that they (=tron developers) wouldn't edit the output format of their transactions, right?
And even if they adjust protobuf files, usually from what I know they only add new contract types?
I just wonder what happens if they e.g. add a new field to a TransferContract datatype, would it mean this offline signing fails or not really

I'll review this PR in full a bit later, thank you so much!

@rudolf-del
Copy link
Author

Sometimes they do rename some things in proto files:

Our core logic relies on the base models and fields, so we should be all set. Adding new field values on top of the existing ones won’t break anything as long as their order stays the same. We’d only need to update the proto definitions if a core field gets renamed or removed—which is unlikely and would require an accompanying API update anyway.

@bxchange
Copy link
bxchange commented Jul 9, 2025

Hey guys @MrNaif2018 @andelf ! Did you manage to have a look at this PR? We think that this is a valuable feature that tron libraries in other languages (JS, Java) have. Please let us know if you would like anything to be changed in code

@MrNaif2018
Copy link
Collaborator
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Users/alex/github/tronpy/tronpy/__init__.py", line 2, in <module>
    from tronpy.async_tron import AsyncTron
  File "/Users/alex/github/tronpy/tronpy/async_tron.py", line 30, in <module>
    from tronpy.proto.transaction import create_smart_contract_transaction_offline, create_transaction_offline
  File "/Users/alex/github/tronpy/tronpy/proto/transaction.py", line 4, in <module>
    import google.protobuf.any_pb2
ModuleNotFoundError: No module named 'google`

The thing is, this offline support requires adding protobuf to runtime dependencies
I am thinking to make it an optional extra to be honest
I'll see what we can do

@MrNaif2018
Copy link
Collaborator

The tests pass indeed
The code overall looks good, though a bit unsure about handling for smart contract case
Just to be sure, could you make it import this utility only if protobuf is installed?
And in tests I think what could be done is, compare the offline output with online output to ensure that most fields match, so that even if they do make some changes we can see it right away

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants
0