Skip to content

Commit

Permalink
Merge pull request #3698 from BitGo/BTC-0.prettier-bitgo
Browse files Browse the repository at this point in the history
chore(bitgo): add .prettierrc.yml
  • Loading branch information
OttoAllmendinger authored Jun 28, 2023
2 parents 18f1c89 + e37ffb3 commit b2070b4
Show file tree
Hide file tree
Showing 87 changed files with 7,642 additions and 5,280 deletions.
3 changes: 2 additions & 1 deletion modules/bitgo/.prettierignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
*.json
*.yml
CHANGELOG.md
CHANGELOG.md
dist/
3 changes: 3 additions & 0 deletions modules/bitgo/.prettierrc.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
printWidth: 120
singleQuote: true
trailingComma: 'es5'
36 changes: 23 additions & 13 deletions modules/bitgo/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ Please email us at [email protected] if you have questions or comments about thi

[![Known Vulnerabilities](https://snyk.io/test/github/BitGo/BitGoJS/badge.svg)](https://snyk.io/test/github/BitGo/BitGoJS)
[![BitGo SDK](https://github.com/BitGo/BitGoJS/actions/workflows/ci.yml/badge.svg)](https://github.com/BitGo/BitGoJS/actions/workflows/ci.yml)

# Installation

Please make sure you are running at least Node version 8 (the latest LTS release is recommended) and NPM version 6.
Expand All @@ -29,7 +30,9 @@ You can find the complete release notes (since version 4.44.0) [here](https://gi
# Example Usage

## Initialize SDK

Create an access token by logging into your bitgo account, going to the API access tab [in the settings area](https://www.bitgo.com/settings) and making a developer token.

```js
const BitGo = require('bitgo');
const bitgo = new BitGo.BitGo({ accessToken: ACCESS_TOKEN }); // defaults to testnet. add env: 'prod' if you want to go against mainnet
Expand All @@ -38,37 +41,42 @@ console.dir(result);
```

## Create Wallet

```js
const params = {
"passphrase": "replaceme",
"label": "firstwallet"
passphrase: 'replaceme',
label: 'firstwallet',
};
const { wallet } = await bitgo.coin('tbtc').wallets().generateWallet(params);
console.dir(wallet);
```

## Create new address

```js
const address = await wallet.createAddress();
console.dir(address);
```

## View wallet transfers

```js
const transfers = await wallet.transfers();
```

## Send coins

```js
const result = await wallet.sendCoins({
address: "2NEe9QhKPB2gnQLB3hffMuDcoFKZFjHYJYx",
address: '2NEe9QhKPB2gnQLB3hffMuDcoFKZFjHYJYx',
amount: 0.01 * 1e8,
walletPassphrase: "replaceme"
walletPassphrase: 'replaceme',
});
console.dir(result);
```

## More examples

Further demos and examples in both JavaScript and TypeScript can be found in the [example](example) directory.

# Enabling additional debugging output
Expand All @@ -78,21 +86,23 @@ Further demos and examples in both JavaScript and TypeScript can be found in the
When using the `bitgo` npm package, the easiest way to enable debug output is by setting the `DEBUG` environment variable to one or more of the debug namespaces in the table below. Multiple debug namespaces can be enabled by giving a comma-separated list or by using `*` as a wildcard. See the [debug package documentation](https://github.com/visionmedia/debug#readme) for more details.

## Available Debug Namespaces
| Namespace | Description |
| --- | --- |
| `bitgo:index` | Core BitGo object. Currently only constant fetch failures and HMAC response failures will emit debug information for this namespace. |
| `bitgo:v1:txb` | Version 1 (legacy) transaction builder |
| `bitgo:v2:pendingapprovals` | Pending approval operations. Currently only wallet fetch errors will emit debug information for this namespace. |
| `bitgo:v2:wallet` | Wallet operations including transaction prebuild, sendMany calls and consolidation transactions |
| `bitgo:v2:utxo` | Low level operations for UTXO coins, including transaction parsing, verification, signing and explanations |
| `bitgo:v2:eth` | Ethereum specific output. Currently only failures to require the optional Ethereum libraries are reported |
| `bitgo:v2:util` | SDK utilities specific output. Currently only failures to require the optional Ethereum libraries are reported |

| Namespace | Description |
| --------------------------- | ------------------------------------------------------------------------------------------------------------------------------------ |
| `bitgo:index` | Core BitGo object. Currently only constant fetch failures and HMAC response failures will emit debug information for this namespace. |
| `bitgo:v1:txb` | Version 1 (legacy) transaction builder |
| `bitgo:v2:pendingapprovals` | Pending approval operations. Currently only wallet fetch errors will emit debug information for this namespace. |
| `bitgo:v2:wallet` | Wallet operations including transaction prebuild, sendMany calls and consolidation transactions |
| `bitgo:v2:utxo` | Low level operations for UTXO coins, including transaction parsing, verification, signing and explanations |
| `bitgo:v2:eth` | Ethereum specific output. Currently only failures to require the optional Ethereum libraries are reported |
| `bitgo:v2:util` | SDK utilities specific output. Currently only failures to require the optional Ethereum libraries are reported |

Another debug namespace which is not provided by BitGoJS but is helpful nonetheless is the `superagent` namespace, which will output all HTTP requests and responses (only the URL, not bodies).

## Example

To run an SDK script with debug output enabled, export the DEBUG environment variable before running.

```shell script
export DEBUG='bitgo:*' # enable all bitgo debug namespaces
node myScript.js
Expand Down
27 changes: 3 additions & 24 deletions modules/bitgo/karma.conf.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,13 @@ if (!process.env.CHROME_BIN) {

module.exports = function (config) {
config.set({

// base path that will be used to resolve all patterns (eg. files, exclude)
basePath: '',


// frameworks to use
// available frameworks: https://npmjs.org/browse/keyword/karma-adapter
frameworks: ['jasmine', 'karma-typescript'],


// list of files / patterns to load in the browser
files: [
'dist/browser/BitGoJS.min.js',
Expand All @@ -24,16 +21,9 @@ module.exports = function (config) {
{ pattern: 'dist/browser/*.wasm', included: false, served: true, watched: false, nocache: true },
],

plugins: [
require('karma-jasmine'),
require('karma-typescript'),
require('karma-chrome-launcher'),
],
plugins: [require('karma-jasmine'), require('karma-typescript'), require('karma-chrome-launcher')],
// list of files / patterns to exclude
exclude: [
'node_modules',
],

exclude: ['node_modules'],

// preprocess matching files before serving them to the browser
// available preprocessors: https://npmjs.org/browse/keyword/karma-preprocessor
Expand All @@ -47,13 +37,7 @@ module.exports = function (config) {
compilerOptions: {
sourceMap: true,
target: 'es6',
lib: [
'dom',
'es6',
'es5',
'es2017',
'es2018',
],
lib: ['dom', 'es6', 'es5', 'es2017', 'es2018'],
types: ['jasmine'],
},
include: ['test/browser/**/*.ts'],
Expand All @@ -64,24 +48,19 @@ module.exports = function (config) {
// available reporters: https://npmjs.org/browse/keyword/karma-reporter
reporters: ['progress'],


// web server port
port: 9876,


// enable / disable colors in the output (reporters and logs)
colors: true,


// level of logging
// possible values: config.LOG_DISABLE || config.LOG_ERROR || config.LOG_WARN || config.LOG_INFO || config.LOG_DEBUG
logLevel: config.LOG_INFO,


// enable / disable watching file and executing tests whenever any file changes
autoWatch: true,


// start these browsers
browsers: ['HeadlessChromeNoSandbox'],
customLaunchers: {
Expand Down
8 changes: 2 additions & 6 deletions modules/bitgo/scripts/upload-docs.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,7 @@ const S3 = require('aws-sdk/clients/s3');
const { version } = require('../package.json');
const { promisify } = require('util');

const {
reports_s3_akid,
reports_s3_sak,
} = process.env;
const { reports_s3_akid, reports_s3_sak } = process.env;

const s3 = new S3({
accessKeyId: reports_s3_akid,
Expand Down Expand Up @@ -92,6 +89,5 @@ async function uploadDocs(root, key) {
if (!fs.existsSync(DOCS_ROOT) || !fs.statSync(DOCS_ROOT).isDirectory()) {
console.warn(`Docs directory '${DOCS_ROOT}' not found. Skipping docs upload...`);
} else {
uploadDocs(DOCS_ROOT, OBJECT_ROOT)
.catch((e) => console.error('fatal', e, e.stack));
uploadDocs(DOCS_ROOT, OBJECT_ROOT).catch((e) => console.error('fatal', e, e.stack));
}
8 changes: 5 additions & 3 deletions modules/bitgo/scripts/upload-tools/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@
`bitgo/upload-tools` is a `node:10`-derived docker image which has a few npm packages pre-installed to reduce install times during builds.

Currently, the following binaries are installed via `yarn global add`:
* `codecov`
* `typedoc`

- `codecov`
- `typedoc`

The following libraries are installed via `yarn add`:
* `aws-sdk`

- `aws-sdk`
27 changes: 20 additions & 7 deletions modules/bitgo/src/bitgo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,7 @@ import * as _ from 'lodash';

import GlobalCoinFactory from './v2/coinFactory';
import { BaseCoin, common } from '@bitgo/sdk-core';
import {
BitGoAPI,
BitGoAPIOptions,
} from '@bitgo/sdk-api';
import { BitGoAPI, BitGoAPIOptions } from '@bitgo/sdk-api';

export type BitGoOptions = BitGoAPIOptions;

Expand All @@ -21,12 +18,28 @@ export class BitGo extends BitGoAPI {
*/
constructor(params: BitGoAPIOptions = {}) {
super(params);
if (!common.validateParams(params, [], ['clientId', 'clientSecret', 'refreshToken', 'accessToken', 'userAgent', 'customRootURI', 'customBitcoinNetwork', 'serverXpub', 'stellarFederationServerUrl']) ||
(params.useProduction && !_.isBoolean(params.useProduction))) {
if (
!common.validateParams(
params,
[],
[
'clientId',
'clientSecret',
'refreshToken',
'accessToken',
'userAgent',
'customRootURI',
'customBitcoinNetwork',
'serverXpub',
'stellarFederationServerUrl',
]
) ||
(params.useProduction && !_.isBoolean(params.useProduction))
) {
throw new Error('invalid argument');
}

if ((!params.clientId) !== (!params.clientSecret)) {
if (!params.clientId !== !params.clientSecret) {
throw new Error('invalid argument - must provide both client id and secret');
}

Expand Down
1 change: 0 additions & 1 deletion modules/bitgo/src/config.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { ethGasConfigs, mainnetTokens, testnetTokens, Tokens, tokens } from '@bitgo/statics';
import { Config, defaults, defaultConstants } from '@bitgo/sdk-core';
export { Config, defaultConstants, defaults, ethGasConfigs, mainnetTokens, testnetTokens, tokens, Tokens };

1 change: 0 additions & 1 deletion modules/bitgo/src/v2/coins/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,4 +82,3 @@ export { Tzec, Zec };
import { coins } from '@bitgo/sdk-core';
const { Ofc, OfcToken, Susd, FiatUsd, FiatEur, FiatGBP, Tsusd, TfiatUsd, TfiatEur, TfiatGBP } = coins;
export { FiatEur, FiatGBP, FiatUsd, Ofc, OfcToken, Susd, TfiatEur, TfiatGBP, TfiatUsd, Tsusd };

2 changes: 0 additions & 2 deletions modules/bitgo/src/v2/internal/seedValidator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import { Hbar, Algo } from '@bitgo/account-lib';
* This classes intention is to guess/verify what seeds come from where.
*/
export class SeedValidator {

/**
* Try to guess what kind of seed this could be
* @param seed
Expand Down Expand Up @@ -66,5 +65,4 @@ export class SeedValidator {
}
return true;
}

}
84 changes: 43 additions & 41 deletions modules/bitgo/test/browser/browser.spec.ts
Original file line number Diff line number Diff line change
@@ -1,49 +1,51 @@
describe('BitGoJS in the browser', () => {
it('Should work', () => {
const BitGoJS = window['BitGoJS'];
expect(BitGoJS).toBeTruthy();
});
it('Should work', () => {
const BitGoJS = window['BitGoJS'];
expect(BitGoJS).toBeTruthy();
});
});

describe('Coins', () => {
it('Should work for all coins', () => {
const BitGoJS = window['BitGoJS'];
const bitgo = new BitGoJS.BitGo({ env: 'test' });
// these objects are defined in BitGoJS.Coin, but are not coins in the traditional sense
const excludedKeys = {
'AbstractUtxoCoin': 1,
'Erc20Token': 1,
'OfcToken': 1,
'StellarToken': 1,
'CeloToken': 1,
'EosToken': 1,
'AlgoToken': 1,
'AvaxCToken': 1,
'PolygonToken': 1,
'BscToken': 1,
};
Object.keys(BitGoJS.Coin)
.filter((coinName) => !excludedKeys[coinName])
.forEach((coinName) => {
const coinIdentifier = coinName.toLowerCase();
const coin = bitgo.coin(coinIdentifier);
expect(coin).toBeTruthy();
expect(coin.type).toEqual(coinIdentifier);
})
})
it('Should work for all coins', () => {
const BitGoJS = window['BitGoJS'];
const bitgo = new BitGoJS.BitGo({ env: 'test' });
// these objects are defined in BitGoJS.Coin, but are not coins in the traditional sense
const excludedKeys = {
AbstractUtxoCoin: 1,
Erc20Token: 1,
OfcToken: 1,
StellarToken: 1,
CeloToken: 1,
EosToken: 1,
AlgoToken: 1,
AvaxCToken: 1,
PolygonToken: 1,
BscToken: 1,
};
Object.keys(BitGoJS.Coin)
.filter((coinName) => !excludedKeys[coinName])
.forEach((coinName) => {
const coinIdentifier = coinName.toLowerCase();
const coin = bitgo.coin(coinIdentifier);
expect(coin).toBeTruthy();
expect(coin.type).toEqual(coinIdentifier);
});
});

it('UTXO bufferutils should work', () => {
const BitGoJS = window['BitGoJS'];
const bitgo = new BitGoJS.BitGo({ env: 'test' });
it('UTXO bufferutils should work', () => {
const BitGoJS = window['BitGoJS'];
const bitgo = new BitGoJS.BitGo({ env: 'test' });

const txHex = '0100000002008287fa5a4e9d393134b525ae038cbcb4c757eadaa378c33caeed294c63137f000000006b48304502204dc8131adb9420729ff1580bbbcf01f5ef879defee5225a8261b5681075b8a120221009dc3386f5301ab4a88dfd89d8927a7807242a30ee252fa864d61d0b079aaa2c20121038c4b3c81572d84ce32a2a41c5bb54d4c408b5ce3df9be451f4f57ba8bd8ebf59ffffffff17ed0cc32384bf9c410d023d4ab25f4499992824308e89c448fd570e1060fc0e000000006b48304502202c0ff069c0783c11259936307ef906b211542a01ca33cf6993ddb7b8d55b42ac02210095c4bceb1886f5bcc6ca2dbb909259c0509e768693a93fe2d01d511a57356f25012102e99ed9483d91f1fa67abd838f21afd80bf6a3732128ab5aad0ee5b975679c13dffffffff02bca2b100000000001976a914cc3aa0deca267914cbcf96f79ccd1b679d85e20188ac08c2eb0b000000001976a914380c5a7247e945a5aa242056f9b046a9366fe21788ac00000000';
const btc = bitgo.coin('btc');
const tx = btc.createTransactionFromHex(txHex);
expect(tx.getId()).toEqual('4f666850ac8a54c834a90e62fc9dc50b3c99275dd1f91960e1ea89813970e444');
const txHex =
'0100000002008287fa5a4e9d393134b525ae038cbcb4c757eadaa378c33caeed294c63137f000000006b48304502204dc8131adb9420729ff1580bbbcf01f5ef879defee5225a8261b5681075b8a120221009dc3386f5301ab4a88dfd89d8927a7807242a30ee252fa864d61d0b079aaa2c20121038c4b3c81572d84ce32a2a41c5bb54d4c408b5ce3df9be451f4f57ba8bd8ebf59ffffffff17ed0cc32384bf9c410d023d4ab25f4499992824308e89c448fd570e1060fc0e000000006b48304502202c0ff069c0783c11259936307ef906b211542a01ca33cf6993ddb7b8d55b42ac02210095c4bceb1886f5bcc6ca2dbb909259c0509e768693a93fe2d01d511a57356f25012102e99ed9483d91f1fa67abd838f21afd80bf6a3732128ab5aad0ee5b975679c13dffffffff02bca2b100000000001976a914cc3aa0deca267914cbcf96f79ccd1b679d85e20188ac08c2eb0b000000001976a914380c5a7247e945a5aa242056f9b046a9366fe21788ac00000000';
const btc = bitgo.coin('btc');
const tx = btc.createTransactionFromHex(txHex);
expect(tx.getId()).toEqual('4f666850ac8a54c834a90e62fc9dc50b3c99275dd1f91960e1ea89813970e444');

const txHexBig = '0100000002008287fa5a4e9d393134b525ae038cbcb4c757eadaa378c33caeed294c63137f000000006b48304502204dc8131adb9420729ff1580bbbcf01f5ef879defee5225a8261b5681075b8a120221009dc3386f5301ab4a88dfd89d8927a7807242a30ee252fa864d61d0b079aaa2c20121038c4b3c81572d84ce32a2a41c5bb54d4c408b5ce3df9be451f4f57ba8bd8ebf59ffffffff17ed0cc32384bf9c410d023d4ab25f4499992824308e89c448fd570e1060fc0e000000006b48304502202c0ff069c0783c11259936307ef906b211542a01ca33cf6993ddb7b8d55b42ac02210095c4bceb1886f5bcc6ca2dbb909259c0509e768693a93fe2d01d511a57356f25012102e99ed9483d91f1fa67abd838f21afd80bf6a3732128ab5aad0ee5b975679c13dffffffff02f8ffc42ebca2b1001976a914cc3aa0deca267914cbcf96f79ccd1b679d85e20188ac08c2eb0b000000001976a914380c5a7247e945a5aa242056f9b046a9366fe21788ac00000000';
const doge = bitgo.coin('doge');
const txBig = doge.createTransactionFromHex(txHexBig);
expect(txBig.outs[0].value.toString()).toEqual('49999999999999992');
})
const txHexBig =
'0100000002008287fa5a4e9d393134b525ae038cbcb4c757eadaa378c33caeed294c63137f000000006b48304502204dc8131adb9420729ff1580bbbcf01f5ef879defee5225a8261b5681075b8a120221009dc3386f5301ab4a88dfd89d8927a7807242a30ee252fa864d61d0b079aaa2c20121038c4b3c81572d84ce32a2a41c5bb54d4c408b5ce3df9be451f4f57ba8bd8ebf59ffffffff17ed0cc32384bf9c410d023d4ab25f4499992824308e89c448fd570e1060fc0e000000006b48304502202c0ff069c0783c11259936307ef906b211542a01ca33cf6993ddb7b8d55b42ac02210095c4bceb1886f5bcc6ca2dbb909259c0509e768693a93fe2d01d511a57356f25012102e99ed9483d91f1fa67abd838f21afd80bf6a3732128ab5aad0ee5b975679c13dffffffff02f8ffc42ebca2b1001976a914cc3aa0deca267914cbcf96f79ccd1b679d85e20188ac08c2eb0b000000001976a914380c5a7247e945a5aa242056f9b046a9366fe21788ac00000000';
const doge = bitgo.coin('doge');
const txBig = doge.createTransactionFromHex(txHexBig);
expect(txBig.outs[0].value.toString()).toEqual('49999999999999992');
});
});
10 changes: 5 additions & 5 deletions modules/bitgo/test/browser/karmaHelper.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
// intercept & mock all the redudant calls to fetch constants by the browser
(function(global) {
const constants = {};
BitGoJS.BitGo.prototype.getConstants = (params) => constants;
BitGoJS.BitGo.prototype.fetchConstants = (params, callback) =>
(function (global) {
const constants = {};
BitGoJS.BitGo.prototype.getConstants = (params) => constants;
BitGoJS.BitGo.prototype.fetchConstants = (params, callback) =>
new Promise((resolve) => {
resolve({
...constants,
});
});
})(window)
})(window);
Loading

0 comments on commit b2070b4

Please sign in to comment.