SefazNfe.AccessKey (sefaz_nfe v0.5.0)

Copy Markdown View Source

The 44 character access key (chNFe) of an NF-e or NFC-e.

Layout, fixed by the MOC: cUF(2) AAMM(4) emitter CNPJ(14) mod(2) serie(3) nNF(9) tpEmis(1) cNF(8) cDV(1).

The CT-e (model 57), CT-e OS (67) and MDF-e (58) keys have the same 44 characters and the same check digit (CTe / MDFe Id prefix aside): nNF is then nCT or nMDF and cNF is cCT or cMDF, so the fields keep the names :n_nf and :c_nf. NFePHP's MakeCTe, MakeCTeOS and Make (MDF-e) take the code from the caller and rebuild the key with the same Keys::build, so the random code follows the same cNF rules.

The rules mirror NFePHP sped-common NFePHP\Common\Keys:

  • Emitter: a CNPJ is 14 characters, alphanumeric in the first 12 (NT 2025.001) and numeric in the two check digits. A CPF (11 digits) is left-padded with zeros to 14, exactly as Keys::build does.
  • cDV (Keys::verifyingDigit): the sum of (ASCII code - 48) * weight, weights 2 to 9 cycling from the right. So 0..9 weigh as themselves and A is 17. The digit is 0 when the remainder modulo 11 is 0 or 1, else 11 - remainder.
  • cNF (Keys::random and Keys::cNFIsValid, NT 2019.001, cStat 897): 8 digits, never equal to nNF (compared as integers) and never one of 00000000 to 99999999 (all digits equal) nor the ascending runs 12345678, 23456789, 34567890, 45678901, 56789012, 67890123, 78901234, 89012345, 90123456, 01234567.
  • valid? (Keys::isValid): 44 characters matching ^[0-9]{6}[A-Z0-9]{12}[0-9]{26}$ and a correct cDV.

Summary

Functions

Builds the key from fields, each an integer or a string

The check digit of the first 43 characters of a key.

Whether c_nf equals n_nf as integers (the cStat 897 case).

A random cNF that SEFAZ accepts: 8 digits, not equal to n_nf and not one of the trivial sequences (see the module doc).

Whether key is a well-formed 44 character key with a correct check digit.

Types

field()

@type field() ::
  :c_uf | :dh_emi | :cnpj | :cpf | :mod | :serie | :n_nf | :tp_emis | :c_nf

fields()

@type fields() :: %{optional(field()) => term()}

Functions

build(fields)

@spec build(fields()) :: {:ok, String.t()} | {:error, {atom(), term()}}

Builds the key from fields, each an integer or a string:

  • :c_uf, :mod, :serie, :n_nf, :tp_emis - numbers, zero-padded;
  • :dh_emi - "YYYY-MM-DDThh:mm:ss+hh:mm"; only year and month are used;
  • :cnpj or :cpf - the emitter;
  • :c_nf - 8 digits; random (see random_c_nf/2) when absent.

The check digit is always computed. {:error, {field, reason}} names the first field that is missing or out of range.

dv(key43)

@spec dv(String.t()) :: 0..9

The check digit of the first 43 characters of a key.

equals_n_nf?(c_nf, n_nf)

@spec equals_n_nf?(String.t(), pos_integer() | String.t()) :: boolean()

Whether c_nf equals n_nf as integers (the cStat 897 case).

random_c_nf(n_nf, opts \\ [])

@spec random_c_nf(pos_integer() | String.t(), keyword()) :: String.t()

A random cNF that SEFAZ accepts: 8 digits, not equal to n_nf and not one of the trivial sequences (see the module doc).

Option :random_bytes is a function from a byte count to that many bytes (default :crypto.strong_rand_bytes/1); tests use it to drive the rejection.

valid?(key)

@spec valid?(term()) :: boolean()

Whether key is a well-formed 44 character key with a correct check digit.