Skip to content

Commit

Permalink
chore(root): add eslint-plugin-import
Browse files Browse the repository at this point in the history
- prevent importing from internal modules
- fixes lint errors from new rule

Ticket: BG-00000
  • Loading branch information
bitgoAaron committed Jun 11, 2022
1 parent 3fe6b99 commit d584b4d
Show file tree
Hide file tree
Showing 30 changed files with 132 additions and 118 deletions.
29 changes: 16 additions & 13 deletions .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,13 @@
"mocha": true
},
"parser": "@typescript-eslint/parser",
"plugins": ["@typescript-eslint", "prettier"],
"plugins": ["@typescript-eslint", "prettier", "import"],
"globals": {
"app": true, // BitGo side-effect from testutil
"ethUtil": true, // BitGo side-effect from testutil
"app": true, // BitGo side-effect from testutil
"ethUtil": true, // BitGo side-effect from testutil
"requireCommon": true
},
"extends": [
"plugin:@typescript-eslint/recommended",
"plugin:prettier/recommended"
],
"extends": ["plugin:@typescript-eslint/recommended", "plugin:prettier/recommended"],
"parserOptions": {
"ecmaVersion": 6
},
Expand All @@ -36,7 +33,7 @@
"eqeqeq": ["warn", "always"],
"func-call-spacing": ["error", "never"],
"func-names": "off",
"indent": ["warn", 2, {"SwitchCase": 1, "MemberExpression": 1}],
"indent": ["warn", 2, { "SwitchCase": 1, "MemberExpression": 1 }],
"key-spacing": ["error", { "beforeColon": false, "afterColon": true, "mode": "strict" }],
"keyword-spacing": ["error"],
"linebreak-style": ["error", "unix"],
Expand All @@ -51,7 +48,7 @@
"no-fallthrough": "error",
"no-inner-declarations": "off",
"no-mixed-spaces-and-tabs": "error",
"no-multi-spaces": ["error", {"ignoreEOLComments": true}],
"no-multi-spaces": ["error", { "ignoreEOLComments": true }],
"no-multiple-empty-lines": ["error", { "max": 2, "maxEOF": 1 }],
"no-octal": "error",
"no-path-concat": "off",
Expand All @@ -63,10 +60,10 @@
"no-undef": "error",
"no-unneeded-ternary": "error",
"no-unreachable": "error",
"@typescript-eslint/no-unused-vars": ["error", {"vars": "all", "args": "none"}],
"@typescript-eslint/no-unused-vars": ["error", { "vars": "all", "args": "none" }],
"no-useless-escape": "off",
"no-var": "error",
"object-curly-spacing": ["error", "always", {"objectsInObjects": true, "arraysInObjects": true}],
"object-curly-spacing": ["error", "always", { "objectsInObjects": true, "arraysInObjects": true }],
"prefer-const": "error",
"prefer-rest-params": "warn",
"prefer-spread": "warn",
Expand All @@ -78,8 +75,14 @@
"space-before-blocks": ["error"],
"space-infix-ops": ["error"],
"spaced-comment": ["error", "always"],
"switch-colon-spacing": ["error", {"before": false, "after": true}],
"yield-star-spacing": ["error", {"before": true, "after": false}]
"switch-colon-spacing": ["error", { "before": false, "after": true }],
"yield-star-spacing": ["error", { "before": true, "after": false }],
"import/no-internal-modules": [
"error",
{
"forbid": ["@bitgo/*/**"]
}
]
},
"overrides": [
{
Expand Down
22 changes: 9 additions & 13 deletions modules/abstract-utxo/src/abstractUtxoCoin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,7 @@
*/
import * as bip32 from 'bip32';
import * as utxolib from '@bitgo/utxo-lib';
import {
getExternalChainCode,
isChainCode,
RootWalletKeys,
scriptTypeForChain,
outputScripts,
toOutput,
Unspent,
verifySignatureWithUnspent,
WalletUnspentSigner,
} from '@bitgo/utxo-lib/dist/src/bitgo';
import { bitgo } from '@bitgo/utxo-lib';
import * as bitcoinMessage from 'bitcoinjs-message';
import { randomBytes } from 'crypto';
import * as debugLib from 'debug';
Expand Down Expand Up @@ -70,6 +60,11 @@ import ScriptType2Of3 = utxolib.bitgo.outputScripts.ScriptType2Of3;
import { isReplayProtectionUnspent } from './replayProtection';
import { signAndVerifyWalletTransaction } from './sign';
import { supportedCrossChainRecoveries } from './config';

const { getExternalChainCode, isChainCode, scriptTypeForChain, outputScripts, toOutput, verifySignatureWithUnspent } =
bitgo;
type Unspent = bitgo.Unspent;
type RootWalletKeys = bitgo.RootWalletKeys;
export interface VerifyAddressOptions extends BaseVerifyAddressOptions {
chain: number;
index: number;
Expand Down Expand Up @@ -1094,7 +1089,7 @@ export abstract class AbstractUtxoCoin extends BaseCoin {
const signedTransaction = signAndVerifyWalletTransaction(
transaction,
txPrebuild.txInfo.unspents,
new WalletUnspentSigner<RootWalletKeys>(keychains, signerKeychain, cosignerKeychain),
new bitgo.WalletUnspentSigner<RootWalletKeys>(keychains, signerKeychain, cosignerKeychain),
{ isLastSignature }
);

Expand Down Expand Up @@ -1208,7 +1203,8 @@ export abstract class AbstractUtxoCoin extends BaseCoin {

// if keys are provided, prepare the keys for input signature checking
const keys = params.pubs?.map((xpub) => bip32.fromBase58(xpub));
const walletKeys = keys && keys.length === 3 ? new RootWalletKeys(keys as Triple<bip32.BIP32Interface>) : undefined;
const walletKeys =
keys && keys.length === 3 ? new bitgo.RootWalletKeys(keys as Triple<bip32.BIP32Interface>) : undefined;

// get the number of signatures per input
const inputSignatureCounts = transaction.ins.map((input, idx): number => {
Expand Down
3 changes: 2 additions & 1 deletion modules/abstract-utxo/src/recovery/RecoveryProvider.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { AddressInfo, BlockchairApi, BlockstreamApi } from '@bitgo/blockapis';
import { Unspent } from '@bitgo/utxo-lib/dist/src/bitgo/Unspent';
import { bitgo } from '@bitgo/utxo-lib';
type Unspent = bitgo.Unspent;

import { ApiNotImplementedError } from './baseApi';

Expand Down
22 changes: 12 additions & 10 deletions modules/abstract-utxo/src/recovery/backupKeyRecovery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,19 @@

import * as _ from 'lodash';
import * as utxolib from '@bitgo/utxo-lib';
import {
ChainCode,
const {
getInternalChainCode,
parseOutputId,
RootWalletKeys,
scriptTypeForChain,
WalletUnspent,
WalletUnspentSigner,
outputScripts,
getExternalChainCode,
} from '@bitgo/utxo-lib/dist/src/bitgo';
} = utxolib.bitgo;

type ChainCode = utxolib.bitgo.ChainCode;
type RootWalletKeys = utxolib.bitgo.RootWalletKeys;
type WalletUnspent = utxolib.bitgo.WalletUnspent;
type ScriptType2Of3 = utxolib.bitgo.outputScripts.ScriptType2Of3;

import { VirtualSizes } from '@bitgo/unspents';

Expand Down Expand Up @@ -123,7 +125,7 @@ export interface RecoverParams {
bitgoKey: string;
recoveryDestination: string;
krsProvider?: string;
ignoreAddressTypes: outputScripts.ScriptType2Of3[];
ignoreAddressTypes: ScriptType2Of3[];
walletPassphrase?: string;
apiKey?: string;
userKeyPath?: string;
Expand Down Expand Up @@ -258,10 +260,10 @@ export async function backupKeyRecovery(
if (!isTriple(keys)) {
throw new Error(`expected key triple`);
}
const walletKeys = new RootWalletKeys(keys, [
params.userKeyPath || RootWalletKeys.defaultPrefix,
RootWalletKeys.defaultPrefix,
RootWalletKeys.defaultPrefix,
const walletKeys = new utxolib.bitgo.RootWalletKeys(keys, [
params.userKeyPath || utxolib.bitgo.RootWalletKeys.defaultPrefix,
utxolib.bitgo.RootWalletKeys.defaultPrefix,
utxolib.bitgo.RootWalletKeys.defaultPrefix,
]);

const unspents: WalletUnspent[] = (
Expand Down
24 changes: 11 additions & 13 deletions modules/abstract-utxo/src/recovery/crossChainRecovery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,12 @@ import * as Bluebird from 'bluebird';

import * as bip32 from 'bip32';
import * as utxolib from '@bitgo/utxo-lib';
import {
RootWalletKeys,
Unspent,
unspentSum,
scriptTypeForChain,
outputScripts,
WalletUnspent,
WalletUnspentLegacy,
WalletUnspentSigner,
} from '@bitgo/utxo-lib/dist/src/bitgo';
const { unspentSum, scriptTypeForChain, outputScripts } = utxolib.bitgo;
type RootWalletKeys = utxolib.bitgo.RootWalletKeys;
type Unspent = utxolib.bitgo.Unspent;
type WalletUnspent = utxolib.bitgo.WalletUnspent;
type WalletUnspentLegacy = utxolib.bitgo.WalletUnspentLegacy;

import { Dimensions } from '@bitgo/unspents';

import { BitGoBase, IWallet, Keychain, Triple, Wallet } from '@bitgo/sdk-core';
Expand Down Expand Up @@ -153,7 +149,7 @@ async function getWalletKeys(recoveryCoin: AbstractUtxoCoin, wallet: IWallet | W
xpubs = (wallet as WalletV1).keychains.map((k) => k.xpub) as Triple<string>;
}

return new RootWalletKeys(xpubs.map((k) => bip32.fromBase58(k)) as Triple<bip32.BIP32Interface>);
return new utxolib.bitgo.RootWalletKeys(xpubs.map((k) => bip32.fromBase58(k)) as Triple<bip32.BIP32Interface>);
}

/**
Expand Down Expand Up @@ -293,7 +289,7 @@ function createSweepTransaction(
unspents: WalletUnspent[],
targetAddress: string,
feeRateSatVB: number,
signer?: WalletUnspentSigner<RootWalletKeys>
signer?: utxolib.bitgo.WalletUnspentSigner<RootWalletKeys>
): utxolib.bitgo.UtxoTransaction {
const inputValue = unspentSum(unspents);
const vsize = Dimensions.fromUnspents(unspents)
Expand Down Expand Up @@ -408,7 +404,9 @@ export async function recoverCrossChain(
const walletKeys = await getWalletKeys(params.recoveryCoin, wallet);
const prv =
params.xprv || params.walletPassphrase ? await getPrv(params.xprv, params.walletPassphrase, wallet) : undefined;
const signer = prv ? new WalletUnspentSigner<RootWalletKeys>(walletKeys, prv, walletKeys.bitgo) : undefined;
const signer = prv
? new utxolib.bitgo.WalletUnspentSigner<RootWalletKeys>(walletKeys, prv, walletKeys.bitgo)
: undefined;
const feeRateSatVB = await getFeeRateSatVB(params.sourceCoin);
const transaction = createSweepTransaction(
params.sourceCoin.network,
Expand Down
3 changes: 2 additions & 1 deletion modules/abstract-utxo/src/replayProtection.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import * as utxolib from '@bitgo/utxo-lib';
import { Unspent } from '@bitgo/utxo-lib/dist/src/bitgo';
import { bitgo } from '@bitgo/utxo-lib';
type Unspent = bitgo.Unspent;

export function getReplayProtectionAddresses(network: utxolib.Network): string[] {
switch (network) {
Expand Down
14 changes: 5 additions & 9 deletions modules/abstract-utxo/src/sign.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,10 @@
* @prettier
*/
import * as utxolib from '@bitgo/utxo-lib';
import {
isWalletUnspent,
RootWalletKeys,
signInputWithUnspent,
toOutput,
Unspent,
WalletUnspentSigner,
} from '@bitgo/utxo-lib/dist/src/bitgo';
const { isWalletUnspent, signInputWithUnspent, toOutput } = utxolib.bitgo;
type Unspent = utxolib.bitgo.Unspent;
type RootWalletKeys = utxolib.bitgo.RootWalletKeys;

import * as debugLib from 'debug';

import { isReplayProtectionUnspent } from './replayProtection';
Expand Down Expand Up @@ -48,7 +44,7 @@ export class TransactionSigningError extends Error {
export function signAndVerifyWalletTransaction(
transaction: utxolib.bitgo.UtxoTransaction | utxolib.bitgo.UtxoTransactionBuilder,
unspents: Unspent[],
walletSigner: WalletUnspentSigner<RootWalletKeys>,
walletSigner: utxolib.bitgo.WalletUnspentSigner<RootWalletKeys>,
{ isLastSignature }: { isLastSignature: boolean }
): utxolib.bitgo.UtxoTransaction {
const network = transaction.network as utxolib.Network;
Expand Down
1 change: 1 addition & 0 deletions modules/account-lib/src/coin/cspr/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import * as Utils from './utils';

export { Transaction } from './transaction';
export { TransactionBuilderFactory } from './transactionBuilderFactory';
export { KeyPair } from './keyPair';

Expand Down
4 changes: 2 additions & 2 deletions modules/bitgo/src/v2/coins/sol.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import * as base58 from 'bs58';
import { BaseCoin as StaticsBaseCoin, CoinFamily, coins } from '@bitgo/statics';
import * as accountLib from '@bitgo/account-lib';
import * as _ from 'lodash';
import { AtaInitializationBuilder } from '@bitgo/account-lib/dist/src/coin/sol';
import { Sol as SolLib } from '@bitgo/account-lib';
import {
BaseCoin,
BitGoBase,
Expand Down Expand Up @@ -305,7 +305,7 @@ export class Sol extends BaseCoin {

try {
const transactionBuilder = factory.from(params.txBase64).fee({ amount: params.feeInfo.fee });
if (transactionBuilder instanceof AtaInitializationBuilder && params.tokenAccountRentExemptAmount) {
if (transactionBuilder instanceof SolLib.AtaInitializationBuilder && params.tokenAccountRentExemptAmount) {
transactionBuilder.rentExemptAmount(params.tokenAccountRentExemptAmount);
}
rebuiltTransaction = await transactionBuilder.build();
Expand Down
4 changes: 2 additions & 2 deletions modules/bitgo/src/wallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,12 @@ import { common, getNetwork, getSharedSecret, makeRandomKey, sanitizeLegacyPath
import * as Bluebird from 'bluebird';
const co = Bluebird.coroutine;
import * as _ from 'lodash';
import {
const {
getExternalChainCode,
getInternalChainCode,
isChainCode,
scriptTypeForChain,
} from '@bitgo/utxo-lib/dist/src/bitgo';
} = utxolib.bitgo;
const request = require('superagent');

//
Expand Down
3 changes: 2 additions & 1 deletion modules/bitgo/test/v2/unit/coins/cspr.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@ import { Cspr as CsprAccountLib, register } from '@bitgo/account-lib';
import { TestBitGo } from '../../../lib/test_bitgo';
import { Cspr, Tcspr } from '../../../../src/v2/coins';
import { ExplainTransactionOptions, TransactionFee } from '../../../../src/v2/coins/cspr';
import { Transaction } from '@bitgo/account-lib/dist/src/coin/cspr/transaction';
import { randomBytes } from 'crypto';
import * as should from 'should';
import { signedRawDelegateTx, signedRawTransferTx, signedRawUndelegateTx } from '../../fixtures/coins/cspr';
import { TransactionType } from '@bitgo/sdk-core';

type Transaction = CsprAccountLib.Transaction;

describe('Casper', function () {
const coinName = 'tcspr';
let bitgo;
Expand Down
1 change: 1 addition & 0 deletions modules/bitgo/test/v2/unit/coins/sol.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import * as sinon from 'sinon';
import { TestBitGo } from '../../../lib/test_bitgo';
import * as testData from '../../fixtures/coins/sol';
import * as should from 'should';
// eslint-disable-next-line import/no-internal-modules
import * as resources from '@bitgo/account-lib/test/resources/sol/sol';
import * as _ from 'lodash';
import * as accountLib from '@bitgo/account-lib';
Expand Down
2 changes: 1 addition & 1 deletion modules/bitgo/test/v2/unit/coins/utxo/address.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import 'should';
import * as assert from 'assert';
import * as utxolib from '@bitgo/utxo-lib';
import { chainCodes } from '@bitgo/utxo-lib/dist/src/bitgo';
const { chainCodes } = utxolib.bitgo;

import { AbstractUtxoCoin, GenerateAddressOptions } from '@bitgo/abstract-utxo';

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@ import * as nock from 'nock';
import * as bip32 from 'bip32';

import * as utxolib from '@bitgo/utxo-lib';
import { RootWalletKeys, toOutput, outputScripts, WalletUnspent } from '@bitgo/utxo-lib/dist/src/bitgo';
const { toOutput, outputScripts } = utxolib.bitgo;
type WalletUnspent = utxolib.bitgo.WalletUnspent;
type RootWalletKeys = utxolib.bitgo.RootWalletKeys;
type ScriptType2Of3 = utxolib.bitgo.outputScripts.ScriptType2Of3;

import { Config } from '../../../../../../src/config';
import {
Expand Down Expand Up @@ -93,7 +96,7 @@ function getKeysForFullSignedRecovery(

function run(
coin: AbstractUtxoCoin,
scriptType: outputScripts.ScriptType2Of3,
scriptType: ScriptType2Of3,
walletKeys: RootWalletKeys,
params: {
keys: NamedKeys;
Expand Down Expand Up @@ -269,10 +272,10 @@ utxoCoins.forEach((coin) => {

{
const userKeyPath = '99/99';
const exoticWalletKeys = new RootWalletKeys(keychains, [
const exoticWalletKeys = new utxolib.bitgo.RootWalletKeys(keychains, [
userKeyPath,
RootWalletKeys.defaultPrefix,
RootWalletKeys.defaultPrefix,
utxolib.bitgo.RootWalletKeys.defaultPrefix,
utxolib.bitgo.RootWalletKeys.defaultPrefix,
]);

run(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ import * as assert from 'assert';
import * as should from 'should';
import * as nock from 'nock';
import * as utxolib from '@bitgo/utxo-lib';
import { Unspent, WalletUnspent } from '@bitgo/utxo-lib/dist/src/bitgo';

import { Triple } from '@bitgo/sdk-core';
import {
AbstractUtxoCoin,
Expand Down Expand Up @@ -36,6 +34,9 @@ import { nockBitGoPublicAddressUnspents, nockBitGoPublicTransaction } from '../u
import { createFullSignedTransaction } from '../util/transaction';
import { getDefaultWalletUnspentSigner } from '../util/keychains';

type Unspent = utxolib.bitgo.Unspent;
type WalletUnspent = utxolib.bitgo.WalletUnspent;

function getKeyId(k: KeychainBase58): string {
return getSeed(k.pub).toString('hex');
}
Expand Down
Loading

0 comments on commit d584b4d

Please sign in to comment.