Skip to content

Commit

Permalink
replace noble with node:crypto
Browse files Browse the repository at this point in the history
  • Loading branch information
leordev committed Oct 11, 2023
1 parent c8a521b commit 9874883
Showing 1 changed file with 22 additions and 28 deletions.
50 changes: 22 additions & 28 deletions packages/agent/src/app-data-store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ export class AppDataVault implements AppDataStore {
if (crypto && typeof crypto.subtle === 'object' && crypto.subtle != null) {
return this.generateVaultUnlockKeyWithSubtleCrypto(options);
} else {
return this.generateVaultUnlockKeyWithNoble(options);
return this.generateVaultUnlockKeyWithNodeCrypto(options);
}
}

Expand Down Expand Up @@ -181,34 +181,28 @@ export class AppDataVault implements AppDataStore {
return new Uint8Array(vaultUnlockKey);
}

private async generateVaultUnlockKeyWithNoble(options: {
passphrase: string,
salt: Uint8Array
}): Promise<Uint8Array> {
private async generateVaultUnlockKeyWithNodeCrypto(
options: { passphrase: string; salt: Uint8Array }
): Promise<Uint8Array> {
const { passphrase, salt } = options;

const passwordBuffer = new TextEncoder().encode(passphrase);

const importedKey = await crypto.subtle.importKey(
'raw',
passwordBuffer,
'PBKDF2',
false,
['deriveBits']
);

const vaultUnlockKey = await crypto.subtle.deriveBits(
{
name : 'PBKDF2',
hash : 'SHA-512',
salt : salt,
iterations : this._keyDerivationWorkFactor,
},
importedKey,
32 * 8, // 32 bytes
);

return new Uint8Array(vaultUnlockKey);
const { pbkdf2 } = await import('node:crypto');

return new Promise((resolve, reject) => {
pbkdf2(
passphrase,
salt,
this._keyDerivationWorkFactor,
32,
'sha512',
(err, derivedKey) => {
if (err) {
reject(err);
} else {
resolve(new Uint8Array(derivedKey));
}
}
);
});
}

async getDid(): Promise<string> {
Expand Down

0 comments on commit 9874883

Please sign in to comment.