Skip to content
This repository has been archived by the owner on Aug 28, 2019. It is now read-only.

Commit

Permalink
feat: add generation of BIP21 payment requests (#26)
Browse files Browse the repository at this point in the history
  • Loading branch information
michael1011 authored Jan 3, 2019
1 parent 0fd61a7 commit a06aaa7
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 34 deletions.
4 changes: 0 additions & 4 deletions lib/proto/boltzrpc_pb.d.ts

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

29 changes: 1 addition & 28 deletions lib/proto/boltzrpc_pb.js

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

19 changes: 19 additions & 0 deletions lib/service/PaymentRequestUtils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/**
* Get the BIP21 prefix for a currency
*/
const getBip21Prefix = (symbol: string) => {
return symbol === 'BTC' ? 'bitcoin' : 'litecoin';
};

/**
* Encode a BIP21 payment request
*/
export const encodeBip21 = (symbol: string, address: string, satoshis: number, label?: string) => {
let request = `${getBip21Prefix(symbol)}:${address}?value=${satoshis / 100000000}`;

if (label) {
request += `&label=${label.replace(/ /g, '%20')}`;
}

return request;
};
19 changes: 18 additions & 1 deletion lib/service/Service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import RateProvider from '../rates/RateProvider';
import { PairInstance, PairFactory } from '../consts/Database';
import { splitPairId, stringify, generateId, mapToObject } from '../Utils';
import Errors from './Errors';
import { encodeBip21 } from './PaymentRequestUtils';

type PairConfig = {
base: string;
Expand Down Expand Up @@ -177,8 +178,11 @@ class Service extends EventEmitter {

const side = this.getOrderSide(orderSide);

const chainCurrency = this.getChainCurrency(side, base, quote);
const lightningCurrency = this.getLightningCurrency(side, base, quote);

const swapResponse = await this.boltz.createSwap(base, quote, side, rate, invoice, refundPublicKey, OutputType.COMPATIBILITY);
await this.boltz.listenOnAddress(this.getChainCurrency(side, base, quote), swapResponse.address);
await this.boltz.listenOnAddress(chainCurrency, swapResponse.address);

const id = generateId(6);

Expand All @@ -189,6 +193,12 @@ class Service extends EventEmitter {

return {
id,
bip21: encodeBip21(
chainCurrency,
swapResponse.address,
swapResponse.expectedAmount,
`Submarine Swap to ${lightningCurrency}`,
),
...swapResponse,
};
}
Expand All @@ -204,6 +214,13 @@ class Service extends EventEmitter {
}
}

/**
* Get the currency on which the Lightning transaction happens
*/
private getLightningCurrency = (orderSide: OrderSide, base: string, quote: string) => {
return this.getChainCurrency(orderSide === OrderSide.BUY ? OrderSide.SELL : OrderSide.BUY, base, quote);
}

/**
* Gets the corresponding OrderSide enum of a string
*/
Expand Down
1 change: 0 additions & 1 deletion proto/boltzrpc.proto
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,6 @@ message CreateSwapResponse {
int64 timeout_block_height = 2;
string address = 3;
int64 expected_amount = 4;
string bip21 = 5;
}

message CreateReverseSwapRequest {
Expand Down

0 comments on commit a06aaa7

Please sign in to comment.