MPP.Client.Transport.JsonRpc (mpp v0.16.0)

Copy Markdown View Source

JSON-RPC implementation of MPP.Client.Transport for generic (non-MCP) RPC.

Operates on JSON-RPC request/response maps. Challenges arrive in error.data.challenges on error code -32042; credentials are attached at the request's root-level _meta["org.paymentauth/credential"] so params may be an array (e.g. eth_getBlockByNumber).

MCP's nested params._meta placement is MPP.Client.Transport.MCP.

API Functions

FunctionArityDescriptionParam Kinds
set_credential2Attach a credential at the JSON-RPC request's root _meta.request: value, credential: value
get_challenges1Parse Payment challenges from a JSON-RPC payment-required message.response: value
payment_required?1Return true if the JSON-RPC message signals payment required.response: value

Summary

Functions

Parse Payment challenges from a JSON-RPC payment-required message.

Return true if the JSON-RPC message signals payment required.

Attach a credential at the JSON-RPC request's root _meta.

Functions

get_challenges(response)

@spec get_challenges(term()) :: {:ok, [MPP.Challenge.t()]} | {:error, term()}
@spec get_challenges(term()) :: {:error, :malformed_envelope}

Parse Payment challenges from a JSON-RPC payment-required message.

Parameters

  • response - JSON-RPC response envelope or error object (value)

Returns

{:ok, [challenge]} on success, {:error, reason} otherwise (tagged_tuple)

Errors

  • :malformed_envelope
  • :no_challenges
  • :invalid_challenge
# descripex:contract
%{
  params: %{
    response: %{
      description: "JSON-RPC response envelope or error object",
      kind: :value
    }
  },
  errors: [:malformed_envelope, :no_challenges, :invalid_challenge],
  returns: %{
    type: :tagged_tuple,
    description: "`{:ok, [challenge]}` on success, `{:error, reason}` otherwise"
  }
}

payment_required?(response)

@spec payment_required?(term()) :: boolean()
@spec payment_required?(term()) :: false

Return true if the JSON-RPC message signals payment required.

Parameters

  • response - JSON-RPC response envelope or error object (value)

Returns

true when the error code is -32042 (boolean)

# descripex:contract
%{
  params: %{
    response: %{
      description: "JSON-RPC response envelope or error object",
      kind: :value
    }
  },
  returns: %{type: :boolean, description: "true when the error code is -32042"}
}

set_credential(request, credential)

@spec set_credential(map(), MPP.Credential.t()) :: map()

Attach a credential at the JSON-RPC request's root _meta.

Parameters

  • request - JSON-RPC request envelope (value)
  • credential - MPP.Credential struct (value)

Returns

Request with the credential in root _meta (map)

# descripex:contract
%{
  params: %{
    request: %{description: "JSON-RPC request envelope", kind: :value},
    credential: %{description: "MPP.Credential struct", kind: :value}
  },
  returns: %{
    type: :map,
    description: "Request with the credential in root `_meta`"
  }
}