0
# Key Management
1
2
Core functionality for generating and handling ed25519 keys, supporting both synchronous and asynchronous operations with optional seed input for deterministic key generation.
3
4
## Capabilities
5
6
### Key Pair Generation
7
8
Generates a new ed25519 key pair with optional seed for deterministic generation.
9
10
```typescript { .api }
11
/**
12
* Generate ed25519 key pair asynchronously
13
* @param seed - Optional 32-byte seed for deterministic generation
14
* @returns Promise resolving to key pair object
15
*/
16
function keygenAsync(seed?: Bytes): Promise<{ secretKey: Bytes; publicKey: Bytes }>;
17
18
/**
19
* Generate ed25519 key pair synchronously (requires hashes.sha512 to be set)
20
* @param seed - Optional 32-byte seed for deterministic generation
21
* @returns Key pair object
22
*/
23
function keygen(seed?: Bytes): { secretKey: Bytes; publicKey: Bytes };
24
```
25
26
**Usage Examples:**
27
28
```typescript
29
import * as ed from '@noble/ed25519';
30
31
// Generate random key pair (async)
32
const keyPair = await ed.keygenAsync();
33
console.log(keyPair.secretKey.length); // 32
34
console.log(keyPair.publicKey.length); // 32
35
36
// Generate deterministic key pair from seed
37
const seed = new Uint8Array(32).fill(1); // Example seed
38
const deterministicKeys = await ed.keygenAsync(seed);
39
40
// Sync version (requires setting hash function first)
41
import { sha512 } from '@noble/hashes/sha512';
42
ed.hashes.sha512 = sha512;
43
const syncKeys = ed.keygen();
44
```
45
46
### Public Key Derivation
47
48
Derives the public key from a given secret key.
49
50
```typescript { .api }
51
/**
52
* Derive public key from secret key asynchronously
53
* @param secretKey - 32-byte secret key
54
* @returns Promise resolving to 32-byte public key
55
*/
56
function getPublicKeyAsync(secretKey: Bytes): Promise<Bytes>;
57
58
/**
59
* Derive public key from secret key synchronously (requires hashes.sha512 to be set)
60
* @param secretKey - 32-byte secret key
61
* @returns 32-byte public key
62
*/
63
function getPublicKey(secretKey: Bytes): Bytes;
64
```
65
66
**Usage Examples:**
67
68
```typescript
69
import * as ed from '@noble/ed25519';
70
71
// Generate or obtain a secret key
72
const secretKey = ed.utils.randomSecretKey();
73
74
// Derive public key (async)
75
const publicKey = await ed.getPublicKeyAsync(secretKey);
76
77
// Derive public key (sync, requires hash function)
78
import { sha512 } from '@noble/hashes/sha512';
79
ed.hashes.sha512 = sha512;
80
const syncPublicKey = ed.getPublicKey(secretKey);
81
82
// Verify they're the same
83
console.log(Buffer.from(publicKey).equals(Buffer.from(syncPublicKey))); // true
84
```
85
86
### Random Key Generation
87
88
Utility function for generating cryptographically secure random secret keys.
89
90
```typescript { .api }
91
/**
92
* Generate a cryptographically secure random secret key
93
* @param seed - Optional seed for deterministic generation
94
* @returns 32-byte secret key
95
*/
96
utils.randomSecretKey(seed?: Bytes): Bytes;
97
```
98
99
**Usage Example:**
100
101
```typescript
102
import { utils } from '@noble/ed25519';
103
104
// Generate random secret key
105
const secretKey = utils.randomSecretKey();
106
console.log(secretKey.length); // 32
107
108
// Generate deterministic secret key from seed
109
const seed = new Uint8Array(32).fill(42);
110
const deterministicKey = utils.randomSecretKey(seed);
111
```
112
113
## Types
114
115
```typescript { .api }
116
type Bytes = Uint8Array;
117
```