tron v0.1.0-rc Tron View Source

WIP TRON protocol implementation

Examples

# your private key
privkey = <<...>>

# where you want to send TRX to
to_address = <<...>>

# build a transfer contract
transfer_contract = Protocol.TransferContract.new(
  owner_address: Tron.address(privkey), # your address
  to_address: to_address,
  amount: 100_000 # 0.1 TRX
)

# embed the transfer contract in a transaction contract
transaction_contract = Protocol.Transaction.Contract.new(
  type: Protocol.Transaction.Contract.ContractType.value(:TransferContract),
  parameter:
    Google.Protobuf.Any.new(
      value: Protocol.TransferContract.encode(transfer_contract),
      type_url: "type.googleapis.com/protocol.TransferContract"
    )
)

timestamp = DateTime.to_unix(DateTime.utc_now(), :millisecond)

# build a transaction
transaction = Protocol.Transaction.new(
  raw_data: Protocol.Transaction.Raw.new(contract: [transaction_contract], timestamp: timestamp),
  signature: []
)

# connect to a node (https://github.com/tronprotocol/Documentation/blob/master/TRX/Official_Public_Node.md)
{:ok, channel} = GRPC.Stub.connect("47.254.77.146:50051")

# get latest block for reference
{:ok,
   %Protocol.BlockExtention{
     block_header: %Protocol.BlockHeader{raw_data: %Protocol.BlockHeader.Raw{} = block_header_raw}
   }} = Tron.Client.get_now_block2(channel, Protocol.EmptyMessage.new())

# set transaction's block reference and sign it
transaction =
  transaction
  |> Tron.set_reference(block_header_raw)
  |> Tron.sign_transaction(privkey)

# broadcast the transaction
{:ok, %Protocol.Return{code: 0, message: "", result: true}} =
  Tron.Client.broadcast_transaction(channel, transaction)

Link to this section Summary

Functions

Computes an address from a private key

Generates a private key

Computes a public key from a private key

Populates a transaction’s refs with a block. Needs to be done before broadcasting the transaction

Signs a transaction with a private key. A transactions needs to be signed before being broadcasted

Link to this section Types

Link to this type address() View Source
address() :: <<_::168>>
Link to this type private_key() View Source
private_key() :: <<_::256>>
Link to this type public_key() View Source
public_key() :: <<_::520>>

Link to this section Functions

Computes an address from a private key

Example:

iex> privkey_base16 = "b9a367686d7fce1a5fdceb9b3b6ff116b5df5c9c8c9899dbaeaa00c1cb7b02a6"
iex> privkey = Base.decode16!(privkey_base16, case: :lower)
iex> address(privkey)
<<65, 192, 82, 108, 106, 223, 154, 173, 171, 167>> <>
<<80, 45, 69, 65, 191, 240, 51, 185, 140, 72, 181>>
Link to this function private_key() View Source
private_key() :: private_key()

Generates a private key.

Example:

iex> <<key::32-bytes>> = private_key()
iex> byte_size(key)
32

Computes a public key from a private key.

Example:

iex> privkey_base16 = "b9a367686d7fce1a5fdceb9b3b6ff116b5df5c9c8c9899dbaeaa00c1cb7b02a6"
iex> privkey = Base.decode16!(privkey_base16, case: :lower)
iex> pubkey = public_key(privkey)
iex> Base.encode16(pubkey, case: :lower)
"047adf4255f518ca27b9b41ddfd97d4a3799e02347b3b1b7c525b67371b3db350" <> 
"a571b3bddb9732868daeab70f9ac9bd842c8b26e605855899f32f8526c2e6d5ed"
Link to this function set_reference(transaction, block_header_raw) View Source
set_reference(Protocol.Transaction.t(), Protocol.BlockHeader.Raw.t()) ::
  Protocol.Transaction.t()

Populates a transaction’s refs with a block. Needs to be done before broadcasting the transaction.

Example:

iex> transaction = Protocol.Transaction.new(raw_data: Protocol.Transaction.Raw.new())
iex> block_header = Protocol.BlockHeader.Raw.new(
...>   number: 123,
...>   timestamp: 12334556787
...> )
iex> %Protocol.Transaction{raw_data: raw_data} = set_reference(transaction, block_header)
iex> {raw_data.ref_block_bytes, raw_data.ref_block_hash, raw_data.expiration}
{<<0, 123>>, <<30, 70, 44, 221, 244, 196, 156, 248>>, 12370556787}
Link to this function sign_transaction(transaction, arg) View Source
sign_transaction(Protocol.Transaction.t(), private_key()) ::
  Protocol.Transaction.t()

Signs a transaction with a private key. A transactions needs to be signed before being broadcasted.

Example:

iex> transaction = Protocol.Transaction.new(raw_data: Protocol.Transaction.Raw.new())
iex> privkey_base16 = "b9a367686d7fce1a5fdceb9b3b6ff116b5df5c9c8c9899dbaeaa00c1cb7b02a6"
iex> privkey = Base.decode16!(privkey_base16, case: :lower)
iex> %Protocol.Transaction{signature: [signature]} = sign_transaction(transaction, privkey)
iex> Base.encode16(signature, case: :lower)
"a7cb7d32e8e50097a65d0ca3c5a2f54491d018346dc5a6cbdbcb7086e4ffcc6a4" <>
"19dcbe0630b9d09cf8ec76d3b74fbc3d2adc78f307c260ec3f9b651dc37c8db01"