0
# Ed25519 Digital Signatures
1
2
Ed25519 signature scheme implementation providing high-performance digital signatures with keypair generation, signing, and verification capabilities.
3
4
## Capabilities
5
6
### Generate Keypair from Seed
7
8
Generates an Ed25519 keypair from a 32-byte seed.
9
10
```typescript { .api }
11
/**
12
* Generates Ed25519 keypair from seed
13
* @param seed - 32-byte seed as Uint8Array
14
* @returns 64-byte keypair as Uint8Array (32-byte secret key + 32-byte public key)
15
*/
16
function ed25519KeypairFromSeed(seed: Uint8Array): Uint8Array;
17
```
18
19
**Usage Example:**
20
21
```typescript
22
import { waitReady, ed25519KeypairFromSeed } from "@polkadot/wasm-crypto";
23
24
await waitReady();
25
26
const seed = new Uint8Array(32).fill(1); // Example seed
27
const keypair = ed25519KeypairFromSeed(seed);
28
29
const secretKey = keypair.slice(0, 32);
30
const publicKey = keypair.slice(32, 64);
31
32
console.log("Secret key length:", secretKey.length); // 32
33
console.log("Public key length:", publicKey.length); // 32
34
```
35
36
### Sign Message
37
38
Signs a message using Ed25519 private key.
39
40
```typescript { .api }
41
/**
42
* Signs a message using Ed25519
43
* @param pubkey - 32-byte public key as Uint8Array
44
* @param seckey - 32-byte secret key as Uint8Array
45
* @param message - Message to sign as Uint8Array
46
* @returns 64-byte signature as Uint8Array
47
*/
48
function ed25519Sign(pubkey: Uint8Array, seckey: Uint8Array, message: Uint8Array): Uint8Array;
49
```
50
51
**Usage Example:**
52
53
```typescript
54
import { waitReady, ed25519KeypairFromSeed, ed25519Sign } from "@polkadot/wasm-crypto";
55
56
await waitReady();
57
58
const seed = new Uint8Array(32).fill(1);
59
const keypair = ed25519KeypairFromSeed(seed);
60
const secretKey = keypair.slice(0, 32);
61
const publicKey = keypair.slice(32, 64);
62
63
const message = new TextEncoder().encode("Hello, Ed25519!");
64
const signature = ed25519Sign(publicKey, secretKey, message);
65
66
console.log("Signature length:", signature.length); // 64
67
```
68
69
### Verify Signature
70
71
Verifies an Ed25519 signature against a message and public key.
72
73
```typescript { .api }
74
/**
75
* Verifies Ed25519 signature
76
* @param signature - 64-byte signature as Uint8Array
77
* @param message - Original message as Uint8Array
78
* @param pubkey - 32-byte public key as Uint8Array
79
* @returns true if signature is valid, false otherwise
80
*/
81
function ed25519Verify(signature: Uint8Array, message: Uint8Array, pubkey: Uint8Array): boolean;
82
```
83
84
**Usage Example:**
85
86
```typescript
87
import {
88
waitReady,
89
ed25519KeypairFromSeed,
90
ed25519Sign,
91
ed25519Verify
92
} from "@polkadot/wasm-crypto";
93
94
await waitReady();
95
96
const seed = new Uint8Array(32).fill(1);
97
const keypair = ed25519KeypairFromSeed(seed);
98
const secretKey = keypair.slice(0, 32);
99
const publicKey = keypair.slice(32, 64);
100
101
const message = new TextEncoder().encode("Hello, Ed25519!");
102
const signature = ed25519Sign(publicKey, secretKey, message);
103
104
const isValid = ed25519Verify(signature, message, publicKey);
105
console.log("Signature is valid:", isValid); // true
106
107
// Test with wrong message
108
const wrongMessage = new TextEncoder().encode("Wrong message");
109
const isInvalid = ed25519Verify(signature, wrongMessage, publicKey);
110
console.log("Wrong message signature is valid:", isInvalid); // false
111
```
112
113
## Complete Ed25519 Workflow
114
115
```typescript
116
import {
117
waitReady,
118
ed25519KeypairFromSeed,
119
ed25519Sign,
120
ed25519Verify
121
} from "@polkadot/wasm-crypto";
122
123
async function demonstrateEd25519() {
124
await waitReady();
125
126
// Generate keypair from seed
127
const seed = new Uint8Array(32);
128
crypto.getRandomValues(seed); // Use random seed in production
129
130
const keypair = ed25519KeypairFromSeed(seed);
131
const secretKey = keypair.slice(0, 32);
132
const publicKey = keypair.slice(32, 64);
133
134
// Sign multiple messages
135
const messages = [
136
"First message",
137
"Second message",
138
"Third message"
139
];
140
141
const signatures = messages.map(msg => {
142
const msgBytes = new TextEncoder().encode(msg);
143
return ed25519Sign(publicKey, secretKey, msgBytes);
144
});
145
146
// Verify all signatures
147
const verifications = messages.map((msg, index) => {
148
const msgBytes = new TextEncoder().encode(msg);
149
return ed25519Verify(signatures[index], msgBytes, publicKey);
150
});
151
152
console.log("All signatures valid:", verifications.every(v => v)); // true
153
154
return { keypair, signatures, verifications };
155
}
156
157
demonstrateEd25519();
158
```