From: Matt Corallo Date: Mon, 17 Jan 2022 02:55:58 +0000 (+0000) Subject: [TS] Update tests to build a funding tx and get to broadcast X-Git-Tag: v0.0.105.0~5^2~3 X-Git-Url: http://git.bitcoin.ninja/index.cgi?p=ldk-java;a=commitdiff_plain;h=d1d0121c000b713c10fd0bedd249eb8dda2e4db7 [TS] Update tests to build a funding tx and get to broadcast --- diff --git a/ts/test/tests.mts b/ts/test/tests.mts index b72703e7..751d7ddf 100644 --- a/ts/test/tests.mts +++ b/ts/test/tests.mts @@ -61,9 +61,12 @@ function get_chanman() { return 253; } } as ldk.FeeEstimatorInterface); - const tx_broadcaster = ldk.BroadcasterInterface.new_impl({ - broadcast_transaction(tx: Uint8Array): void { console.log("Tx Broadcast: " + tx); } - } as ldk.BroadcasterInterfaceInterface); + var tx_broadcaster; + const tx_broadcasted: Promise = new Promise((resolve, reject) => { + tx_broadcaster = ldk.BroadcasterInterface.new_impl({ + broadcast_transaction(tx: Uint8Array): void { console.log("Tx Broadcast: " + tx); resolve(tx); } + } as ldk.BroadcasterInterfaceInterface); + }); const logger = ldk.Logger.new_impl({ log(record: ldk.Record): void { console.log(record.get_module_path() + ": " + record.get_args()); @@ -90,7 +93,7 @@ function get_chanman() { const config = ldk.UserConfig.constructor_default(); const params = ldk.ChainParameters.constructor_new(ldk.Network.LDKNetwork_Testnet, ldk.BestBlock.constructor_from_genesis(ldk.Network.LDKNetwork_Testnet)); - return ldk.ChannelManager.constructor_new(fee_est, chain_watch, tx_broadcaster, logger, keys_interface, config, params); + return [ldk.ChannelManager.constructor_new(fee_est, chain_watch, tx_broadcaster, logger, keys_interface, config, params), tx_broadcasted]; } function exchange_messages(a: ldk.ChannelManager, b: ldk.ChannelManager) { @@ -110,6 +113,12 @@ function exchange_messages(a: ldk.ChannelManager, b: ldk.ChannelManager) { } else if (msg instanceof ldk.MessageSendEvent_SendAcceptChannel) { if (!array_eq(msg.node_id, to.get_our_node_id())) return false; to.as_ChannelMessageHandler().handle_accept_channel(from.get_our_node_id(), ldk.InitFeatures.constructor_known(), msg.msg); + } else if (msg instanceof ldk.MessageSendEvent_SendFundingCreated) { + if (!array_eq(msg.node_id, to.get_our_node_id())) return false; + to.as_ChannelMessageHandler().handle_funding_created(from.get_our_node_id(), msg.msg); + } else if (msg instanceof ldk.MessageSendEvent_SendFundingSigned) { + if (!array_eq(msg.node_id, to.get_our_node_id())) return false; + to.as_ChannelMessageHandler().handle_funding_signed(from.get_our_node_id(), msg.msg); } else { return false; } @@ -119,9 +128,22 @@ function exchange_messages(a: ldk.ChannelManager, b: ldk.ChannelManager) { return true; } +function assign_u64(arr: Uint8Array, offset: number, value: bigint) { + arr[offset + 0] = Number((value >> BigInt(8 * 0)) & BigInt(0xff)); + arr[offset + 1] = Number((value >> BigInt(8 * 1)) & BigInt(0xff)); + arr[offset + 2] = Number((value >> BigInt(8 * 2)) & BigInt(0xff)); + arr[offset + 3] = Number((value >> BigInt(8 * 3)) & BigInt(0xff)); + arr[offset + 4] = Number((value >> BigInt(8 * 4)) & BigInt(0xff)); + arr[offset + 5] = Number((value >> BigInt(8 * 5)) & BigInt(0xff)); + arr[offset + 6] = Number((value >> BigInt(8 * 6)) & BigInt(0xff)); + arr[offset + 7] = Number((value >> BigInt(8 * 7)) & BigInt(0xff)); +} + tests.push(async () => { - const chan_man_a = get_chanman(); - const chan_man_b = get_chanman(); + const peer_a = get_chanman(); + const peer_b = get_chanman(); + const chan_man_a: ldk.ChannelManager = peer_a[0] as ldk.ChannelManager; + const chan_man_b: ldk.ChannelManager = peer_b[0] as ldk.ChannelManager; chan_man_a.as_ChannelMessageHandler().peer_connected(chan_man_b.get_our_node_id(), ldk.Init.constructor_new(ldk.InitFeatures.constructor_known())); 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 () => { if (events.length != 1) return false; if (!(events[0] instanceof ldk.Event_FundingGenerationReady)) return false; + // (very) manually create a funding transaction + const witness_pos = events[0].output_script.length + 58; + const funding_tx = new Uint8Array(witness_pos + 7); + funding_tx[0] = 2; // 4-byte tx version 2 + funding_tx[4] = 0; funding_tx[5] = 1; // segwit magic bytes + funding_tx[6] = 1; // 1-byte input count 1 + // 36 bytes previous outpoint all-0s + funding_tx[43] = 0; // 1-byte input script length 0 + funding_tx[44] = 0xff; funding_tx[45] = 0xff; funding_tx[46] = 0xff; funding_tx[47] = 0xff; // 4-byte nSequence + funding_tx[48] = 1; // one output + assign_u64(funding_tx, 49, events[0].channel_value_satoshis); + funding_tx[57] = events[0].output_script.length; // 1-byte output script length + funding_tx.set(events[0].output_script, 58); + 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 + 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 + + const funding_res = chan_man_a.funding_transaction_generated(events[0].temporary_channel_id, funding_tx); + if (!(funding_res instanceof ldk.Result_NoneAPIErrorZ_OK)) return false; + + if (!exchange_messages(chan_man_a, chan_man_b)) return false; + + const tx_broadcasted: Uint8Array = (await peer_a[1]) as Uint8Array; + if (!array_eq(tx_broadcasted, funding_tx)) return false; + return true; });