Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding runOnce and validationMethod and bug fix #95

Merged
merged 1 commit into from
Aug 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { Currency, generateWallet } from '@tatumio/tatum'
import axios from 'axios'
import dotenv from 'dotenv'
import meow from 'meow'
import { PasswordType } from './interfaces'
import { ExternalUrlMethod, PasswordType } from './interfaces'
import {
checkConfig,
exportWallets,
Expand Down Expand Up @@ -70,6 +70,8 @@ const { input: command, flags, help } = meow(
--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.
--externalUrlMethod Determine what http method to use when calling the url passed in the --externalUrl option. Accepts GET or POST. Defaults to GET method. Daemon mode only.
--runOnce Run the daemon command one time. Check for a new transactions to sign once, and then exit the process. Daemon mode only.
`,
{
flags: {
Expand Down Expand Up @@ -106,6 +108,14 @@ const { input: command, flags, help } = meow(
'env-file': {
type: 'string',
},
externalUrlMethod: {
type: 'string',
default: 'GET',
},
runOnce: {
type: 'boolean',
default: false,
}
},
},
)
Expand Down Expand Up @@ -145,7 +155,9 @@ const startup = async () => {
flags.path,
flags.chain?.split(',') as Currency[],
flags.externalUrl,
flags.externalUrlMethod as ExternalUrlMethod,
flags.period,
flags.runOnce,
)
break
}
Expand Down
2 changes: 2 additions & 0 deletions src/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,5 @@ export interface StoreWalletValue {
address?: string
xpub?: string
}

export type ExternalUrlMethod = 'GET' | 'POST';
2 changes: 1 addition & 1 deletion src/management.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@
}
}

export const exportWallets = (pwd: string, _path1: string | undefined, path?: string) => {
export const exportWallets = (pwd: string, path?: string) => {
const pathToWallet = path || homedir() + '/.tatumrc/wallet.dat'
if (!existsSync(pathToWallet)) {
console.error(JSON.stringify({ error: `No such wallet file.` }, null, 2))
Expand Down Expand Up @@ -122,7 +122,7 @@
}

const generatePureWallet = async (chain: Currency, testnet: boolean, mnemonic?: string) => {
let wallet: any

Check warning on line 125 in src/management.ts

View workflow job for this annotation

GitHub Actions / 🏗️ Install and build

Unexpected any. Specify a different type
if (chain === Currency.SOL) {
const sdk = TatumSolanaSDK({ apiKey: '' })
wallet = sdk.wallet.wallet()
Expand Down Expand Up @@ -224,7 +224,7 @@
const cnt = Number(count)
for (let i = 0; i < cnt; i++) {
const wallet = await generatePureWallet(chain, testnet)
let address: any

Check warning on line 227 in src/management.ts

View workflow job for this annotation

GitHub Actions / 🏗️ Install and build

Unexpected any. Specify a different type
if (wallet.address) {
address = wallet.address
} else {
Expand Down Expand Up @@ -261,7 +261,7 @@
}

// TODO: validate all properties from wallet and create a type or interface to replace any bellow
export const isWalletsValid = (wallets: any, options: WalletsValidationOptions) => {

Check warning on line 264 in src/management.ts

View workflow job for this annotation

GitHub Actions / 🏗️ Install and build

Unexpected any. Specify a different type
if (Object.keys(wallets).length === 0) {
console.error(JSON.stringify({ error: `No such wallet for chain '${options.chain}'.` }, null, 2))
return false
Expand Down Expand Up @@ -354,7 +354,7 @@
console.error(JSON.stringify({ error: `No such wallet for signatureId '${id}'.` }, null, 2))
return null
}
let pk: { address: any }

Check warning on line 357 in src/management.ts

View workflow job for this annotation

GitHub Actions / 🏗️ Install and build

Unexpected any. Specify a different type
if (wallet[id].address) {
pk = {
address: wallet[id].address,
Expand Down
38 changes: 32 additions & 6 deletions src/signatures.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@
import { AxiosInstance } from 'axios'
import _ from 'lodash'
import { KMS_CONSTANTS } from './constants'
import { Wallet } from './interfaces'
import { ExternalUrlMethod, Wallet } from './interfaces'
import { getManagedWallets, getWallet, getWalletForSignature } from './management'
import semver from 'semver'
import { Config, ConfigOption } from './config'
Expand All @@ -72,7 +72,7 @@
return [...new Set(keys)]
}

function validatePrivateKeyWasFound(wallet: any, blockchainSignature: TransactionKMS, privateKey: string | undefined) {

Check warning on line 75 in src/signatures.ts

View workflow job for this annotation

GitHub Actions / 🏗️ Install and build

Unexpected any. Specify a different type
if (privateKey) return

const index = blockchainSignature.index
Expand All @@ -99,11 +99,16 @@
axios: AxiosInstance,
path?: string,
externalUrl?: string,
externalUrlMethod?: ExternalUrlMethod
) => {
if (externalUrl) {
console.log(`${new Date().toISOString()} - External url '${externalUrl}' is present, checking against it.`)
try {
await axios.get(`${externalUrl}/${blockchainSignature.id}`)
if (externalUrlMethod === 'POST') {
await axios.post(`${externalUrl}/${blockchainSignature.id}`, blockchainSignature);
} else {
await axios.get(`${externalUrl}/${blockchainSignature.id}`)
}
} catch (e) {
console.error(e)
console.error(
Expand Down Expand Up @@ -143,7 +148,7 @@
return
}
case Currency.SOL: {
const solSDK = TatumSolanaSDK({ apiKey, url: TATUM_URL as any })

Check warning on line 151 in src/signatures.ts

View workflow job for this annotation

GitHub Actions / 🏗️ Install and build

Unexpected any. Specify a different type
txData = await solSDK.kms.sign(
blockchainSignature as PendingTransaction,
wallets.map(w => w.privateKey),
Expand Down Expand Up @@ -194,16 +199,16 @@
return
}
case Currency.XRP: {
const xrpSdk = TatumXrpSDK({ apiKey, url: TATUM_URL as any })

Check warning on line 202 in src/signatures.ts

View workflow job for this annotation

GitHub Actions / 🏗️ Install and build

Unexpected any. Specify a different type
const xrpSecret = wallets[0].secret ? wallets[0].secret : wallets[0].privateKey
txData = await xrpSdk.kms.sign(blockchainSignature as any, xrpSecret)

Check warning on line 204 in src/signatures.ts

View workflow job for this annotation

GitHub Actions / 🏗️ Install and build

Unexpected any. Specify a different type
await xrpSdk.blockchain.broadcast({ txData, signatureId: blockchainSignature.id })
return
}
case Currency.XLM: {
const xlmSdk = TatumXlmSDK({ apiKey, url: TATUM_URL as any })

Check warning on line 209 in src/signatures.ts

View workflow job for this annotation

GitHub Actions / 🏗️ Install and build

Unexpected any. Specify a different type
const xlmSecret = wallets[0].secret ? wallets[0].secret : wallets[0].privateKey
txData = await xlmSdk.kms.sign(blockchainSignature as any, xlmSecret, testnet)

Check warning on line 211 in src/signatures.ts

View workflow job for this annotation

GitHub Actions / 🏗️ Install and build

Unexpected any. Specify a different type
await xlmSdk.blockchain.broadcast({ txData, signatureId: blockchainSignature.id })
return
}
Expand Down Expand Up @@ -570,7 +575,9 @@
path?: string,
chains?: Currency[],
externalUrl?: string,
externalUrlMethod?: ExternalUrlMethod,
period = 5,
runOnce?: boolean,
) => {
let running = false
const supportedChains = chains || [
Expand All @@ -597,13 +604,34 @@
Currency.ALGO,
Currency.KCS,
]

if (runOnce) {
await processPendingTransactions(supportedChains, pwd, testnet, path, axios, externalUrl, externalUrlMethod)
return;
}

setInterval(async () => {
if (running) {
return
}
running = true

const transactions = []
await processPendingTransactions(supportedChains, pwd, testnet, path, axios, externalUrl, externalUrlMethod)

running = false
}, period * 1000)
}

async function processPendingTransactions(
supportedChains: Currency[],
pwd: string,
testnet: boolean,
path: string | undefined,
axios: AxiosInstance,
externalUrl: string | undefined,
externalUrlMethod: ExternalUrlMethod | undefined
) {
const transactions = []
try {
for (const supportedChain of supportedChains) {
const wallets = getManagedWallets(pwd, supportedChain, testnet, path)
Expand All @@ -615,7 +643,7 @@
const data = []
for (const transaction of transactions) {
try {
await processTransaction(transaction, testnet, pwd, axios, path, externalUrl)
await processTransaction(transaction, testnet, pwd, axios, path, externalUrl, externalUrlMethod)
console.log(`${new Date().toISOString()} - Tx was processed: ${transaction.id}`)
} catch (e) {
const msg = (<any>e).response ? JSON.stringify((<any>e).response.data, null, 2) : `${e}`
Expand All @@ -638,8 +666,6 @@
)
}
}
running = false
}, period * 1000)
}

function isValidNumber(value: number | undefined): boolean {
Expand Down
Loading