UNPKG

eip-712

Version:

Tiny library with utility functions that can help with signing and verifying EIP-712 based messages

90 lines (75 loc) 3.12 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.isValidType = exports.EIP_712_STRICT_TYPED_DATA_TYPE = exports.EIP_712_TYPED_DATA_TYPE = exports.EIP_712_DOMAIN_TYPE = exports.EIP_712_TYPE = exports.STATIC_TYPES = exports.NUMBER_REGEX = exports.BYTES_REGEX = exports.ARRAY_REGEX = exports.TYPE_REGEX = void 0; var _superstruct = require("superstruct"); const TYPE_REGEX = /^\w+/; exports.TYPE_REGEX = TYPE_REGEX; const ARRAY_REGEX = /^(.*)\[([0-9]*?)]$/; exports.ARRAY_REGEX = ARRAY_REGEX; const BYTES_REGEX = /^bytes([0-9]{1,2})$/; exports.BYTES_REGEX = BYTES_REGEX; const NUMBER_REGEX = /^u?int([0-9]{0,3})$/; exports.NUMBER_REGEX = NUMBER_REGEX; const STATIC_TYPES = ['address', 'bool', 'bytes', 'string']; exports.STATIC_TYPES = STATIC_TYPES; const TYPE = (0, _superstruct.refine)((0, _superstruct.string)(), 'Type', (type, context) => { return isValidType(context.branch[0].types, type); }); const EIP_712_TYPE = (0, _superstruct.object)({ name: (0, _superstruct.string)(), type: TYPE }); exports.EIP_712_TYPE = EIP_712_TYPE; const EIP_712_DOMAIN_TYPE = (0, _superstruct.object)({ name: (0, _superstruct.optional)((0, _superstruct.string)()), version: (0, _superstruct.optional)((0, _superstruct.string)()), chainId: (0, _superstruct.optional)((0, _superstruct.union)([(0, _superstruct.string)(), (0, _superstruct.number)()])), verifyingContract: (0, _superstruct.optional)((0, _superstruct.pattern)((0, _superstruct.string)(), /^0x[0-9a-z]{40}$/i)), salt: (0, _superstruct.optional)((0, _superstruct.union)([(0, _superstruct.array)((0, _superstruct.number)()), (0, _superstruct.pattern)((0, _superstruct.string)(), /^0x[0-9a-z]{64}$/i)])) }); exports.EIP_712_DOMAIN_TYPE = EIP_712_DOMAIN_TYPE; const EIP_712_TYPED_DATA_TYPE = (0, _superstruct.object)({ types: (0, _superstruct.record)((0, _superstruct.string)(), (0, _superstruct.array)(EIP_712_TYPE)), primaryType: (0, _superstruct.string)(), domain: (0, _superstruct.object)(), message: (0, _superstruct.object)() }); exports.EIP_712_TYPED_DATA_TYPE = EIP_712_TYPED_DATA_TYPE; const EIP_712_STRICT_TYPED_DATA_TYPE = (0, _superstruct.assign)(EIP_712_TYPED_DATA_TYPE, (0, _superstruct.object)({ domain: EIP_712_DOMAIN_TYPE })); exports.EIP_712_STRICT_TYPED_DATA_TYPE = EIP_712_STRICT_TYPED_DATA_TYPE; const isValidType = (types, type) => { if (STATIC_TYPES.includes(type)) { return true; } if (types[type]) { return true; } if (type.match(ARRAY_REGEX)) { const match = type.match(TYPE_REGEX); if (match) { const innerType = match[0]; return isValidType(types, innerType); } } const bytesMatch = type.match(BYTES_REGEX); if (bytesMatch) { const length = Number(bytesMatch[1]); if (length >= 1 && length <= 32) { return true; } } const numberMatch = type.match(NUMBER_REGEX); if (numberMatch) { const length = Number(numberMatch[1]); if (length >= 8 && length <= 256 && length % 8 === 0) { return true; } } return false; }; exports.isValidType = isValidType; //# sourceMappingURL=types.js.map