defmodule RNS.Packet.DataPacketProof do @moduledoc """ A data packet proof packet is a proof sent by the destination of a data packet to confirm that it has received the packet. """ @type t() :: RNS.Packet.t() @doc "Creates a new (implicit) data packet proof packet from the hash and destination of the packet to prove." @spec new(RNS.Packet.hash(), RNS.Identity.t()) :: RNS.Packet.t() def new(packet_hash, identity) do RNS.Identity.sign(packet_hash, identity) |> RNS.Packet.new(RNS.Crypto.truncate(packet_hash, 16), packet_type: :proof) end @doc """ Verifies a data packet proof using the proof packet and the packet hash and destination of the packet that has been proven. Returns a boolean. """ @spec validate?(t(), RNS.Packet.hash(), RNS.Identity.t()) :: boolean() def validate?(proof, packet_hash, identity) do case RNS.Packet.data(proof) do # Explicit proof <<^packet_hash::binary-size(16), signature::binary-size(64)>> -> RNS.Identity.validate?(packet_hash, signature, identity) # Implicit proof <> -> RNS.Identity.validate?(packet_hash, signature, identity) # Invalid _ -> false end end end