-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathkeys.ts
More file actions
44 lines (38 loc) · 1.6 KB
/
Copy pathkeys.ts
File metadata and controls
44 lines (38 loc) · 1.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
/**
* Ed25519 key helpers and `did:key` derivation for the Verifiable OKF signer.
*
* Signing uses a 32-byte Ed25519 seed (private key). The signer never logs or
* writes the private key; only the derived public key / DID and the signature
* leave this module.
*/
import { ed25519 } from "@noble/curves/ed25519";
import { base58 } from "@scure/base";
/** multicodec prefix for an Ed25519 public key (0xed 0x01, varint). */
const ED25519_PUB_MULTICODEC = Uint8Array.from([0xed, 0x01]);
/** Public key (32 bytes) for an Ed25519 seed. */
export function publicKeyFromSeed(seed: Uint8Array): Uint8Array {
return ed25519.getPublicKey(seed);
}
/** Detached Ed25519 signature (64 bytes) over `message`. Deterministic (RFC 8032). */
export function signEd25519(message: Uint8Array, seed: Uint8Array): Uint8Array {
return ed25519.sign(message, seed);
}
/** Verify a detached Ed25519 signature. */
export function verifyEd25519(
signature: Uint8Array,
message: Uint8Array,
publicKey: Uint8Array,
): boolean {
return ed25519.verify(signature, message, publicKey);
}
/** `did:key` (multibase base58btc, multicodec ed25519-pub) for a public key. */
export function didKeyFromPublicKey(publicKey: Uint8Array): string {
const bytes = new Uint8Array(ED25519_PUB_MULTICODEC.length + publicKey.length);
bytes.set(ED25519_PUB_MULTICODEC, 0);
bytes.set(publicKey, ED25519_PUB_MULTICODEC.length);
return "did:key:z" + base58.encode(bytes); // multibase 'z' = base58btc
}
/** `did:key` for an Ed25519 seed. */
export function didKeyFromSeed(seed: Uint8Array): string {
return didKeyFromPublicKey(publicKeyFromSeed(seed));
}