@abraca/wiki 2.34.0 → 2.35.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/crypto.ts DELETED
@@ -1,70 +0,0 @@
1
- /**
2
- * Ed25519 key generation, persistence, and challenge signing for CLI auth.
3
- * Mirrors @abraca/mcp/src/crypto.ts — standalone to avoid MCP SDK dependency.
4
- */
5
- import * as ed from '@noble/ed25519'
6
- import { sha512 } from '@noble/hashes/sha2.js'
7
- import { readFile, writeFile, mkdir } from 'node:fs/promises'
8
-
9
- // @noble/ed25519 v3 hash hook
10
- ed.hashes.sha512 = sha512
11
- ed.hashes.sha512Async = (m: Uint8Array) => Promise.resolve(sha512(m))
12
- import { existsSync } from 'node:fs'
13
- import { homedir } from 'node:os'
14
- import { join, dirname } from 'node:path'
15
-
16
- const DEFAULT_KEY_PATH = join(homedir(), '.abracadabra', 'cli.key')
17
-
18
- function toBase64url(bytes: Uint8Array): string {
19
- return Buffer.from(bytes).toString('base64url')
20
- }
21
-
22
- function fromBase64url(b64: string): Uint8Array {
23
- return new Uint8Array(Buffer.from(b64, 'base64url'))
24
- }
25
-
26
- export interface CLIKeypair {
27
- privateKey: Uint8Array
28
- publicKeyB64: string
29
- }
30
-
31
- /**
32
- * Load an existing Ed25519 keypair from disk, or generate and persist a new one.
33
- * The file stores the raw 32-byte private key seed.
34
- */
35
- export async function loadOrCreateKeypair(keyPath?: string): Promise<CLIKeypair> {
36
- const path = keyPath || DEFAULT_KEY_PATH
37
-
38
- if (existsSync(path)) {
39
- const seed = await readFile(path)
40
- if (seed.length !== 32) {
41
- throw new Error(`Invalid key file at ${path}: expected 32 bytes, got ${seed.length}`)
42
- }
43
- const privateKey = new Uint8Array(seed)
44
- const publicKey = ed.getPublicKey(privateKey)
45
- return { privateKey, publicKeyB64: toBase64url(publicKey) }
46
- }
47
-
48
- // Generate new keypair
49
- const privateKey = ed.utils.randomSecretKey()
50
- const publicKey = ed.getPublicKey(privateKey)
51
-
52
- // Ensure directory exists and write seed with restricted permissions
53
- const dir = dirname(path)
54
- if (!existsSync(dir)) {
55
- await mkdir(dir, { recursive: true, mode: 0o700 })
56
- }
57
- await writeFile(path, Buffer.from(privateKey), { mode: 0o600 })
58
-
59
- console.error(`[abracadabra] Generated new keypair at ${path}`)
60
- console.error(`[abracadabra] Public key: ${toBase64url(publicKey)}`)
61
-
62
- return { privateKey, publicKeyB64: toBase64url(publicKey) }
63
- }
64
-
65
- /** Sign a base64url challenge with the private key; returns base64url signature. */
66
- export function signChallenge(challengeB64: string, privateKey: Uint8Array): string {
67
- const challenge = fromBase64url(challengeB64)
68
- const sig = ed.sign(challenge, privateKey)
69
- return toBase64url(sig)
70
- }