Skip to content

Commit

Permalink
Support tilde expansion in apiKey file path (#45)
Browse files Browse the repository at this point in the history
  • Loading branch information
erdimaden authored May 31, 2024
1 parent 1b29d3a commit 067314e
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/coinbase/coinbase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import { InternalError, InvalidAPIKeyFormat, InvalidConfiguration } from "./erro
import { ApiClients } from "./types";
import { User } from "./user";
import { logApiResponse, registerAxiosInterceptors } from "./utils";
import * as os from "os";

/**
* The Coinbase SDK.
Expand Down Expand Up @@ -111,6 +112,7 @@ export class Coinbase {
debugging: boolean = false,
basePath: string = BASE_PATH,
): Coinbase {
filePath = filePath.startsWith("~") ? filePath.replace("~", os.homedir()) : filePath;
if (!fs.existsSync(filePath)) {
throw new InvalidConfiguration(`Invalid configuration: file not found at ${filePath}`);
}
Expand Down
14 changes: 14 additions & 0 deletions src/coinbase/tests/coinbase_test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import * as os from "os";
import * as fs from "fs";
import { randomUUID } from "crypto";
import { APIError } from "../api_error";
import { Coinbase } from "../coinbase";
Expand All @@ -11,6 +13,7 @@ import {
walletsApiMock,
} from "./utils";
import { ethers } from "ethers";
import path from "path";

const PATH_PREFIX = "./src/coinbase/tests/config";

Expand Down Expand Up @@ -43,6 +46,17 @@ describe("Coinbase tests", () => {
);
});

it("should expand the tilde to the home directory", () => {
const configuration = fs.readFileSync(`${PATH_PREFIX}/coinbase_cloud_api_key.json`, "utf8");
const homeDir = os.homedir();
const relativePath = "~/test_config.json";
const expandedPath = path.join(homeDir, "test_config.json");
fs.writeFileSync(expandedPath, configuration, "utf8");
const cbInstance = Coinbase.configureFromJson(relativePath);
expect(cbInstance).toBeInstanceOf(Coinbase);
fs.unlinkSync(expandedPath);
});

describe("should able to interact with the API", () => {
let user, walletId, publicKey, addressId, transactionHash;
const cbInstance = Coinbase.configureFromJson(
Expand Down

0 comments on commit 067314e

Please sign in to comment.