From 9d00a25325a208e9c536f5118777375fdba5ba93 Mon Sep 17 00:00:00 2001 From: Tyler Levine Date: Mon, 29 Jun 2020 14:50:25 -0700 Subject: [PATCH] Remove usages of `import * from crypto` This avoid an unwanted deprecation notice. See https://github.com/nodejs/node/issues/23203 for more details. Ticket: BG-22502 --- modules/core/src/bitgo.ts | 8 ++++---- modules/core/src/keychains.ts | 4 ++-- modules/core/src/v2/coins/abstractEthLikeCoin.ts | 4 ++-- modules/core/src/v2/coins/abstractUtxoCoin.ts | 4 ++-- modules/core/src/v2/coins/eos.ts | 4 ++-- modules/core/src/v2/coins/eth.ts | 4 ++-- modules/core/src/v2/coins/ofc.ts | 4 ++-- modules/core/src/v2/coins/trx.ts | 4 ++-- modules/core/src/v2/coins/xrp.ts | 4 ++-- modules/core/src/v2/internal/util.ts | 4 ++-- modules/core/test/v2/unit/coins/algo.ts | 4 ++-- modules/core/test/v2/unit/coins/xlm.ts | 4 ++-- 12 files changed, 26 insertions(+), 26 deletions(-) diff --git a/modules/core/src/bitgo.ts b/modules/core/src/bitgo.ts index 66a0d73c7f..f7aa765e50 100644 --- a/modules/core/src/bitgo.ts +++ b/modules/core/src/bitgo.ts @@ -27,7 +27,7 @@ import * as _ from 'lodash'; import * as url from 'url'; import * as querystring from 'querystring'; import * as config from './config'; -import * as crypto from 'crypto'; +import { createHmac, randomBytes } from 'crypto'; import * as debugLib from 'debug'; import { bytesToWord } from './v2/internal/internal'; @@ -733,7 +733,7 @@ export class BitGo { * @returns {*} - the result of the HMAC operation */ calculateHMAC(key: string, message: string): string { - return crypto.createHmac('sha256', key).update(message).digest('hex'); + return createHmac('sha256', key).update(message).digest('hex'); } /** @@ -895,8 +895,8 @@ export class BitGo { encrypt(params: EncryptOptions = {}): string { common.validateParams(params, ['input', 'password'], []); - const randomSalt = crypto.randomBytes(8); - const randomIV = crypto.randomBytes(16); + const randomSalt = randomBytes(8); + const randomIV = randomBytes(16); const encryptOptions = { iter: 10000, ks: 256, diff --git a/modules/core/src/keychains.ts b/modules/core/src/keychains.ts index 7d0f93fb86..c92e1858aa 100644 --- a/modules/core/src/keychains.ts +++ b/modules/core/src/keychains.ts @@ -11,7 +11,7 @@ // Copyright 2014, BitGo, Inc. All Rights Reserved. // -import * as crypto from 'crypto'; +import { randomBytes } from 'crypto'; import * as common from './common'; import { Util } from './v2/internal/util'; import * as bitcoin from 'bitgo-utxo-lib'; @@ -82,7 +82,7 @@ Keychains.prototype.create = function(params) { // An extended private key has both a normal 256 bit private key and a 256 // bit chain code, both of which must be random. 512 bits is therefore the // maximum entropy and gives us maximum security against cracking. - seed = crypto.randomBytes(512 / 8); + seed = randomBytes(512 / 8); } else { seed = params.seed; } diff --git a/modules/core/src/v2/coins/abstractEthLikeCoin.ts b/modules/core/src/v2/coins/abstractEthLikeCoin.ts index a4668f8d8a..b328cf2344 100644 --- a/modules/core/src/v2/coins/abstractEthLikeCoin.ts +++ b/modules/core/src/v2/coins/abstractEthLikeCoin.ts @@ -6,7 +6,7 @@ import { getBuilder, Eth } from '@bitgo/account-lib'; import * as Bluebird from 'bluebird'; import * as bitcoinMessage from 'bitcoinjs-message'; import * as bitgoUtxoLib from 'bitgo-utxo-lib'; -import * as crypto from 'crypto'; +import { randomBytes } from 'crypto'; import { BaseCoin, @@ -102,7 +102,7 @@ export abstract class AbstractEthLikeCoin extends BaseCoin { generateKeyPair(seed?: Buffer): KeyPair { if (!seed) { - seed = crypto.randomBytes(512 / 8); + seed = randomBytes(512 / 8); } const extendedKey = bitgoUtxoLib.HDNode.fromSeedBuffer(seed); const xpub = extendedKey.neutered().toBase58(); diff --git a/modules/core/src/v2/coins/abstractUtxoCoin.ts b/modules/core/src/v2/coins/abstractUtxoCoin.ts index d7665f8d3b..ede3d3848c 100644 --- a/modules/core/src/v2/coins/abstractUtxoCoin.ts +++ b/modules/core/src/v2/coins/abstractUtxoCoin.ts @@ -1,7 +1,7 @@ import * as bitcoin from 'bitgo-utxo-lib'; import * as bitcoinMessage from 'bitcoinjs-message'; import * as Bluebird from 'bluebird'; -import * as crypto from 'crypto'; +import { randomBytes } from 'crypto'; import * as request from 'superagent'; import * as _ from 'lodash'; import * as debugLib from 'debug'; @@ -1983,7 +1983,7 @@ export abstract class AbstractUtxoCoin extends BaseCoin { // An extended private key has both a normal 256 bit private key and a 256 // bit chain code, both of which must be random. 512 bits is therefore the // maximum entropy and gives us maximum security against cracking. - seed = crypto.randomBytes(512 / 8); + seed = randomBytes(512 / 8); } const extendedKey = bitcoin.HDNode.fromSeedBuffer(seed); const xpub = extendedKey.neutered().toBase58(); diff --git a/modules/core/src/v2/coins/eos.ts b/modules/core/src/v2/coins/eos.ts index 9353293185..c657f0d13c 100644 --- a/modules/core/src/v2/coins/eos.ts +++ b/modules/core/src/v2/coins/eos.ts @@ -15,7 +15,7 @@ import { } from '../baseCoin'; import { NodeCallback } from '../types'; import { BigNumber } from 'bignumber.js'; -import * as crypto from 'crypto'; +import { randomBytes } from 'crypto'; import { HDNode } from 'bitgo-utxo-lib'; import * as EosJs from 'eosjs'; import * as ecc from 'eosjs-ecc'; @@ -194,7 +194,7 @@ export class Eos extends BaseCoin { // An extended private key has both a normal 256 bit private key and a 256 // bit chain code, both of which must be random. 512 bits is therefore the // maximum entropy and gives us maximum security against cracking. - seed = crypto.randomBytes(512 / 8); + seed = randomBytes(512 / 8); } const extendedKey = HDNode.fromSeedBuffer(seed); const xpub = extendedKey.neutered().toBase58(); diff --git a/modules/core/src/v2/coins/eth.ts b/modules/core/src/v2/coins/eth.ts index 1e1db2f592..809891c929 100644 --- a/modules/core/src/v2/coins/eth.ts +++ b/modules/core/src/v2/coins/eth.ts @@ -4,7 +4,7 @@ import { BigNumber } from 'bignumber.js'; import * as utxoLib from 'bitgo-utxo-lib'; import * as Bluebird from 'bluebird'; -import * as crypto from 'crypto'; +import { randomBytes } from 'crypto'; import * as debugLib from 'debug'; import * as Keccak from 'keccak'; import * as _ from 'lodash'; @@ -1390,7 +1390,7 @@ export class Eth extends BaseCoin { // An extended private key has both a normal 256 bit private key and a 256 // bit chain code, both of which must be random. 512 bits is therefore the // maximum entropy and gives us maximum security against cracking. - seed = crypto.randomBytes(512 / 8); + seed = randomBytes(512 / 8); } const extendedKey = utxoLib.HDNode.fromSeedBuffer(seed); const xpub = extendedKey.neutered().toBase58(); diff --git a/modules/core/src/v2/coins/ofc.ts b/modules/core/src/v2/coins/ofc.ts index 36dc453e18..75d8cb2e95 100644 --- a/modules/core/src/v2/coins/ofc.ts +++ b/modules/core/src/v2/coins/ofc.ts @@ -15,7 +15,7 @@ import { import { BitGo } from '../../bitgo'; import { NodeCallback } from '../types'; import * as Bluebird from 'bluebird'; -import * as crypto from 'crypto'; +import { randomBytes } from 'crypto'; import * as bitGoUtxoLib from 'bitgo-utxo-lib'; export class Ofc extends BaseCoin { @@ -38,7 +38,7 @@ export class Ofc extends BaseCoin { // An extended private key has both a normal 256 bit private key and a 256 // bit chain code, both of which must be random. 512 bits is therefore the // maximum entropy and gives us maximum security against cracking. - seed = crypto.randomBytes(512 / 8); + seed = randomBytes(512 / 8); } const extendedKey = bitGoUtxoLib.HDNode.fromSeedBuffer(seed); const xpub = extendedKey.neutered().toBase58(); diff --git a/modules/core/src/v2/coins/trx.ts b/modules/core/src/v2/coins/trx.ts index bc72d65079..8df456af7f 100644 --- a/modules/core/src/v2/coins/trx.ts +++ b/modules/core/src/v2/coins/trx.ts @@ -2,7 +2,7 @@ * @prettier */ import * as Bluebird from 'bluebird'; -import * as crypto from 'crypto'; +import { randomBytes } from 'crypto'; import { CoinFamily, BaseCoin as StaticsBaseCoin } from '@bitgo/statics'; const co = Bluebird.coroutine; import * as bitgoAccountLib from '@bitgo/account-lib'; @@ -162,7 +162,7 @@ export class Trx extends BaseCoin { if (!seed) { // An extended private key has both a normal 256 bit private key and a 256 bit chain code, both of which must be // random. 512 bits is therefore the maximum entropy and gives us maximum security against cracking. - seed = crypto.randomBytes(512 / 8); + seed = randomBytes(512 / 8); } const hd = HDNode.fromSeedBuffer(seed); return { diff --git a/modules/core/src/v2/coins/xrp.ts b/modules/core/src/v2/coins/xrp.ts index 1175ab0f64..c9d28bdbf7 100644 --- a/modules/core/src/v2/coins/xrp.ts +++ b/modules/core/src/v2/coins/xrp.ts @@ -1,7 +1,7 @@ import { BigNumber } from 'bignumber.js'; import { HDNode, ECPair } from 'bitgo-utxo-lib'; import * as Bluebird from 'bluebird'; -import * as crypto from 'crypto'; +import { randomBytes } from 'crypto'; import * as _ from 'lodash'; import * as url from 'url'; import * as querystring from 'querystring'; @@ -624,7 +624,7 @@ export class Xrp extends BaseCoin { // An extended private key has both a normal 256 bit private key and a 256 // bit chain code, both of which must be random. 512 bits is therefore the // maximum entropy and gives us maximum security against cracking. - seed = crypto.randomBytes(512 / 8); + seed = randomBytes(512 / 8); } const extendedKey = HDNode.fromSeedBuffer(seed); const xpub = extendedKey.neutered().toBase58(); diff --git a/modules/core/src/v2/internal/util.ts b/modules/core/src/v2/internal/util.ts index a3b1445920..989d34aabc 100644 --- a/modules/core/src/v2/internal/util.ts +++ b/modules/core/src/v2/internal/util.ts @@ -8,7 +8,7 @@ import * as bitcoin from 'bitgo-utxo-lib'; import * as Big from 'big.js'; import * as _ from 'lodash'; -import * as crypto from 'crypto'; +import { randomBytes } from 'crypto'; import * as debugLib from 'debug'; import { EthereumLibraryUnavailableError } from '../../errors'; import { RequestTracer as IRequestTracer } from '../types'; @@ -37,7 +37,7 @@ export class RequestTracer implements IRequestTracer { private _seq = 0; private readonly _seed: Buffer; constructor() { - this._seed = crypto.randomBytes(10); + this._seed = randomBytes(10); } inc() { diff --git a/modules/core/test/v2/unit/coins/algo.ts b/modules/core/test/v2/unit/coins/algo.ts index 7d1b3bda32..0100f8b7e3 100644 --- a/modules/core/test/v2/unit/coins/algo.ts +++ b/modules/core/test/v2/unit/coins/algo.ts @@ -1,5 +1,5 @@ import * as Promise from 'bluebird'; -import * as crypto from 'crypto'; +import { randomBytes } from 'crypto'; import * as algosdk from 'algosdk'; import 'should'; @@ -30,7 +30,7 @@ describe('ALGO:', function() { }); it('should generate a keypair from seed', function() { - const seed = crypto.randomBytes(32); + const seed = randomBytes(32); const keyPair = basecoin.generateKeyPair(seed); keyPair.should.have.property('pub'); keyPair.should.have.property('prv'); diff --git a/modules/core/test/v2/unit/coins/xlm.ts b/modules/core/test/v2/unit/coins/xlm.ts index a204a0cb6b..97bfcc270a 100644 --- a/modules/core/test/v2/unit/coins/xlm.ts +++ b/modules/core/test/v2/unit/coins/xlm.ts @@ -1,5 +1,5 @@ import * as should from 'should'; -import * as crypto from 'crypto'; +import { randomBytes } from 'crypto'; import * as stellar from 'stellar-sdk'; import * as Bluebird from 'bluebird'; import { Environments } from '../../../../src'; @@ -714,7 +714,7 @@ describe('XLM:', function() { }); it('should generate a keypair from seed', function() { - const seed = crypto.randomBytes(32); + const seed = randomBytes(32); const keyPair = basecoin.generateKeyPair(seed); keyPair.should.have.property('pub'); keyPair.should.have.property('prv');