-
Notifications
You must be signed in to change notification settings - Fork 210
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
152 additions
and
119 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
{-# LANGUAGE DataKinds #-} | ||
{-# LANGUAGE GADTs #-} | ||
{-# LANGUAGE LambdaCase #-} | ||
{-# LANGUAGE RankNTypes #-} | ||
|
||
module Cardano.Wallet.Address.Keys.MintBurn | ||
( derivePolicyKeyAndHash | ||
, toTokenMapAndScript | ||
, toTokenPolicyId | ||
) where | ||
|
||
import Prelude | ||
|
||
import Cardano.Wallet.Address.Derivation | ||
( Index, DerivationType (..), Depth (..)) | ||
import Cardano.Address.Script | ||
( Cosigner, KeyHash, Script (..), ScriptHash (unScriptHash), toScriptHash ) | ||
import Cardano.Wallet.Address.Keys.WalletKey | ||
( AfterByron, hashVerificationKeyNew, liftRawKeyNew, getRawKeyNew, publicKeyNew ) | ||
import Cardano.Wallet.Flavor | ||
( KeyFlavorS ) | ||
import Cardano.Wallet.Primitive.Types.Hash | ||
( Hash (..) ) | ||
import Cardano.Wallet.Primitive.Types.TokenMap | ||
( AssetId (..) ) | ||
import Cardano.Wallet.Primitive.Types.TokenPolicy | ||
( TokenName, TokenPolicyId (..) ) | ||
import Cardano.Wallet.Primitive.Types.TokenQuantity | ||
( TokenQuantity (..) ) | ||
import Data.Map | ||
( Map ) | ||
import GHC.Natural | ||
( Natural ) | ||
import GHC.Stack | ||
( HasCallStack ) | ||
|
||
import qualified Cardano.Address.Script as CA | ||
import qualified Data.Map as Map | ||
import Cardano.Wallet.Primitive.Passphrase (Passphrase(..)) | ||
import Cardano.Address.Derivation (XPrv, XPub) | ||
import Cardano.Wallet.Address.Derivation.MintBurn (derivePolicyPrivateKey) | ||
|
||
toTokenPolicyId | ||
:: forall key | ||
. (HasCallStack, AfterByron key ~ 'True) | ||
=> KeyFlavorS key | ||
-> Script Cosigner | ||
-> Map Cosigner XPub | ||
-> TokenPolicyId | ||
toTokenPolicyId kf scriptTempl cosignerMap = | ||
UnsafeTokenPolicyId | ||
. Hash | ||
. unScriptHash | ||
. toScriptHash | ||
$ replaceCosigner kf cosignerMap scriptTempl | ||
|
||
toTokenMapAndScript | ||
:: forall key | ||
. (HasCallStack, AfterByron key ~ 'True) | ||
=> KeyFlavorS key | ||
-> Script Cosigner | ||
-> Map Cosigner XPub | ||
-> TokenName | ||
-> Natural | ||
-> (AssetId, TokenQuantity, Script KeyHash) | ||
toTokenMapAndScript kf scriptTempl cosignerMap tName val = | ||
( AssetId (toTokenPolicyId kf scriptTempl cosignerMap) tName | ||
, TokenQuantity val | ||
, replaceCosigner kf cosignerMap scriptTempl | ||
) | ||
|
||
replaceCosigner | ||
:: forall key | ||
. (HasCallStack, AfterByron key ~ 'True) | ||
=> KeyFlavorS key | ||
-> Map Cosigner XPub | ||
-> Script Cosigner | ||
-> Script KeyHash | ||
replaceCosigner kf cosignerMap = \case | ||
RequireSignatureOf c -> | ||
RequireSignatureOf $ toKeyHash c | ||
RequireAllOf xs -> | ||
RequireAllOf (map (replaceCosigner kf cosignerMap) xs) | ||
RequireAnyOf xs -> | ||
RequireAnyOf (map (replaceCosigner kf cosignerMap) xs) | ||
RequireSomeOf m xs -> | ||
RequireSomeOf m (map (replaceCosigner kf cosignerMap) xs) | ||
ActiveFromSlot s -> | ||
ActiveFromSlot s | ||
ActiveUntilSlot s -> | ||
ActiveUntilSlot s | ||
where | ||
toKeyHash :: HasCallStack => Cosigner -> KeyHash | ||
toKeyHash c = case Map.lookup c cosignerMap of | ||
Just xpub -> hashVerificationKeyNew kf CA.Policy (liftRawKeyNew kf xpub) | ||
Nothing -> error "Impossible: cosigner without xpub." | ||
|
||
|
||
-- | Derive the policy private key that should be used to create mint/burn | ||
-- scripts, as well as the key hash of the policy public key. | ||
derivePolicyKeyAndHash | ||
:: AfterByron key ~ 'True | ||
=> KeyFlavorS 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 | ||
derivePolicyKeyAndHash kf pwd rootPrv policyIx = (policyK, vkeyHash) | ||
where | ||
policyK = liftRawKeyNew kf policyPrv | ||
policyPrv = derivePolicyPrivateKey pwd (getRawKeyNew kf rootPrv) policyIx | ||
vkeyHash = hashVerificationKeyNew kf CA.Payment (publicKeyNew kf policyK) |
Oops, something went wrong.