Skip to content

Commit 1a885ab

Browse files
feat: rename audit function naming and signature
TICKET: WP-4242
1 parent e78a1d9 commit 1a885ab

File tree

43 files changed

+428
-373
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

43 files changed

+428
-373
lines changed

modules/abstract-cosmos/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
},
4040
"dependencies": {
4141
"@bitgo/sdk-core": "^35.0.0",
42+
"@bitgo/sdk-lib-mpc": "^10.3.0",
4243
"@bitgo/secp256k1": "^1.3.3",
4344
"@bitgo/statics": "^54.0.0",
4445
"@cosmjs/amino": "^0.29.5",

modules/abstract-cosmos/src/cosmosCoin.ts

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import {
22
AuditDecryptedKeyParams,
3-
AuditKeyResponse,
43
BaseCoin,
54
BaseTransaction,
65
BitGoBase,
@@ -670,21 +669,11 @@ export class CosmosCoin<CustomMessage = never> extends BaseCoin {
670669
}
671670

672671
/** @inheritDoc **/
673-
auditDecryptedKey({ multiSigType, publicKey, prv }: AuditDecryptedKeyParams): AuditKeyResponse {
672+
auditDecryptedKey({ multiSigType, publicKey, prv }: AuditDecryptedKeyParams) {
674673
if (multiSigType !== 'tss') {
675674
throw new Error('Unsupported multiSigType');
676675
} else {
677-
const result = auditEcdsaPrivateKey(prv as string, publicKey as string);
678-
if (result.isValid) {
679-
return { isValid: true };
680-
} else {
681-
if (!result.isCommonKeychainValid) {
682-
return { isValid: false, message: 'Invalid common keychain' };
683-
} else if (!result.isPrivateKeyValid) {
684-
return { isValid: false, message: 'Invalid private key' };
685-
}
686-
return { isValid: false };
687-
}
676+
auditEcdsaPrivateKey(prv as string, publicKey as string);
688677
}
689678
}
690679
}

modules/abstract-eth/src/abstractEthLikeCoin.ts

Lines changed: 5 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import { bip32 } from '@bitgo/secp256k1';
77
import { randomBytes } from 'crypto';
88
import {
99
AuditDecryptedKeyParams,
10-
AuditKeyResponse,
1110
BaseCoin,
1211
bitcoin,
1312
BitGoBase,
@@ -233,31 +232,20 @@ export abstract class AbstractEthLikeCoin extends BaseCoin {
233232
*/
234233
protected abstract getTransactionBuilder(common?: EthLikeCommon.default): TransactionBuilder;
235234

236-
auditDecryptedKey({ multiSigType, publicKey, prv }: AuditDecryptedKeyParams): AuditKeyResponse {
235+
/** @inheritDoc */
236+
auditDecryptedKey({ multiSigType, publicKey, prv }: AuditDecryptedKeyParams): void {
237237
if (multiSigType === 'tss') {
238-
const result = auditEcdsaPrivateKey(prv as string, publicKey as string);
239-
if (result.isValid) {
240-
return { isValid: true };
241-
} else {
242-
if (!result.isCommonKeychainValid) {
243-
return { isValid: false, message: 'Invalid common keychain' };
244-
} else if (!result.isPrivateKeyValid) {
245-
return { isValid: false, message: 'Invalid private key' };
246-
}
247-
return { isValid: false };
248-
}
238+
auditEcdsaPrivateKey(prv as string, publicKey as string);
249239
} else {
250240
if (!isValidPrv(prv) && !isValidXprv(prv)) {
251-
return { isValid: false, message: 'Invalid private key' };
241+
throw new Error('Invalid private key');
252242
}
253243
if (publicKey) {
254244
const genPubKey = bitcoin.HDNode.fromBase58(prv).neutered().toBase58();
255245
if (genPubKey !== publicKey) {
256-
return { isValid: false, message: 'Incorrect xpub' };
246+
throw new Error('Incorrect xpub');
257247
}
258248
}
259-
260-
return { isValid: true };
261249
}
262250
}
263251
}

modules/abstract-lightning/src/abstractLightningCoin.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import {
2+
AuditDecryptedKeyParams,
23
BaseCoin,
34
BitGoBase,
45
KeyPair,
@@ -61,4 +62,9 @@ export abstract class AbstractLightningCoin extends BaseCoin {
6162
signTransaction(params: SignTransactionOptions): Promise<SignedTransaction> {
6263
throw new Error('Method not implemented.');
6364
}
65+
66+
/** @inheritDoc */
67+
auditDecryptedKey(params: AuditDecryptedKeyParams) {
68+
throw new Error('Method not implemented.');
69+
}
6470
}

modules/abstract-substrate/src/abstractSubstrateCoin.ts

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import {
22
AuditDecryptedKeyParams,
3-
AuditKeyResponse,
43
BaseCoin,
54
BitGoBase,
65
EDDSAMethods,
@@ -523,20 +522,10 @@ export class SubstrateCoin extends BaseCoin {
523522
}
524523

525524
/** inherited doc */
526-
auditDecryptedKey({ publicKey, prv, multiSigType }: AuditDecryptedKeyParams): AuditKeyResponse {
525+
auditDecryptedKey({ publicKey, prv, multiSigType }: AuditDecryptedKeyParams) {
527526
if (multiSigType !== 'tss') {
528527
throw new Error('Unsupported multiSigType');
529528
}
530-
const result = auditEddsaPrivateKey(prv, publicKey ?? '');
531-
if (result.isValid) {
532-
return { isValid: true };
533-
} else {
534-
if (!result.isCommonKeychainValid) {
535-
return { isValid: false, message: 'Invalid common keychain' };
536-
} else if (!result.isPrivateKeyValid) {
537-
return { isValid: false, message: 'Invalid private key' };
538-
}
539-
return { isValid: false };
540-
}
529+
auditEddsaPrivateKey(prv, publicKey ?? '');
541530
}
542531
}

modules/abstract-utxo/src/abstractUtxoCoin.ts

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ import {
1313
ExtraPrebuildParamsOptions,
1414
HalfSignedUtxoTransaction,
1515
IBaseCoin,
16-
AuditKeyResponse,
1716
InvalidAddressDerivationPropertyError,
1817
InvalidAddressError,
1918
IRequestTracer,
@@ -1141,20 +1140,19 @@ export abstract class AbstractUtxoCoin extends BaseCoin {
11411140
return forCoin(this.getChain(), apiToken);
11421141
}
11431142

1144-
auditDecryptedKey({ multiSigType, publicKey, prv }): AuditKeyResponse {
1143+
/** @inheritDoc */
1144+
auditDecryptedKey({ multiSigType, publicKey, prv }) {
11451145
if (multiSigType === 'tss') {
11461146
throw new Error('tss auditing is not supported for this coin');
11471147
}
11481148
if (!isValidPrv(prv) && !isValidXprv(prv)) {
1149-
return { isValid: false, message: 'Invalid private key' };
1149+
throw new Error('invalid private key');
11501150
}
11511151
if (publicKey) {
11521152
const genPubKey = bitcoin.HDNode.fromBase58(prv).neutered().toBase58();
11531153
if (genPubKey !== publicKey) {
1154-
return { isValid: false, message: 'Incorrect xpub' };
1154+
throw new Error('public key does not match private key');
11551155
}
11561156
}
1157-
1158-
return { isValid: true };
11591157
}
11601158
}

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

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@ import {
3232
MultisigType,
3333
multisigTypes,
3434
AuditDecryptedKeyParams,
35-
AuditKeyResponse,
3635
} from '@bitgo/sdk-core';
3736
import { KeyPair as AdaKeyPair, Transaction, TransactionBuilderFactory, Utils } from './lib';
3837
import { BaseCoin as StaticsBaseCoin, CoinFamily, coins } from '@bitgo/statics';
@@ -632,22 +631,11 @@ export class Ada extends BaseCoin {
632631
}
633632

634633
/** inherited doc */
635-
auditDecryptedKey({ publicKey, prv, multiSigType }: AuditDecryptedKeyParams): AuditKeyResponse {
634+
auditDecryptedKey({ publicKey, prv, multiSigType }: AuditDecryptedKeyParams) {
636635
if (multiSigType !== 'tss') {
637636
throw new Error('Unsupported multiSigType');
638637
}
639638

640-
const result = auditEddsaPrivateKey(prv, publicKey ?? '');
641-
642-
if (result.isValid) {
643-
return { isValid: true };
644-
} else {
645-
if (!result.isCommonKeychainValid) {
646-
return { isValid: false, message: 'Invalid common keychain' };
647-
} else if (!result.isPrivateKeyValid) {
648-
return { isValid: false, message: 'Invalid private key' };
649-
}
650-
return { isValid: false };
651-
}
639+
auditEddsaPrivateKey(prv, publicKey ?? '');
652640
}
653641
}

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

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ import {
3030
MultisigType,
3131
multisigTypes,
3232
AuditDecryptedKeyParams,
33-
AuditKeyResponse,
3433
} from '@bitgo/sdk-core';
3534
import stellar from 'stellar-sdk';
3635
import BigNumber from 'bignumber.js';
@@ -850,20 +849,21 @@ export class Algo extends BaseCoin {
850849
}
851850

852851
/** @inheritDoc */
853-
protected auditDecryptedKey({ publicKey, prv, multiSigType }: AuditDecryptedKeyParams): AuditKeyResponse {
852+
auditDecryptedKey({ publicKey, prv, multiSigType }: AuditDecryptedKeyParams) {
854853
if (multiSigType === 'tss') {
855854
throw new Error('Unsupported multiSigType');
856855
}
857856

857+
let algoKey;
858858
try {
859-
const algoKey = new AlgoLib.KeyPair({ prv });
860-
if (publicKey && publicKey !== algoKey.getKeys().pub) {
861-
return { isValid: false, message: 'Incorrect ALGO public key' };
862-
}
859+
algoKey = new AlgoLib.KeyPair({ prv });
863860
} catch (e) {
864-
return { isValid: false, message: 'Invalid private key' };
861+
throw new Error('Invalid private key');
862+
}
863+
if (publicKey && publicKey !== algoKey.getKeys().pub) {
864+
throw new Error('Invalid public key');
865865
}
866866

867-
return { isValid: true };
867+
return;
868868
}
869869
}

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

Lines changed: 33 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1134,38 +1134,50 @@ describe('ALGO:', function () {
11341134
const { key } = algoBackupKey;
11351135
const walletPassphrase = 'ZQ8MhxT84m4P';
11361136

1137-
it('should return { isValid: true) } for valid inputs', async () => {
1138-
const result = await basecoin.auditKey({
1137+
it('should return for valid inputs', () => {
1138+
basecoin.assertIsValidKey({
11391139
encryptedPrv: key,
11401140
walletPassphrase,
11411141
});
1142-
result.should.deepEqual({ isValid: true });
11431142
});
11441143

1145-
it('should return { isValid: false } if the walletPassphrase is incorrect', async () => {
1146-
const result = await basecoin.auditKey({
1147-
encryptedPrv: key,
1148-
walletPassphrase: 'foo',
1149-
});
1150-
result.should.deepEqual({ isValid: false, message: "failed to decrypt prv: ccm: tag doesn't match" });
1144+
it('should throw error if the walletPassphrase is incorrect', () => {
1145+
assert.throws(
1146+
() => {
1147+
basecoin.assertIsValidKey({
1148+
encryptedPrv: key,
1149+
walletPassphrase: 'foo',
1150+
});
1151+
},
1152+
{ message: "failed to decrypt prv: ccm: tag doesn't match" }
1153+
);
11511154
});
1152-
it('should return { isValid: false } if the key is altered', async () => {
1155+
1156+
it('should throw error if the key is altered', () => {
11531157
const alteredKey = key.replace(/[0-9]/g, '0');
1154-
const result = await basecoin.auditKey({
1155-
encryptedPrv: alteredKey,
1156-
walletPassphrase,
1157-
});
1158-
result.isValid.should.equal(false);
1158+
assert.throws(
1159+
() => {
1160+
basecoin.assertIsValidKey({
1161+
encryptedPrv: alteredKey,
1162+
walletPassphrase,
1163+
});
1164+
},
1165+
{ message: 'failed to decrypt prv: json decrypt: invalid parameters' }
1166+
);
11591167
});
11601168

1161-
it('should return { isValid: false } if the key is not a valid key', async () => {
1169+
it('should throw error if the key is not a valid key', () => {
11621170
const invalidKey = '#@)$#($*@)#($*';
11631171
const encryptedPrv = encrypt(walletPassphrase, invalidKey);
1164-
const result = await basecoin.auditKey({
1165-
encryptedPrv,
1166-
walletPassphrase,
1167-
});
1168-
result.should.deepEqual({ isValid: false, message: 'Invalid private key' });
1172+
assert.throws(
1173+
() => {
1174+
basecoin.assertIsValidKey({
1175+
encryptedPrv,
1176+
walletPassphrase,
1177+
});
1178+
},
1179+
{ message: 'Invalid private key' }
1180+
);
11691181
});
11701182
});
11711183
});

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import {
2+
AuditDecryptedKeyParams,
23
BaseCoin,
34
BaseTransaction,
45
BitGoBase,
@@ -21,6 +22,7 @@ import * as _ from 'lodash';
2122
import BigNumber from 'bignumber.js';
2223
import { ExplainTransactionOptions } from './lib/types';
2324
import { AptTransactionExplanation } from './lib/iface';
25+
import { auditEddsaPrivateKey } from '@bitgo/sdk-lib-mpc';
2426

2527
export interface AptParseTransactionOptions extends ParseTransactionOptions {
2628
txHex: string;
@@ -197,4 +199,12 @@ export class Apt extends BaseCoin {
197199
throw new Error('Failed to rebuild transaction');
198200
}
199201
}
202+
203+
/** @inheritDoc */
204+
auditDecryptedKey({ multiSigType, prv, publicKey }: AuditDecryptedKeyParams): void {
205+
if (multiSigType !== 'tss') {
206+
throw new Error('Unsupported multiSigType');
207+
}
208+
auditEddsaPrivateKey(prv, publicKey ?? '');
209+
}
200210
}

0 commit comments

Comments
 (0)