Skip to content

Commit b45de2b

Browse files
committed
feat: add v2 encrypt/decrypt support to WRW flows
Ticket: WCN-172
1 parent b2239f5 commit b45de2b

27 files changed

Lines changed: 61 additions & 61 deletions

File tree

modules/abstract-eth/src/abstractEthLikeNewCoins.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1478,7 +1478,7 @@ export abstract class AbstractEthLikeNewCoins extends AbstractEthLikeCoin {
14781478

14791479
if (!userKey.startsWith('xpub') && !userKey.startsWith('xprv')) {
14801480
try {
1481-
userKey = this.bitgo.decrypt({
1481+
userKey = await this.bitgo.decryptAsync({
14821482
input: userKey,
14831483
password: params.walletPassphrase,
14841484
});
@@ -1497,7 +1497,7 @@ export abstract class AbstractEthLikeNewCoins extends AbstractEthLikeCoin {
14971497
let backupPrv;
14981498

14991499
try {
1500-
backupPrv = this.bitgo.decrypt({
1500+
backupPrv = await this.bitgo.decryptAsync({
15011501
input: backupKey,
15021502
password: params.walletPassphrase,
15031503
});
@@ -1670,7 +1670,7 @@ export abstract class AbstractEthLikeNewCoins extends AbstractEthLikeCoin {
16701670

16711671
let userKeyPrv;
16721672
try {
1673-
userKeyPrv = this.bitgo.decrypt({
1673+
userKeyPrv = await this.bitgo.decryptAsync({
16741674
input: params.encryptedPrv,
16751675
password: params.walletPassphrase,
16761676
});
@@ -1749,7 +1749,7 @@ export abstract class AbstractEthLikeNewCoins extends AbstractEthLikeCoin {
17491749
if (params.walletPassphrase) {
17501750
if (!userKey.startsWith('xpub') && !userKey.startsWith('xprv')) {
17511751
try {
1752-
userKeyPrv = this.bitgo.decrypt({
1752+
userKeyPrv = await this.bitgo.decryptAsync({
17531753
input: userKey,
17541754
password: params.walletPassphrase,
17551755
});

modules/abstract-eth/src/ethLikeToken.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,7 @@ export class EthLikeToken extends AbstractEthLikeNewCoins {
219219
// Decrypt private keys from KeyCard values
220220
if (!userKey.startsWith('xpub') && !userKey.startsWith('xprv')) {
221221
try {
222-
userKey = this.bitgo.decrypt({
222+
userKey = await this.bitgo.decryptAsync({
223223
input: userKey,
224224
password: params.walletPassphrase,
225225
});
@@ -239,7 +239,7 @@ export class EthLikeToken extends AbstractEthLikeNewCoins {
239239
let backupPrv;
240240

241241
try {
242-
backupPrv = this.bitgo.decrypt({
242+
backupPrv = await this.bitgo.decryptAsync({
243243
input: backupKey,
244244
password: params.walletPassphrase,
245245
});

modules/abstract-substrate/src/abstractSubstrateCoin.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -300,7 +300,7 @@ export class SubstrateCoin extends BaseCoin {
300300
// Decrypt private keys from KeyCard values
301301
let userPrv;
302302
try {
303-
userPrv = this.bitgo.decrypt({
303+
userPrv = await this.bitgo.decryptAsync({
304304
input: userKey,
305305
password: params.walletPassphrase,
306306
});
@@ -311,7 +311,7 @@ export class SubstrateCoin extends BaseCoin {
311311

312312
let backupPrv;
313313
try {
314-
backupPrv = this.bitgo.decrypt({
314+
backupPrv = await this.bitgo.decryptAsync({
315315
input: backupKey,
316316
password: params.walletPassphrase,
317317
});

modules/sdk-api/src/encryptV2.ts

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
import { argon2id } from '@bitgo/argon2';
22
import { base64String, boundedInt, decodeWithCodec } from '@bitgo/sdk-core';
3-
import { randomBytes } from 'crypto';
3+
import { randomBytes, webcrypto } from 'crypto';
44
import * as t from 'io-ts';
55

6+
/** Web Crypto subtle — browser global in DOM; Node/Electron main must use `webcrypto`. */
7+
const subtle = globalThis.crypto?.subtle ?? webcrypto.subtle;
8+
69
/** Default Argon2id parameters per RFC 9106 second recommendation
710
* @see https://www.rfc-editor.org/rfc/rfc9106#section-4
811
*/
@@ -80,7 +83,7 @@ async function argon2ToAesKey(
8083
params: { memorySize: number; iterations: number; parallelism: number }
8184
): Promise<CryptoKey> {
8285
const keyBytes = await argon2Hash(password, salt, params);
83-
return crypto.subtle.importKey('raw', keyBytes, { name: 'AES-GCM' }, false, ['encrypt', 'decrypt']);
86+
return subtle.importKey('raw', keyBytes, { name: 'AES-GCM' }, false, ['encrypt', 'decrypt']);
8487
}
8588

8689
export async function argon2ToHkdfKey(
@@ -89,11 +92,11 @@ export async function argon2ToHkdfKey(
8992
params: { memorySize: number; iterations: number; parallelism: number }
9093
): Promise<CryptoKey> {
9194
const keyBytes = await argon2Hash(password, salt, params);
92-
return crypto.subtle.importKey('raw', keyBytes, 'HKDF', false, ['deriveKey']);
95+
return subtle.importKey('raw', keyBytes, 'HKDF', false, ['deriveKey']);
9396
}
9497

9598
export function hkdfDeriveAesKey(hkdfKey: CryptoKey, hkdfSalt: Uint8Array, usage: KeyUsage): Promise<CryptoKey> {
96-
return crypto.subtle.deriveKey(
99+
return subtle.deriveKey(
97100
{ name: 'HKDF', hash: 'SHA-256', salt: hkdfSalt, info: HKDF_INFO },
98101
hkdfKey,
99102
{ name: 'AES-GCM', length: 256 },
@@ -110,7 +113,7 @@ export async function aesGcmEncrypt(
110113
): Promise<Uint8Array> {
111114
const params: AesGcmParams = { name: 'AES-GCM', iv, tagLength: 128 };
112115
if (additionalData) params.additionalData = additionalData;
113-
const ct = await crypto.subtle.encrypt(params, key, new TextEncoder().encode(plaintext));
116+
const ct = await subtle.encrypt(params, key, new TextEncoder().encode(plaintext));
114117
return new Uint8Array(ct);
115118
}
116119

@@ -122,7 +125,7 @@ export async function aesGcmDecrypt(
122125
): Promise<string> {
123126
const params: AesGcmParams = { name: 'AES-GCM', iv, tagLength: 128 };
124127
if (additionalData) params.additionalData = additionalData;
125-
const plaintext = await crypto.subtle.decrypt(params, key, ct);
128+
const plaintext = await subtle.decrypt(params, key, ct);
126129
return new TextDecoder().decode(plaintext);
127130
}
128131

modules/sdk-coin-ada/src/ada.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -464,7 +464,7 @@ export class Ada extends BaseCoin {
464464
// Decrypt private keys from KeyCard values
465465
let userPrv;
466466
try {
467-
userPrv = this.bitgo.decrypt({
467+
userPrv = await this.bitgo.decryptAsync({
468468
input: userKey,
469469
password: params.walletPassphrase,
470470
});
@@ -476,7 +476,7 @@ export class Ada extends BaseCoin {
476476

477477
let backupPrv;
478478
try {
479-
backupPrv = this.bitgo.decrypt({
479+
backupPrv = await this.bitgo.decryptAsync({
480480
input: backupKey,
481481
password: params.walletPassphrase,
482482
});

modules/sdk-coin-algo/src/algo.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -883,8 +883,8 @@ export class Algo extends BaseCoin {
883883
throw new Error('bitgo public key from the keyCard is required for non-bitgo recovery');
884884
}
885885
try {
886-
userPrv = this.bitgo.decrypt({ input: params.userKey, password: params.walletPassphrase });
887-
backupPrv = this.bitgo.decrypt({ input: params.backupKey, password: params.walletPassphrase });
886+
userPrv = await this.bitgo.decryptAsync({ input: params.userKey, password: params.walletPassphrase });
887+
backupPrv = await this.bitgo.decryptAsync({ input: params.backupKey, password: params.walletPassphrase });
888888
const userKeyAddress = Utils.privateKeyToAlgoAddress(userPrv);
889889
const backupKeyAddress = Utils.privateKeyToAlgoAddress(backupPrv);
890890
txBuilder.numberOfRequiredSigners(2).setSigners([userKeyAddress, backupKeyAddress, params.bitgoKey]);

modules/sdk-coin-algo/test/unit/algo.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -919,7 +919,7 @@ describe('ALGO:', function () {
919919
},
920920
{
921921
message:
922-
"unable to decrypt userKey or backupKey with the walletPassphrase provided, got error: password error - ccm: tag doesn't match",
922+
'unable to decrypt userKey or backupKey with the walletPassphrase provided, got error: incorrect password',
923923
}
924924
);
925925
});

modules/sdk-coin-avaxc/src/avaxc.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -635,7 +635,7 @@ export class AvaxC extends AbstractEthLikeNewCoins {
635635
: new optionalDeps.ethUtil.BN(this.setGasPrice(params.gasPrice));
636636
if (!userKey.startsWith('xpub') && !userKey.startsWith('xprv')) {
637637
try {
638-
userKey = this.bitgo.decrypt({
638+
userKey = await this.bitgo.decryptAsync({
639639
input: userKey,
640640
password: params.walletPassphrase,
641641
});
@@ -654,7 +654,7 @@ export class AvaxC extends AbstractEthLikeNewCoins {
654654
let backupPrv;
655655

656656
try {
657-
backupPrv = this.bitgo.decrypt({
657+
backupPrv = await this.bitgo.decryptAsync({
658658
input: backupKey,
659659
password: params.walletPassphrase,
660660
});

modules/sdk-coin-dot/src/dot.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -410,7 +410,7 @@ export class Dot extends BaseCoin {
410410
// Decrypt private keys from KeyCard values
411411
let userPrv;
412412
try {
413-
userPrv = this.bitgo.decrypt({
413+
userPrv = await this.bitgo.decryptAsync({
414414
input: userKey,
415415
password: params.walletPassphrase,
416416
});
@@ -422,7 +422,7 @@ export class Dot extends BaseCoin {
422422

423423
let backupPrv;
424424
try {
425-
backupPrv = this.bitgo.decrypt({
425+
backupPrv = await this.bitgo.decryptAsync({
426426
input: backupKey,
427427
password: params.walletPassphrase,
428428
});

modules/sdk-coin-etc/src/etc.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ export class Etc extends AbstractEthLikeCoin {
8989

9090
if (!userKey.startsWith('xpub') && !userKey.startsWith('xprv')) {
9191
try {
92-
userKey = this.bitgo.decrypt({
92+
userKey = await this.bitgo.decryptAsync({
9393
input: userKey,
9494
password: params.walletPassphrase,
9595
});
@@ -108,7 +108,7 @@ export class Etc extends AbstractEthLikeCoin {
108108
let backupPrv;
109109

110110
try {
111-
backupPrv = this.bitgo.decrypt({
111+
backupPrv = await this.bitgo.decryptAsync({
112112
input: backupKey,
113113
password: params.walletPassphrase,
114114
});

0 commit comments

Comments
 (0)