// Bitcoin protocol primitives library. // // SPDX-License-Identifier: Apache-2.0 // // Written in 2019-2023 by // Dr Maxim Orlovsky // // Copyright (C) 2019-2023 LNP/BP Standards Association. All rights reserved. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. use amplify::hex::{self, FromHex}; use amplify::{Bytes32, Bytes32StrRev, Wrapper}; use crate::LIB_NAME_BITCOIN; #[derive(Wrapper, Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash, From)] #[derive(StrictType, StrictDumb, StrictEncode, StrictDecode)] #[strict_type(lib = LIB_NAME_BITCOIN)] #[cfg_attr( feature = "serde", derive(Serialize, Deserialize), serde(crate = "serde_crate", transparent) )] #[wrapper(BorrowSlice, Index, RangeOps, Debug, LowerHex, UpperHex, Display, FromStr)] pub struct BlockHash( #[from] #[from([u8; 32])] Bytes32StrRev, ); impl FromHex for BlockHash { fn from_byte_iter(iter: I) -> Result where I: Iterator> + ExactSizeIterator + DoubleEndedIterator { Bytes32StrRev::from_byte_iter(iter).map(Self) } } #[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Debug)] #[derive(StrictType, StrictEncode, StrictDecode, StrictDumb)] #[strict_type(lib = LIB_NAME_BITCOIN)] #[cfg_attr( feature = "serde", derive(Serialize, Deserialize), serde(crate = "serde_crate", rename_all = "camelCase") )] pub struct BlockHeader { /// Block version, now repurposed for soft fork signalling. pub version: i32, /// Reference to the previous block in the chain. pub prev_block_hash: BlockHash, /// The root hash of the merkle tree of transactions in the block. pub merkle_root: Bytes32, /// The timestamp of the block, as claimed by the miner. pub time: u32, /// The target value below which the blockhash must lie. pub bits: u32, /// The nonce, selected to obtain a low enough blockhash. pub nonce: u32, }