@thru/crypto 0.1.29 → 0.1.33

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/index.d.ts CHANGED
@@ -67,6 +67,7 @@ declare class SecureMemory {
67
67
 
68
68
  /**
69
69
  * HD Wallet helpers for Thru (BIP44 coin type 9999).
70
+ * Uses SLIP-0010 for Ed25519 key derivation via micro-key-producer.
70
71
  * Returns raw key material along with encoded addresses.
71
72
  */
72
73
  declare class ThruHDWallet {
package/dist/index.js CHANGED
@@ -1,8 +1,7 @@
1
1
  import { getWebCrypto, encodeAddress } from '@thru/helpers';
2
2
  export { getWebCrypto } from '@thru/helpers';
3
3
  import scrypt from 'scrypt-js';
4
- import { getPublicKeyAsync } from '@noble/ed25519';
5
- import { derivePath } from 'ed25519-hd-key';
4
+ import HDKey from 'micro-key-producer/slip10.js';
6
5
  import * as bip39 from 'bip39';
7
6
 
8
7
  // src/index.ts
@@ -159,11 +158,15 @@ var _ThruHDWallet = class _ThruHDWallet {
159
158
  throw new Error("Seed must be 64 bytes");
160
159
  }
161
160
  }
162
- static async deriveKeyPair(seed, path) {
161
+ static deriveKeyPair(seed, path) {
163
162
  _ThruHDWallet.ensureSeed(seed);
164
- const derived = derivePath(path, Buffer.from(seed).toString("hex"));
165
- const privateKey = new Uint8Array(derived.key);
166
- const publicKey = new Uint8Array(await getPublicKeyAsync(privateKey));
163
+ const hdkey = HDKey.fromMasterSeed(seed);
164
+ const derived = hdkey.derive(path);
165
+ if (!derived.privateKey || !derived.publicKey) {
166
+ throw new Error("Failed to derive key pair");
167
+ }
168
+ const privateKey = derived.privateKey;
169
+ const publicKey = derived.publicKeyRaw;
167
170
  const secretKey = new Uint8Array(privateKey.length + publicKey.length);
168
171
  secretKey.set(privateKey, 0);
169
172
  secretKey.set(publicKey, privateKey.length);
@@ -178,7 +181,7 @@ var _ThruHDWallet = class _ThruHDWallet {
178
181
  throw new Error("Account index must be non-negative");
179
182
  }
180
183
  const path = `${_ThruHDWallet.THRU_DERIVATION_PATH}/${accountIndex}'/${change}'`;
181
- const { publicKey, privateKey, secretKey } = await _ThruHDWallet.deriveKeyPair(seed, path);
184
+ const { publicKey, privateKey, secretKey } = _ThruHDWallet.deriveKeyPair(seed, path);
182
185
  return {
183
186
  address: encodeAddress(publicKey),
184
187
  publicKey,
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/encryption.ts","../src/hdwallet.ts","../src/mnemonic.ts"],"names":[],"mappings":";;;;;;;;AAiBO,IAAM,oBAAN,MAAwB;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAe7B,aAAa,OAAA,CAAQ,IAAA,EAAkB,QAAA,EAA0C;AAE/E,IAAA,MAAM,SAAS,YAAA,EAAa;AAC5B,IAAA,MAAM,OAAO,MAAA,CAAO,eAAA,CAAgB,IAAI,UAAA,CAAW,IAAA,CAAK,WAAW,CAAC,CAAA;AACpE,IAAA,MAAM,KAAK,MAAA,CAAO,eAAA,CAAgB,IAAI,UAAA,CAAW,IAAA,CAAK,SAAS,CAAC,CAAA;AAGhE,IAAA,MAAM,aAAA,GAAgB,IAAI,WAAA,EAAY,CAAE,OAAO,QAAQ,CAAA;AACvD,IAAA,MAAM,UAAA,GAAa,MAAM,MAAA,CAAO,MAAA;AAAA,MAC9B,aAAA;AAAA,MACA,IAAA;AAAA,MACA,IAAA,CAAK,SAAA;AAAA,MACL,IAAA,CAAK,SAAA;AAAA,MACL,IAAA,CAAK,SAAA;AAAA,MACL,IAAA,CAAK;AAAA,KACP;AAGA,IAAA,MAAM,SAAA,GAAY,MAAM,MAAA,CAAO,MAAA,CAAO,SAAA;AAAA,MACpC,KAAA;AAAA,MACA,IAAI,WAAW,UAAU,CAAA;AAAA,MACzB,EAAE,MAAM,SAAA,EAAU;AAAA,MAClB,KAAA;AAAA,MACA,CAAC,SAAS;AAAA,KACZ;AAGA,IAAA,MAAM,UAAA,GAAa,MAAM,MAAA,C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mB,MAAA,EAAQ,UAAU,CAAA;AACxD,IAAA,OAAO,IAAI,WAAW,IAAI,CAAA;AAAA,EAC5B;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAOA,OAAO,aAAa,MAAA,EAAwB;AAC1C,IAAA,OAAO,MAAA,CAAO,IAAA,EAAK,CAAE,KAAA,CAAM,KAAK,CAAA,CAAE,MAAA;AAAA,EACpC;AACF","file":"index.js","sourcesContent":["import scrypt from 'scrypt-js';\nimport { getWebCrypto } from '@thru/helpers';\n\nexport interface EncryptedData {\n ciphertext: Uint8Array;\n salt: Uint8Array;\n iv: Uint8Array;\n kdfParams: {\n N: number; // CPU/memory cost parameter\n r: number; // Block size\n p: number; // Parallelization parameter\n };\n}\n\n/**\n * Encryption service using scrypt KDF and AES-GCM\n */\nexport class EncryptionService {\n // Default scrypt parameters (can be adjusted for performance/security trade-off)\n private static readonly DEFAULT_N = 8192; // 2^15\n private static readonly DEFAULT_R = 8;\n private static readonly DEFAULT_P = 1;\n private static readonly KEY_LENGTH = 32; // 256 bits for AES-256\n private static readonly SALT_LENGTH = 32;\n private static readonly IV_LENGTH = 12; // Recommended for AES-GCM\n\n /**\n * Encrypt data using password-based encryption\n * @param data - Data to encrypt\n * @param password - User password\n * @returns Encrypted data with parameters\n */\n static async encrypt(data: Uint8Array, password: string): Promise<EncryptedData> {\n // Generate random salt and IV\n const crypto = getWebCrypto();\n const salt = crypto.getRandomValues(new Uint8Array(this.SALT_LENGTH));\n const iv = crypto.getRandomValues(new Uint8Array(this.IV_LENGTH));\n\n // Derive key from password using scrypt\n const passwordBytes = new TextEncoder().encode(password);\n const derivedKey = await scrypt.scrypt(\n passwordBytes,\n salt,\n this.DEFAULT_N,\n this.DEFAULT_R,\n this.DEFAULT_P,\n this.KEY_LENGTH\n );\n\n // Import key for WebCrypto API\n const cryptoKey = await crypto.subtle.importKey(\n 'raw',\n new Uint8Array(derivedKey),\n { name: 'AES-GCM' },\n false,\n ['encrypt']\n );\n\n // Encrypt data using AES-GCM\n const ciphertext = await crypto.subtle.encrypt(\n { name: 'AES-GCM', iv },\n cryptoKey,\n data as unknown as ArrayBuffer\n );\n\n // Zero out sensitive data\n derivedKey.fill(0);\n passwordBytes.fill(0);\n\n return {\n ciphertext: new Uint8Array(ciphertext),\n salt,\n iv,\n kdfParams: {\n N: this.DEFAULT_N,\n r: this.DEFAULT_R,\n p: this.DEFAULT_P,\n },\n };\n }\n\n /**\n * Decrypt encrypted data using password\n * @param encrypted - Encrypted data with parameters\n * @param password - User password\n * @returns Decrypted data\n */\n static async decrypt(encrypted: EncryptedData, password: string): Promise<Uint8Array> {\n // Derive key from password using stored parameters\n const passwordBytes = new TextEncoder().encode(password);\n const derivedKey = await scrypt.scrypt(\n passwordBytes,\n encrypted.salt,\n encrypted.kdfParams.N,\n encrypted.kdfParams.r,\n encrypted.kdfParams.p,\n this.KEY_LENGTH\n );\n\n // Import key for WebCrypto API\n const crypto = getWebCrypto();\n const cryptoKey = await crypto.subtle.importKey(\n 'raw',\n new Uint8Array(derivedKey),\n { name: 'AES-GCM' },\n false,\n ['decrypt']\n );\n\n try {\n // Decrypt data using AES-GCM\n const decrypted = await crypto.subtle.decrypt(\n { name: 'AES-GCM', iv: encrypted.iv as unknown as ArrayBuffer },\n cryptoKey,\n encrypted.ciphertext as unknown as ArrayBuffer\n );\n\n // Zero out sensitive data\n derivedKey.fill(0);\n passwordBytes.fill(0);\n\n return new Uint8Array(decrypted);\n } catch (error) {\n // Zero out sensitive data even on error\n derivedKey.fill(0);\n passwordBytes.fill(0);\n throw new Error('Decryption failed - incorrect password or corrupted data');\n }\n }\n\n /**\n * Serialize encrypted data to a storable format\n * @param encrypted - Encrypted data\n * @returns Base64-encoded JSON string\n */\n static serialize(encrypted: EncryptedData): string {\n return JSON.stringify({\n ciphertext: Buffer.from(encrypted.ciphertext).toString('base64'),\n salt: Buffer.from(encrypted.salt).toString('base64'),\n iv: Buffer.from(encrypted.iv).toString('base64'),\n kdfParams: encrypted.kdfParams,\n });\n }\n\n /**\n * Deserialize encrypted data from storage\n * @param serialized - Serialized encrypted data\n * @returns Encrypted data object\n */\n static deserialize(serialized: string): EncryptedData {\n const parsed = JSON.parse(serialized);\n return {\n ciphertext: new Uint8Array(Buffer.from(parsed.ciphertext, 'base64')),\n salt: new Uint8Array(Buffer.from(parsed.salt, 'base64')),\n iv: new Uint8Array(Buffer.from(parsed.iv, 'base64')),\n kdfParams: parsed.kdfParams,\n };\n }\n}\n\n/**\n * Utility functions for secure memory management\n */\nexport class SecureMemory {\n /**\n * Zero out a Uint8Array to remove sensitive data from memory\n * @param array - Array to zero out\n */\n static zeroize(array: Uint8Array): void {\n array.fill(0);\n }\n\n /**\n * Compare two Uint8Arrays in constant time to prevent timing attacks\n * @param a - First array\n * @param b - Second array\n * @returns true if arrays are equal\n */\n static constantTimeEqual(a: Uint8Array, b: Uint8Array): boolean {\n if (a.length !== b.length) {\n return false;\n }\n\n let result = 0;\n for (let i = 0; i < a.length; i++) {\n result |= a[i] ^ b[i];\n }\n\n return result === 0;\n }\n}\n","import { getPublicKeyAsync } from '@noble/ed25519';\nimport { derivePath } from 'ed25519-hd-key';\nimport { encodeAddress } from '@thru/helpers';\n\n/**\n * HD Wallet helpers for Thru (BIP44 coin type 9999).\n * Returns raw key material along with encoded addresses.\n */\nexport class ThruHDWallet {\n static readonly THRU_COIN_TYPE = 9999;\n static readonly THRU_DERIVATION_PATH = `m/44'/${ThruHDWallet.THRU_COIN_TYPE}'`;\n\n private static ensureSeed(seed: Uint8Array): void {\n if (seed.length !== 64) {\n throw new Error('Seed must be 64 bytes');\n }\n }\n\n private static async deriveKeyPair(seed: Uint8Array, path: string) {\n ThruHDWallet.ensureSeed(seed);\n const derived = derivePath(path, Buffer.from(seed).toString('hex'));\n const privateKey = new Uint8Array(derived.key);\n const publicKey = new Uint8Array(await getPublicKeyAsync(privateKey));\n const secretKey = new Uint8Array(privateKey.length + publicKey.length);\n secretKey.set(privateKey, 0);\n secretKey.set(publicKey, privateKey.length);\n\n return {\n publicKey,\n privateKey,\n secretKey,\n };\n }\n\n static async getAccount(\n seed: Uint8Array,\n accountIndex: number = 0,\n change: number = 0\n ): Promise<{\n address: string;\n publicKey: Uint8Array;\n privateKey: Uint8Array;\n secretKey: Uint8Array;\n path: string;\n }> {\n if (accountIndex < 0) {\n throw new Error('Account index must be non-negative');\n }\n\n const path = `${ThruHDWallet.THRU_DERIVATION_PATH}/${accountIndex}'/${change}'`;\n const { publicKey, privateKey, secretKey } = await ThruHDWallet.deriveKeyPair(seed, path);\n\n return {\n address: encodeAddress(publicKey),\n publicKey,\n privateKey,\n secretKey,\n path,\n };\n }\n\n static async deriveAccounts(\n seed: Uint8Array,\n count: number\n ): Promise<Array<{\n index: number;\n address: string;\n path: string;\n publicKey: Uint8Array;\n }>> {\n const accounts = [];\n for (let i = 0; i < count; i++) {\n const account = await ThruHDWallet.getAccount(seed, i);\n accounts.push({\n index: i,\n address: account.address,\n path: account.path,\n publicKey: account.publicKey,\n });\n }\n return accounts;\n }\n\n static isValidPath(path: string): boolean {\n const pathRegex = /^m(\\/\\d+')+$/;\n return pathRegex.test(path);\n }\n}\n","import * as bip39 from 'bip39';\n\n/**\n * Handles BIP39 mnemonic phrase generation and validation\n */\nexport class MnemonicGenerator {\n /**\n * Generate a new 12-word mnemonic phrase\n * @returns 12-word mnemonic string\n */\n static generate(): string {\n // 128 bits of entropy = 12 words\n return bip39.generateMnemonic(128);\n }\n\n /**\n * Validate a mnemonic phrase\n * @param phrase - Mnemonic phrase to validate\n * @returns true if valid, false otherwise\n */\n static validate(phrase: string): boolean {\n return bip39.validateMnemonic(phrase);\n }\n\n /**\n * Convert mnemonic phrase to seed bytes\n * @param phrase - Valid mnemonic phrase\n * @param passphrase - Optional passphrase for additional security\n * @returns Seed as Uint8Array (64 bytes)\n */\n static toSeed(phrase: string, passphrase: string = ''): Uint8Array {\n if (!this.validate(phrase)) {\n throw new Error('Invalid mnemonic phrase');\n }\n const seed = bip39.mnemonicToSeedSync(phrase, passphrase);\n return new Uint8Array(seed);\n }\n\n /**\n * Get the number of words in a mnemonic\n * @param phrase - Mnemonic phrase\n * @returns Number of words\n */\n static getWordCount(phrase: string): number {\n return phrase.trim().split(/\\s+/).length;\n }\n}\n"]}
1
+ {"version":3,"sources":["../src/encryption.ts","../src/hdwallet.ts","../src/mnemonic.ts"],"names":[],"mappings":";;;;;;;AAiBO,IAAM,oBAAN,MAAwB;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAe7B,aAAa,OAAA,CAAQ,IAAA,EAAkB,QAAA,EAA0C;AAE/E,IAAA,MAAM,SAAS,YAAA,EAAa;AAC5B,IAAA,MAAM,OAAO,MAAA,CAAO,eAAA,CAAgB,IAAI,UAAA,CAAW,IAAA,CAAK,WAAW,CAAC,CAAA;AACpE,IAAA,MAAM,KAAK,MAAA,CAAO,eAAA,CAAgB,IAAI,UAAA,CAAW,IAAA,CAAK,SAAS,CAAC,CAAA;AAGhE,IAAA,MAAM,aAAA,GAAgB,IAAI,WAAA,EAAY,CAAE,OAAO,QAAQ,CAAA;AACvD,IAAA,MAAM,UAAA,GAAa,MAAM,MAAA,CAAO,MAAA;AAAA,MAC9B,aAAA;AAAA,MACA,IAAA;AAAA,MACA,IAAA,CAAK,SAAA;AAAA,MACL,IAAA,CAAK,SAAA;AAAA,MACL,IAAA,CAAK,SAAA;AAAA,MACL,IAAA,CAAK;AAAA,KACP;AAGA,IAAA,MAAM,SAAA,GAAY,MAAM,MAAA,CAAO,MAAA,CAAO,SAAA;AAAA,MACpC,KAAA;AAAA,MACA,IAAI,WAAW,UAAU,CAAA;AAAA,MACzB,EAAE,MAAM,SAAA,EAAU;AAAA,MAClB,KAAA;AAAA,MACA,CAAC,SAAS;AAAA,KACZ;AAGA,IAAA,MAAM,UAAA,GAAa,MAAM,MAAA,CAAO,MAAA,CAAO,OAAA;AAAA,MACrC,EAAE,IAAA,EAAM,SAAA,EAAW,EAAA,EAAG;AAAA,MACtB,SAAA;A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CAAA;AACxD,IAAA,OAAO,IAAI,WAAW,IAAI,CAAA;AAAA,EAC5B;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAOA,OAAO,aAAa,MAAA,EAAwB;AAC1C,IAAA,OAAO,MAAA,CAAO,IAAA,EAAK,CAAE,KAAA,CAAM,KAAK,CAAA,CAAE,MAAA;AAAA,EACpC;AACF","file":"index.js","sourcesContent":["import scrypt from 'scrypt-js';\nimport { getWebCrypto } from '@thru/helpers';\n\nexport interface EncryptedData {\n ciphertext: Uint8Array;\n salt: Uint8Array;\n iv: Uint8Array;\n kdfParams: {\n N: number; // CPU/memory cost parameter\n r: number; // Block size\n p: number; // Parallelization parameter\n };\n}\n\n/**\n * Encryption service using scrypt KDF and AES-GCM\n */\nexport class EncryptionService {\n // Default scrypt parameters (can be adjusted for performance/security trade-off)\n private static readonly DEFAULT_N = 8192; // 2^15\n private static readonly DEFAULT_R = 8;\n private static readonly DEFAULT_P = 1;\n private static readonly KEY_LENGTH = 32; // 256 bits for AES-256\n private static readonly SALT_LENGTH = 32;\n private static readonly IV_LENGTH = 12; // Recommended for AES-GCM\n\n /**\n * Encrypt data using password-based encryption\n * @param data - Data to encrypt\n * @param password - User password\n * @returns Encrypted data with parameters\n */\n static async encrypt(data: Uint8Array, password: string): Promise<EncryptedData> {\n // Generate random salt and IV\n const crypto = getWebCrypto();\n const salt = crypto.getRandomValues(new Uint8Array(this.SALT_LENGTH));\n const iv = crypto.getRandomValues(new Uint8Array(this.IV_LENGTH));\n\n // Derive key from password using scrypt\n const passwordBytes = new TextEncoder().encode(password);\n const derivedKey = await scrypt.scrypt(\n passwordBytes,\n salt,\n this.DEFAULT_N,\n this.DEFAULT_R,\n this.DEFAULT_P,\n this.KEY_LENGTH\n );\n\n // Import key for WebCrypto API\n const cryptoKey = await crypto.subtle.importKey(\n 'raw',\n new Uint8Array(derivedKey),\n { name: 'AES-GCM' },\n false,\n ['encrypt']\n );\n\n // Encrypt data using AES-GCM\n const ciphertext = await crypto.subtle.encrypt(\n { name: 'AES-GCM', iv },\n cryptoKey,\n data as unknown as ArrayBuffer\n );\n\n // Zero out sensitive data\n derivedKey.fill(0);\n passwordBytes.fill(0);\n\n return {\n ciphertext: new Uint8Array(ciphertext),\n salt,\n iv,\n kdfParams: {\n N: this.DEFAULT_N,\n r: this.DEFAULT_R,\n p: this.DEFAULT_P,\n },\n };\n }\n\n /**\n * Decrypt encrypted data using password\n * @param encrypted - Encrypted data with parameters\n * @param password - User password\n * @returns Decrypted data\n */\n static async decrypt(encrypted: EncryptedData, password: string): Promise<Uint8Array> {\n // Derive key from password using stored parameters\n const passwordBytes = new TextEncoder().encode(password);\n const derivedKey = await scrypt.scrypt(\n passwordBytes,\n encrypted.salt,\n encrypted.kdfParams.N,\n encrypted.kdfParams.r,\n encrypted.kdfParams.p,\n this.KEY_LENGTH\n );\n\n // Import key for WebCrypto API\n const crypto = getWebCrypto();\n const cryptoKey = await crypto.subtle.importKey(\n 'raw',\n new Uint8Array(derivedKey),\n { name: 'AES-GCM' },\n false,\n ['decrypt']\n );\n\n try {\n // Decrypt data using AES-GCM\n const decrypted = await crypto.subtle.decrypt(\n { name: 'AES-GCM', iv: encrypted.iv as unknown as ArrayBuffer },\n cryptoKey,\n encrypted.ciphertext as unknown as ArrayBuffer\n );\n\n // Zero out sensitive data\n derivedKey.fill(0);\n passwordBytes.fill(0);\n\n return new Uint8Array(decrypted);\n } catch (error) {\n // Zero out sensitive data even on error\n derivedKey.fill(0);\n passwordBytes.fill(0);\n throw new Error('Decryption failed - incorrect password or corrupted data');\n }\n }\n\n /**\n * Serialize encrypted data to a storable format\n * @param encrypted - Encrypted data\n * @returns Base64-encoded JSON string\n */\n static serialize(encrypted: EncryptedData): string {\n return JSON.stringify({\n ciphertext: Buffer.from(encrypted.ciphertext).toString('base64'),\n salt: Buffer.from(encrypted.salt).toString('base64'),\n iv: Buffer.from(encrypted.iv).toString('base64'),\n kdfParams: encrypted.kdfParams,\n });\n }\n\n /**\n * Deserialize encrypted data from storage\n * @param serialized - Serialized encrypted data\n * @returns Encrypted data object\n */\n static deserialize(serialized: string): EncryptedData {\n const parsed = JSON.parse(serialized);\n return {\n ciphertext: new Uint8Array(Buffer.from(parsed.ciphertext, 'base64')),\n salt: new Uint8Array(Buffer.from(parsed.salt, 'base64')),\n iv: new Uint8Array(Buffer.from(parsed.iv, 'base64')),\n kdfParams: parsed.kdfParams,\n };\n }\n}\n\n/**\n * Utility functions for secure memory management\n */\nexport class SecureMemory {\n /**\n * Zero out a Uint8Array to remove sensitive data from memory\n * @param array - Array to zero out\n */\n static zeroize(array: Uint8Array): void {\n array.fill(0);\n }\n\n /**\n * Compare two Uint8Arrays in constant time to prevent timing attacks\n * @param a - First array\n * @param b - Second array\n * @returns true if arrays are equal\n */\n static constantTimeEqual(a: Uint8Array, b: Uint8Array): boolean {\n if (a.length !== b.length) {\n return false;\n }\n\n let result = 0;\n for (let i = 0; i < a.length; i++) {\n result |= a[i] ^ b[i];\n }\n\n return result === 0;\n }\n}\n","import HDKey from 'micro-key-producer/slip10.js';\nimport { encodeAddress } from '@thru/helpers';\n\n/**\n * HD Wallet helpers for Thru (BIP44 coin type 9999).\n * Uses SLIP-0010 for Ed25519 key derivation via micro-key-producer.\n * Returns raw key material along with encoded addresses.\n */\nexport class ThruHDWallet {\n static readonly THRU_COIN_TYPE = 9999;\n static readonly THRU_DERIVATION_PATH = `m/44'/${ThruHDWallet.THRU_COIN_TYPE}'`;\n\n private static ensureSeed(seed: Uint8Array): void {\n if (seed.length !== 64) {\n throw new Error('Seed must be 64 bytes');\n }\n }\n\n private static deriveKeyPair(seed: Uint8Array, path: string) {\n ThruHDWallet.ensureSeed(seed);\n const hdkey = HDKey.fromMasterSeed(seed);\n const derived = hdkey.derive(path);\n\n if (!derived.privateKey || !derived.publicKey) {\n throw new Error('Failed to derive key pair');\n }\n\n const privateKey = derived.privateKey;\n const publicKey = derived.publicKeyRaw;\n const secretKey = new Uint8Array(privateKey.length + publicKey.length);\n secretKey.set(privateKey, 0);\n secretKey.set(publicKey, privateKey.length);\n\n return {\n publicKey,\n privateKey,\n secretKey,\n };\n }\n\n static async getAccount(\n seed: Uint8Array,\n accountIndex: number = 0,\n change: number = 0\n ): Promise<{\n address: string;\n publicKey: Uint8Array;\n privateKey: Uint8Array;\n secretKey: Uint8Array;\n path: string;\n }> {\n if (accountIndex < 0) {\n throw new Error('Account index must be non-negative');\n }\n\n const path = `${ThruHDWallet.THRU_DERIVATION_PATH}/${accountIndex}'/${change}'`;\n const { publicKey, privateKey, secretKey } = ThruHDWallet.deriveKeyPair(seed, path);\n\n return {\n address: encodeAddress(publicKey),\n publicKey,\n privateKey,\n secretKey,\n path,\n };\n }\n\n static async deriveAccounts(\n seed: Uint8Array,\n count: number\n ): Promise<Array<{\n index: number;\n address: string;\n path: string;\n publicKey: Uint8Array;\n }>> {\n const accounts = [];\n for (let i = 0; i < count; i++) {\n const account = await ThruHDWallet.getAccount(seed, i);\n accounts.push({\n index: i,\n address: account.address,\n path: account.path,\n publicKey: account.publicKey,\n });\n }\n return accounts;\n }\n\n static isValidPath(path: string): boolean {\n const pathRegex = /^m(\\/\\d+')+$/;\n return pathRegex.test(path);\n }\n}\n","import * as bip39 from 'bip39';\n\n/**\n * Handles BIP39 mnemonic phrase generation and validation\n */\nexport class MnemonicGenerator {\n /**\n * Generate a new 12-word mnemonic phrase\n * @returns 12-word mnemonic string\n */\n static generate(): string {\n // 128 bits of entropy = 12 words\n return bip39.generateMnemonic(128);\n }\n\n /**\n * Validate a mnemonic phrase\n * @param phrase - Mnemonic phrase to validate\n * @returns true if valid, false otherwise\n */\n static validate(phrase: string): boolean {\n return bip39.validateMnemonic(phrase);\n }\n\n /**\n * Convert mnemonic phrase to seed bytes\n * @param phrase - Valid mnemonic phrase\n * @param passphrase - Optional passphrase for additional security\n * @returns Seed as Uint8Array (64 bytes)\n */\n static toSeed(phrase: string, passphrase: string = ''): Uint8Array {\n if (!this.validate(phrase)) {\n throw new Error('Invalid mnemonic phrase');\n }\n const seed = bip39.mnemonicToSeedSync(phrase, passphrase);\n return new Uint8Array(seed);\n }\n\n /**\n * Get the number of words in a mnemonic\n * @param phrase - Mnemonic phrase\n * @returns Number of words\n */\n static getWordCount(phrase: string): number {\n return phrase.trim().split(/\\s+/).length;\n }\n}\n"]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@thru/crypto",
3
- "version": "0.1.29",
3
+ "version": "0.1.33",
4
4
  "type": "module",
5
5
  "main": "./dist/index.js",
6
6
  "types": "./dist/index.d.ts",
@@ -11,16 +11,22 @@
11
11
  }
12
12
  },
13
13
  "dependencies": {
14
- "@noble/ed25519": "^2.3.0",
15
14
  "bip39": "^3.1.0",
16
- "ed25519-hd-key": "^1.3.0",
15
+ "micro-key-producer": "^0.8.2",
17
16
  "scrypt-js": "^3.0.1",
18
- "@thru/helpers": "0.1.29"
17
+ "@thru/helpers": "0.1.33"
18
+ },
19
+ "devDependencies": {
20
+ "@types/node": "^24.10.1",
21
+ "vitest": "^3.2.4"
19
22
  },
20
23
  "scripts": {
21
24
  "build": "tsup",
22
25
  "dev": "tsup --watch",
23
26
  "lint": "eslint src/",
27
+ "test": "vitest",
28
+ "test:run": "vitest run",
29
+ "test:watch": "vitest --watch",
24
30
  "clean": "rm -rf dist"
25
31
  }
26
32
  }
@@ -0,0 +1,156 @@
1
+ import { describe, it, expect } from 'vitest';
2
+ import { mnemonicToSeed } from 'bip39';
3
+ import { ThruHDWallet } from './hdwallet';
4
+
5
+ /**
6
+ * Test vectors for ThruHDWallet using SLIP-0010 Ed25519 derivation.
7
+ *
8
+ * These vectors use the standard BIP39 test mnemonic and ensure that
9
+ * key derivation remains consistent across library changes.
10
+ *
11
+ * IMPORTANT: If these tests fail after a library update, existing wallets
12
+ * will derive different addresses and users will lose access to funds.
13
+ */
14
+
15
+ // Standard BIP39 test mnemonic (12 words)
16
+ const TEST_MNEMONIC =
17
+ 'abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon about';
18
+
19
+ // Pre-computed test vectors using micro-key-producer v0.8.2
20
+ // Path format: m/44'/9999'/accountIndex'/0'
21
+ const TEST_VECTORS = [
22
+ {
23
+ accountIndex: 0,
24
+ path: "m/44'/9999'/0'/0'",
25
+ privateKey: 'f320787900b2be9214778b4219212b681d2d77f45e7045575680be4a6fe076e6',
26
+ publicKey: 'a20a6cddb7cf52fbe4403024e1ce8463ac0d70870bac6f492f5d16ba405fde12',
27
+ },
28
+ {
29
+ accountIndex: 1,
30
+ path: "m/44'/9999'/1'/0'",
31
+ privateKey: '092413cbbdffd063d2f5eca4d8aa881b6d25a7703af1d47dd14be87180675db1',
32
+ publicKey: '8b8f363e252268cbd1255f10c02a39c62594849f19fe72ed52e966ee0f3fdf5c',
33
+ },
34
+ {
35
+ accountIndex: 2,
36
+ path: "m/44'/9999'/2'/0'",
37
+ privateKey: 'dfffc0d7734b32429b30530972410a743f636ca6831b02d25ef794c3ee0eb09e',
38
+ publicKey: '7cc170536e078b25dd4d621d2cd347c9cdd225edfaf2eab51fe3883b843db144',
39
+ },
40
+ ];
41
+
42
+ function bytesToHex(bytes: Uint8Array): string {
43
+ return Array.from(bytes)
44
+ .map((b) => b.toString(16).padStart(2, '0'))
45
+ .join('');
46
+ }
47
+
48
+ describe('ThruHDWallet', () => {
49
+ describe('SLIP-0010 Ed25519 derivation', () => {
50
+ let seed: Uint8Array;
51
+
52
+ beforeAll(async () => {
53
+ seed = await mnemonicToSeed(TEST_MNEMONIC);
54
+ });
55
+
56
+ it('should use correct coin type for Thru', () => {
57
+ expect(ThruHDWallet.THRU_COIN_TYPE).toBe(9999);
58
+ });
59
+
60
+ it('should use correct derivation path prefix', () => {
61
+ expect(ThruHDWallet.THRU_DERIVATION_PATH).toBe("m/44'/9999'");
62
+ });
63
+
64
+ it('should reject seeds that are not 64 bytes', async () => {
65
+ const shortSeed = new Uint8Array(32);
66
+ await expect(ThruHDWallet.getAccount(shortSeed, 0)).rejects.toThrow(
67
+ 'Seed must be 64 bytes'
68
+ );
69
+ });
70
+
71
+ it('should reject negative account indices', async () => {
72
+ await expect(ThruHDWallet.getAccount(seed, -1)).rejects.toThrow(
73
+ 'Account index must be non-negative'
74
+ );
75
+ });
76
+
77
+ TEST_VECTORS.forEach(({ accountIndex, path, privateKey, publicKey }) => {
78
+ describe(`Account ${accountIndex}`, () => {
79
+ it(`should derive correct path: ${path}`, async () => {
80
+ const account = await ThruHDWallet.getAccount(seed, accountIndex);
81
+ expect(account.path).toBe(path);
82
+ });
83
+
84
+ it('should derive correct private key (32 bytes)', async () => {
85
+ const account = await ThruHDWallet.getAccount(seed, accountIndex);
86
+ expect(account.privateKey.length).toBe(32);
87
+ expect(bytesToHex(account.privateKey)).toBe(privateKey);
88
+ });
89
+
90
+ it('should derive correct public key (32 bytes)', async () => {
91
+ const account = await ThruHDWallet.getAccount(seed, accountIndex);
92
+ expect(account.publicKey.length).toBe(32);
93
+ expect(bytesToHex(account.publicKey)).toBe(publicKey);
94
+ });
95
+
96
+ it('should produce 64-byte secret key (private + public)', async () => {
97
+ const account = await ThruHDWallet.getAccount(seed, accountIndex);
98
+ expect(account.secretKey.length).toBe(64);
99
+ // First 32 bytes should be private key
100
+ expect(bytesToHex(account.secretKey.slice(0, 32))).toBe(privateKey);
101
+ // Last 32 bytes should be public key
102
+ expect(bytesToHex(account.secretKey.slice(32))).toBe(publicKey);
103
+ });
104
+
105
+ it('should return a valid Thru address', async () => {
106
+ const account = await ThruHDWallet.getAccount(seed, accountIndex);
107
+ expect(account.address).toBeTruthy();
108
+ expect(typeof account.address).toBe('string');
109
+ });
110
+ });
111
+ });
112
+ });
113
+
114
+ describe('deriveAccounts', () => {
115
+ let seed: Uint8Array;
116
+
117
+ beforeAll(async () => {
118
+ seed = await mnemonicToSeed(TEST_MNEMONIC);
119
+ });
120
+
121
+ it('should derive multiple accounts', async () => {
122
+ const accounts = await ThruHDWallet.deriveAccounts(seed, 3);
123
+ expect(accounts.length).toBe(3);
124
+
125
+ accounts.forEach((account, i) => {
126
+ expect(account.index).toBe(i);
127
+ expect(account.path).toBe(`m/44'/9999'/${i}'/0'`);
128
+ expect(account.publicKey.length).toBe(32);
129
+ expect(account.address).toBeTruthy();
130
+ });
131
+ });
132
+
133
+ it('should derive accounts with matching test vectors', async () => {
134
+ const accounts = await ThruHDWallet.deriveAccounts(seed, 3);
135
+
136
+ TEST_VECTORS.forEach(({ accountIndex, publicKey }) => {
137
+ expect(bytesToHex(accounts[accountIndex].publicKey)).toBe(publicKey);
138
+ });
139
+ });
140
+ });
141
+
142
+ describe('isValidPath', () => {
143
+ it('should validate correct paths', () => {
144
+ expect(ThruHDWallet.isValidPath("m/44'/9999'/0'/0'")).toBe(true);
145
+ expect(ThruHDWallet.isValidPath("m/44'/0'")).toBe(true);
146
+ expect(ThruHDWallet.isValidPath("m/0'")).toBe(true);
147
+ });
148
+
149
+ it('should reject invalid paths', () => {
150
+ expect(ThruHDWallet.isValidPath('m/44/9999/0/0')).toBe(false); // non-hardened
151
+ expect(ThruHDWallet.isValidPath('44/9999/0/0')).toBe(false); // no m prefix
152
+ expect(ThruHDWallet.isValidPath('')).toBe(false);
153
+ expect(ThruHDWallet.isValidPath('invalid')).toBe(false);
154
+ });
155
+ });
156
+ });
package/src/hdwallet.ts CHANGED
@@ -1,9 +1,9 @@
1
- import { getPublicKeyAsync } from '@noble/ed25519';
2
- import { derivePath } from 'ed25519-hd-key';
1
+ import HDKey from 'micro-key-producer/slip10.js';
3
2
  import { encodeAddress } from '@thru/helpers';
4
3
 
5
4
  /**
6
5
  * HD Wallet helpers for Thru (BIP44 coin type 9999).
6
+ * Uses SLIP-0010 for Ed25519 key derivation via micro-key-producer.
7
7
  * Returns raw key material along with encoded addresses.
8
8
  */
9
9
  export class ThruHDWallet {
@@ -16,11 +16,17 @@ export class ThruHDWallet {
16
16
  }
17
17
  }
18
18
 
19
- private static async deriveKeyPair(seed: Uint8Array, path: string) {
19
+ private static deriveKeyPair(seed: Uint8Array, path: string) {
20
20
  ThruHDWallet.ensureSeed(seed);
21
- const derived = derivePath(path, Buffer.from(seed).toString('hex'));
22
- const privateKey = new Uint8Array(derived.key);
23
- const publicKey = new Uint8Array(await getPublicKeyAsync(privateKey));
21
+ const hdkey = HDKey.fromMasterSeed(seed);
22
+ const derived = hdkey.derive(path);
23
+
24
+ if (!derived.privateKey || !derived.publicKey) {
25
+ throw new Error('Failed to derive key pair');
26
+ }
27
+
28
+ const privateKey = derived.privateKey;
29
+ const publicKey = derived.publicKeyRaw;
24
30
  const secretKey = new Uint8Array(privateKey.length + publicKey.length);
25
31
  secretKey.set(privateKey, 0);
26
32
  secretKey.set(publicKey, privateKey.length);
@@ -48,7 +54,7 @@ export class ThruHDWallet {
48
54
  }
49
55
 
50
56
  const path = `${ThruHDWallet.THRU_DERIVATION_PATH}/${accountIndex}'/${change}'`;
51
- const { publicKey, privateKey, secretKey } = await ThruHDWallet.deriveKeyPair(seed, path);
57
+ const { publicKey, privateKey, secretKey } = ThruHDWallet.deriveKeyPair(seed, path);
52
58
 
53
59
  return {
54
60
  address: encodeAddress(publicKey),
@@ -0,0 +1,9 @@
1
+ import { defineConfig } from 'vitest/config';
2
+
3
+ export default defineConfig({
4
+ test: {
5
+ globals: true,
6
+ include: ['src/**/*.{test,spec}.ts'],
7
+ exclude: ['**/node_modules/**', '**/dist/**'],
8
+ },
9
+ });