Skip to content

Commit 32d9e3c

Browse files
committed
Add CoinType used for Ledger and encode/decode utils
- also use the specific version of parrity scale codec with the fix for NanoX
1 parent 4e21bf8 commit 32d9e3c

3 files changed

Lines changed: 140 additions & 10 deletions

File tree

Cargo.lock

Lines changed: 2 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,18 @@ repository = "https://github.com/mintlayer/mintlayer-core-primitives"
66
readme = "README.md"
77
license = "MIT"
88
version = "1.0.0"
9-
edition = "2024"
10-
rust-version = "1.88"
9+
edition = "2021"
10+
rust-version = "1.85"
1111

1212
[dependencies]
13-
derive_more = { version = "2.0", default-features = false, features = ["debug"] }
13+
derive_more = { version = "2.0", default-features = false, features = [
14+
"debug",
15+
] }
1416
fixed-hash = { version = "0.8", default-features = false }
15-
parity-scale-codec = { version = "3.7", default-features = false, features = ["derive"] }
17+
parity-scale-codec = { git = "https://github.com/paritytech/parity-scale-codec.git", rev = "5021525697edc0661591ebc71392c48d950a10b0", default-features = false, features = [
18+
"derive",
19+
] }
20+
1621
strum = { version = "0.27", default-features = false, features = ["derive"] }
1722

1823
[dev-dependencies]

src/misc.rs

Lines changed: 129 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@
1313
// See the License for the specific language governing permissions and
1414
// limitations under the License.
1515

16-
use parity_scale_codec::{Decode, Encode};
16+
use parity_scale_codec::{Decode, DecodeAll, Encode};
1717

18-
use crate::TokenId;
18+
use crate::{Destination, TokenId};
1919

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

@@ -32,6 +32,8 @@ pub struct Amount {
3232
}
3333

3434
impl Amount {
35+
pub const ZERO: Self = Self::from_atoms(0);
36+
3537
pub const fn from_atoms(v: AmountUIntType) -> Self {
3638
Amount { atoms: v }
3739
}
@@ -89,3 +91,128 @@ pub type SecondsCountUIntType = u64;
8991

9092
#[derive(Debug, Clone, Copy, PartialOrd, Ord, PartialEq, Eq, Encode, Decode)]
9193
pub struct SecondsCount(#[codec(compact)] pub SecondsCountUIntType);
94+
95+
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
96+
pub enum CoinType {
97+
Mainnet,
98+
Testnet,
99+
Regtest,
100+
Signet,
101+
}
102+
103+
impl CoinType {
104+
pub const fn coin_ticker(&self) -> &'static str {
105+
match self {
106+
Self::Mainnet => "ML",
107+
Self::Testnet => "TML",
108+
Self::Regtest => "RML",
109+
Self::Signet => "SML",
110+
}
111+
}
112+
113+
pub const fn bip44_coin_type(&self) -> u32 {
114+
let hardened_bit = 1 << 31;
115+
match self {
116+
Self::Mainnet => 19788 + hardened_bit,
117+
Self::Testnet | Self::Regtest | Self::Signet => 1 + hardened_bit,
118+
}
119+
}
120+
121+
pub const fn coin_decimals(&self) -> u8 {
122+
11
123+
}
124+
125+
pub const fn address_prefix(&self, destination: &Destination) -> &'static str {
126+
match self {
127+
Self::Mainnet => match destination {
128+
Destination::AnyoneCanSpend => "mxanyonecanspend",
129+
Destination::PublicKeyHash(_) => "mtc",
130+
Destination::PublicKey(_) => "mptc",
131+
Destination::ScriptHash(_) => "mstc",
132+
Destination::ClassicMultisig(_) => "mmtc",
133+
},
134+
Self::Testnet => match destination {
135+
Destination::AnyoneCanSpend => "txanyonecanspend",
136+
Destination::PublicKeyHash(_) => "tmt",
137+
Destination::PublicKey(_) => "tpmt",
138+
Destination::ScriptHash(_) => "tstc",
139+
Destination::ClassicMultisig(_) => "tmtc",
140+
},
141+
Self::Regtest => match destination {
142+
Destination::AnyoneCanSpend => "rxanyonecanspend",
143+
Destination::PublicKeyHash(_) => "rmt",
144+
Destination::PublicKey(_) => "rpmt",
145+
Destination::ScriptHash(_) => "rstc",
146+
Destination::ClassicMultisig(_) => "rmtc",
147+
},
148+
Self::Signet => match destination {
149+
Destination::AnyoneCanSpend => "sxanyonecanspend",
150+
Destination::PublicKeyHash(_) => "smt",
151+
Destination::PublicKey(_) => "spmt",
152+
Destination::ScriptHash(_) => "sstc",
153+
Destination::ClassicMultisig(_) => "smtc",
154+
},
155+
}
156+
}
157+
158+
pub const fn pool_id_address_prefix(&self) -> &'static str {
159+
match self {
160+
Self::Mainnet => "mpool",
161+
Self::Testnet => "tpool",
162+
Self::Regtest => "rpool",
163+
Self::Signet => "spool",
164+
}
165+
}
166+
167+
pub const fn delegation_id_address_prefix(&self) -> &'static str {
168+
match self {
169+
Self::Mainnet => "mdelg",
170+
Self::Testnet => "tdelg",
171+
Self::Regtest => "rdelg",
172+
Self::Signet => "sdelg",
173+
}
174+
}
175+
176+
pub const fn token_id_address_prefix(&self) -> &'static str {
177+
match self {
178+
Self::Mainnet => "mmltk",
179+
Self::Testnet => "tmltk",
180+
Self::Regtest => "rmltk",
181+
Self::Signet => "smltk",
182+
}
183+
}
184+
185+
pub const fn order_id_address_prefix(&self) -> &'static str {
186+
match self {
187+
Self::Mainnet => "mordr",
188+
Self::Testnet => "tordr",
189+
Self::Regtest => "rordr",
190+
Self::Signet => "sordr",
191+
}
192+
}
193+
194+
pub const fn vrf_public_key_address_prefix(&self) -> &'static str {
195+
match self {
196+
Self::Mainnet => "mvrfpk",
197+
Self::Testnet => "tvrfpk",
198+
Self::Regtest => "rvrfpk",
199+
Self::Signet => "svrfpk",
200+
}
201+
}
202+
}
203+
204+
pub fn encode<T: Encode>(t: &T) -> PscVec<u8> {
205+
t.encode()
206+
}
207+
208+
pub fn encode_to<T: Encode>(t: &T, buf: &mut PscVec<u8>) {
209+
t.encode_to(buf)
210+
}
211+
212+
pub fn decode_all<T: Decode>(mut bytes: &[u8]) -> Option<T> {
213+
T::decode_all(&mut bytes).ok()
214+
}
215+
216+
pub fn encode_as_compact(num: u32) -> PscVec<u8> {
217+
parity_scale_codec::Compact::<u32>::encode(&num.into())
218+
}

0 commit comments

Comments
 (0)