Skip to content

Commit bd7069f

Browse files
feat: eth like coins family and follow up feedback
1 parent 3932df7 commit bd7069f

3 files changed

Lines changed: 42 additions & 52 deletions

File tree

src/api/enclaved/recoveryMultisigTransaction.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import { SignFinalOptions } from '@bitgo/abstract-eth';
2+
import { MethodNotImplementedError } from 'bitgo';
23
import { EnclavedApiSpecRouteRequest } from '../../enclavedBitgoExpress/routers/enclavedApiSpec';
34
import logger from '../../logger';
4-
import { isEthCoin } from '../../shared/coinUtils';
5+
import { isEthLikeCoin } from '../../shared/coinUtils';
56
import { retrieveKmsKey } from './utils';
67

78
export async function recoveryMultisigTransaction(
@@ -20,13 +21,13 @@ export async function recoveryMultisigTransaction(
2021
}
2122

2223
const bitgo = req.bitgo;
23-
const coin = bitgo.coin(req.params.coin);
24+
const coin = bitgo.coin(req.decoded.coin);
2425

2526
// The signed transaction format depends on the coin type so we do this check as a guard
2627
// If you check the type of coin before and after the "if", you may see "BaseCoin" vs "AbstractEthLikeCoin"
2728
if (coin.isEVM()) {
2829
// Every recovery method on every coin family varies one from another so we need to ensure with a guard.
29-
if (isEthCoin(coin)) {
30+
if (isEthLikeCoin(coin)) {
3031
// TODO: populate coinSpecificParams with things like replayProtectionOptions
3132
// coinSpecificParams type could be "recoverOptions"
3233
try {
@@ -59,11 +60,11 @@ export async function recoveryMultisigTransaction(
5960
throw error;
6061
}
6162
} else {
62-
const errorMsg = 'Unsupported coin type for recovery: ' + req.params.coin;
63+
const errorMsg = 'Unsupported coin type for recovery: ' + req.decoded.coin;
6364
logger.error(errorMsg);
6465
throw new Error(errorMsg);
6566
}
6667
} else {
67-
throw new Error('Unsupported coin type for recovery: ' + coin);
68+
throw new MethodNotImplementedError('Unsupported coin type for recovery: ' + coin);
6869
}
6970
}

src/masterBitgoExpress/recoveryWallet.ts

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import assert from 'assert';
2-
import { isEthCoin } from '../shared/coinUtils';
2+
import { MethodNotImplementedError } from 'bitgo';
3+
import { isEthLikeCoin } from '../shared/coinUtils';
34
import { isMasterExpressConfig } from '../types';
45
import { createEnclavedExpressClient } from './enclavedExpressClient';
56
import { MasterApiSpecRouteRequest } from './routers/masterApiSpec';
@@ -8,7 +9,7 @@ export async function handleRecoveryWalletOnPrem(
89
req: MasterApiSpecRouteRequest<'v1.wallet.recovery', 'post'>,
910
) {
1011
const bitgo = req.bitgo;
11-
const coin = req.params.coin;
12+
const coin = req.decoded.coin;
1213
assert(
1314
isMasterExpressConfig(req.config),
1415
'Expected req.config to be of type MasterExpressConfig',
@@ -27,7 +28,7 @@ export async function handleRecoveryWalletOnPrem(
2728
recoveryDestinationAddress,
2829
coinSpecificParams,
2930
apiKey,
30-
} = req.body;
31+
} = req.decoded;
3132

3233
//construct a common payload for the recovery that it's repeated in any kind of recovery
3334
const commonRecoveryParams = {
@@ -40,12 +41,10 @@ export async function handleRecoveryWalletOnPrem(
4041

4142
const sdkCoin = bitgo.coin(coin);
4243

43-
if (isEthCoin(sdkCoin)) {
44+
if (isEthLikeCoin(sdkCoin)) {
4445
try {
4546
const unsignedSweepPrebuildTx = await sdkCoin.recover({
4647
...commonRecoveryParams,
47-
//TODO: DELETE. it's needed for keycard debugging, the walletPassphrase
48-
//walletPassphrase: passphrase,
4948
});
5049
const fullSignedRecoveryTx = await enclavedExpressClient.recoveryMultisig({
5150
userPub,
@@ -59,10 +58,9 @@ export async function handleRecoveryWalletOnPrem(
5958

6059
return fullSignedRecoveryTx;
6160
} catch (err) {
62-
//TODO: check other error handling for ref on mbe
6361
throw err;
6462
}
6563
} else {
66-
throw new Error('Recovery wallet is not supported for this coin: ' + coin);
64+
throw new MethodNotImplementedError('Recovery wallet is not supported for this coin: ' + coin);
6765
}
6866
}

src/shared/coinUtils.ts

Lines changed: 30 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,66 +1,57 @@
11
import { AbstractEthLikeNewCoins } from '@bitgo/abstract-eth';
2+
import { CoinFamily } from '@bitgo/statics';
23
import { BaseCoin } from 'bitgo';
34
import { AbstractUtxoCoin, Eos, Stx, Xtz } from 'bitgo/dist/types/src/v2/coins';
45

5-
export function isEthCoin(coin: BaseCoin): coin is AbstractEthLikeNewCoins {
6-
const isEthPure =
7-
isFamily(coin, 'eth', 'gteth') ||
8-
isFamily(coin, 'eth', 'hteth') ||
9-
isFamily(coin, 'ethw', 'tethw');
6+
export function isEthLikeCoin(coin: BaseCoin): coin is AbstractEthLikeNewCoins {
7+
const isEthPure = isFamily(coin, CoinFamily.ETH);
108

119
const isEthLike =
12-
isFamily(coin, 'rbtc', 'trbtc') ||
13-
isFamily(coin, 'etc', 'tetc') ||
14-
isFamily(coin, 'avaxc', 'tavaxc') ||
15-
isFamily(coin, 'polygon', 'tpolygon') ||
16-
isFamily(coin, 'arbeth', 'tarbeth') ||
17-
isFamily(coin, 'opeth', 'topeth') ||
18-
isFamily(coin, 'bsc', 'tbsc') ||
19-
isFamily(coin, 'baseeth', 'tbaseeth') ||
20-
isFamily(coin, 'coredao', 'tcoredao') ||
21-
isFamily(coin, 'oas', 'toas') ||
22-
isFamily(coin, 'flr', 'tflr') ||
23-
isFamily(coin, 'sgb', 'tsgb') ||
24-
isFamily(coin, 'wemix', 'twemix') ||
25-
isFamily(coin, 'xdc', 'txdc');
10+
isFamily(coin, CoinFamily.ETHW) || // ethw has its own family. as the others
11+
isFamily(coin, CoinFamily.RBTC) ||
12+
isFamily(coin, CoinFamily.ETC) ||
13+
isFamily(coin, CoinFamily.AVAXC) ||
14+
isFamily(coin, CoinFamily.POLYGON) ||
15+
isFamily(coin, CoinFamily.ARBETH) ||
16+
isFamily(coin, CoinFamily.OPETH) ||
17+
isFamily(coin, CoinFamily.BSC) ||
18+
isFamily(coin, CoinFamily.BASEETH) ||
19+
isFamily(coin, CoinFamily.COREDAO) ||
20+
isFamily(coin, CoinFamily.OAS) ||
21+
isFamily(coin, CoinFamily.FLR) ||
22+
isFamily(coin, CoinFamily.SGB) ||
23+
isFamily(coin, CoinFamily.WEMIX) ||
24+
isFamily(coin, CoinFamily.XDC);
2625

2726
return isEthPure || isEthLike;
2827
}
2928

3029
export function isUtxoCoin(coin: BaseCoin): coin is AbstractUtxoCoin {
31-
// how to check if coin is UTXO? so many families
32-
const isBtc = isFamily(coin, 'btc', 'tbtc');
30+
const isBtc = isFamily(coin, CoinFamily.BTC);
3331

3432
const isBtcLike =
35-
isFamily(coin, 'ltc', 'tltc') ||
36-
isFamily(coin, 'bch', 'tbch') ||
37-
isFamily(coin, 'zec', 'tzec') ||
38-
isFamily(coin, 'dash', 'tdash') ||
39-
isFamily(coin, 'doge', 'tdoge') ||
40-
isFamily(coin, 'btg', 'tbtg');
33+
isFamily(coin, CoinFamily.LTC) ||
34+
isFamily(coin, CoinFamily.BCH) ||
35+
isFamily(coin, CoinFamily.ZEC) ||
36+
isFamily(coin, CoinFamily.DASH) ||
37+
isFamily(coin, CoinFamily.DASH) ||
38+
isFamily(coin, CoinFamily.BTG);
4139

4240
return isBtc || isBtcLike;
4341
}
4442

45-
//look for those on OVC repo
46-
//https://github.com/BitGo/offline-vault-console/blob/7f850cdd10c89ceb850c69759349b9e0bbfb56db/frontend/src/pkg/bitgo/transaction-utils.ts#L595
4743
export function isEosCoin(coin: BaseCoin): coin is Eos {
48-
return isFamily(coin, 'eos', 'teos');
44+
return isFamily(coin, CoinFamily.EOS);
4945
}
5046

5147
export function isStxCoin(coin: BaseCoin): coin is Stx {
52-
return isFamily(coin, 'stx', 'tstx');
48+
return isFamily(coin, CoinFamily.STX);
5349
}
5450

5551
export function isXtzCoin(coin: BaseCoin): coin is Xtz {
56-
// Tezos faucet: https://faucet.ghostnet.teztnets.com/
57-
return isFamily(coin, 'xtz', 'txtz');
52+
return isFamily(coin, CoinFamily.XTZ);
5853
}
5954

60-
function isFamily(coin: BaseCoin, coinFamily: string, testFamily: string) {
61-
if (!coin) {
62-
return false;
63-
}
64-
const family = coin.getFamily();
65-
return family === coinFamily || family === testFamily;
55+
function isFamily(coin: BaseCoin, family: CoinFamily) {
56+
return Boolean(coin && coin.getFamily() === family);
6657
}

0 commit comments

Comments
 (0)