Skip to content

Commit d1d0121

Browse files
committed
[TS] Update tests to build a funding tx and get to broadcast
1 parent 071b5fa commit d1d0121

File tree

1 file changed

+52
-6
lines changed

1 file changed

+52
-6
lines changed

ts/test/tests.mts

Lines changed: 52 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -61,9 +61,12 @@ function get_chanman() {
6161
return 253;
6262
}
6363
} as ldk.FeeEstimatorInterface);
64-
const tx_broadcaster = ldk.BroadcasterInterface.new_impl({
65-
broadcast_transaction(tx: Uint8Array): void { console.log("Tx Broadcast: " + tx); }
66-
} as ldk.BroadcasterInterfaceInterface);
64+
var tx_broadcaster;
65+
const tx_broadcasted: Promise<Uint8Array> = new Promise((resolve, reject) => {
66+
tx_broadcaster = ldk.BroadcasterInterface.new_impl({
67+
broadcast_transaction(tx: Uint8Array): void { console.log("Tx Broadcast: " + tx); resolve(tx); }
68+
} as ldk.BroadcasterInterfaceInterface);
69+
});
6770
const logger = ldk.Logger.new_impl({
6871
log(record: ldk.Record): void {
6972
console.log(record.get_module_path() + ": " + record.get_args());
@@ -90,7 +93,7 @@ function get_chanman() {
9093
const config = ldk.UserConfig.constructor_default();
9194
const params = ldk.ChainParameters.constructor_new(ldk.Network.LDKNetwork_Testnet, ldk.BestBlock.constructor_from_genesis(ldk.Network.LDKNetwork_Testnet));
9295

93-
return ldk.ChannelManager.constructor_new(fee_est, chain_watch, tx_broadcaster, logger, keys_interface, config, params);
96+
return [ldk.ChannelManager.constructor_new(fee_est, chain_watch, tx_broadcaster, logger, keys_interface, config, params), tx_broadcasted];
9497
}
9598

9699
function exchange_messages(a: ldk.ChannelManager, b: ldk.ChannelManager) {
@@ -110,6 +113,12 @@ function exchange_messages(a: ldk.ChannelManager, b: ldk.ChannelManager) {
110113
} else if (msg instanceof ldk.MessageSendEvent_SendAcceptChannel) {
111114
if (!array_eq(msg.node_id, to.get_our_node_id())) return false;
112115
to.as_ChannelMessageHandler().handle_accept_channel(from.get_our_node_id(), ldk.InitFeatures.constructor_known(), msg.msg);
116+
} else if (msg instanceof ldk.MessageSendEvent_SendFundingCreated) {
117+
if (!array_eq(msg.node_id, to.get_our_node_id())) return false;
118+
to.as_ChannelMessageHandler().handle_funding_created(from.get_our_node_id(), msg.msg);
119+
} else if (msg instanceof ldk.MessageSendEvent_SendFundingSigned) {
120+
if (!array_eq(msg.node_id, to.get_our_node_id())) return false;
121+
to.as_ChannelMessageHandler().handle_funding_signed(from.get_our_node_id(), msg.msg);
113122
} else {
114123
return false;
115124
}
@@ -119,9 +128,22 @@ function exchange_messages(a: ldk.ChannelManager, b: ldk.ChannelManager) {
119128
return true;
120129
}
121130

131+
function assign_u64(arr: Uint8Array, offset: number, value: bigint) {
132+
arr[offset + 0] = Number((value >> BigInt(8 * 0)) & BigInt(0xff));
133+
arr[offset + 1] = Number((value >> BigInt(8 * 1)) & BigInt(0xff));
134+
arr[offset + 2] = Number((value >> BigInt(8 * 2)) & BigInt(0xff));
135+
arr[offset + 3] = Number((value >> BigInt(8 * 3)) & BigInt(0xff));
136+
arr[offset + 4] = Number((value >> BigInt(8 * 4)) & BigInt(0xff));
137+
arr[offset + 5] = Number((value >> BigInt(8 * 5)) & BigInt(0xff));
138+
arr[offset + 6] = Number((value >> BigInt(8 * 6)) & BigInt(0xff));
139+
arr[offset + 7] = Number((value >> BigInt(8 * 7)) & BigInt(0xff));
140+
}
141+
122142
tests.push(async () => {
123-
const chan_man_a = get_chanman();
124-
const chan_man_b = get_chanman();
143+
const peer_a = get_chanman();
144+
const peer_b = get_chanman();
145+
const chan_man_a: ldk.ChannelManager = peer_a[0] as ldk.ChannelManager;
146+
const chan_man_b: ldk.ChannelManager = peer_b[0] as ldk.ChannelManager;
125147

126148
chan_man_a.as_ChannelMessageHandler().peer_connected(chan_man_b.get_our_node_id(), ldk.Init.constructor_new(ldk.InitFeatures.constructor_known()));
127149
chan_man_b.as_ChannelMessageHandler().peer_connected(chan_man_a.get_our_node_id(), ldk.Init.constructor_new(ldk.InitFeatures.constructor_known()));
@@ -148,6 +170,30 @@ tests.push(async () => {
148170
if (events.length != 1) return false;
149171
if (!(events[0] instanceof ldk.Event_FundingGenerationReady)) return false;
150172

173+
// (very) manually create a funding transaction
174+
const witness_pos = events[0].output_script.length + 58;
175+
const funding_tx = new Uint8Array(witness_pos + 7);
176+
funding_tx[0] = 2; // 4-byte tx version 2
177+
funding_tx[4] = 0; funding_tx[5] = 1; // segwit magic bytes
178+
funding_tx[6] = 1; // 1-byte input count 1
179+
// 36 bytes previous outpoint all-0s
180+
funding_tx[43] = 0; // 1-byte input script length 0
181+
funding_tx[44] = 0xff; funding_tx[45] = 0xff; funding_tx[46] = 0xff; funding_tx[47] = 0xff; // 4-byte nSequence
182+
funding_tx[48] = 1; // one output
183+
assign_u64(funding_tx, 49, events[0].channel_value_satoshis);
184+
funding_tx[57] = events[0].output_script.length; // 1-byte output script length
185+
funding_tx.set(events[0].output_script, 58);
186+
funding_tx[witness_pos] = 1; funding_tx[witness_pos + 1] = 1; funding_tx[witness_pos + 2] = 0xff; // one witness element of size 1 with contents 0xff
187+
funding_tx[witness_pos + 3] = 0; funding_tx[witness_pos + 4] = 0; funding_tx[witness_pos + 5] = 0; funding_tx[witness_pos + 6] = 0; // lock time 0
188+
189+
const funding_res = chan_man_a.funding_transaction_generated(events[0].temporary_channel_id, funding_tx);
190+
if (!(funding_res instanceof ldk.Result_NoneAPIErrorZ_OK)) return false;
191+
192+
if (!exchange_messages(chan_man_a, chan_man_b)) return false;
193+
194+
const tx_broadcasted: Uint8Array = (await peer_a[1]) as Uint8Array;
195+
if (!array_eq(tx_broadcasted, funding_tx)) return false;
196+
151197
return true;
152198
});
153199

0 commit comments

Comments
 (0)