mirror of
https://github.com/xtr-dev/rondevu-server.git
synced 2025-12-11 19:33:25 +00:00
feat: Implement content-based offer IDs with SHA-256 hashing
- Added hash-id.ts utility for SHA-256 content hashing
- Offer IDs now generated from hash of {sdp, topics} (sorted)
- Removed peerId from hash (inferred from authentication)
- Server generates deterministic IDs for idempotent offer creation
- Updated SQLite and D1 storage implementations
- Removed optional id field from CreateOfferRequest
- Same offer content always produces same ID
🤖 Generated with [Claude Code](https://claude.com/claude-code)
Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
37
src/storage/hash-id.ts
Normal file
37
src/storage/hash-id.ts
Normal file
@@ -0,0 +1,37 @@
|
||||
/**
|
||||
* Generates a content-based offer ID using SHA-256 hash
|
||||
* Creates deterministic IDs based on offer content (sdp, topics)
|
||||
* PeerID is not included as it's inferred from authentication
|
||||
* Uses Web Crypto API for compatibility with both Node.js and Cloudflare Workers
|
||||
*
|
||||
* @param sdp - The WebRTC SDP offer
|
||||
* @param topics - Array of topic strings
|
||||
* @returns SHA-256 hash of the sanitized offer content
|
||||
*/
|
||||
export async function generateOfferHash(
|
||||
sdp: string,
|
||||
topics: string[]
|
||||
): Promise<string> {
|
||||
// Sanitize and normalize the offer content
|
||||
// Only include core offer content (not peerId - that's inferred from auth)
|
||||
const sanitizedOffer = {
|
||||
sdp,
|
||||
topics: [...topics].sort(), // Sort topics for consistency
|
||||
};
|
||||
|
||||
// Create non-prettified JSON string
|
||||
const jsonString = JSON.stringify(sanitizedOffer);
|
||||
|
||||
// Convert string to Uint8Array for hashing
|
||||
const encoder = new TextEncoder();
|
||||
const data = encoder.encode(jsonString);
|
||||
|
||||
// Generate SHA-256 hash
|
||||
const hashBuffer = await crypto.subtle.digest('SHA-256', data);
|
||||
|
||||
// Convert hash to hex string
|
||||
const hashArray = Array.from(new Uint8Array(hashBuffer));
|
||||
const hashHex = hashArray.map(b => b.toString(16).padStart(2, '0')).join('');
|
||||
|
||||
return hashHex;
|
||||
}
|
||||
Reference in New Issue
Block a user