-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4 from txbody-org/transaction-builder
Transaction Decoder
- Loading branch information
Showing
21 changed files
with
1,884 additions
and
21 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
defmodule Sutra.Cardano.Common.PoolRelay do | ||
@moduledoc """ | ||
Pool Relay Information | ||
""" | ||
|
||
use TypedStruct | ||
|
||
typedstruct(module: SingleHostAddr) do | ||
field(:port, :integer) | ||
field(:ipv4, :string) | ||
field(:ipv6, :string) | ||
end | ||
|
||
typedstruct(module: SingleHostName) do | ||
field(:port, :integer) | ||
field(:dns_name, :string) | ||
end | ||
|
||
typedstruct(module: MultiHostName) do | ||
field(:dns_name, :string) | ||
end | ||
|
||
def decode([0, port, ipv4, ipv6]) do | ||
%SingleHostAddr{port: port, ipv4: ipv4, ipv6: ipv6} | ||
end | ||
|
||
def decode([1, port, dns_name]) do | ||
%SingleHostName{port: port, dns_name: dns_name} | ||
end | ||
|
||
def decode([2, dns_name]) do | ||
%MultiHostName{dns_name: dns_name} | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
defmodule Sutra.Cardano.Script.NativeScript do | ||
@moduledoc """ | ||
Cardano Native Script | ||
""" | ||
alias Sutra.Cardano.Script.NativeScript | ||
|
||
@type t() :: | ||
ScriptPubkey.t() | ||
| ScriptAll.t() | ||
| ScriptAny.t() | ||
| ScriptNOfK.t() | ||
| ScriptInvalidBefore.t() | ||
| ScriptInvalidHereafter.t() | ||
|
||
use TypedStruct | ||
|
||
typedstruct(module: ScriptPubkey) do | ||
field(:pubkey_hash, String.t()) | ||
end | ||
|
||
typedstruct(module: ScriptAll) do | ||
field(:scripts, [String.t()]) | ||
end | ||
|
||
typedstruct(module: ScriptAny) do | ||
field(:scripts, [String.t()]) | ||
end | ||
|
||
typedstruct(module: ScriptNOfK) do | ||
field(:n, integer()) | ||
field(:scripts, [NativeScript.t()]) | ||
end | ||
|
||
typedstruct(module: ScriptInvalidBefore) do | ||
field(:slot, integer()) | ||
end | ||
|
||
typedstruct(module: ScriptInvalidHereafter) do | ||
field(:slot, integer()) | ||
end | ||
|
||
def from_witness_set([0, pubkey]) do | ||
%ScriptPubkey{pubkey_hash: pubkey} | ||
end | ||
|
||
def from_witness_set([1, native_scripts]) do | ||
%ScriptAll{scripts: Enum.map(native_scripts, &from_witness_set/1)} | ||
end | ||
|
||
def from_witness_set([2, native_scripts]) do | ||
%ScriptAny{scripts: Enum.map(native_scripts, &from_witness_set/1)} | ||
end | ||
|
||
def from_witness_set([3, n, native_scripts]) do | ||
%ScriptNOfK{n: n, scripts: Enum.map(native_scripts, &from_witness_set/1)} | ||
end | ||
|
||
def from_witness_set([4, slot]) do | ||
%ScriptInvalidBefore{slot: slot} | ||
end | ||
|
||
def from_witness_set([5, slot]) do | ||
%ScriptInvalidHereafter{slot: slot} | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,50 @@ | ||
defmodule Sutra.Cardano.Transaction do | ||
@moduledoc """ | ||
@moduledoc ~S""" | ||
Cardano Transaction | ||
""" | ||
|
||
alias Sutra.Cardano.Transaction.TxBody | ||
alias Sutra.Cardano.Transaction.Witness | ||
alias Sutra.Data.Cbor | ||
alias Sutra.Data.Plutus.PList | ||
|
||
use Sutra.Data | ||
|
||
use TypedStruct | ||
|
||
typedstruct do | ||
field(:tx_body, TxBody.t()) | ||
field(:witnesses, [Witness.t()]) | ||
field(:is_valid, boolean()) | ||
field(:metadata, any()) | ||
end | ||
|
||
def from_hex(cbor) when is_binary(cbor) do | ||
case Sutra.Data.decode(cbor) do | ||
{:ok, data} -> from_cbor(data) | ||
{:error, _} -> {:error, :invalid_cbor} | ||
end | ||
end | ||
|
||
# Conway era transaction | ||
def from_cbor(%PList{value: [tx_body, witness, is_valid, metadata]}) | ||
when is_boolean(is_valid) do | ||
witness = | ||
Enum.reduce(Cbor.extract_value!(witness), [], fn w, acc -> | ||
acc ++ Witness.decode(w) | ||
end) | ||
|
||
%__MODULE__{ | ||
tx_body: TxBody.decode(tx_body), | ||
witnesses: witness, | ||
is_valid: is_valid, | ||
metadata: metadata | ||
} | ||
end | ||
|
||
def from_cbor(%PList{value: _values}) do | ||
raise """ | ||
Only Conway era transaction supported | ||
""" | ||
end | ||
end |
Empty file.
Oops, something went wrong.