module Cometa.Crypto.DerivationPath
(
Index,
mkIndex,
indexValue,
AccountPath,
accountPath,
purpose,
coinType,
account,
accountIndices,
DerivationPath,
derivationPath,
accountPrefix,
role,
index,
toIndices,
relativeIndices,
cardanoCoinType,
standardPurpose,
multisigPurpose,
externalRole,
internalRole,
stakingRole,
drepRole,
committeeColdRole,
committeeHotRole,
standardAccountPath,
)
where
import Cometa.Error (Error (InvalidDerivationIndex))
import Data.Bits ((.|.))
import Data.Word (Word32)
newtype Index = Index Word32 deriving (Index -> Index -> Bool
(Index -> Index -> Bool) -> (Index -> Index -> Bool) -> Eq Index
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: Index -> Index -> Bool
== :: Index -> Index -> Bool
$c/= :: Index -> Index -> Bool
/= :: Index -> Index -> Bool
Eq, Eq Index
Eq Index =>
(Index -> Index -> Ordering)
-> (Index -> Index -> Bool)
-> (Index -> Index -> Bool)
-> (Index -> Index -> Bool)
-> (Index -> Index -> Bool)
-> (Index -> Index -> Index)
-> (Index -> Index -> Index)
-> Ord Index
Index -> Index -> Bool
Index -> Index -> Ordering
Index -> Index -> Index
forall a.
Eq a =>
(a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
$ccompare :: Index -> Index -> Ordering
compare :: Index -> Index -> Ordering
$c< :: Index -> Index -> Bool
< :: Index -> Index -> Bool
$c<= :: Index -> Index -> Bool
<= :: Index -> Index -> Bool
$c> :: Index -> Index -> Bool
> :: Index -> Index -> Bool
$c>= :: Index -> Index -> Bool
>= :: Index -> Index -> Bool
$cmax :: Index -> Index -> Index
max :: Index -> Index -> Index
$cmin :: Index -> Index -> Index
min :: Index -> Index -> Index
Ord, Int -> Index -> ShowS
[Index] -> ShowS
Index -> String
(Int -> Index -> ShowS)
-> (Index -> String) -> ([Index] -> ShowS) -> Show Index
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> Index -> ShowS
showsPrec :: Int -> Index -> ShowS
$cshow :: Index -> String
show :: Index -> String
$cshowList :: [Index] -> ShowS
showList :: [Index] -> ShowS
Show)
instance Bounded Index where
minBound :: Index
minBound = Word32 -> Index
Index Word32
0
maxBound :: Index
maxBound = Word32 -> Index
Index Word32
2147483647
data AccountPath = AccountPath !Index !Index !Index deriving (AccountPath -> AccountPath -> Bool
(AccountPath -> AccountPath -> Bool)
-> (AccountPath -> AccountPath -> Bool) -> Eq AccountPath
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: AccountPath -> AccountPath -> Bool
== :: AccountPath -> AccountPath -> Bool
$c/= :: AccountPath -> AccountPath -> Bool
/= :: AccountPath -> AccountPath -> Bool
Eq, Eq AccountPath
Eq AccountPath =>
(AccountPath -> AccountPath -> Ordering)
-> (AccountPath -> AccountPath -> Bool)
-> (AccountPath -> AccountPath -> Bool)
-> (AccountPath -> AccountPath -> Bool)
-> (AccountPath -> AccountPath -> Bool)
-> (AccountPath -> AccountPath -> AccountPath)
-> (AccountPath -> AccountPath -> AccountPath)
-> Ord AccountPath
AccountPath -> AccountPath -> Bool
AccountPath -> AccountPath -> Ordering
AccountPath -> AccountPath -> AccountPath
forall a.
Eq a =>
(a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
$ccompare :: AccountPath -> AccountPath -> Ordering
compare :: AccountPath -> AccountPath -> Ordering
$c< :: AccountPath -> AccountPath -> Bool
< :: AccountPath -> AccountPath -> Bool
$c<= :: AccountPath -> AccountPath -> Bool
<= :: AccountPath -> AccountPath -> Bool
$c> :: AccountPath -> AccountPath -> Bool
> :: AccountPath -> AccountPath -> Bool
$c>= :: AccountPath -> AccountPath -> Bool
>= :: AccountPath -> AccountPath -> Bool
$cmax :: AccountPath -> AccountPath -> AccountPath
max :: AccountPath -> AccountPath -> AccountPath
$cmin :: AccountPath -> AccountPath -> AccountPath
min :: AccountPath -> AccountPath -> AccountPath
Ord, Int -> AccountPath -> ShowS
[AccountPath] -> ShowS
AccountPath -> String
(Int -> AccountPath -> ShowS)
-> (AccountPath -> String)
-> ([AccountPath] -> ShowS)
-> Show AccountPath
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> AccountPath -> ShowS
showsPrec :: Int -> AccountPath -> ShowS
$cshow :: AccountPath -> String
show :: AccountPath -> String
$cshowList :: [AccountPath] -> ShowS
showList :: [AccountPath] -> ShowS
Show)
data DerivationPath = DerivationPath !AccountPath !Index !Index deriving (DerivationPath -> DerivationPath -> Bool
(DerivationPath -> DerivationPath -> Bool)
-> (DerivationPath -> DerivationPath -> Bool) -> Eq DerivationPath
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: DerivationPath -> DerivationPath -> Bool
== :: DerivationPath -> DerivationPath -> Bool
$c/= :: DerivationPath -> DerivationPath -> Bool
/= :: DerivationPath -> DerivationPath -> Bool
Eq, Eq DerivationPath
Eq DerivationPath =>
(DerivationPath -> DerivationPath -> Ordering)
-> (DerivationPath -> DerivationPath -> Bool)
-> (DerivationPath -> DerivationPath -> Bool)
-> (DerivationPath -> DerivationPath -> Bool)
-> (DerivationPath -> DerivationPath -> Bool)
-> (DerivationPath -> DerivationPath -> DerivationPath)
-> (DerivationPath -> DerivationPath -> DerivationPath)
-> Ord DerivationPath
DerivationPath -> DerivationPath -> Bool
DerivationPath -> DerivationPath -> Ordering
DerivationPath -> DerivationPath -> DerivationPath
forall a.
Eq a =>
(a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
$ccompare :: DerivationPath -> DerivationPath -> Ordering
compare :: DerivationPath -> DerivationPath -> Ordering
$c< :: DerivationPath -> DerivationPath -> Bool
< :: DerivationPath -> DerivationPath -> Bool
$c<= :: DerivationPath -> DerivationPath -> Bool
<= :: DerivationPath -> DerivationPath -> Bool
$c> :: DerivationPath -> DerivationPath -> Bool
> :: DerivationPath -> DerivationPath -> Bool
$c>= :: DerivationPath -> DerivationPath -> Bool
>= :: DerivationPath -> DerivationPath -> Bool
$cmax :: DerivationPath -> DerivationPath -> DerivationPath
max :: DerivationPath -> DerivationPath -> DerivationPath
$cmin :: DerivationPath -> DerivationPath -> DerivationPath
min :: DerivationPath -> DerivationPath -> DerivationPath
Ord, Int -> DerivationPath -> ShowS
[DerivationPath] -> ShowS
DerivationPath -> String
(Int -> DerivationPath -> ShowS)
-> (DerivationPath -> String)
-> ([DerivationPath] -> ShowS)
-> Show DerivationPath
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> DerivationPath -> ShowS
showsPrec :: Int -> DerivationPath -> ShowS
$cshow :: DerivationPath -> String
show :: DerivationPath -> String
$cshowList :: [DerivationPath] -> ShowS
showList :: [DerivationPath] -> ShowS
Show)
mkIndex :: Integer -> Either Error Index
mkIndex :: Integer -> Either Error Index
mkIndex Integer
value
| Integer
value Integer -> Integer -> Bool
forall a. Ord a => a -> a -> Bool
< Integer
0 Bool -> Bool -> Bool
|| Integer
value Integer -> Integer -> Bool
forall a. Ord a => a -> a -> Bool
> Integer
2147483647 = Error -> Either Error Index
forall a b. a -> Either a b
Left (Integer -> Error
InvalidDerivationIndex Integer
value)
| Bool
otherwise = Index -> Either Error Index
forall a b. b -> Either a b
Right (Word32 -> Index
Index (Integer -> Word32
forall a. Num a => Integer -> a
fromInteger Integer
value))
indexValue :: Index -> Word32
indexValue :: Index -> Word32
indexValue (Index Word32
value) = Word32
value
accountPath :: Index -> Index -> Index -> AccountPath
accountPath :: Index -> Index -> Index -> AccountPath
accountPath = Index -> Index -> Index -> AccountPath
AccountPath
purpose :: AccountPath -> Index
purpose :: AccountPath -> Index
purpose (AccountPath Index
value Index
_ Index
_) = Index
value
coinType :: AccountPath -> Index
coinType :: AccountPath -> Index
coinType (AccountPath Index
_ Index
value Index
_) = Index
value
account :: AccountPath -> Index
account :: AccountPath -> Index
account (AccountPath Index
_ Index
_ Index
value) = Index
value
accountIndices :: AccountPath -> [Word32]
accountIndices :: AccountPath -> [Word32]
accountIndices AccountPath
path = (Index -> Word32) -> [Index] -> [Word32]
forall a b. (a -> b) -> [a] -> [b]
map ((Word32 -> Word32 -> Word32
forall a. Bits a => a -> a -> a
.|. Word32
0x80000000) (Word32 -> Word32) -> (Index -> Word32) -> Index -> Word32
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Index -> Word32
indexValue) [AccountPath -> Index
purpose AccountPath
path, AccountPath -> Index
coinType AccountPath
path, AccountPath -> Index
account AccountPath
path]
derivationPath :: AccountPath -> Index -> Index -> DerivationPath
derivationPath :: AccountPath -> Index -> Index -> DerivationPath
derivationPath = AccountPath -> Index -> Index -> DerivationPath
DerivationPath
accountPrefix :: DerivationPath -> AccountPath
accountPrefix :: DerivationPath -> AccountPath
accountPrefix (DerivationPath AccountPath
value Index
_ Index
_) = AccountPath
value
role :: DerivationPath -> Index
role :: DerivationPath -> Index
role (DerivationPath AccountPath
_ Index
value Index
_) = Index
value
index :: DerivationPath -> Index
index :: DerivationPath -> Index
index (DerivationPath AccountPath
_ Index
_ Index
value) = Index
value
toIndices :: DerivationPath -> [Word32]
toIndices :: DerivationPath -> [Word32]
toIndices DerivationPath
path = AccountPath -> [Word32]
accountIndices (DerivationPath -> AccountPath
accountPrefix DerivationPath
path) [Word32] -> [Word32] -> [Word32]
forall a. [a] -> [a] -> [a]
++ DerivationPath -> [Word32]
relativeIndices DerivationPath
path
relativeIndices :: DerivationPath -> [Word32]
relativeIndices :: DerivationPath -> [Word32]
relativeIndices DerivationPath
path = (Index -> Word32) -> [Index] -> [Word32]
forall a b. (a -> b) -> [a] -> [b]
map Index -> Word32
indexValue [DerivationPath -> Index
role DerivationPath
path, DerivationPath -> Index
index DerivationPath
path]
cardanoCoinType :: Index
cardanoCoinType :: Index
cardanoCoinType = Word32 -> Index
Index Word32
1815
standardPurpose :: Index
standardPurpose :: Index
standardPurpose = Word32 -> Index
Index Word32
1852
multisigPurpose :: Index
multisigPurpose :: Index
multisigPurpose = Word32 -> Index
Index Word32
1854
externalRole :: Index
externalRole :: Index
externalRole = Word32 -> Index
Index Word32
0
internalRole :: Index
internalRole :: Index
internalRole = Word32 -> Index
Index Word32
1
stakingRole :: Index
stakingRole :: Index
stakingRole = Word32 -> Index
Index Word32
2
drepRole :: Index
drepRole :: Index
drepRole = Word32 -> Index
Index Word32
3
committeeColdRole :: Index
committeeColdRole :: Index
committeeColdRole = Word32 -> Index
Index Word32
4
committeeHotRole :: Index
committeeHotRole :: Index
committeeHotRole = Word32 -> Index
Index Word32
5
standardAccountPath :: Index -> AccountPath
standardAccountPath :: Index -> AccountPath
standardAccountPath = Index -> Index -> Index -> AccountPath
accountPath Index
standardPurpose Index
cardanoCoinType