-- Copyright 2026 Biglup Labs
-- SPDX-License-Identifier: Apache-2.0
--
-- Licensed under the Apache License, Version 2.0 (the "License");
-- you may not use this file except in compliance with the License.
-- You may obtain a copy of the License at
--
--     https://www.apache.org/licenses/LICENSE-2.0
--
-- Unless required by applicable law or agreed to in writing, software
-- distributed under the License is distributed on an "AS IS" BASIS,
-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-- See the License for the specific language governing permissions and
-- limitations under the License.

-- | Validated five-level wallet paths: @m / purpose' / coin_type' / account' /
-- role / index@. Components are unencoded numbers; the first three become
-- hardened when converted to BIP32 indices. Custom purposes, coins and roles
-- are supported within the same bounds as standard paths.
--
-- @
-- import qualified Cometa.Crypto.DerivationPath as Path
--
-- firstAccount :: Path.AccountPath
-- firstAccount = Path.standardAccountPath minBound
-- @
module Cometa.Crypto.DerivationPath
  ( -- * Components
    Index,
    mkIndex,
    indexValue,

    -- * Account paths
    AccountPath,
    accountPath,
    purpose,
    coinType,
    account,
    accountIndices,

    -- * Key paths
    DerivationPath,
    derivationPath,
    accountPrefix,
    role,
    index,
    toIndices,
    relativeIndices,

    -- * Cardano wallet components
    cardanoCoinType,
    standardPurpose,
    multisigPurpose,
    externalRole,
    internalRole,
    stakingRole,
    drepRole,
    committeeColdRole,
    committeeHotRole,
    standardAccountPath,
  )
where

-- IMPORTS --------------------------------------------------------------------

import Cometa.Error (Error (InvalidDerivationIndex))
import Data.Bits ((.|.))
import Data.Word (Word32)

-- TYPES ----------------------------------------------------------------------

-- | An unencoded component in @0..2147483647@. Equality and ordering compare
-- the component number; 'Show' is diagnostic. 'minBound' is zero and 'maxBound'
-- is 2147483647.
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

-- | Three unencoded components in purpose, coin type, account order.
-- Equality and ordering compare components in that order; 'Show' is diagnostic.
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)

-- | An account prefix followed by an unhardened role and key index.
-- Equality and ordering compare all five components in derivation order;
-- 'Show' is diagnostic.
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)

-- DEFINITIONS ----------------------------------------------------------------

-- | Validate an unencoded component. Negative values and values above
-- @2147483647@ return 'InvalidDerivationIndex', retaining the exact input.
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))

-- | The unencoded component number, without the hardening bit.
indexValue :: Index -> Word32
indexValue :: Index -> Word32
indexValue (Index Word32
value) = Word32
value

-- | Construct an account prefix from purpose, coin type and account number.
-- All three components will be hardened by 'accountIndices'.
accountPath :: Index -> Index -> Index -> AccountPath
accountPath :: Index -> Index -> Index -> AccountPath
accountPath = Index -> Index -> Index -> AccountPath
AccountPath

-- | Unencoded purpose number, for example 1852 or 1854.
purpose :: AccountPath -> Index
purpose :: AccountPath -> Index
purpose (AccountPath Index
value Index
_ Index
_) = Index
value

-- | Unencoded coin type, for example 1815 for Cardano.
coinType :: AccountPath -> Index
coinType :: AccountPath -> Index
coinType (AccountPath Index
_ Index
value Index
_) = Index
value

-- | Unencoded account number, numbered from zero.
account :: AccountPath -> Index
account :: AccountPath -> Index
account (AccountPath Index
_ Index
_ Index
value) = Index
value

-- | Three encoded hardened indices in derivation order, suitable for private
-- BIP32 derivation from a root key. Each has its high bit set.
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]

-- | Extend an account prefix with an unhardened role and key index, in that order.
derivationPath :: AccountPath -> Index -> Index -> DerivationPath
derivationPath :: AccountPath -> Index -> Index -> DerivationPath
derivationPath = AccountPath -> Index -> Index -> DerivationPath
DerivationPath

-- | The three-component account prefix.
accountPrefix :: DerivationPath -> AccountPath
accountPrefix :: DerivationPath -> AccountPath
accountPrefix (DerivationPath AccountPath
value Index
_ Index
_) = AccountPath
value

-- | Unhardened role number. Standard roles are named in this module.
role :: DerivationPath -> Index
role :: DerivationPath -> Index
role (DerivationPath AccountPath
_ Index
value Index
_) = Index
value

-- | Unhardened key index, numbered from zero.
index :: DerivationPath -> Index
index :: DerivationPath -> Index
index (DerivationPath AccountPath
_ Index
_ Index
value) = Index
value

-- | Five encoded indices in derivation order, suitable for private BIP32
-- derivation from the root. Only the first three indices are hardened.
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

-- | The two unhardened indices, role then key index, relative to an already
-- derived account key. Suitable for private or public BIP32 derivation.
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]

-- Values audited against key_handlers/cip_1852_constants.h at native revision
-- 7811fe94dad90f30478ed4b61d96677f188abaa8; native-oracle tests verify agreement.

-- | Cardano coin type.
cardanoCoinType :: Index
cardanoCoinType :: Index
cardanoCoinType = Word32 -> Index
Index Word32
1815

-- | Standard wallet purpose.
standardPurpose :: Index
standardPurpose :: Index
standardPurpose = Word32 -> Index
Index Word32
1852

-- | CIP-1854 multisignature wallet purpose.
multisigPurpose :: Index
multisigPurpose :: Index
multisigPurpose = Word32 -> Index
Index Word32
1854

-- | External payment chain role.
externalRole :: Index
externalRole :: Index
externalRole = Word32 -> Index
Index Word32
0

-- | Internal change chain role.
internalRole :: Index
internalRole :: Index
internalRole = Word32 -> Index
Index Word32
1

-- | Staking key role (CIP-0011).
stakingRole :: Index
stakingRole :: Index
stakingRole = Word32 -> Index
Index Word32
2

-- | Delegated representative key role (CIP-0105).
drepRole :: Index
drepRole :: Index
drepRole = Word32 -> Index
Index Word32
3

-- | Constitutional committee cold key role (CIP-0105).
committeeColdRole :: Index
committeeColdRole :: Index
committeeColdRole = Word32 -> Index
Index Word32
4

-- | Constitutional committee hot key role (CIP-0105).
committeeHotRole :: Index
committeeHotRole :: Index
committeeHotRole = Word32 -> Index
Index Word32
5

-- | Standard Cardano account prefix for the supplied unencoded account number.
-- Uses purpose 1852 and coin type 1815. For multisignature or custom purposes,
-- use 'accountPath' with the desired components.
standardAccountPath :: Index -> AccountPath
standardAccountPath :: Index -> AccountPath
standardAccountPath = Index -> Index -> Index -> AccountPath
accountPath Index
standardPurpose Index
cardanoCoinType