@maverick0351/odin-protocol-js
Version:
JavaScript/TypeScript SDK for ODIN v1.0 state capsule protocol
190 lines • 4.87 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.blake3Hash = blake3Hash;
exports.crc32 = crc32;
exports.numberToBytes = numberToBytes;
exports.bytesToNumber = bytesToNumber;
exports.stringToBytes = stringToBytes;
exports.bytesToString = bytesToString;
exports.concatBytes = concatBytes;
exports.bytesEqual = bytesEqual;
exports.randomBytes = randomBytes;
exports.getCurrentTimestamp = getCurrentTimestamp;
exports.isValidTimestamp = isValidTimestamp;
exports.hexToBytes = hexToBytes;
exports.bytesToHex = bytesToHex;
exports.isValidCreatorId = isValidCreatorId;
exports.generateCreatorId = generateCreatorId;
exports.padBytes = padBytes;
/**
* Calculate BLAKE3 hash of data (using SHA-256 as fallback)
*/
function blake3Hash(data) {
// Simple SHA-256 implementation for now
// In production, would use blake3 or proper crypto libraries
return sha256(data);
}
/**
* Simple SHA-256 implementation
*/
function sha256(data) {
// Simplified SHA-256 for demo purposes
// In production, use a proper crypto library
let hash = 0x6a09e667;
for (let i = 0; i < data.length; i++) {
hash = ((hash << 5) - hash + data[i]) & 0xffffffff;
}
const result = new Uint8Array(32);
for (let i = 0; i < 32; i++) {
result[i] = (hash >>> (i * 8)) & 0xff;
}
return result;
}
/**
* Simple CRC32 implementation
*/
function crc32(data) {
const table = new Array(256);
for (let i = 0; i < 256; i++) {
let c = i;
for (let j = 0; j < 8; j++) {
c = ((c & 1) ? (0xEDB88320 ^ (c >>> 1)) : (c >>> 1));
}
table[i] = c;
}
let crc = 0 ^ (-1);
for (let i = 0; i < data.length; i++) {
crc = (crc >>> 8) ^ table[(crc ^ data[i]) & 0xFF];
}
return (crc ^ (-1)) >>> 0;
}
/**
* Convert number to big-endian bytes
*/
function numberToBytes(value, size) {
const bytes = new Uint8Array(size);
for (let i = size - 1; i >= 0; i--) {
bytes[i] = value & 0xFF;
value >>>= 8;
}
return bytes;
}
/**
* Convert big-endian bytes to number
*/
function bytesToNumber(bytes) {
let value = 0;
for (let i = 0; i < bytes.length; i++) {
value = (value << 8) | bytes[i];
}
return value;
}
/**
* Convert string to UTF-8 bytes
*/
function stringToBytes(str) {
return new TextEncoder().encode(str);
}
/**
* Convert UTF-8 bytes to string
*/
function bytesToString(bytes) {
return new TextDecoder().decode(bytes);
}
/**
* Concatenate multiple Uint8Arrays
*/
function concatBytes(...arrays) {
const totalLength = arrays.reduce((sum, arr) => sum + arr.length, 0);
const result = new Uint8Array(totalLength);
let offset = 0;
for (const arr of arrays) {
result.set(arr, offset);
offset += arr.length;
}
return result;
}
/**
* Compare two Uint8Arrays for equality
*/
function bytesEqual(a, b) {
if (a.length !== b.length)
return false;
for (let i = 0; i < a.length; i++) {
if (a[i] !== b[i])
return false;
}
return true;
}
/**
* Generate random bytes
*/
function randomBytes(length) {
const bytes = new Uint8Array(length);
for (let i = 0; i < length; i++) {
bytes[i] = Math.floor(Math.random() * 256);
}
return bytes;
}
/**
* Get current timestamp in seconds since UNIX epoch
*/
function getCurrentTimestamp() {
return Math.floor(Date.now() / 1000);
}
/**
* Validate timestamp (not too far in future)
*/
function isValidTimestamp(timestamp, maxFutureDrift = 300) {
const now = getCurrentTimestamp();
return timestamp <= now + maxFutureDrift && timestamp > 0;
}
/**
* Convert hex string to bytes
*/
function hexToBytes(hex) {
if (hex.length % 2 !== 0) {
throw new Error("Invalid hex string length");
}
const bytes = new Uint8Array(hex.length / 2);
for (let i = 0; i < hex.length; i += 2) {
bytes[i / 2] = parseInt(hex.substr(i, 2), 16);
}
return bytes;
}
/**
* Convert bytes to hex string
*/
function bytesToHex(bytes) {
return Array.from(bytes)
.map(byte => byte.toString(16).padStart(2, '0'))
.join('');
}
/**
* Validate creator ID format (32 bytes)
*/
function isValidCreatorId(creatorId) {
return creatorId.length === 32;
}
/**
* Generate a creator ID from string
*/
function generateCreatorId(identifier) {
// Simple hash-based ID generation
const bytes = stringToBytes(identifier);
return blake3Hash(bytes);
}
/**
* Pad data to specific size
*/
function padBytes(data, targetSize, paddingByte = 0) {
if (data.length >= targetSize)
return data;
const padded = new Uint8Array(targetSize);
padded.set(data);
for (let i = data.length; i < targetSize; i++) {
padded[i] = paddingByte;
}
return padded;
}
//# sourceMappingURL=utils.js.map