Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
93 changes: 18 additions & 75 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 4 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
"prepare": "npm run build && husky",
"prettier": "prettier --config .prettierrc 'src/**/*.ts' 'test/**/*.spec.ts' 'examples/**/*'",
"test": "npm run format:ci && npm run lint && npm run coverage",
"unit": "jest test/clientUnit.spec.ts test/Client/bitPayResponseUnit.spec.ts --runInBand",
"unit": "jest test/clientUnit.spec.ts test/Client/bitPayResponseUnit.spec.ts test/keyUtilsUnit.spec.ts test/setup/secretFileUnit.spec.ts --runInBand",
"functional": "jest test/clientFunctional.spec.ts --runInBand"
},
"author": "Antonio Buedo <integrations@bitpay.com>",
Expand All @@ -51,8 +51,9 @@
},
"homepage": "https://github.com/bitpay/nodejs-bitpay-client#readme",
"dependencies": {
"@noble/secp256k1": "1.7.2",
"bn.js": "4.12.5",
"bs58": "6.0.0",
"elliptic": "6.6.1",
"lodash": "4.18.1",
"zod": "4.5.1"
},
Expand All @@ -61,7 +62,7 @@
"@babel/preset-env": "7.29.7",
"@eslint/eslintrc": "3.3.7",
"@eslint/js": "10.0.1",
"@types/elliptic": "6.4.18",
"@types/bn.js": "5.2.0",
"@types/jest": "30.0.0",
"@types/lodash": "4.17.25",
"@types/node": "26.6.3",
Expand Down
4 changes: 2 additions & 2 deletions src/Client.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
/* eslint-disable @typescript-eslint/no-unused-vars*/

import { ec } from 'elliptic';
import {
BillClient,
BitPayClient,
Expand Down Expand Up @@ -41,6 +40,7 @@ import { PosToken } from './PosToken';
import { PrivateKey } from './PrivateKey';
import { TokenContainer } from './TokenContainer';
import { GuidGenerator } from './util/GuidGenerator';
import { KeyPair } from './util/KeyUtils';
import { ParamsRemover } from './util/ParamsRemover';

export class Client {
Expand Down Expand Up @@ -757,7 +757,7 @@ export class Client {
return environment.toUpperCase() == Env.Test ? Env.TestUrl : Env.ProdUrl;
}

private getIdentity(ecKey: ec.KeyPair) {
private getIdentity(ecKey: KeyPair) {
return this.keyUtils.getPublicKeyFromPrivateKey(ecKey);
}

Expand Down
7 changes: 3 additions & 4 deletions src/Client/BitPayClient.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,20 @@
import { Env, KeyUtils } from '../index';
import { ec } from 'elliptic';
import KeyPair = ec.KeyPair;
import { KeyPairLike } from '../util/KeyUtils';
import * as qs from 'querystring';
import BitPayException from '../Exceptions/BitPayException';
import { BitPayResponseParser } from '../util/BitPayResponseParser';
import { BitPayExceptionProvider } from '../Exceptions/BitPayExceptionProvider';
import { LoggerProvider } from '../Logger/LoggerProvider';

export class BitPayClient {
private readonly ecKey: KeyPair | null;
private readonly ecKey: KeyPairLike | null;
private readonly identity: string | null;
private readonly baseUrl: string;
private readonly defaultHeaders: Record<string, string>;
private readonly keyUtils: KeyUtils;
private readonly responseParser: BitPayResponseParser;

public constructor(baseUrl: string, ecKey: KeyPair | null, identity: string | null, platformInfo?: string) {
public constructor(baseUrl: string, ecKey: KeyPairLike | null, identity: string | null, platformInfo?: string) {
this.ecKey = ecKey;
this.baseUrl = baseUrl;
this.identity = identity;
Expand Down
6 changes: 5 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
* See the LICENSE file for more info.
*/

import { KeyUtils } from './util/KeyUtils';
import { KeyUtils, KeyPair, PublicKey, Signature } from './util/KeyUtils';
export type { KeyInput, KeyPairLike } from './util/KeyUtils';
import * as BitPayExceptions from './Exceptions/index';
import * as Models from './Model/index';
import { Client } from './Client';
Expand All @@ -32,6 +33,9 @@ import * as PayoutStatus from './Model/Payout/PayoutStatus';

export {
KeyUtils,
KeyPair,
PublicKey,
Signature,
BitPayExceptions,
Models,
Config,
Expand Down
21 changes: 9 additions & 12 deletions src/setup/BitPaySetup.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import { ec } from 'elliptic';
import * as fs from 'fs';
import * as readline from 'readline';
import * as BitPaySDK from '../index';
import { writeSecretFile } from './SecretFile';
import { KeyPair } from '../util/KeyUtils';

const keyUtils = new BitPaySDK.KeyUtils();
let configFilePath = process.cwd();
let keyPair: ec.KeyPair;
let ecKey: ec.KeyPair;
let keyPair: KeyPair;
let ecKey: KeyPair;
let environment: string;
// eslint-disable-next-line @typescript-eslint/no-unused-vars
let storeFile = true;
Expand Down Expand Up @@ -165,10 +166,8 @@ const storeKey = async () => {

console.log('Saving private key... \n');
sleep(500);
fs.writeFile(keyPath, ecKey.getPrivate('hex'), { mode: 0o755 }, function (err) {
if (err) throw err;
console.log('Private key saved in file: ' + keyPath + '\n');
});
writeSecretFile(keyPath, ecKey.getPrivate('hex'));
console.log('Private key saved in file: ' + keyPath + '\n');
await sleep(1000);

selectTokens();
Expand Down Expand Up @@ -323,11 +322,9 @@ const updateConfigFile = async () => {
}
};

fs.writeFile(configFilePath + '/BitPay.config.json', JSON.stringify(configurationObject, null, 4), function (err) {
if (err) throw err;
console.log('Generated configuration file');
console.log('And saved in file: ' + configFilePath + '/BitPay.config.json' + '\n');
});
writeSecretFile(configFilePath + '/BitPay.config.json', JSON.stringify(configurationObject, null, 4));
console.log('Generated configuration file');
console.log('And saved in file: ' + configFilePath + '/BitPay.config.json' + '\n');
await sleep(5000);

console.log('Configuration generated successfully! \n');
Expand Down
16 changes: 16 additions & 0 deletions src/setup/SecretFile.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import * as fs from 'fs';

/**
* Writes a file that holds secrets (private key, API tokens) so only the owner can read and write it.
*
* The mode given to writeFileSync only applies when the file is created. If the file already
* exists, for example from an older setup run, its permissions are tightened before and after
* writing. On Windows, chmod only controls the read-only flag, so this has no effect there.
*/
export function writeSecretFile(path: string, content: string): void {
if (fs.existsSync(path)) {
fs.chmodSync(path, 0o600);
}
fs.writeFileSync(path, content, { mode: 0o600 });
fs.chmodSync(path, 0o600);
}
Loading
Loading