Skip to content

Commit

Permalink
adding runOnce and validationMethod and bug fix (#95)
Browse files Browse the repository at this point in the history
  • Loading branch information
tokayer authored Aug 21, 2023
1 parent b00a67e commit a6d3841
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 8 deletions.
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 getPassword = async (pwdType: PasswordType, axiosInstance: AxiosIns
}
}

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
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 { TatumXrpSDK } from '@tatumio/xrp'
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 Down Expand Up @@ -99,11 +99,16 @@ const processTransaction = async (
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 @@ -570,7 +575,9 @@ export const processSignatures = async (
path?: string,
chains?: Currency[],
externalUrl?: string,
externalUrlMethod?: ExternalUrlMethod,
period = 5,
runOnce?: boolean,
) => {
let running = false
const supportedChains = chains || [
Expand All @@ -597,13 +604,34 @@ export const processSignatures = async (
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 @@ export const processSignatures = async (
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 @@ export const processSignatures = async (
)
}
}
running = false
}, period * 1000)
}

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

0 comments on commit a6d3841

Please sign in to comment.