Implement crypto adapter pattern for platform independence

Adds CryptoAdapter interface with WebCryptoAdapter (browser) and
NodeCryptoAdapter (Node.js 19+) implementations.

Changes:
- Created crypto-adapter.ts interface
- Created web-crypto-adapter.ts for browser environments
- Created node-crypto-adapter.ts for Node.js environments
- Updated RondevuAPI to accept optional CryptoAdapter
- Updated Rondevu class to pass crypto adapter through
- Exported adapters and types in index.ts
- Updated README with platform support documentation
- Bumped version to 0.15.0

This allows the client library to work in both browser and Node.js
environments by providing platform-specific crypto implementations.

Example usage in Node.js:
  import { Rondevu, NodeCryptoAdapter } from '@xtr-dev/rondevu-client'

  const rondevu = new Rondevu({
    apiUrl: 'https://api.ronde.vu',
    cryptoAdapter: new NodeCryptoAdapter()
  })

🤖 Generated with Claude Code
https://claude.com/claude-code

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
This commit is contained in:
2025-12-12 20:34:27 +01:00
parent 4ce5217135
commit d55abf2b63
8 changed files with 312 additions and 62 deletions

View File

@@ -1,9 +1,11 @@
import { RondevuAPI, Keypair, Service, ServiceRequest, IceCandidate } from './api.js'
import { CryptoAdapter } from './crypto-adapter.js'
export interface RondevuOptions {
apiUrl: string
username?: string // Optional, will generate anonymous if not provided
keypair?: Keypair // Optional, will generate if not provided
cryptoAdapter?: CryptoAdapter // Optional, defaults to WebCryptoAdapter
}
export interface PublishServiceOptions {
@@ -52,11 +54,13 @@ export class Rondevu {
private username: string
private keypair: Keypair | null = null
private usernameClaimed = false
private cryptoAdapter?: CryptoAdapter
constructor(options: RondevuOptions) {
this.apiUrl = options.apiUrl
this.username = options.username || this.generateAnonymousUsername()
this.keypair = options.keypair || null
this.cryptoAdapter = options.cryptoAdapter
console.log('[Rondevu] Constructor called:', {
username: this.username,
@@ -89,14 +93,14 @@ export class Rondevu {
// Generate keypair if not provided
if (!this.keypair) {
console.log('[Rondevu] Generating new keypair...')
this.keypair = await RondevuAPI.generateKeypair()
this.keypair = await RondevuAPI.generateKeypair(this.cryptoAdapter)
console.log('[Rondevu] Generated keypair, publicKey:', this.keypair.publicKey)
} else {
console.log('[Rondevu] Using existing keypair, publicKey:', this.keypair.publicKey)
}
// Create API instance with username and keypair
this.api = new RondevuAPI(this.apiUrl, this.username, this.keypair)
// Create API instance with username, keypair, and crypto adapter
this.api = new RondevuAPI(this.apiUrl, this.username, this.keypair, this.cryptoAdapter)
console.log('[Rondevu] Created API instance with username:', this.username)
}