Skip to content

Commit f431c41

Browse files
Merge pull request #5610 from BitGo/BTC-1837-more-codecs
feat(abstract-lightning): add codecs for more lightning apis
2 parents 253e6d2 + 1b6d238 commit f431c41

File tree

22 files changed

+549
-397
lines changed

22 files changed

+549
-397
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import * as t from 'io-ts';
2+
3+
// codecs for lightning wallet channel backup api
4+
5+
export const ChanPoints = t.strict(
6+
{
7+
fundingTxid: t.string,
8+
outputIndex: t.number,
9+
},
10+
'ChanPoints'
11+
);
12+
13+
export type ChanPoints = t.TypeOf<typeof ChanPoints>;
14+
15+
export const BackupResponse = t.strict(
16+
{
17+
chanPoints: t.array(ChanPoints),
18+
multiChanBackup: t.string,
19+
},
20+
'BackupResponse'
21+
);
22+
23+
export type BackupResponse = t.TypeOf<typeof BackupResponse>;
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
import * as t from 'io-ts';
2+
import { BigIntFromString } from 'io-ts-types/BigIntFromString';
3+
4+
// codecs for lightning wallet balance api
5+
6+
export const LndAmount = t.strict(
7+
{
8+
sat: BigIntFromString,
9+
msat: BigIntFromString,
10+
},
11+
'LndAmount'
12+
);
13+
14+
export type LndAmount = t.TypeOf<typeof LndAmount>;
15+
16+
export const ChannelBalance = t.strict(
17+
{
18+
/** The balance on your side of the channel and what you the user can spend. */
19+
localBalance: LndAmount,
20+
/** The balance on the other side of the channel, what your channel partner can controls. */
21+
remoteBalance: LndAmount,
22+
/** Sum of local unsettled balances. */
23+
unsettledLocalBalance: LndAmount,
24+
/** Sum of remote unsettled balances. */
25+
unsettledRemoteBalance: LndAmount,
26+
/** Sum of local pending balances. */
27+
pendingOpenLocalBalance: LndAmount,
28+
/** Sum of local remote balances. */
29+
pendingOpenRemoteBalance: LndAmount,
30+
},
31+
'ChannelBalance'
32+
);
33+
34+
export type ChannelBalance = t.TypeOf<typeof ChannelBalance>;
35+
36+
export const LndWalletBalance = t.strict(
37+
{
38+
/** Total balance, confirmed and unconfirmed */
39+
totalBalance: BigIntFromString,
40+
confirmedBalance: BigIntFromString,
41+
unconfirmedBalance: BigIntFromString,
42+
lockedBalance: BigIntFromString,
43+
reservedBalanceAnchorChan: BigIntFromString,
44+
},
45+
'LndWalletBalance'
46+
);
47+
48+
export type LndWalletBalance = t.TypeOf<typeof LndWalletBalance>;
49+
50+
/**
51+
The balances as returned from lnd.
52+
53+
Wallet Balance
54+
https://api.lightning.community/api/lnd/lightning/wallet-balance/index.html
55+
56+
Channel Balance
57+
https://api.lightning.community/api/lnd/lightning/channel-balance/index.html
58+
*/
59+
export const LndBalance = t.strict(
60+
{
61+
offchain: ChannelBalance,
62+
onchain: LndWalletBalance,
63+
totalLimboBalance: BigIntFromString,
64+
},
65+
'LndBalance'
66+
);
67+
68+
export type LndBalance = t.TypeOf<typeof LndBalance>;
69+
70+
export const LndGetBalancesResponse = t.strict(
71+
{
72+
inboundBalance: BigIntFromString,
73+
inboundPendingBalance: BigIntFromString,
74+
inboundUnsettledBalance: BigIntFromString,
75+
outboundBalance: BigIntFromString,
76+
outboundPendingBalance: BigIntFromString,
77+
outboundUnsettledBalance: BigIntFromString,
78+
// wallet balances, names forced by type in AbstractCoin
79+
spendableBalanceString: BigIntFromString,
80+
balanceString: BigIntFromString,
81+
confirmedBalanceString: BigIntFromString,
82+
},
83+
'LndGetBalancesResponse'
84+
);
85+
86+
export type LndGetBalancesResponse = t.TypeOf<typeof LndGetBalancesResponse>;
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
export * from './backup';
2+
export * from './balance';
3+
export * from './invoice';
4+
export * from './payment';
5+
export * from './transaction';
6+
export * from './wallet';
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
import * as t from 'io-ts';
2+
import { BigIntFromString } from 'io-ts-types/BigIntFromString';
3+
import { DateFromISOString } from 'io-ts-types/DateFromISOString';
4+
5+
// codecs for lightning wallet invoice related apis
6+
7+
export const InvoiceStatus = t.union(
8+
[
9+
// Initial state.
10+
// Transitions to 'settled' or 'canceled' on LND notification.
11+
t.literal('open'),
12+
// Final state.
13+
t.literal('settled'),
14+
// Final state.
15+
t.literal('canceled'),
16+
],
17+
'InvoiceStatus'
18+
);
19+
export type InvoiceStatus = t.TypeOf<typeof InvoiceStatus>;
20+
21+
export const CreateInvoiceBody = t.intersection(
22+
[
23+
t.type({
24+
valueMsat: BigIntFromString,
25+
}),
26+
t.partial({
27+
memo: t.string,
28+
expiry: t.number,
29+
}),
30+
],
31+
'CreateInvoiceBody'
32+
);
33+
export type CreateInvoiceBody = t.TypeOf<typeof CreateInvoiceBody>;
34+
35+
/**
36+
* A representation of the data tracked by the indexer for an invoice it has
37+
* created within lnd.
38+
*/
39+
export const Invoice = t.intersection(
40+
[
41+
t.type({
42+
valueMsat: BigIntFromString,
43+
paymentHash: t.string,
44+
/** The BOLT #11 encoded invoice string */
45+
invoice: t.string,
46+
/** The public BitGo walletId to which this invoice belongs */
47+
walletId: t.string,
48+
status: InvoiceStatus,
49+
/** A date in ISO format representing when this invoice expires. */
50+
expiresAt: DateFromISOString,
51+
}),
52+
t.partial({
53+
memo: t.string,
54+
}),
55+
],
56+
'Invoice'
57+
);
58+
export type Invoice = t.TypeOf<typeof Invoice>;
59+
60+
export const InvoiceInfo = t.intersection(
61+
[
62+
t.type({
63+
valueMsat: BigIntFromString,
64+
paymentHash: t.string,
65+
invoice: t.string,
66+
walletId: t.string,
67+
status: InvoiceStatus,
68+
expiresAt: DateFromISOString,
69+
createdAt: DateFromISOString,
70+
updatedAt: DateFromISOString,
71+
}),
72+
t.partial({
73+
/**
74+
* The number of millisats actually paid to this invoice, this may be greater
75+
* than the amount requested by the invoice, since lightning allows overpaying
76+
* (but not underpaying) invoices.
77+
*/
78+
amtPaidMsat: BigIntFromString,
79+
}),
80+
],
81+
'InvoiceInfo'
82+
);
83+
export type InvoiceInfo = t.TypeOf<typeof InvoiceInfo>;
84+
85+
export const InvoiceQuery = t.partial(
86+
{
87+
status: InvoiceStatus,
88+
limit: BigIntFromString,
89+
startDate: DateFromISOString,
90+
endDate: DateFromISOString,
91+
},
92+
'InvoiceQuery'
93+
);
94+
95+
export type InvoiceQuery = t.TypeOf<typeof InvoiceQuery>;
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
import * as t from 'io-ts';
2+
import { BigIntFromString } from 'io-ts-types/BigIntFromString';
3+
import { DateFromISOString } from 'io-ts-types/DateFromISOString';
4+
5+
// codecs for lightning wallet payment related apis
6+
7+
export const PaymentStatus = t.union([
8+
// Initial state when payment is initiated.
9+
// Transitions to 'settled' or 'failed' on LND notification.
10+
t.literal('in_flight'),
11+
// Final state.
12+
t.literal('settled'),
13+
// Final state.
14+
t.literal('failed'),
15+
]);
16+
export type PaymentStatus = t.TypeOf<typeof PaymentStatus>;
17+
18+
export const PAYMENT_FAILURE_TIMEOUT = 'TIMEOUT';
19+
export const PAYMENT_FAILURE_NO_ROUTE = 'NO_ROUTE';
20+
/** A non-recoverable error */
21+
export const PAYMENT_FAILURE_ERROR = 'ERROR';
22+
export const PAYMENT_FAILURE_INCORRECT_DETAILS = 'INCORRECT_PAYMENT_DETAILS';
23+
/** Insufficient channel outbound capacity */
24+
export const PAYMENT_FAILURE_INSUFFICIENT_CHANNEL_BALANCE = 'INSUFFICIENT_BALANCE';
25+
/** Insufficient custodial lightning balance for the customer's wallet */
26+
export const PAYMENT_FAILURE_INSUFFICIENT_WALLET_BALANCE = 'INSUFFICIENT_WALLET_BALANCE';
27+
/** Excess custodial lightning balance for the customer's wallet */
28+
export const PAYMENT_FAILURE_EXCESS_WALLET_BALANCE = 'EXCESS_WALLET_BALANCE';
29+
export const PAYMENT_FAILURE_INVOICE_EXPIRED = 'INVOICE_EXPIRED';
30+
export const PAYMENT_FAILURE_CANCELED = 'CANCELED';
31+
32+
export const PaymentFailureReason = t.union([
33+
t.literal(PAYMENT_FAILURE_TIMEOUT),
34+
t.literal(PAYMENT_FAILURE_NO_ROUTE),
35+
t.literal(PAYMENT_FAILURE_ERROR),
36+
t.literal(PAYMENT_FAILURE_INCORRECT_DETAILS),
37+
t.literal(PAYMENT_FAILURE_INSUFFICIENT_CHANNEL_BALANCE),
38+
t.literal(PAYMENT_FAILURE_INSUFFICIENT_WALLET_BALANCE),
39+
t.literal(PAYMENT_FAILURE_EXCESS_WALLET_BALANCE),
40+
t.literal(PAYMENT_FAILURE_INVOICE_EXPIRED),
41+
t.literal(PAYMENT_FAILURE_CANCELED),
42+
]);
43+
44+
export type PaymentFailureReason = t.TypeOf<typeof PaymentFailureReason>;
45+
46+
/**
47+
* Off-chain payment information
48+
*/
49+
export const PaymentInfo = t.intersection(
50+
[
51+
t.type({
52+
paymentHash: t.string,
53+
walletId: t.string,
54+
txRequestId: t.string,
55+
status: PaymentStatus,
56+
invoice: t.string,
57+
feeLimitMsat: BigIntFromString,
58+
destination: t.string,
59+
updatedAt: DateFromISOString,
60+
createdAt: DateFromISOString,
61+
amountMsat: BigIntFromString,
62+
}),
63+
t.partial({
64+
feeMsat: BigIntFromString,
65+
failureReason: PaymentFailureReason,
66+
paymentPreimage: t.string,
67+
}),
68+
],
69+
'PaymentInfo'
70+
);
71+
72+
export type PaymentInfo = t.TypeOf<typeof PaymentInfo>;
73+
74+
/**
75+
* Payment query parameters
76+
*/
77+
export const PaymentQuery = t.partial(
78+
{
79+
status: PaymentStatus,
80+
limit: BigIntFromString,
81+
startDate: DateFromISOString,
82+
endDate: DateFromISOString,
83+
},
84+
'PaymentQuery'
85+
);
86+
export type PaymentQuery = t.TypeOf<typeof PaymentQuery>;
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
import * as t from 'io-ts';
2+
import { DateFromISOString } from 'io-ts-types/DateFromISOString';
3+
import { BigIntFromString } from 'io-ts-types/BigIntFromString';
4+
5+
// codecs for lightning wallet transaction related apis
6+
7+
/**
8+
* Transaction entry details
9+
*/
10+
export const TransactionEntry = t.intersection(
11+
[
12+
t.type({
13+
inputs: t.number,
14+
outputs: t.number,
15+
value: t.number,
16+
valueString: t.string,
17+
address: t.string,
18+
}),
19+
t.partial({
20+
wallet: t.string,
21+
}),
22+
],
23+
'TransactionEntry'
24+
);
25+
export type TransactionEntry = t.TypeOf<typeof TransactionEntry>;
26+
27+
/**
28+
* Transaction input/output details
29+
*/
30+
export const Output = t.intersection(
31+
[
32+
t.type({
33+
id: t.string,
34+
value: t.number,
35+
valueString: t.string,
36+
}),
37+
t.partial({
38+
wallet: t.string,
39+
address: t.string,
40+
}),
41+
],
42+
'Output'
43+
);
44+
export type Output = t.TypeOf<typeof Output>;
45+
46+
/**
47+
* Transaction details
48+
*/
49+
export const Transaction = t.intersection(
50+
[
51+
t.type({
52+
id: t.string,
53+
normalizedTxHash: t.string,
54+
blockHeight: t.number,
55+
blockHash: t.string,
56+
blockPosition: t.number,
57+
inputIds: t.array(t.string),
58+
entries: t.array(TransactionEntry),
59+
inputs: t.array(Output),
60+
outputs: t.array(Output),
61+
size: t.number,
62+
date: DateFromISOString,
63+
fee: t.number,
64+
feeString: t.string,
65+
hex: t.string,
66+
confirmations: t.number,
67+
}),
68+
t.partial({
69+
label: t.string,
70+
}),
71+
],
72+
'Transaction'
73+
);
74+
export type Transaction = t.TypeOf<typeof Transaction>;
75+
76+
/**
77+
* Transaction query parameters
78+
*/
79+
export const TransactionQuery = t.partial(
80+
{
81+
blockHeight: BigIntFromString,
82+
limit: BigIntFromString,
83+
startDate: DateFromISOString,
84+
endDate: DateFromISOString,
85+
},
86+
'TransactionQuery'
87+
);
88+
export type TransactionQuery = t.TypeOf<typeof TransactionQuery>;

0 commit comments

Comments
 (0)