ex_plasma v0.1.0 ExPlasma.Transaction behaviour
The base transaction for now. There's actually a lot of different transaction types.
Link to this section Summary
Functions
Encodes a transaction into an RLP encodable list.
Encodes a transaction into an RLP encodable list.
Keccak hash a transaction.
Generate a new Transaction. This generates an empty transaction as this is the base class.
The associated value for the output type. This is the base representation and is 0 because it does not exist.
## Examples
Generate an RLP-encodable list for the transaction.
The transaction type value as defined by the contract. This is the base representation and is 0 because it does not exist.
Link to this section Types
t()
t() :: %ExPlasma.Transaction{
inputs: [ExPlasma.Utxo.t()] | [],
metadata: metadata() | nil,
outputs: [ExPlasma.Utxo.t()] | [],
sigs: [binary()] | [],
tx_data: term(),
tx_type: term()
}
Link to this section Functions
decode(rlp_encoded_txn)
decode(binary()) :: {:ok, t()} | ExPlasma.Utxo.validation_tuples()
Encodes a transaction into an RLP encodable list.
Examples
iex> rlp_encoded = <<248, 116, 128, 225, 160, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 238, 237, 1, 235, 148, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 148, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 128, 160, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0>> iex> ExPlasma.Transaction.decode(rlp_encoded) {:ok, %ExPlasma.Transaction{
inputs: [
%ExPlasma.Utxo{
amount: nil,
blknum: 0,
currency: nil,
oindex: 0,
output_type: 1,
owner: nil,
txindex: 0
}
],
metadata: <<0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0>>,
outputs: [
%ExPlasma.Utxo{
amount: 1,
blknum: nil,
currency: <<0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0>>,
oindex: nil,
output_type: 1,
owner: <<0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1>>,
txindex: nil
}
],
tx_type: "",
tx_data: "",
sigs: []
}}
# Create a transaction from a signed encoded hash of a transaction iex> signed_encoded_hash = "0xf85ef843b841aa061b1df64f0b5c3bd350d9444b8fd2d02d4523abb23fbe8d270d6bc2e782c037d45e0c0afaf615cfc0701f4cde6af04f60ddb756e52f8459f459f1e65dcd511b80c0c080940000000000000000000000000000000000000000" iex> signed_encoded_hash |> ExPlasma.Encoding.to_binary() |> ExPlasma.Transaction.decode() {:ok, %ExPlasma.Transaction{
tx_type: "",
tx_data: "",
inputs: [],
metadata: <<0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0>>,
outputs: [],
sigs: [
<<170, 6, 27, 29, 246, 79, 11, 92, 59, 211, 80, 217, 68, 75, 143, 210, 208,
45, 69, 35, 171, 178, 63, 190, 141, 39, 13, 107, 194, 231, 130, 192, 55,
212, 94, 12, 10, 250, 246, 21, 207, 192, 112, 31, 76, 222, 106, 240, 79,
96, 221, 183, 86, 229, 47, 132, 89, 244, 89, 241, 230, 93, 205, 81, 27>>
]
}}
Encodes a transaction into an RLP encodable list.
Examples
iex> txn = %ExPlasma.Transaction{} iex> ExPlasma.Transaction.encode(txn) <<229, 128, 192, 192, 128, 160, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0>>
Keccak hash a transaction.
Examples
# Hash a transaction
iex> txn = %ExPlasma.Transaction{}
iex> ExPlasma.Transaction.hash(txn)
<<95, 34, 177, 98, 209, 215, 55, 18, 40, 254, 215, 73, 183, 221,
118, 253, 137, 66, 155, 62, 39, 96, 202, 110, 29, 216, 60, 225,
201, 158, 136, 67>>
# Hash an encoded transaction bytes
iex> tx_bytes = ExPlasma.Transaction.encode(%ExPlasma.Transaction{})
iex> ExPlasma.Transaction.hash(tx_bytes)
<<95, 34, 177, 98, 209, 215, 55, 18, 40, 254, 215, 73, 183, 221,
118, 253, 137, 66, 155, 62, 39, 96, 202, 110, 29, 216, 60, 225,
201, 158, 136, 67>>
new(transaction)
new(t() | rlp()) :: {:ok, t()} | ExPlasma.Utxo.validation_tuples()
Generate a new Transaction. This generates an empty transaction as this is the base class.
Examples
iex> ExPlasma.Transaction.new(%ExPlasma.Transaction{inputs: [], outputs: []}) {:ok, %ExPlasma.Transaction{
inputs: [],
outputs: [],
tx_data: nil,
tx_type: nil,
metadata: nil
}}
# Create a transaction from a RLP list iex> rlp = [ ...> <<1>>, ...> [<<0>>], ...> [ ...> [ ...> <<1>>, ...> [ ...> <<29, 246, 47, 41, 27, 46, 150, 159, 176, 132, 157, 153, 217, 206, 65, ...> 226, 241, 55, 0, 110>>, ...> <<46, 38, 45, 41, 28, 46, 150, 159, 176, 132, 157, 153, 217, 206, 65, 226, ...> 241, 55, 0, 110>>, ...> <<0, 0, 0, 0, 0, 0, 0, 1>> ...> ] ...> ] ...> ], ...> <<0>>, ...> <<0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0>> ...>] iex> ExPlasma.Transaction.new(rlp) {:ok, %ExPlasma.Transaction{inputs: [%ExPlasma.Utxo{amount: nil, blknum: 0, currency: nil, oindex: 0, owner: nil, txindex: 0 }],
metadata: <<0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0>>,
outputs: [%ExPlasma.Utxo{amount: 1, blknum: nil, currency: <<46, 38, 45, 41, 28, 46, 150, 159, 176, 132, 157, 153, 217, 206, 65, 226, 241, 55, 0, 110>>, oindex: nil, owner: <<29, 246, 47, 41, 27, 46, 150, 159, 176, 132, 157, 153, 217, 206, 65, 226, 241, 55, 0, 110>>, txindex: nil}],
tx_data: 0,
tx_type: 1,
sigs: []}}
The associated value for the output type. This is the base representation and is 0 because it does not exist.
sign(transaction, list)
sign(ExPlasma.Transaction.t(), [{:keys, []}]) :: ExPlasma.Transaction.t()
## Examples
# Signs transaction with the given key.
iex> txn = %ExPlasma.Transaction{metadata: <<0::160>>}
iex> key = "0x79298b0292bbfa9b15705c56b6133201c62b798f102d7d096d31d7637f9b2382"
iex> ExPlasma.Transaction.sign(txn, keys: [key])
%ExPlasma.Transaction{
inputs: [],
metadata: <<0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0>>,
outputs: [],
sigs: [
<<105, 151, 242, 23, 175, 7, 237, 244, 242, 110, 41, 1, 242, 106, 61, 65,
85, 117, 213, 245, 188, 155, 84, 3, 131, 195, 107, 76, 179, 55, 195, 148,
84, 167, 162, 147, 33, 147, 74, 24, 136, 15, 26, 202, 78, 80, 182, 183,
206, 139, 75, 29, 91, 90, 136, 158, 223, 112, 82, 222, 37, 55, 104, 202,
27>>
]
}
# Signs the transaction with no keys.
iex> txn = %ExPlasma.Transaction{metadata: <<0::160>>}
iex> ExPlasma.Transaction.sign(txn, keys: [])
%ExPlasma.Transaction{
inputs: [],
metadata: <<0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0>>,
outputs: [],
sigs: []
}
Generate an RLP-encodable list for the transaction.
Examples
iex> txn = %ExPlasma.Transaction{} iex> ExPlasma.Transaction.to_rlp(txn) [0, [], [], 0, <<0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0>>]
The transaction type value as defined by the contract. This is the base representation and is 0 because it does not exist.