Skip to content

Commit

Permalink
Merge pull request #63 from fr1t2/master
Browse files Browse the repository at this point in the history
feat: Adding JSON response to output
  • Loading branch information
jplomas authored Mar 17, 2022
2 parents 66aa261 + 409ad24 commit a08af82
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 11 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ $ npm install -g @theqrl/cli
$ qrl-cli COMMAND
running command...
$ qrl-cli (-v|--version|version)
@theqrl/cli/1.9.0 linux-x64 node-v16.13.1
@theqrl/cli/1.9.0 linux-x64 node-v16.13.2
$ qrl-cli --help [COMMAND]
USAGE
$ qrl-cli COMMAND
Expand Down Expand Up @@ -311,6 +311,7 @@ OPTIONS
-g, --grpc=grpc advanced: grpc endpoint (for devnet/custom QRL network deployments)
-h, --hexseed=hexseed Secret (h)exseed/mnemonic of address notarization should be sent from
-i, --otsindex=otsindex Unused OTS key (i)ndex for message transaction
-j, --json Return JSON response
-m, --mainnet uses mainnet for the notarization
-p, --password=password Encrypted QRL wallet file (p)assword
-t, --testnet uses testnet for the notarization
Expand Down Expand Up @@ -346,6 +347,7 @@ ARGUMENTS
OPTIONS
-g, --grpc=grpc Custom grcp endpoint to connect a hosted QRL node (-g 127.0.0.1:19009)
-j, --json Output in JSON
-m, --mainnet (default) Queries mainnet for the OTS state
-p, --password=password wallet file password if encrypted
-t, --testnet Queries testnet for the OTS state
Expand Down
21 changes: 18 additions & 3 deletions src/commands/notarize.js
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,9 @@ class Notarise extends Command {
grpcEndpoint = 'mainnet-1.automated.theqrl.org:19009'
network = 'Mainnet'
}
this.log(white().bgBlue(network))
if (!flags.json){
this.log(white().bgBlue(network))
}
// the data to notarise here, can be a file submitted (path) or a string passed on cli
const spinner = ora({ text: 'Notarising Data...\n', }).start()
if (args.dataHash) {
Expand Down Expand Up @@ -347,14 +349,20 @@ class Notarise extends Command {
const pushTransactionRes = JSON.stringify(response.tx_hash)
const txhash = JSON.parse(pushTransactionRes)
if (txnHash === bytesToHex(txhash.data)) {
spinner4.succeed(`Transaction submitted to node: transaction ID: ${bytesToHex(txhash.data)}`)

// return link to explorer
if (network === 'Mainnet') {
spinner4.succeed(`Transaction submitted to Mainnet node: transaction ID: ${bytesToHex(txhash.data)}`)
spinner3.succeed(`https://explorer.theqrl.org/tx/${bytesToHex(txhash.data)}`)
if (flags.json){
this.log(`[{"tx_id":"${bytesToHex(txhash.data)}"}]`)
}
}
else if (network === 'Testnet') {
spinner4.succeed(`Transaction submitted to Testnet node: transaction ID: ${bytesToHex(txhash.data)}`)
spinner3.succeed(`https://testnet-explorer.theqrl.org/tx/${bytesToHex(txhash.data)}`)
if (flags.json){
this.log(`[{"tx_id":"${bytesToHex(txhash.data)}"}]`)
}
}
// this.exit(0)
}
Expand Down Expand Up @@ -396,6 +404,13 @@ Notarise.flags = {
description: 'uses mainnet for the notarization'
}),

json: flags.boolean({
char: 'j',
default: false,
description: 'Return JSON response'
}),


grpc: flags.string({
char: 'g',
required: false,
Expand Down
28 changes: 21 additions & 7 deletions src/commands/ots.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,31 +80,39 @@ class OTSKey extends Command {
grpcEndpoint = 'mainnet-1.automated.theqrl.org:19009'
network = 'Mainnet'
}
this.log(white().bgBlue(network))
const spinner = ora({text: 'Fetching OTS from API...'}).start()
let spinner = ""
if (!flags.json) {
this.log(white().bgBlue(network))
spinner = ora({text: 'Fetching OTS from API...'}).start()
}
const Qrlnetwork = await new Qrlnode(grpcEndpoint)
try {
await Qrlnetwork.connect()
// verify we have connected and try again if not
let i = 0
const count = 5
while (Qrlnetwork.connection === false && i < count) {
spinner.succeed(`retry connection attempt: ${i}...`)
if (!flags.json) {
spinner.succeed(`retry connection attempt: ${i}...`)
}
// eslint-disable-next-line no-await-in-loop
await Qrlnetwork.connect()
// eslint-disable-next-line no-plusplus
i++
}
} catch (e) {
spinner.fail(`Failed to connect to node. Check network connection & parameters.\n${e}`)
this.log(`Failed to connect to node. Check network connection & parameters.\n${e}`)
this.exit(1)
}

const request = { address: Buffer.from(address.substring(1), 'hex') }
const response = await Qrlnetwork.api('GetOTS', request)
if (response.unused_ots_index_found) {
spinner.succeed(`Next unused OTS key: ${response.next_unused_ots_index}`)
if (response.unused_ots_index_found && flags.json) {
this.log(`[{"next_key": ${response.next_unused_ots_index}}]`)
this.exit(0)
} else if (response.unused_ots_index_found && !flags.json) {
spinner.succeed(`Next unused OTS key: ${response.next_unused_ots_index}`)
this.exit(0)
} else {
this.log(`${red('⨉')} Unable to fetch an OTS key`)
this.exit(1)
Expand Down Expand Up @@ -149,6 +157,12 @@ OTSKey.flags = {
required: false,
description: 'wallet file password if encrypted'
}),
json: flags.boolean({
char: 'j',
required: false,
description: 'Output in JSON'
}),

}

module.exports = {OTSKey}
module.exports = {OTSKey}

0 comments on commit a08af82

Please sign in to comment.