-impl Into<address::WitnessVersion> for WitnessVersion {
- fn into(self) -> address::WitnessVersion {
- address::WitnessVersion::try_from(self.0).expect("WitnessVersion objects must be in the range 0..=16")
+impl WitnessProgram {
+ pub(crate) fn from_bitcoin(o: BitcoinWitnessProgram) -> Self {
+ Self {
+ version: o.version().into(),
+ program: o.program().as_bytes().to_vec().into(),
+ }
+ }
+ pub(crate) fn into_bitcoin(mut self) -> BitcoinWitnessProgram {
+ BitcoinWitnessProgram::new(
+ self.version.into(),
+ self.program.as_slice(),
+ ).expect("Program length was previously checked")
+ }
+}
+
+#[no_mangle]
+/// Constructs a new WitnessProgram given a version and program bytes.
+///
+/// The program MUST be at least 2 bytes and no longer than 40 bytes long.
+/// Further, if the version is 0, the program MUST be either exactly 20 or exactly 32 bytes long.
+pub extern "C" fn WitnessProgram_new(version: WitnessVersion, program: derived::CVec_u8Z) -> WitnessProgram {
+ assert!(program.datalen >= 2, "WitnessProgram program lengths must be at least 2 bytes long");
+ assert!(program.datalen <= 40, "WitnessProgram program lengths must be no longer than 40 bytes");
+ if version.0 == 0 {
+ assert!(program.datalen == 20 || program.datalen == 32, "WitnessProgram program length must be 20 or 32 for version-0 programs");