UNPKG

antelope-webauthn

Version:

A WebAuthn.io crypto utility for generating signatures, creating public keys, and verifying them, designed for Antelope-based blockchains such as Vaulta, WAX, and other related platforms. This package provides convenient tools to handle key pair generatio

154 lines (153 loc) 5.7 kB
const POW_2_32 = 4294967296, POW_2_53 = 9007199254740992; export function encodeCBOR(value) { var data = new ArrayBuffer(256); var dataView = new DataView(data); var lastLength; var offset = 0; function prepareWrite(length) { var newByteLength = data.byteLength; var requiredLength = offset + length; while (newByteLength < requiredLength) newByteLength <<= 1; if (newByteLength !== data.byteLength) { var oldDataView = dataView; data = new ArrayBuffer(newByteLength); dataView = new DataView(data); var uint32count = (offset + 3) >> 2; for (var i = 0; i < uint32count; ++i) dataView.setUint32(i << 2, oldDataView.getUint32(i << 2)); } lastLength = length; return dataView; } function commitWrite(a) { offset += lastLength; } function writeFloat64(value) { commitWrite(prepareWrite(8).setFloat64(offset, value)); } function writeUint8(value) { commitWrite(prepareWrite(1).setUint8(offset, value)); } function writeUint8Array(value) { var dataView = prepareWrite(value.length); for (var i = 0; i < value.length; ++i) dataView.setUint8(offset + i, value[i]); commitWrite(); } function writeUint16(value) { commitWrite(prepareWrite(2).setUint16(offset, value)); } function writeUint32(value) { commitWrite(prepareWrite(4).setUint32(offset, value)); } function writeUint64(value) { var low = value % POW_2_32; var high = (value - low) / POW_2_32; var dataView = prepareWrite(8); dataView.setUint32(offset, high); dataView.setUint32(offset + 4, low); commitWrite(); } function writeTypeAndLength(type, length) { if (length < 24) { writeUint8((type << 5) | length); } else if (length < 0x100) { writeUint8((type << 5) | 24); writeUint8(length); } else if (length < 0x10000) { writeUint8((type << 5) | 25); writeUint16(length); } else if (length < 0x100000000) { writeUint8((type << 5) | 26); writeUint32(length); } else { writeUint8((type << 5) | 27); writeUint64(length); } } function encodeItem(value) { var i; if (value === false) return writeUint8(0xf4); if (value === true) return writeUint8(0xf5); if (value === null) return writeUint8(0xf6); if (value === undefined) return writeUint8(0xf7); switch (typeof value) { case "number": if (Math.floor(value) === value) { if (0 <= value && value <= POW_2_53) return writeTypeAndLength(0, value); if (-POW_2_53 <= value && value < 0) return writeTypeAndLength(1, -(value + 1)); } writeUint8(0xfb); return writeFloat64(value); case "string": var utf8data = []; for (i = 0; i < value.length; ++i) { var charCode = value.charCodeAt(i); if (charCode < 0x80) { utf8data.push(charCode); } else if (charCode < 0x800) { utf8data.push(0xc0 | (charCode >> 6)); utf8data.push(0x80 | (charCode & 0x3f)); } else if (charCode < 0xd800) { utf8data.push(0xe0 | (charCode >> 12)); utf8data.push(0x80 | ((charCode >> 6) & 0x3f)); utf8data.push(0x80 | (charCode & 0x3f)); } else { charCode = (charCode & 0x3ff) << 10; charCode |= value.charCodeAt(++i) & 0x3ff; charCode += 0x10000; utf8data.push(0xf0 | (charCode >> 18)); utf8data.push(0x80 | ((charCode >> 12) & 0x3f)); utf8data.push(0x80 | ((charCode >> 6) & 0x3f)); utf8data.push(0x80 | (charCode & 0x3f)); } } writeTypeAndLength(3, utf8data.length); return writeUint8Array(utf8data); default: var length; if (Array.isArray(value)) { length = value.length; writeTypeAndLength(4, length); for (i = 0; i < length; ++i) encodeItem(value[i]); } else if (value instanceof Uint8Array) { writeTypeAndLength(2, value.length); writeUint8Array(value); } else { var keys = Object.keys(value); length = keys.length; writeTypeAndLength(5, length); for (i = 0; i < length; ++i) { var key = keys[i]; encodeItem(key); encodeItem(value[key]); } } } } encodeItem(value); if ("slice" in data) return data.slice(0, offset); var ret = new ArrayBuffer(offset); var retView = new DataView(ret); for (var i = 0; i < offset; ++i) retView.setUint8(i, dataView.getUint8(i)); return ret; }