@ixo/supamoto-bot-sdk
Version:
An SDK to easily interact with Supamoto bot db
32 lines (28 loc) • 1.27 kB
JavaScript
import crypto from 'crypto';
/**
* Generates a unique Customer ID in the format C21009802
* Uses high-precision timestamp + deterministic hashing for guaranteed uniqueness
*
* Approach:
* 1. Get high-precision timestamp (microseconds via process.hrtime.bigint())
* 2. Create deterministic hash of the precise timestamp
* 3. Convert to 8-character alphanumeric string
*
* This handles concurrent sessions by using microsecond precision (~1,000,000 unique
* values per second) making same-timestamp collisions virtually impossible.
*/
export function generateUniqueCustomerId() {
// Get high-precision timestamp in nanoseconds
const hrTime = process.hrtime.bigint();
// Convert to microseconds (divide by 1000) for reasonable precision
// This gives us ~1,000,000 unique values per second
const microseconds = hrTime / BigInt(1000);
// Create deterministic hash of the microsecond timestamp
const timestampStr = microseconds.toString();
const hash = crypto.createHash('sha256').update(timestampStr).digest('hex');
// Take first 8 characters of hash and convert to uppercase
// This gives us 8 characters from [0-9A-F] set
const hashPrefix = hash.substring(0, 8).toUpperCase();
const customerId = `C${hashPrefix}`;
return customerId;
}