Skip to content

Commit

Permalink
feat(grpc): add timeout height to swap commands (#57)
Browse files Browse the repository at this point in the history
* feat: add timeout height to grpc swap commands

* chore: lint fix

* refactor: review fix

* refactor: fix review
  • Loading branch information
ImmanuelSegol authored and michael1011 committed Jan 30, 2019
1 parent c1af188 commit 61c7d44
Show file tree
Hide file tree
Showing 10 changed files with 101 additions and 21 deletions.
2 changes: 1 addition & 1 deletion lib/cli/Utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ export const refundSwap = (argv: Arguments<any>) => {
txHash: lockupTransaction.getHash(),
}],
destinationScript,
argv.timeout_block_height,
argv.timeout_block_number,
argv.fee_per_byte,
);

Expand Down
7 changes: 6 additions & 1 deletion lib/cli/commands/CreateReverseSwap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import BuilderComponents from '../BuilderComponents';
import { CreateReverseSwapRequest } from '../../proto/boltzrpc_pb';
import { getOrderSide } from '../Utils';

export const command = 'createreverseswap <base_currency> <quote_currency> <order_side> <rate> <claim_public_key> <amount>';
export const command = 'createreverseswap <base_currency> <quote_currency> <order_side> <rate> <claim_public_key> <amount> <timeout_block_number>';

export const describe = 'creates a new swap from Lightning to the chain';

Expand All @@ -21,6 +21,10 @@ export const builder = {
describe: 'amount of the invoice that will be returned',
type: 'number',
},
timeout_block_number: {
describe: 'block height timeout',
type: 'number',
},
};

export const handler = (argv: Arguments<any>) => {
Expand All @@ -32,6 +36,7 @@ export const handler = (argv: Arguments<any>) => {
request.setRate(argv.rate);
request.setClaimPublicKey(argv.claim_public_key);
request.setAmount(argv.amount);
request.setTimeoutBlockHeight(argv.timeout_block_number);

loadBoltzClient(argv).createReverseSwap(request, callback);
};
8 changes: 7 additions & 1 deletion lib/cli/commands/CreateSwap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ import BuilderComponents from '../BuilderComponents';
import { CreateSwapRequest } from '../../proto/boltzrpc_pb';
import { getOutputType, getOrderSide } from '../Utils';

export const command = 'createswap <base_currency> <quote_currency> <order_side> <rate> <invoice> <refund_public_key> [output_type]';
export const command = 'createswap <base_currency> <quote_currency> <order_side> <rate> <invoice>' +
'<refund_public_key> [timeout_block_number] [output_type]';

export const describe = 'create a new swap from the chain to Lightning';

Expand All @@ -21,6 +22,10 @@ export const builder = {
describe: 'public key with which a refund transaction has to be signed',
type: 'string',
},
timeout_block_number: {
describe: 'block height timeout',
type: 'number',
},
output_type: BuilderComponents.outputType,
};

Expand All @@ -34,6 +39,7 @@ export const handler = (argv: Arguments<any>) => {
request.setInvoice(argv.invoice);
request.setRefundPublicKey(argv.refund_public_key);
request.setOutputType(getOutputType(argv.output_type));
request.setTimeoutBlockHeight(argv.timeout_block_number);

loadBoltzClient(argv).createSwap(request, callback);
};
8 changes: 4 additions & 4 deletions lib/cli/commands/RefundSwap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { refundSwap, parseCommands } from '../Utils';
import { printResponse } from '../Command';
import BuilderComponents from '../BuilderComponents';

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

export const describe = 'refunds the onchain part of a swap';
Expand All @@ -13,8 +13,8 @@ export const builder = {
network: BuilderComponents.network,
lockup_transaction: BuilderComponents.lockupTransaction,
redeem_script: BuilderComponents.redeemScript,
timeout_block_height: {
describe: 'timeout block height of the timelock',
timeout_block_number: {
describe: 'timeout block number of the timelock',
type: 'number',
},
refund_private_key: {
Expand Down Expand Up @@ -44,7 +44,7 @@ const inquiries = [
},
{
type: 'input',
name: 'timeout_block_height',
name: 'timeout_block_number',
message: 'timeout block height of the timelock',
validate: (value) => {
const valid = !isNaN(parseFloat(value));
Expand Down
8 changes: 8 additions & 0 deletions lib/proto/boltzrpc_pb.d.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

60 changes: 57 additions & 3 deletions lib/proto/boltzrpc_pb.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 6 additions & 4 deletions lib/service/Service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ class Service extends EventEmitter {
* Creates a new Swap from the chain to Lightning
*/
public createSwap = async (args: { baseCurrency: string, quoteCurrency: string, orderSide: number, rate: number
invoice: string, refundPublicKey: string, outputType: number }) => {
invoice: string, refundPublicKey: string, outputType: number, timeoutBlockHeight: number}) => {

const { swapManager } = this.serviceComponents;

Expand All @@ -182,21 +182,23 @@ class Service extends EventEmitter {

const refundPublicKey = getHexBuffer(args.refundPublicKey);

return await swapManager.createSwap(args.baseCurrency, args.quoteCurrency, orderSide, args.rate, args.invoice, refundPublicKey, outputType);
return await swapManager.createSwap(args.baseCurrency, args.quoteCurrency, orderSide,
args.rate, args.invoice, refundPublicKey, outputType, args.timeoutBlockHeight);
}

/**
* Creates a new Swap from Lightning to the chain
*/
public createReverseSwap = async (args: { baseCurrency: string, quoteCurrency: string, orderSide: number, rate: number,
claimPublicKey: string, amount: number }) => {
claimPublicKey: string, amount: number, timeoutBlockHeight: number}) => {

const { swapManager } = this.serviceComponents;

const orderSide = this.getOrderSide(args.orderSide);
const claimPublicKey = getHexBuffer(args.claimPublicKey);

return await swapManager.createReverseSwap(args.baseCurrency, args.quoteCurrency, orderSide, args.rate, claimPublicKey, args.amount);
return await swapManager.createReverseSwap(args.baseCurrency, args.quoteCurrency, orderSide, args.rate,
claimPublicKey, args.amount, args.timeoutBlockHeight);
}

/**
Expand Down
14 changes: 8 additions & 6 deletions lib/swap/SwapManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,11 +66,12 @@ class SwapManager {
* @param invoice the invoice that should be paid
* @param refundPublicKey public key of the keypair needed for claiming
* @param outputType what kind of adress should be returned
* @param timeoutBlockHeight block height timeout
*
* @returns an onchain address
*/
public createSwap = async (baseCurrency: string, quoteCurrency: string, orderSide: OrderSide, rate: number,
invoice: string, refundPublicKey: Buffer, outputType: OutputType) => {
invoice: string, refundPublicKey: Buffer, outputType: OutputType, timeoutBlockHeight = 10) => {

const { sendingCurrency, receivingCurrency } = this.getCurrencies(baseCurrency, quoteCurrency, orderSide);

Expand All @@ -83,12 +84,12 @@ class SwapManager {

const { keys } = receivingCurrency.wallet.getNewKeys();

const timeoutBlockHeight = bestBlock.height + 10;
const timeoutHeight = bestBlock.height + timeoutBlockHeight;
const redeemScript = pkRefundSwap(
getHexBuffer(paymentHash),
keys.publicKey,
refundPublicKey,
timeoutBlockHeight,
timeoutHeight,
);

const encodeFunction = getScriptHashEncodeFunction(outputType);
Expand Down Expand Up @@ -125,11 +126,12 @@ class SwapManager {
* @param rate conversion rate of base and quote currency
* @param claimPublicKey public key of the keypair needed for claiming
* @param amount amount of the invoice
* @param timeoutBlockHeight block height timeout
*
* @returns a Lightning invoice, the lockup transaction and its hash
*/
public createReverseSwap = async (baseCurrency: string, quoteCurrency: string, orderSide: OrderSide, rate: number,
claimPublicKey: Buffer, amount: number) => {
claimPublicKey: Buffer, amount: number, timeoutBlockHeight: number) => {

const { sendingCurrency, receivingCurrency } = this.getCurrencies(baseCurrency, quoteCurrency, orderSide);

Expand All @@ -140,13 +142,13 @@ class SwapManager {
const bestBlock = await sendingCurrency.chainClient.getBestBlock();
const { rHash, paymentRequest } = await receivingCurrency.lndClient.addInvoice(amount);
const { keys } = sendingCurrency.wallet.getNewKeys();
const timeoutHeight = bestBlock.height + timeoutBlockHeight;

const timeoutBlockHeight = bestBlock.height + 10;
const redeemScript = pkRefundSwap(
Buffer.from(rHash as string, 'base64'),
claimPublicKey,
keys.publicKey,
timeoutBlockHeight,
timeoutHeight,
);

const outputScript = p2shP2wshOutput(redeemScript);
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
"dev:watch": "concurrently --kill-others \"npm run compile:watch\" \"npm run nodemon:watch\"",
"nodemon:watch": "nodemon --watch dist -e js bin/boltzd",
"lint": "tslint --project tsconfig.json && tslint --config tslint-alt.json 'bin/*' 'test/**/*.ts'",
"lint:fix": "tslint --fix --project tsconfig.json && tslint --fix --config tslint-alt.json 'bin/*' 'test/**/*.ts'",
"docker:start": "docker run -d --name simnet -p 18556:18556 -p 19556:19556 -p 10009:10009 -p 10010:10010 -p 11009:11009 -p 11010:11010 boltz/simnet",
"docker:stop": "docker kill simnet && docker rm simnet",
"test": "npm run test:unit && npm run test:int",
Expand Down
4 changes: 3 additions & 1 deletion proto/boltzrpc.proto
Original file line number Diff line number Diff line change
Expand Up @@ -151,10 +151,11 @@ message CreateSwapRequest {
string invoice = 5;
string refund_public_key = 6;
OutputType output_type = 7;
int64 timeout_block_number = 8;
}
message CreateSwapResponse {
string redeem_script = 1;
int64 timeout_block_height = 2;
int64 timeout_block_number = 2;
string address = 3;
int64 expected_amount = 4;
}
Expand All @@ -167,6 +168,7 @@ message CreateReverseSwapRequest {
string claim_public_key = 5;
// Amount of the invoice that will be returned
int64 amount = 6;
int64 timeout_block_number = 7;
}
message CreateReverseSwapResponse {
string invoice = 1;
Expand Down

0 comments on commit 61c7d44

Please sign in to comment.