|
1 | | -use miniscript::bitcoin::{psbt, Psbt, TxIn, TxOut}; |
| 1 | +use miniscript::bitcoin::{psbt, psbt::raw, Psbt, TxIn, TxOut}; |
2 | 2 |
|
3 | 3 | /// Shared accessor trait for types that wrap a `Psbt`. |
4 | 4 | /// |
@@ -27,6 +27,138 @@ pub trait PsbtAccess { |
27 | 27 | fn unsigned_tx_id(&self) -> String { |
28 | 28 | self.psbt().unsigned_tx.compute_txid().to_string() |
29 | 29 | } |
| 30 | + |
| 31 | + // ------------------------------------------------------------------------- |
| 32 | + // Global KV accessors |
| 33 | + // ------------------------------------------------------------------------- |
| 34 | + |
| 35 | + fn set_global_unknown_kv(&mut self, key: raw::Key, value: Vec<u8>) { |
| 36 | + self.psbt_mut().unknown.insert(key, value); |
| 37 | + } |
| 38 | + |
| 39 | + fn get_global_unknown_kv(&self, key: &raw::Key) -> Option<Vec<u8>> { |
| 40 | + self.psbt().unknown.get(key).cloned() |
| 41 | + } |
| 42 | + |
| 43 | + fn set_global_proprietary_kv(&mut self, key: raw::ProprietaryKey, value: Vec<u8>) { |
| 44 | + self.psbt_mut().proprietary.insert(key, value); |
| 45 | + } |
| 46 | + |
| 47 | + fn get_global_proprietary_kv(&self, key: &raw::ProprietaryKey) -> Option<Vec<u8>> { |
| 48 | + self.psbt().proprietary.get(key).cloned() |
| 49 | + } |
| 50 | + |
| 51 | + // ------------------------------------------------------------------------- |
| 52 | + // Per-input KV accessors |
| 53 | + // ------------------------------------------------------------------------- |
| 54 | + |
| 55 | + fn set_input_unknown_kv( |
| 56 | + &mut self, |
| 57 | + index: usize, |
| 58 | + key: raw::Key, |
| 59 | + value: Vec<u8>, |
| 60 | + ) -> Result<(), String> { |
| 61 | + let len = self.psbt().inputs.len(); |
| 62 | + if index >= len { |
| 63 | + return Err(format!("input index {index} out of bounds (have {len} inputs)")); |
| 64 | + } |
| 65 | + self.psbt_mut().inputs[index].unknown.insert(key, value); |
| 66 | + Ok(()) |
| 67 | + } |
| 68 | + |
| 69 | + fn get_input_unknown_kv( |
| 70 | + &self, |
| 71 | + index: usize, |
| 72 | + key: &raw::Key, |
| 73 | + ) -> Result<Option<Vec<u8>>, String> { |
| 74 | + let len = self.psbt().inputs.len(); |
| 75 | + if index >= len { |
| 76 | + return Err(format!("input index {index} out of bounds (have {len} inputs)")); |
| 77 | + } |
| 78 | + Ok(self.psbt().inputs[index].unknown.get(key).cloned()) |
| 79 | + } |
| 80 | + |
| 81 | + fn set_input_proprietary_kv( |
| 82 | + &mut self, |
| 83 | + index: usize, |
| 84 | + key: raw::ProprietaryKey, |
| 85 | + value: Vec<u8>, |
| 86 | + ) -> Result<(), String> { |
| 87 | + let len = self.psbt().inputs.len(); |
| 88 | + if index >= len { |
| 89 | + return Err(format!("input index {index} out of bounds (have {len} inputs)")); |
| 90 | + } |
| 91 | + self.psbt_mut().inputs[index].proprietary.insert(key, value); |
| 92 | + Ok(()) |
| 93 | + } |
| 94 | + |
| 95 | + fn get_input_proprietary_kv( |
| 96 | + &self, |
| 97 | + index: usize, |
| 98 | + key: &raw::ProprietaryKey, |
| 99 | + ) -> Result<Option<Vec<u8>>, String> { |
| 100 | + let len = self.psbt().inputs.len(); |
| 101 | + if index >= len { |
| 102 | + return Err(format!("input index {index} out of bounds (have {len} inputs)")); |
| 103 | + } |
| 104 | + Ok(self.psbt().inputs[index].proprietary.get(key).cloned()) |
| 105 | + } |
| 106 | + |
| 107 | + // ------------------------------------------------------------------------- |
| 108 | + // Per-output KV accessors |
| 109 | + // ------------------------------------------------------------------------- |
| 110 | + |
| 111 | + fn set_output_unknown_kv( |
| 112 | + &mut self, |
| 113 | + index: usize, |
| 114 | + key: raw::Key, |
| 115 | + value: Vec<u8>, |
| 116 | + ) -> Result<(), String> { |
| 117 | + let len = self.psbt().outputs.len(); |
| 118 | + if index >= len { |
| 119 | + return Err(format!("output index {index} out of bounds (have {len} outputs)")); |
| 120 | + } |
| 121 | + self.psbt_mut().outputs[index].unknown.insert(key, value); |
| 122 | + Ok(()) |
| 123 | + } |
| 124 | + |
| 125 | + fn get_output_unknown_kv( |
| 126 | + &self, |
| 127 | + index: usize, |
| 128 | + key: &raw::Key, |
| 129 | + ) -> Result<Option<Vec<u8>>, String> { |
| 130 | + let len = self.psbt().outputs.len(); |
| 131 | + if index >= len { |
| 132 | + return Err(format!("output index {index} out of bounds (have {len} outputs)")); |
| 133 | + } |
| 134 | + Ok(self.psbt().outputs[index].unknown.get(key).cloned()) |
| 135 | + } |
| 136 | + |
| 137 | + fn set_output_proprietary_kv( |
| 138 | + &mut self, |
| 139 | + index: usize, |
| 140 | + key: raw::ProprietaryKey, |
| 141 | + value: Vec<u8>, |
| 142 | + ) -> Result<(), String> { |
| 143 | + let len = self.psbt().outputs.len(); |
| 144 | + if index >= len { |
| 145 | + return Err(format!("output index {index} out of bounds (have {len} outputs)")); |
| 146 | + } |
| 147 | + self.psbt_mut().outputs[index].proprietary.insert(key, value); |
| 148 | + Ok(()) |
| 149 | + } |
| 150 | + |
| 151 | + fn get_output_proprietary_kv( |
| 152 | + &self, |
| 153 | + index: usize, |
| 154 | + key: &raw::ProprietaryKey, |
| 155 | + ) -> Result<Option<Vec<u8>>, String> { |
| 156 | + let len = self.psbt().outputs.len(); |
| 157 | + if index >= len { |
| 158 | + return Err(format!("output index {index} out of bounds (have {len} outputs)")); |
| 159 | + } |
| 160 | + Ok(self.psbt().outputs[index].proprietary.get(key).cloned()) |
| 161 | + } |
30 | 162 | } |
31 | 163 |
|
32 | 164 | fn check_bounds(index: usize, len: usize, name: &str) -> Result<(), String> { |
|
0 commit comments