Skip to content

Commit

Permalink
Merge pull request #30 from provable-things/feat/add-prompt
Browse files Browse the repository at this point in the history
Ask the user to select the correct `.env` & `private-key.gpg` file
  • Loading branch information
gitmp01 authored Apr 13, 2022
2 parents 920661d + 539deea commit 5bd0fd7
Show file tree
Hide file tree
Showing 13 changed files with 3,598 additions and 1,755 deletions.
4 changes: 2 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
/build
**flattened**
/.openzeppelin
.env
*.env
/artifacts
/cache
*.gpg
!private-key.gpg
private-key*
2 changes: 0 additions & 2 deletions cli.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
#!/usr/bin/env node
/* eslint-disable max-len */

require('dotenv').config()
const { docopt } = require('docopt')
const { pegIn } = require('./lib/peg-in')
const { version } = require('./package.json')
Expand Down
1 change: 0 additions & 1 deletion hardhat.config.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
require('dotenv').config()
const {
has,
prop
Expand Down
4 changes: 3 additions & 1 deletion lib/deploy-vault.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@ const { checkEndpoint } = require('./check-endpoint')
const { deployContract } = require('./deploy-contract')
const { ENDPOINT_ENV_VAR_KEY } = require('./constants')
const { getEthersWallet } = require('./get-ethers-wallet')
const { getEnvConfiguration } = require('./get-env-configuration')
const { getVaultContractFactory } = require('./get-contract-factory')
const { getEnvironmentVariable } = require('./get-environment-variable')

const deployVault = _ =>
console.info('✔ Deploying vault logic contract...') ||
getEnvironmentVariable(ENDPOINT_ENV_VAR_KEY)
getEnvConfiguration()
.then(() => getEnvironmentVariable(ENDPOINT_ENV_VAR_KEY))
.then(getProvider)
.then(checkEndpoint)
.then(getEthersWallet)
Expand Down
10 changes: 10 additions & 0 deletions lib/get-env-configuration.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
const path = require('path')
const { cli, utils } = require('ptokens-utils')

const ENV_FILE_REGEXP = new RegExp('.*.env')

module.exports.getEnvConfiguration = () =>
utils.listFilesInFolder(path.resolve(__dirname, '..'))
.then(utils.applyRegExpToListOfStrings(ENV_FILE_REGEXP))
.then(cli.maybeAskUserToSelectOption('Please select the .env file', 'None file .env found!'))
.then(_choice => require('dotenv').config({ path: path.resolve(process.cwd(), _choice) }))
15 changes: 9 additions & 6 deletions lib/get-gpg-encrypted-private-key.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ const path = require('path')
const ethers = require('ethers')
const { existsSync } = require('fs')
const { exec } = require('child_process')
const { cli, utils } = require('ptokens-utils')

const stripNewLines = _s =>
_s.replace(/\r?\n|\r/g, '')
Expand All @@ -19,11 +20,6 @@ const checkIsHex = _hex =>
: Promise.reject(new Error(`${_hex} is NOT valid hex!`))
)

const PRIVATE_KEY_FILE_NAME = 'private-key.gpg'

const getPrivateKeyFilePath = _ =>
Promise.resolve(path.resolve(__dirname, `../${PRIVATE_KEY_FILE_NAME}`))

const executeCommand = _cmd =>
console.info('✔ Decrypting private key...') ||
new Promise((resolve, reject) =>
Expand Down Expand Up @@ -51,9 +47,16 @@ const checkPrivateKey = _privateKey =>
: Promise.reject(new Error(`ETH private keys should be ${ETH_KEY_NUM_CHARS - 2} hex chars long!`))
})

const PRIVATE_KEY_REGEXP = new RegExp('private-key.*.gpg')

const maybeGetPrivateKeyFiles = () =>
utils.listFilesInFolder(path.resolve(__dirname, '..'))
.then(utils.applyRegExpToListOfStrings(PRIVATE_KEY_REGEXP))
.then(cli.maybeAskUserToSelectOption('Select the private key to use:', 'None file private-key*.gpg found!'))

const getGpgEncryptedPrivateKey = _ =>
console.info('✔ Getting GPG encrypted private key..') ||
getPrivateKeyFilePath()
maybeGetPrivateKeyFiles()
.then(checkPrivateKeyFileExists)
.then(getDecryptionCommand)
.then(executeCommand)
Expand Down
4 changes: 3 additions & 1 deletion lib/get-vault-contract.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const { checkEndpoint } = require('./check-endpoint')
const { getAbi } = require('./get-contract-artifacts')
const { ENDPOINT_ENV_VAR_KEY } = require('./constants')
const { getEthersWallet } = require('./get-ethers-wallet')
const { getEnvConfiguration } = require('./get-env-configuration')
const { getEnvironmentVariable } = require('./get-environment-variable')

const getEthersContract = curry((_address, _abi, _signer) => {
Expand All @@ -15,7 +16,8 @@ const getEthersContract = curry((_address, _abi, _signer) => {

const getVaultContract = _deployedContractAddress =>
console.info(`✔ Getting pToken contract @ '${_deployedContractAddress}'...`) ||
getEnvironmentVariable(ENDPOINT_ENV_VAR_KEY)
getEnvConfiguration()
.then(() => getEnvironmentVariable(ENDPOINT_ENV_VAR_KEY))
.then(getProvider)
.then(checkEndpoint)
.then(_endpoint => Promise.all([ getEthersWallet(_endpoint), getAbi() ]))
Expand Down
4 changes: 3 additions & 1 deletion lib/show-suggested-fees.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const { prop } = require('ramda')
const { getProvider } = require('./get-provider')
const { checkEndpoint } = require('./check-endpoint')
const { ENDPOINT_ENV_VAR_KEY } = require('./constants')
const { getEnvConfiguration } = require('./get-env-configuration')
const { getEnvironmentVariable } = require('./get-environment-variable')

const getSuggestedFees = _provider =>
Expand Down Expand Up @@ -34,7 +35,8 @@ const parseSuggestedFees = _suggestedFees =>
: parseSuggestedFeesWithEIP1559Enabled(_suggestedFees)

const showSuggestedFees = _ =>
getEnvironmentVariable(ENDPOINT_ENV_VAR_KEY)
getEnvConfiguration()
.then(() => getEnvironmentVariable(ENDPOINT_ENV_VAR_KEY))
.then(getProvider)
.then(checkEndpoint)
.then(getSuggestedFees)
Expand Down
4 changes: 3 additions & 1 deletion lib/show-wallet-details.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ const { getProvider } = require('./get-provider')
const { checkEndpoint } = require('./check-endpoint')
const { ENDPOINT_ENV_VAR_KEY } = require('./constants')
const { getEthersWallet } = require('./get-ethers-wallet')
const { getEnvConfiguration } = require('./get-env-configuration')
const { getEnvironmentVariable } = require('./get-environment-variable')

const getWalletAddress = _wallet =>
Expand All @@ -25,7 +26,8 @@ const getWalletBalance = _wallet =>

const showWalletDetails = _ =>
console.info('✔ Showing wallet details...') ||
getEnvironmentVariable(ENDPOINT_ENV_VAR_KEY)
getEnvConfiguration()
.then(() => getEnvironmentVariable(ENDPOINT_ENV_VAR_KEY))
.then(getProvider)
.then(checkEndpoint)
.then(getEthersWallet)
Expand Down
4 changes: 3 additions & 1 deletion lib/verify-vault.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ const { readFileSync } = require('fs')
const { exec } = require('child_process')
const { checkEthAddress } = require('./utils')
const { ETHERSCAN_ENV_VAR_KEY } = require('./constants')
const { getEnvConfiguration } = require('./get-env-configuration')
const { checkEnvironmentVariableExists } = require('./get-environment-variable')

const HARDHAT_CONFIG_FILE_NAME = 'hardhat.config.js'
Expand Down Expand Up @@ -38,7 +39,8 @@ const checkNetwork = _network =>

const verifyVault = (_network, _address) =>
console.info('✔ Verifying vault contract...') ||
checkEthAddress(_address)
getEnvConfiguration()
.then(_ => checkEthAddress(_address))
.then(_ => checkNetwork(_network))
.then(_ => checkEnvironmentVariableExists(ETHERSCAN_ENV_VAR_KEY))
.then(_ => getVerificationCommand(_address, _network))
Expand Down
Loading

0 comments on commit 5bd0fd7

Please sign in to comment.