@confluxfans/cip-23
Version:
Tiny library with utility functions that can help with signing and verifying CIP-23 based messages
88 lines (73 loc) • 2.96 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.isValidType = exports.CIP_23_TYPED_DATA_TYPE = exports.CIP_23_DOMAIN_TYPE = exports.CIP_23_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.refinement)((0, _superstruct.string)(), 'Type', (type, context) => {
return isValidType(context.branch[0].types, type);
});
const CIP_23_TYPE = (0, _superstruct.object)({
name: (0, _superstruct.string)(),
type: TYPE
});
exports.CIP_23_TYPE = CIP_23_TYPE;
const CIP_23_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.CIP_23_DOMAIN_TYPE = CIP_23_DOMAIN_TYPE;
const CIP_23_TYPED_DATA_TYPE = (0, _superstruct.object)({
types: (0, _superstruct.intersection)([(0, _superstruct.type)({
CIP23Domain: (0, _superstruct.array)(CIP_23_TYPE)
}), (0, _superstruct.record)((0, _superstruct.string)(), (0, _superstruct.array)(CIP_23_TYPE))]),
primaryType: (0, _superstruct.string)(),
domain: CIP_23_DOMAIN_TYPE,
message: (0, _superstruct.object)()
});
exports.CIP_23_TYPED_DATA_TYPE = CIP_23_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