0
# Sr25519 Digital Signatures
1
2
Sr25519 signature scheme implementation with keypair generation, signing, verification, and hierarchical key derivation. Sr25519 is the primary signature scheme used in the Polkadot ecosystem.
3
4
## Capabilities
5
6
### Generate Keypair from Seed
7
8
Generates an Sr25519 keypair from a 32-byte seed.
9
10
```typescript { .api }
11
/**
12
* Generates Sr25519 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 sr25519KeypairFromSeed(seed: Uint8Array): Uint8Array;
17
```
18
19
**Usage Example:**
20
21
```typescript
22
import { waitReady, sr25519KeypairFromSeed } from "@polkadot/wasm-crypto";
23
24
await waitReady();
25
26
const seed = new Uint8Array(32).fill(1); // Example seed
27
const keypair = sr25519KeypairFromSeed(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 Sr25519 private key.
39
40
```typescript { .api }
41
/**
42
* Signs a message using Sr25519
43
* @param pubkey - 32-byte public key as Uint8Array
44
* @param secret - 32-byte secret key as Uint8Array
45
* @param message - Message to sign as Uint8Array
46
* @returns 64-byte signature as Uint8Array
47
*/
48
function sr25519Sign(pubkey: Uint8Array, secret: Uint8Array, message: Uint8Array): Uint8Array;
49
```
50
51
**Usage Example:**
52
53
```typescript
54
import { waitReady, sr25519KeypairFromSeed, sr25519Sign } from "@polkadot/wasm-crypto";
55
56
await waitReady();
57
58
const seed = new Uint8Array(32).fill(1);
59
const keypair = sr25519KeypairFromSeed(seed);
60
const secretKey = keypair.slice(0, 32);
61
const publicKey = keypair.slice(32, 64);
62
63
const message = new TextEncoder().encode("Hello, Polkadot!");
64
const signature = sr25519Sign(publicKey, secretKey, message);
65
66
console.log("Signature length:", signature.length); // 64
67
```
68
69
### Verify Signature
70
71
Verifies an Sr25519 signature against a message and public key.
72
73
```typescript { .api }
74
/**
75
* Verifies Sr25519 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 sr25519Verify(signature: Uint8Array, message: Uint8Array, pubkey: Uint8Array): boolean;
82
```
83
84
**Usage Example:**
85
86
```typescript
87
import {
88
waitReady,
89
sr25519KeypairFromSeed,
90
sr25519Sign,
91
sr25519Verify
92
} from "@polkadot/wasm-crypto";
93
94
await waitReady();
95
96
const seed = new Uint8Array(32).fill(1);
97
const keypair = sr25519KeypairFromSeed(seed);
98
const secretKey = keypair.slice(0, 32);
99
const publicKey = keypair.slice(32, 64);
100
101
const message = new TextEncoder().encode("Hello, Polkadot!");
102
const signature = sr25519Sign(publicKey, secretKey, message);
103
104
const isValid = sr25519Verify(signature, message, publicKey);
105
console.log("Signature is valid:", isValid); // true
106
```
107
108
### Hard Key Derivation
109
110
Derives a new keypair using hard derivation (cannot derive public key without private key).
111
112
```typescript { .api }
113
/**
114
* Derives Sr25519 keypair using hard derivation
115
* @param pair - 64-byte parent keypair as Uint8Array
116
* @param cc - 32-byte chain code as Uint8Array
117
* @returns 64-byte derived keypair as Uint8Array
118
*/
119
function sr25519DeriveKeypairHard(pair: Uint8Array, cc: Uint8Array): Uint8Array;
120
```
121
122
**Usage Example:**
123
124
```typescript
125
import {
126
waitReady,
127
sr25519KeypairFromSeed,
128
sr25519DeriveKeypairHard
129
} from "@polkadot/wasm-crypto";
130
131
await waitReady();
132
133
const seed = new Uint8Array(32).fill(1);
134
const parentKeypair = sr25519KeypairFromSeed(seed);
135
const chainCode = new Uint8Array(32).fill(2);
136
137
const derivedKeypair = sr25519DeriveKeypairHard(parentKeypair, chainCode);
138
const derivedPublicKey = derivedKeypair.slice(32, 64);
139
140
console.log("Derived public key:", derivedPublicKey);
141
```
142
143
### Soft Key Derivation
144
145
Derives a new keypair using soft derivation (can derive public key without private key).
146
147
```typescript { .api }
148
/**
149
* Derives Sr25519 keypair using soft derivation
150
* @param pair - 64-byte parent keypair as Uint8Array
151
* @param cc - 32-byte chain code as Uint8Array
152
* @returns 64-byte derived keypair as Uint8Array
153
*/
154
function sr25519DeriveKeypairSoft(pair: Uint8Array, cc: Uint8Array): Uint8Array;
155
```
156
157
### Soft Public Key Derivation
158
159
Derives a public key using soft derivation from just the parent public key.
160
161
```typescript { .api }
162
/**
163
* Derives Sr25519 public key using soft derivation
164
* @param pubkey - 32-byte parent public key as Uint8Array
165
* @param cc - 32-byte chain code as Uint8Array
166
* @returns 32-byte derived public key as Uint8Array
167
*/
168
function sr25519DerivePublicSoft(pubkey: Uint8Array, cc: Uint8Array): Uint8Array;
169
```
170
171
**Usage Example:**
172
173
```typescript
174
import {
175
waitReady,
176
sr25519KeypairFromSeed,
177
sr25519DeriveKeypairSoft,
178
sr25519DerivePublicSoft
179
} from "@polkadot/wasm-crypto";
180
181
await waitReady();
182
183
const seed = new Uint8Array(32).fill(1);
184
const parentKeypair = sr25519KeypairFromSeed(seed);
185
const parentPublicKey = parentKeypair.slice(32, 64);
186
const chainCode = new Uint8Array(32).fill(2);
187
188
// Derive keypair (requires full parent keypair)
189
const derivedKeypair = sr25519DeriveKeypairSoft(parentKeypair, chainCode);
190
191
// Derive just public key (only needs parent public key)
192
const derivedPublicKey = sr25519DerivePublicSoft(parentPublicKey, chainCode);
193
194
console.log("Keys match:",
195
Array.from(derivedKeypair.slice(32, 64)).join(',') ===
196
Array.from(derivedPublicKey).join(',')
197
); // true
198
```
199
200
### Key Agreement (Diffie-Hellman)
201
202
Performs Diffie-Hellman key agreement to generate a shared secret.
203
204
```typescript { .api }
205
/**
206
* Performs Sr25519 Diffie-Hellman key agreement
207
* @param pubkey - 32-byte other party's public key as Uint8Array
208
* @param secret - 32-byte own secret key as Uint8Array
209
* @returns 32-byte shared secret as Uint8Array
210
*/
211
function sr25519Agree(pubkey: Uint8Array, secret: Uint8Array): Uint8Array;
212
```
213
214
**Usage Example:**
215
216
```typescript
217
import {
218
waitReady,
219
sr25519KeypairFromSeed,
220
sr25519Agree
221
} from "@polkadot/wasm-crypto";
222
223
await waitReady();
224
225
// Alice's keypair
226
const aliceSeed = new Uint8Array(32).fill(1);
227
const aliceKeypair = sr25519KeypairFromSeed(aliceSeed);
228
const aliceSecret = aliceKeypair.slice(0, 32);
229
const alicePublic = aliceKeypair.slice(32, 64);
230
231
// Bob's keypair
232
const bobSeed = new Uint8Array(32).fill(2);
233
const bobKeypair = sr25519KeypairFromSeed(bobSeed);
234
const bobSecret = bobKeypair.slice(0, 32);
235
const bobPublic = bobKeypair.slice(32, 64);
236
237
// Generate shared secrets
238
const aliceShared = sr25519Agree(bobPublic, aliceSecret);
239
const bobShared = sr25519Agree(alicePublic, bobSecret);
240
241
// Shared secrets should be identical
242
console.log("Shared secrets match:",
243
Array.from(aliceShared).join(',') === Array.from(bobShared).join(',')
244
); // true
245
```
246
247
## Complete Sr25519 Workflow
248
249
```typescript
250
import {
251
waitReady,
252
sr25519KeypairFromSeed,
253
sr25519Sign,
254
sr25519Verify,
255
sr25519DeriveKeypairSoft
256
} from "@polkadot/wasm-crypto";
257
258
async function demonstrateSr25519() {
259
await waitReady();
260
261
// Generate master keypair
262
const seed = new Uint8Array(32).fill(1);
263
const masterKeypair = sr25519KeypairFromSeed(seed);
264
const masterSecret = masterKeypair.slice(0, 32);
265
const masterPublic = masterKeypair.slice(32, 64);
266
267
// Derive child keypair
268
const chainCode = new Uint8Array(32).fill(2);
269
const childKeypair = sr25519DeriveKeypairSoft(masterKeypair, chainCode);
270
const childSecret = childKeypair.slice(0, 32);
271
const childPublic = childKeypair.slice(32, 64);
272
273
// Sign and verify with child key
274
const message = new TextEncoder().encode("Child key signature");
275
const signature = sr25519Sign(childPublic, childSecret, message);
276
const isValid = sr25519Verify(signature, message, childPublic);
277
278
console.log("Child signature valid:", isValid);
279
280
return { masterKeypair, childKeypair, signature };
281
}
282
283
demonstrateSr25519();
284
```