Skip to content

Commit

Permalink
Merge pull request #4 from txbody-org/transaction-builder
Browse files Browse the repository at this point in the history
Transaction Decoder
  • Loading branch information
piyushthapa authored Nov 12, 2024
2 parents f36652e + 527656a commit 08b0de2
Show file tree
Hide file tree
Showing 21 changed files with 1,884 additions and 21 deletions.
8 changes: 4 additions & 4 deletions flake.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 7 additions & 1 deletion flake.nix
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
description = "Elixir flake";
inputs = {
nixpkgs = { url = "github:NixOS/nixpkgs/nixos-23.11"; };
nixpkgs = { url = "github:NixOS/nixpkgs/nixpkgs-unstable"; };
flake-utils = { url = "github:numtide/flake-utils"; };
};

Expand All @@ -11,6 +11,12 @@ outputs = { self, nixpkgs, flake-utils }:
inherit (pkgs.lib) optional optionals;
pkgs = import nixpkgs { inherit system; };

#elixir = pkgs.beam.packages.erlang_27.elixir.override {
# version = "1.17.2";
# rev = "47abe2d107e654ccede845356773bcf6e11ef7cb";
# sha256 = "sha256-8rb2f4CvJzio3QgoxvCv1iz8HooXze0tWUJ4Sc13dxg=";
#};

in
with pkgs;
{
Expand Down
9 changes: 6 additions & 3 deletions lib/sutra/cardano/asset.ex
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,16 @@ defmodule Sutra.Cardano.Asset do
{:ok, value} -> value
end

Map.put(acc, key, to_asset_class(val))
Map.put(acc, key, to_asset_class(key, val))
end)

{:ok, result}
end

defp to_asset_class(%{%CBOR.Tag{tag: :bytes, value: ""} => lovelace}),
defp to_asset_class("lovelace", %{%CBOR.Tag{tag: :bytes, value: ""} => lovelace}),
do: lovelace

defp to_asset_class(value) when is_map(value) do
defp to_asset_class(_, value) when is_map(value) do
Enum.reduce(value, %{}, fn {key, val}, acc ->
{:ok, key} = extract_value(key)
Map.put(acc, key, val)
Expand Down Expand Up @@ -63,4 +63,7 @@ defmodule Sutra.Cardano.Asset do
Map.put(acc, key, val)
end)
end

def lovelace_of(value) when is_integer(value), do: %{"lovelace" => value}
def lovelace_of(_), do: nil
end
34 changes: 34 additions & 0 deletions lib/sutra/cardano/common/pool_relay.ex
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
65 changes: 65 additions & 0 deletions lib/sutra/cardano/script/native_script.ex
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
47 changes: 46 additions & 1 deletion lib/sutra/cardano/transaction.ex
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.
Loading

0 comments on commit 08b0de2

Please sign in to comment.