src/crypto/primitives.ts is the bottom layer of the app's cryptography: thin, exact wrappers over @noble/curves, @noble/hashes, @noble/ciphers, @scure/bip39 and multiformats (Dependencies). seedkey.ts adds the desktop key file format.
Key derivation
Function | Takes | Returns |
|---|---|---|
exactly 16 bytes, supplied by the caller | 12 English BIP-39 words; throws on any other length | |
a phrase |
| |
phrase, optional passphrase |
| |
same | the same | |
a 32-byte Ed25519 seed |
|
KeyPair is {privateKey, publicKey, accountId}: the raw 32-byte seed, the raw 32-byte public key, and the account id string.
The algorithm, "matching RN and Go":
BIP-39 seed: PBKDF2-HMAC-SHA512, 2048 rounds, salt "mnemonic" + passphrase, one 64-byte block.
SLIP-10 master: HMAC-SHA512(key = "ed25519 seed", seed).
Three hardened steps along m/44'/104109'/0': HMAC-SHA512(chainCode, 0x00 || key || uint32be(index + 0x80000000)).
The first 32 bytes are the Ed25519 seed.
The async variant re-implements PBKDF2 by hand so it can call yieldCryptoTurn() every 64 rounds. It normalizes to NFKD and zeroes its buffers in a finally. Sources that import a phrase must use it (see Me: sources).
yieldCryptoTurn() awaits globalThis.scheduler?.yield(). On Hermes that starts a fresh turn of the 100 ms turn budget; under Bun, where scheduler is absent, it continues at once.
Signing and account ids
sign(privateKey, message): RFC 8032 Ed25519, no prehash, 64-byte signature.
verify(publicKey, message, signature): returns false for malformed input instead of throwing.
principalFromEd25519(publicKey): 0xed 0x01 || publicKey, 34 bytes (the ed25519-pub multicodec prefix).
principalToString, principalFromString: base58btc multibase, so an account id starts z6Mk. Decoding rejects anything that is not 34 bytes with that prefix.
accountIdFromPublicKey(publicKey): the two steps combined.
"Principal" and "account id" are defined in the Glossary.
Randomness, hashing and base64
randomBytes(n): crypto.getRandomValues. The comment adds that "the caller supplies time separately".
sha256: re-exported from @noble/hashes.
b64url: URL-safe, no padding on encode (Go RawURLEncoding). b64std: standard alphabet with padding (Go JSON []byte). Both decoders trim whitespace and throw Invalid base64 on a wrong alphabet or impossible length. base64urlEncode and base64urlDecode are aliases.
Vault keys and encryption
Constant or function | Value |
|---|---|
|
|
| 32 bytes (remote secrets and local wrapping keys) |
| 64 bytes (the AEAD uses the first 32) |
HKDF-SHA256, no salt, the info string above, 32 bytes out; throws unless the secret is 32 bytes | |
| |
XChaCha20-Poly1305; the output is the 24-byte nonce, then the ciphertext, then the 16-byte tag. Random nonce by default | |
authenticates and decrypts; throws on a wrong key or altered bytes |
Both AEAD functions take key.subarray(0, 32), which is how a 64-byte data key works. These are the building blocks of the vault codec and the bearer used against the vault server.
UUIDs and timestamps
randomUUID(entropy?): RFC 4122 version 4 from 16 bytes.
wireTimestamp(ms): floors to whole milliseconds and throws on a negative or non-finite value. The comment explains why: Exact clocks "are fractional, while RN signs Date.now() integers and BigInt() throws on fractions". Every blob and envelope builder passes its time through this.
The .seedkey file
buildSeedKeyExport({seed, createTime}) returns the desktop format: two-space indented JSON {createTime, publicKey, keyB64} plus a newline, where createTime is RFC 3339, publicKey the account id and keyB64 the seed in unpadded base64url. parseSeedKeyImport(json) returns {seed, publicKey} and throws on invalid JSON, a missing or malformed keyB64, a seed that is not 32 bytes, a publicKey that does not match, or an encryption field. The last case reports ENCRYPTED_SEEDKEY_ERROR: password-protected exports need Argon2id, "which Hermes cannot run".
The running app uses the equivalent functions in Identity; this copy exists for byte parity with the earlier client and is exercised only by tests.
Tests
crypto.test.ts: 13 derivation and account tests, 2 seedkey tests, the base64, HKDF (also checked against WebCrypto) and AEAD tests, 1 UUID test and 4 async-derivation tests. review.test.ts: 3 tests on fractional and non-finite clocks. See Parity vectors.
Do you like what you are reading? Subscribe to receive updates.
Unsubscribe anytime