Skip to content
Open
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
6 changes: 2 additions & 4 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 9 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,18 @@ repository = "https://github.com/mintlayer/mintlayer-core-primitives"
readme = "README.md"
license = "MIT"
version = "1.0.0"
edition = "2024"
rust-version = "1.88"
edition = "2021"
rust-version = "1.85"

[dependencies]
derive_more = { version = "2.0", default-features = false, features = ["debug"] }
derive_more = { version = "2.0", default-features = false, features = [
"debug",
] }
fixed-hash = { version = "0.8", default-features = false }
parity-scale-codec = { version = "3.7", default-features = false, features = ["derive"] }
parity-scale-codec = { git = "https://github.com/paritytech/parity-scale-codec.git", rev = "5021525697edc0661591ebc71392c48d950a10b0", default-features = false, features = [
"derive",
] }
Comment on lines +17 to +19
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.


strum = { version = "0.27", default-features = false, features = ["derive"] }

[dev-dependencies]
Expand Down
131 changes: 129 additions & 2 deletions src/misc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@
// See the License for the specific language governing permissions and
// limitations under the License.

use parity_scale_codec::{Decode, Encode};
use parity_scale_codec::{Decode, DecodeAll, Encode};

use crate::TokenId;
use crate::{Destination, TokenId};

pub type PscVec<T> = parity_scale_codec::alloc::vec::Vec<T>;

Expand All @@ -32,6 +32,8 @@ pub struct Amount {
}

impl Amount {
pub const ZERO: Self = Self::from_atoms(0);

pub const fn from_atoms(v: AmountUIntType) -> Self {
Amount { atoms: v }
}
Expand Down Expand Up @@ -89,3 +91,128 @@ pub type SecondsCountUIntType = u64;

#[derive(Debug, Clone, Copy, PartialOrd, Ord, PartialEq, Eq, Encode, Decode)]
pub struct SecondsCount(#[codec(compact)] pub SecondsCountUIntType);

#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum CoinType {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's put it in a separate file, e.g. coin_type.rs

Mainnet,
Testnet,
Regtest,
Signet,
}

impl CoinType {
pub const fn coin_ticker(&self) -> &'static str {
match self {
Self::Mainnet => "ML",
Self::Testnet => "TML",
Self::Regtest => "RML",
Self::Signet => "SML",
}
}

pub const fn bip44_coin_type(&self) -> u32 {
let hardened_bit = 1 << 31;
match self {
Self::Mainnet => 19788 + hardened_bit,
Self::Testnet | Self::Regtest | Self::Signet => 1 + hardened_bit,
}
}

pub const fn coin_decimals(&self) -> u8 {
11
}

pub const fn address_prefix(&self, destination: &Destination) -> &'static str {
match self {
Self::Mainnet => match destination {
Destination::AnyoneCanSpend => "mxanyonecanspend",
Destination::PublicKeyHash(_) => "mtc",
Destination::PublicKey(_) => "mptc",
Destination::ScriptHash(_) => "mstc",
Destination::ClassicMultisig(_) => "mmtc",
},
Self::Testnet => match destination {
Destination::AnyoneCanSpend => "txanyonecanspend",
Destination::PublicKeyHash(_) => "tmt",
Destination::PublicKey(_) => "tpmt",
Destination::ScriptHash(_) => "tstc",
Destination::ClassicMultisig(_) => "tmtc",
},
Self::Regtest => match destination {
Destination::AnyoneCanSpend => "rxanyonecanspend",
Destination::PublicKeyHash(_) => "rmt",
Destination::PublicKey(_) => "rpmt",
Destination::ScriptHash(_) => "rstc",
Destination::ClassicMultisig(_) => "rmtc",
},
Self::Signet => match destination {
Destination::AnyoneCanSpend => "sxanyonecanspend",
Destination::PublicKeyHash(_) => "smt",
Destination::PublicKey(_) => "spmt",
Destination::ScriptHash(_) => "sstc",
Destination::ClassicMultisig(_) => "smtc",
},
}
}

pub const fn pool_id_address_prefix(&self) -> &'static str {
match self {
Self::Mainnet => "mpool",
Self::Testnet => "tpool",
Self::Regtest => "rpool",
Self::Signet => "spool",
}
}

pub const fn delegation_id_address_prefix(&self) -> &'static str {
match self {
Self::Mainnet => "mdelg",
Self::Testnet => "tdelg",
Self::Regtest => "rdelg",
Self::Signet => "sdelg",
}
}

pub const fn token_id_address_prefix(&self) -> &'static str {
match self {
Self::Mainnet => "mmltk",
Self::Testnet => "tmltk",
Self::Regtest => "rmltk",
Self::Signet => "smltk",
}
}

pub const fn order_id_address_prefix(&self) -> &'static str {
match self {
Self::Mainnet => "mordr",
Self::Testnet => "tordr",
Self::Regtest => "rordr",
Self::Signet => "sordr",
}
}

pub const fn vrf_public_key_address_prefix(&self) -> &'static str {
match self {
Self::Mainnet => "mvrfpk",
Self::Testnet => "tvrfpk",
Self::Regtest => "rvrfpk",
Self::Signet => "svrfpk",
}
}
}

pub fn encode<T: Encode>(t: &T) -> PscVec<u8> {
t.encode()
}

pub fn encode_to<T: Encode>(t: &T, buf: &mut PscVec<u8>) {
t.encode_to(buf)
}

pub fn decode_all<T: Decode>(mut bytes: &[u8]) -> Option<T> {
T::decode_all(&mut bytes).ok()
}
Comment on lines +212 to +214
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's better to return a Result and let the caller decide what to do with it,


pub fn encode_as_compact(num: u32) -> PscVec<u8> {
parity_scale_codec::Compact::<u32>::encode(&num.into())
}
Comment on lines +216 to +218
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This function has a generic name, but it's only defined for u32. Either make it generic over the type or at least give it a specific name, e.g. encode_u32_as_compact (though this will also look quite ugly, so plz try making it generic first).

But are all these functions really needed?

Loading