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

feat: choose random source #3

Merged
merged 2 commits into from
Aug 16, 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
28 changes: 24 additions & 4 deletions activity-generator/bin/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { program } from 'commander';
import { select, input, confirm } from '@inquirer/prompts';
import { v4 } from 'uuid';
import { parse } from 'json2csv';
import { getFrequency, getAmountInSats } from './validation/inputGetters.js';
import { getFrequency, getAmountInSats, verifyPubKey } from './validation/inputGetters.js';
program.requiredOption('--config <file>');
program.option('--csv');
program.parse();
Expand Down Expand Up @@ -54,6 +54,7 @@ async function buildControlNodes() {
console.log(`Node: ${node.alias} has no graph`)
return console.error("Please check that controlled nodes have open channels to other nodes")
}

//dump graph information
nodeObj[current_node.identity_pubkey] = current_node;
nodeObj[current_node.identity_pubkey].graph = nodeGraph;
Expand Down Expand Up @@ -87,17 +88,29 @@ async function promptForActivities() {
let activity = {};
activity.uuid = v4();

let sourceArray = [{
name: '(choose random)',
value: Object.values(nodeObj)[Math.floor(Math.random() * Object.values(nodeObj).length)].identity_pubkey
}, {
name: '(input pubkey)',
value: false
}]

activity.src = await select({
message: "Choose a source? \n",
choices: Object.keys(nodeObj).map(key => {
choices: sourceArray.concat(Object.keys(nodeObj).map(key => {
let node = nodeObj[key];
return {
name: `${node.alias}: (${node.identity_pubkey})`,
value: node.identity_pubkey
}
})
}))
})

if (!activity.src) {
activity.src = await input({ message: 'Enter pubkey:' });
}

let destArray = [{
name: `(choose random)`,
value: nodeObj[activity.src].possible_dests[Math.floor(Math.random() * nodeObj[activity.src].possible_dests.length)].pub_key
Expand All @@ -118,7 +131,14 @@ async function promptForActivities() {
})

if (!activity.dest) {
activity.dest = await input({ message: 'Enter pubkey:' });
// console.log(nodeObj[Object.keys(nodeObj)[0]].graph.nodes)
const singleNodeGraph = Object.values(nodeObj).find((node) => {
return node.graph.nodes
}).graph

const allPossibleNodes = singleNodeGraph.nodes.map((node) => node.pub_key)
activity.dest = await verifyPubKey(allPossibleNodes)
// activity.dest = await input({ message: 'Enter pubkey:' });
}

activity.action = await input({ message: 'What action?', default: "keysend" });
Expand Down
3 changes: 2 additions & 1 deletion activity-generator/bin/validation/err_config.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export const ErrorConfig = {
"POSITIVE_INTEGER": "must be a positive interger"
"POSITIVE_INTEGER": "must be a positive interger",
"PUBKEY_NOT_FOUND": "pubkey does not exist on the network"
}
17 changes: 16 additions & 1 deletion activity-generator/bin/validation/inputGetters.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,20 @@ const getAmountInSats = async () => {
}
}

const verifyPubKey = async (allPossibleNodes) => {
try {
const pubkey = await input({ message: 'Enter pubkey:' });
const isValidPubkey = allPossibleNodes.find((_pubkey) => pubkey === _pubkey )
if (!isValidPubkey) {
throw new Error(ErrorConfig.PUBKEY_NOT_FOUND)
}
return pubkey
} catch (err) {
console.error(err.message)
if (err.message === ErrorConfig.PUBKEY_NOT_FOUND) {
return await verifyPubKey(allPossibleNodes)
}
}
}

export { getFrequency, getAmountInSats }
export { getFrequency, getAmountInSats, verifyPubKey }