expede/ethereum-status-codes

View on GitHub
lib/fission.js

Summary

Maintainability
B
5 hrs
Test Coverage
const Category = require('./fission/category');
const Reason = require('./fission/reason');
const { toHexDigit } = require('./fission/hex');

// Mappings

const combine = ({categoryId, reasonId}) => (categoryId * 0x10) + reasonId;

const split = (codeNum) => {
  if (!Number.isInteger(codeNum)) throw TypeError(`${codeNum} is not a number`);
  if (codeNum < 0 || codeNum > 255) throw RangeError(`${codeNum} is not a valid ERC-1066 code`);

  return {
    categoryId: Math.floor(codeNum / 16),
    reasonId:   codeNum % 16
  };
};

// Convenience

const humanize = ({categoryId, reasonId}) => {
  return {
    category: Category.CATEGORY_NAMES[categoryId],
    reason:   Reason.REASON_NAMES[reasonId]
  };
};

const dehumanize = ({category, reason}) => {
  return {
    categoryId: Category.CATEGORIES[category],
    reasonId: Reason.REASONS[reason]
  };
};

const categoryIdOf = (codeNum) => split(codeNum).categoryId;
const categoryOf = (codeNum) => Category.CATEGORY_NAMES[categoryIdOf(codeNum)];

const reasonIdOf = (codeNum) => split(codeNum).reasonId;
const reasonOf = (codeNum) => Reason.REASON_NAMES[reasonIdOf(codeNum)];

// Formatters

const toNumber = ({category, reason}) => {
  const categoryId = Category.CATEGORIES[category];
  if (categoryId === undefined) throw Category.badLookup(category);

  const reasonId = Reason.REASON[reason];
  if (reasonId === undefined) throw Reason.badLookup(reason);

  return combine({categoryId, reasonId});
};

const toHexString = ({categoryId, reasonId}) =>
  `0x${toHexDigit(categoryId)}${toHexDigit(reasonId)}`;

const hexifyCode = (codeNum) => {
  const {categoryId, reasonId} = split(codeNum);
  return `0x${toHexDigit(categoryId)}${toHexDigit(reasonId)}`;
};

// Enum

const STATUSES = Object.freeze({
  // 0x0*
  FAILURE: 0x00,
  SUCCESS: 0x01,
  AWATING_OTHERS: 0x02,
  ACCEPTED: 0x03,
  LOWER_LIMIT: 0x04,
  RECIEVER_ACTION_REQUESTED: 0x05,
  UPPER_LIMIT: 0x06,
  RESERVEDx07: 0x07,
  INAPPLICABLE: 0x08,
  RESERVEDx09: 0x09,
  RESERVEDx0A: 0xA0,
  RESERVEDx0B: 0x0B,
  RESERVEDx0C: 0x0C,
  RESERVEDx0D: 0x0D,
  RESERVEDx0E: 0x0E,
  INFORMATIONAL: 0x0F,

  // 0x1*
  DISALLOWED_STOP: 0x10,
  ALLOWED_GO: 0x11,
  AWAITING_OTHERS_PERMISSION: 0x12,
  PERMISSION_REQUESTED: 0x13,
  TOO_OPEN_INSECURE: 0x14,
  NEEDS_YOUR_PERMISSION_REQUEST_FOR_CONTINUATION: 0x15,
  REVOKED: 0x16,
  RESERVEDx17: 0x17,
  NOT_APPLICATABLE_TO_CURRENT_STATE: 0x18,
  RESERVEDx19: 0x19,
  RESERVEDx1A: 0x1A,
  RESERVEDx1B: 0x1B,
  RESERVEDx1C: 0x1C,
  RESERVEDx1D: 0x1D,
  RESERVEDx1E: 0x1E,
  PERMISSION_DETAILS_CONTROL_CONDITIONS: 0x1F,

  // 0x2*
  NOT_FOUND_UNEQUAL_OUT_OF_RANGE: 0x20,
  FOUND_EQUAL_IN_RANGE: 0x21,
  AWAITING_MATCH: 0x22,
  MATCH_REQUEST_SENT: 0x23,
  BELOW_RANGE_UNDERFLOW: 0x24,
  REQUEST_FOR_MATCH: 0x25,
  ABOVE_RANGE_OVERFLOW: 0x26,
  RESERVEDx27: 0x27,
  DUPLICATE_CONFLICT_COLLISION: 0x28,
  RESERVEDx29: 0x29,
  RESERVEDx2A: 0x2A,
  RESERVEDx2B: 0x2B,
  RESERVEDx2C: 0x2C,
  RESERVEDx2D: 0x2D,
  RESERVEDx2E: 0x2E,
  MATCHING_INFORMATION: 0x2F,

  // 0x3*
  SENDER_DISAGREES_NAY: 0x30,
  SENDER_AGREES_YEA: 0x31,
  AWAITING_RATIFICATION: 0x32,
  OFFER_SENT_VOTED: 0x33,
  QUORUM_NOT_REACHED: 0x34,
  RECEIVERS_RATIFICATION_REQUESTED: 0x35,
  OFFER_OR_VOTE_LIMIT_REACHED: 0x36,
  RESERVEDx37: 0x37,
  ALREADY_VOTED: 0x38,
  RESERVEDx39: 0x39,
  RESERVEDx3A: 0x3A,
  RESERVEDx3B: 0x3B,
  RESERVEDx3C: 0x3C,
  RESERVEDx3D: 0x3D,
  RESERVEDx3E: 0x3E,
  NEGOTIATION_RULES_PARTICIPATION_INFORMATION: 0x3F,

  // 0x4*
  UNAVAILABLE: 0x40,
  AVAILABLE: 0x41,
  PAUSED: 0x42,
  QUEUED: 0x43,
  NOT_AVAILABLE_YET: 0x44,
  AWAITING_YOUR_AVAILABILITY: 0x45,
  EXPIRED: 0x46,
  RESERVEDx47: 0x47,
  ALREADY_DONE: 0x48,
  RESERVEDx49: 0x49,
  RESERVEDx4A: 0x4A,
  RESERVEDx4B: 0x4B,
  RESERVEDx4C: 0x4C,
  RESERVEDx4D: 0x4D,
  RESERVEDx4E: 0x4E,
  AVAILABILITY_RULES_INFORMATION: 0x4F,

  // 0x5*
  TRANSFER_FAILED: 0x50,
  TRANSFER_SUCCESSFUL: 0x51,
  AWAITING_PAYMENT_FROM_OTHERS: 0x52,
  HOLD_ESCROW: 0x53,
  INSUFFICIENT_FUNDS: 0x54,
  FUNDS_REQUESTED: 0x55,
  TRANSFER_VOLUME_EXCEEDED: 0x56,
  RESERVEDx57: 0x57,
  FUNDS_NOT_REQUIRED: 0x58,
  RESERVEDx59: 0x59,
  RESERVEDx5A: 0x5A,
  RESERVEDx5B: 0x5B,
  RESERVEDx5C: 0x5C,
  RESERVEDx5D: 0x5D,
  RESERVEDx5E: 0x5E,
  FINANCIAL_INFORMATION: 0x5F,

  // 0x6*
  RESERVEDx60: 0x60,
  RESERVEDx61: 0x61,
  RESERVEDx62: 0x62,
  RESERVEDx63: 0x63,
  RESERVEDx64: 0x64,
  RESERVEDx65: 0x65,
  RESERVEDx66: 0x66,
  RESERVEDx67: 0x67,
  RESERVEDx68: 0x68,
  RESERVEDx69: 0x69,
  RESERVEDx6A: 0x6A,
  RESERVEDx6B: 0x6B,
  RESERVEDx6C: 0x6C,
  RESERVEDx6D: 0x6D,
  RESERVEDx6E: 0x6E,
  RESERVEDx6F: 0x6F,

  // 0x7*
  RESERVEDx70: 0x70,
  RESERVEDx71: 0x71,
  RESERVEDx72: 0x72,
  RESERVEDx73: 0x73,
  RESERVEDx74: 0x74,
  RESERVEDx75: 0x75,
  RESERVEDx76: 0x76,
  RESERVEDx77: 0x77,
  RESERVEDx78: 0x78,
  RESERVEDx79: 0x79,
  RESERVEDx7A: 0x7A,
  RESERVEDx7B: 0x7B,
  RESERVEDx7C: 0x7C,
  RESERVEDx7D: 0x7D,
  RESERVEDx7E: 0x7E,
  RESERVEDx7F: 0x7F,

  // 0x8*
  RESERVEDx80: 0x80,
  RESERVEDx81: 0x81,
  RESERVEDx82: 0x82,
  RESERVEDx83: 0x83,
  RESERVEDx84: 0x84,
  RESERVEDx85: 0x85,
  RESERVEDx86: 0x86,
  RESERVEDx87: 0x87,
  RESERVEDx88: 0x88,
  RESERVEDx89: 0x89,
  RESERVEDx8A: 0x8A,
  RESERVEDx8B: 0x8B,
  RESERVEDx8C: 0x8C,
  RESERVEDx8D: 0x8D,
  RESERVEDx8E: 0x8E,
  RESERVEDx8F: 0x8F,

  // 0x9*
  RESERVEDx90: 0x90,
  RESERVEDx91: 0x91,
  RESERVEDx92: 0x92,
  RESERVEDx93: 0x93,
  RESERVEDx94: 0x94,
  RESERVEDx95: 0x95,
  RESERVEDx96: 0x96,
  RESERVEDx97: 0x97,
  RESERVEDx98: 0x98,
  RESERVEDx99: 0x99,
  RESERVEDx9A: 0x9A,
  RESERVEDx9B: 0x9B,
  RESERVEDx9C: 0x9C,
  RESERVEDx9D: 0x9D,
  RESERVEDx9E: 0x9E,
  RESERVEDx9F: 0x9F,

  // 0xA*
  APPLICATION_SPECIFIC_FAILURE: 0xA0,
  APPLICATION_SPECIFIC_SUCCESS: 0xA1,
  APPLICATION_SPECIFIC_AWATING_OTHERS: 0xA2,
  APPLICATION_SPECIFIC_ACCEPTED: 0xA3,
  APPLICATION_SPECIFIC_LOWER_LIMIT: 0xA4,
  APPLICATION_SPECIFIC_RECIEVER_ACTION_REQUESTED: 0xA5,
  APPLICATION_SPECIFIC_UPPER_LIMIT: 0xA6,
  RESERVEDxA7: 0xA7,
  APPLICATION_SPECIFIC_INAPPLICABLE: 0xA8,
  RESERVEDxA9: 0xA9,
  RESERVEDxAA: 0xAA,
  RESERVEDxAB: 0xAB,
  RESERVEDxAC: 0xAC,
  RESERVEDxAD: 0xAD,
  RESERVEDxAE: 0xAE,
  APPLICATION_SPECIFIC_INFORMATIONAL: 0xAF,

  // 0xB*
  RESERVEDxB0: 0xB0,
  RESERVEDxB1: 0xB1,
  RESERVEDxB2: 0xB2,
  RESERVEDxB3: 0xB3,
  RESERVEDxB4: 0xB4,
  RESERVEDxB5: 0xB5,
  RESERVEDxB6: 0xB6,
  RESERVEDxB7: 0xB7,
  RESERVEDxB8: 0xB8,
  RESERVEDxB9: 0xB9,
  RESERVEDxBA: 0xBA,
  RESERVEDxBB: 0xBB,
  RESERVEDxBC: 0xBC,
  RESERVEDxBD: 0xBD,
  RESERVEDxBE: 0xBE,
  RESERVEDxBF: 0xBF,

  // 0xC*
  RESERVEDxC0: 0xC0,
  RESERVEDxC1: 0xC1,
  RESERVEDxC2: 0xC2,
  RESERVEDxC3: 0xC3,
  RESERVEDxC4: 0xC4,
  RESERVEDxC5: 0xC5,
  RESERVEDxC6: 0xC6,
  RESERVEDxC7: 0xC7,
  RESERVEDxC8: 0xC8,
  RESERVEDxC9: 0xC9,
  RESERVEDxCA: 0xCA,
  RESERVEDxCB: 0xCB,
  RESERVEDxCC: 0xCC,
  RESERVEDxCD: 0xCD,
  RESERVEDxCE: 0xCE,
  RESERVEDxCF: 0xCF,

  // 0xD*
  RESERVEDxD0: 0xD0,
  RESERVEDxD1: 0xD1,
  RESERVEDxD2: 0xD2,
  RESERVEDxD3: 0xD3,
  RESERVEDxD4: 0xD4,
  RESERVEDxD5: 0xD5,
  RESERVEDxD6: 0xD6,
  RESERVEDxD7: 0xD7,
  RESERVEDxD8: 0xD8,
  RESERVEDxD9: 0xD9,
  RESERVEDxDA: 0xDA,
  RESERVEDxDB: 0xDB,
  RESERVEDxDC: 0xDC,
  RESERVEDxDD: 0xDD,
  RESERVEDxDE: 0xDE,
  RESERVEDxDF: 0xDF,

  // 0xE*
  DECRYPT_FAILURE: 0xE0,
  DECRYPT_SUCCESS: 0xE1,
  AWAITING_OTHER_SIGNATURES_OR_KEYS: 0xE2,
  SIGNED: 0xE3,
  UNSIGNED_UNTRUSTED: 0xE4,
  SIGNATURE_REQUIRED: 0xE5,
  KNOWN_TO_BE_COMPROMISED: 0xE6,
  RESERVEDxE7: 0xE7,
  ALREADY_SIGNED_NOT_ENCRYPTED: 0xE8,
  RESERVEDxE9: 0xE9,
  RESERVEDxEA: 0xEA,
  RESERVEDxEB: 0xEB,
  RESERVEDxEC: 0xEC,
  RESERVEDxED: 0xED,
  RESERVEDxEE: 0xEE,
  CRYPTOGRAPHY_ID_PROOF_METADATA: 0xEF,

  // 0xF*
  OFF_CHAIN_FAILURE: 0xF0,
  OFF_CHAIN_SUCCESS: 0xF1,
  AWATING_OFF_CHAIN_PROCESS: 0xF2,
  OFF_CHAIN_PROCESS_STARTED: 0xF3,
  OFF_CHAIN_SERVICE_UNREACHABLE: 0xF4,
  OFF_CHAIN_ACTION_REQUIRED: 0xF5,
  OFF_CHAIN_EXPIRY_LIMIT_REACHED: 0xF6,
  RESERVEDxF7: 0xF7,
  DUPLICATE_OFF_CHAIN_REQUEST: 0xF8,
  RESERVEDxF9: 0xF9,
  RESERVEDxFA: 0xFA,
  RESERVEDxFB: 0xFB,
  RESERVEDxFC: 0xFC,
  RESERVEDxFD: 0xFD,
  RESERVEDxFE: 0xFE,
  OFF_CHAIN_INFORMATION: 0xFF
});


module.exports = {
  CATEGORIES: Category.CATEGORIES,
  CATEGORY_NAMES: Category.CATEGORY_NAMES,
  REASONS: Reason.REASONS,
  REASON_NAMES: Reason.REASON_NAMES,
  STATUSES,
  categoryOf,
  categoryIdOf,
  combine,
  dehumanize,
  hexifyCode,
  humanize,
  reasonOf,
  reasonIdOf,
  split,
  toHexString,
  toNumber
};