@@ -38,6 +38,8 @@ use bitcoin::hash_types::{Txid, BlockHash};
38
38
use core:: marker:: Sized ;
39
39
use core:: time:: Duration ;
40
40
use crate :: ln:: msgs:: DecodeError ;
41
+ #[ cfg( taproot) ]
42
+ use crate :: ln:: msgs:: PartialSignatureWithNonce ;
41
43
use crate :: ln:: { PaymentPreimage , PaymentHash , PaymentSecret } ;
42
44
43
45
use crate :: util:: byte_utils:: { be48_to_array, slice_to_be48} ;
@@ -574,6 +576,7 @@ impl_array!(16); // for IPv6
574
576
impl_array ! ( 32 ) ; // for channel id & hmac
575
577
impl_array ! ( PUBLIC_KEY_SIZE ) ; // for PublicKey
576
578
impl_array ! ( 64 ) ; // for ecdsa::Signature and schnorr::Signature
579
+ impl_array ! ( 66 ) ; // for MuSig2 nonces
577
580
impl_array ! ( 1300 ) ; // for OnionPacket.hop_data
578
581
579
582
impl Writeable for [ u16 ; 8 ] {
@@ -861,6 +864,39 @@ impl Readable for SecretKey {
861
864
}
862
865
}
863
866
867
+ #[ cfg( taproot) ]
868
+ impl Writeable for musig2:: types:: PublicNonce {
869
+ fn write < W : Writer > ( & self , w : & mut W ) -> Result < ( ) , io:: Error > {
870
+ self . serialize ( ) . write ( w)
871
+ }
872
+ }
873
+
874
+ #[ cfg( taproot) ]
875
+ impl Readable for musig2:: types:: PublicNonce {
876
+ fn read < R : Read > ( r : & mut R ) -> Result < Self , DecodeError > {
877
+ let buf: [ u8 ; PUBLIC_KEY_SIZE * 2 ] = Readable :: read ( r) ?;
878
+ musig2:: types:: PublicNonce :: from_slice ( & buf) . map_err ( |_| DecodeError :: InvalidValue )
879
+ }
880
+ }
881
+
882
+ #[ cfg( taproot) ]
883
+ impl Writeable for PartialSignatureWithNonce {
884
+ fn write < W : Writer > ( & self , w : & mut W ) -> Result < ( ) , io:: Error > {
885
+ self . 0 . serialize ( ) . write ( w) ?;
886
+ self . 1 . write ( w)
887
+ }
888
+ }
889
+
890
+ #[ cfg( taproot) ]
891
+ impl Readable for PartialSignatureWithNonce {
892
+ fn read < R : Read > ( r : & mut R ) -> Result < Self , DecodeError > {
893
+ let partial_signature_buf: [ u8 ; SECRET_KEY_SIZE ] = Readable :: read ( r) ?;
894
+ let partial_signature = musig2:: types:: PartialSignature :: from_slice ( & partial_signature_buf) . map_err ( |_| DecodeError :: InvalidValue ) ?;
895
+ let public_nonce: musig2:: types:: PublicNonce = Readable :: read ( r) ?;
896
+ Ok ( PartialSignatureWithNonce ( partial_signature, public_nonce) )
897
+ }
898
+ }
899
+
864
900
impl Writeable for Sha256dHash {
865
901
fn write < W : Writer > ( & self , w : & mut W ) -> Result < ( ) , io:: Error > {
866
902
w. write_all ( & self [ ..] )
@@ -1251,6 +1287,7 @@ impl Readable for Duration {
1251
1287
#[ cfg( test) ]
1252
1288
mod tests {
1253
1289
use core:: convert:: TryFrom ;
1290
+ use bitcoin:: secp256k1:: ecdsa;
1254
1291
use crate :: util:: ser:: { Readable , Hostname , Writeable } ;
1255
1292
1256
1293
#[ test]
@@ -1273,11 +1310,22 @@ mod tests {
1273
1310
assert_eq ! ( Hostname :: read( & mut buf. as_slice( ) ) . unwrap( ) . as_str( ) , "test" ) ;
1274
1311
}
1275
1312
1313
+ #[ test]
1314
+ /// Taproot will likely fill legacy signature fields with all 0s.
1315
+ /// This test ensures that doing so won't break serialization.
1316
+ fn null_signature_codec ( ) {
1317
+ let buffer = vec ! [ 0u8 ; 64 ] ;
1318
+ let mut cursor = crate :: io:: Cursor :: new ( buffer. clone ( ) ) ;
1319
+ let signature = ecdsa:: Signature :: read ( & mut cursor) . unwrap ( ) ;
1320
+ let serialization = signature. serialize_compact ( ) ;
1321
+ assert_eq ! ( buffer, serialization. to_vec( ) )
1322
+ }
1323
+
1276
1324
#[ test]
1277
1325
fn bigsize_encoding_decoding ( ) {
1278
1326
let values = vec ! [ 0 , 252 , 253 , 65535 , 65536 , 4294967295 , 4294967296 , 18446744073709551615 ] ;
1279
1327
let bytes = vec ! [
1280
- "00" ,
1328
+ "00" ,
1281
1329
"fc" ,
1282
1330
"fd00fd" ,
1283
1331
"fdffff" ,
@@ -1286,7 +1334,7 @@ mod tests {
1286
1334
"ff0000000100000000" ,
1287
1335
"ffffffffffffffffff"
1288
1336
] ;
1289
- for i in 0 ..=7 {
1337
+ for i in 0 ..=7 {
1290
1338
let mut stream = crate :: io:: Cursor :: new ( :: hex:: decode ( bytes[ i] ) . unwrap ( ) ) ;
1291
1339
assert_eq ! ( super :: BigSize :: read( & mut stream) . unwrap( ) . 0 , values[ i] ) ;
1292
1340
let mut stream = super :: VecWriter ( Vec :: new ( ) ) ;
0 commit comments