@@ -684,8 +684,20 @@ impl MsgEncodable for ClosingSigned {
684
684
}
685
685
686
686
impl MsgDecodable for UpdateAddHTLC {
687
- fn decode ( _v : & [ u8 ] ) -> Result < Self , DecodeError > {
688
- unimplemented ! ( ) ;
687
+ fn decode ( v : & [ u8 ] ) -> Result < Self , DecodeError > {
688
+ if v. len ( ) < 32 +8 +8 +32 +4 +1 +33 +20 * 65 +32 {
689
+ return Err ( DecodeError :: WrongLength ) ;
690
+ }
691
+ let mut payment_hash = [ 0 ; 32 ] ;
692
+ payment_hash. copy_from_slice ( & v[ 48 ..80 ] ) ;
693
+ Ok ( Self {
694
+ channel_id : deserialize ( & v[ 0 ..32 ] ) . unwrap ( ) ,
695
+ htlc_id : byte_utils:: slice_to_be64 ( & v[ 32 ..40 ] ) ,
696
+ amount_msat : byte_utils:: slice_to_be64 ( & v[ 40 ..48 ] ) ,
697
+ payment_hash,
698
+ cltv_expiry : byte_utils:: slice_to_be32 ( & v[ 80 ..84 ] ) ,
699
+ onion_routing_packet : OnionPacket :: decode ( & v[ 84 ..] ) ?,
700
+ } )
689
701
}
690
702
}
691
703
impl MsgEncodable for UpdateAddHTLC {
@@ -695,8 +707,17 @@ impl MsgEncodable for UpdateAddHTLC {
695
707
}
696
708
697
709
impl MsgDecodable for UpdateFulfillHTLC {
698
- fn decode ( _v : & [ u8 ] ) -> Result < Self , DecodeError > {
699
- unimplemented ! ( ) ;
710
+ fn decode ( v : & [ u8 ] ) -> Result < Self , DecodeError > {
711
+ if v. len ( ) < 32 +8 +32 {
712
+ return Err ( DecodeError :: WrongLength ) ;
713
+ }
714
+ let mut payment_preimage = [ 0 ; 32 ] ;
715
+ payment_preimage. copy_from_slice ( & v[ 40 ..72 ] ) ;
716
+ Ok ( Self {
717
+ channel_id : deserialize ( & v[ 0 ..32 ] ) . unwrap ( ) ,
718
+ htlc_id : byte_utils:: slice_to_be64 ( & v[ 32 ..40 ] ) ,
719
+ payment_preimage,
720
+ } )
700
721
}
701
722
}
702
723
impl MsgEncodable for UpdateFulfillHTLC {
@@ -706,8 +727,15 @@ impl MsgEncodable for UpdateFulfillHTLC {
706
727
}
707
728
708
729
impl MsgDecodable for UpdateFailHTLC {
709
- fn decode ( _v : & [ u8 ] ) -> Result < Self , DecodeError > {
710
- unimplemented ! ( ) ;
730
+ fn decode ( v : & [ u8 ] ) -> Result < Self , DecodeError > {
731
+ if v. len ( ) < 32 +8 {
732
+ return Err ( DecodeError :: WrongLength ) ;
733
+ }
734
+ Ok ( Self {
735
+ channel_id : deserialize ( & v[ 0 ..32 ] ) . unwrap ( ) ,
736
+ htlc_id : byte_utils:: slice_to_be64 ( & v[ 32 ..40 ] ) ,
737
+ reason : OnionErrorPacket :: decode ( & v[ 40 ..] ) ?,
738
+ } )
711
739
}
712
740
}
713
741
impl MsgEncodable for UpdateFailHTLC {
@@ -717,8 +745,18 @@ impl MsgEncodable for UpdateFailHTLC {
717
745
}
718
746
719
747
impl MsgDecodable for UpdateFailMalformedHTLC {
720
- fn decode ( _v : & [ u8 ] ) -> Result < Self , DecodeError > {
721
- unimplemented ! ( ) ;
748
+ fn decode ( v : & [ u8 ] ) -> Result < Self , DecodeError > {
749
+ if v. len ( ) < 32 +8 +32 +2 {
750
+ return Err ( DecodeError :: WrongLength ) ;
751
+ }
752
+ let mut sha256_of_onion = [ 0 ; 32 ] ;
753
+ sha256_of_onion. copy_from_slice ( & v[ 40 ..72 ] ) ;
754
+ Ok ( Self {
755
+ channel_id : deserialize ( & v[ 0 ..32 ] ) . unwrap ( ) ,
756
+ htlc_id : byte_utils:: slice_to_be64 ( & v[ 32 ..40 ] ) ,
757
+ sha256_of_onion,
758
+ failure_code : byte_utils:: slice_to_be16 ( & v[ 72 ..74 ] ) ,
759
+ } )
722
760
}
723
761
}
724
762
impl MsgEncodable for UpdateFailMalformedHTLC {
@@ -728,8 +766,24 @@ impl MsgEncodable for UpdateFailMalformedHTLC {
728
766
}
729
767
730
768
impl MsgDecodable for CommitmentSigned {
731
- fn decode ( _v : & [ u8 ] ) -> Result < Self , DecodeError > {
732
- unimplemented ! ( ) ;
769
+ fn decode ( v : & [ u8 ] ) -> Result < Self , DecodeError > {
770
+ if v. len ( ) < 32 +64 +2 {
771
+ return Err ( DecodeError :: WrongLength ) ;
772
+ }
773
+ let htlcs = byte_utils:: slice_to_be16 ( & v[ 96 ..98 ] ) as usize ;
774
+ if v. len ( ) < 32 +64 +2 +htlcs* 64 {
775
+ return Err ( DecodeError :: WrongLength ) ;
776
+ }
777
+ let mut htlc_signatures = Vec :: with_capacity ( htlcs) ;
778
+ let secp_ctx = Secp256k1 :: without_caps ( ) ;
779
+ for i in 0 ..htlcs {
780
+ htlc_signatures. push ( secp_signature ! ( & secp_ctx, & v[ 98 +i* 64 ..98 +( i+1 ) * 64 ] ) ) ;
781
+ }
782
+ Ok ( Self {
783
+ channel_id : deserialize ( & v[ 0 ..32 ] ) . unwrap ( ) ,
784
+ signature : secp_signature ! ( & secp_ctx, & v[ 32 ..96 ] ) ,
785
+ htlc_signatures,
786
+ } )
733
787
}
734
788
}
735
789
impl MsgEncodable for CommitmentSigned {
@@ -739,8 +793,18 @@ impl MsgEncodable for CommitmentSigned {
739
793
}
740
794
741
795
impl MsgDecodable for RevokeAndACK {
742
- fn decode ( _v : & [ u8 ] ) -> Result < Self , DecodeError > {
743
- unimplemented ! ( ) ;
796
+ fn decode ( v : & [ u8 ] ) -> Result < Self , DecodeError > {
797
+ if v. len ( ) < 32 +32 +33 {
798
+ return Err ( DecodeError :: WrongLength ) ;
799
+ }
800
+ let mut per_commitment_secret = [ 0 ; 32 ] ;
801
+ per_commitment_secret. copy_from_slice ( & v[ 32 ..64 ] ) ;
802
+ let secp_ctx = Secp256k1 :: without_caps ( ) ;
803
+ Ok ( Self {
804
+ channel_id : deserialize ( & v[ 0 ..32 ] ) . unwrap ( ) ,
805
+ per_commitment_secret,
806
+ next_per_commitment_point : secp_pubkey ! ( & secp_ctx, & v[ 64 ..97 ] ) ,
807
+ } )
744
808
}
745
809
}
746
810
impl MsgEncodable for RevokeAndACK {
@@ -750,8 +814,14 @@ impl MsgEncodable for RevokeAndACK {
750
814
}
751
815
752
816
impl MsgDecodable for UpdateFee {
753
- fn decode ( _v : & [ u8 ] ) -> Result < Self , DecodeError > {
754
- unimplemented ! ( ) ;
817
+ fn decode ( v : & [ u8 ] ) -> Result < Self , DecodeError > {
818
+ if v. len ( ) < 32 +4 {
819
+ return Err ( DecodeError :: WrongLength ) ;
820
+ }
821
+ Ok ( Self {
822
+ channel_id : deserialize ( & v[ 0 ..32 ] ) . unwrap ( ) ,
823
+ feerate_per_kw : byte_utils:: slice_to_be32 ( & v[ 32 ..36 ] ) ,
824
+ } )
755
825
}
756
826
}
757
827
impl MsgEncodable for UpdateFee {
@@ -954,8 +1024,21 @@ impl MsgEncodable for OnionHopData {
954
1024
}
955
1025
956
1026
impl MsgDecodable for OnionPacket {
957
- fn decode ( _v : & [ u8 ] ) -> Result < Self , DecodeError > {
958
- unimplemented ! ( ) ;
1027
+ fn decode ( v : & [ u8 ] ) -> Result < Self , DecodeError > {
1028
+ if v. len ( ) < 1 +33 +20 * 65 +32 {
1029
+ return Err ( DecodeError :: WrongLength ) ;
1030
+ }
1031
+ let mut hop_data = [ 0 ; 20 * 65 ] ;
1032
+ hop_data. copy_from_slice ( & v[ 34 ..1334 ] ) ;
1033
+ let mut hmac = [ 0 ; 32 ] ;
1034
+ hmac. copy_from_slice ( & v[ 1334 ..1366 ] ) ;
1035
+ let secp_ctx = Secp256k1 :: without_caps ( ) ;
1036
+ Ok ( Self {
1037
+ version : v[ 0 ] ,
1038
+ public_key : secp_pubkey ! ( & secp_ctx, & v[ 1 ..34 ] ) ,
1039
+ hop_data,
1040
+ hmac,
1041
+ } )
959
1042
}
960
1043
}
961
1044
impl MsgEncodable for OnionPacket {
@@ -987,8 +1070,17 @@ impl MsgEncodable for DecodedOnionErrorPacket {
987
1070
}
988
1071
989
1072
impl MsgDecodable for OnionErrorPacket {
990
- fn decode ( _v : & [ u8 ] ) -> Result < Self , DecodeError > {
991
- unimplemented ! ( ) ;
1073
+ fn decode ( v : & [ u8 ] ) -> Result < Self , DecodeError > {
1074
+ if v. len ( ) < 2 {
1075
+ return Err ( DecodeError :: WrongLength ) ;
1076
+ }
1077
+ let len = byte_utils:: slice_to_be16 ( & v[ 0 ..2 ] ) as usize ;
1078
+ if v. len ( ) < 2 + len {
1079
+ return Err ( DecodeError :: WrongLength ) ;
1080
+ }
1081
+ Ok ( Self {
1082
+ data : v[ 2 ..len+2 ] . to_vec ( ) ,
1083
+ } )
992
1084
}
993
1085
}
994
1086
impl MsgEncodable for OnionErrorPacket {
0 commit comments