Resolve "WIP: UTXO Selection and Storage"

This commit is contained in:
Jim Hamill
2025-01-11 08:39:24 +00:00
parent f934216d68
commit 33fd9bac4d
156 changed files with 13304 additions and 4595 deletions
+31
View File
@@ -0,0 +1,31 @@
export function generateUUIDV7(customTimestamp?: bigint) {
// Step 1: Get the current timestamp in milliseconds
const timestamp = customTimestamp ?? BigInt(Date.now()); // Use BigInt to handle 48-bit values
// Step 2: Convert the timestamp to a 48-bit hexadecimal string
const timestampHex = timestamp.toString(16).padStart(12, '0'); // Ensure 12 hex characters
// Step 3: Generate 74 bits (19 hex characters) of random data
const randomHex = crypto
.getRandomValues(new Uint8Array(10))
.map((b) => Number(b.toString(16).padStart(2, '0')))
.join(''); // 10 bytes = 20 hex characters
// Step 4: Combine fields to form the UUID
const version = '7'; // Version 7 (0111)
const variant = '8'; // Variant 1 (10xx) is "8", "9", "a", or "b"
return (
`${timestampHex.substring(0, 8)}-${timestampHex.substring(8, 12)}` + // Timestamp (48 bits)
`-${version}${randomHex.substring(0, 3)}` + // Version (4 bits) + 12 random bits
`-${variant}${randomHex.substring(3, 6)}` + // Variant (2 bits) + 14 random bits
`-${randomHex.substring(6)}` // Remaining 48 random bits
);
}
/**
* Extract the timestamp from a UUID v7
*/
export function getTimestampFromUUID(uuid: string): bigint {
return BigInt(parseInt(uuid.slice(0, 8), 16));
}