|
1 | | -bitflags::bitflags! { |
2 | | - /// The different SigHash types, as defined in <https://zips.z.cash/zip-0143> |
3 | | - /// |
4 | | - /// TODO: This is currently defined as `i32` to match the `c_int` constants in this package, but |
5 | | - /// should use librustzcash’s `u8` constants once we’ve removed the C++. |
6 | | - #[derive(Copy, Clone, Debug, PartialEq, Eq)] |
7 | | - pub struct HashType: i32 { |
8 | | - /// Sign all the outputs |
9 | | - const All = 1; |
10 | | - /// Sign none of the outputs - anyone can spend |
11 | | - const None = 2; |
12 | | - /// Sign one of the outputs - anyone can spend the rest |
13 | | - const Single = 3; |
14 | | - /// Anyone can add inputs to this transaction |
15 | | - const AnyoneCanPay = 0x80; |
| 1 | +#[derive(Copy, Clone, Debug, PartialEq, Eq)] |
| 2 | +pub enum SignedOutputs { |
| 3 | + /// Sign all the outputs |
| 4 | + All, |
| 5 | + /// Sign one of the outputs - anyone can spend the rest |
| 6 | + Single, |
| 7 | + /// Sign none of the outputs - anyone can spend |
| 8 | + None, |
| 9 | +} |
| 10 | + |
| 11 | +/// The different SigHash types, as defined in <https://zips.z.cash/zip-0143> |
| 12 | +#[derive(Copy, Clone, Debug, PartialEq, Eq)] |
| 13 | +pub struct HashType { |
| 14 | + pub signed_outputs: SignedOutputs, |
| 15 | + /// Anyone can add inputs to this transaction |
| 16 | + pub anyone_can_pay: bool, |
| 17 | +} |
| 18 | + |
| 19 | +impl HashType { |
| 20 | + pub fn from_bits(bits: i32) -> Option<Self> { |
| 21 | + let msigned_outputs = match (bits & 2 != 0, bits & 1 != 0) { |
| 22 | + (false, false) => None, |
| 23 | + (false, true) => Some(SignedOutputs::All), |
| 24 | + (true, false) => Some(SignedOutputs::None), |
| 25 | + (true, true) => Some(SignedOutputs::Single), |
| 26 | + }; |
| 27 | + |
| 28 | + msigned_outputs.map(|signed_outputs| HashType { |
| 29 | + signed_outputs, |
| 30 | + anyone_can_pay: bits & 0x80 != 0, |
| 31 | + }) |
16 | 32 | } |
17 | 33 | } |
18 | 34 |
|
|
0 commit comments