diff --git a/CHANGELOG.md b/CHANGELOG.md index 55f29026..b24eec41 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,9 @@ # Changelog +## 0.10.0 (24.04.2023) + +* Add [Soroban Preview 8](https://soroban.stellar.org/docs/reference/releases#preview-8-april-4th-2023) Support in the XDR version: [7356dc237ee0db5626561c129fb3fa4beaabbac6](https://github.com/stellar/stellar-xdr/commit/7356dc237ee0db5626561c129fb3fa4beaabbac6) with its respective [Preview 8 changes](https://github.com/stellar/stellar-xdr/compare/df18148747e807618acf4639db41c4fd6f0be9fc...7356dc237ee0db5626561c129fb3fa4beaabbac6#diff-891b3a6e0eb8f9ac887a8129e2c821931837fb42224200ee78cce639eeb61c15). + ## 0.9.1 (31.03.2023) * Update boundary for InvokeHostFuntion, from `StellarBase.XDR.InvokeHostFunctionOp` to `StellarBase.XDR.Operations.InvokeHostFunction`. diff --git a/README.md b/README.md index 10d2abcb..0738a9bc 100644 --- a/README.md +++ b/README.md @@ -26,7 +26,7 @@ You should only use **`stellar_base`** if you are planning to build on top of it ```elixir def deps do [ - {:stellar_base, "~> 0.9.1"} + {:stellar_base, "~> 0.10.0"} ] end ``` diff --git a/lib/xdr/contract/optional_sc_object.ex b/lib/xdr/contract/optional_sc_map.ex similarity index 50% rename from lib/xdr/contract/optional_sc_object.ex rename to lib/xdr/contract/optional_sc_map.ex index 60b009a3..1412c915 100644 --- a/lib/xdr/contract/optional_sc_object.ex +++ b/lib/xdr/contract/optional_sc_map.ex @@ -1,33 +1,33 @@ -defmodule StellarBase.XDR.OptionalSCObject do +defmodule StellarBase.XDR.OptionalSCMap do @moduledoc """ - Representation of Stellar `OptionalSCObject` type. + Representation of Stellar `OptionalSCMap` type. """ - alias StellarBase.XDR.SCObject + alias StellarBase.XDR.SCMap @behaviour XDR.Declaration - @optional_spec XDR.Optional.new(SCObject) + @optional_spec XDR.Optional.new(SCMap) - @type sc_object :: SCObject.t() | nil + @type sc_map :: SCMap.t() | nil - @type t :: %__MODULE__{sc_object: sc_object()} + @type t :: %__MODULE__{sc_map: sc_map()} - defstruct [:sc_object] + defstruct [:sc_map] - @spec new(sc_object :: sc_object()) :: t() - def new(sc_object \\ nil), do: %__MODULE__{sc_object: sc_object} + @spec new(sc_map :: sc_map()) :: t() + def new(sc_map \\ nil), do: %__MODULE__{sc_map: sc_map} @impl true - def encode_xdr(%__MODULE__{sc_object: sc_object}) do - sc_object + def encode_xdr(%__MODULE__{sc_map: sc_map}) do + sc_map |> XDR.Optional.new() |> XDR.Optional.encode_xdr() end @impl true - def encode_xdr!(%__MODULE__{sc_object: sc_object}) do - sc_object + def encode_xdr!(%__MODULE__{sc_map: sc_map}) do + sc_map |> XDR.Optional.new() |> XDR.Optional.encode_xdr!() end @@ -37,8 +37,8 @@ defmodule StellarBase.XDR.OptionalSCObject do def decode_xdr(bytes, optional_spec) do case XDR.Optional.decode_xdr(bytes, optional_spec) do - {:ok, {%XDR.Optional{type: sc_object}, rest}} -> - {:ok, {new(sc_object), rest}} + {:ok, {%XDR.Optional{type: sc_map}, rest}} -> + {:ok, {new(sc_map), rest}} {:ok, {nil, rest}} -> {:ok, {new(), rest}} @@ -53,7 +53,7 @@ defmodule StellarBase.XDR.OptionalSCObject do def decode_xdr!(bytes, optional_spec) do case XDR.Optional.decode_xdr!(bytes, optional_spec) do - {%XDR.Optional{type: sc_object}, rest} -> {new(sc_object), rest} + {%XDR.Optional{type: sc_map}, rest} -> {new(sc_map), rest} {nil, rest} -> {new(), rest} end end diff --git a/lib/xdr/contract/optional_sc_vec.ex b/lib/xdr/contract/optional_sc_vec.ex new file mode 100644 index 00000000..1a4395ed --- /dev/null +++ b/lib/xdr/contract/optional_sc_vec.ex @@ -0,0 +1,60 @@ +defmodule StellarBase.XDR.OptionalSCVec do + @moduledoc """ + Representation of Stellar `OptionalSCVec` type. + """ + + alias StellarBase.XDR.SCVec + + @behaviour XDR.Declaration + + @optional_spec XDR.Optional.new(SCVec) + + @type sc_vec :: SCVec.t() | nil + + @type t :: %__MODULE__{sc_vec: sc_vec()} + + defstruct [:sc_vec] + + @spec new(sc_vec :: sc_vec()) :: t() + def new(sc_vec \\ nil), do: %__MODULE__{sc_vec: sc_vec} + + @impl true + def encode_xdr(%__MODULE__{sc_vec: sc_vec}) do + sc_vec + |> XDR.Optional.new() + |> XDR.Optional.encode_xdr() + end + + @impl true + def encode_xdr!(%__MODULE__{sc_vec: sc_vec}) do + sc_vec + |> XDR.Optional.new() + |> XDR.Optional.encode_xdr!() + end + + @impl true + def decode_xdr(bytes, optional_spec \\ @optional_spec) + + def decode_xdr(bytes, optional_spec) do + case XDR.Optional.decode_xdr(bytes, optional_spec) do + {:ok, {%XDR.Optional{type: sc_vec}, rest}} -> + {:ok, {new(sc_vec), rest}} + + {:ok, {nil, rest}} -> + {:ok, {new(), rest}} + + error -> + error + end + end + + @impl true + def decode_xdr!(bytes, optional_spec \\ @optional_spec) + + def decode_xdr!(bytes, optional_spec) do + case XDR.Optional.decode_xdr!(bytes, optional_spec) do + {%XDR.Optional{type: sc_vec}, rest} -> {new(sc_vec), rest} + {nil, rest} -> {new(), rest} + end + end +end diff --git a/lib/xdr/contract/sc_bytes.ex b/lib/xdr/contract/sc_bytes.ex new file mode 100644 index 00000000..3765f5b8 --- /dev/null +++ b/lib/xdr/contract/sc_bytes.ex @@ -0,0 +1,47 @@ +defmodule StellarBase.XDR.SCBytes do + @moduledoc """ + Representation of Stellar `SCBytes` type. + """ + alias StellarBase.XDR.VariableOpaque256000 + + @behaviour XDR.Declaration + + @type t :: %__MODULE__{value: binary()} + + defstruct [:value] + + @spec new(value :: binary()) :: t() + def new(value), do: %__MODULE__{value: value} + + @impl true + def encode_xdr(%__MODULE__{value: value}) do + value + |> VariableOpaque256000.new() + |> VariableOpaque256000.encode_xdr() + end + + @impl true + def encode_xdr!(%__MODULE__{value: value}) do + value + |> VariableOpaque256000.new() + |> VariableOpaque256000.encode_xdr!() + end + + @impl true + def decode_xdr(bytes, term \\ nil) + + def decode_xdr(bytes, _term) do + case VariableOpaque256000.decode_xdr(bytes) do + {:ok, {%VariableOpaque256000{opaque: value}, rest}} -> {:ok, {new(value), rest}} + error -> error + end + end + + @impl true + def decode_xdr!(bytes, term \\ nil) + + def decode_xdr!(bytes, _term) do + {%VariableOpaque256000{opaque: value}, rest} = VariableOpaque256000.decode_xdr!(bytes) + {new(value), rest} + end +end diff --git a/lib/xdr/contract/sc_contract_code.ex b/lib/xdr/contract/sc_contract_code.ex deleted file mode 100644 index aa2858e1..00000000 --- a/lib/xdr/contract/sc_contract_code.ex +++ /dev/null @@ -1,63 +0,0 @@ -defmodule StellarBase.XDR.SCContractCode do - @moduledoc """ - Representation of Stellar `SCContractCode` type. - """ - - alias StellarBase.XDR.{Hash, SCContractCodeType, Void} - - @behaviour XDR.Declaration - - @arms [ - SCCONTRACT_CODE_WASM_REF: Hash, - SCCONTRACT_CODE_TOKEN: Void - ] - - @type contract_code :: Hash.t() | Void.t() - - @type t :: %__MODULE__{contract_code: contract_code(), type: SCContractCodeType.t()} - - defstruct [:contract_code, :type] - - @spec new(contract_code :: contract_code(), type :: SCContractCodeType.t()) :: t() - def new(contract_code, %SCContractCodeType{} = type), - do: %__MODULE__{contract_code: contract_code, type: type} - - @impl true - def encode_xdr(%__MODULE__{contract_code: contract_code, type: type}) do - type - |> XDR.Union.new(@arms, contract_code) - |> XDR.Union.encode_xdr() - end - - @impl true - def encode_xdr!(%__MODULE__{contract_code: contract_code, type: type}) do - type - |> XDR.Union.new(@arms, contract_code) - |> XDR.Union.encode_xdr!() - end - - @impl true - def decode_xdr(bytes, spec \\ union_spec()) - - def decode_xdr(bytes, spec) do - case XDR.Union.decode_xdr(bytes, spec) do - {:ok, {{type, contract_code}, rest}} -> {:ok, {new(contract_code, type), rest}} - error -> error - end - end - - @impl true - def decode_xdr!(bytes, spec \\ union_spec()) - - def decode_xdr!(bytes, spec) do - {{type, contract_code}, rest} = XDR.Union.decode_xdr!(bytes, spec) - {new(contract_code, type), rest} - end - - @spec union_spec() :: XDR.Union.t() - defp union_spec do - nil - |> SCContractCodeType.new() - |> XDR.Union.new(@arms) - end -end diff --git a/lib/xdr/contract/sc_contract_executable.ex b/lib/xdr/contract/sc_contract_executable.ex new file mode 100644 index 00000000..1bcb6e2d --- /dev/null +++ b/lib/xdr/contract/sc_contract_executable.ex @@ -0,0 +1,67 @@ +defmodule StellarBase.XDR.SCContractExecutable do + @moduledoc """ + Representation of Stellar `SCContractExecutable` type. + """ + + alias StellarBase.XDR.{Hash, SCContractExecutableType, Void} + + @behaviour XDR.Declaration + + @arms [ + SCCONTRACT_EXECUTABLE_WASM_REF: Hash, + SCCONTRACT_EXECUTABLE_TOKEN: Void + ] + + @type contract_executable :: Hash.t() | Void.t() + + @type t :: %__MODULE__{ + contract_executable: contract_executable(), + type: SCContractExecutableType.t() + } + + defstruct [:contract_executable, :type] + + @spec new(contract_executable :: contract_executable(), type :: SCContractExecutableType.t()) :: + t() + def new(contract_executable, %SCContractExecutableType{} = type), + do: %__MODULE__{contract_executable: contract_executable, type: type} + + @impl true + def encode_xdr(%__MODULE__{contract_executable: contract_executable, type: type}) do + type + |> XDR.Union.new(@arms, contract_executable) + |> XDR.Union.encode_xdr() + end + + @impl true + def encode_xdr!(%__MODULE__{contract_executable: contract_executable, type: type}) do + type + |> XDR.Union.new(@arms, contract_executable) + |> XDR.Union.encode_xdr!() + end + + @impl true + def decode_xdr(bytes, spec \\ union_spec()) + + def decode_xdr(bytes, spec) do + case XDR.Union.decode_xdr(bytes, spec) do + {:ok, {{type, contract_executable}, rest}} -> {:ok, {new(contract_executable, type), rest}} + error -> error + end + end + + @impl true + def decode_xdr!(bytes, spec \\ union_spec()) + + def decode_xdr!(bytes, spec) do + {{type, contract_executable}, rest} = XDR.Union.decode_xdr!(bytes, spec) + {new(contract_executable, type), rest} + end + + @spec union_spec() :: XDR.Union.t() + defp union_spec do + nil + |> SCContractExecutableType.new() + |> XDR.Union.new(@arms) + end +end diff --git a/lib/xdr/contract/sc_contract_code_type.ex b/lib/xdr/contract/sc_contract_executable_type.ex similarity index 84% rename from lib/xdr/contract/sc_contract_code_type.ex rename to lib/xdr/contract/sc_contract_executable_type.ex index 583a5bc2..271e092e 100644 --- a/lib/xdr/contract/sc_contract_code_type.ex +++ b/lib/xdr/contract/sc_contract_executable_type.ex @@ -1,13 +1,13 @@ -defmodule StellarBase.XDR.SCContractCodeType do +defmodule StellarBase.XDR.SCContractExecutableType do @moduledoc """ - Representation of Stellar `SCContractCodeType` type. + Representation of Stellar `SCContractExecutableType` type. """ @behaviour XDR.Declaration @declarations [ - SCCONTRACT_CODE_WASM_REF: 0, - SCCONTRACT_CODE_TOKEN: 1 + SCCONTRACT_EXECUTABLE_WASM_REF: 0, + SCCONTRACT_EXECUTABLE_TOKEN: 1 ] @enum_spec %XDR.Enum{declarations: @declarations, identifier: nil} diff --git a/lib/xdr/contract/sc_nonce_key.ex b/lib/xdr/contract/sc_nonce_key.ex new file mode 100644 index 00000000..0374d78e --- /dev/null +++ b/lib/xdr/contract/sc_nonce_key.ex @@ -0,0 +1,55 @@ +defmodule StellarBase.XDR.SCNonceKey do + @moduledoc """ + Representation of Stellar `SCNonceKey` type. + """ + alias StellarBase.XDR.SCAddress + + @behaviour XDR.Declaration + + @struct_spec XDR.Struct.new(nonce_address: SCAddress) + + @type t :: %__MODULE__{nonce_address: SCAddress.t()} + + defstruct [:nonce_address] + + @spec new(nonce_address :: SCAddress.t()) :: t() + def new(%SCAddress{} = nonce_address), + do: %__MODULE__{nonce_address: nonce_address} + + @impl true + def encode_xdr(%__MODULE__{nonce_address: nonce_address}) do + [nonce_address: nonce_address] + |> XDR.Struct.new() + |> XDR.Struct.encode_xdr() + end + + @impl true + def encode_xdr!(%__MODULE__{nonce_address: nonce_address}) do + [nonce_address: nonce_address] + |> XDR.Struct.new() + |> XDR.Struct.encode_xdr!() + end + + @impl true + def decode_xdr(bytes, struct \\ @struct_spec) + + def decode_xdr(bytes, struct) do + case XDR.Struct.decode_xdr(bytes, struct) do + {:ok, {%XDR.Struct{components: [nonce_address: nonce_address]}, rest}} -> + {:ok, {new(nonce_address), rest}} + + error -> + error + end + end + + @impl true + def decode_xdr!(bytes, struct \\ @struct_spec) + + def decode_xdr!(bytes, struct) do + {%XDR.Struct{components: [nonce_address: nonce_address]}, rest} = + XDR.Struct.decode_xdr!(bytes, struct) + + {new(nonce_address), rest} + end +end diff --git a/lib/xdr/contract/sc_object.ex b/lib/xdr/contract/sc_object.ex deleted file mode 100644 index d05363e3..00000000 --- a/lib/xdr/contract/sc_object.ex +++ /dev/null @@ -1,88 +0,0 @@ -defmodule StellarBase.XDR.SCObject do - @moduledoc """ - Representation of Stellar `SCObjectStellarBase.XDR.SCObject` type. - """ - - alias StellarBase.XDR.{ - Int128Parts, - Int64, - SCAddress, - SCContractCode, - SCMap, - SCObjectType, - SCVec, - UInt64, - VariableOpaque256000 - } - - @behaviour XDR.Declaration - - @arms [ - SCO_VEC: SCVec, - SCO_MAP: SCMap, - SCO_U64: UInt64, - SCO_I64: Int64, - SCO_U128: Int128Parts, - SCO_I128: Int128Parts, - SCO_BYTES: VariableOpaque256000, - SCO_CONTRACT_CODE: SCContractCode, - SCO_ADDRESS: SCAddress, - SCO_NONCE_KEY: SCAddress - ] - - @type sc_object :: - SCVec.t() - | SCMap.t() - | UInt64.t() - | Int64.t() - | Int128Parts.t() - | VariableOpaque256000.t() - | SCContractCode.t() - | SCAddress.t() - - @type t :: %__MODULE__{sc_object: sc_object(), type: SCObjectType.t()} - - defstruct [:sc_object, :type] - - @spec new(sc_object :: sc_object(), type :: SCObjectType.t()) :: t() - def new(sc_object, %SCObjectType{} = type), do: %__MODULE__{sc_object: sc_object, type: type} - - @impl true - def encode_xdr(%__MODULE__{sc_object: sc_object, type: type}) do - type - |> XDR.Union.new(@arms, sc_object) - |> XDR.Union.encode_xdr() - end - - @impl true - def encode_xdr!(%__MODULE__{sc_object: sc_object, type: type}) do - type - |> XDR.Union.new(@arms, sc_object) - |> XDR.Union.encode_xdr!() - end - - @impl true - def decode_xdr(bytes, spec \\ union_spec()) - - def decode_xdr(bytes, spec) do - case XDR.Union.decode_xdr(bytes, spec) do - {:ok, {{type, sc_object}, rest}} -> {:ok, {new(sc_object, type), rest}} - error -> error - end - end - - @impl true - def decode_xdr!(bytes, spec \\ union_spec()) - - def decode_xdr!(bytes, spec) do - {{type, sc_object}, rest} = XDR.Union.decode_xdr!(bytes, spec) - {new(sc_object, type), rest} - end - - @spec union_spec() :: XDR.Union.t() - defp union_spec do - nil - |> SCObjectType.new() - |> XDR.Union.new(@arms) - end -end diff --git a/lib/xdr/contract/sc_object_type.ex b/lib/xdr/contract/sc_object_type.ex deleted file mode 100644 index 228c6110..00000000 --- a/lib/xdr/contract/sc_object_type.ex +++ /dev/null @@ -1,61 +0,0 @@ -defmodule StellarBase.XDR.SCObjectType do - @moduledoc """ - Representation of Stellar `SCObjectType` type. - """ - - @behaviour XDR.Declaration - - @declarations [ - SCO_VEC: 0, - SCO_MAP: 1, - SCO_U64: 2, - SCO_I64: 3, - SCO_U128: 4, - SCO_I128: 5, - SCO_BYTES: 6, - SCO_CONTRACT_CODE: 7, - SCO_ADDRESS: 8, - SCO_NONCE_KEY: 9 - ] - - @enum_spec %XDR.Enum{declarations: @declarations, identifier: nil} - - @type t :: %__MODULE__{identifier: atom()} - - defstruct [:identifier] - - @spec new(type :: atom()) :: t() - def new(type), do: %__MODULE__{identifier: type} - - @impl true - def encode_xdr(%__MODULE__{identifier: type}) do - @declarations - |> XDR.Enum.new(type) - |> XDR.Enum.encode_xdr() - end - - @impl true - def encode_xdr!(%__MODULE__{identifier: type}) do - @declarations - |> XDR.Enum.new(type) - |> XDR.Enum.encode_xdr!() - end - - @impl true - def decode_xdr(bytes, spec \\ @enum_spec) - - def decode_xdr(bytes, spec) do - case XDR.Enum.decode_xdr(bytes, spec) do - {:ok, {%XDR.Enum{identifier: type}, rest}} -> {:ok, {new(type), rest}} - error -> error - end - end - - @impl true - def decode_xdr!(bytes, spec \\ @enum_spec) - - def decode_xdr!(bytes, spec) do - {%XDR.Enum{identifier: type}, rest} = XDR.Enum.decode_xdr!(bytes, spec) - {new(type), rest} - end -end diff --git a/lib/xdr/contract/sc_string.ex b/lib/xdr/contract/sc_string.ex new file mode 100644 index 00000000..4aad8a1f --- /dev/null +++ b/lib/xdr/contract/sc_string.ex @@ -0,0 +1,48 @@ +defmodule StellarBase.XDR.SCString do + @moduledoc """ + Representation of Stellar `SCString` type. + """ + + @behaviour XDR.Declaration + + @type t :: %__MODULE__{value: String.t()} + + defstruct [:value] + + @max_length 256_000 + + @spec new(value :: String.t()) :: t() + def new(value), do: %__MODULE__{value: value} + + @impl true + def encode_xdr(%__MODULE__{value: value}) do + value + |> XDR.String.new(@max_length) + |> XDR.String.encode_xdr() + end + + @impl true + def encode_xdr!(%__MODULE__{value: value}) do + value + |> XDR.String.new(@max_length) + |> XDR.String.encode_xdr!() + end + + @impl true + def decode_xdr(bytes, term \\ nil) + + def decode_xdr(bytes, _term) do + case XDR.String.decode_xdr(bytes) do + {:ok, {%XDR.String{string: value}, rest}} -> {:ok, {new(value), rest}} + error -> error + end + end + + @impl true + def decode_xdr!(bytes, term \\ nil) + + def decode_xdr!(bytes, _term) do + {%XDR.String{string: value}, rest} = XDR.String.decode_xdr!(bytes) + {new(value), rest} + end +end diff --git a/lib/xdr/contract/sc_symbol.ex b/lib/xdr/contract/sc_symbol.ex index b1c28236..f05552b1 100644 --- a/lib/xdr/contract/sc_symbol.ex +++ b/lib/xdr/contract/sc_symbol.ex @@ -9,7 +9,7 @@ defmodule StellarBase.XDR.SCSymbol do defstruct [:value] - @max_length 10 + @max_length 32 @spec new(value :: String.t()) :: t() def new(value), do: %__MODULE__{value: value} diff --git a/lib/xdr/contract/sc_val.ex b/lib/xdr/contract/sc_val.ex index b586f7cf..4f1b8c05 100644 --- a/lib/xdr/contract/sc_val.ex +++ b/lib/xdr/contract/sc_val.ex @@ -4,39 +4,75 @@ defmodule StellarBase.XDR.SCVal do """ alias StellarBase.XDR.{ + Bool, + Duration, + Int128Parts, Int64, Int32, - OptionalSCObject, + UInt256, UInt32, UInt64, + SCAddress, + SCBytes, + SCContractExecutable, + SCNonceKey, SCValType, - SCStatic, SCStatus, - SCSymbol + SCString, + SCSymbol, + TimePoint, + OptionalSCVec, + OptionalSCMap, + Void } @behaviour XDR.Declaration @arms [ - SCV_U63: Int64, + SCV_BOOL: Bool, + SCV_VOID: Void, + SCV_STATUS: SCStatus, SCV_U32: UInt32, SCV_I32: Int32, - SCV_STATIC: SCStatic, - SCV_OBJECT: OptionalSCObject, + SCV_U64: UInt64, + SCV_I64: Int64, + SCV_TIMEPOINT: TimePoint, + SCV_DURATION: Duration, + SCV_U128: Int128Parts, + SCV_I128: Int128Parts, + SCV_U256: UInt256, + SCV_I256: UInt256, + SCV_BYTES: SCBytes, + SCV_STRING: SCString, SCV_SYMBOL: SCSymbol, - SCV_BITSET: UInt64, - SCV_STATUS: SCStatus + SCV_VEC: OptionalSCVec, + SCV_MAP: OptionalSCMap, + SCV_CONTRACT_EXECUTABLE: SCContractExecutable, + SCV_ADDRESS: SCAddress, + SCV_LEDGER_KEY_CONTRACT_EXECUTABLE: Void, + SCV_LEDGER_KEY_NONCE: SCNonceKey ] @type value :: - Int64.t() + Bool.t() + | Void.t() + | SCStatus.t() | UInt32.t() | Int32.t() - | SCStatic.t() - | OptionalSCObject.t() - | SCSymbol.t() | UInt64.t() - | SCStatus.t() + | Int64.t() + | TimePoint.t() + | Duration.t() + | Int128Parts.t() + | UInt256.t() + | SCBytes.t() + | SCString.t() + | SCSymbol.t() + | OptionalSCVec.t() + | OptionalSCMap.t() + | SCContractExecutable.t() + | SCAddress.t() + | SCNonceKey.t() @type t :: %__MODULE__{value: value(), type: SCValType.t()} diff --git a/lib/xdr/contract/sc_val_type.ex b/lib/xdr/contract/sc_val_type.ex index 5ea744f1..cc6d0d3a 100644 --- a/lib/xdr/contract/sc_val_type.ex +++ b/lib/xdr/contract/sc_val_type.ex @@ -6,14 +6,28 @@ defmodule StellarBase.XDR.SCValType do @behaviour XDR.Declaration @declarations [ - SCV_U63: 0, - SCV_U32: 1, - SCV_I32: 2, - SCV_STATIC: 3, - SCV_OBJECT: 4, - SCV_SYMBOL: 5, - SCV_BITSET: 6, - SCV_STATUS: 7 + SCV_BOOL: 0, + SCV_VOID: 1, + SCV_STATUS: 2, + SCV_U32: 3, + SCV_I32: 4, + SCV_U64: 5, + SCV_I64: 6, + SCV_TIMEPOINT: 7, + SCV_DURATION: 8, + SCV_U128: 9, + SCV_I128: 10, + SCV_U256: 11, + SCV_I256: 12, + SCV_BYTES: 13, + SCV_STRING: 14, + SCV_SYMBOL: 15, + SCV_VEC: 16, + SCV_MAP: 17, + SCV_CONTRACT_EXECUTABLE: 18, + SCV_ADDRESS: 19, + SCV_LEDGER_KEY_CONTRACT_EXECUTABLE: 20, + SCV_LEDGER_KEY_NONCE: 21 ] @enum_spec %XDR.Enum{declarations: @declarations, identifier: nil} @@ -23,7 +37,7 @@ defmodule StellarBase.XDR.SCValType do defstruct [:identifier] @spec new(type :: atom()) :: t() - def new(type \\ :SCV_U63), do: %__MODULE__{identifier: type} + def new(type \\ :SCV_BOOL), do: %__MODULE__{identifier: type} @impl true def encode_xdr(%__MODULE__{identifier: type}) do diff --git a/lib/xdr/contract/spec/sc_spec_type.ex b/lib/xdr/contract/spec/sc_spec_type.ex index d4a22352..92ab083d 100644 --- a/lib/xdr/contract/spec/sc_spec_type.ex +++ b/lib/xdr/contract/spec/sc_spec_type.ex @@ -9,19 +9,23 @@ defmodule StellarBase.XDR.SCSpecType do SC_SPEC_TYPE_VAL: 0, # Types with no parameters. - SC_SPEC_TYPE_U32: 1, - SC_SPEC_TYPE_I32: 2, - SC_SPEC_TYPE_U64: 3, - SC_SPEC_TYPE_I64: 4, - SC_SPEC_TYPE_U128: 5, - SC_SPEC_TYPE_I128: 6, - SC_SPEC_TYPE_BOOL: 7, - SC_SPEC_TYPE_SYMBOL: 8, - SC_SPEC_TYPE_BITSET: 9, - SC_SPEC_TYPE_STATUS: 10, - SC_SPEC_TYPE_BYTES: 11, - SC_SPEC_TYPE_INVOKER: 12, - SC_SPEC_TYPE_ADDRESS: 13, + SC_SPEC_TYPE_BOOL: 1, + SC_SPEC_TYPE_VOID: 2, + SC_SPEC_TYPE_STATUS: 3, + SC_SPEC_TYPE_U32: 4, + SC_SPEC_TYPE_I32: 5, + SC_SPEC_TYPE_U64: 6, + SC_SPEC_TYPE_I64: 7, + SC_SPEC_TYPE_TIMEPOINT: 8, + SC_SPEC_TYPE_DURATION: 9, + SC_SPEC_TYPE_U128: 10, + SC_SPEC_TYPE_I128: 11, + SC_SPEC_TYPE_U256: 12, + SC_SPEC_TYPE_I256: 13, + SC_SPEC_TYPE_BYTES: 14, + SC_SPEC_TYPE_STRING: 16, + SC_SPEC_TYPE_SYMBOL: 17, + SC_SPEC_TYPE_ADDRESS: 19, # Types with parameters. SC_SPEC_TYPE_OPTION: 1000, diff --git a/lib/xdr/contract/spec/sc_spec_type_def.ex b/lib/xdr/contract/spec/sc_spec_type_def.ex index ef32f4c8..0cdaf5ca 100644 --- a/lib/xdr/contract/spec/sc_spec_type_def.ex +++ b/lib/xdr/contract/spec/sc_spec_type_def.ex @@ -20,17 +20,22 @@ defmodule StellarBase.XDR.SCSpecTypeDef do @arms [ SC_SPEC_TYPE_VAL: Void, + SC_SPEC_TYPE_BOOL: Void, + SC_SPEC_TYPE_VOID: Void, + SC_SPEC_TYPE_STATUS: Void, + SC_SPEC_TYPE_U32: Void, + SC_SPEC_TYPE_I32: Void, SC_SPEC_TYPE_U64: Void, SC_SPEC_TYPE_I64: Void, + SC_SPEC_TYPE_TIMEPOINT: Void, + SC_SPEC_TYPE_DURATION: Void, SC_SPEC_TYPE_U128: Void, SC_SPEC_TYPE_I128: Void, - SC_SPEC_TYPE_U32: Void, - SC_SPEC_TYPE_I32: Void, - SC_SPEC_TYPE_BOOL: Void, - SC_SPEC_TYPE_SYMBOL: Void, - SC_SPEC_TYPE_BITSET: Void, - SC_SPEC_TYPE_STATUS: Void, + SC_SPEC_TYPE_U256: Void, + SC_SPEC_TYPE_I256: Void, SC_SPEC_TYPE_BYTES: Void, + SC_SPEC_TYPE_STRING: Void, + SC_SPEC_TYPE_SYMBOL: Void, SC_SPEC_TYPE_ADDRESS: Void, SC_SPEC_TYPE_OPTION: SCSpecTypeOption, SC_SPEC_TYPE_RESULT: SCSpecTypeResult, diff --git a/lib/xdr/transactions/create_contract_args.ex b/lib/xdr/transactions/create_contract_args.ex index 7365df6c..efee89f1 100644 --- a/lib/xdr/transactions/create_contract_args.ex +++ b/lib/xdr/transactions/create_contract_args.ex @@ -2,18 +2,18 @@ defmodule StellarBase.XDR.CreateContractArgs do @moduledoc """ Representation of Stellar `CreateContractArgs` type. """ - alias StellarBase.XDR.{ContractID, SCContractCode} + alias StellarBase.XDR.{ContractID, SCContractExecutable} @behaviour XDR.Declaration - @struct_spec XDR.Struct.new(contract_id: ContractID, source: SCContractCode) + @struct_spec XDR.Struct.new(contract_id: ContractID, source: SCContractExecutable) - @type t :: %__MODULE__{contract_id: ContractID.t(), source: SCContractCode.t()} + @type t :: %__MODULE__{contract_id: ContractID.t(), source: SCContractExecutable.t()} defstruct [:contract_id, :source] - @spec new(contract_id :: ContractID.t(), source :: SCContractCode.t()) :: t() - def new(%ContractID{} = contract_id, %SCContractCode{} = source), + @spec new(contract_id :: ContractID.t(), source :: SCContractExecutable.t()) :: t() + def new(%ContractID{} = contract_id, %SCContractExecutable{} = source), do: %__MODULE__{contract_id: contract_id, source: source} @impl true diff --git a/lib/xdr/transactions/hash_id_preimage_create_contract_args.ex b/lib/xdr/transactions/hash_id_preimage_create_contract_args.ex index 0a8f584c..c0286689 100644 --- a/lib/xdr/transactions/hash_id_preimage_create_contract_args.ex +++ b/lib/xdr/transactions/hash_id_preimage_create_contract_args.ex @@ -2,18 +2,23 @@ defmodule StellarBase.XDR.HashIDPreimageCreateContractArgs do @moduledoc """ Representation of Stellar `HashIDPreimageCreateContractArgs` type. """ - alias StellarBase.XDR.{Hash, SCContractCode, UInt256} + alias StellarBase.XDR.{Hash, SCContractExecutable, UInt256} @behaviour XDR.Declaration - @struct_spec XDR.Struct.new(network_id: Hash, source: SCContractCode, salt: UInt256) + @struct_spec XDR.Struct.new(network_id: Hash, source: SCContractExecutable, salt: UInt256) - @type t :: %__MODULE__{network_id: Hash.t(), source: SCContractCode.t(), salt: UInt256.t()} + @type t :: %__MODULE__{ + network_id: Hash.t(), + source: SCContractExecutable.t(), + salt: UInt256.t() + } defstruct [:network_id, :source, :salt] - @spec new(network_id :: Hash.t(), source :: SCContractCode.t(), salt :: UInt256.t()) :: t() - def new(%Hash{} = network_id, %SCContractCode{} = source, %UInt256{} = salt), + @spec new(network_id :: Hash.t(), source :: SCContractExecutable.t(), salt :: UInt256.t()) :: + t() + def new(%Hash{} = network_id, %SCContractExecutable{} = source, %UInt256{} = salt), do: %__MODULE__{network_id: network_id, source: source, salt: salt} @impl true diff --git a/lib/xdr/types/bool.ex b/lib/xdr/types/bool.ex new file mode 100644 index 00000000..53efd4b5 --- /dev/null +++ b/lib/xdr/types/bool.ex @@ -0,0 +1,42 @@ +defmodule StellarBase.XDR.Bool do + @moduledoc """ + Representation of Stellar `Bool` type. + """ + + @behaviour XDR.Declaration + + @type t :: %__MODULE__{value: boolean()} + + defstruct [:value] + + @spec new(value :: boolean()) :: t() + def new(val), do: %__MODULE__{value: val} + + @impl true + def encode_xdr(%__MODULE__{value: value}) do + XDR.Bool.encode_xdr(%XDR.Bool{identifier: value}) + end + + @impl true + def encode_xdr!(%__MODULE__{value: value}) do + XDR.Bool.encode_xdr!(%XDR.Bool{identifier: value}) + end + + @impl true + def decode_xdr(bytes, term \\ nil) + + def decode_xdr(bytes, _term) do + case XDR.Bool.decode_xdr(bytes) do + {:ok, {%XDR.Bool{identifier: val}, rest}} -> {:ok, {new(val), rest}} + error -> error + end + end + + @impl true + def decode_xdr!(bytes, term \\ nil) + + def decode_xdr!(bytes, _term) do + {%XDR.Bool{identifier: val}, rest} = XDR.Bool.decode_xdr!(bytes) + {new(val), rest} + end +end diff --git a/lib/xdr/transactions/duration.ex b/lib/xdr/types/duration.ex similarity index 100% rename from lib/xdr/transactions/duration.ex rename to lib/xdr/types/duration.ex diff --git a/lib/xdr/transactions/time_point.ex b/lib/xdr/types/time_point.ex similarity index 100% rename from lib/xdr/transactions/time_point.ex rename to lib/xdr/types/time_point.ex diff --git a/mix.exs b/mix.exs index 04d77bcf..038afaea 100644 --- a/mix.exs +++ b/mix.exs @@ -2,7 +2,7 @@ defmodule StellarBase.MixProject do use Mix.Project @github_url "https://github.com/kommitters/stellar_base" - @version "0.9.1" + @version "0.10.0" def project do [ diff --git a/test/xdr/contract/optional_sc_map_test.exs b/test/xdr/contract/optional_sc_map_test.exs new file mode 100644 index 00000000..c0f9982b --- /dev/null +++ b/test/xdr/contract/optional_sc_map_test.exs @@ -0,0 +1,76 @@ +defmodule StellarBase.XDR.OptionalSCMapTest do + use ExUnit.Case + + alias StellarBase.XDR.{ + Int64, + OptionalSCMap, + SCMap, + SCMapEntry, + SCVal, + SCValType + } + + describe "OptionalSCMap" do + setup do + key1 = SCVal.new(Int64.new(1), SCValType.new(:SCV_I64)) + val1 = SCVal.new(Int64.new(2), SCValType.new(:SCV_I64)) + scmap_entry1 = SCMapEntry.new(key1, val1) + + key2 = SCVal.new(Int64.new(1), SCValType.new(:SCV_I64)) + val2 = SCVal.new(Int64.new(2), SCValType.new(:SCV_I64)) + scmap_entry2 = SCMapEntry.new(key2, val2) + + scmap_entries = [scmap_entry1, scmap_entry2] + + sc_map = SCMap.new(scmap_entries) + + %{ + optional_sc_map: OptionalSCMap.new(sc_map), + empty_sc_map: OptionalSCMap.new(nil), + binary: + <<0, 0, 0, 1, 0, 0, 0, 2, 0, 0, 0, 6, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 6, 0, 0, 0, 0, 0, + 0, 0, 2, 0, 0, 0, 6, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 6, 0, 0, 0, 0, 0, 0, 0, 2>> + } + end + + test "new/1", %{optional_sc_map: optional_sc_map} do + %OptionalSCMap{sc_map: ^optional_sc_map} = OptionalSCMap.new(optional_sc_map) + end + + test "new/1 no sc_map opted" do + %OptionalSCMap{sc_map: nil} = OptionalSCMap.new(nil) + end + + test "encode_xdr/1", %{optional_sc_map: optional_sc_map, binary: binary} do + {:ok, ^binary} = OptionalSCMap.encode_xdr(optional_sc_map) + end + + test "encode_xdr/1 no sc_map opted", %{empty_sc_map: empty_sc_map} do + {:ok, <<0, 0, 0, 0>>} = OptionalSCMap.encode_xdr(empty_sc_map) + end + + test "encode_xdr!/1", %{optional_sc_map: optional_sc_map, binary: binary} do + ^binary = OptionalSCMap.encode_xdr!(optional_sc_map) + end + + test "decode_xdr/2", %{optional_sc_map: optional_sc_map, binary: binary} do + {:ok, {^optional_sc_map, ""}} = OptionalSCMap.decode_xdr(binary) + end + + test "decode_xdr/2 with an invalid binary" do + {:error, :not_binary} = OptionalSCMap.decode_xdr(1234) + end + + test "decode_xdr/2 when sc_map is not opted" do + {:ok, {%OptionalSCMap{sc_map: nil}, ""}} = OptionalSCMap.decode_xdr(<<0, 0, 0, 0>>) + end + + test "decode_xdr!/2", %{optional_sc_map: optional_sc_map, binary: binary} do + {^optional_sc_map, ^binary} = OptionalSCMap.decode_xdr!(binary <> binary) + end + + test "decode_xdr!/2 when sc_map is not opted" do + {%OptionalSCMap{sc_map: nil}, ""} = OptionalSCMap.decode_xdr!(<<0, 0, 0, 0>>) + end + end +end diff --git a/test/xdr/contract/optional_sc_object_test.exs b/test/xdr/contract/optional_sc_object_test.exs deleted file mode 100644 index 2179e74a..00000000 --- a/test/xdr/contract/optional_sc_object_test.exs +++ /dev/null @@ -1,87 +0,0 @@ -defmodule StellarBase.XDR.OptionalSCObjectTest do - use ExUnit.Case - - alias StellarBase.XDR.{ - AccountID, - SCObject, - SCObjectType, - OptionalSCObject, - SCAddress, - SCAddressType, - PublicKey, - PublicKeyType, - UInt256 - } - - alias StellarBase.StrKey - - describe "OptionalSCObject" do - setup do - pk_type = PublicKeyType.new(:PUBLIC_KEY_TYPE_ED25519) - - account_id = - "GBZNLMUQMIN3VGUJISKZU7GNY3O3XLMYEHJCKCSMDHKLGSMKALRXOEZD" - |> StrKey.decode!(:ed25519_public_key) - |> UInt256.new() - |> PublicKey.new(pk_type) - |> AccountID.new() - - sc_address_type = SCAddressType.new(:SC_ADDRESS_TYPE_ACCOUNT) - - sc_address = SCAddress.new(account_id, sc_address_type) - - sc_object_type = SCObjectType.new(:SCO_ADDRESS) - - sc_object = SCObject.new(sc_address, sc_object_type) - - %{ - optional_sc_object: OptionalSCObject.new(sc_object), - empty_sc_object: OptionalSCObject.new(nil), - binary: - <<0, 0, 0, 1, 0, 0, 0, 8, 0, 0, 0, 0, 0, 0, 0, 0, 114, 213, 178, 144, 98, 27, 186, 154, - 137, 68, 149, 154, 124, 205, 198, 221, 187, 173, 152, 33, 210, 37, 10, 76, 25, 212, - 179, 73, 138, 2, 227, 119>> - } - end - - test "new/1", %{optional_sc_object: optional_sc_object} do - %OptionalSCObject{sc_object: ^optional_sc_object} = OptionalSCObject.new(optional_sc_object) - end - - test "new/1 no sc_object opted" do - %OptionalSCObject{sc_object: nil} = OptionalSCObject.new(nil) - end - - test "encode_xdr/1", %{optional_sc_object: optional_sc_object, binary: binary} do - {:ok, ^binary} = OptionalSCObject.encode_xdr(optional_sc_object) - end - - test "encode_xdr/1 no sc_object opted", %{empty_sc_object: empty_sc_object} do - {:ok, <<0, 0, 0, 0>>} = OptionalSCObject.encode_xdr(empty_sc_object) - end - - test "encode_xdr!/1", %{optional_sc_object: optional_sc_object, binary: binary} do - ^binary = OptionalSCObject.encode_xdr!(optional_sc_object) - end - - test "decode_xdr/2", %{optional_sc_object: optional_sc_object, binary: binary} do - {:ok, {^optional_sc_object, ""}} = OptionalSCObject.decode_xdr(binary) - end - - test "decode_xdr/2 with an invalid binary" do - {:error, :not_binary} = OptionalSCObject.decode_xdr(1234) - end - - test "decode_xdr/2 when sc_object is not opted" do - {:ok, {%OptionalSCObject{sc_object: nil}, ""}} = OptionalSCObject.decode_xdr(<<0, 0, 0, 0>>) - end - - test "decode_xdr!/2", %{optional_sc_object: optional_sc_object, binary: binary} do - {^optional_sc_object, ^binary} = OptionalSCObject.decode_xdr!(binary <> binary) - end - - test "decode_xdr!/2 when sc_object is not opted" do - {%OptionalSCObject{sc_object: nil}, ""} = OptionalSCObject.decode_xdr!(<<0, 0, 0, 0>>) - end - end -end diff --git a/test/xdr/contract/optional_sc_vec_test.exs b/test/xdr/contract/optional_sc_vec_test.exs new file mode 100644 index 00000000..d8329fd7 --- /dev/null +++ b/test/xdr/contract/optional_sc_vec_test.exs @@ -0,0 +1,68 @@ +defmodule StellarBase.XDR.OptionalSCVecTest do + use ExUnit.Case + + alias StellarBase.XDR.{ + Int64, + OptionalSCVec, + SCVec, + SCVal, + SCValType + } + + describe "OptionalSCVec" do + setup do + scval1 = SCVal.new(Int64.new(3), SCValType.new(:SCV_I64)) + scval2 = SCVal.new(Int64.new(2), SCValType.new(:SCV_I64)) + + sc_vec = SCVec.new([scval1, scval2]) + + %{ + optional_sc_vec: OptionalSCVec.new(sc_vec), + empty_sc_object: OptionalSCVec.new(nil), + binary: + <<0, 0, 0, 1, 0, 0, 0, 2, 0, 0, 0, 6, 0, 0, 0, 0, 0, 0, 0, 3, 0, 0, 0, 6, 0, 0, 0, 0, 0, + 0, 0, 2>> + } + end + + test "new/1", %{optional_sc_vec: optional_sc_vec} do + %OptionalSCVec{sc_vec: ^optional_sc_vec} = OptionalSCVec.new(optional_sc_vec) + end + + test "new/1 no sc_vec opted" do + %OptionalSCVec{sc_vec: nil} = OptionalSCVec.new(nil) + end + + test "encode_xdr/1", %{optional_sc_vec: optional_sc_vec, binary: binary} do + {:ok, ^binary} = OptionalSCVec.encode_xdr(optional_sc_vec) + end + + test "encode_xdr/1 no sc_vec opted", %{empty_sc_object: empty_sc_object} do + {:ok, <<0, 0, 0, 0>>} = OptionalSCVec.encode_xdr(empty_sc_object) + end + + test "encode_xdr!/1", %{optional_sc_vec: optional_sc_vec, binary: binary} do + ^binary = OptionalSCVec.encode_xdr!(optional_sc_vec) + end + + test "decode_xdr/2", %{optional_sc_vec: optional_sc_vec, binary: binary} do + {:ok, {^optional_sc_vec, ""}} = OptionalSCVec.decode_xdr(binary) + end + + test "decode_xdr/2 with an invalid binary" do + {:error, :not_binary} = OptionalSCVec.decode_xdr(1234) + end + + test "decode_xdr/2 when sc_vec is not opted" do + {:ok, {%OptionalSCVec{sc_vec: nil}, ""}} = OptionalSCVec.decode_xdr(<<0, 0, 0, 0>>) + end + + test "decode_xdr!/2", %{optional_sc_vec: optional_sc_vec, binary: binary} do + {^optional_sc_vec, ^binary} = OptionalSCVec.decode_xdr!(binary <> binary) + end + + test "decode_xdr!/2 when sc_vec is not opted" do + {%OptionalSCVec{sc_vec: nil}, ""} = OptionalSCVec.decode_xdr!(<<0, 0, 0, 0>>) + end + end +end diff --git a/test/xdr/contract/sc_bytes_test.exs b/test/xdr/contract/sc_bytes_test.exs new file mode 100644 index 00000000..fa10ff30 --- /dev/null +++ b/test/xdr/contract/sc_bytes_test.exs @@ -0,0 +1,51 @@ +defmodule StellarBase.XDR.SCBytesTest do + use ExUnit.Case + + alias StellarBase.XDR.SCBytes + + describe "SCBytes" do + setup do + %{ + sc_bytes: SCBytes.new("GCIZ3GSM5"), + binary: <<0, 0, 0, 9, 71, 67, 73, 90, 51, 71, 83, 77, 53, 0, 0, 0>> + } + end + + test "new/1" do + %SCBytes{value: sc_bytes} = + SCBytes.new(<<0, 0, 0, 9, 71, 67, 73, 90, 51, 71, 83, 77, 53, 0, 0, 0>>) + + 16 = String.length(sc_bytes) + end + + test "encode_xdr/1", %{sc_bytes: sc_bytes, binary: binary} do + {:ok, ^binary} = SCBytes.encode_xdr(sc_bytes) + end + + test "encode_xdr!/1", %{sc_bytes: sc_bytes, binary: binary} do + ^binary = SCBytes.encode_xdr!(sc_bytes) + end + + test "decode_xdr/2", %{sc_bytes: sc_bytes, binary: binary} do + {:ok, {^sc_bytes, ""}} = SCBytes.decode_xdr(binary) + end + + test "decode_xdr/2 with an invalid binary" do + {:error, :not_binary} = SCBytes.decode_xdr(123) + end + + test "decode_xdr!/2", %{sc_bytes: sc_bytes, binary: binary} do + {^sc_bytes, ""} = SCBytes.decode_xdr!(binary) + end + + test "invalid length" do + bits = 256_001 * 8 + binary = <<0::size(bits)>> + + {:error, :invalid_length} = + SCBytes.encode_xdr(%SCBytes{ + value: binary + }) + end + end +end diff --git a/test/xdr/contract/sc_contract_code_test.exs b/test/xdr/contract/sc_contract_code_test.exs deleted file mode 100644 index 4785fc30..00000000 --- a/test/xdr/contract/sc_contract_code_test.exs +++ /dev/null @@ -1,59 +0,0 @@ -defmodule StellarBase.XDR.SCContractCodeTest do - use ExUnit.Case - - alias StellarBase.XDR.{SCContractCode, SCContractCodeType, Hash} - - describe "SCContractCode" do - setup do - contract_code = Hash.new("GCIZ3GSM5XL7OUS4UP64THMDZ7CZ3ZWN") - - sc_contract_code_type = SCContractCodeType.new(:SCCONTRACT_CODE_WASM_REF) - - %{ - contract_code: contract_code, - sc_contract_code_type: sc_contract_code_type, - sc_contract_code: SCContractCode.new(contract_code, sc_contract_code_type), - binary: - <<0, 0, 0, 0, 71, 67, 73, 90, 51, 71, 83, 77, 53, 88, 76, 55, 79, 85, 83, 52, 85, 80, - 54, 52, 84, 72, 77, 68, 90, 55, 67, 90, 51, 90, 87, 78>> - } - end - - test "new/1", %{contract_code: contract_code, sc_contract_code_type: sc_contract_code_type} do - %SCContractCode{contract_code: ^contract_code, type: ^sc_contract_code_type} = - SCContractCode.new(contract_code, sc_contract_code_type) - end - - test "encode_xdr/1", %{sc_contract_code: sc_contract_code, binary: binary} do - {:ok, ^binary} = SCContractCode.encode_xdr(sc_contract_code) - end - - test "encode_xdr/1 with an invalid type", %{contract_code: contract_code} do - sc_contract_code_type = SCContractCodeType.new(:NEW_CONTRACT) - - assert_raise XDR.EnumError, - "The key which you try to encode doesn't belong to the current declarations", - fn -> - contract_code - |> SCContractCode.new(sc_contract_code_type) - |> SCContractCode.encode_xdr() - end - end - - test "encode_xdr!/1", %{sc_contract_code: sc_contract_code, binary: binary} do - ^binary = SCContractCode.encode_xdr!(sc_contract_code) - end - - test "decode_xdr/2", %{sc_contract_code: sc_contract_code, binary: binary} do - {:ok, {^sc_contract_code, ""}} = SCContractCode.decode_xdr(binary) - end - - test "decode_xdr/2 with an invalid binary" do - {:error, :not_binary} = SCContractCode.decode_xdr(123) - end - - test "decode_xdr!/2", %{sc_contract_code: sc_contract_code, binary: binary} do - {^sc_contract_code, ^binary} = SCContractCode.decode_xdr!(binary <> binary) - end - end -end diff --git a/test/xdr/contract/sc_contract_executable_test.exs b/test/xdr/contract/sc_contract_executable_test.exs new file mode 100644 index 00000000..bfd42c86 --- /dev/null +++ b/test/xdr/contract/sc_contract_executable_test.exs @@ -0,0 +1,79 @@ +defmodule StellarBase.XDR.SCContractExecutableTest do + use ExUnit.Case + + alias StellarBase.XDR.{SCContractExecutable, SCContractExecutableType, Hash} + + describe "SCContractExecutable" do + setup do + contract_executable = Hash.new("GCIZ3GSM5XL7OUS4UP64THMDZ7CZ3ZWN") + + sc_contract_executable_type = SCContractExecutableType.new(:SCCONTRACT_EXECUTABLE_WASM_REF) + + %{ + contract_executable: contract_executable, + sc_contract_executable_type: sc_contract_executable_type, + sc_contract_executable: + SCContractExecutable.new(contract_executable, sc_contract_executable_type), + binary: + <<0, 0, 0, 0, 71, 67, 73, 90, 51, 71, 83, 77, 53, 88, 76, 55, 79, 85, 83, 52, 85, 80, + 54, 52, 84, 72, 77, 68, 90, 55, 67, 90, 51, 90, 87, 78>> + } + end + + test "new/1", %{ + contract_executable: contract_executable, + sc_contract_executable_type: sc_contract_executable_type + } do + %SCContractExecutable{ + contract_executable: ^contract_executable, + type: ^sc_contract_executable_type + } = SCContractExecutable.new(contract_executable, sc_contract_executable_type) + end + + test "encode_xdr/1", %{ + sc_contract_executable: sc_contract_executable, + binary: binary + } do + {:ok, ^binary} = SCContractExecutable.encode_xdr(sc_contract_executable) + end + + test "encode_xdr/1 with an invalid type", %{ + contract_executable: contract_executable + } do + sc_contract_executable_type = SCContractExecutableType.new(:NEW_CONTRACT) + + assert_raise XDR.EnumError, + "The key which you try to encode doesn't belong to the current declarations", + fn -> + contract_executable + |> SCContractExecutable.new(sc_contract_executable_type) + |> SCContractExecutable.encode_xdr() + end + end + + test "encode_xdr!/1", %{ + sc_contract_executable: sc_contract_executable, + binary: binary + } do + ^binary = SCContractExecutable.encode_xdr!(sc_contract_executable) + end + + test "decode_xdr/2", %{ + sc_contract_executable: sc_contract_executable, + binary: binary + } do + {:ok, {^sc_contract_executable, ""}} = SCContractExecutable.decode_xdr(binary) + end + + test "decode_xdr/2 with an invalid binary" do + {:error, :not_binary} = SCContractExecutable.decode_xdr(123) + end + + test "decode_xdr!/2", %{ + sc_contract_executable: sc_contract_executable, + binary: binary + } do + {^sc_contract_executable, ^binary} = SCContractExecutable.decode_xdr!(binary <> binary) + end + end +end diff --git a/test/xdr/contract/sc_contract_code_type_test.exs b/test/xdr/contract/sc_contract_executable_type_test.exs similarity index 51% rename from test/xdr/contract/sc_contract_code_type_test.exs rename to test/xdr/contract/sc_contract_executable_type_test.exs index e2d89c61..3d427ef6 100644 --- a/test/xdr/contract/sc_contract_code_type_test.exs +++ b/test/xdr/contract/sc_contract_executable_type_test.exs @@ -1,11 +1,11 @@ -defmodule StellarBase.XDR.SCContractCodeTypeTest do +defmodule StellarBase.XDR.SCContractExecutableTypeTest do use ExUnit.Case - alias StellarBase.XDR.SCContractCodeType + alias StellarBase.XDR.SCContractExecutableType @codes [ - :SCCONTRACT_CODE_WASM_REF, - :SCCONTRACT_CODE_TOKEN + :SCCONTRACT_EXECUTABLE_WASM_REF, + :SCCONTRACT_EXECUTABLE_TOKEN ] @binaries [ @@ -13,52 +13,54 @@ defmodule StellarBase.XDR.SCContractCodeTypeTest do <<0, 0, 0, 1>> ] - describe "SCContractCodeType" do + describe "SCContractExecutableType" do setup do %{ codes: @codes, - results: Enum.map(@codes, &SCContractCodeType.new/1), + results: Enum.map(@codes, &SCContractExecutableType.new/1), binaries: @binaries } end test "new/1", %{codes: types} do for type <- types, - do: %SCContractCodeType{identifier: ^type} = SCContractCodeType.new(type) + do: %SCContractExecutableType{identifier: ^type} = SCContractExecutableType.new(type) end test "encode_xdr/1", %{results: results, binaries: binaries} do for {result, binary} <- Enum.zip(results, binaries), - do: {:ok, ^binary} = SCContractCodeType.encode_xdr(result) + do: {:ok, ^binary} = SCContractExecutableType.encode_xdr(result) end test "encode_xdr/1 with an invalid code" do {:error, :invalid_key} = - SCContractCodeType.encode_xdr(%SCContractCodeType{identifier: :TEST}) + SCContractExecutableType.encode_xdr(%SCContractExecutableType{identifier: :TEST}) end test "encode_xdr!/1", %{results: results, binaries: binaries} do for {result, binary} <- Enum.zip(results, binaries), - do: ^binary = SCContractCodeType.encode_xdr!(result) + do: ^binary = SCContractExecutableType.encode_xdr!(result) end test "decode_xdr/2", %{results: results, binaries: binaries} do for {result, binary} <- Enum.zip(results, binaries), - do: {:ok, {^result, ""}} = SCContractCodeType.decode_xdr(binary) + do: {:ok, {^result, ""}} = SCContractExecutableType.decode_xdr(binary) end test "decode_xdr/2 with an invalid declaration" do - {:error, :invalid_key} = SCContractCodeType.decode_xdr(<<1, 0, 0, 1>>) + {:error, :invalid_key} = SCContractExecutableType.decode_xdr(<<1, 0, 0, 1>>) end test "decode_xdr!/2", %{results: results, binaries: binaries} do for {result, binary} <- Enum.zip(results, binaries), - do: {^result, ^binary} = SCContractCodeType.decode_xdr!(binary <> binary) + do: {^result, ^binary} = SCContractExecutableType.decode_xdr!(binary <> binary) end test "decode_xdr!/2 with an error code", %{binaries: binaries} do for binary <- binaries, - do: {%SCContractCodeType{identifier: _}, ""} = SCContractCodeType.decode_xdr!(binary) + do: + {%SCContractExecutableType{identifier: _}, ""} = + SCContractExecutableType.decode_xdr!(binary) end end end diff --git a/test/xdr/contract/sc_map_entry_test.exs b/test/xdr/contract/sc_map_entry_test.exs index 9a38f0b8..3a5d9f68 100644 --- a/test/xdr/contract/sc_map_entry_test.exs +++ b/test/xdr/contract/sc_map_entry_test.exs @@ -5,14 +5,14 @@ defmodule StellarBase.XDR.SCMapEntryTest do describe "SCMapEntry" do setup do - key = SCVal.new(Int64.new(1), SCValType.new(:SCV_U63)) - val = SCVal.new(Int64.new(2), SCValType.new(:SCV_U63)) + key = SCVal.new(Int64.new(1), SCValType.new(:SCV_I64)) + val = SCVal.new(Int64.new(2), SCValType.new(:SCV_I64)) %{ key: key, val: val, scmap_entry: SCMapEntry.new(key, val), - binary: <<0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2>> + binary: <<0, 0, 0, 6, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 6, 0, 0, 0, 0, 0, 0, 0, 2>> } end diff --git a/test/xdr/contract/sc_map_test.exs b/test/xdr/contract/sc_map_test.exs index 476bdad7..35d2f9ad 100644 --- a/test/xdr/contract/sc_map_test.exs +++ b/test/xdr/contract/sc_map_test.exs @@ -11,12 +11,12 @@ defmodule StellarBase.XDR.SCMapTest do describe "SCMap" do setup do - key1 = SCVal.new(Int64.new(1), SCValType.new(:SCV_U63)) - val1 = SCVal.new(Int64.new(2), SCValType.new(:SCV_U63)) + key1 = SCVal.new(Int64.new(1), SCValType.new(:SCV_I64)) + val1 = SCVal.new(Int64.new(2), SCValType.new(:SCV_I64)) scmap_entry1 = SCMapEntry.new(key1, val1) - key2 = SCVal.new(Int64.new(1), SCValType.new(:SCV_U63)) - val2 = SCVal.new(Int64.new(2), SCValType.new(:SCV_U63)) + key2 = SCVal.new(Int64.new(1), SCValType.new(:SCV_I64)) + val2 = SCVal.new(Int64.new(2), SCValType.new(:SCV_I64)) scmap_entry2 = SCMapEntry.new(key2, val2) scmap_entries = [scmap_entry1, scmap_entry2] @@ -25,8 +25,8 @@ defmodule StellarBase.XDR.SCMapTest do scmap_entries: scmap_entries, scmap: SCMap.new(scmap_entries), binary: - <<0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2>> + <<0, 0, 0, 2, 0, 0, 0, 6, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 6, 0, 0, 0, 0, 0, 0, 0, 2, 0, + 0, 0, 6, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 6, 0, 0, 0, 0, 0, 0, 0, 2>> } end diff --git a/test/xdr/contract/sc_nonce_key_test.exs b/test/xdr/contract/sc_nonce_key_test.exs new file mode 100644 index 00000000..7da60652 --- /dev/null +++ b/test/xdr/contract/sc_nonce_key_test.exs @@ -0,0 +1,65 @@ +defmodule StellarBase.XDR.SCNonceKeyTest do + use ExUnit.Case + + alias StellarBase.StrKey + + alias StellarBase.XDR.{ + AccountID, + SCNonceKey, + PublicKeyType, + PublicKey, + SCAddressType, + SCAddress, + UInt256 + } + + describe "SCNonceKey" do + setup do + pk_type = PublicKeyType.new(:PUBLIC_KEY_TYPE_ED25519) + + account_id = + "GBZNLMUQMIN3VGUJISKZU7GNY3O3XLMYEHJCKCSMDHKLGSMKALRXOEZD" + |> StrKey.decode!(:ed25519_public_key) + |> UInt256.new() + |> PublicKey.new(pk_type) + |> AccountID.new() + + sc_address_type = SCAddressType.new(:SC_ADDRESS_TYPE_ACCOUNT) + + sc_address = SCAddress.new(account_id, sc_address_type) + + %{ + sc_address: sc_address, + sc_nonce_key: SCNonceKey.new(sc_address), + binary: + <<0, 0, 0, 0, 0, 0, 0, 0, 114, 213, 178, 144, 98, 27, 186, 154, 137, 68, 149, 154, 124, + 205, 198, 221, 187, 173, 152, 33, 210, 37, 10, 76, 25, 212, 179, 73, 138, 2, 227, + 119>> + } + end + + test "new/1", %{sc_address: sc_address} do + %SCNonceKey{nonce_address: ^sc_address} = SCNonceKey.new(sc_address) + end + + test "encode_xdr/1", %{sc_nonce_key: sc_nonce_key, binary: binary} do + {:ok, ^binary} = SCNonceKey.encode_xdr(sc_nonce_key) + end + + test "encode_xdr!/1", %{sc_nonce_key: sc_nonce_key, binary: binary} do + ^binary = SCNonceKey.encode_xdr!(sc_nonce_key) + end + + test "decode_xdr/2", %{sc_nonce_key: sc_nonce_key, binary: binary} do + {:ok, {^sc_nonce_key, ""}} = SCNonceKey.decode_xdr(binary) + end + + test "decode_xdr/2 with an invalid binary" do + {:error, :not_binary} = SCNonceKey.decode_xdr(123) + end + + test "decode_xdr!/2", %{sc_nonce_key: sc_nonce_key, binary: binary} do + {^sc_nonce_key, ^binary} = SCNonceKey.decode_xdr!(binary <> binary) + end + end +end diff --git a/test/xdr/contract/sc_object_test.exs b/test/xdr/contract/sc_object_test.exs deleted file mode 100644 index 9ff3ed46..00000000 --- a/test/xdr/contract/sc_object_test.exs +++ /dev/null @@ -1,178 +0,0 @@ -defmodule StellarBase.XDR.SCObjectTest do - use ExUnit.Case - - alias StellarBase.XDR.{SCAddress, SCAddressType, SCObject, SCObjectType} - - alias StellarBase.XDR.{ - AccountID, - PublicKey, - PublicKeyType, - Int64, - UInt256, - SCValType, - SCVal, - SCVec, - SCMap, - SCMapEntry, - SCContractCodeType, - SCContractCode, - Hash, - UInt64, - Int128Parts, - VariableOpaque256000 - } - - alias StellarBase.StrKey - - describe "SCObject" do - setup do - ## SCVec structs - sc_val_type = SCValType.new(:SCV_U63) - int64 = Int64.new(2) - sc_val = SCVal.new(int64, sc_val_type) - - ## SCMap structs - key = SCVal.new(Int64.new(1), SCValType.new(:SCV_U63)) - val = SCVal.new(Int64.new(2), SCValType.new(:SCV_U63)) - scmap_entry = SCMapEntry.new(key, val) - - ## Int128Parts structs - lo = UInt64.new(3312) - hi = UInt64.new(3313) - - ## SCAddresss structs - pk_type = PublicKeyType.new(:PUBLIC_KEY_TYPE_ED25519) - - account_id = - "GBZNLMUQMIN3VGUJISKZU7GNY3O3XLMYEHJCKCSMDHKLGSMKALRXOEZD" - |> StrKey.decode!(:ed25519_public_key) - |> UInt256.new() - |> PublicKey.new(pk_type) - |> AccountID.new() - - sc_address_type = SCAddressType.new(:SC_ADDRESS_TYPE_ACCOUNT) - - ## SCContractCode structs - contract_code = Hash.new("GCIZ3GSM5XL7OUS4UP64THMDZ7CZ3ZWN") - sc_contract_code_type = SCContractCodeType.new(:SCCONTRACT_CODE_WASM_REF) - - discriminants = [ - %{ - sc_object_type: SCObjectType.new(:SCO_VEC), - sc_object: SCVec.new([sc_val]), - binary: <<0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2>> - }, - %{ - sc_object_type: SCObjectType.new(:SCO_MAP), - sc_object: SCMap.new([scmap_entry]), - binary: - <<0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 2>> - }, - %{ - sc_object_type: SCObjectType.new(:SCO_U64), - sc_object: UInt64.new(18_446_744_073_709_551_615), - binary: <<0, 0, 0, 2, 255, 255, 255, 255, 255, 255, 255, 255>> - }, - %{ - sc_object_type: SCObjectType.new(:SCO_I64), - sc_object: Int64.new(2), - binary: <<0, 0, 0, 3, 0, 0, 0, 0, 0, 0, 0, 2>> - }, - %{ - sc_object_type: SCObjectType.new(:SCO_U128), - sc_object: Int128Parts.new(lo, hi), - binary: <<0, 0, 0, 4, 0, 0, 0, 0, 0, 0, 12, 240, 0, 0, 0, 0, 0, 0, 12, 241>> - }, - %{ - sc_object_type: SCObjectType.new(:SCO_I128), - sc_object: Int128Parts.new(lo, hi), - binary: <<0, 0, 0, 5, 0, 0, 0, 0, 0, 0, 12, 240, 0, 0, 0, 0, 0, 0, 12, 241>> - }, - %{ - sc_object_type: SCObjectType.new(:SCO_BYTES), - sc_object: - VariableOpaque256000.new(<<0, 0, 0, 9, 71, 67, 73, 90, 51, 71, 83, 77, 53, 0, 0, 0>>), - binary: - <<0, 0, 0, 6, 0, 0, 0, 16, 0, 0, 0, 9, 71, 67, 73, 90, 51, 71, 83, 77, 53, 0, 0, 0>> - }, - %{ - sc_object_type: SCObjectType.new(:SCO_CONTRACT_CODE), - sc_object: SCContractCode.new(contract_code, sc_contract_code_type), - binary: - <<0, 0, 0, 7, 0, 0, 0, 0, 71, 67, 73, 90, 51, 71, 83, 77, 53, 88, 76, 55, 79, 85, 83, - 52, 85, 80, 54, 52, 84, 72, 77, 68, 90, 55, 67, 90, 51, 90, 87, 78>> - }, - %{ - sc_object_type: SCObjectType.new(:SCO_ADDRESS), - sc_object: SCAddress.new(account_id, sc_address_type), - binary: - <<0, 0, 0, 8, 0, 0, 0, 0, 0, 0, 0, 0, 114, 213, 178, 144, 98, 27, 186, 154, 137, 68, - 149, 154, 124, 205, 198, 221, 187, 173, 152, 33, 210, 37, 10, 76, 25, 212, 179, 73, - 138, 2, 227, 119>> - }, - %{ - sc_object_type: SCObjectType.new(:SCO_NONCE_KEY), - sc_object: SCAddress.new(account_id, sc_address_type), - binary: - <<0, 0, 0, 9, 0, 0, 0, 0, 0, 0, 0, 0, 114, 213, 178, 144, 98, 27, 186, 154, 137, 68, - 149, 154, 124, 205, 198, 221, 187, 173, 152, 33, 210, 37, 10, 76, 25, 212, 179, 73, - 138, 2, 227, 119>> - } - ] - - %{discriminants: discriminants} - end - - test "new/1", %{discriminants: discriminants} do - for %{sc_object_type: sc_object_type, sc_object: sc_object} <- discriminants do - %SCObject{sc_object: ^sc_object, type: ^sc_object_type} = - SCObject.new(sc_object, sc_object_type) - end - end - - test "encode_xdr/1", %{discriminants: discriminants} do - for %{sc_object: sc_object, sc_object_type: sc_object_type, binary: binary} <- discriminants do - xdr = SCObject.new(sc_object, sc_object_type) - {:ok, ^binary} = SCObject.encode_xdr(xdr) - end - end - - test "encode_xdr!/1", %{discriminants: discriminants} do - for %{sc_object: sc_object, sc_object_type: sc_object_type, binary: binary} <- discriminants do - xdr = SCObject.new(sc_object, sc_object_type) - ^binary = SCObject.encode_xdr!(xdr) - end - end - - test "encode_xdr/1 with an invalid type", %{discriminants: [sc_object | _rest]} do - sc_object_type = SCObjectType.new(:NEW_ADDRESS) - - assert_raise XDR.EnumError, - "The key which you try to encode doesn't belong to the current declarations", - fn -> - sc_object - |> SCObject.new(sc_object_type) - |> SCObject.encode_xdr() - end - end - - test "decode_xdr/2", %{discriminants: discriminants} do - for %{sc_object: sc_object, sc_object_type: sc_object_type, binary: binary} <- discriminants do - xdr = SCObject.new(sc_object, sc_object_type) - {:ok, {^xdr, ""}} = SCObject.decode_xdr(binary) - end - end - - test "decode_xdr/2 with an invalid binary" do - {:error, :not_binary} = SCObject.decode_xdr(123) - end - - test "decode_xdr!/2", %{discriminants: discriminants} do - for %{sc_object: sc_object, sc_object_type: sc_object_type, binary: binary} <- discriminants do - xdr = SCObject.new(sc_object, sc_object_type) - {^xdr, ""} = SCObject.decode_xdr!(binary) - end - end - end -end diff --git a/test/xdr/contract/sc_object_type_test.exs b/test/xdr/contract/sc_object_type_test.exs deleted file mode 100644 index aff3098d..00000000 --- a/test/xdr/contract/sc_object_type_test.exs +++ /dev/null @@ -1,79 +0,0 @@ -defmodule StellarBase.XDR.SCObjectTypeTest do - use ExUnit.Case - - alias StellarBase.XDR.SCObjectType - - @codes [ - :SCO_VEC, - :SCO_MAP, - :SCO_U64, - :SCO_I64, - :SCO_U128, - :SCO_I128, - :SCO_BYTES, - :SCO_CONTRACT_CODE, - :SCO_ADDRESS, - :SCO_NONCE_KEY - ] - - @binaries [ - <<0, 0, 0, 0>>, - <<0, 0, 0, 1>>, - <<0, 0, 0, 2>>, - <<0, 0, 0, 3>>, - <<0, 0, 0, 4>>, - <<0, 0, 0, 5>>, - <<0, 0, 0, 6>>, - <<0, 0, 0, 7>>, - <<0, 0, 0, 8>>, - <<0, 0, 0, 9>> - ] - - describe "SCObjectType" do - setup do - %{ - codes: @codes, - results: Enum.map(@codes, &SCObjectType.new/1), - binaries: @binaries - } - end - - test "new/1", %{codes: types} do - for type <- types, - do: %SCObjectType{identifier: ^type} = SCObjectType.new(type) - end - - test "encode_xdr/1", %{results: results, binaries: binaries} do - for {result, binary} <- Enum.zip(results, binaries), - do: {:ok, ^binary} = SCObjectType.encode_xdr(result) - end - - test "encode_xdr/1 with an invalid code" do - {:error, :invalid_key} = SCObjectType.encode_xdr(%SCObjectType{identifier: :TEST}) - end - - test "encode_xdr!/1", %{results: results, binaries: binaries} do - for {result, binary} <- Enum.zip(results, binaries), - do: ^binary = SCObjectType.encode_xdr!(result) - end - - test "decode_xdr/2", %{results: results, binaries: binaries} do - for {result, binary} <- Enum.zip(results, binaries), - do: {:ok, {^result, ""}} = SCObjectType.decode_xdr(binary) - end - - test "decode_xdr/2 with an invalid declaration" do - {:error, :invalid_key} = SCObjectType.decode_xdr(<<1, 0, 0, 1>>) - end - - test "decode_xdr!/2", %{results: results, binaries: binaries} do - for {result, binary} <- Enum.zip(results, binaries), - do: {^result, ^binary} = SCObjectType.decode_xdr!(binary <> binary) - end - - test "decode_xdr!/2 with an error code", %{binaries: binaries} do - for binary <- binaries, - do: {%SCObjectType{identifier: _}, ""} = SCObjectType.decode_xdr!(binary) - end - end -end diff --git a/test/xdr/contract/sc_string_test.exs b/test/xdr/contract/sc_string_test.exs new file mode 100644 index 00000000..73edb3d4 --- /dev/null +++ b/test/xdr/contract/sc_string_test.exs @@ -0,0 +1,76 @@ +defmodule StellarBase.XDR.SCStringTest do + use ExUnit.Case + + alias StellarBase.XDR.SCString + + describe "SCString" do + setup do + %{ + sc_symbol: SCString.new("Hello"), + binary: <<0, 0, 0, 5, 72, 101, 108, 108, 111, 0, 0, 0>> + } + end + + test "new/1" do + %SCString{value: "Hello"} = SCString.new("Hello") + end + + test "encode_xdr/1", %{sc_symbol: sc_symbol, binary: binary} do + {:ok, ^binary} = SCString.encode_xdr(sc_symbol) + end + + test "encode_xdr/1 a non-string value" do + {:error, :not_bitstring} = + 123 + |> SCString.new() + |> SCString.encode_xdr() + end + + test "encode_xdr/1 an invalid string lenght" do + bits = 256_001 * 8 + large_string = <<64::size(bits)>> + + {:error, :invalid_length} = + large_string + |> SCString.new() + |> SCString.encode_xdr() + end + + test "encode_xdr!/1", %{sc_symbol: sc_symbol, binary: binary} do + ^binary = SCString.encode_xdr!(sc_symbol) + end + + test "encode_xdr!/1 a string longer than 256000-bytes" do + bits = 256_001 * 8 + large_string = <<64::size(bits)>> + + assert_raise XDR.StringError, + "The length of the string exceeds the max length allowed", + fn -> + large_string + |> SCString.new() + |> SCString.encode_xdr!() + end + end + + test "decode_xdr/2", %{sc_symbol: sc_symbol, binary: binary} do + {:ok, {^sc_symbol, ""}} = SCString.decode_xdr(binary) + end + + test "decode_xdr/2 with an invalid binary" do + {:error, :not_binary} = SCString.decode_xdr(123) + end + + test "decode_xdr/2 an invalid binary" do + assert_raise XDR.VariableOpaqueError, + "The XDR has an invalid length, it must be less than byte-size of the rest", + fn -> + SCString.decode_xdr(<<0, 0, 4, 210>>) + end + end + + test "decode_xdr!/2", %{sc_symbol: sc_symbol, binary: binary} do + {^sc_symbol, ^binary} = SCString.decode_xdr!(binary <> binary) + end + end +end diff --git a/test/xdr/contract/sc_symbol_test.exs b/test/xdr/contract/sc_symbol_test.exs index 7ea92a0b..aabb3b4a 100644 --- a/test/xdr/contract/sc_symbol_test.exs +++ b/test/xdr/contract/sc_symbol_test.exs @@ -28,7 +28,7 @@ defmodule StellarBase.XDR.SCSymbolTest do test "encode_xdr/1 an invalid string lenght" do {:error, :invalid_length} = - "Hello There" + "The length of the string exceeds the max length allowed" |> SCSymbol.new() |> SCSymbol.encode_xdr() end diff --git a/test/xdr/contract/sc_val_test.exs b/test/xdr/contract/sc_val_test.exs index 3cf7a089..99b1878f 100644 --- a/test/xdr/contract/sc_val_test.exs +++ b/test/xdr/contract/sc_val_test.exs @@ -1,68 +1,197 @@ defmodule StellarBase.XDR.SCValTest do use ExUnit.Case + alias StellarBase.StrKey + alias StellarBase.XDR.{ - Int64, + AccountID, + Bool, + Duration, + Hash, Int32, - OptionalSCObject, - UInt32, - UInt64, + Int64, + Int128Parts, + SCAddress, + SCAddressType, + OptionalSCMap, + OptionalSCVec, + PublicKeyType, + PublicKey, + SCBytes, + SCNonceKey, + SCContractExecutable, + SCContractExecutableType, + SCVal, SCValType, - SCStatic, SCStatus, SCStatusType, - SCUnknownErrorCode, + SCString, SCSymbol, - SCVal, - SCValType + SCUnknownErrorCode, + TimePoint, + UInt256, + UInt32, + UInt64, + Void } describe "SCVal" do setup do + # SCAddress + pk_type = PublicKeyType.new(:PUBLIC_KEY_TYPE_ED25519) + + account_id = + "GBZNLMUQMIN3VGUJISKZU7GNY3O3XLMYEHJCKCSMDHKLGSMKALRXOEZD" + |> StrKey.decode!(:ed25519_public_key) + |> UInt256.new() + |> PublicKey.new(pk_type) + |> AccountID.new() + + sc_address_type = SCAddressType.new(:SC_ADDRESS_TYPE_ACCOUNT) + sc_address = SCAddress.new(account_id, sc_address_type) + + # SCContractExecutable + sc_contract_executable_type = SCContractExecutableType.new(:SCCONTRACT_EXECUTABLE_WASM_REF) + + sc_contract_executable = + "GCIZ3GSM5XL7OUS4UP64THMDZ7CZ3ZWN" + |> Hash.new() + |> SCContractExecutable.new(sc_contract_executable_type) + discriminants = [ %{ - val_type: SCValType.new(:SCV_U63), - value: Int64.new(2), - binary: <<0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2>> + val_type: SCValType.new(:SCV_BOOL), + value: Bool.new(true), + binary: <<0, 0, 0, 0, 0, 0, 0, 1>> + }, + %{ + val_type: SCValType.new(:SCV_VOID), + value: Void.new(), + binary: <<0, 0, 0, 1>> + }, + %{ + val_type: SCValType.new(:SCV_STATUS), + value: + SCStatus.new( + SCUnknownErrorCode.new(:UNKNOWN_ERROR_GENERAL), + SCStatusType.new(:SST_UNKNOWN_ERROR) + ), + binary: <<0, 0, 0, 2, 0, 0, 0, 1, 0, 0, 0, 0>> }, %{ val_type: SCValType.new(:SCV_U32), value: UInt32.new(2), - binary: <<0, 0, 0, 1, 0, 0, 0, 2>> + binary: <<0, 0, 0, 3, 0, 0, 0, 2>> }, %{ val_type: SCValType.new(:SCV_I32), - value: Int32.new(3), - binary: <<0, 0, 0, 2, 0, 0, 0, 3>> + value: Int32.new(2), + binary: <<0, 0, 0, 4, 0, 0, 0, 2>> }, %{ - val_type: SCValType.new(:SCV_STATIC), - value: SCStatic.new(:SCS_TRUE), - binary: <<0, 0, 0, 3, 0, 0, 0, 1>> + val_type: SCValType.new(:SCV_U64), + value: UInt64.new(4), + binary: <<0, 0, 0, 5, 0, 0, 0, 0, 0, 0, 0, 4>> }, %{ - val_type: SCValType.new(:SCV_OBJECT), - value: OptionalSCObject.new(nil), - binary: <<0, 0, 0, 4, 0, 0, 0, 0>> + val_type: SCValType.new(:SCV_I64), + value: Int64.new(4), + binary: <<0, 0, 0, 6, 0, 0, 0, 0, 0, 0, 0, 4>> }, %{ - val_type: SCValType.new(:SCV_SYMBOL), - value: SCSymbol.new("World"), - binary: <<0, 0, 0, 5, 0, 0, 0, 5, 87, 111, 114, 108, 100, 0, 0, 0>> + val_type: SCValType.new(:SCV_TIMEPOINT), + value: TimePoint.new(1234), + binary: <<0, 0, 0, 7, 0, 0, 0, 0, 0, 0, 4, 210>> }, %{ - val_type: SCValType.new(:SCV_BITSET), - value: UInt64.new(4), - binary: <<0, 0, 0, 6, 0, 0, 0, 0, 0, 0, 0, 4>> + val_type: SCValType.new(:SCV_DURATION), + value: Duration.new(1234), + binary: <<0, 0, 0, 8, 0, 0, 0, 0, 0, 0, 4, 210>> }, %{ - val_type: SCValType.new(:SCV_STATUS), + val_type: SCValType.new(:SCV_U128), + value: Int128Parts.new(UInt64.new(3312), UInt64.new(3313)), + binary: <<0, 0, 0, 9, 0, 0, 0, 0, 0, 0, 12, 240, 0, 0, 0, 0, 0, 0, 12, 241>> + }, + %{ + val_type: SCValType.new(:SCV_I128), + value: Int128Parts.new(UInt64.new(3312), UInt64.new(3313)), + binary: <<0, 0, 0, 10, 0, 0, 0, 0, 0, 0, 12, 240, 0, 0, 0, 0, 0, 0, 12, 241>> + }, + %{ + val_type: SCValType.new(:SCV_U256), value: - SCStatus.new( - SCUnknownErrorCode.new(:UNKNOWN_ERROR_GENERAL), - SCStatusType.new(:SST_UNKNOWN_ERROR) + UInt256.new( + <<72, 101, 108, 108, 111, 32, 119, 111, 114, 108, 100, 0, 21, 0, 1, 0, 72, 101, 108, + 108, 111, 32, 119, 111, 114, 108, 100, 0, 21, 0, 1, 0>> ), - binary: <<0, 0, 0, 7, 0, 0, 0, 1, 0, 0, 0, 0>> + binary: + <<0, 0, 0, 11, 72, 101, 108, 108, 111, 32, 119, 111, 114, 108, 100, 0, 21, 0, 1, 0, + 72, 101, 108, 108, 111, 32, 119, 111, 114, 108, 100, 0, 21, 0, 1, 0>> + }, + %{ + val_type: SCValType.new(:SCV_I256), + value: + UInt256.new( + <<72, 101, 108, 108, 111, 32, 119, 111, 114, 108, 100, 0, 21, 0, 1, 0, 72, 101, 108, + 108, 111, 32, 119, 111, 114, 108, 100, 0, 21, 0, 1, 0>> + ), + binary: + <<0, 0, 0, 12, 72, 101, 108, 108, 111, 32, 119, 111, 114, 108, 100, 0, 21, 0, 1, 0, + 72, 101, 108, 108, 111, 32, 119, 111, 114, 108, 100, 0, 21, 0, 1, 0>> + }, + %{ + val_type: SCValType.new(:SCV_BYTES), + value: SCBytes.new("GCIZ3GSM5"), + binary: <<0, 0, 0, 13, 0, 0, 0, 9, 71, 67, 73, 90, 51, 71, 83, 77, 53, 0, 0, 0>> + }, + %{ + val_type: SCValType.new(:SCV_STRING), + value: SCString.new("Hello"), + binary: <<0, 0, 0, 14, 0, 0, 0, 5, 72, 101, 108, 108, 111, 0, 0, 0>> + }, + %{ + val_type: SCValType.new(:SCV_SYMBOL), + value: SCSymbol.new("World"), + binary: <<0, 0, 0, 15, 0, 0, 0, 5, 87, 111, 114, 108, 100, 0, 0, 0>> + }, + %{ + val_type: SCValType.new(:SCV_VEC), + value: OptionalSCVec.new(nil), + binary: <<0, 0, 0, 16, 0, 0, 0, 0>> + }, + %{ + val_type: SCValType.new(:SCV_MAP), + value: OptionalSCMap.new(nil), + binary: <<0, 0, 0, 17, 0, 0, 0, 0>> + }, + %{ + val_type: SCValType.new(:SCV_CONTRACT_EXECUTABLE), + value: sc_contract_executable, + binary: + <<0, 0, 0, 18, 0, 0, 0, 0, 71, 67, 73, 90, 51, 71, 83, 77, 53, 88, 76, 55, 79, 85, 83, + 52, 85, 80, 54, 52, 84, 72, 77, 68, 90, 55, 67, 90, 51, 90, 87, 78>> + }, + %{ + val_type: SCValType.new(:SCV_ADDRESS), + value: sc_address, + binary: + <<0, 0, 0, 19, 0, 0, 0, 0, 0, 0, 0, 0, 114, 213, 178, 144, 98, 27, 186, 154, 137, 68, + 149, 154, 124, 205, 198, 221, 187, 173, 152, 33, 210, 37, 10, 76, 25, 212, 179, 73, + 138, 2, 227, 119>> + }, + %{ + val_type: SCValType.new(:SCV_LEDGER_KEY_CONTRACT_EXECUTABLE), + value: Void.new(), + binary: <<0, 0, 0, 20>> + }, + %{ + val_type: SCValType.new(:SCV_LEDGER_KEY_NONCE), + value: SCNonceKey.new(sc_address), + binary: + <<0, 0, 0, 21, 0, 0, 0, 0, 0, 0, 0, 0, 114, 213, 178, 144, 98, 27, 186, 154, 137, 68, + 149, 154, 124, 205, 198, 221, 187, 173, 152, 33, 210, 37, 10, 76, 25, 212, 179, 73, + 138, 2, 227, 119>> } ] diff --git a/test/xdr/contract/sc_val_type_test.exs b/test/xdr/contract/sc_val_type_test.exs index 99f70d8f..e14ab76b 100644 --- a/test/xdr/contract/sc_val_type_test.exs +++ b/test/xdr/contract/sc_val_type_test.exs @@ -7,9 +7,9 @@ defmodule StellarBase.XDR.SCValTypeTest do setup do %{ binary: <<0, 0, 0, 1>>, - identifier: :SCV_U32, - default_identifier: :SCV_U63, - xdr_type: SCValType.new(:SCV_U32) + identifier: :SCV_VOID, + default_identifier: :SCV_BOOL, + xdr_type: SCValType.new(:SCV_VOID) } end diff --git a/test/xdr/contract/sc_vec_test.exs b/test/xdr/contract/sc_vec_test.exs index e47da282..e5cbb4bf 100644 --- a/test/xdr/contract/sc_vec_test.exs +++ b/test/xdr/contract/sc_vec_test.exs @@ -10,8 +10,8 @@ defmodule StellarBase.XDR.SCVecTest do describe "SCVec" do setup do - scval1 = SCVal.new(Int64.new(3), SCValType.new(:SCV_U63)) - scval2 = SCVal.new(Int64.new(2), SCValType.new(:SCV_U63)) + scval1 = SCVal.new(Int64.new(3), SCValType.new(:SCV_I64)) + scval2 = SCVal.new(Int64.new(2), SCValType.new(:SCV_I64)) sc_vals = [scval1, scval2] @@ -19,7 +19,7 @@ defmodule StellarBase.XDR.SCVecTest do sc_vals: sc_vals, scvec: SCVec.new(sc_vals), binary: - <<0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2>> + <<0, 0, 0, 2, 0, 0, 0, 6, 0, 0, 0, 0, 0, 0, 0, 3, 0, 0, 0, 6, 0, 0, 0, 0, 0, 0, 0, 2>> } end diff --git a/test/xdr/contract/spec/sc_spec_type_def_list12_test.exs b/test/xdr/contract/spec/sc_spec_type_def_list12_test.exs index adb2a781..c67b5a44 100644 --- a/test/xdr/contract/spec/sc_spec_type_def_list12_test.exs +++ b/test/xdr/contract/spec/sc_spec_type_def_list12_test.exs @@ -7,7 +7,7 @@ defmodule StellarBase.XDR.SCSpecTypeDefList12Test do setup do code = Void.new() type_val = SCSpecType.new(:SC_SPEC_TYPE_VAL) - type_u128 = SCSpecType.new(:SC_SPEC_TYPE_U128) + type_u128 = SCSpecType.new(:SC_SPEC_TYPE_I32) sc_spec_type_def1 = SCSpecTypeDef.new(code, type_val) sc_spec_type_def2 = SCSpecTypeDef.new(code, type_u128) diff --git a/test/xdr/contract/spec/sc_spec_type_def_test.exs b/test/xdr/contract/spec/sc_spec_type_def_test.exs index de0f7ff3..babf6835 100644 --- a/test/xdr/contract/spec/sc_spec_type_def_test.exs +++ b/test/xdr/contract/spec/sc_spec_type_def_test.exs @@ -22,7 +22,7 @@ defmodule StellarBase.XDR.SCSpecTypeDefTest do setup do code = Void.new() type_val = SCSpecType.new(:SC_SPEC_TYPE_VAL) - type_u128 = SCSpecType.new(:SC_SPEC_TYPE_U128) + type_u128 = SCSpecType.new(:SC_SPEC_TYPE_I32) sc_spec_type_def_1 = SCSpecTypeDef.new(code, type_val) sc_spec_type_def_2 = SCSpecTypeDef.new(code, type_u128) @@ -33,65 +33,90 @@ defmodule StellarBase.XDR.SCSpecTypeDefTest do binary: <<0, 0, 0, 0>> }, %{ - status_type: SCSpecType.new(:SC_SPEC_TYPE_U32), + status_type: SCSpecType.new(:SC_SPEC_TYPE_BOOL), sc_code: Void.new(), binary: <<0, 0, 0, 1>> }, %{ - status_type: SCSpecType.new(:SC_SPEC_TYPE_I32), + status_type: SCSpecType.new(:SC_SPEC_TYPE_VOID), sc_code: Void.new(), binary: <<0, 0, 0, 2>> }, %{ - status_type: SCSpecType.new(:SC_SPEC_TYPE_U64), + status_type: SCSpecType.new(:SC_SPEC_TYPE_STATUS), sc_code: Void.new(), binary: <<0, 0, 0, 3>> }, %{ - status_type: SCSpecType.new(:SC_SPEC_TYPE_I64), + status_type: SCSpecType.new(:SC_SPEC_TYPE_U32), sc_code: Void.new(), binary: <<0, 0, 0, 4>> }, %{ - status_type: SCSpecType.new(:SC_SPEC_TYPE_U128), + status_type: SCSpecType.new(:SC_SPEC_TYPE_I32), sc_code: Void.new(), binary: <<0, 0, 0, 5>> }, %{ - status_type: SCSpecType.new(:SC_SPEC_TYPE_I128), + status_type: SCSpecType.new(:SC_SPEC_TYPE_U64), sc_code: Void.new(), binary: <<0, 0, 0, 6>> }, %{ - status_type: SCSpecType.new(:SC_SPEC_TYPE_BOOL), + status_type: SCSpecType.new(:SC_SPEC_TYPE_I64), sc_code: Void.new(), binary: <<0, 0, 0, 7>> }, %{ - status_type: SCSpecType.new(:SC_SPEC_TYPE_SYMBOL), + status_type: SCSpecType.new(:SC_SPEC_TYPE_TIMEPOINT), sc_code: Void.new(), binary: <<0, 0, 0, 8>> }, %{ - status_type: SCSpecType.new(:SC_SPEC_TYPE_BITSET), + status_type: SCSpecType.new(:SC_SPEC_TYPE_DURATION), sc_code: Void.new(), binary: <<0, 0, 0, 9>> }, %{ - status_type: SCSpecType.new(:SC_SPEC_TYPE_STATUS), + status_type: SCSpecType.new(:SC_SPEC_TYPE_U128), sc_code: Void.new(), binary: <<0, 0, 0, 10>> }, %{ - status_type: SCSpecType.new(:SC_SPEC_TYPE_BYTES), + status_type: SCSpecType.new(:SC_SPEC_TYPE_I128), sc_code: Void.new(), binary: <<0, 0, 0, 11>> }, %{ - status_type: SCSpecType.new(:SC_SPEC_TYPE_ADDRESS), + status_type: SCSpecType.new(:SC_SPEC_TYPE_U256), + sc_code: Void.new(), + binary: <<0, 0, 0, 12>> + }, + %{ + status_type: SCSpecType.new(:SC_SPEC_TYPE_I256), sc_code: Void.new(), binary: <<0, 0, 0, 13>> }, + %{ + status_type: SCSpecType.new(:SC_SPEC_TYPE_BYTES), + sc_code: Void.new(), + binary: <<0, 0, 0, 14>> + }, + %{ + status_type: SCSpecType.new(:SC_SPEC_TYPE_STRING), + sc_code: Void.new(), + binary: <<0, 0, 0, 16>> + }, + %{ + status_type: SCSpecType.new(:SC_SPEC_TYPE_SYMBOL), + sc_code: Void.new(), + binary: <<0, 0, 0, 17>> + }, + %{ + status_type: SCSpecType.new(:SC_SPEC_TYPE_ADDRESS), + sc_code: Void.new(), + binary: <<0, 0, 0, 19>> + }, %{ sc_code: sc_spec_type_def_1 diff --git a/test/xdr/contract/spec/sc_spec_type_test.exs b/test/xdr/contract/spec/sc_spec_type_test.exs index 2f2cd9aa..4b8799ea 100644 --- a/test/xdr/contract/spec/sc_spec_type_test.exs +++ b/test/xdr/contract/spec/sc_spec_type_test.exs @@ -5,18 +5,22 @@ defmodule StellarBase.XDR.SCSpecTypeTest do @types [ :SC_SPEC_TYPE_VAL, + :SC_SPEC_TYPE_BOOL, + :SC_SPEC_TYPE_VOID, + :SC_SPEC_TYPE_STATUS, :SC_SPEC_TYPE_U32, :SC_SPEC_TYPE_I32, :SC_SPEC_TYPE_U64, :SC_SPEC_TYPE_I64, + :SC_SPEC_TYPE_TIMEPOINT, + :SC_SPEC_TYPE_DURATION, :SC_SPEC_TYPE_U128, :SC_SPEC_TYPE_I128, - :SC_SPEC_TYPE_BOOL, - :SC_SPEC_TYPE_SYMBOL, - :SC_SPEC_TYPE_BITSET, - :SC_SPEC_TYPE_STATUS, + :SC_SPEC_TYPE_U256, + :SC_SPEC_TYPE_I256, :SC_SPEC_TYPE_BYTES, - :SC_SPEC_TYPE_INVOKER, + :SC_SPEC_TYPE_STRING, + :SC_SPEC_TYPE_SYMBOL, :SC_SPEC_TYPE_ADDRESS, :SC_SPEC_TYPE_OPTION, :SC_SPEC_TYPE_RESULT, @@ -43,6 +47,10 @@ defmodule StellarBase.XDR.SCSpecTypeTest do <<0, 0, 0, 11>>, <<0, 0, 0, 12>>, <<0, 0, 0, 13>>, + <<0, 0, 0, 14>>, + <<0, 0, 0, 16>>, + <<0, 0, 0, 17>>, + <<0, 0, 0, 19>>, <<0, 0, 3, 232>>, <<0, 0, 3, 233>>, <<0, 0, 3, 234>>, diff --git a/test/xdr/contract/spec/sc_spec_type_tuple_test.exs b/test/xdr/contract/spec/sc_spec_type_tuple_test.exs index fa2f24f6..f86090a5 100644 --- a/test/xdr/contract/spec/sc_spec_type_tuple_test.exs +++ b/test/xdr/contract/spec/sc_spec_type_tuple_test.exs @@ -7,7 +7,7 @@ defmodule StellarBase.XDR.SCSpecTypeTupleTest do setup do code = Void.new() type_val = SCSpecType.new(:SC_SPEC_TYPE_VAL) - type_u128 = SCSpecType.new(:SC_SPEC_TYPE_U128) + type_u128 = SCSpecType.new(:SC_SPEC_TYPE_I32) sc_spec_type_def_1 = SCSpecTypeDef.new(code, type_val) sc_spec_type_def_2 = SCSpecTypeDef.new(code, type_u128) diff --git a/test/xdr/contract/spec/sc_spec_udt_union_case_tuple_v0_test.exs b/test/xdr/contract/spec/sc_spec_udt_union_case_tuple_v0_test.exs index 60f71a0a..02c795b3 100644 --- a/test/xdr/contract/spec/sc_spec_udt_union_case_tuple_v0_test.exs +++ b/test/xdr/contract/spec/sc_spec_udt_union_case_tuple_v0_test.exs @@ -17,7 +17,7 @@ defmodule StellarBase.XDR.SCSpecUDTUnionCaseTupleV0Test do name = String60.new("Hello there") code = Void.new() type_val = SCSpecType.new(:SC_SPEC_TYPE_VAL) - type_u128 = SCSpecType.new(:SC_SPEC_TYPE_U128) + type_u128 = SCSpecType.new(:SC_SPEC_TYPE_I32) sc_spec_type_def1 = SCSpecTypeDef.new(code, type_val) sc_spec_type_def2 = SCSpecTypeDef.new(code, type_u128) diff --git a/test/xdr/contract/spec/sc_spec_udt_union_case_v0_test.exs b/test/xdr/contract/spec/sc_spec_udt_union_case_v0_test.exs index 4444d00c..b57948aa 100644 --- a/test/xdr/contract/spec/sc_spec_udt_union_case_v0_test.exs +++ b/test/xdr/contract/spec/sc_spec_udt_union_case_v0_test.exs @@ -20,7 +20,7 @@ defmodule StellarBase.XDR.SCSpecUDTUnionCaseV0Test do name = String60.new("Hello there") code = Void.new() type_val = SCSpecType.new(:SC_SPEC_TYPE_VAL) - type_u128 = SCSpecType.new(:SC_SPEC_TYPE_U128) + type_u128 = SCSpecType.new(:SC_SPEC_TYPE_I32) sc_spec_type_def1 = SCSpecTypeDef.new(code, type_val) sc_spec_type_def2 = SCSpecTypeDef.new(code, type_u128) diff --git a/test/xdr/ledger_entries/contract_data_entry_test.exs b/test/xdr/ledger_entries/contract_data_entry_test.exs index 6bbbb6f7..60774df6 100644 --- a/test/xdr/ledger_entries/contract_data_entry_test.exs +++ b/test/xdr/ledger_entries/contract_data_entry_test.exs @@ -6,8 +6,8 @@ defmodule StellarBase.XDR.ContractDataEntryTest do describe "ContractDataEntry" do setup do contract_id = Hash.new("GCIZ3GSM5XL7OUS4UP64THMDZ7CZ3ZWN") - key = SCVal.new(Int64.new(1), SCValType.new(:SCV_U63)) - val = SCVal.new(Int64.new(2), SCValType.new(:SCV_U63)) + key = SCVal.new(Int64.new(1), SCValType.new(:SCV_I64)) + val = SCVal.new(Int64.new(2), SCValType.new(:SCV_I64)) %{ contract_id: contract_id, @@ -16,8 +16,8 @@ defmodule StellarBase.XDR.ContractDataEntryTest do contract_data_entry: ContractDataEntry.new(contract_id, key, val), binary: <<71, 67, 73, 90, 51, 71, 83, 77, 53, 88, 76, 55, 79, 85, 83, 52, 85, 80, 54, 52, 84, - 72, 77, 68, 90, 55, 67, 90, 51, 90, 87, 78, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 2>> + 72, 77, 68, 90, 55, 67, 90, 51, 90, 87, 78, 0, 0, 0, 6, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, + 0, 6, 0, 0, 0, 0, 0, 0, 0, 2>> } end diff --git a/test/xdr/ledger_entries/contract_data_test.exs b/test/xdr/ledger_entries/contract_data_test.exs index e0e4dfb5..f3208c56 100644 --- a/test/xdr/ledger_entries/contract_data_test.exs +++ b/test/xdr/ledger_entries/contract_data_test.exs @@ -6,7 +6,7 @@ defmodule StellarBase.XDR.ContractDataTest do describe "ContractData" do setup do contract_id = Hash.new("GCIZ3GSM5XL7OUS4UP64THMDZ7CZ3ZWN") - key = SCVal.new(Int64.new(1), SCValType.new(:SCV_U63)) + key = SCVal.new(Int64.new(1), SCValType.new(:SCV_I64)) %{ contract_id: contract_id, @@ -14,7 +14,7 @@ defmodule StellarBase.XDR.ContractDataTest do contract_data_entry: ContractData.new(contract_id, key), binary: <<71, 67, 73, 90, 51, 71, 83, 77, 53, 88, 76, 55, 79, 85, 83, 52, 85, 80, 54, 52, 84, - 72, 77, 68, 90, 55, 67, 90, 51, 90, 87, 78, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1>> + 72, 77, 68, 90, 55, 67, 90, 51, 90, 87, 78, 0, 0, 0, 6, 0, 0, 0, 0, 0, 0, 0, 1>> } end diff --git a/test/xdr/ledger_entries/ledger_entry_data_test.exs b/test/xdr/ledger_entries/ledger_entry_data_test.exs index e4c4950c..9274647e 100644 --- a/test/xdr/ledger_entries/ledger_entry_data_test.exs +++ b/test/xdr/ledger_entries/ledger_entry_data_test.exs @@ -122,8 +122,8 @@ defmodule StellarBase.XDR.LedgerEntryDataTest do ## ContractDataEntry contract_id = Hash.new("GCIZ3GSM5XL7OUS4UP64THMDZ7CZ3ZWN") - key = SCVal.new(Int64.new(1), SCValType.new(:SCV_U63)) - val = SCVal.new(Int64.new(2), SCValType.new(:SCV_U63)) + key = SCVal.new(Int64.new(1), SCValType.new(:SCV_I64)) + val = SCVal.new(Int64.new(2), SCValType.new(:SCV_I64)) discriminants = [ %{ @@ -192,8 +192,8 @@ defmodule StellarBase.XDR.LedgerEntryDataTest do ledger_entry_data: ContractDataEntry.new(contract_id, key, val), binary: <<0, 0, 0, 6, 71, 67, 73, 90, 51, 71, 83, 77, 53, 88, 76, 55, 79, 85, 83, 52, 85, 80, - 54, 52, 84, 72, 77, 68, 90, 55, 67, 90, 51, 90, 87, 78, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2>> + 54, 52, 84, 72, 77, 68, 90, 55, 67, 90, 51, 90, 87, 78, 0, 0, 0, 6, 0, 0, 0, 0, 0, 0, + 0, 1, 0, 0, 0, 6, 0, 0, 0, 0, 0, 0, 0, 2>> }, %{ type: LedgerEntryType.new(:CONTRACT_CODE), diff --git a/test/xdr/ledger_entries/ledger_entry_test.exs b/test/xdr/ledger_entries/ledger_entry_test.exs index 64a8a664..26091a38 100644 --- a/test/xdr/ledger_entries/ledger_entry_test.exs +++ b/test/xdr/ledger_entries/ledger_entry_test.exs @@ -33,8 +33,8 @@ defmodule StellarBase.XDR.LedgerEntryTest do last_modified_ledger_seq = UInt32.new(5) contract_id = Hash.new("GCIZ3GSM5XL7OUS4UP64THMDZ7CZ3ZWN") - key = SCVal.new(Int64.new(1), SCValType.new(:SCV_U63)) - val = SCVal.new(Int64.new(2), SCValType.new(:SCV_U63)) + key = SCVal.new(Int64.new(1), SCValType.new(:SCV_I64)) + val = SCVal.new(Int64.new(2), SCValType.new(:SCV_I64)) ledger_entry_type = LedgerEntryType.new(:CONTRACT_DATA) ledger_entry_data = ContractDataEntry.new(contract_id, key, val) @@ -69,11 +69,11 @@ defmodule StellarBase.XDR.LedgerEntryTest do ledger_entry_list: ledger_entry_list, binaries: [ <<0, 0, 0, 5, 0, 0, 0, 6, 71, 67, 73, 90, 51, 71, 83, 77, 53, 88, 76, 55, 79, 85, 83, - 52, 85, 80, 54, 52, 84, 72, 77, 68, 90, 55, 67, 90, 51, 90, 87, 78, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0>>, + 52, 85, 80, 54, 52, 84, 72, 77, 68, 90, 55, 67, 90, 51, 90, 87, 78, 0, 0, 0, 6, 0, 0, + 0, 0, 0, 0, 0, 1, 0, 0, 0, 6, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0>>, <<0, 0, 0, 5, 0, 0, 0, 6, 71, 67, 73, 90, 51, 71, 83, 77, 53, 88, 76, 55, 79, 85, 83, - 52, 85, 80, 54, 52, 84, 72, 77, 68, 90, 55, 67, 90, 51, 90, 87, 78, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, + 52, 85, 80, 54, 52, 84, 72, 77, 68, 90, 55, 67, 90, 51, 90, 87, 78, 0, 0, 0, 6, 0, 0, + 0, 0, 0, 0, 0, 1, 0, 0, 0, 6, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 155, 142, 186, 248, 150, 56, 85, 29, 207, 158, 164, 247, 67, 32, 113, 16, 107, 135, 171, 14, 45, 179, 214, 155, 117, 165, 56, 34, 114, 247, 89, 216, 0, 0, 0, 0>> ] diff --git a/test/xdr/transactions/authorized_invocation_list_test.exs b/test/xdr/transactions/authorized_invocation_list_test.exs index 393814cb..24f460db 100644 --- a/test/xdr/transactions/authorized_invocation_list_test.exs +++ b/test/xdr/transactions/authorized_invocation_list_test.exs @@ -16,8 +16,8 @@ defmodule StellarBase.XDR.AuthorizedInvocationListTest do setup do contract_id = Hash.new("GCIZ3GSM5XL7OUS4UP64THMDZ7CZ3ZWN") function_name = SCSymbol.new("Hello") - scval1 = SCVal.new(Int64.new(3), SCValType.new(:SCV_U63)) - scval2 = SCVal.new(Int64.new(2), SCValType.new(:SCV_U63)) + scval1 = SCVal.new(Int64.new(3), SCValType.new(:SCV_I64)) + scval2 = SCVal.new(Int64.new(2), SCValType.new(:SCV_I64)) sc_vals = [scval1, scval2] args = SCVec.new(sc_vals) @@ -42,14 +42,14 @@ defmodule StellarBase.XDR.AuthorizedInvocationListTest do binary: <<0, 0, 0, 3, 71, 67, 73, 90, 51, 71, 83, 77, 53, 88, 76, 55, 79, 85, 83, 52, 85, 80, 54, 52, 84, 72, 77, 68, 90, 55, 67, 90, 51, 90, 87, 78, 0, 0, 0, 5, 72, 101, 108, 108, - 111, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 0, 0, 0, 0, 0, 0, 0, 0, + 111, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 6, 0, 0, 0, 0, 0, 0, 0, 3, 0, 0, 0, 6, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 71, 67, 73, 90, 51, 71, 83, 77, 53, 88, 76, 55, 79, 85, 83, 52, 85, 80, 54, 52, 84, 72, 77, 68, 90, 55, 67, 90, 51, 90, 87, 78, 0, 0, 0, 5, 72, - 101, 108, 108, 111, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 71, 67, 73, 90, 51, 71, 83, 77, 53, 88, 76, 55, + 101, 108, 108, 111, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 6, 0, 0, 0, 0, 0, 0, 0, 3, 0, 0, 0, + 6, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 71, 67, 73, 90, 51, 71, 83, 77, 53, 88, 76, 55, 79, 85, 83, 52, 85, 80, 54, 52, 84, 72, 77, 68, 90, 55, 67, 90, 51, 90, 87, 78, 0, 0, - 0, 5, 72, 101, 108, 108, 111, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0>> + 0, 5, 72, 101, 108, 108, 111, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 6, 0, 0, 0, 0, 0, 0, 0, 3, + 0, 0, 0, 6, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0>> } end diff --git a/test/xdr/transactions/authorized_invocation_test.exs b/test/xdr/transactions/authorized_invocation_test.exs index d455a61c..afe766f6 100644 --- a/test/xdr/transactions/authorized_invocation_test.exs +++ b/test/xdr/transactions/authorized_invocation_test.exs @@ -16,8 +16,8 @@ defmodule StellarBase.XDR.AuthorizedInvocationTest do setup do contract_id = Hash.new("GCIZ3GSM5XL7OUS4UP64THMDZ7CZ3ZWN") function_name = SCSymbol.new("Hello") - scval1 = SCVal.new(Int64.new(3), SCValType.new(:SCV_U63)) - scval2 = SCVal.new(Int64.new(2), SCValType.new(:SCV_U63)) + scval1 = SCVal.new(Int64.new(3), SCValType.new(:SCV_I64)) + scval2 = SCVal.new(Int64.new(2), SCValType.new(:SCV_I64)) sc_vals = [scval1, scval2] args = SCVec.new(sc_vals) @@ -46,17 +46,17 @@ defmodule StellarBase.XDR.AuthorizedInvocationTest do binary: <<71, 67, 73, 90, 51, 71, 83, 77, 53, 88, 76, 55, 79, 85, 83, 52, 85, 80, 54, 52, 84, 72, 77, 68, 90, 55, 67, 90, 51, 90, 87, 78, 0, 0, 0, 5, 72, 101, 108, 108, 111, 0, 0, - 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, + 0, 0, 0, 0, 2, 0, 0, 0, 6, 0, 0, 0, 0, 0, 0, 0, 3, 0, 0, 0, 6, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 3, 71, 67, 73, 90, 51, 71, 83, 77, 53, 88, 76, 55, 79, 85, 83, 52, 85, 80, 54, 52, 84, 72, 77, 68, 90, 55, 67, 90, 51, 90, 87, 78, 0, 0, 0, 5, 72, 101, 108, 108, - 111, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 0, 0, 0, 0, 0, 0, 0, 0, + 111, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 6, 0, 0, 0, 0, 0, 0, 0, 3, 0, 0, 0, 6, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 71, 67, 73, 90, 51, 71, 83, 77, 53, 88, 76, 55, 79, 85, 83, 52, 85, 80, 54, 52, 84, 72, 77, 68, 90, 55, 67, 90, 51, 90, 87, 78, 0, 0, 0, 5, 72, - 101, 108, 108, 111, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 71, 67, 73, 90, 51, 71, 83, 77, 53, 88, 76, 55, + 101, 108, 108, 111, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 6, 0, 0, 0, 0, 0, 0, 0, 3, 0, 0, 0, + 6, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 71, 67, 73, 90, 51, 71, 83, 77, 53, 88, 76, 55, 79, 85, 83, 52, 85, 80, 54, 52, 84, 72, 77, 68, 90, 55, 67, 90, 51, 90, 87, 78, 0, 0, - 0, 5, 72, 101, 108, 108, 111, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0>> + 0, 5, 72, 101, 108, 108, 111, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 6, 0, 0, 0, 0, 0, 0, 0, 3, + 0, 0, 0, 6, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0>> } end diff --git a/test/xdr/transactions/contract_auth_list_test.exs b/test/xdr/transactions/contract_auth_list_test.exs index 31baf71a..2353ecbe 100644 --- a/test/xdr/transactions/contract_auth_list_test.exs +++ b/test/xdr/transactions/contract_auth_list_test.exs @@ -45,8 +45,8 @@ defmodule StellarBase.XDR.ContractAuthListTest do sc_address |> AddressWithNonce.new(nonce) |> OptionalAddressWithNonce.new() # SCVec - scval1 = SCVal.new(Int64.new(3), SCValType.new(:SCV_U63)) - scval2 = SCVal.new(Int64.new(2), SCValType.new(:SCV_U63)) + scval1 = SCVal.new(Int64.new(3), SCValType.new(:SCV_I64)) + scval2 = SCVal.new(Int64.new(2), SCValType.new(:SCV_I64)) sc_vals = [scval1, scval2] sc_vec = SCVec.new(sc_vals) @@ -74,16 +74,16 @@ defmodule StellarBase.XDR.ContractAuthListTest do 137, 68, 149, 154, 124, 205, 198, 221, 187, 173, 152, 33, 210, 37, 10, 76, 25, 212, 179, 73, 138, 2, 227, 119, 0, 0, 0, 0, 0, 0, 0, 123, 71, 67, 73, 90, 51, 71, 83, 77, 53, 88, 76, 55, 79, 85, 83, 52, 85, 80, 54, 52, 84, 72, 77, 68, 90, 55, 67, 90, 51, - 90, 87, 78, 0, 0, 0, 5, 72, 101, 108, 108, 111, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 1, 0, 0, 0, 0, + 90, 87, 78, 0, 0, 0, 5, 72, 101, 108, 108, 111, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 6, 0, 0, + 0, 0, 0, 0, 0, 3, 0, 0, 0, 6, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, + 6, 0, 0, 0, 0, 0, 0, 0, 3, 0, 0, 0, 6, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 114, 213, 178, 144, 98, 27, 186, 154, 137, 68, 149, 154, 124, 205, 198, 221, 187, 173, 152, 33, 210, 37, 10, 76, 25, 212, 179, 73, 138, 2, 227, 119, 0, 0, 0, 0, 0, 0, 0, 123, 71, 67, 73, 90, 51, 71, 83, 77, 53, 88, 76, 55, 79, 85, 83, 52, 85, 80, 54, 52, 84, 72, 77, 68, 90, 55, 67, 90, 51, 90, 87, 78, 0, 0, 0, 5, 72, 101, 108, - 108, 111, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 2>> + 108, 111, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 6, 0, 0, 0, 0, 0, 0, 0, 3, 0, 0, 0, 6, 0, 0, + 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 6, 0, 0, 0, 0, 0, 0, 0, 3, 0, 0, 0, + 6, 0, 0, 0, 0, 0, 0, 0, 2>> } end diff --git a/test/xdr/transactions/contract_auth_test.exs b/test/xdr/transactions/contract_auth_test.exs index 0d54e1b7..069c652a 100644 --- a/test/xdr/transactions/contract_auth_test.exs +++ b/test/xdr/transactions/contract_auth_test.exs @@ -46,8 +46,8 @@ defmodule StellarBase.XDR.ContractAuthTest do address_with_nonce_nil = OptionalAddressWithNonce.new() # SCVec - scval1 = SCVal.new(Int64.new(3), SCValType.new(:SCV_U63)) - scval2 = SCVal.new(Int64.new(2), SCValType.new(:SCV_U63)) + scval1 = SCVal.new(Int64.new(3), SCValType.new(:SCV_I64)) + scval2 = SCVal.new(Int64.new(2), SCValType.new(:SCV_I64)) sc_vals = [scval1, scval2] sc_vec = SCVec.new(sc_vals) @@ -74,9 +74,9 @@ defmodule StellarBase.XDR.ContractAuthTest do 149, 154, 124, 205, 198, 221, 187, 173, 152, 33, 210, 37, 10, 76, 25, 212, 179, 73, 138, 2, 227, 119, 0, 0, 0, 0, 0, 0, 0, 123, 71, 67, 73, 90, 51, 71, 83, 77, 53, 88, 76, 55, 79, 85, 83, 52, 85, 80, 54, 52, 84, 72, 77, 68, 90, 55, 67, 90, 51, 90, 87, - 78, 0, 0, 0, 5, 72, 101, 108, 108, 111, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2>> + 78, 0, 0, 0, 5, 72, 101, 108, 108, 111, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 6, 0, 0, 0, 0, + 0, 0, 0, 3, 0, 0, 0, 6, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 6, + 0, 0, 0, 0, 0, 0, 0, 3, 0, 0, 0, 6, 0, 0, 0, 0, 0, 0, 0, 2>> }, %{ address_with_nonce: address_with_nonce_nil, @@ -86,9 +86,9 @@ defmodule StellarBase.XDR.ContractAuthTest do binary: <<0, 0, 0, 0, 71, 67, 73, 90, 51, 71, 83, 77, 53, 88, 76, 55, 79, 85, 83, 52, 85, 80, 54, 52, 84, 72, 77, 68, 90, 55, 67, 90, 51, 90, 87, 78, 0, 0, 0, 5, 72, 101, 108, - 108, 111, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 2>> + 108, 111, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 6, 0, 0, 0, 0, 0, 0, 0, 3, 0, 0, 0, 6, 0, 0, + 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 6, 0, 0, 0, 0, 0, 0, 0, 3, 0, 0, + 0, 6, 0, 0, 0, 0, 0, 0, 0, 2>> } ] diff --git a/test/xdr/transactions/create_contract_args_test.exs b/test/xdr/transactions/create_contract_args_test.exs index 4ebb7d2c..7a57c24c 100644 --- a/test/xdr/transactions/create_contract_args_test.exs +++ b/test/xdr/transactions/create_contract_args_test.exs @@ -5,8 +5,8 @@ defmodule StellarBase.XDR.CreateContractArgsTest do ContractID, ContractIDType, CreateContractArgs, - SCContractCode, - SCContractCodeType, + SCContractExecutable, + SCContractExecutableType, Hash, UInt256 } @@ -15,11 +15,12 @@ defmodule StellarBase.XDR.CreateContractArgsTest do describe "CreateContractArgs" do setup do - # SCContractCode - contract_code = Hash.new("GCIZ3GSM5XL7OUS4UP64THMDZ7CZ3ZWN") - sc_contract_code_type = SCContractCodeType.new(:SCCONTRACT_CODE_WASM_REF) + # SCContractExecutable + contract_executable = Hash.new("GCIZ3GSM5XL7OUS4UP64THMDZ7CZ3ZWN") + sc_contract_executable_type = SCContractExecutableType.new(:SCCONTRACT_EXECUTABLE_WASM_REF) - sc_contract_code = SCContractCode.new(contract_code, sc_contract_code_type) + sc_contract_executable = + SCContractExecutable.new(contract_executable, sc_contract_executable_type) # ContractID salt = @@ -31,9 +32,9 @@ defmodule StellarBase.XDR.CreateContractArgsTest do contract_id = ContractID.new(salt, contract_id_type) %{ - sc_contract_code: sc_contract_code, + sc_contract_executable: sc_contract_executable, contract_id: contract_id, - create_contract_args: CreateContractArgs.new(contract_id, sc_contract_code), + create_contract_args: CreateContractArgs.new(contract_id, sc_contract_executable), binary: <<0, 0, 0, 0, 146, 34, 171, 230, 201, 29, 122, 130, 214, 117, 176, 89, 199, 162, 234, 50, 8, 7, 21, 233, 208, 154, 26, 185, 125, 2, 52, 182, 37, 173, 239, 146, 0, 0, 0, 0, @@ -42,9 +43,9 @@ defmodule StellarBase.XDR.CreateContractArgsTest do } end - test "new/1", %{contract_id: contract_id, sc_contract_code: sc_contract_code} do - %CreateContractArgs{contract_id: ^contract_id, source: ^sc_contract_code} = - CreateContractArgs.new(contract_id, sc_contract_code) + test "new/1", %{contract_id: contract_id, sc_contract_executable: sc_contract_executable} do + %CreateContractArgs{contract_id: ^contract_id, source: ^sc_contract_executable} = + CreateContractArgs.new(contract_id, sc_contract_executable) end test "encode_xdr/1", %{create_contract_args: create_contract_args, binary: binary} do diff --git a/test/xdr/transactions/hash_id_preimage_contract_auth_test.exs b/test/xdr/transactions/hash_id_preimage_contract_auth_test.exs index 3c748fd2..cb619b3e 100644 --- a/test/xdr/transactions/hash_id_preimage_contract_auth_test.exs +++ b/test/xdr/transactions/hash_id_preimage_contract_auth_test.exs @@ -22,8 +22,8 @@ defmodule StellarBase.XDR.HashIDPreimageContractAuthTest do contract_id = Hash.new("GCIZ3GSM5XL7OUS4UP64THMDZ7CZ3ZWK") function_name = SCSymbol.new("Hello") - scval1 = SCVal.new(Int64.new(3), SCValType.new(:SCV_U63)) - scval2 = SCVal.new(Int64.new(2), SCValType.new(:SCV_U63)) + scval1 = SCVal.new(Int64.new(3), SCValType.new(:SCV_I64)) + scval2 = SCVal.new(Int64.new(2), SCValType.new(:SCV_I64)) sc_vals = [scval1, scval2] args = SCVec.new(sc_vals) @@ -55,17 +55,17 @@ defmodule StellarBase.XDR.HashIDPreimageContractAuthTest do 72, 77, 68, 90, 55, 67, 90, 51, 90, 87, 78, 255, 255, 255, 255, 255, 255, 255, 255, 71, 67, 73, 90, 51, 71, 83, 77, 53, 88, 76, 55, 79, 85, 83, 52, 85, 80, 54, 52, 84, 72, 77, 68, 90, 55, 67, 90, 51, 90, 87, 75, 0, 0, 0, 5, 72, 101, 108, 108, 111, 0, 0, - 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, + 0, 0, 0, 0, 2, 0, 0, 0, 6, 0, 0, 0, 0, 0, 0, 0, 3, 0, 0, 0, 6, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 3, 71, 67, 73, 90, 51, 71, 83, 77, 53, 88, 76, 55, 79, 85, 83, 52, 85, 80, 54, 52, 84, 72, 77, 68, 90, 55, 67, 90, 51, 90, 87, 75, 0, 0, 0, 5, 72, 101, 108, 108, - 111, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 0, 0, 0, 0, 0, 0, 0, 0, + 111, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 6, 0, 0, 0, 0, 0, 0, 0, 3, 0, 0, 0, 6, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 71, 67, 73, 90, 51, 71, 83, 77, 53, 88, 76, 55, 79, 85, 83, 52, 85, 80, 54, 52, 84, 72, 77, 68, 90, 55, 67, 90, 51, 90, 87, 75, 0, 0, 0, 5, 72, - 101, 108, 108, 111, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 71, 67, 73, 90, 51, 71, 83, 77, 53, 88, 76, 55, + 101, 108, 108, 111, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 6, 0, 0, 0, 0, 0, 0, 0, 3, 0, 0, 0, + 6, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 71, 67, 73, 90, 51, 71, 83, 77, 53, 88, 76, 55, 79, 85, 83, 52, 85, 80, 54, 52, 84, 72, 77, 68, 90, 55, 67, 90, 51, 90, 87, 75, 0, 0, - 0, 5, 72, 101, 108, 108, 111, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0>> + 0, 5, 72, 101, 108, 108, 111, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 6, 0, 0, 0, 0, 0, 0, 0, 3, + 0, 0, 0, 6, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0>> } end diff --git a/test/xdr/transactions/hash_id_preimage_create_contract_args_test.exs b/test/xdr/transactions/hash_id_preimage_create_contract_args_test.exs index 1b791842..91f5402e 100644 --- a/test/xdr/transactions/hash_id_preimage_create_contract_args_test.exs +++ b/test/xdr/transactions/hash_id_preimage_create_contract_args_test.exs @@ -5,17 +5,17 @@ defmodule StellarBase.XDR.HashIDPreimageCreateContractArgsTest do HashIDPreimageCreateContractArgs, Hash, UInt256, - SCContractCode, - SCContractCodeType + SCContractExecutable, + SCContractExecutableType } describe "HashIDPreimageCreateContractArgs" do setup do network_id = Hash.new("GCIZ3GSM5XL7OUS4UP64THMDZ7CZ3ZWN") - contract_code = Hash.new("GCIZ3GSM5XL7OUS4UP64THMDZ7CZ3ZWK") - sc_contract_code_type = SCContractCodeType.new(:SCCONTRACT_CODE_WASM_REF) - source = SCContractCode.new(contract_code, sc_contract_code_type) + contract_executable = Hash.new("GCIZ3GSM5XL7OUS4UP64THMDZ7CZ3ZWK") + sc_contract_executable_type = SCContractExecutableType.new(:SCCONTRACT_EXECUTABLE_WASM_REF) + source = SCContractExecutable.new(contract_executable, sc_contract_executable_type) salt = UInt256.new( diff --git a/test/xdr/transactions/host_function_test.exs b/test/xdr/transactions/host_function_test.exs index 3c396cea..c94a523f 100644 --- a/test/xdr/transactions/host_function_test.exs +++ b/test/xdr/transactions/host_function_test.exs @@ -12,8 +12,8 @@ defmodule StellarBase.XDR.HostFunctionTest do SCVec, InstallContractCodeArgs, Hash, - SCContractCodeType, - SCContractCode, + SCContractExecutableType, + SCContractExecutable, UInt256, ContractIDType, ContractID @@ -24,17 +24,18 @@ defmodule StellarBase.XDR.HostFunctionTest do describe "HostFunction" do setup do ## SCVec - scval1 = SCVal.new(Int64.new(3), SCValType.new(:SCV_U63)) - scval2 = SCVal.new(Int64.new(2), SCValType.new(:SCV_U63)) + scval1 = SCVal.new(Int64.new(3), SCValType.new(:SCV_I64)) + scval2 = SCVal.new(Int64.new(2), SCValType.new(:SCV_I64)) sc_vals = [scval1, scval2] sc_vec = SCVec.new(sc_vals) ## CreateContractArgs - # SCContractCode - contract_code = Hash.new("GCIZ3GSM5XL7OUS4UP64THMDZ7CZ3ZWN") - sc_contract_code_type = SCContractCodeType.new(:SCCONTRACT_CODE_WASM_REF) + # SCContractExecutable + contract_executable = Hash.new("GCIZ3GSM5XL7OUS4UP64THMDZ7CZ3ZWN") + sc_contract_executable_type = SCContractExecutableType.new(:SCCONTRACT_EXECUTABLE_WASM_REF) - sc_contract_code = SCContractCode.new(contract_code, sc_contract_code_type) + sc_contract_executable = + SCContractExecutable.new(contract_executable, sc_contract_executable_type) # ContractID salt = @@ -45,7 +46,7 @@ defmodule StellarBase.XDR.HostFunctionTest do contract_id_type = ContractIDType.new(:CONTRACT_ID_FROM_SOURCE_ACCOUNT) contract_id = ContractID.new(salt, contract_id_type) - create_contract_args = CreateContractArgs.new(contract_id, sc_contract_code) + create_contract_args = CreateContractArgs.new(contract_id, sc_contract_executable) ## InstallContractCodeArgs code = VariableOpaque256000.new("GCIZ3GSM5") @@ -56,7 +57,7 @@ defmodule StellarBase.XDR.HostFunctionTest do host_function_type: HostFunctionType.new(:HOST_FUNCTION_TYPE_INVOKE_CONTRACT), host_function: sc_vec, binary: - <<0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 0, 0, 0, 0, 0, 0, 0, 0, + <<0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 6, 0, 0, 0, 0, 0, 0, 0, 3, 0, 0, 0, 6, 0, 0, 0, 0, 0, 0, 0, 2>> }, %{ diff --git a/test/xdr/transactions/invoke_host_function_result_test.exs b/test/xdr/transactions/invoke_host_function_result_test.exs index 4c24df46..13e056a8 100644 --- a/test/xdr/transactions/invoke_host_function_result_test.exs +++ b/test/xdr/transactions/invoke_host_function_result_test.exs @@ -24,7 +24,7 @@ defmodule StellarBase.XDR.InvokeHostFunctionResultTest do void = Void.new() int_64 = Int64.new(3) - scval_type = SCValType.new(:SCV_U63) + scval_type = SCValType.new(:SCV_I64) scval = SCVal.new(int_64, scval_type) discriminants = [ @@ -33,7 +33,7 @@ defmodule StellarBase.XDR.InvokeHostFunctionResultTest do invoke_host_function_result_code: invoke_host_function_result_success, invoke_host_function_result: InvokeHostFunctionResult.new(scval, invoke_host_function_result_success), - binary: <<0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3>> + binary: <<0, 0, 0, 0, 0, 0, 0, 6, 0, 0, 0, 0, 0, 0, 0, 3>> }, %{ value: void, diff --git a/test/xdr/transactions/operations/invoke_host_function_test.exs b/test/xdr/transactions/operations/invoke_host_function_test.exs index 68e43c34..b1eaadac 100644 --- a/test/xdr/transactions/operations/invoke_host_function_test.exs +++ b/test/xdr/transactions/operations/invoke_host_function_test.exs @@ -44,8 +44,8 @@ defmodule StellarBase.XDR.Operations.InvokeHostFunctionTest do describe "InvokeHostFunction" do setup do ## HostFunction - scval1 = SCVal.new(Int64.new(3), SCValType.new(:SCV_U63)) - scval2 = SCVal.new(Int64.new(2), SCValType.new(:SCV_U63)) + scval1 = SCVal.new(Int64.new(3), SCValType.new(:SCV_I64)) + scval2 = SCVal.new(Int64.new(2), SCValType.new(:SCV_I64)) sc_vals = [scval1, scval2] sc_vec = SCVec.new(sc_vals) @@ -102,7 +102,7 @@ defmodule StellarBase.XDR.Operations.InvokeHostFunctionTest do auth: auth_list, invoke_host_function_op: InvokeHostFunction.new(host_function, footprint, auth_list), binary: - <<0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, + <<0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 6, 0, 0, 0, 0, 0, 0, 0, 3, 0, 0, 0, 6, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 114, 213, 178, 144, 98, 27, 186, 154, 137, 68, 149, 154, 124, 205, 198, 221, 187, 173, 152, 33, 210, 37, 10, 76, 25, 212, 179, 73, 138, 2, 227, 119, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 114, 213, 178, 144, 98, @@ -112,15 +112,15 @@ defmodule StellarBase.XDR.Operations.InvokeHostFunctionTest do 173, 152, 33, 210, 37, 10, 76, 25, 212, 179, 73, 138, 2, 227, 119, 0, 0, 0, 0, 0, 0, 0, 123, 71, 67, 73, 90, 51, 71, 83, 77, 53, 88, 76, 55, 79, 85, 83, 52, 85, 80, 54, 52, 84, 72, 77, 68, 90, 55, 67, 90, 51, 90, 87, 78, 0, 0, 0, 5, 72, 101, 108, 108, - 111, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 0, 0, 0, 0, 0, + 111, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 6, 0, 0, 0, 0, 0, 0, 0, 3, 0, 0, 0, 6, 0, 0, 0, 0, + 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 6, 0, 0, 0, 0, 0, 0, 0, 3, 0, 0, 0, 6, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 114, 213, 178, 144, 98, 27, 186, 154, 137, 68, 149, 154, 124, 205, 198, 221, 187, 173, 152, 33, 210, 37, 10, 76, 25, 212, 179, 73, 138, 2, 227, 119, 0, 0, 0, 0, 0, 0, 0, 123, 71, 67, 73, 90, 51, 71, 83, 77, 53, 88, 76, 55, 79, 85, 83, 52, 85, 80, 54, 52, 84, 72, 77, 68, 90, 55, 67, 90, 51, 90, 87, 78, 0, 0, 0, 5, 72, 101, 108, 108, 111, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 2, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2>> + 6, 0, 0, 0, 0, 0, 0, 0, 3, 0, 0, 0, 6, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 2, + 0, 0, 0, 6, 0, 0, 0, 0, 0, 0, 0, 3, 0, 0, 0, 6, 0, 0, 0, 0, 0, 0, 0, 2>> } end diff --git a/test/xdr/types/bool_test.exs b/test/xdr/types/bool_test.exs new file mode 100644 index 00000000..bdf92ea1 --- /dev/null +++ b/test/xdr/types/bool_test.exs @@ -0,0 +1,42 @@ +defmodule StellarBase.XDR.BoolTest do + use ExUnit.Case + + alias StellarBase.XDR.Bool + + describe "Bool" do + setup do + %{ + bool: Bool.new(true), + binary: <<0, 0, 0, 1>> + } + end + + test "new/1" do + %Bool{value: false} = Bool.new(false) + end + + test "encode_xdr/1", %{bool: bool, binary: binary} do + {:ok, ^binary} = Bool.encode_xdr(bool) + end + + test "encode_xdr/1 with a non boolean" do + {:error, :not_boolean} = Bool.encode_xdr(%Bool{value: "1234"}) + end + + test "encode_xdr!/1", %{bool: bool, binary: binary} do + ^binary = Bool.encode_xdr!(bool) + end + + test "decode_xdr/2", %{bool: bool, binary: binary} do + {:ok, {^bool, ""}} = Bool.decode_xdr(binary) + end + + test "decode_xdr/2 with an invalid binary" do + {:error, :invalid_value} = Bool.decode_xdr(123) + end + + test "decode_xdr!/2", %{bool: bool, binary: binary} do + {^bool, ^binary} = Bool.decode_xdr!(binary <> binary) + end + end +end diff --git a/test/xdr/transactions/duration_test.exs b/test/xdr/types/duration_test.exs similarity index 100% rename from test/xdr/transactions/duration_test.exs rename to test/xdr/types/duration_test.exs diff --git a/test/xdr/transactions/time_point_test.exs b/test/xdr/types/time_point_test.exs similarity index 100% rename from test/xdr/transactions/time_point_test.exs rename to test/xdr/types/time_point_test.exs