-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathext.rs
More file actions
125 lines (106 loc) · 3.28 KB
/
Copy pathext.rs
File metadata and controls
125 lines (106 loc) · 3.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
use bitcoin::{consensus::encode, hashes::Hash, Network};
use bitcoinkernel::{core::BlockHashExt, Block as KernelBlock, BlockTreeEntry, ChainType};
use std::{fs, path::PathBuf};
use wallet::silentpayments::Network as WalletNetwork;
pub trait ChainExt {
fn chain_type(&self) -> ChainType;
}
impl ChainExt for Network {
fn chain_type(&self) -> ChainType {
match self {
Network::Bitcoin => ChainType::Mainnet,
Network::Signet => ChainType::Signet,
Network::Testnet => ChainType::Testnet,
Network::Testnet4 => ChainType::Testnet4,
Network::Regtest => ChainType::Regtest,
}
}
}
fn unix_home_dir() -> PathBuf {
let home_dir_string = std::env::var("HOME").unwrap();
home_dir_string.parse::<PathBuf>().unwrap()
}
pub trait ExpandTildeExt {
fn expand_tilde(&self) -> PathBuf;
}
impl<S: AsRef<str>> ExpandTildeExt for S {
fn expand_tilde(&self) -> PathBuf {
match self.as_ref().strip_prefix("~/") {
Some(rest) => unix_home_dir().join(rest),
None => PathBuf::from(self.as_ref()),
}
}
}
pub trait DirnameExt {
fn data_dir(&self) -> String;
}
impl<S: AsRef<str>> DirnameExt for S {
fn data_dir(&self) -> String {
let path = self.expand_tilde();
// Create directories if they don't exist
fs::create_dir_all(&path).unwrap();
// Get canonical (full) path
path.canonicalize().unwrap().to_str().unwrap().to_string()
}
}
pub trait NetworkExt {
// The P2P port for a given [`Network`].
fn default_p2p_port(self) -> u16;
fn wallet_network(self) -> WalletNetwork;
}
impl NetworkExt for Network {
// The P2P port for a given [`Network`].
fn default_p2p_port(self) -> u16 {
match &self {
Self::Bitcoin => 8333,
Self::Signet => 38333,
Self::Testnet => 18333,
Self::Testnet4 => 48333,
Self::Regtest => 18444,
}
}
fn wallet_network(self) -> WalletNetwork {
match self {
Self::Bitcoin => WalletNetwork::Mainnet,
Self::Regtest => WalletNetwork::Regtest,
_ => WalletNetwork::Testnet,
}
}
}
pub trait KernelBlockExt {
fn convert(self) -> bitcoin::Block;
}
impl KernelBlockExt for KernelBlock {
fn convert(self) -> bitcoin::Block {
encode::deserialize(
&self
.consensus_encode()
.expect("block has valid serialization."),
)
.expect("block has valid serialization.")
}
}
pub trait CrateBlockExt {
fn convert(self) -> KernelBlock;
}
impl CrateBlockExt for bitcoin::Block {
fn convert(self) -> KernelBlock {
KernelBlock::try_from(encode::serialize(&self).as_slice()).unwrap()
}
}
pub trait CrateHeaderExt {
fn convert(self) -> bitcoinkernel::BlockHeader;
}
impl CrateHeaderExt for bitcoin::block::Header {
fn convert(self) -> bitcoinkernel::BlockHeader {
bitcoinkernel::BlockHeader::new(encode::serialize(&self).as_slice()).unwrap()
}
}
pub trait GetBlockHashExt {
fn block_hash(&self) -> bitcoin::BlockHash;
}
impl GetBlockHashExt for BlockTreeEntry<'_> {
fn block_hash(&self) -> bitcoin::BlockHash {
bitcoin::BlockHash::from_byte_array(self.block_hash().to_bytes())
}
}