@oxyhq/core 12.2.1 → 12.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/cjs/.tsbuildinfo +1 -1
- package/dist/cjs/crypto/aead.js +79 -0
- package/dist/cjs/crypto/ecdh.js +53 -0
- package/dist/cjs/crypto/kdf.js +39 -0
- package/dist/cjs/crypto/keyManager.js +7 -0
- package/dist/cjs/crypto/recoveryPhrase.js +89 -1
- package/dist/cjs/i18n/locales/en-US.json +5 -0
- package/dist/cjs/i18n/locales/es-ES.json +5 -0
- package/dist/cjs/i18n/locales/locales/en-US.json +5 -0
- package/dist/cjs/i18n/locales/locales/es-ES.json +5 -0
- package/dist/cjs/index.js +17 -4
- package/dist/cjs/mixins/OxyServices.identity.js +166 -0
- package/dist/cjs/mixins/OxyServices.identityBackup.js +161 -0
- package/dist/cjs/mixins/OxyServices.user.js +43 -0
- package/dist/cjs/mixins/index.js +4 -0
- package/dist/cjs/utils/validationUtils.js +58 -21
- package/dist/esm/.tsbuildinfo +1 -1
- package/dist/esm/crypto/aead.js +74 -0
- package/dist/esm/crypto/ecdh.js +51 -0
- package/dist/esm/crypto/kdf.js +36 -0
- package/dist/esm/crypto/keyManager.js +7 -0
- package/dist/esm/crypto/recoveryPhrase.js +88 -0
- package/dist/esm/i18n/locales/en-US.json +5 -0
- package/dist/esm/i18n/locales/es-ES.json +5 -0
- package/dist/esm/i18n/locales/locales/en-US.json +5 -0
- package/dist/esm/i18n/locales/locales/es-ES.json +5 -0
- package/dist/esm/index.js +5 -1
- package/dist/esm/mixins/OxyServices.identity.js +166 -0
- package/dist/esm/mixins/OxyServices.identityBackup.js +158 -0
- package/dist/esm/mixins/OxyServices.user.js +43 -0
- package/dist/esm/mixins/index.js +4 -0
- package/dist/esm/utils/validationUtils.js +60 -23
- package/dist/types/.tsbuildinfo +1 -1
- package/dist/types/crypto/aead.d.ts +56 -0
- package/dist/types/crypto/ecdh.d.ts +29 -0
- package/dist/types/crypto/kdf.d.ts +25 -0
- package/dist/types/crypto/keyManager.d.ts +5 -0
- package/dist/types/crypto/recoveryPhrase.d.ts +85 -0
- package/dist/types/index.d.ts +8 -4
- package/dist/types/mixins/OxyServices.identity.d.ts +95 -0
- package/dist/types/mixins/OxyServices.identityBackup.d.ts +129 -0
- package/dist/types/mixins/OxyServices.user.d.ts +38 -8
- package/dist/types/mixins/index.d.ts +2 -1
- package/dist/types/utils/validationUtils.d.ts +65 -0
- package/package.json +4 -2
- package/src/crypto/__tests__/backupMaterial.test.ts +86 -0
- package/src/crypto/__tests__/cryptoPrimitives.test.ts +225 -0
- package/src/crypto/__tests__/keyManager.atomicity.test.ts +33 -0
- package/src/crypto/__tests__/recoveryPhrase.test.ts +61 -0
- package/src/crypto/aead.ts +97 -0
- package/src/crypto/ecdh.ts +60 -0
- package/src/crypto/kdf.ts +43 -0
- package/src/crypto/keyManager.ts +8 -0
- package/src/crypto/recoveryPhrase.ts +133 -0
- package/src/i18n/locales/en-US.json +5 -0
- package/src/i18n/locales/es-ES.json +5 -0
- package/src/index.ts +19 -1
- package/src/mixins/OxyServices.identity.ts +250 -0
- package/src/mixins/OxyServices.identityBackup.ts +237 -0
- package/src/mixins/OxyServices.user.ts +82 -4
- package/src/mixins/__tests__/OxyServices.rotateKey.test.ts +277 -0
- package/src/mixins/__tests__/getFollowStatuses.test.ts +95 -0
- package/src/mixins/__tests__/identityBackup.test.ts +258 -0
- package/src/mixins/index.ts +5 -0
- package/src/types/elliptic.d.ts +10 -2
- package/src/utils/__tests__/validationUtils.test.ts +27 -0
- package/src/utils/validationUtils.ts +61 -20
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Authenticated Encryption with Associated Data (XChaCha20-Poly1305)
|
|
3
|
+
*
|
|
4
|
+
* Pure-JS/TS AEAD via `@noble/ciphers` — identical behaviour on web, Node, and
|
|
5
|
+
* React Native with zero WebCrypto / native-module dependency. This replaces
|
|
6
|
+
* the `crypto.subtle`-based path (unreliable on React Native) for the Commons
|
|
7
|
+
* encrypted backup and device-to-device transfer flows.
|
|
8
|
+
*
|
|
9
|
+
* XChaCha20-Poly1305 is chosen over AES-GCM specifically for its 24-byte
|
|
10
|
+
* (192-bit) random nonce: the nonce space is large enough that random nonces
|
|
11
|
+
* never collide in practice, so callers do not need to maintain a per-key
|
|
12
|
+
* counter. The 16-byte Poly1305 tag is appended to the ciphertext by the
|
|
13
|
+
* underlying library and validated on decrypt.
|
|
14
|
+
*
|
|
15
|
+
* The optional Associated Data (AAD) is authenticated but NOT encrypted: it
|
|
16
|
+
* binds the ciphertext to its context (e.g. a backup version, a device id, a
|
|
17
|
+
* DID). Decryption fails if the key, nonce, ciphertext, OR aad differ from
|
|
18
|
+
* those used at encryption time.
|
|
19
|
+
*
|
|
20
|
+
* ESM/CJS safe: static `import` only, no `require()`.
|
|
21
|
+
*/
|
|
22
|
+
// Loading the polyfill guarantees `globalThis.crypto.getRandomValues` exists on
|
|
23
|
+
// every platform (native crypto on web/Node; expo-crypto-backed shim on RN).
|
|
24
|
+
import './polyfill.js';
|
|
25
|
+
import { xchacha20poly1305 } from '@noble/ciphers/chacha';
|
|
26
|
+
/** Key length for XChaCha20-Poly1305, in bytes (256-bit). */
|
|
27
|
+
export const AEAD_KEY_LENGTH = 32;
|
|
28
|
+
/** Nonce length for XChaCha20-Poly1305, in bytes (192-bit). */
|
|
29
|
+
export const AEAD_NONCE_LENGTH = 24;
|
|
30
|
+
function assertKey(key) {
|
|
31
|
+
if (key.length !== AEAD_KEY_LENGTH) {
|
|
32
|
+
throw new Error(`AEAD key must be ${AEAD_KEY_LENGTH} bytes, got ${key.length}`);
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
/** Generate a fresh 24-byte random nonce via the platform CSPRNG. */
|
|
36
|
+
function randomNonce() {
|
|
37
|
+
const nonce = new Uint8Array(AEAD_NONCE_LENGTH);
|
|
38
|
+
globalThis.crypto.getRandomValues(nonce);
|
|
39
|
+
return nonce;
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* Encrypt `plaintext` under `key` with a fresh random nonce, authenticating the
|
|
43
|
+
* optional `aad`.
|
|
44
|
+
*
|
|
45
|
+
* @param key 32-byte symmetric key (e.g. from `hkdfSha256`).
|
|
46
|
+
* @param plaintext Bytes to encrypt.
|
|
47
|
+
* @param aad Optional associated data authenticated but not encrypted.
|
|
48
|
+
* @returns `{ nonce, ciphertext }` — both are required to decrypt.
|
|
49
|
+
*/
|
|
50
|
+
export function encryptAead(key, plaintext, aad) {
|
|
51
|
+
assertKey(key);
|
|
52
|
+
const nonce = randomNonce();
|
|
53
|
+
const ciphertext = xchacha20poly1305(key, nonce, aad).encrypt(plaintext);
|
|
54
|
+
return { nonce, ciphertext };
|
|
55
|
+
}
|
|
56
|
+
/**
|
|
57
|
+
* Decrypt and authenticate `ciphertext` produced by {@link encryptAead}.
|
|
58
|
+
*
|
|
59
|
+
* Throws if the key, nonce, ciphertext, or aad differ from those used at
|
|
60
|
+
* encryption time (tamper detection), or if the tag is invalid.
|
|
61
|
+
*
|
|
62
|
+
* @param key 32-byte symmetric key.
|
|
63
|
+
* @param nonce The 24-byte nonce returned by `encryptAead`.
|
|
64
|
+
* @param ciphertext Ciphertext with the appended Poly1305 tag.
|
|
65
|
+
* @param aad The same associated data supplied at encryption time.
|
|
66
|
+
* @returns The recovered plaintext bytes.
|
|
67
|
+
*/
|
|
68
|
+
export function decryptAead(key, nonce, ciphertext, aad) {
|
|
69
|
+
assertKey(key);
|
|
70
|
+
if (nonce.length !== AEAD_NONCE_LENGTH) {
|
|
71
|
+
throw new Error(`AEAD nonce must be ${AEAD_NONCE_LENGTH} bytes, got ${nonce.length}`);
|
|
72
|
+
}
|
|
73
|
+
return xchacha20poly1305(key, nonce, aad).decrypt(ciphertext);
|
|
74
|
+
}
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* ECDH shared-secret derivation (secp256k1)
|
|
3
|
+
*
|
|
4
|
+
* Derives a raw 32-byte ECDH shared secret from a local private key and a
|
|
5
|
+
* remote public key, using the SAME `elliptic` `EC('secp256k1')` primitive the
|
|
6
|
+
* rest of core's identity layer uses (`keyManager.ts`). This is the key-exchange
|
|
7
|
+
* step for the Commons device-to-device transfer flow: each side computes the
|
|
8
|
+
* same shared secret, which is then run through `hkdfSha256` to derive the
|
|
9
|
+
* symmetric key handed to `encryptAead` / `decryptAead`.
|
|
10
|
+
*
|
|
11
|
+
* The returned value is the raw x-coordinate of the ECDH point, big-endian,
|
|
12
|
+
* zero-padded to 32 bytes. It is NOT itself a symmetric key — always pass it
|
|
13
|
+
* through a KDF (HKDF) with a context-binding `info` before use.
|
|
14
|
+
*
|
|
15
|
+
* ESM/CJS safe: static `import` only, no `require()`.
|
|
16
|
+
*/
|
|
17
|
+
import _cjs_elliptic from 'elliptic';
|
|
18
|
+
const { ec: EC } = _cjs_elliptic;
|
|
19
|
+
const ec = new EC('secp256k1');
|
|
20
|
+
/** Lowercase and left-pad a private-key hex string to canonical 64-char form. */
|
|
21
|
+
function canonicalPrivateKey(key) {
|
|
22
|
+
return key.toLowerCase().padStart(64, '0');
|
|
23
|
+
}
|
|
24
|
+
const HEX_RE = /^[0-9a-fA-F]+$/;
|
|
25
|
+
/**
|
|
26
|
+
* Compute the ECDH shared secret between a local private key and a remote
|
|
27
|
+
* public key on secp256k1.
|
|
28
|
+
*
|
|
29
|
+
* Symmetric by construction:
|
|
30
|
+
* `deriveSharedSecret(privA, pubB) === deriveSharedSecret(privB, pubA)`.
|
|
31
|
+
*
|
|
32
|
+
* @param privateKeyHex Local private key, hex (up to 64 chars; canonicalized).
|
|
33
|
+
* @param otherPublicKeyHex Remote public key, hex — compressed (`02`/`03` + 32
|
|
34
|
+
* bytes) or uncompressed (`04` + 64 bytes).
|
|
35
|
+
* @returns The 32-byte big-endian shared secret.
|
|
36
|
+
*/
|
|
37
|
+
export function deriveSharedSecret(privateKeyHex, otherPublicKeyHex) {
|
|
38
|
+
if (typeof privateKeyHex !== 'string' || !HEX_RE.test(privateKeyHex)) {
|
|
39
|
+
throw new Error('deriveSharedSecret: privateKeyHex must be a hex string');
|
|
40
|
+
}
|
|
41
|
+
if (typeof otherPublicKeyHex !== 'string' || !HEX_RE.test(otherPublicKeyHex)) {
|
|
42
|
+
throw new Error('deriveSharedSecret: otherPublicKeyHex must be a hex string');
|
|
43
|
+
}
|
|
44
|
+
const keyPair = ec.keyFromPrivate(canonicalPrivateKey(privateKeyHex));
|
|
45
|
+
const otherKey = ec.keyFromPublic(otherPublicKeyHex, 'hex');
|
|
46
|
+
// `derive` returns a BN (the shared point's x-coordinate). Serialize it
|
|
47
|
+
// big-endian, fixed 32 bytes, so both sides agree byte-for-byte regardless of
|
|
48
|
+
// any leading-zero stripping.
|
|
49
|
+
const sharedBytes = keyPair.derive(otherKey.getPublic()).toArray('be', 32);
|
|
50
|
+
return Uint8Array.from(sharedBytes);
|
|
51
|
+
}
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Key Derivation Function (HKDF-SHA256)
|
|
3
|
+
*
|
|
4
|
+
* Pure-JS/TS HKDF via `@noble/hashes` — identical behaviour on web, Node, and
|
|
5
|
+
* React Native with zero WebCrypto / native-module dependency. Used to derive
|
|
6
|
+
* fixed-length symmetric keys from higher-entropy input keying material (an
|
|
7
|
+
* ECDH shared secret, a recovery-phrase seed, etc.) for the Commons encrypted
|
|
8
|
+
* backup and device-to-device transfer flows.
|
|
9
|
+
*
|
|
10
|
+
* ESM/CJS safe: static `import` only, no `require()`.
|
|
11
|
+
*/
|
|
12
|
+
import { hkdf } from '@noble/hashes/hkdf';
|
|
13
|
+
import { sha256 } from '@noble/hashes/sha256';
|
|
14
|
+
/**
|
|
15
|
+
* Derive `length` bytes of keying material from `ikm` using HKDF-SHA256
|
|
16
|
+
* (RFC 5869 — extract-then-expand).
|
|
17
|
+
*
|
|
18
|
+
* @param ikm Input keying material (the raw secret; NOT necessarily uniform).
|
|
19
|
+
* @param salt Non-secret random salt. An empty array is treated by HKDF as a
|
|
20
|
+
* zero-filled salt of the hash length — pass a real salt whenever
|
|
21
|
+
* one is available so derivations for different contexts diverge.
|
|
22
|
+
* @param info Context/application-binding string ("what is this key for").
|
|
23
|
+
* Distinct `info` values yield independent keys from the same ikm.
|
|
24
|
+
* @param length Number of output bytes. Must be in (0, 255 * 32].
|
|
25
|
+
* @returns Exactly `length` bytes of derived keying material.
|
|
26
|
+
*/
|
|
27
|
+
export function hkdfSha256(ikm, salt, info, length) {
|
|
28
|
+
if (!Number.isInteger(length) || length <= 0) {
|
|
29
|
+
throw new Error('hkdfSha256: length must be a positive integer');
|
|
30
|
+
}
|
|
31
|
+
// HKDF-Expand is defined for at most 255 * HashLen bytes of output.
|
|
32
|
+
if (length > 255 * 32) {
|
|
33
|
+
throw new Error('hkdfSha256: length must not exceed 8160 bytes (255 * 32)');
|
|
34
|
+
}
|
|
35
|
+
return hkdf(sha256, ikm, salt, info, length);
|
|
36
|
+
}
|
|
@@ -1291,6 +1291,13 @@ export class KeyManager {
|
|
|
1291
1291
|
const keyPair = ec.keyFromPrivate(KeyManager.canonicalPrivateKey(privateKey));
|
|
1292
1292
|
return keyPair.getPublic('hex');
|
|
1293
1293
|
}
|
|
1294
|
+
/**
|
|
1295
|
+
* Normalize a public key to uncompressed, lowercased hex. Used when building
|
|
1296
|
+
* signed rotation payloads so legacy compressed/cased encodings still verify.
|
|
1297
|
+
*/
|
|
1298
|
+
static canonicalPublicKey(publicKey) {
|
|
1299
|
+
return ec.keyFromPublic(publicKey, 'hex').getPublic(false, 'hex').toLowerCase();
|
|
1300
|
+
}
|
|
1294
1301
|
/**
|
|
1295
1302
|
* Validate that a string is a valid public key
|
|
1296
1303
|
*
|
|
@@ -8,6 +8,7 @@
|
|
|
8
8
|
*/
|
|
9
9
|
import * as bip39 from 'bip39';
|
|
10
10
|
import { KeyManager } from './keyManager.js';
|
|
11
|
+
import { hkdfSha256 } from './kdf.js';
|
|
11
12
|
/**
|
|
12
13
|
* Convert Uint8Array or array-like to hexadecimal string
|
|
13
14
|
* Works in both Node.js and React Native without depending on Buffer
|
|
@@ -19,6 +20,22 @@ function toHex(data) {
|
|
|
19
20
|
.map(b => b.toString(16).padStart(2, '0'))
|
|
20
21
|
.join('');
|
|
21
22
|
}
|
|
23
|
+
/** UTF-8 encode an ASCII label to bytes (for HKDF salt/info). */
|
|
24
|
+
function utf8(label) {
|
|
25
|
+
return new TextEncoder().encode(label);
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* HKDF context tag for the encrypted-backup key schedule (b3 Feature 1). Used as
|
|
29
|
+
* the HKDF `salt`; distinct from any other Oxy key-derivation salt so the backup
|
|
30
|
+
* key schedule is independent. Versioned so a future scheme change is a new tag.
|
|
31
|
+
*/
|
|
32
|
+
export const BACKUP_KDF_SALT = 'oxy-identity-backup-v1';
|
|
33
|
+
/** HKDF `info` label that derives the symmetric AEAD key from the seed. */
|
|
34
|
+
export const BACKUP_KDF_ENCRYPTION_INFO = 'oxy-backup-encryption-key';
|
|
35
|
+
/** HKDF `info` label that derives the (server-hashed) backup locator from the seed. */
|
|
36
|
+
export const BACKUP_KDF_LOOKUP_INFO = 'oxy-backup-lookup-id';
|
|
37
|
+
/** Byte length of both the derived backup key and the derived lookup id (256-bit). */
|
|
38
|
+
export const BACKUP_MATERIAL_LENGTH = 32;
|
|
22
39
|
export class RecoveryPhraseService {
|
|
23
40
|
/**
|
|
24
41
|
* Generate a new identity with a recovery phrase.
|
|
@@ -69,6 +86,77 @@ export class RecoveryPhraseService {
|
|
|
69
86
|
publicKey,
|
|
70
87
|
};
|
|
71
88
|
}
|
|
89
|
+
/**
|
|
90
|
+
* Derive a brand-new identity + recovery phrase WITHOUT persisting anything.
|
|
91
|
+
*
|
|
92
|
+
* Pure: same derivation as {@link generateIdentityWithRecovery} (128-bit
|
|
93
|
+
* mnemonic → seed → first 32 bytes as the secp256k1 private key) but it stops
|
|
94
|
+
* BEFORE `KeyManager.importKeyPair`, so no on-device identity is touched. The
|
|
95
|
+
* caller decides if/when to commit the material (e.g. only after a server
|
|
96
|
+
* confirms a key rotation). Works on web too — it never reads or writes secure
|
|
97
|
+
* storage.
|
|
98
|
+
*
|
|
99
|
+
* The 12-word `phrase` MUST be shown to the user before the identity is
|
|
100
|
+
* committed anywhere — if it is lost the account becomes unrecoverable.
|
|
101
|
+
*/
|
|
102
|
+
static async derivePendingIdentity() {
|
|
103
|
+
const mnemonic = bip39.generateMnemonic(128);
|
|
104
|
+
const seed = await bip39.mnemonicToSeed(mnemonic);
|
|
105
|
+
const seedSlice = seed.subarray ? seed.subarray(0, 32) : seed.slice(0, 32);
|
|
106
|
+
const privateKey = toHex(seedSlice);
|
|
107
|
+
const publicKey = KeyManager.derivePublicKey(privateKey);
|
|
108
|
+
return {
|
|
109
|
+
phrase: mnemonic,
|
|
110
|
+
words: mnemonic.split(' '),
|
|
111
|
+
privateKey,
|
|
112
|
+
publicKey,
|
|
113
|
+
};
|
|
114
|
+
}
|
|
115
|
+
/**
|
|
116
|
+
* Derive the private key from a recovery phrase WITHOUT storing it.
|
|
117
|
+
*
|
|
118
|
+
* The private-key counterpart of {@link derivePublicKeyFromPhrase}. Used to
|
|
119
|
+
* re-derive a key in memory (e.g. to sign a rotation proof with the current
|
|
120
|
+
* key when the device has no SecureStore copy). Never persists — the returned
|
|
121
|
+
* material lives only in the caller's memory.
|
|
122
|
+
*/
|
|
123
|
+
static async derivePrivateKeyFromPhrase(phrase) {
|
|
124
|
+
const normalizedPhrase = phrase.trim().toLowerCase();
|
|
125
|
+
if (!bip39.validateMnemonic(normalizedPhrase)) {
|
|
126
|
+
throw new Error('Invalid recovery phrase');
|
|
127
|
+
}
|
|
128
|
+
const seed = await bip39.mnemonicToSeed(normalizedPhrase);
|
|
129
|
+
const seedSlice = seed.subarray ? seed.subarray(0, 32) : seed.slice(0, 32);
|
|
130
|
+
return toHex(seedSlice);
|
|
131
|
+
}
|
|
132
|
+
/**
|
|
133
|
+
* Derive the encrypted-backup key material from a recovery phrase (b3 Feature
|
|
134
|
+
* 1). PURE and additive — it does NOT touch the frozen phrase→privateKey
|
|
135
|
+
* derivation ({@link derivePrivateKeyFromPhrase} slices the first 32 seed
|
|
136
|
+
* bytes) and never reads or writes secure storage.
|
|
137
|
+
*
|
|
138
|
+
* Both outputs are derived from the FULL 64-byte BIP-39 seed via HKDF-SHA256
|
|
139
|
+
* with domain-separated `info` labels, so the domain separation is real: a
|
|
140
|
+
* device compromise that leaks only the raw 32-byte private key can compute
|
|
141
|
+
* NEITHER the backup key nor the lookup id (both need the whole seed). Locating
|
|
142
|
+
* AND decrypting a backup therefore requires the recovery phrase.
|
|
143
|
+
*
|
|
144
|
+
* @param phrase - The BIP-39 recovery phrase (validated + normalized here).
|
|
145
|
+
* @returns `{ backupKey, lookupId }` — the AEAD key (kept local) and the hex
|
|
146
|
+
* locator (uploaded; server stores only its hash).
|
|
147
|
+
* @throws if the phrase is not a valid BIP-39 mnemonic.
|
|
148
|
+
*/
|
|
149
|
+
static async deriveBackupMaterial(phrase) {
|
|
150
|
+
const normalizedPhrase = phrase.trim().toLowerCase();
|
|
151
|
+
if (!bip39.validateMnemonic(normalizedPhrase)) {
|
|
152
|
+
throw new Error('Invalid recovery phrase. Please check the words and try again.');
|
|
153
|
+
}
|
|
154
|
+
const seed = await bip39.mnemonicToSeed(normalizedPhrase);
|
|
155
|
+
const salt = utf8(BACKUP_KDF_SALT);
|
|
156
|
+
const backupKey = hkdfSha256(seed, salt, utf8(BACKUP_KDF_ENCRYPTION_INFO), BACKUP_MATERIAL_LENGTH);
|
|
157
|
+
const lookupId = toHex(hkdfSha256(seed, salt, utf8(BACKUP_KDF_LOOKUP_INFO), BACKUP_MATERIAL_LENGTH));
|
|
158
|
+
return { backupKey, lookupId };
|
|
159
|
+
}
|
|
72
160
|
/**
|
|
73
161
|
* Restore an identity from a recovery phrase.
|
|
74
162
|
*
|
|
@@ -72,6 +72,9 @@
|
|
|
72
72
|
"backToSignInLink": "Already have an account? Sign in",
|
|
73
73
|
"hubExplainer": "Create your Oxy ID in a secure window, then come right back.",
|
|
74
74
|
"continueInWindow": "Continue in a new window",
|
|
75
|
+
"toasts": {
|
|
76
|
+
"passkeyCreateFailed": "We couldn't create your account. Please try again."
|
|
77
|
+
},
|
|
75
78
|
"welcome": {
|
|
76
79
|
"title": "Welcome to Oxy!",
|
|
77
80
|
"subtitle": "Let's create your account in just a few steps",
|
|
@@ -374,6 +377,8 @@
|
|
|
374
377
|
"toasts": {
|
|
375
378
|
"switchSuccess": "Account switched successfully!",
|
|
376
379
|
"switchFailed": "There was a problem switching accounts. Please try again.",
|
|
380
|
+
"signInFailed": "Sign-in failed. Please try again.",
|
|
381
|
+
"passkeySignInFailed": "Passkey sign-in failed. Please try again.",
|
|
377
382
|
"removeSuccess": "Account removed successfully!",
|
|
378
383
|
"removeFailed": "There was a problem removing the account. Please try again.",
|
|
379
384
|
"signOutAllSuccess": "All accounts signed out successfully!",
|
|
@@ -72,6 +72,9 @@
|
|
|
72
72
|
"backToSignInLink": "¿Ya tienes cuenta? Inicia sesión",
|
|
73
73
|
"hubExplainer": "Crea tu Oxy ID en una ventana segura y vuelve enseguida.",
|
|
74
74
|
"continueInWindow": "Continuar en una ventana nueva",
|
|
75
|
+
"toasts": {
|
|
76
|
+
"passkeyCreateFailed": "No pudimos crear tu cuenta. Inténtalo de nuevo."
|
|
77
|
+
},
|
|
75
78
|
"welcome": {
|
|
76
79
|
"title": "¡Te damos la bienvenida a Oxy!",
|
|
77
80
|
"subtitle": "Crea tu cuenta en pocos pasos",
|
|
@@ -572,6 +575,8 @@
|
|
|
572
575
|
"toasts": {
|
|
573
576
|
"switchSuccess": "¡Cuenta cambiada correctamente!",
|
|
574
577
|
"switchFailed": "Hubo un problema al cambiar de cuenta. Inténtalo de nuevo.",
|
|
578
|
+
"signInFailed": "No se pudo iniciar sesión. Inténtalo de nuevo.",
|
|
579
|
+
"passkeySignInFailed": "No se pudo iniciar sesión con la llave de acceso. Inténtalo de nuevo.",
|
|
575
580
|
"removeSuccess": "¡Cuenta eliminada correctamente!",
|
|
576
581
|
"removeFailed": "Hubo un problema al eliminar la cuenta. Inténtalo de nuevo.",
|
|
577
582
|
"signOutAllSuccess": "¡Todas las cuentas cerradas correctamente!",
|
|
@@ -72,6 +72,9 @@
|
|
|
72
72
|
"backToSignInLink": "Already have an account? Sign in",
|
|
73
73
|
"hubExplainer": "Create your Oxy ID in a secure window, then come right back.",
|
|
74
74
|
"continueInWindow": "Continue in a new window",
|
|
75
|
+
"toasts": {
|
|
76
|
+
"passkeyCreateFailed": "We couldn't create your account. Please try again."
|
|
77
|
+
},
|
|
75
78
|
"welcome": {
|
|
76
79
|
"title": "Welcome to Oxy!",
|
|
77
80
|
"subtitle": "Let's create your account in just a few steps",
|
|
@@ -374,6 +377,8 @@
|
|
|
374
377
|
"toasts": {
|
|
375
378
|
"switchSuccess": "Account switched successfully!",
|
|
376
379
|
"switchFailed": "There was a problem switching accounts. Please try again.",
|
|
380
|
+
"signInFailed": "Sign-in failed. Please try again.",
|
|
381
|
+
"passkeySignInFailed": "Passkey sign-in failed. Please try again.",
|
|
377
382
|
"removeSuccess": "Account removed successfully!",
|
|
378
383
|
"removeFailed": "There was a problem removing the account. Please try again.",
|
|
379
384
|
"signOutAllSuccess": "All accounts signed out successfully!",
|
|
@@ -72,6 +72,9 @@
|
|
|
72
72
|
"backToSignInLink": "¿Ya tienes cuenta? Inicia sesión",
|
|
73
73
|
"hubExplainer": "Crea tu Oxy ID en una ventana segura y vuelve enseguida.",
|
|
74
74
|
"continueInWindow": "Continuar en una ventana nueva",
|
|
75
|
+
"toasts": {
|
|
76
|
+
"passkeyCreateFailed": "No pudimos crear tu cuenta. Inténtalo de nuevo."
|
|
77
|
+
},
|
|
75
78
|
"welcome": {
|
|
76
79
|
"title": "¡Te damos la bienvenida a Oxy!",
|
|
77
80
|
"subtitle": "Crea tu cuenta en pocos pasos",
|
|
@@ -572,6 +575,8 @@
|
|
|
572
575
|
"toasts": {
|
|
573
576
|
"switchSuccess": "¡Cuenta cambiada correctamente!",
|
|
574
577
|
"switchFailed": "Hubo un problema al cambiar de cuenta. Inténtalo de nuevo.",
|
|
578
|
+
"signInFailed": "No se pudo iniciar sesión. Inténtalo de nuevo.",
|
|
579
|
+
"passkeySignInFailed": "No se pudo iniciar sesión con la llave de acceso. Inténtalo de nuevo.",
|
|
575
580
|
"removeSuccess": "¡Cuenta eliminada correctamente!",
|
|
576
581
|
"removeFailed": "Hubo un problema al eliminar la cuenta. Inténtalo de nuevo.",
|
|
577
582
|
"signOutAllSuccess": "¡Todas las cuentas cerradas correctamente!",
|
package/dist/esm/index.js
CHANGED
|
@@ -65,6 +65,10 @@ export { mergeSessions, normalizeAndSortSessions, sessionsArraysEqual, } from '.
|
|
|
65
65
|
export { KeyManager, IdentityAlreadyExistsError, IdentityPersistError, } from './crypto/keyManager.js';
|
|
66
66
|
export { SignatureService } from './crypto/signatureService.js';
|
|
67
67
|
export { RecoveryPhraseService } from './crypto/recoveryPhrase.js';
|
|
68
|
+
// Low-level crypto primitives (b3 Phase 0 — encrypted backup + device transfer)
|
|
69
|
+
export { hkdfSha256 } from './crypto/kdf.js';
|
|
70
|
+
export { encryptAead, decryptAead, AEAD_KEY_LENGTH, AEAD_NONCE_LENGTH, } from './crypto/aead.js';
|
|
71
|
+
export { deriveSharedSecret } from './crypto/ecdh.js';
|
|
68
72
|
// ---------------------------------------------------------------------------
|
|
69
73
|
// Devices
|
|
70
74
|
// ---------------------------------------------------------------------------
|
|
@@ -103,7 +107,7 @@ export { retryAsync } from './utils/asyncUtils.js';
|
|
|
103
107
|
// ---------------------------------------------------------------------------
|
|
104
108
|
// Validation
|
|
105
109
|
// ---------------------------------------------------------------------------
|
|
106
|
-
export { EMAIL_REGEX, USERNAME_REGEX, PASSWORD_REGEX, isValidEmail, isValidUsername, isValidPassword, isValidDisplayName, isRequiredString, isRequiredNumber, isRequiredBoolean, isValidArray, isValidObject, isValidUUID, isValidURL, isValidDate, isValidFileSize, isValidFileType, sanitizeString, sanitizeHTML, isValidObjectId, validateAndSanitizeUserInput, } from './utils/validationUtils.js';
|
|
110
|
+
export { EMAIL_REGEX, USERNAME_REGEX, PASSWORD_REGEX, isValidEmail, isValidUsername, isValidPassword, isValidDisplayName, DISPLAY_NAME_ALLOWED_SCRIPTS, DISPLAY_NAME_DISALLOWED_SOURCE, DISPLAY_NAME_ORPHANED_MARK_SOURCE, isRequiredString, isRequiredNumber, isRequiredBoolean, isValidArray, isValidObject, isValidUUID, isValidURL, isValidDate, isValidFileSize, isValidFileType, sanitizeString, sanitizeHTML, isValidObjectId, validateAndSanitizeUserInput, } from './utils/validationUtils.js';
|
|
107
111
|
// ---------------------------------------------------------------------------
|
|
108
112
|
// Text normalization
|
|
109
113
|
// ---------------------------------------------------------------------------
|
|
@@ -1,5 +1,9 @@
|
|
|
1
|
+
import { signMessage } from '@oxyhq/protocol';
|
|
1
2
|
import { KeyManager } from '../crypto/keyManager.js';
|
|
2
3
|
import { SignatureService } from '../crypto/signatureService.js';
|
|
4
|
+
import { RecoveryPhraseService } from '../crypto/recoveryPhrase.js';
|
|
5
|
+
import { isWeb } from '../utils/platform.js';
|
|
6
|
+
import { logger } from '../logger/index.js';
|
|
3
7
|
import { CACHE_TIMES } from './mixinHelpers.js';
|
|
4
8
|
/**
|
|
5
9
|
* Registrable apex the Oxy DID method is anchored on. A user's DID is
|
|
@@ -137,6 +141,168 @@ export function OxyServicesIdentityMixin(Base) {
|
|
|
137
141
|
throw this.handleError(error);
|
|
138
142
|
}
|
|
139
143
|
}
|
|
144
|
+
/**
|
|
145
|
+
* Rotate the account's identity key: derive a brand-new keypair, prove
|
|
146
|
+
* control of the CURRENT key, and have the server ATOMICALLY replace the old
|
|
147
|
+
* key with the new one.
|
|
148
|
+
*
|
|
149
|
+
* The rotation is an atomic REPLACE on the server (never remove-then-add), so
|
|
150
|
+
* it never passes through a zero-auth-method state and is independent of the
|
|
151
|
+
* unlink guards. Because control of the current key is PROVEN (from
|
|
152
|
+
* SecureStore in `'device'` mode, or a recovery-phrase re-derivation in
|
|
153
|
+
* `'phrase'` mode), even the LAST remaining credential can be replaced.
|
|
154
|
+
*
|
|
155
|
+
* Ordering (safety-critical): the new key is persisted on-device ONLY AFTER
|
|
156
|
+
* the server confirms the swap. Persisting earlier would clobber the local
|
|
157
|
+
* key while the server still trusts the old one, locking the device out.
|
|
158
|
+
*
|
|
159
|
+
* Ambiguous-network-failure guard: if the `complete` response is lost
|
|
160
|
+
* (request sent, no reply), the swap may already have applied server-side.
|
|
161
|
+
* Before surfacing the error we reconcile against the derived DID document —
|
|
162
|
+
* if it already advertises the new key, the rotation is treated as done.
|
|
163
|
+
*
|
|
164
|
+
* NOTE: the UI is responsible for showing `newPhrase` to the user. For a
|
|
165
|
+
* "show-phrase-first" flow, derive the identity up front via
|
|
166
|
+
* {@link RecoveryPhraseService.derivePendingIdentity}, display it, then pass
|
|
167
|
+
* it back as `options.pendingIdentity` so the SAME identity is committed.
|
|
168
|
+
*
|
|
169
|
+
* @throws when no user is authenticated, when `proof: 'phrase'` is given
|
|
170
|
+
* without a `phrase`, when `proof: 'device'` runs with no on-device key,
|
|
171
|
+
* or when the rotation does not complete.
|
|
172
|
+
*/
|
|
173
|
+
async rotateKey(options) {
|
|
174
|
+
try {
|
|
175
|
+
const userId = this.getCurrentUserId();
|
|
176
|
+
if (!userId) {
|
|
177
|
+
throw new Error('No authenticated user — sign in before rotating your key.');
|
|
178
|
+
}
|
|
179
|
+
// 1. The NEW identity (in memory only). The UI may pre-derive + pre-show
|
|
180
|
+
// it and pass it back here so the phrase shown === the phrase committed.
|
|
181
|
+
const pending = options.pendingIdentity ?? (await RecoveryPhraseService.derivePendingIdentity());
|
|
182
|
+
const newPublicKey = pending.publicKey;
|
|
183
|
+
// 2. Resolve the OLD signing capability from the chosen proof mode.
|
|
184
|
+
let oldPublicKey;
|
|
185
|
+
let signWithOldKey;
|
|
186
|
+
if (options.proof === 'phrase') {
|
|
187
|
+
const phrase = options.phrase?.trim();
|
|
188
|
+
if (!phrase) {
|
|
189
|
+
throw new Error('A recovery phrase is required for phrase-proof rotation.');
|
|
190
|
+
}
|
|
191
|
+
const oldPrivateKey = await RecoveryPhraseService.derivePrivateKeyFromPhrase(phrase);
|
|
192
|
+
oldPublicKey = KeyManager.derivePublicKey(oldPrivateKey);
|
|
193
|
+
signWithOldKey = (message) => signMessage(message, oldPrivateKey);
|
|
194
|
+
}
|
|
195
|
+
else {
|
|
196
|
+
const currentPublicKey = await KeyManager.getPublicKey();
|
|
197
|
+
if (!currentPublicKey) {
|
|
198
|
+
throw new Error('No on-device identity found. Use the recovery-phrase option to rotate your key.');
|
|
199
|
+
}
|
|
200
|
+
oldPublicKey = currentPublicKey;
|
|
201
|
+
signWithOldKey = (message) => SignatureService.sign(message);
|
|
202
|
+
}
|
|
203
|
+
// 3. Request a single-use rotate_key challenge (bearer).
|
|
204
|
+
const { challenge } = await this.makeRequest('POST', '/auth/rotate/challenge', undefined, { cache: false });
|
|
205
|
+
// 4. Sign the rotation proofs. The OLD key proves control of the key being
|
|
206
|
+
// replaced; the NEW key proves possession of the key being rotated in
|
|
207
|
+
// (so the server never accepts a re-encoding of a key the caller does
|
|
208
|
+
// not control). Both signed byte strings MUST match the server's
|
|
209
|
+
// reconstruction exactly (this key order). The old key is canonicalized
|
|
210
|
+
// so legacy compressed encodings in Mongo still verify.
|
|
211
|
+
const timestamp = Date.now();
|
|
212
|
+
const canonicalOldPublicKey = KeyManager.canonicalPublicKey(oldPublicKey);
|
|
213
|
+
const message = JSON.stringify({
|
|
214
|
+
action: 'rotate_key',
|
|
215
|
+
userId,
|
|
216
|
+
oldPublicKey: canonicalOldPublicKey,
|
|
217
|
+
newPublicKey,
|
|
218
|
+
challenge,
|
|
219
|
+
timestamp,
|
|
220
|
+
});
|
|
221
|
+
const signature = await signWithOldKey(message);
|
|
222
|
+
const newKeyMessage = JSON.stringify({
|
|
223
|
+
action: 'rotate_key_new',
|
|
224
|
+
userId,
|
|
225
|
+
newPublicKey,
|
|
226
|
+
challenge,
|
|
227
|
+
timestamp,
|
|
228
|
+
});
|
|
229
|
+
const newKeyProof = await signMessage(newKeyMessage, pending.privateKey);
|
|
230
|
+
// 5. Complete the rotation. On an AMBIGUOUS failure, reconcile against the
|
|
231
|
+
// DID before deciding the rotation failed.
|
|
232
|
+
let applied = false;
|
|
233
|
+
try {
|
|
234
|
+
const result = await this.makeRequest('POST', '/auth/rotate/complete', {
|
|
235
|
+
newPublicKey,
|
|
236
|
+
challenge,
|
|
237
|
+
signature,
|
|
238
|
+
newKeyProof,
|
|
239
|
+
timestamp,
|
|
240
|
+
...(options.signOutEverywhere ? { signOutEverywhere: true } : {}),
|
|
241
|
+
}, { cache: false });
|
|
242
|
+
applied = result.success && result.publicKey.toLowerCase() === newPublicKey.toLowerCase();
|
|
243
|
+
}
|
|
244
|
+
catch (error) {
|
|
245
|
+
const reconciled = await this._rotationAlreadyApplied(userId, newPublicKey);
|
|
246
|
+
if (!reconciled) {
|
|
247
|
+
throw error;
|
|
248
|
+
}
|
|
249
|
+
applied = true;
|
|
250
|
+
}
|
|
251
|
+
if (!applied) {
|
|
252
|
+
throw new Error('Key rotation did not complete — your previous key is unchanged.');
|
|
253
|
+
}
|
|
254
|
+
// 6. ONLY after the server confirms the swap, persist the new key locally,
|
|
255
|
+
// overwriting the old one. `importKeyPair({ overwrite: true })` uses the
|
|
256
|
+
// atomic persist path (backs the previous key up first). Native-only —
|
|
257
|
+
// on web the key never lived in SecureStore, so there is nothing to
|
|
258
|
+
// persist locally.
|
|
259
|
+
//
|
|
260
|
+
// If this local write fails the server key is ALREADY the new one, so
|
|
261
|
+
// we must NOT throw and swallow the phrase — the caller needs it to
|
|
262
|
+
// re-import the now-live key. Surface the result with
|
|
263
|
+
// `localPersistFailed: true` (mirrors the pendingIdentity
|
|
264
|
+
// show-phrase-first path, where the caller already holds the phrase).
|
|
265
|
+
let localPersistFailed = false;
|
|
266
|
+
if (!isWeb()) {
|
|
267
|
+
try {
|
|
268
|
+
await KeyManager.importKeyPair(pending.privateKey, { overwrite: true });
|
|
269
|
+
}
|
|
270
|
+
catch (persistError) {
|
|
271
|
+
localPersistFailed = true;
|
|
272
|
+
logger.warn('Key rotated on the server but persisting the new key on-device failed; returning the new phrase so it can be re-imported.', { component: 'OxyServices.identity', method: 'rotateKey' }, persistError);
|
|
273
|
+
}
|
|
274
|
+
}
|
|
275
|
+
this._invalidateIdentityCaches(userId);
|
|
276
|
+
return localPersistFailed
|
|
277
|
+
? { newPublicKey, newPhrase: pending.phrase, words: pending.words, localPersistFailed: true }
|
|
278
|
+
: { newPublicKey, newPhrase: pending.phrase, words: pending.words };
|
|
279
|
+
}
|
|
280
|
+
catch (error) {
|
|
281
|
+
throw this.handleError(error);
|
|
282
|
+
}
|
|
283
|
+
}
|
|
284
|
+
/**
|
|
285
|
+
* Reconciliation probe for the rotation ambiguous-failure guard: fetch the
|
|
286
|
+
* account's derived DID document (uncached) and report whether it already
|
|
287
|
+
* advertises `newPublicKey` as a verification method — i.e. whether the swap
|
|
288
|
+
* already landed server-side. A failed probe returns `false` (unconfirmed),
|
|
289
|
+
* so the caller surfaces the original network error.
|
|
290
|
+
*
|
|
291
|
+
* Uses the DID document rather than `GET /auth/methods` because the latter
|
|
292
|
+
* intentionally does NOT expose raw public keys, whereas the DID's
|
|
293
|
+
* `verificationMethod[].publicKeyHex` is derived live from the account's
|
|
294
|
+
* current key — so it reflects a completed rotation immediately.
|
|
295
|
+
*
|
|
296
|
+
* Internal helper (leading underscore); public rather than `private` for the
|
|
297
|
+
* same TS4094 reason as {@link _invalidateIdentityCaches}.
|
|
298
|
+
*/
|
|
299
|
+
async _rotationAlreadyApplied(userId, newPublicKey) {
|
|
300
|
+
return this.makeRequest('GET', `/u/${encodeURIComponent(userId)}/did.json`, undefined, { cache: false })
|
|
301
|
+
.then((doc) => doc.verificationMethod.some((vm) => 'publicKeyHex' in vm &&
|
|
302
|
+
typeof vm.publicKeyHex === 'string' &&
|
|
303
|
+
vm.publicKeyHex.toLowerCase() === newPublicKey.toLowerCase()))
|
|
304
|
+
.catch(() => false);
|
|
305
|
+
}
|
|
140
306
|
/**
|
|
141
307
|
* Sign a record with the on-device identity key, WITHOUT publishing it.
|
|
142
308
|
* The subject is the current user's DID. NATIVE-ONLY (requires a stored
|