@trustbaseai/account 0.1.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.
Files changed (62) hide show
  1. package/README.md +351 -0
  2. package/dist/address.d.ts +70 -0
  3. package/dist/address.d.ts.map +1 -0
  4. package/dist/address.js +104 -0
  5. package/dist/address.js.map +1 -0
  6. package/dist/bech32.d.ts +78 -0
  7. package/dist/bech32.d.ts.map +1 -0
  8. package/dist/bech32.js +246 -0
  9. package/dist/bech32.js.map +1 -0
  10. package/dist/bytes.d.ts +88 -0
  11. package/dist/bytes.d.ts.map +1 -0
  12. package/dist/bytes.js +215 -0
  13. package/dist/bytes.js.map +1 -0
  14. package/dist/envelope.d.ts +116 -0
  15. package/dist/envelope.d.ts.map +1 -0
  16. package/dist/envelope.js +237 -0
  17. package/dist/envelope.js.map +1 -0
  18. package/dist/errors.d.ts +48 -0
  19. package/dist/errors.d.ts.map +1 -0
  20. package/dist/errors.js +41 -0
  21. package/dist/errors.js.map +1 -0
  22. package/dist/hd.d.ts +85 -0
  23. package/dist/hd.d.ts.map +1 -0
  24. package/dist/hd.js +112 -0
  25. package/dist/hd.js.map +1 -0
  26. package/dist/index.d.ts +60 -0
  27. package/dist/index.d.ts.map +1 -0
  28. package/dist/index.js +199 -0
  29. package/dist/index.js.map +1 -0
  30. package/dist/kdf.d.ts +163 -0
  31. package/dist/kdf.d.ts.map +1 -0
  32. package/dist/kdf.js +351 -0
  33. package/dist/kdf.js.map +1 -0
  34. package/dist/keys.d.ts +55 -0
  35. package/dist/keys.d.ts.map +1 -0
  36. package/dist/keys.js +114 -0
  37. package/dist/keys.js.map +1 -0
  38. package/dist/keystore.d.ts +168 -0
  39. package/dist/keystore.d.ts.map +1 -0
  40. package/dist/keystore.js +340 -0
  41. package/dist/keystore.js.map +1 -0
  42. package/dist/mnemonic.d.ts +84 -0
  43. package/dist/mnemonic.d.ts.map +1 -0
  44. package/dist/mnemonic.js +199 -0
  45. package/dist/mnemonic.js.map +1 -0
  46. package/dist/password.d.ts +79 -0
  47. package/dist/password.d.ts.map +1 -0
  48. package/dist/password.js +237 -0
  49. package/dist/password.js.map +1 -0
  50. package/dist/prf-webauthn.d.ts +85 -0
  51. package/dist/prf-webauthn.d.ts.map +1 -0
  52. package/dist/prf-webauthn.js +204 -0
  53. package/dist/prf-webauthn.js.map +1 -0
  54. package/dist/prf.d.ts +157 -0
  55. package/dist/prf.d.ts.map +1 -0
  56. package/dist/prf.js +318 -0
  57. package/dist/prf.js.map +1 -0
  58. package/dist/webcrypto.d.ts +93 -0
  59. package/dist/webcrypto.d.ts.map +1 -0
  60. package/dist/webcrypto.js +157 -0
  61. package/dist/webcrypto.js.map +1 -0
  62. package/package.json +51 -0
package/dist/hd.js ADDED
@@ -0,0 +1,112 @@
1
+ "use strict";
2
+ /**
3
+ * 助记词 → 账户(设备账户的"首次打开即建账户"路径,SDK 方案 §3.2.3 / §8.3.2 ②)。
4
+ *
5
+ * ```
6
+ * BIP39 助记词 ──(PBKDF2-HMAC-SHA512, 2048 次, salt="mnemonic"+passphrase)──▶ 64 字节种子
7
+ * 种子 ──(BIP32/CIP-3 路径 m/44'/118'/0'/0/0)──▶ secp256k1 私钥(32 字节)
8
+ * 私钥 ──▶ 33 字节压缩公钥 ──▶ tct1… 地址(口径见 address.ts)
9
+ * ```
10
+ *
11
+ * ## 为什么路径是 `m/44'/118'/0'/0/0`
12
+ *
13
+ * `118` 是 Cosmos 的 coin type(SLIP-44 登记值),后面 `0'/0/0` 是"第一个账户、
14
+ * 外部链、第一个地址"。这与 cosmjs 的 `DirectSecp256k1HdWallet` 默认值一致
15
+ * (`@trustbase/protocol` 的 `HD_PATH` 就是这一串),所以同一份助记词在本 SDK、
16
+ * 在 cosmjs、在链侧 keyring 里派生出的是**同一个地址**。改这个路径 = 让所有
17
+ * 已发出的纸密钥指向另一个地址,属于红线。
18
+ *
19
+ * ## 助记词口令(passphrase,BIP39 的"第 13/25 个词")
20
+ *
21
+ * 本 SDK v0 **不使用**它(不引导用户设置,避免多一层"忘了我还设过口令"的坑),
22
+ * 但参数保留并直通底层:将来要支持时不必改签名,且**必须**记住 BIP39 的交错
23
+ * 结构 —— 助记词带不带 passphrase 派生出的是两个完全不同的账户。
24
+ */
25
+ Object.defineProperty(exports, "__esModule", { value: true });
26
+ exports.WORDLIST_ENGLISH = exports.ACCOUNT_DERIVATION_PATH = void 0;
27
+ exports.mnemonicToSeed = mnemonicToSeed;
28
+ exports.deriveAccountFromMnemonic = deriveAccountFromMnemonic;
29
+ exports.accountFromPrivateKey = accountFromPrivateKey;
30
+ exports.createAccount = createAccount;
31
+ const bip32_1 = require("@scure/bip32");
32
+ const bip39_1 = require("@scure/bip39");
33
+ const english_1 = require("@scure/bip39/wordlists/english");
34
+ const address_1 = require("./address");
35
+ const bytes_1 = require("./bytes");
36
+ const errors_1 = require("./errors");
37
+ const keys_1 = require("./keys");
38
+ const mnemonic_1 = require("./mnemonic");
39
+ /**
40
+ * 账户派生路径。与 `@trustbase/protocol` 的 `HD_PATH` 同值
41
+ * (漂移由 `test/constants-drift.test.ts` 静态核对)。
42
+ */
43
+ exports.ACCOUNT_DERIVATION_PATH = "m/44'/118'/0'/0/0";
44
+ /** 助记词(+可选 passphrase)→ 64 字节 BIP39 种子。非法助记词抛 `INVALID_MNEMONIC` */
45
+ function mnemonicToSeed(mnemonic, passphrase = '') {
46
+ if (!(0, mnemonic_1.validateMnemonic)(mnemonic)) {
47
+ throw new errors_1.AccountError('INVALID_MNEMONIC', '助记词不合法(词表或校验和不对)—— 请逐词检查拼写');
48
+ }
49
+ // 先做空白/NFKD 归一化再交给 BIP39:种子是对助记词**字符串本身**做 PBKDF2,
50
+ // 多一个空格就是另一份种子(见 mnemonic.ts 的 normalizeMnemonic)
51
+ return (0, bip39_1.mnemonicToSeedSync)((0, mnemonic_1.normalizeMnemonic)(mnemonic), passphrase);
52
+ }
53
+ function assertPath(path) {
54
+ if (typeof path !== 'string' || !/^m(\/\d+'?)*$/u.test(path)) {
55
+ throw new errors_1.AccountError('INVALID_ARGUMENT', `派生路径非法:${String(path)}(形如 m/44'/118'/0'/0/0)`);
56
+ }
57
+ }
58
+ /**
59
+ * 助记词 → 账户密钥对 + 地址。
60
+ *
61
+ * 种子在函数内派生出私钥后立即清零(种子是我们自己分配的缓冲区)。
62
+ */
63
+ function deriveAccountFromMnemonic(mnemonic, options = {}) {
64
+ const path = options.path ?? exports.ACCOUNT_DERIVATION_PATH;
65
+ const hrp = options.hrp ?? address_1.ACCOUNT_ADDRESS_HRP;
66
+ assertPath(path);
67
+ const seed = mnemonicToSeed(mnemonic, options.passphrase ?? '');
68
+ try {
69
+ let node;
70
+ try {
71
+ node = bip32_1.HDKey.fromMasterSeed(seed).derive(path);
72
+ }
73
+ catch (error) {
74
+ throw new errors_1.AccountError('INVALID_ARGUMENT', `派生失败:${error instanceof Error ? error.message : String(error)}(路径 ${path})`);
75
+ }
76
+ const privateKey = node.privateKey;
77
+ if (!privateKey || privateKey.length === 0) {
78
+ throw new errors_1.AccountError('INVALID_MNEMONIC', '派生出的私钥为空 —— 助记词或路径有问题');
79
+ }
80
+ const privateKeyCopy = Uint8Array.from(privateKey);
81
+ const publicKey = (0, keys_1.publicKeyFromPrivateKey)(privateKeyCopy);
82
+ return { privateKey: privateKeyCopy, publicKey, address: (0, address_1.accountAddressFromPublicKey)(publicKey, hrp), path };
83
+ }
84
+ finally {
85
+ (0, bytes_1.wipe)(seed);
86
+ }
87
+ }
88
+ /**
89
+ * 已有私钥 → 账户(地址口径与助记词路径完全一致)。
90
+ *
91
+ * 用于备份恢复、测试向量、以及将来"把链上 keyring 里的密钥导进设备账户"。
92
+ */
93
+ function accountFromPrivateKey(privateKey, options = {}) {
94
+ const hrp = options.hrp ?? address_1.ACCOUNT_ADDRESS_HRP;
95
+ const privateKeyCopy = Uint8Array.from(privateKey);
96
+ const publicKey = (0, keys_1.publicKeyFromPrivateKey)(privateKeyCopy);
97
+ return { privateKey: privateKeyCopy, publicKey, address: (0, address_1.accountAddressFromPublicKey)(publicKey, hrp) };
98
+ }
99
+ /**
100
+ * 建一个新设备账户(助记词 + 密钥对 + 地址)。
101
+ *
102
+ * ⚠️ 调用方拿到 `mnemonic` 后应当立刻走"抄写门"(见 mnemonic.ts),
103
+ * 然后**只在内存里**留到写入加密 keystore 为止 —— 本包不落任何明文。
104
+ */
105
+ function createAccount(options = {}) {
106
+ const mnemonic = (0, mnemonic_1.generateMnemonic)(options.strength ?? mnemonic_1.DEFAULT_MNEMONIC_STRENGTH, options.rng);
107
+ const derived = deriveAccountFromMnemonic(mnemonic, options);
108
+ return { ...derived, mnemonic };
109
+ }
110
+ /** 助记词词表(界面上做拼写提示/校验用;只导出英文表,与生成口径一致) */
111
+ exports.WORDLIST_ENGLISH = english_1.wordlist;
112
+ //# sourceMappingURL=hd.js.map
package/dist/hd.js.map ADDED
@@ -0,0 +1 @@
1
+ {"version":3,"file":"hd.js","sourceRoot":"","sources":["../src/hd.ts"],"names":[],"mappings":";AAAA;;;;;;;;;;;;;;;;;;;;;;GAsBG;;;AAiDH,wCAOC;AAaD,8DA0BC;AAOD,sDAKC;AAoBD,sCAIC;AAjID,wCAAqC;AACrC,wCAAkD;AAClD,4DAA6E;AAE7E,uCAA6E;AAC7E,mCAA+B;AAC/B,qCAAwC;AACxC,iCAAiD;AACjD,yCAOoB;AAEpB;;;GAGG;AACU,QAAA,uBAAuB,GAAG,mBAAmB,CAAC;AAyB3D,oEAAoE;AACpE,SAAgB,cAAc,CAAC,QAAgB,EAAE,UAAU,GAAG,EAAE;IAC9D,IAAI,CAAC,IAAA,2BAAgB,EAAC,QAAQ,CAAC,EAAE,CAAC;QAChC,MAAM,IAAI,qBAAY,CAAC,kBAAkB,EAAE,4BAA4B,CAAC,CAAC;IAC3E,CAAC;IACD,mDAAmD;IACnD,kDAAkD;IAClD,OAAO,IAAA,0BAAkB,EAAC,IAAA,4BAAiB,EAAC,QAAQ,CAAC,EAAE,UAAU,CAAC,CAAC;AACrE,CA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AM,SAAS,GAAG,IAAA,8BAAuB,EAAC,cAAc,CAAC,CAAC;QAC1D,OAAO,EAAE,UAAU,EAAE,cAAc,EAAE,SAAS,EAAE,OAAO,EAAE,IAAA,qCAA2B,EAAC,SAAS,EAAE,GAAG,CAAC,EAAE,IAAI,EAAE,CAAC;IAC/G,CAAC;YAAS,CAAC;QACT,IAAA,YAAI,EAAC,IAAI,CAAC,CAAC;IACb,CAAC;AACH,CAAC;AAED;;;;GAIG;AACH,SAAgB,qBAAqB,CAAC,UAAsB,EAAE,UAA4B,EAAE;IAC1F,MAAM,GAAG,GAAG,OAAO,CAAC,GAAG,IAAI,6BAAmB,CAAC;IAC/C,MAAM,cAAc,GAAG,UAAU,CAAC,IAAI,CAAC,UAAU,CAAC,CAAC;IACnD,MAAM,SAAS,GAAG,IAAA,8BAAuB,EAAC,cAAc,CAAC,CAAC;IAC1D,OAAO,EAAE,UAAU,EAAE,cAAc,EAAE,SAAS,EAAE,OAAO,EAAE,IAAA,qCAA2B,EAAC,SAAS,EAAE,GAAG,CAAC,EAAE,CAAC;AACzG,CAAC;AAcD;;;;;GAKG;AACH,SAAgB,aAAa,CAAC,UAAgC,EAAE;IAC9D,MAAM,QAAQ,GAAG,IAAA,2BAAgB,EAAC,OAAO,CAAC,QAAQ,IAAI,oCAAyB,EAAE,OAAO,CAAC,GAAG,CAAC,CAAC;IAC9F,MAAM,OAAO,GAAG,yBAAyB,CAAC,QAAQ,EAAE,OAAO,CAAC,CAAC;IAC7D,OAAO,EAAE,GAAG,OAAO,EAAE,QAAQ,EAAE,CAAC;AAClC,CAAC;AAED,yCAAyC;AAC5B,QAAA,gBAAgB,GAAsB,kBAAe,CAAC"}
@@ -0,0 +1,60 @@
1
+ /**
2
+ * `@trustbase/account` —— 账户与密钥库(SDK 方案的"第一块砖")。
3
+ *
4
+ * 任何基于本 SDK 的 PWA 打开就要能"登录并管理自己的账户"(SDK 方案 §3.2),
5
+ * 本包提供这件事里所有**与密钥材料打交道**的部分,四块能力:
6
+ *
7
+ * | 模块 | 能力 | 依据 |
8
+ * |---|---|---|
9
+ * | `address` / `keys` / `bech32` | `tct1…` 地址派生(与链上账户地址同口径) | 入口文档 §8.3.2 ①②、SDK 方案 §3.2.3 |
10
+ * | `keystore` / `kdf` | 口令加密的密钥库(Argon2id + AES-256-GCM,版本化、参数落盘) | SDK 方案 §3.3.1 / §3.3.2 |
11
+ * | `prf` / `prf-webauthn` / `envelope` | WebAuthn PRF 硬件解锁(生物识别优先、口令回退,双轨解出同一份密钥材料) | SDK 方案 §3.3.5 / §3.3.7 |
12
+ * | `password` / `mnemonic` / `hd` | 口令强度估算(≥3 分 + 本地黑名单)与 BIP39 助记词 + 抄写门 | SDK 方案 §3.3.6 / §3.3.3 |
13
+ *
14
+ * ## 三条不变量(本包的"红线",改代码前先读)
15
+ *
16
+ * 1. **同构**:`src/` 里不得出现任何 Node 内建模块或 Node 全局
17
+ * (`node:` 导入 / `process.` / `Buffer` / `__dirname`)。
18
+ * 这条由 `test/isomorphic.test.ts` 静态扫描守着,不靠自觉。
19
+ * 2. **绝不托管**:本包不知道任何用户的明文;没有"找回密码"能力;
20
+ * 助记词是唯一退路(SDK 方案 §3.3.3)。**不提供任何把明文写进
21
+ * localStorage / sessionStorage 的路径**。
22
+ * 3. **一个数据一个来源**:地址 HRP(`tct`)与派生路径(`m/44'/118'/0'/0/0`)
23
+ * 以 `@trustbase/protocol` 为真源,本包同值声明并由 `test/constants-drift.test.ts`
24
+ * 静态核对 —— 不在源码里 import 它,是为了让本包能独立构建/发布。
25
+ *
26
+ * ## 典型用法(PWA 建户 + 解锁)
27
+ *
28
+ * ```ts
29
+ * // 1) 建户:生成助记词 → 派生账户 → 抄写门 → 写加密 keystore
30
+ * const account = createAccount(); // 12 词助记词 + 私钥 + tct1…
31
+ * const indices = pickTranscriptionIndices(account.mnemonic, 3);
32
+ * // …让用户回填,然后 checkTranscription(account.mnemonic, indices, answers)
33
+ * const envelope = await createUnlockEnvelope(account.privateKey, password, { minStrength: 3 });
34
+ * wipe(account.privateKey); // 明文用完就清
35
+ * // 只把 envelope / 助记词(纸密钥)交给持久化与用户
36
+ *
37
+ * // 2) 解锁(口令轨),拿到私钥去做签名
38
+ * const privateKey = await unlockEnvelope(envelope, { strategy: 'password', password });
39
+ *
40
+ * // 3) 之后开启生物识别:不需要重新加密,也不需要重新抄助记词
41
+ * const withPrf = await attachPrfTrack(envelope, { password, prfSecret, credentialId });
42
+ * ```
43
+ *
44
+ * 详细的格式规范、金标准向量、未验证清单见包 README。
45
+ */
46
+ export { AccountError, isAccountError, type AccountErrorCode } from './errors';
47
+ export { base64Decode, base64Encode, bytesToHex, bytesToUtf8, concatBytes, decodeBlobField, equalBytes, hexToBytes, utf8ToBytes, wipe, wipeRandom, zeroBytes, type BlobFieldLengthRule, } from './bytes';
48
+ export { AES_GCM_IV_LENGTH, AES_GCM_TAG_LENGTH, AES_KEY_LENGTH, aesGcmDecrypt, aesGcmEncrypt, isWebCryptoAvailable, randomBytes, requireSubtle, type CryptoKeyLike, type CryptoLike, type SubtleCryptoLike, } from './webcrypto';
49
+ export { BECH32_CHARSET, BECH32_MAX_LENGTH, bech32Decode, bech32Encode, bech32HrpExpand, bech32Polymod, convertBits, decodeBech32Address, encodeBech32Address, type Bech32Decoded, } from './bech32';
50
+ export { COMPRESSED_PUBLIC_KEY_LENGTH, PRIVATE_KEY_LENGTH, UNCOMPRESSED_PUBLIC_KEY_LENGTH, compressPublicKey, generatePrivateKey, isValidPrivateKey, isValidPublicKey, publicKeyFromPrivateKey, } from './keys';
51
+ export { ACCOUNT_ADDRESS_BYTES, ACCOUNT_ADDRESS_HRP, accountAddressBytes, accountAddressFromPrivateKey, accountAddressFromPublicKey, isAccountAddress, normalizeAccountAddress, parseAccountAddress, type ParsedAccountAddress, } from './address';
52
+ export { ARGON2ID_MIN_CREATE_PARAMS, ARGON2ID_VERSION, DEFAULT_ARGON2ID_PARAMS, DEFAULT_PBKDF2_ITERATIONS, DERIVED_KEY_LENGTH, KDF_ALGORITHMS, KDF_SALT_LENGTH, MAX_PASSWORD_LENGTH, PBKDF2_MIN_CREATE_ITERATIONS, UNTRUSTED_ARGON2ID_LIMITS, UNTRUSTED_PBKDF2_LIMITS, assertArgon2idParams, assertCreateTimeKdfParams, assertPbkdf2Params, deriveArgon2idKey, derivePbkdf2Key, generateSalt, isArgon2idAvailable, isKdfAlgorithm, passwordToKdfInput, resetArgon2AvailabilityCache, type Argon2idParams, type KdfAlgorithm, type Pbkdf2Params, } from './kdf';
53
+ export { KEYSTORE_AAD_PREFIX, KEYSTORE_CIPHER, KEYSTORE_VERSION, MAX_SECRET_MATERIAL_LENGTH, createKeystore, isKeystoreBlob, keystoreAad, keystoreAadText, parseKeystore, serializeKeystore, unlockKeystore, type CreateKeystoreOptions, type KeystoreBlob, type KeystoreHeader, } from './keystore';
54
+ export { PRF_SALT_DEFAULT_LENGTH, PRF_SALT_MAX_LENGTH, PRF_SALT_MIN_LENGTH, PRF_SECRET_LENGTH, PRF_WRAP_AAD_PREFIX, PRF_WRAP_CIPHER, PRF_WRAP_INFO, PRF_WRAP_KDF, PRF_WRAP_VERSION, createStaticPrfProvider, derivePrfKek, isPrfWrappedKey, isSameSecret, parsePrfWrappedKey, prfWrapAadText, serializePrfWrappedKey, unwrapKeyWithPrf, wrapKeyWithPrf, type PrfProvider, type PrfWrappedKey, type UnlockStrategy, type WrapKeyWithPrfOptions, } from './prf';
55
+ export { UNLOCK_ENVELOPE_AAD_PREFIX, UNLOCK_ENVELOPE_VERSION, DATA_KEY_LENGTH, attachPrfTrack, createUnlockEnvelope, detachPrfTrack, envelopeAadText, isUnlockEnvelope, parseUnlockEnvelope, serializeUnlockEnvelope, unlockEnvelope, type AttachPrfTrackRequest, type EnvelopePayload, type EnvelopeUnlockRequest, type UnlockEnvelope, } from './envelope';
56
+ export { base64UrlToBytes, bytesToBase64Url, createWebAuthnPrfProvider, isWebAuthnPrfSupported, registerWebAuthnPrfCredential, wipePrfSecret, type RegisterWebAuthnPrfOptions, type RegisteredWebAuthnPrfCredential, type WebAuthnPrfProviderOptions, } from './prf-webauthn';
57
+ export { ACCEPTABLE_SCORE, APP_BLOCKLIST, estimatePasswordStrength, isBlocklisted, type PasswordScore, type PasswordStrength, type PasswordStrengthOptions, } from './password';
58
+ export { DEFAULT_MNEMONIC_STRENGTH, MNEMONIC_STRENGTHS, checkTranscription, entropyToMnemonic, generateMnemonic, mnemonicToEntropy, normalizeMnemonic, pickTranscriptionIndices, splitMnemonicWords, validateMnemonic, type MnemonicStrength, type RandomSource, type TranscriptionAnswer, type TranscriptionResult, } from './mnemonic';
59
+ export { ACCOUNT_DERIVATION_PATH, WORDLIST_ENGLISH, accountFromPrivateKey, createAccount, deriveAccountFromMnemonic, mnemonicToSeed, type AccountKeyPair, type CreateAccountOptions, type CreatedAccount, type DeriveAccountOptions, type DerivedAccount, } from './hd';
60
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA4CG;AAEH,OAAO,EAAE,YAAY,EAAE,cAAc,EAAE,KAAK,gBAAgB,EAAE,MAAM,UAAU,CAAC;AAE/E,OAAO,EACL,YAAY,EACZ,YAAY,EACZ,UAAU,EACV,WAAW,EACX,WAAW,EACX,eAAe,EACf,UAAU,EACV,UAAU,EACV,WAAW,EACX,IAAI,EACJ,UAAU,EACV,SAAS,EACT,KAAK,mBAAmB,GACzB,MAAM,SAAS,CAAC;AAEjB,OAAO,EACL,iBAAiB,EACjB,kBAAkB,EAClB,cAAc,EACd,aAAa,EACb,aAAa,EACb,oBAAoB,EACpB,WAAW,EACX,aAAa,EACb,KAAK,aAAa,EAClB,KAAK,UAAU,EACf,KAAK,gBAAgB,GACtB,MAAM,aAAa,CAAC;AAErB,OAAO,EACL,cAAc,EACd,iBAAiB,EACjB,YAAY,EACZ,Y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DED
@@ -0,0 +1,199 @@
1
+ "use strict";
2
+ /**
3
+ * `@trustbase/account` —— 账户与密钥库(SDK 方案的"第一块砖")。
4
+ *
5
+ * 任何基于本 SDK 的 PWA 打开就要能"登录并管理自己的账户"(SDK 方案 §3.2),
6
+ * 本包提供这件事里所有**与密钥材料打交道**的部分,四块能力:
7
+ *
8
+ * | 模块 | 能力 | 依据 |
9
+ * |---|---|---|
10
+ * | `address` / `keys` / `bech32` | `tct1…` 地址派生(与链上账户地址同口径) | 入口文档 §8.3.2 ①②、SDK 方案 §3.2.3 |
11
+ * | `keystore` / `kdf` | 口令加密的密钥库(Argon2id + AES-256-GCM,版本化、参数落盘) | SDK 方案 §3.3.1 / §3.3.2 |
12
+ * | `prf` / `prf-webauthn` / `envelope` | WebAuthn PRF 硬件解锁(生物识别优先、口令回退,双轨解出同一份密钥材料) | SDK 方案 §3.3.5 / §3.3.7 |
13
+ * | `password` / `mnemonic` / `hd` | 口令强度估算(≥3 分 + 本地黑名单)与 BIP39 助记词 + 抄写门 | SDK 方案 §3.3.6 / §3.3.3 |
14
+ *
15
+ * ## 三条不变量(本包的"红线",改代码前先读)
16
+ *
17
+ * 1. **同构**:`src/` 里不得出现任何 Node 内建模块或 Node 全局
18
+ * (`node:` 导入 / `process.` / `Buffer` / `__dirname`)。
19
+ * 这条由 `test/isomorphic.test.ts` 静态扫描守着,不靠自觉。
20
+ * 2. **绝不托管**:本包不知道任何用户的明文;没有"找回密码"能力;
21
+ * 助记词是唯一退路(SDK 方案 §3.3.3)。**不提供任何把明文写进
22
+ * localStorage / sessionStorage 的路径**。
23
+ * 3. **一个数据一个来源**:地址 HRP(`tct`)与派生路径(`m/44'/118'/0'/0/0`)
24
+ * 以 `@trustbase/protocol` 为真源,本包同值声明并由 `test/constants-drift.test.ts`
25
+ * 静态核对 —— 不在源码里 import 它,是为了让本包能独立构建/发布。
26
+ *
27
+ * ## 典型用法(PWA 建户 + 解锁)
28
+ *
29
+ * ```ts
30
+ * // 1) 建户:生成助记词 → 派生账户 → 抄写门 → 写加密 keystore
31
+ * const account = createAccount(); // 12 词助记词 + 私钥 + tct1…
32
+ * const indices = pickTranscriptionIndices(account.mnemonic, 3);
33
+ * // …让用户回填,然后 checkTranscription(account.mnemonic, indices, answers)
34
+ * const envelope = await createUnlockEnvelope(account.privateKey, password, { minStrength: 3 });
35
+ * wipe(account.privateKey); // 明文用完就清
36
+ * // 只把 envelope / 助记词(纸密钥)交给持久化与用户
37
+ *
38
+ * // 2) 解锁(口令轨),拿到私钥去做签名
39
+ * const privateKey = await unlockEnvelope(envelope, { strategy: 'password', password });
40
+ *
41
+ * // 3) 之后开启生物识别:不需要重新加密,也不需要重新抄助记词
42
+ * const withPrf = await attachPrfTrack(envelope, { password, prfSecret, credentialId });
43
+ * ```
44
+ *
45
+ * 详细的格式规范、金标准向量、未验证清单见包 README。
46
+ */
47
+ Object.defineProperty(exports, "__esModule", { value: true });
48
+ exports.DEFAULT_ARGON2ID_PARAMS = exports.ARGON2ID_VERSION = exports.ARGON2ID_MIN_CREATE_PARAMS = exports.parseAccountAddress = exports.normalizeAccountAddress = exports.isAccountAddress = exports.accountAddressFromPublicKey = exports.accountAddressFromPrivateKey = exports.accountAddressBytes = exports.ACCOUNT_ADDRESS_HRP = exports.ACCOUNT_ADDRESS_BYTES = exports.publicKeyFromPrivateKey = exports.isValidPublicKey = exports.isValidPrivateKey = exports.generatePrivateKey = exports.compressPublicKey = exports.UNCOMPRESSED_PUBLIC_KEY_LENGTH = exports.PRIVATE_KEY_LENGTH = exports.COMPRESSED_PUBLIC_KEY_LENGTH = exports.encodeBech32Address = exports.decodeBech32Address = exports.convertBits = exports.bech32Polymod = exports.bech32HrpExpand = exports.bech32Encode = exports.bech32Decode = exports.BECH32_MAX_LENGTH = exports.BECH32_CHARSET = exports.requireSubtle = exports.randomBytes = exports.isWebCryptoAvailable = exports.aesGcmEncrypt = exports.aesGcmDecrypt = exports.AES_KEY_LENGTH = exports.AES_GCM_TAG_LENGTH = exports.AES_GCM_IV_LENGTH = exports.zeroBytes = exports.wipeRandom = exports.wipe = exports.utf8ToBytes = exports.hexToBytes = exports.equalBytes = exports.decodeBlobField = exports.concatBytes = exports.bytesToUtf8 = exports.bytesToHex = exports.base64Encode = exports.base64Decode = exports.isAccountError = exports.AccountError = void 0;
49
+ exports.DATA_KEY_LENGTH = exports.UNLOCK_ENVELOPE_VERSION = exports.UNLOCK_ENVELOPE_AAD_PREFIX = exports.wrapKeyWithPrf = exports.unwrapKeyWithPrf = exports.serializePrfWrappedKey = exports.prfWrapAadText = exports.parsePrfWrappedKey = exports.isSameSecret = exports.isPrfWrappedKey = exports.derivePrfKek = exports.createStaticPrfProvider = exports.PRF_WRAP_VERSION = exports.PRF_WRAP_KDF = exports.PRF_WRAP_INFO = exports.PRF_WRAP_CIPHER = exports.PRF_WRAP_AAD_PREFIX = exports.PRF_SECRET_LENGTH = exports.PRF_SALT_MIN_LENGTH = exports.PRF_SALT_MAX_LENGTH = exports.PRF_SALT_DEFAULT_LENGTH = exports.unlockKeystore = exports.serializeKeystore = exports.parseKeystore = exports.keystoreAadText = exports.keystoreAad = exports.isKeystoreBlob = exports.createKeystore = exports.MAX_SECRET_MATERIAL_LENGTH = exports.KEYSTORE_VERSION = exports.KEYSTORE_CIPHER = exports.KEYSTORE_AAD_PREFIX = exports.resetArgon2AvailabilityCache = exports.passwordToKdfInput = exports.isKdfAlgorithm = exports.isArgon2idAvailable = exports.generateSalt = exports.derivePbkdf2Key = exports.deriveArgon2idKey = exports.assertPbkdf2Params = exports.assertCreateTimeKdfParams = exports.assertArgon2idParams = exports.UNTRUSTED_PBKDF2_LIMITS = exports.UNTRUSTED_ARGON2ID_LIMITS = exports.PBKDF2_MIN_CREATE_ITERATIONS = exports.MAX_PASSWORD_LENGTH = exports.KDF_SALT_LENGTH = exports.KDF_ALGORITHMS = exports.DERIVED_KEY_LENGTH = exports.DEFAULT_PBKDF2_ITERATIONS = void 0;
50
+ exports.mnemonicToSeed = exports.deriveAccountFromMnemonic = exports.createAccount = exports.accountFromPrivateKey = exports.WORDLIST_ENGLISH = exports.ACCOUNT_DERIVATION_PATH = exports.validateMnemonic = exports.splitMnemonicWords = exports.pickTranscriptionIndices = exports.normalizeMnemonic = exports.mnemonicToEntropy = exports.generateMnemonic = exports.entropyToMnemonic = exports.checkTranscription = exports.MNEMONIC_STRENGTHS = exports.DEFAULT_MNEMONIC_STRENGTH = exports.isBlocklisted = exports.estimatePasswordStrength = exports.APP_BLOCKLIST = exports.ACCEPTABLE_SCORE = exports.wipePrfSecret = exports.registerWebAuthnPrfCredential = exports.isWebAuthnPrfSupported = exports.createWebAuthnPrfProvider = exports.bytesToBase64Url = exports.base64UrlToBytes = exports.unlockEnvelope = exports.serializeUnlockEnvelope = exports.parseUnlockEnvelope = exports.isUnlockEnvelope = exports.envelopeAadText = exports.detachPrfTrack = exports.createUnlockEnvelope = exports.attachPrfTrack = void 0;
51
+ var errors_1 = require("./errors");
52
+ Object.defineProperty(exports, "AccountError", { enumerable: true, get: function () { return errors_1.AccountError; } });
53
+ Object.defineProperty(exports, "isAccountError", { enumerable: true, get: function () { return errors_1.isAccountError; } });
54
+ var bytes_1 = require("./bytes");
55
+ Object.defineProperty(exports, "base64Decode", { enumerable: true, get: function () { return bytes_1.base64Decode; } });
56
+ Object.defineProperty(exports, "base64Encode", { enumerable: true, get: function () { return bytes_1.base64Encode; } });
57
+ Object.defineProperty(exports, "bytesToHex", { enumerable: true, get: function () { return bytes_1.bytesToHex; } });
58
+ Object.defineProperty(exports, "bytesToUtf8", { enumerable: true, get: function () { return bytes_1.bytesToUtf8; } });
59
+ Object.defineProperty(exports, "concatBytes", { enumerable: true, get: function () { return bytes_1.concatBytes; } });
60
+ Object.defineProperty(exports, "decodeBlobField", { enumerable: true, get: function () { return bytes_1.decodeBlobField; } });
61
+ Object.defineProperty(exports, "equalBytes", { enumerable: true, get: function () { return bytes_1.equalBytes; } });
62
+ Object.defineProperty(exports, "hexToBytes", { enumerable: true, get: function () { return bytes_1.hexToBytes; } });
63
+ Object.defineProperty(exports, "utf8ToBytes", { enumerable: true, get: function () { return bytes_1.utf8ToBytes; } });
64
+ Object.defineProperty(exports, "wipe", { enumerable: true, get: function () { return bytes_1.wipe; } });
65
+ Object.defineProperty(exports, "wipeRandom", { enumerable: true, get: function () { return bytes_1.wipeRandom; } });
66
+ Object.defineProperty(exports, "zeroBytes", { enumerable: true, get: function () { return bytes_1.zeroBytes; } });
67
+ var webcrypto_1 = require("./webcrypto");
68
+ Object.defineProperty(exports, "AES_GCM_IV_LENGTH", { enumerable: true, get: function () { return webcrypto_1.AES_GCM_IV_LENGTH; } });
69
+ Object.defineProperty(exports, "AES_GCM_TAG_LENGTH", { enumerable: true, get: function () { return webcrypto_1.AES_GCM_TAG_LENGTH; } });
70
+ Object.defineProperty(exports, "AES_KEY_LENGTH", { enumerable: true, get: function () { return webcrypto_1.AES_KEY_LENGTH; } });
71
+ Object.defineProperty(exports, "aesGcmDecrypt", { enumerable: true, get: function () { return webcrypto_1.aesGcmDecrypt; } });
72
+ Object.defineProperty(exports, "aesGcmEncrypt", { enumerable: true, get: function () { return webcrypto_1.aesGcmEncrypt; } });
73
+ Object.defineProperty(exports, "isWebCryptoAvailable", { enumerable: true, get: function () { return webcrypto_1.isWebCryptoAvailable; } });
74
+ Object.defineProperty(exports, "randomBytes", { enumerable: true, get: function () { return webcrypto_1.randomBytes; } });
75
+ Object.defineProperty(exports, "requireSubtle", { enumerable: true, get: function () { return webcrypto_1.requireSubtle; } });
76
+ var bech32_1 = require("./bech32");
77
+ Object.defineProperty(exports, "BECH32_CHARSET", { enumerable: true, get: function () { return bech32_1.BECH32_CHARSET; } });
78
+ Object.defineProperty(exports, "BECH32_MAX_LENGTH", { enumerable: true, get: function () { return bech32_1.BECH32_MAX_LENGTH; } });
79
+ Object.defineProperty(exports, "bech32Decode", { enumerable: true, get: function () { return bech32_1.bech32Decode; } });
80
+ Object.defineProperty(exports, "bech32Encode", { enumerable: true, get: function () { return bech32_1.bech32Encode; } });
81
+ Object.defineProperty(exports, "bech32HrpExpand", { enumerable: true, get: function () { return bech32_1.bech32HrpExpand; } });
82
+ Object.defineProperty(exports, "bech32Polymod", { enumerable: true, get: function () { return bech32_1.bech32Polymod; } });
83
+ Object.defineProperty(exports, "convertBits", { enumerable: true, get: function () { return bech32_1.convertBits; } });
84
+ Object.defineProperty(exports, "decodeBech32Address", { enumerable: true, get: function () { return bech32_1.decodeBech32Address; } });
85
+ Object.defineProperty(exports, "encodeBech32Address", { enumerable: true, get: function () { return bech32_1.encodeBech32Address; } });
86
+ var keys_1 = require("./keys");
87
+ Object.defineProperty(exports, "COMPRESSED_PUBLIC_KEY_LENGTH", { enumerable: true, get: function () { return keys_1.COMPRESSED_PUBLIC_KEY_LENGTH; } });
88
+ Object.defineProperty(exports, "PRIVATE_KEY_LENGTH", { enumerable: true, get: function () { return keys_1.PRIVATE_KEY_LENGTH; } });
89
+ Object.defineProperty(exports, "UNCOMPRESSED_PUBLIC_KEY_LENGTH", { enumerable: true, get: function () { return keys_1.UNCOMPRESSED_PUBLIC_KEY_LENGTH; } });
90
+ Object.defineProperty(exports, "compressPublicKey", { enumerable: true, get: function () { return keys_1.compressPublicKey; } });
91
+ Object.defineProperty(exports, "generatePrivateKey", { enumerable: true, get: function () { return keys_1.generatePrivateKey; } });
92
+ Object.defineProperty(exports, "isValidPrivateKey", { enumerable: true, get: function () { return keys_1.isValidPrivateKey; } });
93
+ Object.defineProperty(exports, "isValidPublicKey", { enumerable: true, get: function () { return keys_1.isValidPublicKey; } });
94
+ Object.defineProperty(exports, "publicKeyFromPrivateKey", { enumerable: true, get: function () { return keys_1.publicKeyFromPrivateKey; } });
95
+ var address_1 = require("./address");
96
+ Object.defineProperty(exports, "ACCOUNT_ADDRESS_BYTES", { enumerable: true, get: function () { return address_1.ACCOUNT_ADDRESS_BYTES; } });
97
+ Object.defineProperty(exports, "ACCOUNT_ADDRESS_HRP", { enumerable: true, get: function () { return address_1.ACCOUNT_ADDRESS_HRP; } });
98
+ Object.defineProperty(exports, "accountAddressBytes", { enumerable: true, get: function () { return address_1.accountAddressBytes; } });
99
+ Object.defineProperty(exports, "accountAddressFromPrivateKey", { enumerable: true, get: function () { return address_1.accountAddressFromPrivateKey; } });
100
+ Object.defineProperty(exports, "accountAddressFromPublicKey", { enumerable: true, get: function () { return address_1.accountAddressFromPublicKey; } });
101
+ Object.defineProperty(exports, "isAccountAddress", { enumerable: true, get: function () { return address_1.isAccountAddress; } });
102
+ Object.defineProperty(exports, "normalizeAccountAddress", { enumerable: true, get: function () { return address_1.normalizeAccountAddress; } });
103
+ Object.defineProperty(exports, "parseAccountAddress", { enumerable: true, get: function () { return address_1.parseAccountAddress; } });
104
+ var kdf_1 = require("./kdf");
105
+ Object.defineProperty(exports, "ARGON2ID_MIN_CREATE_PARAMS", { enumerable: true, get: function () { return kdf_1.ARGON2ID_MIN_CREATE_PARAMS; } });
106
+ Object.defineProperty(exports, "ARGON2ID_VERSION", { enumerable: true, get: function () { return kdf_1.ARGON2ID_VERSION; } });
107
+ Object.defineProperty(exports, "DEFAULT_ARGON2ID_PARAMS", { enumerable: true, get: function () { return kdf_1.DEFAULT_ARGON2ID_PARAMS; } });
108
+ Object.defineProperty(exports, "DEFAULT_PBKDF2_ITERATIONS", { enumerable: true, get: function () { return kdf_1.DEFAULT_PBKDF2_ITERATIONS; } });
109
+ Object.defineProperty(exports, "DERIVED_KEY_LENGTH", { enumerable: true, get: function () { return kdf_1.DERIVED_KEY_LENGTH; } });
110
+ Object.defineProperty(exports, "KDF_ALGORITHMS", { enumerable: true, get: function () { return kdf_1.KDF_ALGORITHMS; } });
111
+ Object.defineProperty(exports, "KDF_SALT_LENGTH", { enumerable: true, get: function () { return kdf_1.KDF_SALT_LENGTH; } });
112
+ Object.defineProperty(exports, "MAX_PASSWORD_LENGTH", { enumerable: true, get: function () { return kdf_1.MAX_PASSWORD_LENGTH; } });
113
+ Object.defineProperty(exports, "PBKDF2_MIN_CREATE_ITERATIONS", { enumerable: true, get: function () { return kdf_1.PBKDF2_MIN_CREATE_ITERATIONS; } });
114
+ Object.defineProperty(exports, "UNTRUSTED_ARGON2ID_LIMITS", { enumerable: true, get: function () { return kdf_1.UNTRUSTED_ARGON2ID_LIMITS; } });
115
+ Object.defineProperty(exports, "UNTRUSTED_PBKDF2_LIMITS", { enumerable: true, get: function () { return kdf_1.UNTRUSTED_PBKDF2_LIMITS; } });
116
+ Object.defineProperty(exports, "assertArgon2idParams", { enumerable: true, get: function () { return kdf_1.assertArgon2idParams; } });
117
+ Object.defineProperty(exports, "assertCreateTimeKdfParams", { enumerable: true, get: function () { return kdf_1.assertCreateTimeKdfParams; } });
118
+ Object.defineProperty(exports, "assertPbkdf2Params", { enumerable: true, get: function () { return kdf_1.assertPbkdf2Params; } });
119
+ Object.defineProperty(exports, "deriveArgon2idKey", { enumerable: true, get: function () { return kdf_1.deriveArgon2idKey; } });
120
+ Object.defineProperty(exports, "derivePbkdf2Key", { enumerable: true, get: function () { return kdf_1.derivePbkdf2Key; } });
121
+ Object.defineProperty(exports, "generateSalt", { enumerable: true, get: function () { return kdf_1.generateSalt; } });
122
+ Object.defineProperty(exports, "isArgon2idAvailable", { enumerable: true, get: function () { return kdf_1.isArgon2idAvailable; } });
123
+ Object.defineProperty(exports, "isKdfAlgorithm", { enumerable: true, get: function () { return kdf_1.isKdfAlgorithm; } });
124
+ Object.defineProperty(exports, "passwordToKdfInput", { enumerable: true, get: function () { return kdf_1.passwordToKdfInput; } });
125
+ Object.defineProperty(exports, "resetArgon2AvailabilityCache", { enumerable: true, get: function () { return kdf_1.resetArgon2AvailabilityCache; } });
126
+ var keystore_1 = require("./keystore");
127
+ Object.defineProperty(exports, "KEYSTORE_AAD_PREFIX", { enumerable: true, get: function () { return keystore_1.KEYSTORE_AAD_PREFIX; } });
128
+ Object.defineProperty(exports, "KEYSTORE_CIPHER", { enumerable: true, get: function () { return keystore_1.KEYSTORE_CIPHER; } });
129
+ Object.defineProperty(exports, "KEYSTORE_VERSION", { enumerable: true, get: function () { return keystore_1.KEYSTORE_VERSION; } });
130
+ Object.defineProperty(exports, "MAX_SECRET_MATERIAL_LENGTH", { enumerable: true, get: function () { return keystore_1.MAX_SECRET_MATERIAL_LENGTH; } });
131
+ Object.defineProperty(exports, "createKeystore", { enumerable: true, get: function () { return keystore_1.createKeystore; } });
132
+ Object.defineProperty(exports, "isKeystoreBlob", { enumerable: true, get: function () { return keystore_1.isKeystoreBlob; } });
133
+ Object.defineProperty(exports, "keystoreAad", { enumerable: true, get: function () { return keystore_1.keystoreAad; } });
134
+ Object.defineProperty(exports, "keystoreAadText", { enumerable: true, get: function () { return keystore_1.keystoreAadText; } });
135
+ Object.defineProperty(exports, "parseKeystore", { enumerable: true, get: function () { return keystore_1.parseKeystore; } });
136
+ Object.defineProperty(exports, "serializeKeystore", { enumerable: true, get: function () { return keystore_1.serializeKeystore; } });
137
+ Object.defineProperty(exports, "unlockKeystore", { enumerable: true, get: function () { return keystore_1.unlockKeystore; } });
138
+ var prf_1 = require("./prf");
139
+ Object.defineProperty(exports, "PRF_SALT_DEFAULT_LENGTH", { enumerable: true, get: function () { return prf_1.PRF_SALT_DEFAULT_LENGTH; } });
140
+ Object.defineProperty(exports, "PRF_SALT_MAX_LENGTH", { enumerable: true, get: function () { return prf_1.PRF_SALT_MAX_LENGTH; } });
141
+ Object.defineProperty(exports, "PRF_SALT_MIN_LENGTH", { enumerable: true, get: function () { return prf_1.PRF_SALT_MIN_LENGTH; } });
142
+ Object.defineProperty(exports, "PRF_SECRET_LENGTH", { enumerable: true, get: function () { return prf_1.PRF_SECRET_LENGTH; } });
143
+ Object.defineProperty(exports, "PRF_WRAP_AAD_PREFIX", { enumerable: true, get: function () { return prf_1.PRF_WRAP_AAD_PREFIX; } });
144
+ Object.defineProperty(exports, "PRF_WRAP_CIPHER", { enumerable: true, get: function () { return prf_1.PRF_WRAP_CIPHER; } });
145
+ Object.defineProperty(exports, "PRF_WRAP_INFO", { enumerable: true, get: function () { return prf_1.PRF_WRAP_INFO; } });
146
+ Object.defineProperty(exports, "PRF_WRAP_KDF", { enumerable: true, get: function () { return prf_1.PRF_WRAP_KDF; } });
147
+ Object.defineProperty(exports, "PRF_WRAP_VERSION", { enumerable: true, get: function () { return prf_1.PRF_WRAP_VERSION; } });
148
+ Object.defineProperty(exports, "createStaticPrfProvider", { enumerable: true, get: function () { return prf_1.createStaticPrfProvider; } });
149
+ Object.defineProperty(exports, "derivePrfKek", { enumerable: true, get: function () { return prf_1.derivePrfKek; } });
150
+ Object.defineProperty(exports, "isPrfWrappedKey", { enumerable: true, get: function () { return prf_1.isPrfWrappedKey; } });
151
+ Object.defineProperty(exports, "isSameSecret", { enumerable: true, get: function () { return prf_1.isSameSecret; } });
152
+ Object.defineProperty(exports, "parsePrfWrappedKey", { enumerable: true, get: function () { return prf_1.parsePrfWrappedKey; } });
153
+ Object.defineProperty(exports, "prfWrapAadText", { enumerable: true, get: function () { return prf_1.prfWrapAadText; } });
154
+ Object.defineProperty(exports, "serializePrfWrappedKey", { enumerable: true, get: function () { return prf_1.serializePrfWrappedKey; } });
155
+ Object.defineProperty(exports, "unwrapKeyWithPrf", { enumerable: true, get: function () { return prf_1.unwrapKeyWithPrf; } });
156
+ Object.defineProperty(exports, "wrapKeyWithPrf", { enumerable: true, get: function () { return prf_1.wrapKeyWithPrf; } });
157
+ var envelope_1 = require("./envelope");
158
+ Object.defineProperty(exports, "UNLOCK_ENVELOPE_AAD_PREFIX", { enumerable: true, get: function () { return envelope_1.UNLOCK_ENVELOPE_AAD_PREFIX; } });
159
+ Object.defineProperty(exports, "UNLOCK_ENVELOPE_VERSION", { enumerable: true, get: function () { return envelope_1.UNLOCK_ENVELOPE_VERSION; } });
160
+ Object.defineProperty(exports, "DATA_KEY_LENGTH", { enumerable: true, get: function () { return envelope_1.DATA_KEY_LENGTH; } });
161
+ Object.defineProperty(exports, "attachPrfTrack", { enumerable: true, get: function () { return envelope_1.attachPrfTrack; } });
162
+ Object.defineProperty(exports, "createUnlockEnvelope", { enumerable: true, get: function () { return envelope_1.createUnlockEnvelope; } });
163
+ Object.defineProperty(exports, "detachPrfTrack", { enumerable: true, get: function () { return envelope_1.detachPrfTrack; } });
164
+ Object.defineProperty(exports, "envelopeAadText", { enumerable: true, get: function () { return envelope_1.envelopeAadText; } });
165
+ Object.defineProperty(exports, "isUnlockEnvelope", { enumerable: true, get: function () { return envelope_1.isUnlockEnvelope; } });
166
+ Object.defineProperty(exports, "parseUnlockEnvelope", { enumerable: true, get: function () { return envelope_1.parseUnlockEnvelope; } });
167
+ Object.defineProperty(exports, "serializeUnlockEnvelope", { enumerable: true, get: function () { return envelope_1.serializeUnlockEnvelope; } });
168
+ Object.defineProperty(exports, "unlockEnvelope", { enumerable: true, get: function () { return envelope_1.unlockEnvelope; } });
169
+ var prf_webauthn_1 = require("./prf-webauthn");
170
+ Object.defineProperty(exports, "base64UrlToBytes", { enumerable: true, get: function () { return prf_webauthn_1.base64UrlToBytes; } });
171
+ Object.defineProperty(exports, "bytesToBase64Url", { enumerable: true, get: function () { return prf_webauthn_1.bytesToBase64Url; } });
172
+ Object.defineProperty(exports, "createWebAuthnPrfProvider", { enumerable: true, get: function () { return prf_webauthn_1.createWebAuthnPrfProvider; } });
173
+ Object.defineProperty(exports, "isWebAuthnPrfSupported", { enumerable: true, get: function () { return prf_webauthn_1.isWebAuthnPrfSupported; } });
174
+ Object.defineProperty(exports, "registerWebAuthnPrfCredential", { enumerable: true, get: function () { return prf_webauthn_1.registerWebAuthnPrfCredential; } });
175
+ Object.defineProperty(exports, "wipePrfSecret", { enumerable: true, get: function () { return prf_webauthn_1.wipePrfSecret; } });
176
+ var password_1 = require("./password");
177
+ Object.defineProperty(exports, "ACCEPTABLE_SCORE", { enumerable: true, get: function () { return password_1.ACCEPTABLE_SCORE; } });
178
+ Object.defineProperty(exports, "APP_BLOCKLIST", { enumerable: true, get: function () { return password_1.APP_BLOCKLIST; } });
179
+ Object.defineProperty(exports, "estimatePasswordStrength", { enumerable: true, get: function () { return password_1.estimatePasswordStrength; } });
180
+ Object.defineProperty(exports, "isBlocklisted", { enumerable: true, get: function () { return password_1.isBlocklisted; } });
181
+ var mnemonic_1 = require("./mnemonic");
182
+ Object.defineProperty(exports, "DEFAULT_MNEMONIC_STRENGTH", { enumerable: true, get: function () { return mnemonic_1.DEFAULT_MNEMONIC_STRENGTH; } });
183
+ Object.defineProperty(exports, "MNEMONIC_STRENGTHS", { enumerable: true, get: function () { return mnemonic_1.MNEMONIC_STRENGTHS; } });
184
+ Object.defineProperty(exports, "checkTranscription", { enumerable: true, get: function () { return mnemonic_1.checkTranscription; } });
185
+ Object.defineProperty(exports, "entropyToMnemonic", { enumerable: true, get: function () { return mnemonic_1.entropyToMnemonic; } });
186
+ Object.defineProperty(exports, "generateMnemonic", { enumerable: true, get: function () { return mnemonic_1.generateMnemonic; } });
187
+ Object.defineProperty(exports, "mnemonicToEntropy", { enumerable: true, get: function () { return mnemonic_1.mnemonicToEntropy; } });
188
+ Object.defineProperty(exports, "normalizeMnemonic", { enumerable: true, get: function () { return mnemonic_1.normalizeMnemonic; } });
189
+ Object.defineProperty(exports, "pickTranscriptionIndices", { enumerable: true, get: function () { return mnemonic_1.pickTranscriptionIndices; } });
190
+ Object.defineProperty(exports, "splitMnemonicWords", { enumerable: true, get: function () { return mnemonic_1.splitMnemonicWords; } });
191
+ Object.defineProperty(exports, "validateMnemonic", { enumerable: true, get: function () { return mnemonic_1.validateMnemonic; } });
192
+ var hd_1 = require("./hd");
193
+ Object.defineProperty(exports, "ACCOUNT_DERIVATION_PATH", { enumerable: true, get: function () { return hd_1.ACCOUNT_DERIVATION_PATH; } });
194
+ Object.defineProperty(exports, "WORDLIST_ENGLISH", { enumerable: true, get: function () { return hd_1.WORDLIST_ENGLISH; } });
195
+ Object.defineProperty(exports, "accountFromPrivateKey", { enumerable: true, get: function () { return hd_1.accountFromPrivateKey; } });
196
+ Object.defineProperty(exports, "createAccount", { enumerable: true, get: function () { return hd_1.createAccount; } });
197
+ Object.defineProperty(exports, "deriveAccountFromMnemonic", { enumerable: true, get: function () { return hd_1.deriveAccountFromMnemonic; } });
198
+ Object.defineProperty(exports, "mnemonicToSeed", { enumerable: true, get: function () { return hd_1.mnemonicToSeed; } });
199
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA4CG;;;;;AAEH,mCAA+E;AAAtE,sGAAA,YAAY,OAAA;AAAE,wGAAA,cAAc,OAAA;AAErC,iCAciB;AAbf,qGAAA,YAAY,OAAA;AACZ,qGAAA,YAAY,OAAA;AACZ,mGAAA,UAAU,OAAA;AACV,oGAAA,WAAW,OAAA;AACX,oGAAA,WAAW,OAAA;AACX,wGAAA,eAAe,OAAA;AACf,mGAAA,UAAU,OAAA;AACV,mGAAA,UAAU,OAAA;AACV,oGAAA,WAAW,OAAA;AACX,6FAAA,IAAI,OAAA;AACJ,mGAAA,UAAU,OAAA;AACV,kGAAA,SAAS,OAAA;AAIX,yCAYqB;AAXnB,8GAAA,iBAAiB,OAAA;AACjB,+GAAA,kBAAkB,OAAA;AAClB,2GAAA,cAAc,OAAA;AACd,0GAAA,aAAa,OAAA;AACb,0GAAA,aAAa,OAAA;AACb,iHAAA,oBAAoB,OAAA;AACpB,wGAAA,WAAW,OAAA;AACX,0GAAA,aAAa,OAAA;AAMf,mCAWkB;AAVhB,wGAAA,cAAc,OAAA;AACd,2GAAA,iBAAiB,OAAA;AACjB,sGAAA,YAAY,OAAA;AACZ,sGAAA,YAAY,OAAA;AACZ,yGAAA,eAAe,OAAA;AACf,uGAAA,aAAa,OAAA;AACb,qGAAA,WAAW,OAAA;AACX,6GAAA,mBAAmB,OAAA;AACnB,6GAAA,mBAAmB,OAAA;AAIrB,+BASgB;AARd,oHAAA,4BAA4B,OAAA;AAC5B,0GAAA,kBAAk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;AAClB,8GAAA,kBAAkB,OAAA;AAClB,6GAAA,iBAAiB,OAAA;AACjB,4GAAA,gBAAgB,OAAA;AAChB,6GAAA,iBAAiB,OAAA;AACjB,6GAAA,iBAAiB,OAAA;AACjB,oHAAA,wBAAwB,OAAA;AACxB,8GAAA,kBAAkB,OAAA;AAClB,4GAAA,gBAAgB,OAAA;AAOlB,2BAYc;AAXZ,6GAAA,uBAAuB,OAAA;AACvB,sGAAA,gBAAgB,OAAA;AAChB,2GAAA,qBAAqB,OAAA;AACrB,mGAAA,aAAa,OAAA;AACb,+GAAA,yBAAyB,OAAA;AACzB,oGAAA,cAAc,OAAA"}
package/dist/kdf.d.ts ADDED
@@ -0,0 +1,163 @@
1
+ /**
2
+ * 口令 → 密钥(KDF 层):Argon2id 优先,PBKDF2-HMAC-SHA256 兜底。
3
+ *
4
+ * ## 这一层的三条硬规则
5
+ *
6
+ * 1. **参数随密文落盘,解锁时只认密文里的参数**。同一个口令 + 同一份盐 +
7
+ * 不同参数 = 不同的密钥。所以"解锁时按当前默认参数重算"是致命 bug:
8
+ * 它会解不开旧密文,而报错看起来像"密码错了"。参数里每个字段都必须参与
9
+ * 认证(见 keystore 的 AAD)。
10
+ * 2. **不在解锁时偷偷换 KDF**。若密文声明 `argon2id` 而当前环境没有 WASM,
11
+ * 正确行为是**报错**(`UNSUPPORTED_KDF`),不是"用 PBKDF2 试试" ——
12
+ * 后者会派生出另一个密钥,把"环境不支持"伪装成"口令错"。
13
+ * (SDK 方案 §3.3.1:PBKDF2 是**创建时**的退化方案,不是解锁时的备胎。)
14
+ * 3. **不可信参数必须先过界再算**。密文可能来自任何地方,`memory: 2^30` 的
15
+ * blob 就是一次 DoS。所以先 `assert*Params` 再派生。
16
+ *
17
+ * ## 参数默认值的依据
18
+ *
19
+ * | 项 | 默认 | 依据 |
20
+ * |---|---|---|
21
+ * | Argon2id memory / cost / parallelism | 19456 KiB / 2 / 1 | OWASP Password Storage Cheat Sheet 的 Argon2id 推荐下限(19 MiB, t=2, p=1)。本机实测 ~40ms(WASM,见包 README) |
22
+ * | Argon2id 版本 | 19(`0x13`) | hash-wasm 内置 Argon2 v1.3(其 README 明确写 "Argon2d, Argon2i, Argon2id (v1.3)");别的版本号一律拒,不猜 |
23
+ * | PBKDF2 iterations | 600000 | SDK 方案 §3.3.1 规定的下限(仅无 WASM 时使用);本实现不允许低于它**创建** |
24
+ * | 盐 | 16 字节随机 | 每次创建新随机,绝不复用(复用 = 同口令同密钥) |
25
+ * | 派生密钥长度 | 32 字节 | AES-256-GCM 的密钥长度;**格式 v1 固定 32**,将来要加长就换版本号 |
26
+ *
27
+ * ## 口令的归一化(跨平台一致性)
28
+ *
29
+ * 口令先做 **NFC** 归一化再转 UTF-8。理由不是"安全"而是"能打开":iOS/Android/
30
+ * 桌面的输入法对 `é` 这类字符给的是"预组合"还是"组合"序列并不一致,不做归一
31
+ * 会让用户**明明输对了却打不开**(而且我们会说"口令错误")。用 NFC(不是 NFKC)
32
+ * 是有意的:NFC 只做**规范等价**合并,不改变字面;NFKC 会把全角变半角、
33
+ * `①②` 变 `12`,那是真的改口令。将来若有别的语言重写本包,这一步必须一致,
34
+ * 否则跨端互通会碎。
35
+ */
36
+ /** 本实现产出的派生密钥长度(AES-256 用);格式 v1 固定这个值 */
37
+ export declare const DERIVED_KEY_LENGTH = 32;
38
+ /** 盐长度(字节) */
39
+ export declare const KDF_SALT_LENGTH = 16;
40
+ /** hash-wasm 内置的 Argon2 版本(v1.3 = 19);其它版本号不认 */
41
+ export declare const ARGON2ID_VERSION = 19;
42
+ /** Argon2id 默认参数(OWASP 下限,见文件头) */
43
+ export declare const DEFAULT_ARGON2ID_PARAMS: {
44
+ readonly memory: 19456;
45
+ readonly cost: 2;
46
+ readonly parallelism: 1;
47
+ readonly hashLength: 32;
48
+ readonly version: 19;
49
+ };
50
+ /** Argon2id **创建**时的参数下限(低于它 = 不符合设计,直接拒) */
51
+ export declare const ARGON2ID_MIN_CREATE_PARAMS: {
52
+ readonly memory: 19456;
53
+ readonly cost: 2;
54
+ };
55
+ /** PBKDF2 默认迭代次数 */
56
+ export declare const DEFAULT_PBKDF2_ITERATIONS = 600000;
57
+ /** PBKDF2 **创建**时的迭代次数下限(SDK 方案 §3.3.1「≥60 万次」) */
58
+ export declare const PBKDF2_MIN_CREATE_ITERATIONS = 600000;
59
+ /** 口令长度上限(SDK 方案 §3.3.6「上限制长度(≤128)防 DoS」) */
60
+ export declare const MAX_PASSWORD_LENGTH = 128;
61
+ /**
62
+ * 不可信 KDF 参数的取值边界(**解码外来密文**时防 DoS 用)。
63
+ * 上限取"远高于任何合理参数、又低到打不出资源灾难"的位置。
64
+ */
65
+ export declare const UNTRUSTED_ARGON2ID_LIMITS: {
66
+ readonly memory: {
67
+ readonly min: 8;
68
+ readonly max: 1048576;
69
+ };
70
+ readonly cost: {
71
+ readonly min: 1;
72
+ readonly max: 1000;
73
+ };
74
+ readonly parallelism: {
75
+ readonly min: 1;
76
+ readonly max: 64;
77
+ };
78
+ };
79
+ export declare const UNTRUSTED_PBKDF2_LIMITS: {
80
+ readonly iterations: {
81
+ readonly min: 1;
82
+ readonly max: 10000000;
83
+ };
84
+ };
85
+ export type KdfAlgorithm = 'argon2id' | 'pbkdf2';
86
+ export interface Argon2idParams {
87
+ /** 盐(base64) */
88
+ salt: string;
89
+ /** 内存开销,单位 KiB(Argon2 规范 `m`) */
90
+ memory: number;
91
+ /** 迭代次数(Argon2 规范 `t`) */
92
+ cost: number;
93
+ /** 并行度(Argon2 规范 `p`;hash-wasm 串行执行,但该值参与派生,必须原样保留) */
94
+ parallelism: number;
95
+ /** 派生密钥长度(字节)。格式 v1 固定 32 */
96
+ hashLength: number;
97
+ /** Argon2 版本号(19 = v1.3) */
98
+ version: number;
99
+ }
100
+ export interface Pbkdf2Params {
101
+ /** 盐(base64) */
102
+ salt: string;
103
+ iterations: number;
104
+ /** 派生密钥长度(字节)。格式 v1 固定 32 */
105
+ hashLength: number;
106
+ /** 摘要算法;只有 `sha256` —— 别的写法不认(不猜) */
107
+ hash: 'sha256';
108
+ }
109
+ export declare const KDF_ALGORITHMS: readonly KdfAlgorithm[];
110
+ export declare function isKdfAlgorithm(value: unknown): value is KdfAlgorithm;
111
+ /**
112
+ * 口令 → KDF 输入字节(NFC 归一 + UTF-8 + 长度上限)。
113
+ *
114
+ * 返回的是**我们自己的缓冲区**,用完可以 `wipe()`;口令字符串本身清不掉
115
+ * (见 bytes.ts 的 `wipe` 说明),所以这一层的价值是"把不可清的东西缩小到
116
+ * 一次函数调用的范围"。
117
+ */
118
+ export declare function passwordToKdfInput(password: string): Uint8Array;
119
+ /** 校验(不可信的)Argon2id 参数;不合法抛 `INVALID_BLOB` / `UNSUPPORTED_KDF` */
120
+ export declare function assertArgon2idParams(value: unknown): asserts value is Argon2idParams;
121
+ /** 校验(不可信的)PBKDF2 参数;不合法抛 `INVALID_BLOB` / `UNSUPPORTED_KDF` */
122
+ export declare function assertPbkdf2Params(value: unknown): asserts value is Pbkdf2Params;
123
+ /**
124
+ * **创建**时的参数下限校验(调用方给的参数是我们自己的口径,不合格就拒)。
125
+ *
126
+ * 与解锁路径分开:解锁要"照 blob 走",不能因为参数低于今天的下限就拒
127
+ * —— 那是别人(或旧版本的我们)的密文,拒了等于把人锁在门外。
128
+ */
129
+ export declare function assertCreateTimeKdfParams(algorithm: KdfAlgorithm, params: Argon2idParams | Pbkdf2Params): void;
130
+ /**
131
+ * 真跑一次的探测(不是只查 `typeof WebAssembly`)。
132
+ *
133
+ * 为什么要真跑:CSP 里没有 `wasm-unsafe-eval` 时 `WebAssembly.instantiate`
134
+ * 会抛错,而 `typeof WebAssembly === 'object'` 照样为 true —— 只查存在性
135
+ * 会朝"能创建 keystore、将来打不开"的方向失败。这里用一个 8 KiB / 1 迭代的
136
+ * 极小参数实跑一次,失败即判定不可用(于是创建时退到 PBKDF2)。
137
+ *
138
+ * 结果被缓存(包括失败结果):探测本身约几毫秒,但没必要每次创建都付。
139
+ */
140
+ export declare function isArgon2idAvailable(): Promise<boolean>;
141
+ /** 仅供测试:清掉探测缓存(例如模拟"这台设备没有 WASM") */
142
+ export declare function resetArgon2AvailabilityCache(): void;
143
+ /**
144
+ * Argon2id 派生。本环境跑不起来(没有 WASM / 被 CSP 拦下 / 模块加载失败)时
145
+ * 抛 `UNSUPPORTED_KDF` —— **不回退**,理由见文件头第 2 条。
146
+ *
147
+ * 注意这里把底层调用**整个**包在 try/catch 里:hash-wasm 的模块能加载、
148
+ * 但 WASM 实例化失败的情况(CSP 缺 `wasm-unsafe-eval`)是在**调用时**才炸的,
149
+ * 如果只检查"模块加载成功"就会把浏览器原生的错误直接抛给上层,
150
+ * 而调用方需要的是"这个环境跑不了 Argon2id"这个可分支的语义。
151
+ */
152
+ export declare function deriveArgon2idKey(params: Argon2idParams, kdfInput: Uint8Array): Promise<Uint8Array>;
153
+ /**
154
+ * PBKDF2-HMAC-SHA256 派生(无 WASM 时的兜底路径)。
155
+ *
156
+ * 用 `@noble/hashes/pbkdf2` 而不是 WebCrypto 的 `deriveBits`:一是确定性更强
157
+ * (同构环境给同样的字节,便于与 OpenSSL 对照复算),二是这条路径的定位就是
158
+ * "平台能力缺失时的备胎",不该再依赖平台。
159
+ */
160
+ export declare function derivePbkdf2Key(params: Pbkdf2Params, kdfInput: Uint8Array): Promise<Uint8Array>;
161
+ /** 生成一个新盐(base64)—— 只用于**创建**路径 */
162
+ export declare function generateSalt(): string;
163
+ //# sourceMappingURL=kdf.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"kdf.d.ts","sourceRoot":"","sources":["../src/kdf.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAkCG;AASH,0CAA0C;AAC1C,eAAO,MAAM,kBAAkB,KAAK,CAAC;AACrC,cAAc;AACd,eAAO,MAAM,eAAe,KAAK,CAAC;AAClC,iDAAiD;AACjD,eAAO,MAAM,gBAAgB,KAAK,CAAC;AACnC,mCAAmC;AACnC,eAAO,MAAM,uB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