@ngraveio/ur-uuid
Version:
Provides BC UR type for uuid.
63 lines • 1.99 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.UUID = void 0;
const uuid_1 = require("uuid");
const bc_ur_1 = require("@ngraveio/bc-ur");
const UUIDBase = (0, bc_ur_1.registryItemFactory)({
tag: 37,
URType: 'cbor-uuid',
CDDL: `
uuid-tag = #6.37(uuid-bytes)
uuid-bytes = bytes
`,
});
class UUID extends UUIDBase {
constructor(data) {
super(data);
this.data = new Uint8Array(16);
this.toString = () => (0, uuid_1.stringify)(this.data);
this.getBuffer = () => this.data;
if (data instanceof UUID) {
this.data = data.data;
}
else if (data instanceof Uint8Array) {
if (data.length !== 16) {
throw new Error('Invalid UUID byte length. Expected 16 bytes.');
}
this.data = new Uint8Array(data);
}
else if (typeof data === 'string') {
this.data = (0, uuid_1.parse)(data);
}
else {
throw new Error('Invalid input type for UUID. Expected a string or Uint8Array.');
}
}
static generate() {
return new UUID((0, uuid_1.v4)());
}
verifyInput(input) {
if (input instanceof UUID) {
// Already a UUID instance
}
else if (typeof input === 'string') {
try {
(0, uuid_1.parse)(input);
}
catch (error) {
return { valid: false, reasons: [new Error('Invalid UUID string format: ' + error.message)] };
}
}
else if (input instanceof Uint8Array) {
if (input.length !== 16) {
return { valid: false, reasons: [new Error('UUID must be exactly 16 bytes')] };
}
}
else {
return { valid: false, reasons: [new Error('Invalid data type for UUID')] };
}
return { valid: true };
}
}
exports.UUID = UUID;
//# sourceMappingURL=UUID.js.map