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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
use crate::address::public_keys_to_address_hash;
use crate::types::chainstate::StacksPublicKey;
use crate::util::secp256k1::MessageSignature;
use crate::util::secp256k1::Secp256k1PublicKey;
use std::convert::TryFrom;
use std::fmt;
use crate::address::{
C32_ADDRESS_VERSION_MAINNET_MULTISIG, C32_ADDRESS_VERSION_MAINNET_SINGLESIG,
C32_ADDRESS_VERSION_TESTNET_MULTISIG, C32_ADDRESS_VERSION_TESTNET_SINGLESIG,
};
use crate::address::c32::c32_address;
use crate::address::c32::c32_address_decode;
use crate::address::AddressHashMode;
use crate::deps_common::bitcoin::blockdata::transaction::TxOut;
use crate::types::chainstate::StacksAddress;
use crate::util::hash::Hash160;
use std::cmp::Ordering;
pub mod chainstate;
#[cfg(feature = "sqlite")]
pub mod sqlite;
pub struct StacksPublicKeyBuffer(pub [u8; 33]);
impl_array_newtype!(StacksPublicKeyBuffer, u8, 33);
impl_array_hexstring_fmt!(StacksPublicKeyBuffer);
impl_byte_array_newtype!(StacksPublicKeyBuffer, u8, 33);
impl_byte_array_message_codec!(StacksPublicKeyBuffer, 33);
impl_byte_array_serde!(StacksPublicKeyBuffer);
impl StacksPublicKeyBuffer {
pub fn from_public_key(pubkey: &Secp256k1PublicKey) -> StacksPublicKeyBuffer {
let pubkey_bytes_vec = pubkey.to_bytes_compressed();
let mut pubkey_bytes = [0u8; 33];
pubkey_bytes.copy_from_slice(&pubkey_bytes_vec[..]);
StacksPublicKeyBuffer(pubkey_bytes)
}
pub fn to_public_key(&self) -> Result<Secp256k1PublicKey, &'static str> {
Secp256k1PublicKey::from_slice(&self.0)
.map_err(|_e_str| "Failed to decode Stacks public key")
}
}
pub trait PublicKey: Clone + fmt::Debug + serde::Serialize + serde::de::DeserializeOwned {
fn to_bytes(&self) -> Vec<u8>;
fn verify(&self, data_hash: &[u8], sig: &MessageSignature) -> Result<bool, &'static str>;
}
pub trait PrivateKey: Clone + fmt::Debug + serde::Serialize + serde::de::DeserializeOwned {
fn to_bytes(&self) -> Vec<u8>;
fn sign(&self, data_hash: &[u8]) -> Result<MessageSignature, &'static str>;
}
pub trait Address: Clone + fmt::Debug + fmt::Display {
fn to_bytes(&self) -> Vec<u8>;
fn from_string(from: &str) -> Option<Self>
where
Self: Sized;
fn is_burn(&self) -> bool;
}
pub const PEER_VERSION_EPOCH_1_0: u8 = 0x00;
pub const PEER_VERSION_EPOCH_2_0: u8 = 0x00;
pub const PEER_VERSION_EPOCH_2_05: u8 = 0x05;
pub const PEER_VERSION_EPOCH_2_1: u8 = 0x06;
#[repr(u32)]
#[derive(Debug, Clone, Eq, PartialEq, PartialOrd, Ord, Hash, Copy, Serialize, Deserialize)]
pub enum StacksEpochId {
Epoch10 = 0x01000,
Epoch20 = 0x02000,
Epoch2_05 = 0x02005,
Epoch21 = 0x0200a,
}
impl StacksEpochId {
pub fn latest() -> StacksEpochId {
StacksEpochId::Epoch21
}
}
impl std::fmt::Display for StacksEpochId {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
StacksEpochId::Epoch10 => write!(f, "1.0"),
StacksEpochId::Epoch20 => write!(f, "2.0"),
StacksEpochId::Epoch2_05 => write!(f, "2.05"),
StacksEpochId::Epoch21 => write!(f, "2.1"),
}
}
}
impl TryFrom<u32> for StacksEpochId {
type Error = &'static str;
fn try_from(value: u32) -> Result<StacksEpochId, Self::Error> {
match value {
x if x == StacksEpochId::Epoch10 as u32 => Ok(StacksEpochId::Epoch10),
x if x == StacksEpochId::Epoch20 as u32 => Ok(StacksEpochId::Epoch20),
x if x == StacksEpochId::Epoch2_05 as u32 => Ok(StacksEpochId::Epoch2_05),
x if x == StacksEpochId::Epoch21 as u32 => Ok(StacksEpochId::Epoch21),
_ => Err("Invalid epoch"),
}
}
}
impl PartialOrd for StacksAddress {
fn partial_cmp(&self, other: &StacksAddress) -> Option<Ordering> {
Some(self.cmp(other))
}
}
impl Ord for StacksAddress {
fn cmp(&self, other: &StacksAddress) -> Ordering {
match self.version.cmp(&other.version) {
Ordering::Equal => self.bytes.cmp(&other.bytes),
inequality => inequality,
}
}
}
impl StacksAddress {
pub fn new(version: u8, hash: Hash160) -> StacksAddress {
StacksAddress {
version,
bytes: hash,
}
}
pub fn is_mainnet(&self) -> bool {
match self.version {
C32_ADDRESS_VERSION_MAINNET_MULTISIG | C32_ADDRESS_VERSION_MAINNET_SINGLESIG => true,
C32_ADDRESS_VERSION_TESTNET_MULTISIG | C32_ADDRESS_VERSION_TESTNET_SINGLESIG => false,
_ => false,
}
}
pub fn burn_address(mainnet: bool) -> StacksAddress {
StacksAddress {
version: if mainnet {
C32_ADDRESS_VERSION_MAINNET_SINGLESIG
} else {
C32_ADDRESS_VERSION_TESTNET_SINGLESIG
},
bytes: Hash160([0u8; 20]),
}
}
pub fn from_public_keys(
version: u8,
hash_mode: &AddressHashMode,
num_sigs: usize,
pubkeys: &Vec<StacksPublicKey>,
) -> Option<StacksAddress> {
if pubkeys.len() < num_sigs {
return None;
}
match *hash_mode {
AddressHashMode::SerializeP2PKH | AddressHashMode::SerializeP2WPKH => {
if num_sigs != 1 || pubkeys.len() != 1 {
return None;
}
}
_ => {}
}
match *hash_mode {
AddressHashMode::SerializeP2WPKH | AddressHashMode::SerializeP2WSH => {
for pubkey in pubkeys {
if !pubkey.compressed() {
return None;
}
}
}
_ => {}
}
let hash_bits = public_keys_to_address_hash(hash_mode, num_sigs, pubkeys);
Some(StacksAddress::new(version, hash_bits))
}
}
impl std::fmt::Display for StacksAddress {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
c32_address(self.version, self.bytes.as_bytes())
.expect("Stacks version is not C32-encodable")
.fmt(f)
}
}
impl Address for StacksAddress {
fn to_bytes(&self) -> Vec<u8> {
self.bytes.as_bytes().to_vec()
}
fn from_string(s: &str) -> Option<StacksAddress> {
let (version, bytes) = match c32_address_decode(s) {
Ok((v, b)) => (v, b),
Err(_) => {
return None;
}
};
if bytes.len() != 20 {
return None;
}
let mut hash_bytes = [0u8; 20];
hash_bytes.copy_from_slice(&bytes[..]);
Some(StacksAddress {
version: version,
bytes: Hash160(hash_bytes),
})
}
fn is_burn(&self) -> bool {
self.bytes == Hash160([0u8; 20])
}
}
#[derive(Debug, Clone, PartialEq, Eq, Hash, Deserialize)]
pub struct StacksEpoch<L> {
pub epoch_id: StacksEpochId,
pub start_height: u64,
pub end_height: u64,
pub block_limit: L,
pub network_epoch: u8,
}
impl<L> StacksEpoch<L> {
pub fn find_epoch(epochs: &[StacksEpoch<L>], height: u64) -> Option<usize> {
for (i, epoch) in epochs.iter().enumerate() {
if epoch.start_height <= height && height < epoch.end_height {
return Some(i);
}
}
None
}
pub fn find_epoch_by_id(epochs: &[StacksEpoch<L>], epoch_id: StacksEpochId) -> Option<usize> {
for (i, epoch) in epochs.iter().enumerate() {
if epoch.epoch_id == epoch_id {
return Some(i);
}
}
None
}
}
impl<L: PartialEq> PartialOrd for StacksEpoch<L> {
fn partial_cmp(&self, other: &StacksEpoch<L>) -> Option<Ordering> {
self.epoch_id.partial_cmp(&other.epoch_id)
}
}
impl<L: PartialEq + Eq> Ord for StacksEpoch<L> {
fn cmp(&self, other: &StacksEpoch<L>) -> Ordering {
self.epoch_id.cmp(&other.epoch_id)
}
}