Skip to Content
Grail Network

The slip's check character

How to make the last character of a reference, and how Grail Network checks it.

Every slip code ends with a check character. The check character catches a typing mistake before Grail Network links a card to the wrong submission. Your system makes the check character when it makes a reference. Grail Network checks it at the intake. No request to Grail Network is needed for either step.

The form of a slip code

A slip code is <partner code>:<reference>, such as GC:7K3M9QX21.

  • The partner code is your own code. It has 2 to 10 characters. Read it with GET /partner/me.
  • The reference is your own text for one submission. It has 6 to 20 characters.
  • The last character of the reference is the check character.
  • Both parts use only the 32 characters of the alphabet below. Small letters are not valid.

The regular expression of the whole code:

^[0-9A-HJKMNP-TV-Z]{2,10}:[0-9A-HJKMNP-TV-Z]{6,20}$

The alphabet

The alphabet is Crockford's base32. It has the digits and the capital letters, without I, L, O and U. A character's value is its place in this text, from 0 to 31:

0123456789ABCDEFGHJKMNPQRSTVWXYZ
Character01...9ABC...HJK...XYZ
Value01...9101112...171819...293031

Grail Network never reads O as 0, or I and L as 1. A code with one of these letters is not valid.

The rule: Luhn mod 32

The check character uses the public rule "Luhn mod N", with N = 32. It covers the whole code, the partner code too.

The values. Make a list of numbers:

  1. The length of the partner code. For GC, this is 2.
  2. The value of each character of the partner code, from left to right.
  3. The value of each character of the reference, from left to right.

The length makes a moved colon a wrong value. GC:7K... and GC7:K... give different lists.

To make the check character. Use the list without the check character.

  1. Start at the right end of the list. Multiply the last value by 2, the value before it by 1, the one before that by 2, and so on.
  2. Write each product in base 32, and add its two digits: floor(p / 32) + p % 32.
  3. Add all the results. This is the sum.
  4. The check value is (32 - sum % 32) % 32. The check character is the character with that value.

To check a code. Use the list with the check character. Start at the right end with a factor of 1, then 2, then 1, and so on. Add the digits of each product in the same way. The code is valid when the sum is a multiple of 32.

Examples

GC:7K3M9QX21 is valid. Its list of values:

PartLengthGC7K3M9QX21
Value216127193209232921

With the factors 1, 2, 1, 2, ... from the right, the sum is 128. 128 is 4 × 32, so the code is valid.

To make the check character of GC:7K3M9QX2, use the list without the last value, and the factors 2, 1, 2, ... from the right. The sum is 127. (32 - 127 % 32) % 32 is (32 - 31) % 32 = 1. The check character is 1.

GC:5H4R3P007 is valid too. Its sum with the check character is 96, which is 3 × 32.

These codes are not valid:

CodeWhy
GC:7K3M9QX22The check character is wrong.
GC:7K3M9XQ21Two neighbors changed places.
GC:7K3M9QX2II is not in the alphabet.
gc:7k3m9qx21Small letters are not in the alphabet.

Code

The same rule in JavaScript, with no library:

slip-code.js
const ALPHABET = '0123456789ABCDEFGHJKMNPQRSTVWXYZ';
const FORM = /^([0-9A-HJKMNP-TV-Z]{2,10}):([0-9A-HJKMNP-TV-Z]{6,20})$/;

function values(partnerCode, characters) {
  return [
    partnerCode.length,
    ...[...`${partnerCode}${characters}`].map((c) => ALPHABET.indexOf(c)),
  ];
}

function luhnSum(list, firstFactor) {
  let sum = 0;
  let factor = firstFactor;
  for (let place = list.length - 1; place >= 0; place -= 1) {
    const product = list[place] * factor;
    sum += Math.floor(product / 32) + (product % 32);
    factor = factor === 1 ? 2 : 1;
  }
  return sum;
}

// The check character that ends a new reference.
export function checkCharacter(partnerCode, referenceWithoutCheck) {
  return ALPHABET[(32 - (luhnSum(values(partnerCode, referenceWithoutCheck), 2) % 32)) % 32];
}

// True when a whole slip code is valid.
export function isValidSlipCode(code) {
  const match = FORM.exec(code);
  return match !== null && luhnSum(values(match[1], match[2]), 1) % 32 === 0;
}

checkCharacter('GC', '7K3M9QX2'); // '1'
isValidSlipCode('GC:7K3M9QX21'); // true
isValidSlipCode('GC:5H4R3P007'); // true
isValidSlipCode('GC:7K3M9QX22'); // false

What happens to a code that is not valid

The warehouse worker scans the slip at the intake. Grail Network checks the code and shows the worker only "Valid" or "Not valid". It never shows whose code it is.

  • A code that does not have the form gets this text: "This is not a slip code. A slip code looks like ABC:7K3M9QX2K."
  • A code with the form but a wrong check character, or with a partner code that no partner has, gets "Not valid".
  • The worker cannot start the intake of a parcel with a slip code that is not valid. No card is linked to a partner by a code that is not valid.

A mistake in one character, two neighbors that changed places, or your reference under another partner's code all fail the check.

On this page