diff --git a/src/pds.js b/src/pds.js index 4978ded..65fb19e 100644 --- a/src/pds.js +++ b/src/pds.js @@ -1,3 +1,12 @@ +// === CONSTANTS === +// CBOR primitive markers (RFC 8949) +const CBOR_FALSE = 0xf4 +const CBOR_TRUE = 0xf5 +const CBOR_NULL = 0xf6 + +// DAG-CBOR CID link tag +const CBOR_TAG_CID = 42 + // === CID WRAPPER === // Explicit CID type for DAG-CBOR encoding (avoids fragile heuristic detection) @@ -18,11 +27,11 @@ export function cborEncode(value) { function encode(val) { if (val === null) { - parts.push(0xf6) // null + parts.push(CBOR_NULL) } else if (val === true) { - parts.push(0xf5) // true + parts.push(CBOR_TRUE) } else if (val === false) { - parts.push(0xf4) // false + parts.push(CBOR_FALSE) } else if (typeof val === 'number') { encodeInteger(val) } else if (typeof val === 'string') { @@ -82,11 +91,11 @@ function cborEncodeDagCbor(value) { function encode(val) { if (val === null) { - parts.push(0xf6) // null + parts.push(CBOR_NULL) } else if (val === true) { - parts.push(0xf5) // true + parts.push(CBOR_TRUE) } else if (val === false) { - parts.push(0xf4) // false + parts.push(CBOR_FALSE) } else if (typeof val === 'number') { if (Number.isInteger(val) && val >= 0) { encodeHead(0, val) @@ -99,7 +108,7 @@ function cborEncodeDagCbor(value) { parts.push(...bytes) } else if (val instanceof CID) { // CID - encode with CBOR tag 42 + 0x00 prefix - parts.push(0xd8, 42) // tag(42) + parts.push(0xd8, CBOR_TAG_CID) encodeHead(2, val.bytes.length + 1) // +1 for 0x00 prefix parts.push(0x00) // multibase identity prefix parts.push(...val.bytes) @@ -569,12 +578,12 @@ function cborEncodeMstNode(node) { function encode(val) { if (val === null || val === undefined) { - parts.push(0xf6) // null + parts.push(CBOR_NULL) } else if (typeof val === 'number') { encodeHead(0, val) // unsigned int } else if (val instanceof CID) { // CID - encode with CBOR tag 42 + 0x00 prefix (DAG-CBOR CID link) - parts.push(0xd8, 42) // tag 42 + parts.push(0xd8, CBOR_TAG_CID) encodeHead(2, val.bytes.length + 1) // +1 for 0x00 prefix parts.push(0x00) // multibase identity prefix parts.push(...val.bytes)