|
| 1 | +defmodule StellarBase.XDR.HashIDPreimageContractAuth do |
| 2 | + @moduledoc """ |
| 3 | + Representation of Stellar `HashIDPreimageContractAuth` type. |
| 4 | + """ |
| 5 | + alias StellarBase.XDR.{Hash, UInt64, AuthorizedInvocation} |
| 6 | + |
| 7 | + @behaviour XDR.Declaration |
| 8 | + |
| 9 | + @struct_spec XDR.Struct.new(network_id: Hash, nonce: UInt64, invocation: AuthorizedInvocation) |
| 10 | + |
| 11 | + @type t :: %__MODULE__{ |
| 12 | + network_id: Hash.t(), |
| 13 | + nonce: UInt64.t(), |
| 14 | + invocation: AuthorizedInvocation.t() |
| 15 | + } |
| 16 | + |
| 17 | + defstruct [:network_id, :nonce, :invocation] |
| 18 | + |
| 19 | + @spec new( |
| 20 | + network_id :: Hash.t(), |
| 21 | + nonce :: UInt64.t(), |
| 22 | + invocation :: AuthorizedInvocation.t() |
| 23 | + ) :: t() |
| 24 | + def new(%Hash{} = network_id, %UInt64{} = nonce, %AuthorizedInvocation{} = invocation), |
| 25 | + do: %__MODULE__{network_id: network_id, nonce: nonce, invocation: invocation} |
| 26 | + |
| 27 | + @impl true |
| 28 | + def encode_xdr(%__MODULE__{network_id: network_id, nonce: nonce, invocation: invocation}) do |
| 29 | + [network_id: network_id, nonce: nonce, invocation: invocation] |
| 30 | + |> XDR.Struct.new() |
| 31 | + |> XDR.Struct.encode_xdr() |
| 32 | + end |
| 33 | + |
| 34 | + @impl true |
| 35 | + def encode_xdr!(%__MODULE__{network_id: network_id, nonce: nonce, invocation: invocation}) do |
| 36 | + [network_id: network_id, nonce: nonce, invocation: invocation] |
| 37 | + |> XDR.Struct.new() |
| 38 | + |> XDR.Struct.encode_xdr!() |
| 39 | + end |
| 40 | + |
| 41 | + @impl true |
| 42 | + def decode_xdr(bytes, struct \\ @struct_spec) |
| 43 | + |
| 44 | + def decode_xdr(bytes, struct) do |
| 45 | + case XDR.Struct.decode_xdr(bytes, struct) do |
| 46 | + {:ok, |
| 47 | + {%XDR.Struct{ |
| 48 | + components: [ |
| 49 | + network_id: network_id, |
| 50 | + nonce: nonce, |
| 51 | + invocation: invocation |
| 52 | + ] |
| 53 | + }, rest}} -> |
| 54 | + {:ok, {new(network_id, nonce, invocation), rest}} |
| 55 | + |
| 56 | + error -> |
| 57 | + error |
| 58 | + end |
| 59 | + end |
| 60 | + |
| 61 | + @impl true |
| 62 | + def decode_xdr!(bytes, struct \\ @struct_spec) |
| 63 | + |
| 64 | + def decode_xdr!(bytes, struct) do |
| 65 | + {%XDR.Struct{ |
| 66 | + components: [ |
| 67 | + network_id: network_id, |
| 68 | + nonce: nonce, |
| 69 | + invocation: invocation |
| 70 | + ] |
| 71 | + }, rest} = XDR.Struct.decode_xdr!(bytes, struct) |
| 72 | + |
| 73 | + {new(network_id, nonce, invocation), rest} |
| 74 | + end |
| 75 | +end |
0 commit comments