Skip to content

Commit

Permalink
feat: add inquirer as optional input for boltz-cli (#24)
Browse files Browse the repository at this point in the history
* feat: add inquirer as optional input for boltz-cli

* refactor: review
  • Loading branch information
ImmanuelSegol authored and michael1011 committed Dec 8, 2018
1 parent 35cd8cb commit 8a61628
Show file tree
Hide file tree
Showing 6 changed files with 362 additions and 133 deletions.
11 changes: 11 additions & 0 deletions lib/cli/Utils.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Arguments } from 'yargs';
import inquire from './inquire';
import { address, ECPair, Transaction } from 'bitcoinjs-lib';
import { getHexBuffer } from '../Utils';
import Networks from '../consts/Networks';
Expand Down Expand Up @@ -54,6 +55,16 @@ const parseSwapOutput = (redeemScript: Buffer, lockupTransaction: Transaction) =
return swapOutput;
};

export const parseCommands = async (inquiries: any[], argv: Arguments): Promise<Arguments> => {
const argvLength = Object.keys(argv).length;
if (argvLength === inquiries.length) {
const answers = await inquire(inquiries);
return { ...answers, ...argv };
} else {
return argv;
}
};

export const claimSwap = (argv: Arguments) => {
const { network, lockupTransaction, redeemScript, destinationScript } = parseParameters(argv);
const claimKeys = ECPair.fromPrivateKey(getHexBuffer(argv.claim_private_key), { network });
Expand Down
52 changes: 47 additions & 5 deletions lib/cli/commands/ClaimSwap.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { Arguments } from 'yargs';
import { claimSwap } from '../Utils';
import { claimSwap, parseCommands } from '../Utils';
import Networks from '../../consts/Networks';
import { printResponse } from '../Command';
import BuilderComponents from '../BuilderComponents';

export const command = 'claimswap <network> <lockup_transaction> <redeem_script> <preimage> <claim_private_key> <destination_address> [fee_per_byte]';
export const command = 'claimswap [network] [lockup_transaction] [redeem_script] [preimage] [claim_private_key] [destination_address] [fee_per_byte]';

export const describe = 'claims the onchain part of a reverse swap';

Expand All @@ -16,15 +17,56 @@ export const builder = {
type: 'string',
},
claim_private_key: {
describe: 'public key with which a claiming transaction has to be signed',
describe: 'private key with which a claiming transaction has to be signed',
type: 'string',
},
destination_address: BuilderComponents.destinationAddress,
fee_per_byte: BuilderComponents.feePerByte,
};

export const handler = (argv: Arguments) => {
const claimTransaction = claimSwap(argv);
const inquiries = [
{
type: 'list',
name: 'network',
message: BuilderComponents.network.describe,
choices: Object.keys(Networks),
},
{
type: 'input',
name: 'lockup_transaction',
message: BuilderComponents.lockupTransaction.describe,
},
{
type: 'input',
name: 'redeem_script',
message: BuilderComponents.redeemScript.describe,
},
{
type: 'input',
name: 'preimage',
message: 'preimage of the invoice',
},
{
type: 'input',
name: 'claim_private_key',
message: 'private key with which a claiming transaction has to be signed',
},
{
type: 'input',
name: 'destination_address',
message: BuilderComponents.destinationAddress.describe,
},
{
type: 'input',
name: 'fee_per_byte',
message: BuilderComponents.feePerByte.describe,
default: BuilderComponents.feePerByte.detauls,
},
];

export const handler = async (argv: Arguments) => {
const commands = await parseCommands(inquiries, argv);
const claimTransaction = claimSwap(commands);

printResponse({
claimTransaction,
Expand Down
63 changes: 57 additions & 6 deletions lib/cli/commands/RefundSwap.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { Arguments } from 'yargs';
import { refundSwap } from '../Utils';
import { refundSwap, parseCommands } from '../Utils';
import Networks from '../../consts/Networks';
import { printResponse } from '../Command';
import BuilderComponents from '../BuilderComponents';

export const command = 'refundswap <network> <lockup_transaction> <redeem_script> <timeout_block_height> <refund_private_key> ' +
'<destination_address> [fee_per_byte]';
export const command = 'refundswap [network] [lockup_transaction] [redeem_script] [timeout_block_height] [refund_private_key] ' +
'[destination_address] [fee_per_byte]';

export const describe = 'refunds the onchain part of a swap';

Expand All @@ -17,16 +18,66 @@ export const builder = {
type: 'number',
},
refund_private_key: {
describe: 'public key with which a refund transaction has to be signed',
describe: 'private key with which a refund transaction has to be signed',
type: 'string',
},
destination_address: BuilderComponents.destinationAddress,
fee_per_byte: BuilderComponents.feePerByte,
};

export const handler = (argv: Arguments) => {
const refundTransaction = refundSwap(argv);
const inquiries = [
{
type: 'list',
name: 'network',
message: BuilderComponents.network.describe,
choices: Object.keys(Networks),
},
{
type: 'input',
name: 'lockup_transaction',
message: BuilderComponents.lockupTransaction.describe,
},
{
type: 'input',
name: 'redeem_script',
message: BuilderComponents.redeemScript.describe,
},
{
type: 'input',
name: 'timeout_block_height',
message: 'timeout block height of the timelock',
validate: (value) => {
const valid = !isNaN(parseFloat(value));
return valid || 'Please enter a number';
},
filter: Number,
},
{
type: 'input',
name: 'refund_private_key',
message: 'private key with which a refund transaction has to be signed',
},
{
type: 'input',
name: 'destination_address',
message: BuilderComponents.destinationAddress.describe,
},
{
type: 'input',
name: 'fee_per_byte',
message: BuilderComponents.feePerByte.describe,
default: BuilderComponents.feePerByte.detauls,
validate: (value) => {
const valid = !isNaN(parseFloat(value));
return valid || 'Please enter a number';
},
filter: Number,
},
];

export const handler = async (argv: Arguments) => {
const commands = await parseCommands(inquiries, argv);
const refundTransaction = refundSwap(commands);
printResponse({
refundTransaction,
});
Expand Down
7 changes: 7 additions & 0 deletions lib/cli/inquire.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import inquirer from 'inquirer';

const inquire = (inquiries: any[]): Promise<object> => {
return inquirer.prompt(inquiries);
};

export default inquire;

0 comments on commit 8a61628

Please sign in to comment.