Skip to content

Commit

Permalink
CIP-1855: Add primitives for MintBurn policy key derivation
Browse files Browse the repository at this point in the history
- Add primitives for deriving policy keys used to mint/burn assets. Implemented
  according to [CIP-1855](https://github.com/cardano-foundation/CIPs/blob/b2e9d02cb9a71ba9e754a432c78197428abf7e4c/CIP-1855/CIP-1855.md).
  • Loading branch information
sevanspowell committed Jul 20, 2021
1 parent 5c15cf2 commit 32a5a5a
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 1 deletion.
1 change: 1 addition & 0 deletions lib/core/cardano-wallet-core.cabal
Expand Up @@ -168,6 +168,7 @@ library
Cardano.Wallet.Primitive.AddressDerivation
Cardano.Wallet.Primitive.AddressDerivation.Byron
Cardano.Wallet.Primitive.AddressDerivation.Icarus
Cardano.Wallet.Primitive.AddressDerivation.MintBurn
Cardano.Wallet.Primitive.AddressDerivation.Shared
Cardano.Wallet.Primitive.AddressDerivation.SharedKey
Cardano.Wallet.Primitive.AddressDerivation.Shelley
Expand Down
9 changes: 8 additions & 1 deletion lib/core/src/Cardano/Wallet/Primitive/AddressDerivation.hs
Expand Up @@ -172,7 +172,14 @@ import qualified Data.Text.Encoding as T
--
-- @m | purpose' | cointype' | account' | role | address@
data Depth
= RootK | PurposeK | CoinTypeK | AccountK | RoleK | AddressK | ScriptK
= RootK
| PurposeK
| CoinTypeK
| AccountK
| RoleK
| AddressK
| ScriptK
| PolicyK

-- | Marker for addresses type engaged. We want to handle four cases here.
-- The first two are pertinent to UTxO accounting,
Expand Down
@@ -0,0 +1,85 @@
-- |
-- Copyright: © 2018-2021 IOHK
-- License: Apache-2.0
--
-- Definition of minted/burned policy keys.
--
-- The policy keys are derived according to the following path:
--
-- m / purpose' / coin_type' / policy_ix'
-- m / 1855' / 1815' / [2^31 .. 2^32-1]'
--
-- Where purpose' and coin_type' are fixed, and each new policy_ix' represents a
-- different policy key.

module Cardano.Wallet.Primitive.AddressDerivation.MintBurn
( -- * Constants
purposeCIP1855
-- * Helpers
, derivePolicyKey
, derivePolicyPrivateKey
) where

import Prelude
import Cardano.Address.Derivation (XPrv)
import Cardano.Address.Script (KeyHash)
import Cardano.Crypto.Wallet.Types (DerivationScheme(DerivationScheme2))
import Cardano.Wallet.Primitive.AddressDerivation
( Passphrase(Passphrase),
DerivationType(Hardened),
Depth(PolicyK, RootK, PurposeK),
Index(getIndex),
liftRawKey,
getRawKey,
hashVerificationKey,
Role(UtxoExternal),
WalletKey(publicKey),
Index(Index) )
import Cardano.Crypto.Wallet (deriveXPrv)
import Cardano.Wallet.Primitive.AddressDiscovery (coinTypeAda)

-- | Purpose for forged policy keys is a constant set to 1855' (or 0x8000073F)
-- following the original CIP-1855: "Forging policy keys for HD Wallets".
--
-- It indicates that the subtree of this node is used according to this
-- specification.
--
-- Hardened derivation is used at this level.
purposeCIP1855 :: Index 'Hardened 'PurposeK
purposeCIP1855 = toEnum 0x8000073F

-- | Derive the policy key that should be used to create mint/burn scripts.
derivePolicyPrivateKey
:: Passphrase purpose
-- ^ Passphrase for wallet
-> XPrv
-- ^ Root private key to derive policy private key from
-> Index 'Hardened 'PolicyK
-- ^ Index of policy script
-> XPrv
-- ^ Policy private key
derivePolicyPrivateKey (Passphrase pwd) rootXPrv (Index policyIx) =
let
purposeXPrv = -- lvl1 derivation; hardened derivation of purpose'
deriveXPrv DerivationScheme2 pwd rootXPrv (getIndex purposeCIP1855)
coinTypeXPrv = -- lvl2 derivation; hardened derivation of coin_type'
deriveXPrv DerivationScheme2 pwd purposeXPrv (getIndex coinTypeAda)
-- lvl3 derivation; hardened derivation of policy' index
in deriveXPrv DerivationScheme2 pwd coinTypeXPrv policyIx

-- | Derive the policy key that should be used to create mint/burn scripts.
derivePolicyKey
:: WalletKey key
=> Passphrase "encryption"
-- ^ Passphrase for wallet
-> key 'RootK XPrv
-- ^ Root private key to derive policy private key from
-> Index 'Hardened 'PolicyK
-- ^ Index of policy script
-> (key 'PolicyK XPrv, KeyHash)
-- ^ Policy private key
derivePolicyKey pwd rootPrv policyIx = (policyK, vkeyHash)
where
policyK = liftRawKey policyPrv
policyPrv = derivePolicyPrivateKey pwd (getRawKey rootPrv) policyIx
vkeyHash = hashVerificationKey UtxoExternal (publicKey policyK)

0 comments on commit 32a5a5a

Please sign in to comment.