import * as crypto from "@node-lightning/crypto" ; import { expect } from "chai" ; import { OpenChannelMessage } from "../../lib/messages/OpenChannelMessage" ; import { Value , Script } from "@node-lightning/core" ; describe ( "OpenChannelMessage" , ( ) => { const fundingPubKey = Buffer . from ( "029510d1d05474cff7beadffa3175354b148ae6e571f42b893e2a0be33fff57dd0" , "hex" ) ; const revocationBasePoint = Buffer . from ( "03811ed4c385aff9b3084be61e66a8e6572e6b88a6835c83debfbdb70d7f5ad251" , "hex" ) ; const paymentBasePoint = Buffer . from ( "02b331b3a6c68de49a28797cb0715ca0955a8ecd56bea30426a6a92997488425ab" , "hex" ) ; const delayedPaymentBasePoint = Buffer . from ( "03a895e77d461a6bd13cc9d7c2e193de2fa778e06488e6da38340e4bfcf86f038e" , "hex" ) ; const htlcBasePoint = Buffer . from ( "02e572d67e4fa4337e6f8b69f156cf3631a2f46f380d22ff1d5a431e6aa7becba3" , "hex" ) ; const firstCommitmentPoint = Buffer . from ( "039889f60bebba5fd2ee18045b3e5e540b46d07acbc768ba8d9ba9e05458ca4d2a" , "hex" ) ; describe ( "serialize" , ( ) => { it ( "serializes private" , ( ) => { const instance = new OpenChannelMessage ( ) ; instance . chainHash = Buffer . from ( "43497fd7f826957108f4a30fd9cec3aeba79972084e90ead01ea330900000000" , "hex" ) ; instance . temporaryChannelId = Buffer . from ( "0000000000000000000000000000000000000000000000000000000000000000" , "hex" ) ; instance . fundingSatoshis = Value . fromSats ( 200000 ) ; instance . pushMsat = Value . fromMilliSats ( 2000 ) ; instance . dustLimitSatoshis = Value . fromSats ( 330 ) ; instance . maxHtlcValueInFlightMsat = Value . fromMilliSats ( 20000 ) ; instance . channelReserveSatoshis = Value . fromSats ( 2000 ) ; instance . htlcMinimumMsat = Value . fromMilliSats ( 200 ) ; instance . feeRatePerKw = Value . fromSats ( 1000 ) ; instance . toSelfDelay = 144 ; instance . maxAcceptedHtlcs = 30 ; instance . fundingPubKey = fundingPubKey ; instance . revocationBasePoint = revocationBasePoint ; instance . paymentBasePoint = paymentBasePoint ; instance . delayedPaymentBasePoint = delayedPaymentBasePoint ; instance . htlcBasePoint = htlcBasePoint ; instance . firstPerCommitmentPoint = firstCommitmentPoint ; instance . announceChannel = false ; expect ( instance . serialize ( ) . toString ( "hex" ) ) . to . equal ( "0020" + "43497fd7f826957108f4a30fd9cec3aeba79972084e90ead01ea330900000000" + "0000000000000000000000000000000000000000000000000000000000000000" + "0000000000030d40" + "00000000000007d0" + "000000000000014a" + "0000000000004e20" + "00000000000007d0" + "00000000000000c8" + "000003e8" + "0090" + "001e" + "029510d1d05474cff7beadffa3175354b148ae6e571f42b893e2a0be33fff57dd0" + "03811ed4c385aff9b3084be61e66a8e6572e6b88a6835c83debfbdb70d7f5ad251" + "02b331b3a6c68de49a28797cb0715ca0955a8ecd56bea30426a6a92997488425ab" + "03a895e77d461a6bd13cc9d7c2e193de2fa778e06488e6da38340e4bfcf86f038e" + "02e572d67e4fa4337e6f8b69f156cf3631a2f46f380d22ff1d5a431e6aa7becba3" + "039889f60bebba5fd2ee18045b3e5e540b46d07acbc768ba8d9ba9e05458ca4d2a" + "00" ) ; } ) ; it ( "serializes no TLV" , ( ) => { const instance = new OpenChannelMessage ( ) ; instance . chainHash = Buffer . from ( "43497fd7f826957108f4a30fd9cec3aeba79972084e90ead01ea330900000000" , "hex" ) ; instance . temporaryChannelId = Buffer . from ( "0000000000000000000000000000000000000000000000000000000000000000" , "hex" ) ; instance . fundingSatoshis = Value . fromSats ( 200000 ) ; instance . pushMsat = Value . fromMilliSats ( 2000 ) ; instance . dustLimitSatoshis = Value . fromSats ( 330 ) ; instance . maxHtlcValueInFlightMsat = Value . fromMilliSats ( 20000 ) ; instance . channelReserveSatoshis = Value . fromSats ( 2000 ) ; instance . htlcMinimumMsat = Value . fromMilliSats ( 200 ) ; instance . feeRatePerKw = Value . fromSats ( 1000 ) ; instance . toSelfDelay = 144 ; instance . maxAcceptedHtlcs = 30 ; instance . fundingPubKey = fundingPubKey ; instance . revocationBasePoint = revocationBasePoint ; instance . paymentBasePoint = paymentBasePoint ; instance . delayedPaymentBasePoint = delayedPaymentBasePoint ; instance . htlcBasePoint = htlcBasePoint ; instance . firstPerCommitmentPoint = firstCommitmentPoint ; instance . announceChannel = true ; expect ( instance . serialize ( ) . toString ( "hex" ) ) . to . equal ( "0020" + "43497fd7f826957108f4a30fd9cec3aeba79972084e90ead01ea330900000000" + "0000000000000000000000000000000000000000000000000000000000000000" + "0000000000030d40" + "00000000000007d0" + "000000000000014a" + "0000000000004e20" + "00000000000007d0" + "00000000000000c8" + "000003e8" + "0090" + "001e" + "029510d1d05474cff7beadffa3175354b148ae6e571f42b893e2a0be33fff57dd0" + "03811ed4c385aff9b3084be61e66a8e6572e6b88a6835c83debfbdb70d7f5ad251" + "02b331b3a6c68de49a28797cb0715ca0955a8ecd56bea30426a6a92997488425ab" + "03a895e77d461a6bd13cc9d7c2e193de2fa778e06488e6da38340e4bfcf86f038e" + "02e572d67e4fa4337e6f8b69f156cf3631a2f46f380d22ff1d5a431e6aa7becba3" + "039889f60bebba5fd2ee18045b3e5e540b46d07acbc768ba8d9ba9e05458ca4d2a" + "01" ) ; } ) ; it ( "serializes with TLV" , ( ) => { const instance = new OpenChannelMessage ( ) ; instance . chainHash = Buffer . from ( "43497fd7f826957108f4a30fd9cec3aeba79972084e90ead01ea330900000000" , "hex" ) ; instance . temporaryChannelId = Buffer . from ( "0000000000000000000000000000000000000000000000000000000000000000" , "hex" ) ; instance . fundingSatoshis = Value . fromSats ( 200000 ) ; instance . pushMsat = Value . fromMilliSats ( 2000 ) ; instance . dustLimitSatoshis = Value . fromSats ( 330 ) ; instance . maxHtlcValueInFlightMsat = Value . fromMilliSats ( 20000 ) ; instance . channelReserveSatoshis = Value . fromSats ( 2000 ) ; instance . htlcMinimumMsat = Value . fromMilliSats ( 200 ) ; instance . feeRatePerKw = Value . fromSats ( 1000 ) ; instance . toSelfDelay = 144 ; instance . maxAcceptedHtlcs = 30 ; instance . fundingPubKey = fundingPubKey ; instance . revocationBasePoint = revocationBasePoint ; instance . paymentBasePoint = paymentBasePoint ; instance . delayedPaymentBasePoint = delayedPaymentBasePoint ; instance . htlcBasePoint = htlcBasePoint ; instance . firstPerCommitmentPoint = firstCommitmentPoint ; instance . announceChannel = true ; instance . upfrontShutdownScript = Buffer . concat ( [ Buffer . from ( [ 0x00 ] ) , crypto . hash160 ( paymentBasePoint ) , ] ) ; expect ( instance . serialize ( ) . toString ( "hex" ) ) . to . equal ( "0020" + "43497fd7f826957108f4a30fd9cec3aeba79972084e90ead01ea330900000000" + "0000000000000000000000000000000000000000000000000000000000000000" + "0000000000030d40" + "00000000000007d0" + "000000000000014a" + "0000000000004e20" + "00000000000007d0" + "00000000000000c8" + "000003e8" + "0090" + "001e" + "029510d1d05474cff7beadffa3175354b148ae6e571f42b893e2a0be33fff57dd0" + "03811ed4c385aff9b3084be61e66a8e6572e6b88a6835c83debfbdb70d7f5ad251" + "02b331b3a6c68de49a28797cb0715ca0955a8ecd56bea30426a6a92997488425ab" + "03a895e77d461a6bd13cc9d7c2e193de2fa778e06488e6da38340e4bfcf86f038e" + "02e572d67e4fa4337e6f8b69f156cf3631a2f46f380d22ff1d5a431e6aa7becba3" + "039889f60bebba5fd2ee18045b3e5e540b46d07acbc768ba8d9ba9e05458ca4d2a" + "01" + "00" + "15" + "00a41a8527eab06efc0a8df57045d247784a071e23" ) ; } ) ; } ) ; describe ( "deserialize" , ( ) => { it ( "deserializes" , ( ) => { const buf = Buffer . from ( "0020" + "43497fd7f826957108f4a30fd9cec3aeba79972084e90ead01ea330900000000" + "0000000000000000000000000000000000000000000000000000000000000000" + "0000000000030d40" + "00000000000007d0" + "000000000000014a" + "0000000000004e20" + "00000000000007d0" + "00000000000000c8" + "000003e8" + "0090" + "001e" + "029510d1d05474cff7beadffa3175354b148ae6e571f42b893e2a0be33fff57dd0" + "03811ed4c385aff9b3084be61e66a8e6572e6b88a6835c83debfbdb70d7f5ad251" + "02b331b3a6c68de49a28797cb0715ca0955a8ecd56bea30426a6a92997488425ab" + "03a895e77d461a6bd13cc9d7c2e193de2fa778e06488e6da38340e4bfcf86f038e" + "02e572d67e4fa4337e6f8b69f156cf3631a2f46f380d22ff1d5a431e6aa7becba3" + "039889f60bebba5fd2ee18045b3e5e540b46d07acbc768ba8d9ba9e05458ca4d2a" + "01" + "00" + "15" + "00a41a8527eab06efc0a8df57045d247784a071e23" , "hex" ) ; const instance = OpenChannelMessage . deserialize ( buf ) ; expect ( instance . chainHash ) . to . deep . equal ( Buffer . from ( "43497fd7f826957108f4a30fd9cec3aeba79972084e90ead01ea330900000000" , "hex" ) ) ; expect ( instance . temporaryChannelId ) . to . deep . equal ( Buffer . from ( "0000000000000000000000000000000000000000000000000000000000000000" , "hex" ) ) ; expect ( instance . fundingSatoshis . sats ) . to . equal ( BigInt ( 200000 ) ) ; expect ( instance . pushMsat . msats ) . to . equal ( BigInt ( 2000 ) ) ; expect ( instance . dustLimitSatoshis . sats ) . to . equal ( BigInt ( 330 ) ) ; expect ( instance . maxHtlcValueInFlightMsat . msats ) . to . equal ( BigInt ( 20000 ) ) ; expect ( instance . channelReserveSatoshis . sats ) . to . equal ( BigInt ( 2000 ) ) ; expect ( instance . htlcMinimumMsat . msats ) . to . equal ( BigInt ( 200 ) ) ; expect ( Number ( instance . feeRatePerKw . sats ) ) . to . equal ( 1000 ) ; expect ( instance . toSelfDelay ) . to . equal ( 144 ) ; expect ( instance . maxAcceptedHtlcs ) . to . equal ( 30 ) ; expect ( instance . fundingPubKey ) . to . deep . equal ( fundingPubKey ) ; expect ( instance . revocationBasePoint ) . to . deep . equal ( revocationBasePoint ) ; expect ( instance . paymentBasePoint ) . to . deep . equal ( paymentBasePoint ) ; expect ( instance . delayedPaymentBasePoint ) . to . deep . equal ( delayedPaymentBasePoint ) ; expect ( instance . htlcBasePoint ) . to . deep . equal ( htlcBasePoint ) ; expect ( instance . firstPerCommitmentPoint ) . to . deep . equal ( firstCommitmentPoint ) ; expect ( instance . announceChannel ) . to . equal ( true ) ; expect ( instance . upfrontShutdownScript ) . to . deep . equal ( Buffer . concat ( [ Buffer . from ( [ 0x00 ] ) , crypto . hash160 ( paymentBasePoint ) ] ) ) ; } ) ; } ) ; } ) ;