Skip to content

Commit

Permalink
Merge pull request #47 from provable-things/feat/add-priv-key-selecti…
Browse files Browse the repository at this point in the history
…on-prompt

Ask the user to select the correct `.env` & `private-key.gpg` files
  • Loading branch information
gitmp01 authored Apr 13, 2022
2 parents c65aaae + d624598 commit e6da0b5
Show file tree
Hide file tree
Showing 20 changed files with 1,540 additions and 114 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,4 @@ cache/
artifacts/
*flattened*
*.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 {
grantMinterRole,
revokeMinterRole,
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,
assoc,
Expand Down
4 changes: 3 additions & 1 deletion lib/check-erc1820-registry-exists.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
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 ERC1820_REGISTRY_ADDRESS = '0x1820a4B7618BdE71Dce8cdc73aAB6C95905faD24'
Expand All @@ -13,7 +14,8 @@ const byteCodeExists = _byteCode =>

const checkErc1820RegistryExists = _ =>
console.info('✔ Checking if the ERC1820 registry exists...') ||
getEnvironmentVariable(ENDPOINT_ENV_VAR_KEY)
getEnvConfiguration()
.then(() => getEnvironmentVariable(ENDPOINT_ENV_VAR_KEY))
.then(getProvider)
.then(checkEndpoint)
.then(getCodeAtRegistryAddress)
Expand Down
4 changes: 3 additions & 1 deletion lib/deploy-contract.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@ 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 { deployEthersContract } = require('./deploy-ethers-contract')
const { getEnvironmentVariable } = require('./get-environment-variable')
const { getContractFactoryMaybeWithGSN } = require('./get-contract-factory')

const deployContract = _withGSN =>
console.info('✔ Deploying logic contract...') ||
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/deploy-weth.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 { deployEthersContract } = require('./deploy-ethers-contract')
const { getEnvironmentVariable } = require('./get-environment-variable')

Expand All @@ -32,7 +33,8 @@ const getWethContractFactory = _wallet =>

const deployWeth = _ =>
console.info('✔ Deploying wETH 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-ptoken-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 getPTokenContract = _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/get-transaction-count.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ const { curry } = 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 getTransactionCountOfAddress = curry((_address, _provider) =>
Expand All @@ -10,7 +11,8 @@ const getTransactionCountOfAddress = curry((_address, _provider) =>
)

const getTransactionCount = _address =>
getEnvironmentVariable(ENDPOINT_ENV_VAR_KEY)
getEnvConfiguration()
.then(() => getEnvironmentVariable(ENDPOINT_ENV_VAR_KEY))
.then(getProvider)
.then(checkEndpoint)
.then(getTransactionCountOfAddress(_address))
Expand Down
4 changes: 3 additions & 1 deletion lib/get_account_nonce.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ const { curry } = 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 getAccountNonceOfAddress = curry((_address, _provider) =>
Expand All @@ -10,7 +11,8 @@ const getAccountNonceOfAddress = curry((_address, _provider) =>

const getAccountNonce = _address =>
console.info('✔ Showing wallet details...') ||
getEnvironmentVariable(ENDPOINT_ENV_VAR_KEY)
getEnvConfiguration()
.then(() => getEnvironmentVariable(ENDPOINT_ENV_VAR_KEY))
.then(getProvider)
.then(checkEndpoint)
.then(getAccountNonceOfAddress(_address))
Expand Down
4 changes: 3 additions & 1 deletion lib/push-raw-signed-tx.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ const { curry } = 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 sendRawTx = curry((_signedTx, _provider) =>
Expand All @@ -10,7 +11,8 @@ const sendRawTx = curry((_signedTx, _provider) =>

const pushRawSignedTx = _signedTx =>
console.info('✔ Pushing raw signed tx...') ||
getEnvironmentVariable(ENDPOINT_ENV_VAR_KEY)
getEnvConfiguration()
.then(() => getEnvironmentVariable(ENDPOINT_ENV_VAR_KEY))
.then(getProvider)
.then(checkEndpoint)
.then(sendRawTx(_signedTx))
Expand Down
4 changes: 3 additions & 1 deletion lib/send-eth.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@ 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 sendEth = (_address, _amount) =>
console.info(`✔ Sending ${_amount} WEI to ${_address}...`) ||
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/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/sign-message.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,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 signMessageWithWallet = curry((_msg, _wallet) =>
Expand All @@ -17,7 +18,8 @@ const signMessageWithWallet = curry((_msg, _wallet) =>
)

const signMessage = _msgToSign =>
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-contract.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 @@ -44,7 +45,8 @@ const checkNetwork = _network =>

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

0 comments on commit e6da0b5

Please sign in to comment.