MPP.Session.Channel (mpp v0.15.0)

Copy Markdown View Source

State and TIP-1034 precompile identity for an MPP payment channel.

Channel IDs match the Tempo TIP-20 Channel Reserve precompile:

keccak256(abi.encode(payer, payee, operator, token, salt,
                     authorizedSigner, expiringNonceHash,
                     escrowContract, chainId))

Channel lifecycle is deliberately small: a new channel is :open, may be activated once, and an active channel may be closed once.

Summary

Functions

Parse a session credential action JSON value into its Elixir atom.

Return the camelCase JSON value for a session credential action atom.

Move an open channel to the active state.

Deduct a per-request spend from the authorized voucher balance.

Increase the channel deposit by a positive additional amount.

Raise the accepted cumulative voucher amount. Equal amounts are idempotent.

Authorized-but-unspent voucher balance (cumulative_amount - spent).

Move an active channel to the closed state.

Compute the TIP-1034 channel ID from its complete identity descriptor.

Compute a channel ID, raising ArgumentError on invalid input.

Create validated channel state in the :open status.

Create validated channel state, raising ArgumentError on invalid input.

Normalize a 32-byte channel ID to lowercase, 0x-prefixed hex.

Unvouchered remainder of the on-channel deposit (deposit - cumulative_amount).

Types

action()

@type action() :: :open | :top_up | :voucher | :close

id_params()

@type id_params() :: %{
  payer: String.t(),
  payee: String.t(),
  operator: String.t(),
  token: String.t(),
  salt: String.t(),
  authorized_signer: String.t(),
  expiring_nonce_hash: String.t(),
  escrow_contract: String.t(),
  chain_id: non_neg_integer()
}

status()

@type status() :: :open | :active | :closed

t()

@type t() :: %MPP.Session.Channel{
  channel_id: String.t(),
  cumulative_amount: non_neg_integer(),
  deposit: non_neg_integer(),
  payer: String.t(),
  recipient: String.t(),
  spent: non_neg_integer(),
  status: status(),
  token: String.t(),
  units: non_neg_integer()
}

Functions

action_from_wire(arg1)

@spec action_from_wire(term()) :: {:ok, action()} | {:error, :invalid_action}

Parse a session credential action JSON value into its Elixir atom.

action_to_wire(atom)

@spec action_to_wire(action()) :: String.t()

Return the camelCase JSON value for a session credential action atom.

activate(channel)

@spec activate(t()) :: {:ok, t()} | {:error, {:invalid_transition, status(), :active}}

Move an open channel to the active state.

apply_spend(channel, amount)

@spec apply_spend(t(), non_neg_integer()) :: {:ok, t()} | {:error, term()}

Deduct a per-request spend from the authorized voucher balance.

apply_top_up(channel, amount)

@spec apply_top_up(t(), pos_integer()) :: {:ok, t()} | {:error, term()}

Increase the channel deposit by a positive additional amount.

apply_voucher(channel, amount)

@spec apply_voucher(t(), non_neg_integer()) :: {:ok, t()} | {:error, term()}

Raise the accepted cumulative voucher amount. Equal amounts are idempotent.

available_balance(channel)

@spec available_balance(t()) :: non_neg_integer()

Authorized-but-unspent voucher balance (cumulative_amount - spent).

close(channel)

@spec close(t()) :: {:ok, t()} | {:error, {:invalid_transition, status(), :closed}}

Move an active channel to the closed state.

compute_id(params)

@spec compute_id(id_params() | keyword()) :: {:ok, String.t()} | {:error, term()}

Compute the TIP-1034 channel ID from its complete identity descriptor.

compute_id!(params)

@spec compute_id!(id_params() | keyword()) :: String.t()

Compute a channel ID, raising ArgumentError on invalid input.

new(opts)

@spec new(keyword()) :: {:ok, t()} | {:error, term()}

Create validated channel state in the :open status.

new!(opts)

@spec new!(keyword()) :: t()

Create validated channel state, raising ArgumentError on invalid input.

normalize_id(channel_id)

@spec normalize_id(term()) ::
  {:ok, String.t()} | {:error, {:invalid_channel_id, term()}}

Normalize a 32-byte channel ID to lowercase, 0x-prefixed hex.

remaining_deposit(channel)

@spec remaining_deposit(t()) :: non_neg_integer()

Unvouchered remainder of the on-channel deposit (deposit - cumulative_amount).