-
Notifications
You must be signed in to change notification settings - Fork 0
Add CoinType used for Ledger and encode/decode utils #2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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>; | ||
|
|
||
|
|
@@ -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 } | ||
| } | ||
|
|
@@ -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 { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let's put it in a separate file, e.g. |
||
| 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
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. But are all these functions really needed? |
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same comment as in mintlayer/mintlayer-trezor-firmware#24 (comment)