Skip to content

Commit 4b5a20f

Browse files
Add types related to the Stellar.transaction.x file from CAP46 part 2 (#239)
Co-authored-by: norn <keliumJU@users.noreply.github.com>
1 parent ff88029 commit 4b5a20f

23 files changed

+1988
-2
lines changed
+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
defmodule StellarBase.XDR.AddressWithNonce do
2+
@moduledoc """
3+
Representation of Stellar `AddressWithNonce` type.
4+
"""
5+
alias StellarBase.XDR.{SCAddress, UInt64}
6+
7+
@behaviour XDR.Declaration
8+
9+
@struct_spec XDR.Struct.new(address: SCAddress, nonce: UInt64)
10+
11+
@type t :: %__MODULE__{address: SCAddress.t(), nonce: UInt64.t()}
12+
13+
defstruct [:address, :nonce]
14+
15+
@spec new(address :: SCAddress.t(), nonce :: UInt64.t()) :: t()
16+
def new(%SCAddress{} = address, %UInt64{} = nonce),
17+
do: %__MODULE__{address: address, nonce: nonce}
18+
19+
@impl true
20+
def encode_xdr(%__MODULE__{address: address, nonce: nonce}) do
21+
[address: address, nonce: nonce]
22+
|> XDR.Struct.new()
23+
|> XDR.Struct.encode_xdr()
24+
end
25+
26+
@impl true
27+
def encode_xdr!(%__MODULE__{address: address, nonce: nonce}) do
28+
[address: address, nonce: nonce]
29+
|> XDR.Struct.new()
30+
|> XDR.Struct.encode_xdr!()
31+
end
32+
33+
@impl true
34+
def decode_xdr(bytes, struct \\ @struct_spec)
35+
36+
def decode_xdr(bytes, struct) do
37+
case XDR.Struct.decode_xdr(bytes, struct) do
38+
{:ok, {%XDR.Struct{components: [address: address, nonce: nonce]}, rest}} ->
39+
{:ok, {new(address, nonce), rest}}
40+
41+
error ->
42+
error
43+
end
44+
end
45+
46+
@impl true
47+
def decode_xdr!(bytes, struct \\ @struct_spec)
48+
49+
def decode_xdr!(bytes, struct) do
50+
{%XDR.Struct{components: [address: address, nonce: nonce]}, rest} =
51+
XDR.Struct.decode_xdr!(bytes, struct)
52+
53+
{new(address, nonce), rest}
54+
end
55+
end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
defmodule StellarBase.XDR.AuthorizedInvocation do
2+
@moduledoc """
3+
Representation of Stellar `AuthorizedInvocation` type.
4+
"""
5+
6+
alias StellarBase.XDR.{Hash, SCSymbol, SCVec, AuthorizedInvocationList}
7+
8+
@behaviour XDR.Declaration
9+
10+
@struct_spec XDR.Struct.new(
11+
contract_id: Hash,
12+
function_name: SCSymbol,
13+
args: SCVec,
14+
sub_invocations: AuthorizedInvocationList
15+
)
16+
17+
@type contract_id :: Hash.t()
18+
@type function_name :: SCSymbol.t()
19+
@type args :: SCVec.t()
20+
@type sub_invocations :: AuthorizedInvocationList.t()
21+
22+
@type t :: %__MODULE__{
23+
contract_id: contract_id(),
24+
function_name: function_name(),
25+
args: args(),
26+
sub_invocations: sub_invocations()
27+
}
28+
29+
defstruct [:contract_id, :function_name, :args, :sub_invocations]
30+
31+
@spec new(
32+
contract_id :: contract_id(),
33+
function_name :: function_name(),
34+
args :: args(),
35+
sub_invocations :: sub_invocations()
36+
) :: t()
37+
def new(
38+
%Hash{} = contract_id,
39+
%SCSymbol{} = function_name,
40+
%SCVec{} = args,
41+
%AuthorizedInvocationList{} = sub_invocations
42+
),
43+
do: %__MODULE__{
44+
contract_id: contract_id,
45+
function_name: function_name,
46+
args: args,
47+
sub_invocations: sub_invocations
48+
}
49+
50+
@impl true
51+
def encode_xdr(%__MODULE__{
52+
contract_id: contract_id,
53+
function_name: function_name,
54+
args: args,
55+
sub_invocations: sub_invocations
56+
}) do
57+
[
58+
contract_id: contract_id,
59+
function_name: function_name,
60+
args: args,
61+
sub_invocations: sub_invocations
62+
]
63+
|> XDR.Struct.new()
64+
|> XDR.Struct.encode_xdr()
65+
end
66+
67+
@impl true
68+
def encode_xdr!(%__MODULE__{
69+
contract_id: contract_id,
70+
function_name: function_name,
71+
args: args,
72+
sub_invocations: sub_invocations
73+
}) do
74+
[
75+
contract_id: contract_id,
76+
function_name: function_name,
77+
args: args,
78+
sub_invocations: sub_invocations
79+
]
80+
|> XDR.Struct.new()
81+
|> XDR.Struct.encode_xdr!()
82+
end
83+
84+
@impl true
85+
def decode_xdr(bytes, struct \\ @struct_spec)
86+
87+
def decode_xdr(bytes, struct) do
88+
case XDR.Struct.decode_xdr(bytes, struct) do
89+
{:ok,
90+
{%XDR.Struct{
91+
components: [
92+
contract_id: contract_id,
93+
function_name: function_name,
94+
args: args,
95+
sub_invocations: sub_invocations
96+
]
97+
}, rest}} ->
98+
{:ok, {new(contract_id, function_name, args, sub_invocations), rest}}
99+
100+
error ->
101+
error
102+
end
103+
end
104+
105+
@impl true
106+
def decode_xdr!(bytes, struct \\ @struct_spec)
107+
108+
def decode_xdr!(bytes, struct) do
109+
{%XDR.Struct{
110+
components: [
111+
contract_id: contract_id,
112+
function_name: function_name,
113+
args: args,
114+
sub_invocations: sub_invocations
115+
]
116+
}, rest} = XDR.Struct.decode_xdr!(bytes, struct)
117+
118+
{new(contract_id, function_name, args, sub_invocations), rest}
119+
end
120+
end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
defmodule StellarBase.XDR.AuthorizedInvocationList do
2+
@moduledoc """
3+
Representation of a Stellar `AuthorizedInvocationList` list.
4+
"""
5+
alias StellarBase.XDR.AuthorizedInvocation
6+
7+
@behaviour XDR.Declaration
8+
9+
@max_length 4_294_967_295
10+
11+
@array_type AuthorizedInvocation
12+
13+
@array_spec %{type: @array_type, max_length: @max_length}
14+
15+
@type t :: %__MODULE__{sub_invocations: list(AuthorizedInvocation.t())}
16+
17+
defstruct [:sub_invocations]
18+
19+
@spec new(sub_invocations :: list(AuthorizedInvocation.t())) :: t()
20+
def new(sub_invocations \\ []), do: %__MODULE__{sub_invocations: sub_invocations}
21+
22+
@impl true
23+
def encode_xdr(%__MODULE__{sub_invocations: sub_invocations}) do
24+
sub_invocations
25+
|> XDR.VariableArray.new(@array_type, @max_length)
26+
|> XDR.VariableArray.encode_xdr()
27+
end
28+
29+
@impl true
30+
def encode_xdr!(%__MODULE__{sub_invocations: sub_invocations}) do
31+
sub_invocations
32+
|> XDR.VariableArray.new(@array_type, @max_length)
33+
|> XDR.VariableArray.encode_xdr!()
34+
end
35+
36+
@impl true
37+
def decode_xdr(bytes, spec \\ @array_spec)
38+
39+
def decode_xdr(bytes, spec) do
40+
case XDR.VariableArray.decode_xdr(bytes, spec) do
41+
{:ok, {sub_invocations, rest}} -> {:ok, {new(sub_invocations), rest}}
42+
error -> error
43+
end
44+
end
45+
46+
@impl true
47+
def decode_xdr!(bytes, spec \\ @array_spec)
48+
49+
def decode_xdr!(bytes, spec) do
50+
{sub_invocations, rest} = XDR.VariableArray.decode_xdr!(bytes, spec)
51+
{new(sub_invocations), rest}
52+
end
53+
end

lib/xdr/transactions/contract_auth.ex

+103
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
defmodule StellarBase.XDR.ContractAuth do
2+
@moduledoc """
3+
Representation of Stellar `ContractAuth` type.
4+
"""
5+
alias StellarBase.XDR.{OptionalAddressWithNonce, AuthorizedInvocation, SCVec}
6+
7+
@behaviour XDR.Declaration
8+
9+
@struct_spec XDR.Struct.new(
10+
address_with_nonce: OptionalAddressWithNonce,
11+
authorized_invocation: AuthorizedInvocation,
12+
signature_args: SCVec
13+
)
14+
15+
@type t :: %__MODULE__{
16+
address_with_nonce: OptionalAddressWithNonce.t(),
17+
authorized_invocation: AuthorizedInvocation.t(),
18+
signature_args: SCVec.t()
19+
}
20+
21+
defstruct [:address_with_nonce, :authorized_invocation, :signature_args]
22+
23+
@spec new(
24+
address_with_nonce :: OptionalAddressWithNonce.t(),
25+
authorized_invocation :: AuthorizedInvocation.t(),
26+
signature_args :: SCVec.t()
27+
) :: t()
28+
def new(
29+
%OptionalAddressWithNonce{} = address_with_nonce,
30+
%AuthorizedInvocation{} = authorized_invocation,
31+
%SCVec{} = signature_args
32+
),
33+
do: %__MODULE__{
34+
address_with_nonce: address_with_nonce,
35+
authorized_invocation: authorized_invocation,
36+
signature_args: signature_args
37+
}
38+
39+
@impl true
40+
def encode_xdr(%__MODULE__{
41+
address_with_nonce: address_with_nonce,
42+
authorized_invocation: authorized_invocation,
43+
signature_args: signature_args
44+
}) do
45+
[
46+
address_with_nonce: address_with_nonce,
47+
authorized_invocation: authorized_invocation,
48+
signature_args: signature_args
49+
]
50+
|> XDR.Struct.new()
51+
|> XDR.Struct.encode_xdr()
52+
end
53+
54+
@impl true
55+
def encode_xdr!(%__MODULE__{
56+
address_with_nonce: address_with_nonce,
57+
authorized_invocation: authorized_invocation,
58+
signature_args: signature_args
59+
}) do
60+
[
61+
address_with_nonce: address_with_nonce,
62+
authorized_invocation: authorized_invocation,
63+
signature_args: signature_args
64+
]
65+
|> XDR.Struct.new()
66+
|> XDR.Struct.encode_xdr!()
67+
end
68+
69+
@impl true
70+
def decode_xdr(bytes, struct \\ @struct_spec)
71+
72+
def decode_xdr(bytes, struct) do
73+
case XDR.Struct.decode_xdr(bytes, struct) do
74+
{:ok,
75+
{%XDR.Struct{
76+
components: [
77+
address_with_nonce: address_with_nonce,
78+
authorized_invocation: authorized_invocation,
79+
signature_args: signature_args
80+
]
81+
}, rest}} ->
82+
{:ok, {new(address_with_nonce, authorized_invocation, signature_args), rest}}
83+
84+
error ->
85+
error
86+
end
87+
end
88+
89+
@impl true
90+
def decode_xdr!(bytes, struct \\ @struct_spec)
91+
92+
def decode_xdr!(bytes, struct) do
93+
{%XDR.Struct{
94+
components: [
95+
address_with_nonce: address_with_nonce,
96+
authorized_invocation: authorized_invocation,
97+
signature_args: signature_args
98+
]
99+
}, rest} = XDR.Struct.decode_xdr!(bytes, struct)
100+
101+
{new(address_with_nonce, authorized_invocation, signature_args), rest}
102+
end
103+
end
+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
defmodule StellarBase.XDR.ContractAuthList do
2+
@moduledoc """
3+
Representation of a Stellar `ContractAuthList` list.
4+
"""
5+
alias StellarBase.XDR.ContractAuth
6+
7+
@behaviour XDR.Declaration
8+
9+
@max_length 4_294_967_295
10+
11+
@array_type ContractAuth
12+
13+
@array_spec %{type: @array_type, max_length: @max_length}
14+
15+
@type t :: %__MODULE__{auth: list(ContractAuth.t())}
16+
17+
defstruct [:auth]
18+
19+
@spec new(auth :: list(ContractAuth.t())) :: t()
20+
def new(auth), do: %__MODULE__{auth: auth}
21+
22+
@impl true
23+
def encode_xdr(%__MODULE__{auth: auth}) do
24+
auth
25+
|> XDR.VariableArray.new(@array_type, @max_length)
26+
|> XDR.VariableArray.encode_xdr()
27+
end
28+
29+
@impl true
30+
def encode_xdr!(%__MODULE__{auth: auth}) do
31+
auth
32+
|> XDR.VariableArray.new(@array_type, @max_length)
33+
|> XDR.VariableArray.encode_xdr!()
34+
end
35+
36+
@impl true
37+
def decode_xdr(bytes, spec \\ @array_spec)
38+
39+
def decode_xdr(bytes, spec) do
40+
case XDR.VariableArray.decode_xdr(bytes, spec) do
41+
{:ok, {auth, rest}} -> {:ok, {new(auth), rest}}
42+
error -> error
43+
end
44+
end
45+
46+
@impl true
47+
def decode_xdr!(bytes, spec \\ @array_spec)
48+
49+
def decode_xdr!(bytes, spec) do
50+
{auth, rest} = XDR.VariableArray.decode_xdr!(bytes, spec)
51+
{new(auth), rest}
52+
end
53+
end

0 commit comments

Comments
 (0)