mirror of
https://github.com/xtr-dev/rondevu-client.git
synced 2025-12-16 05:43:24 +00:00
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:
98
src/node-crypto-adapter.ts
Normal file
98
src/node-crypto-adapter.ts
Normal file
@@ -0,0 +1,98 @@
|
||||
/**
|
||||
* Node.js Crypto adapter for Node.js environments
|
||||
* Requires Node.js 19+ or Node.js 18 with --experimental-global-webcrypto flag
|
||||
*/
|
||||
|
||||
import * as ed25519 from '@noble/ed25519'
|
||||
import { CryptoAdapter, Keypair } from './crypto-adapter.js'
|
||||
|
||||
/**
|
||||
* Node.js Crypto implementation using Node.js built-in APIs
|
||||
* Uses Buffer for base64 encoding and crypto.randomBytes for random generation
|
||||
*
|
||||
* Requirements:
|
||||
* - Node.js 19+ (crypto.subtle available globally)
|
||||
* - OR Node.js 18 with --experimental-global-webcrypto flag
|
||||
*
|
||||
* @example
|
||||
* ```typescript
|
||||
* import { RondevuAPI } from '@xtr-dev/rondevu-client'
|
||||
* import { NodeCryptoAdapter } from '@xtr-dev/rondevu-client/node'
|
||||
*
|
||||
* const api = new RondevuAPI(
|
||||
* 'https://signal.example.com',
|
||||
* 'alice',
|
||||
* keypair,
|
||||
* new NodeCryptoAdapter()
|
||||
* )
|
||||
* ```
|
||||
*/
|
||||
export class NodeCryptoAdapter implements CryptoAdapter {
|
||||
constructor() {
|
||||
// Set SHA-512 hash function for ed25519 using Node's crypto.subtle
|
||||
if (typeof crypto === 'undefined' || !crypto.subtle) {
|
||||
throw new Error(
|
||||
'crypto.subtle is not available. ' +
|
||||
'Node.js 19+ is required, or Node.js 18 with --experimental-global-webcrypto flag'
|
||||
)
|
||||
}
|
||||
|
||||
ed25519.hashes.sha512Async = async (message: Uint8Array) => {
|
||||
const hash = await crypto.subtle.digest('SHA-512', message as BufferSource)
|
||||
return new Uint8Array(hash)
|
||||
}
|
||||
}
|
||||
|
||||
async generateKeypair(): Promise<Keypair> {
|
||||
const privateKey = ed25519.utils.randomSecretKey()
|
||||
const publicKey = await ed25519.getPublicKeyAsync(privateKey)
|
||||
|
||||
return {
|
||||
publicKey: this.bytesToBase64(publicKey),
|
||||
privateKey: this.bytesToBase64(privateKey),
|
||||
}
|
||||
}
|
||||
|
||||
async signMessage(message: string, privateKeyBase64: string): Promise<string> {
|
||||
const privateKey = this.base64ToBytes(privateKeyBase64)
|
||||
const encoder = new TextEncoder()
|
||||
const messageBytes = encoder.encode(message)
|
||||
const signature = await ed25519.signAsync(messageBytes, privateKey)
|
||||
|
||||
return this.bytesToBase64(signature)
|
||||
}
|
||||
|
||||
async verifySignature(
|
||||
message: string,
|
||||
signatureBase64: string,
|
||||
publicKeyBase64: string
|
||||
): Promise<boolean> {
|
||||
try {
|
||||
const signature = this.base64ToBytes(signatureBase64)
|
||||
const publicKey = this.base64ToBytes(publicKeyBase64)
|
||||
const encoder = new TextEncoder()
|
||||
const messageBytes = encoder.encode(message)
|
||||
|
||||
return await ed25519.verifyAsync(signature, messageBytes, publicKey)
|
||||
} catch {
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
bytesToBase64(bytes: Uint8Array): string {
|
||||
// Node.js Buffer provides native base64 encoding
|
||||
// @ts-expect-error - Buffer is available in Node.js but not in browser TypeScript definitions
|
||||
return Buffer.from(bytes).toString('base64')
|
||||
}
|
||||
|
||||
base64ToBytes(base64: string): Uint8Array {
|
||||
// Node.js Buffer provides native base64 decoding
|
||||
// @ts-expect-error - Buffer is available in Node.js but not in browser TypeScript definitions
|
||||
return new Uint8Array(Buffer.from(base64, 'base64'))
|
||||
}
|
||||
|
||||
randomBytes(length: number): Uint8Array {
|
||||
// Use Web Crypto API's getRandomValues (available in Node 19+)
|
||||
return crypto.getRandomValues(new Uint8Array(length))
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user