diff --git a/src/coinbase/coinbase.ts b/src/coinbase/coinbase.ts index 7f5f25a3..f5c085b2 100644 --- a/src/coinbase/coinbase.ts +++ b/src/coinbase/coinbase.ts @@ -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. @@ -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}`); } diff --git a/src/coinbase/tests/coinbase_test.ts b/src/coinbase/tests/coinbase_test.ts index d6f75010..c74d8078 100644 --- a/src/coinbase/tests/coinbase_test.ts +++ b/src/coinbase/tests/coinbase_test.ts @@ -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"; @@ -11,6 +13,7 @@ import { walletsApiMock, } from "./utils"; import { ethers } from "ethers"; +import path from "path"; const PATH_PREFIX = "./src/coinbase/tests/config"; @@ -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(