lightning
Version:
Lightning Network client library
307 lines (294 loc) • 8.73 kB
JavaScript
const {deepStrictEqual} = require('node:assert').strict;
const {rejects} = require('node:assert').strict;
const test = require('node:test');
const {getChannels} = require('./../../../');
const makeExpected = overrides => {
const expectedChannel = {
capacity: 1,
commit_transaction_fee: 1,
commit_transaction_weight: 1,
cooperative_close_address: 'cooperative_close_address',
cooperative_close_delay_height: undefined,
description: undefined,
id: '0x0x1',
is_active: true,
is_closing: false,
is_opening: false,
is_partner_initiated: false,
is_private: true,
is_trusted_funding: undefined,
local_balance: 1,
local_csv: 1,
local_dust: 1,
local_given: 0,
local_min_htlc_mtokens: '1',
local_max_htlcs: 1,
local_max_pending_mtokens: '1',
local_reserve: 1,
other_ids: [],
partner_public_key: 'b',
past_states: 1,
pending_payments: [{
id: '0000000000000000000000000000000000000000000000000000000000000000',
in_channel: undefined,
in_payment: undefined,
is_forward: undefined,
is_outgoing: false,
out_channel: undefined,
out_payment: undefined,
payment: undefined,
timeout: 1,
tokens: 1,
}],
received: 1,
remote_balance: 1,
remote_csv: 1,
remote_dust: 1,
remote_given: 0,
remote_max_htlcs: 1,
remote_max_pending_mtokens: '1',
remote_min_htlc_mtokens: '1',
remote_reserve: 1,
sent: 1,
time_offline: 0,
time_online: 1000,
transaction_id: '00',
transaction_vout: 1,
type: 'anchor',
unsettled_balance: 1,
};
Object.keys(overrides).forEach(k => expectedChannel[k] = overrides[k]);
return expectedChannel;
};
const makeLnd = overrides => {
const channel = {
active: true,
alias_scids: [],
capacity: 1,
chan_id: '1',
channel_point: '00:1',
close_address: 'cooperative_close_address',
commit_fee: '1',
commit_weight: '1',
commitment_type: 'ANCHORS',
fee_per_kw: '1',
initiator: true,
lifetime: 1,
local_balance: '1',
local_chan_reserve_sat: '1',
local_constraints: {
chan_reserve_sat: '1',
csv_delay: 1,
dust_limit_sat: '1',
max_accepted_htlcs: 1,
max_pending_amt_msat: '1',
min_htlc_msat: '1',
},
num_updates: 1,
pending_htlcs: [{
amount: '1',
expiration_height: 1,
hash_lock: Buffer.alloc(32),
incoming: true,
}],
private: true,
remote_balance: 1,
remote_chan_reserve_sat: '1',
remote_constraints: {
chan_reserve_sat: '1',
csv_delay: 1,
dust_limit_sat: '1',
max_accepted_htlcs: 1,
max_pending_amt_msat: '1',
min_htlc_msat: '1',
},
remote_pubkey: 'b',
thaw_height: 0,
total_satoshis_received: 1,
total_satoshis_sent: 1,
unsettled_balance: 1,
uptime: 1,
};
Object.keys(overrides).forEach(key => channel[key] = overrides[key]);
return {
default: {listChannels: ({}, cbk) => cbk(null, {channels: [channel]})},
};
};
const tests = [
{
args: {},
description: 'LND is required',
error: [400, 'ExpectedLndToGetChannels'],
},
{
args: {lnd: {default: {listChannels: ({}, cbk) => cbk('err')}}},
description: 'List channel errors are passed back',
error: [503, 'UnexpectedGetChannelsError', {err: 'err'}],
},
{
args: {lnd: {default: {listChannels: ({}, cbk) => cbk()}}},
description: 'A response is expected',
error: [503, 'ExpectedChannelsArray'],
},
{
args: {lnd: {default: {listChannels: ({}, cbk) => cbk(null, {})}}},
description: 'A response is expected',
error: [503, 'ExpectedChannelsArray'],
},
{
args: {lnd: {default: {listChannels: ({}, cbk) => cbk(null, {})}}},
description: 'A response is expected',
error: [503, 'ExpectedChannelsArray'],
},
{
args: {lnd: makeLnd({active: undefined})},
description: 'An active state is expected',
error: [503, 'ExpectedChannelActiveStateInChannelMessage'],
},
{
args: {lnd: makeLnd({capacity: undefined})},
description: 'Channel capacity is expected',
error: [503, 'ExpectedChannelCapacityInChannelMessage'],
},
{
args: {lnd: makeLnd({chan_id: undefined})},
description: 'Channel id is expected',
error: [503, 'ExpectedChannelIdNumberInChannelsList'],
},
{
args: {lnd: makeLnd({channel_point: ''})},
description: 'Channel funding outpoint is expected',
error: [503, 'ExpectedChannelPointInChannelMessage'],
},
{
args: {lnd: makeLnd({commit_fee: undefined})},
description: 'Channel commit fee is expected',
error: [503, 'ExpectedCommitFeeInChannelMessage'],
},
{
args: {lnd: makeLnd({commit_weight: undefined})},
description: 'Channel commit weight is expected',
error: [503, 'ExpectedCommitWeightInChannelMessage'],
},
{
args: {lnd: makeLnd({commitment_type: undefined})},
description: 'Channel commit type is expected',
error: [503, 'ExpectedChannelCommitmentTypeInChannelMessage'],
},
{
args: {lnd: makeLnd({fee_per_kw: undefined})},
description: 'Channel fee per kw is expected',
error: [503, 'ExpectedFeePerKwInChannelMessage'],
},
{
args: {lnd: makeLnd({local_balance: undefined})},
description: 'Local balance is expected',
error: [503, 'ExpectedLocalBalanceInChannelMessage'],
},
{
args: {lnd: makeLnd({local_chan_reserve_sat: undefined})},
description: 'Local chan reserve is expected',
error: [503, 'ExpectedLocalChannelReserveAmountInChannelMessage'],
},
{
args: {lnd: makeLnd({num_updates: undefined})},
description: 'Number of updates is expected',
error: [503, 'ExpectedNumUpdatesInChannelMessage'],
},
{
args: {lnd: makeLnd({pending_htlcs: undefined})},
description: 'Pending HTLCs is expected',
error: [503, 'ExpectedChannelPendingHtlcsInChannelMessage'],
},
{
args: {lnd: makeLnd({private: undefined})},
description: 'Private status is expected',
error: [503, 'ExpectedChannelPrivateStatusInChannelMessage'],
},
{
args: {lnd: makeLnd({remote_balance: undefined})},
description: 'Remote balance is expected',
error: [503, 'ExpectedRemoteBalanceInChannelMessage'],
},
{
args: {lnd: makeLnd({remote_chan_reserve_sat: undefined})},
description: 'Remote channel reserve amount is expected',
error: [503, 'ExpectedRemoteChannelReserveAmountInChannelMessage'],
},
{
args: {lnd: makeLnd({remote_pubkey: undefined})},
description: 'Remote public key is expected',
error: [503, 'ExpectedRemotePubkeyInChannelMessage'],
},
{
args: {lnd: makeLnd({total_satoshis_received: undefined})},
description: 'Total satoshis received is expected',
error: [503, 'ExpectedTotalSatoshisReceivedInChannelMessage'],
},
{
args: {lnd: makeLnd({total_satoshis_sent: undefined})},
description: 'Total satoshis sent is expected',
error: [503, 'ExpectedTotalSatoshisSentInChannelMessage'],
},
{
args: {lnd: makeLnd({unsettled_balance: undefined})},
description: 'Unsettled balance is expected',
error: [503, 'ExpectedUnsettledBalanceInChannelMessage'],
},
{
args: {lnd: makeLnd({})},
description: 'Channels are returned',
expected: {channel: makeExpected({})},
},
{
args: {
lnd: makeLnd({
commitment_type: 'ANCHORS',
thaw_height: 1,
}),
},
description: 'Channels are returned when relative thaw height specified',
expected: {
channel: makeExpected({
cooperative_close_delay_height: 1,
}),
},
},
{
args: {lnd: makeLnd({commitment_type: 'ANCHORS', thaw_height: 5e5})},
description: 'Channels are returned when a static thaw height specified',
expected: {
channel: makeExpected({
cooperative_close_delay_height: 5e5,
}),
},
},
{
args: {
is_active: true,
is_offline: true,
is_private: true,
is_public: true,
lnd: makeLnd({}),
},
description: 'Restrictions are used',
expected: {channel: makeExpected({})},
},
{
args: {lnd: makeLnd({}), partner_public_key: 'b'},
description: 'Channels are returned when partner public key is specified',
expected: {channel: makeExpected({})},
},
];
tests.forEach(({args, description, error, expected}) => {
return test(description, async () => {
if (!!error) {
await rejects(() => getChannels(args), error, 'Got expected error');
} else {
const {channels} = await getChannels(args);
const [channel] = channels;
deepStrictEqual(channel, expected.channel, 'Got expected channel');
}
return;
});
});