Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions .github/workflows/rust.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,9 @@ jobs:
DO_FUZZ: true
DO_BENCH: true
AS_DEPENDENCY: true
- rust: 1.65.0
- rust: 1.85.0
env:
AS_DEPENDENCY: true
PIN_VERSIONS: true
steps:
- name: Install test dependencies
run: sudo apt-get install -y binutils-dev libunwind8-dev libcurl4-openssl-dev libelf-dev libdw-dev cmake gcc libiberty-dev
Expand Down
3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
[package]
name = "tapyrus"
version = "0.4.8"
version = "0.5.0"
edition = "2024"
authors = ["Kohei Taniguchi <kohei@chaintope.com>", "Hajime Yamaguchi <h_yamaguchi@chaintope.com>"]
license = "MIT"
homepage = "https://github.com/chaintope/rust-tapyrus/"
Expand Down
8 changes: 4 additions & 4 deletions examples/handshake.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ fn main() {
payload: version_message,
};

if let Ok(mut stream) = TcpStream::connect(address) {
match TcpStream::connect(address) { Ok(mut stream) => {
// Send the message
let _ = stream.write_all(encode::serialize(&first_message).as_slice());
println!("Sent version message");
Expand Down Expand Up @@ -68,9 +68,9 @@ fn main() {
}
}
let _ = stream.shutdown(Shutdown::Both);
} else {
} _ => {
eprintln!("Failed to open connection");
}
}}
}

fn build_version_message(address: SocketAddr) -> message::NetworkMessage {
Expand All @@ -93,7 +93,7 @@ fn build_version_message(address: SocketAddr) -> message::NetworkMessage {
let addr_from = address::Address::new(&my_address, constants::ServiceFlags::NONE);

// "Node random nonce, randomly generated every time a version packet is sent. This nonce is used to detect connections to self."
let nonce: u64 = secp256k1::rand::thread_rng().gen();
let nonce: u64 = secp256k1::rand::thread_rng().r#gen();

// "User Agent (0x00 if string is 0 bytes long)"
let user_agent = String::from("rust-example");
Expand Down
38 changes: 19 additions & 19 deletions src/blockdata/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,18 +28,18 @@
use std::io;
use std::str::FromStr;

use hashes::{Hash, HashEngine};
use hashes::hex::FromHex;
use hash_types::{Wtxid, BlockHash, BlockSigHash, TxMerkleNode, WitnessMerkleNode, WitnessCommitment, XFieldHash};
use consensus::{serialize, encode, Decodable, Encodable};
use consensus::encode::Error;
use consensus::encode::serialize_hex;
use blockdata::constants::WITNESS_SCALE_FACTOR;
use blockdata::transaction::Transaction;
use util::hash::bitcoin_merkle_root;
use util::key::PublicKey;
use util::signature::Signature;
use VarInt;
use crate::hashes::{Hash, HashEngine};
use crate::hashes::hex::FromHex;
use crate::hash_types::{Wtxid, BlockHash, BlockSigHash, TxMerkleNode, WitnessMerkleNode, WitnessCommitment, XFieldHash};
use crate::consensus::{serialize, encode, Decodable, Encodable};
use crate::consensus::encode::Error;
use crate::consensus::encode::serialize_hex;
use crate::blockdata::constants::WITNESS_SCALE_FACTOR;
use crate::blockdata::transaction::Transaction;
use crate::util::hash::bitcoin_merkle_root;
use crate::util::key::PublicKey;
use crate::util::signature::Signature;
use crate::VarInt;


/// A block header, which contains all the block's information except
Expand Down Expand Up @@ -100,7 +100,7 @@ impl BlockHeader {
/// Tapyrus signer needs to sign this hash. The signature will be added to
/// the block header as the proof field and submitted to the tapyrus node.
pub fn signature_hash(&self) -> BlockSigHash {
let block = BlockHeaderWithoutProof::from(&self);
let block = BlockHeaderWithoutProof::from(self);
BlockSigHash::hash(&serialize(&block))
}

Expand Down Expand Up @@ -172,7 +172,7 @@ impl XField {
impl FromStr for XField {
type Err = encode::Error;
fn from_str(s: &str) -> Result<Self, Self::Err> {
let bytes: Vec<u8> = Vec::from_hex(&s[..]).map_err(|_| encode::Error::ParseFailed("invalid hex string"))?;
let bytes: Vec<u8> = Vec::from_hex(s).map_err(|_| encode::Error::ParseFailed("invalid hex string"))?;
XField::consensus_decode(&bytes[..])
}
}
Expand Down Expand Up @@ -399,11 +399,11 @@ impl_consensus_encoding!(
mod tests {
use std::str::FromStr;

use blockdata::block::{Block, XField};
use consensus::encode::{deserialize, serialize, Error};
use util::key::PublicKey;
use hash_types::{BlockSigHash, XFieldHash};
use hashes::hex::FromHex;
use crate::blockdata::block::{Block, XField};
use crate::consensus::encode::{deserialize, serialize, Error};
use crate::util::key::PublicKey;
use crate::hash_types::{BlockSigHash, XFieldHash};
use crate::hashes::hex::FromHex;

#[test]
fn block_test() {
Expand Down
2 changes: 1 addition & 1 deletion src/blockdata/constants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
//! single transaction
//!

use network::constants::Network;
use crate::network::constants::Network;

/// The maximum allowable sequence number
pub const MAX_SEQUENCE: u32 = 0xFFFFFFFF;
Expand Down
6 changes: 3 additions & 3 deletions src/blockdata/opcodes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -567,7 +567,7 @@ impl fmt::Debug for All {
all::OP_PUSHDATA4 => write!(f, "PUSHDATA4"),
all::OP_PUSHNUM_NEG1 => write!(f, "PUSHNUM_NEG1"),
all::OP_RESERVED => write!(f, "RESERVED"),
All {code: x} if x >= all::OP_PUSHNUM_1.code && x <= all::OP_PUSHNUM_16.code => write!(f, "PUSHNUM_{}", x - all::OP_PUSHNUM_1.code + 1),
All {code: x} if (all::OP_PUSHNUM_1.code..=all::OP_PUSHNUM_16.code).contains(&x) => write!(f, "PUSHNUM_{}", x - all::OP_PUSHNUM_1.code + 1),
all::OP_NOP => write!(f, "NOP"),
all::OP_VER => write!(f, "VER"),
all::OP_IF => write!(f, "IF"),
Expand Down Expand Up @@ -650,7 +650,7 @@ impl fmt::Debug for All {
all::OP_CLTV => write!(f, "CLTV"),
all::OP_CSV => write!(f, "CSV"),
all::OP_COLOR => write!(f, "COLOR"),
All {code: x} if x >= all::OP_NOP1.code && x <= all::OP_NOP10.code => write!(f, "NOP{}", x - all::OP_NOP1.code + 1),
All {code: x} if (all::OP_NOP1.code..=all::OP_NOP10.code).contains(&x) => write!(f, "NOP{}", x - all::OP_NOP1.code + 1),
All {code: x} => write!(f, "RETURN_{}", x),
}
}
Expand Down Expand Up @@ -825,7 +825,7 @@ mod tests {
use super::*;

macro_rules! roundtrip {
($unique:expr, $op:ident) => {
($unique:expr_2021, $op:ident) => {
assert_eq!(all::$op, All::from(all::$op.into_u8()));

let s1 = format!("{}", all::$op);
Expand Down
57 changes: 27 additions & 30 deletions src/blockdata/script.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,18 +30,18 @@ use std::str::FromStr;

#[cfg(feature = "serde")] use serde;

use hash_types::{ScriptHash, WScriptHash};
use blockdata::opcodes;
use blockdata::transaction::OutPoint;
use consensus::{deserialize, encode, Decodable, Encodable};
use consensus::encode::serialize_hex;
use hashes::hex::FromHex;
use hashes::{sha256, Hash};
use crate::hash_types::{ScriptHash, WScriptHash};
use crate::blockdata::opcodes;
use crate::blockdata::transaction::OutPoint;
use crate::consensus::{deserialize, encode, Decodable, Encodable};
use crate::consensus::encode::serialize_hex;
use crate::hashes::hex::FromHex;
use crate::hashes::{sha256, Hash};

#[cfg(feature="bitcoinconsensus")] use bitcoinconsensus;
#[cfg(feature="bitcoinconsensus")] use std::convert;

use util::key::PublicKey;
use crate::util::key::PublicKey;

#[derive(Clone, Default, PartialOrd, Ord, PartialEq, Eq, Hash)]
/// A Bitcoin script
Expand Down Expand Up @@ -308,7 +308,7 @@ impl Script {
pub fn is_empty(&self) -> bool { self.0.is_empty() }

/// Returns the script data
pub fn as_bytes(&self) -> &[u8] { &*self.0 }
pub fn as_bytes(&self) -> &[u8] { &self.0 }

/// Returns a copy of the script data
pub fn to_bytes(&self) -> Vec<u8> { self.0.clone().into_vec() }
Expand Down Expand Up @@ -477,7 +477,7 @@ impl Script {
}
}
Instruction::PushBytes(bytes) => {
match PublicKey::from_slice(&bytes) {
match PublicKey::from_slice(bytes) {
Ok(key) => { pubkeys.push(key) },
_ => { return Err(MultisigError::IsNotMultisig) }
}
Expand Down Expand Up @@ -510,7 +510,7 @@ impl Script {
/// it as a slice using `script[..]` or convert it to a vector using `into_bytes()`.
///
/// To force minimal pushes, use [instructions_minimal].
pub fn instructions(&self) -> Instructions {
pub fn instructions(&self) -> Instructions<'_> {
Instructions {
data: &self.0[..],
enforce_minimal: false,
Expand All @@ -519,7 +519,7 @@ impl Script {

/// Iterate over the script in the form of `Instruction`s while enforcing
/// minimal pushes.
pub fn instructions_minimal(&self) -> Instructions {
pub fn instructions_minimal(&self) -> Instructions<'_> {
Instructions {
data: &self.0[..],
enforce_minimal: true,
Expand All @@ -537,7 +537,7 @@ impl Script {
}

/// Write the assembly decoding of the script to the formatter.
pub fn fmt_asm(&self, f: &mut fmt::Write) -> fmt::Result {
pub fn fmt_asm(&self, f: &mut dyn fmt::Write) -> fmt::Result {
let mut index = 0;
while index < self.0.len() {
let opcode = opcodes::All::from(self.0[index]);
Expand All @@ -553,7 +553,7 @@ impl Script {
break;
}
match read_uint(&self.0[index..], 1) {
Ok(n) => { index += 1; n as usize }
Ok(n) => { index += 1; n }
Err(_) => { f.write_str("<bad length>")?; break; }
}
}
Expand All @@ -563,7 +563,7 @@ impl Script {
break;
}
match read_uint(&self.0[index..], 2) {
Ok(n) => { index += 2; n as usize }
Ok(n) => { index += 2; n }
Err(_) => { f.write_str("<bad length>")?; break; }
}
}
Expand All @@ -573,7 +573,7 @@ impl Script {
break;
}
match read_uint(&self.0[index..], 4) {
Ok(n) => { index += 4; n as usize }
Ok(n) => { index += 4; n }
Err(_) => { f.write_str("<bad length>")?; break; }
}
}
Expand Down Expand Up @@ -682,12 +682,11 @@ impl<'a> Iterator for Instructions<'a> {
self.data = &[]; // Kill iterator so that it does not return an infinite stream of errors
return Some(Err(Error::EarlyEndOfScript));
}
if self.enforce_minimal {
if n == 1 && (self.data[1] == 0x81 || (self.data[1] > 0 && self.data[1] <= 16)) {
if self.enforce_minimal
&& n == 1 && (self.data[1] == 0x81 || (self.data[1] > 0 && self.data[1] <= 16)) {
self.data = &[];
return Some(Err(Error::NonMinimalPush));
}
}
let ret = Some(Ok(Instruction::PushBytes(&self.data[1..n+1])));
self.data = &self.data[n + 1..];
ret
Expand Down Expand Up @@ -791,7 +790,7 @@ impl Builder {
/// dedicated opcodes to push some small integers.
pub fn push_int(self, data: i64) -> Builder {
// We can special-case -1, 1-16
if data == -1 || (data >= 1 && data <= 16) {
if data == -1 || (1..=16).contains(&data) {
let opcode = opcodes::All::from(
(data - 1 + opcodes::OP_TRUE.into_u8() as i64) as u8
);
Expand Down Expand Up @@ -1134,7 +1133,7 @@ impl Decodable for ColorIdentifier {

let payload = sha256::Hash::from_slice(&bytes[1..]).map_err(|_| encode::Error::ParseFailed("invalid payload."))?;
Ok(ColorIdentifier {
token_type: token_type,
token_type,
payload: ColorIdentifierPayload(payload)
})
}
Expand Down Expand Up @@ -1162,12 +1161,10 @@ pub enum TokenTypes {
impl TokenTypes {
/// return true if token type is supported
pub fn is_valid(token_type: &u8) -> bool {
vec![
TokenTypes::None,
[TokenTypes::None,
TokenTypes::Reissuable,
TokenTypes::NonReissuable,
TokenTypes::Nft,
].iter().any(|e| e.clone() as u8 == *token_type)
TokenTypes::Nft].iter().any(|e| e.clone() as u8 == *token_type)
}
}

Expand All @@ -1178,11 +1175,11 @@ mod test {
use super::*;
use super::build_scriptint;

use consensus::encode::{deserialize, serialize};
use blockdata::opcodes;
use util::key::PublicKey;
use hash_types::Txid;
use hashes::hex::FromHex;
use crate::consensus::encode::{deserialize, serialize};
use crate::blockdata::opcodes;
use crate::util::key::PublicKey;
use crate::hash_types::Txid;
use crate::hashes::hex::FromHex;

#[test]
fn script() {
Expand Down
Loading
Loading