diff --git a/README.md b/README.md index 31d2386..8836592 100644 --- a/README.md +++ b/README.md @@ -114,11 +114,54 @@ You can run KMS on the following operating systems: We recommend that you run KMS from the [Docker image](https://hub.docker.com/repository/docker/tatumio/tatum-kms) regardless of the operating system used. -### Install KMS from npm +### Environment variables +Create file `.env` file with the following parameters and replace the placeholders with your values: ``` -npm i -g @tatumio/tatum-kms +# required +TATUM_API_KEY=XXXXX-YOUR-API-KEY +# one of the following setups is required: password, VGS, Azure, or AWS +# password setup +TATUM_KMS_PASSWORD=XXXXPASSWORD +# VGS setup +TATUM_KMS_VGS_USERNAME=XXXXUSERNAME +TATUM_KMS_VGS_PASSWORD=XXXXPASSWORDVGS +TATUM_KMS_VGS_ALIAS=XXXVSGALIAS +# Azure setup +TATUM_KMS_AZURE_SECRETVERSION=XXVERSION +TATUM_KMS_AZURE_SECRETNAME=XXSECRETNAME +TATUM_KMS_AZURE_VAULTURL=XXXXVAULTURL +# AWS setup +TATUM_KMS_AWS_REGION=us-east-1 +TATUM_KMS_AWS_SECRET_NAME=YOUR_KMS_SECRET_NAME +TATUM_KMS_AWS_ACCESS_KEY_ID=AKIAYWGKDBVRGMCASWIE +TATUM_KMS_AWS_SECRET_ACCESS_KEY=ZxDq62BZGyGe2CzwnVjL/IH8NnJG5Fu0isN7wev9 +TATUM_KMS_AWS_SECRET_KEY=pwd ``` +### Install KMS from npm +1. Install KMS globally: + ``` + npm i -g @tatumio/tatum-kms + ``` + or + ``` + yarn global add @tatumio/tatum-kms + ``` +1. Use ```.env``` file to configure Tatum KMS + 1. via ```--env-file=/path/to/.env``` + ``` + tatum-kms --env-file=/path/to/.env getaddress 11111111-1111-1111-1111-111111111111 0 + ``` + 1. via environment variables directly + ``` + TATUM_API_KEY=XXXXX-YOUR-API-KEY tatum-kms --help + ``` + 1. via predefined environment vars on global level + ``` + export TATUM_API_KEY=XXXXX-YOUR-API-KEY + tatum-kms --help + ``` + >**IMPORTANT!** NodeJS >=14 and npm@6 are required. KMS does **not** work on npm@7. ### Install KMS via Docker @@ -130,28 +173,7 @@ npm i -g @tatumio/tatum-kms ``` cd $HOME ``` -1. In the home directory, create a `.env` file with the following parameters and replace the placeholders with your values: - ``` - # required - TATUM_API_KEY=XXXXX-YOUR-API-KEY - # one of the following setups is required: password, VGS, Azure, or AWS - # password setup - TATUM_KMS_PASSWORD=XXXXPASSWORD - # VGS setup - TATUM_KMS_VGS_USERNAME=XXXXUSERNAME - TATUM_KMS_VGS_PASSWORD=XXXXPASSWORDVGS - TATUM_KMS_VGS_ALIAS=XXXVSGALIAS - # Azure setup - TATUM_KMS_AZURE_SECRETVERSION=XXVERSION - TATUM_KMS_AZURE_SECRETNAME=XXSECRETNAME - TATUM_KMS_AZURE_VAULTURL=XXXXVAULTURL - # AWS setup - TATUM_KMS_AWS_REGION=us-east-1 - TATUM_KMS_AWS_SECRET_NAME=YOUR_KMS_SECRET_NAME - TATUM_KMS_AWS_ACCESS_KEY_ID=AKIAYWGKDBVRGMCASWIE - TATUM_KMS_AWS_SECRET_ACCESS_KEY=ZxDq62BZGyGe2CzwnVjL/IH8NnJG5Fu0isN7wev9 - TATUM_KMS_AWS_SECRET_KEY=pwd - ``` +1. Use pre-created ```.env``` file to configure Tatum KMS via ```--env-file .env```: 1. Map the Docker volume to the local storage (your home folder). For more details, refer to the [Docker user documentation](https://docs.docker.com/storage/volumes/). @@ -349,7 +371,19 @@ When KMS runs in [daemon mode](#run-kms-in-daemon-mode), use the following comma "chain": "BTC" } } - ``` + ``` +* `checkconfig` shows environment variables for Tatum KMS (for debugging). + + ``` + bash:$ tatum-kms checkconfig + Version : 6.4.0 + Wallet file path : ~/.tatumrc/wallet.dat + Wallet exists : true + Wallet store type : LOCAL + Env file : .env + TATUM_API_KEY : d2eb5c****************************** + ... + ``` ## Common issues **Error message:** diff --git a/package.json b/package.json index 2ec5338..74012ca 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@tatumio/tatum-kms", - "version": "6.4.0", + "version": "6.4.1", "description": "Tatum KMS - Key Management System for Tatum-powered apps.", "main": "dist/index.js", "engines": { diff --git a/src/config.ts b/src/config.ts index 0ceaca9..79ac7f2 100644 --- a/src/config.ts +++ b/src/config.ts @@ -17,66 +17,74 @@ export enum ConfigOption { } export class Config { - private _configOptions = { + private static _configOptions = { [ConfigOption.KMS_PASSWORD]: { environmentKey: 'TATUM_KMS_PASSWORD', - question: 'Enter password to access wallet store:', + question: 'Enter password to access wallet store (or set env var TATUM_KMS_PASSWORD):', }, [ConfigOption.VGS_ALIAS]: { environmentKey: 'TATUM_KMS_VGS_ALIAS', - question: 'Enter alias to obtain from VGS Vault API:', + question: 'Enter alias to obtain from VGS Vault API (or set env var TATUM_KMS_VGS_ALIAS):', }, [ConfigOption.TATUM_API_KEY]: { - environmentKey: 'TATUM_KMS_TATUM_API_KEY', - question: 'Enter alias to obtain from VGS Vault API:', + environmentKey: 'TATUM_API_KEY', + question: 'Enter Tatum Api Key (or set env var TATUM_API_KEY):', }, [ConfigOption.VGS_USERNAME]: { environmentKey: 'TATUM_KMS_VGS_USERNAME', - question: 'Enter username to VGS Vault API:', + question: 'Enter username to VGS Vault API (or set env var TATUM_KMS_VGS_USERNAME):', }, [ConfigOption.VGS_PASSWORD]: { environmentKey: 'TATUM_KMS_VGS_PASSWORD', - question: 'Enter password to VGS Vault API:', + question: 'Enter password to VGS Vault API (or set env var TATUM_KMS_VGS_PASSWORD):', }, [ConfigOption.AZURE_SECRETVERSION]: { - environmentKey: 'TATUM_KMS_VGS_ALIAS', - question: 'Enter Secret version to obtain secret from Azure Vault API:', + environmentKey: 'TATUM_KMS_AZURE_SECRETVERSION', + question: + 'Enter Secret version to obtain secret from Azure Vault API (or set env var TATUM_KMS_AZURE_SECRETVERSION):', }, [ConfigOption.AZURE_SECRETNAME]: { environmentKey: 'TATUM_KMS_AZURE_SECRETNAME', - question: 'Enter Secret name to obtain from Azure Vault API:', + question: 'Enter Secret name to obtain from Azure Vault API (or set env var TATUM_KMS_AZURE_SECRETNAME):', }, [ConfigOption.AZURE_VAULTURL]: { environmentKey: 'TATUM_KMS_AZURE_VAULTURL', - question: 'Enter Vault Base URL to obtain secret from Azure Vault API:', + question: 'Enter Vault Base URL to obtain secret from Azure Vault API (or set env var TATUM_KMS_AZURE_VAULTURL):', }, [ConfigOption.AWS_REGION]: { environmentKey: 'TATUM_KMS_AWS_REGION', - question: 'Enter AWS Region to obtain secret from AWS Secrets Manager:', + question: 'Enter AWS Region to obtain secret from AWS Secrets Manager (or set env var TATUM_KMS_AWS_REGION):', }, [ConfigOption.AWS_ACCESS_KEY_ID]: { environmentKey: 'TATUM_KMS_AWS_ACCESS_KEY_ID', - question: 'Enter AWS Access key ID to obtain secret from AWS Secrets Manager:', + question: + 'Enter AWS Access key ID to obtain secret from AWS Secrets Manager (or set env var TATUM_KMS_AWS_ACCESS_KEY_ID):', }, [ConfigOption.AWS_SECRET_ACCESS_KEY]: { environmentKey: 'TATUM_KMS_AWS_SECRET_ACCESS_KEY', - question: 'Enter AWS Secret access key to obtain secret from AWS Secrets Manager:', + question: + 'Enter AWS Secret access key to obtain secret from AWS Secrets Manager (or set env var TATUM_KMS_AWS_SECRET_ACCESS_KEY):', }, [ConfigOption.AWS_SECRET_NAME]: { environmentKey: 'TATUM_KMS_AWS_SECRET_NAME', - question: 'Enter AWS Secret name to obtain secret from AWS Secrets Manager:', + question: + 'Enter AWS Secret name to obtain secret from AWS Secrets Manager (or set env var TATUM_KMS_AWS_SECRET_NAME):', }, [ConfigOption.AWS_SECRET_KEY]: { environmentKey: 'TATUM_KMS_AWS_SECRET_KEY', - question: 'Enter AWS Secret key from you stored secret to obtain password from AWS Secrets Manager:', + question: + 'Enter AWS Secret key from you stored secret to obtain password from AWS Secrets Manager (or set env var TATUM_KMS_AWS_SECRET_KEYa):', }, } - public getValue(what: ConfigOption): string { + public static getValue(what: ConfigOption): string { const config = this._configOptions[what] if (process.env[config.environmentKey]) { return process.env[config.environmentKey] as string } + if (what === ConfigOption.TATUM_API_KEY) { + throw new Error('Required TATUM_API_KEY is not set. Please set it as env variable or pass it as argument.') + } return question(config.question, { hideEchoBack: true, }) diff --git a/src/index.ts b/src/index.ts index 2ae4a20..7ca50e5 100644 --- a/src/index.ts +++ b/src/index.ts @@ -2,29 +2,29 @@ import { Currency, generateWallet } from '@tatumio/tatum' import axios from 'axios' import dotenv from 'dotenv' -import http from 'http' -import https from 'https' import meow from 'meow' -import { Config } from './config' import { PasswordType } from './interfaces' import { + checkConfig, exportWallets, generateManagedPrivateKeyBatch, getAddress, getPassword, getPrivateKey, getQuestion, - getTatumKey, getWallet, removeWallet, + setTatumKey, storePrivateKey, storeWallet, } from './management' import { processSignatures } from './signatures' import HttpAgent from 'agentkeepalive' +import { existsSync } from 'fs' +import * as process from 'process' +import { homedir } from 'os' dotenv.config() -const config = new Config() const axiosInstance = axios.create({ httpAgent: new HttpAgent({ @@ -41,7 +41,7 @@ const axiosInstance = axios.create({ }), }) -const { input: command, flags } = meow( +const { input: command, flags, help } = meow( ` Usage $ tatum-kms command @@ -65,8 +65,9 @@ const { input: command, flags } = meow( --path Custom path to wallet store file. --period Period in seconds to check for new transactions to sign, defaults to 5 seconds. Daemon mode only. --chain Blockchains to check, separated by comma. Daemon mode only. - --aws Using AWS Secrets Manager (https://aws.amazon.com/secrets-manager/) as a secure storage of the password which unlocks the wallet file. - --vgs Using VGS (https://verygoodsecurity.com) as a secure storage of the password which unlocks the wallet file. + --env-file Path to .env file to set vars. + --aws Using AWS Secrets Manager (https://aws.amazon.com/secrets-manager/) as a secure storage of the password which unlocks the wallet file. + --vgs Using VGS (https://verygoodsecurity.com) as a secure storage of the password which unlocks the wallet file. --azure Using Azure Vault (https://azure.microsoft.com/en-us/services/key-vault/) as a secure storage of the password which unlocks the wallet file. --externalUrl Pass in external url to check valid transaction. This parameter is mandatory for mainnet (if testnet is false). Daemon mode only. `, @@ -102,6 +103,9 @@ const { input: command, flags } = meow( type: 'string', isRequired: (f, input) => input[0] === 'daemon' && !f.testnet, }, + 'env-file': { + type: 'string', + }, }, }, ) @@ -120,13 +124,20 @@ const getPasswordType = (): PasswordType => { } const startup = async () => { + const envFilePath = (flags.envFile as string) ?? homedir() + '/.tatumrc/.env' + if (existsSync(envFilePath)) { + dotenv.config({ path: envFilePath }) + } + + setTatumKey(flags.apiKey as string) + if (command.length === 0) { + console.log(help) return } switch (command[0]) { case 'daemon': { const pwd = await getPassword(getPasswordType(), axiosInstance) - getTatumKey(flags.apiKey as string) await processSignatures( pwd, flags.testnet, @@ -191,6 +202,9 @@ const startup = async () => { case 'removewallet': await removeWallet(command[1], await getPassword(getPasswordType(), axiosInstance), flags.path) break + case 'checkconfig': + checkConfig(getPasswordType(), envFilePath, flags.path) + break default: console.error('Unsupported command. Use tatum-kms --help for details.') process.exit(-1) diff --git a/src/management.ts b/src/management.ts index cc01a00..c24e8ae 100644 --- a/src/management.ts +++ b/src/management.ts @@ -18,9 +18,6 @@ import { v4 as uuid } from 'uuid' import { Config, ConfigOption } from './config' import { PasswordType, Signature, StoreWalletValue, WalletsValidationOptions } from './interfaces' -const cardanoSDK = TatumCardanoSDK({ apiKey: process.env.TATUM_API_KEY as string }) - -const config = new Config() const ensurePathExists = (path: string) => { const dir = dirname(path) if (!existsSync(dir)) { @@ -30,6 +27,7 @@ const ensurePathExists = (path: string) => { const generatePrivateKey = async (mnemonic: string, currency: Currency, index: number, testnet: boolean) => { if (currency === Currency.ADA) { + const cardanoSDK = TatumCardanoSDK({ apiKey: Config.getValue(ConfigOption.TATUM_API_KEY) }) return cardanoSDK.wallet.generatePrivateKeyFromMnemonic(mnemonic, index) } else { return generatePrivateKeyFromMnemonic(currency, testnet, mnemonic, index) @@ -38,9 +36,9 @@ const generatePrivateKey = async (mnemonic: string, currency: Currency, index: n export const getPassword = async (pwdType: PasswordType, axiosInstance: AxiosInstance) => { if (pwdType === PasswordType.AZURE) { - const vaultUrl = config.getValue(ConfigOption.AZURE_VAULTURL) - const secretName = config.getValue(ConfigOption.AZURE_SECRETNAME) - const secretVersion = config.getValue(ConfigOption.AZURE_SECRETVERSION) + const vaultUrl = Config.getValue(ConfigOption.AZURE_VAULTURL) + const secretName = Config.getValue(ConfigOption.AZURE_SECRETNAME) + const secretVersion = Config.getValue(ConfigOption.AZURE_SECRETVERSION) const pwd = (await axiosInstance.get(`https://${vaultUrl}/secrets/${secretName}/${secretVersion}?api-version=7.1`)) .data?.data[0]?.value if (!pwd) { @@ -51,25 +49,25 @@ export const getPassword = async (pwdType: PasswordType, axiosInstance: AxiosIns return pwd } else if (pwdType === PasswordType.AWS) { const client = new SecretsManagerClient({ - region: config.getValue(ConfigOption.AWS_REGION), + region: Config.getValue(ConfigOption.AWS_REGION), credentials: { - accessKeyId: config.getValue(ConfigOption.AWS_ACCESS_KEY_ID), - secretAccessKey: config.getValue(ConfigOption.AWS_SECRET_ACCESS_KEY), + accessKeyId: Config.getValue(ConfigOption.AWS_ACCESS_KEY_ID), + secretAccessKey: Config.getValue(ConfigOption.AWS_SECRET_ACCESS_KEY), }, }) const result = await client.send( - new GetSecretValueCommand({ SecretId: config.getValue(ConfigOption.AWS_SECRET_NAME) }), + new GetSecretValueCommand({ SecretId: Config.getValue(ConfigOption.AWS_SECRET_NAME) }), ) if (!result.SecretString) { console.error('AWS secret does not exists.') process.exit(-1) return } - return JSON.parse(result.SecretString)[config.getValue(ConfigOption.AWS_SECRET_KEY)] + return JSON.parse(result.SecretString)[Config.getValue(ConfigOption.AWS_SECRET_KEY)] } else if (pwdType === PasswordType.VGS) { - const username = config.getValue(ConfigOption.VGS_USERNAME) - const password = config.getValue(ConfigOption.VGS_PASSWORD) - const alias = config.getValue(ConfigOption.VGS_ALIAS) + const username = Config.getValue(ConfigOption.VGS_USERNAME) + const password = Config.getValue(ConfigOption.VGS_PASSWORD) + const alias = Config.getValue(ConfigOption.VGS_ALIAS) const pwd = ( await axiosInstance.get(`https://api.live.verygoodvault.com/aliases/${alias}`, { auth: { @@ -85,7 +83,7 @@ export const getPassword = async (pwdType: PasswordType, axiosInstance: AxiosIns } return pwd } else { - return config.getValue(ConfigOption.KMS_PASSWORD) + return Config.getValue(ConfigOption.KMS_PASSWORD) } } @@ -140,6 +138,7 @@ const generatePureWallet = async (chain: Currency, testnet: boolean, mnemonic?: const sdk = TatumCeloSDK({ apiKey: '' }) wallet = sdk.wallet.generateWallet(mnemonic, { testnet }) } else if (chain === Currency.ADA) { + const cardanoSDK = TatumCardanoSDK({ apiKey: Config.getValue(ConfigOption.TATUM_API_KEY) }) wallet = await cardanoSDK.wallet.generateWallet(mnemonic) } else if (chain === Currency.TRON) { const sdk = TatumTronSDK({ apiKey: '' }) @@ -221,7 +220,7 @@ export const generateManagedPrivateKeyBatch = async ( pwd: string, path?: string, ) => { - config.getValue(ConfigOption.KMS_PASSWORD) + Config.getValue(ConfigOption.KMS_PASSWORD) const cnt = Number(count) for (let i = 0; i < cnt; i++) { const wallet = await generatePureWallet(chain, testnet) @@ -230,6 +229,7 @@ export const generateManagedPrivateKeyBatch = async ( address = wallet.address } else { if (chain === Currency.ADA) { + const cardanoSDK = TatumCardanoSDK({ apiKey: Config.getValue(ConfigOption.TATUM_API_KEY) }) address = await cardanoSDK.wallet.generateAddressFromXPub(wallet.xpub, 1, { testnet }) } else { address = await generateAddressFromXPub(chain, testnet, wallet.xpub, 1) @@ -246,7 +246,7 @@ export const getWalletFromPath = (errorMessage: string, path?: string, pwd?: str console.error('No path or password entered') return } - const password = pwd ?? config.getValue(ConfigOption.KMS_PASSWORD) + const password = pwd ?? Config.getValue(ConfigOption.KMS_PASSWORD) const pathToWallet = path || homedir() + '/.tatumrc/wallet.dat' if (!existsSync(pathToWallet)) { console.error(errorMessage) @@ -310,7 +310,7 @@ export const getWallet = async (id: string, pwd: string, path?: string, print = } export const getPrivateKey = async (id: string, index: string, path?: string, password?: string, print = true) => { - const pwd = password ?? config.getValue(ConfigOption.KMS_PASSWORD) + const pwd = password ?? Config.getValue(ConfigOption.KMS_PASSWORD) const pathToWallet = path || homedir() + '/.tatumrc/wallet.dat' if (!existsSync(pathToWallet)) { console.error(JSON.stringify({ error: `No such wallet for signatureId '${id}'.` }, null, 2)) @@ -338,7 +338,7 @@ export const getPrivateKey = async (id: string, index: string, path?: string, pa } export const getAddress = async (id: string, index: string, path?: string, pwd?: string, print = true) => { - const password = pwd ?? config.getValue(ConfigOption.KMS_PASSWORD) + const password = pwd ?? Config.getValue(ConfigOption.KMS_PASSWORD) const pathToWallet = path || homedir() + '/.tatumrc/wallet.dat' if (!existsSync(pathToWallet)) { console.error(JSON.stringify({ error: `No such wallet for signatureId '${id}'.` }, null, 2)) @@ -361,6 +361,7 @@ export const getAddress = async (id: string, index: string, path?: string, pwd?: } } else { if (wallet[id].chain === Currency.ADA) { + const cardanoSDK = TatumCardanoSDK({ apiKey: Config.getValue(ConfigOption.TATUM_API_KEY) }) pk = { address: await cardanoSDK.wallet.generateAddressFromXPub(wallet[id].xpub, parseInt(index), { testnet: wallet[id].testnet, @@ -394,11 +395,64 @@ export const removeWallet = async (id: string, pwd: string, path?: string) => { writeFileSync(pathToWallet, AES.encrypt(JSON.stringify(wallet), pwd).toString()) } -export const getTatumKey = (apiKey: string) => { +function parseWalletStoreName(pwdType: PasswordType): string { + if (pwdType === PasswordType.CMD_LINE) { + return 'LOCAL' + } else if (pwdType === PasswordType.VGS) { + return 'VGS' + } else if (pwdType === PasswordType.AZURE) { + return 'AZURE' + } else if (pwdType === PasswordType.AWS) { + return 'AWS' + } + return 'N/A' +} + +function hidePassword(password: string | undefined, showSymbols = 6): string { + if (!password) { + return '' + } + if (password.length <= showSymbols) { + return '*'.repeat(password.length) + } + return password.slice(0, showSymbols) + '*'.repeat(password.length - showSymbols) +} + +function secretValue(secretValue: string | undefined): string { + if (!secretValue) { + return 'N/A' + } + return hidePassword(secretValue) +} + +export const checkConfig = (pwdType: PasswordType, envFile?: string, path?: string) => { + const pathToWallet = path || homedir() + '/.tatumrc/wallet.dat' + console.log(`Version : ${process.env.npm_package_version ?? 'N/A'}`) + console.log(`Wallet file path : ${pathToWallet}`) + console.log(`Wallet exists : ${existsSync(pathToWallet)}`) + console.log(`Wallet store type : ${parseWalletStoreName(pwdType)}`) + console.log(`Environment vars file : ${envFile ?? 'N/A'}`) + console.log(`TATUM_API_KEY : ${secretValue(process.env.TATUM_API_KEY)}`) + console.log(`TATUM_KMS_PASSWORD : ${secretValue(process.env.TATUM_KMS_PASSWORD)}`) + console.log(`TATUM_KMS_VGS_ALIAS : ${secretValue(process.env.TATUM_KMS_VGS_ALIAS)}`) + console.log(`TATUM_KMS_VGS_USERNAME : ${secretValue(process.env.TATUM_KMS_VGS_USERNAME)}`) + console.log(`TATUM_KMS_VGS_PASSWORD : ${secretValue(process.env.TATUM_KMS_VGS_PASSWORD)}`) + console.log(`TATUM_KMS_AZURE_SECRETVERSION : ${secretValue(process.env.TATUM_KMS_AZURE_SECRETVERSION)}`) + console.log(`TATUM_KMS_AZURE_SECRETNAME : ${secretValue(process.env.TATUM_KMS_AZURE_SECRETNAME)}`) + console.log(`TATUM_KMS_AZURE_VAULTURL : ${secretValue(process.env.TATUM_KMS_AZURE_VAULTURL)}`) + console.log(`TATUM_KMS_AWS_REGION : ${process.env.TATUM_KMS_AWS_REGION ?? 'N/A'}`) + console.log(`TATUM_KMS_AWS_ACCESS_KEY_ID : ${secretValue(process.env.TATUM_KMS_AWS_ACCESS_KEY_ID)}`) + console.log(`TATUM_KMS_AWS_SECRET_ACCESS_KEY : ${secretValue(process.env.TATUM_KMS_AWS_SECRET_ACCESS_KEY)}`) + console.log(`TATUM_KMS_AWS_SECRET_NAME : ${secretValue(process.env.TATUM_KMS_AWS_SECRET_NAME)}`) + console.log(`TATUM_KMS_AWS_SECRET_KEY : ${secretValue(process.env.TATUM_KMS_AWS_SECRET_KEY)}`) +} + +export const setTatumKey = (apiKey: string) => { if (apiKey) { process.env.TATUM_API_KEY = apiKey } } + export const getQuestion = (q: string, e?: string) => { if (e) { return e diff --git a/src/signatures.ts b/src/signatures.ts index 2d95c4d..85ddadc 100644 --- a/src/signatures.ts +++ b/src/signatures.ts @@ -57,6 +57,7 @@ import { KMS_CONSTANTS } from './constants' import { Wallet } from './interfaces' import { getManagedWallets, getWallet, getWalletForSignature } from './management' import semver from 'semver' +import { Config, ConfigOption } from './config' const TATUM_URL: string = process.env.TATUM_API_URL || 'https://api.tatum.io' @@ -130,7 +131,7 @@ const processTransaction = async ( `${new Date().toISOString()} - Processing pending transaction - ${JSON.stringify(blockchainSignature, null, 2)}.`, ) - const apiKey = process.env.TATUM_API_KEY as string + const apiKey = Config.getValue(ConfigOption.TATUM_API_KEY) switch (blockchainSignature.chain) { case Currency.ALGO: { @@ -142,7 +143,7 @@ const processTransaction = async ( return } case Currency.SOL: { - const solSDK = TatumSolanaSDK({ apiKey: process.env.TATUM_API_KEY as string, url: TATUM_URL as any }) + const solSDK = TatumSolanaSDK({ apiKey, url: TATUM_URL as any }) txData = await solSDK.kms.sign( blockchainSignature as PendingTransaction, wallets.map(w => w.privateKey), @@ -193,14 +194,14 @@ const processTransaction = async ( return } case Currency.XRP: { - const xrpSdk = TatumXrpSDK({ apiKey: process.env.TATUM_API_KEY as string, url: TATUM_URL as any }) + const xrpSdk = TatumXrpSDK({ apiKey, url: TATUM_URL as any }) const xrpSecret = wallets[0].secret ? wallets[0].secret : wallets[0].privateKey txData = await xrpSdk.kms.sign(blockchainSignature as any, xrpSecret) await xrpSdk.blockchain.broadcast({ txData, signatureId: blockchainSignature.id }) return } case Currency.XLM: { - const xlmSdk = TatumXlmSDK({ apiKey: process.env.TATUM_API_KEY as string, url: TATUM_URL as any }) + const xlmSdk = TatumXlmSDK({ apiKey, url: TATUM_URL as any }) const xlmSecret = wallets[0].secret ? wallets[0].secret : wallets[0].privateKey txData = await xlmSdk.kms.sign(blockchainSignature as any, xlmSecret, testnet) await xlmSdk.blockchain.broadcast({ txData, signatureId: blockchainSignature.id }) @@ -279,7 +280,7 @@ const processTransaction = async ( ) : wallet.privateKey validatePrivateKeyWasFound(wallet, blockchainSignature, celoPrivateKey) - const celoSDK = TatumCeloSDK({ apiKey: process.env.TATUM_API_KEY as string, url: TATUM_URL as any }) + const celoSDK = TatumCeloSDK({ apiKey, url: TATUM_URL as any }) txData = await celoSDK.kms.sign(blockchainSignature as PendingTransaction, celoPrivateKey) await celoSDK.blockchain.broadcast({ txData, signatureId: blockchainSignature.id }) return @@ -387,7 +388,7 @@ const processTransaction = async ( ) : wallet.privateKey validatePrivateKeyWasFound(wallet, blockchainSignature, tronPrivateKey) - const tronSDK = TatumTronSDK({ apiKey: process.env.TATUM_API_KEY as string, url: TATUM_URL as any }) + const tronSDK = TatumTronSDK({ apiKey, url: TATUM_URL as any }) txData = await tronSDK.kms.sign(blockchainSignature as PendingTransaction, tronPrivateKey) await axios.post( `${TATUM_URL}/v3/tron/broadcast`, @@ -434,7 +435,7 @@ const processTransaction = async ( break } case Currency.ADA: { - const cardanoSDK = TatumCardanoSDK({ apiKey: process.env.TATUM_API_KEY as string, url: TATUM_URL as any }) + const cardanoSDK = TatumCardanoSDK({ apiKey, url: TATUM_URL as any }) if (blockchainSignature.withdrawalId) { const privateKeys = [] const w: { [walletId: string]: { mnemonic: string } } = {} @@ -545,7 +546,7 @@ const getPendingTransactions = async ( { signatureIds }, { headers: { - 'x-api-key': process.env.TATUM_API_KEY as string, + 'x-api-key': Config.getValue(ConfigOption.TATUM_API_KEY), 'x-ttm-kms-client-version': process.env.npm_package_version ?? '', }, }, @@ -625,7 +626,11 @@ export const processSignatures = async ( if (data.length > 0) { try { const url = `${TATUM_URL}/v3/tatum/kms/batch` - await axios.post(url, { errors: data }, { headers: { 'x-api-key': process.env.TATUM_API_KEY as string } }) + await axios.post( + url, + { errors: data }, + { headers: { 'x-api-key': Config.getValue(ConfigOption.TATUM_API_KEY) } }, + ) console.log(`${new Date().toISOString()} - Send batch call to url '${url}'.`) } catch (e) { console.error(