INNER CODE UNIT · TypeScript
encodeCanonical
backbone-hq/minibone · src/canonical.ts:3
export function encodeCanonical(input: Serializable | Serializable[]): Uint8Array {
const encoder = new TextEncoder()
if (input === null) {
return new Uint8Array([0x00])
}
if (typeof input === 'boolean') {
return new Uint8Array([0x01, input ? 0x01 : 0x00])
}
if (typeof input === 'number') {
if (!Number.isInteger(input) || input < 0 || input > 4294967295) {
throw new Error('Input is not a 32-bit unsigned integer')
}
const header = new Uint8Array([0x02])
const number = new Uint8Array(new Uint32Array([input]).buffer)