Plutus.V1.Ledger.Address

  • 1. πŸ“œ Overview

  • 2. πŸ”§ Language Extensions and Imports

  • 3. πŸ—„οΈ Data Structures

    • 3.1 πŸ†• newtype Example

    • 3.2 πŸ”€ type Synonym Example

    • 3.3 πŸ“¦ data Type Example

    • 3.4 🏠 Address Data Type

  • 4. βš™οΈ Functions

    • 4.1 πŸ”‘ pubKeyHashAddress

    • 4.2 πŸ” toPubKeyHash

    • 4.3 πŸ“œ toValidatorHash

    • 4.4 🏷️ scriptHashAddress

    • 4.5 🎰 stakingCredential

  • 5. 🀝 Typeclass Instances

  • 6. πŸ“š Glossary


1 πŸ“œ Overview

This tutorial walks through a Plutus Address script in Haskell:

  • Defines a custom Address data type with payment and staking credentials.

  • Implements helper functions to build and query addresses.

  • Shows how to derive instances for on-chain data (IsData) and lifting.

  • Explains language extensions, imports, and related typeclasses.

It also illustrates basic newtype, type synonym, and data definitions for comparison.


2 πŸ”§ Language Extensions and Imports

  • DerivingStrategies: chooses between stock and anyclass for deriving.

  • DeriveAnyClass: allows deriving classes like NFData.

  • TemplateHaskell: used by PlutusTx.makeIsDataIndexed and makeLift for on-chain code.

  • OverloadedStrings: for convenient string literals in Prettyprinter.

  • INLINABLE pragma**: marks functions for on-chain compilation and optimization.

Imports:

  • NFData, Generic: for strict evaluation and generic deriving.

  • PlutusTx modules: on-chain utilities (Bool, Eq, makeIsDataIndexed, makeLift).

  • Prettyprinter: for human-readable Pretty instance.

  • Ledger.Credential, Crypto, Scripts: on-chain ledger types (PubKeyHash, ValidatorHash, Credential).


3 πŸ—„οΈ Data Structures

Below are examples of the three Haskell type definitions, followed by our Address.

3.1 πŸ†• newtype Example

  • Inputs: Int value

  • Processing: none (zero-cost wrapper)

  • Output: MyInt

3.2 πŸ”€ type Synonym Example

  • Inputs/Outputs: identical to String, just renamed.

3.3 πŸ“¦ data Type Example

  • Inputs: Double parameters

  • Processing: none at construction

  • Outputs: Shape value

3.4 🏠 Address Data Type

  • Fields:

    • addressCredential: on-chain Credential (either a public key or script)

    • addressStakingCredential: optional StakingCredential

  • Derivations:

    • Eq, Ord, Show, Generic via stock

    • NFData via anyclass

Example Usage:


4 βš™οΈ Functions

Each function below is marked {-# INLINABLE #-} for on-chain use.

4.1 πŸ”‘ pubKeyHashAddress

  • Inputs: PubKeyHash

  • Processing: wraps into PubKeyCredential, sets no staking

  • Output: Address

Example:

4.2 πŸ” toPubKeyHash

  • Inputs: Address

  • Processing: pattern match on Credential

  • Output: Maybe PubKeyHash

Examples:

4.3 πŸ“œ toValidatorHash

  • Inputs: Address

  • Processing: pattern match on ScriptCredential

  • Output: Maybe ValidatorHash

Example:

4.4 🏷️ scriptHashAddress

  • Inputs: ValidatorHash

  • Processing: wraps into ScriptCredential, no staking

  • Output: Address

Example:

4.5 🎰 stakingCredential

  • Inputs: Address

  • Processing: field projection

  • Output: Maybe StakingCredential

Example:


5 🀝 Typeclass Instances

  • Class: Pretty from Prettyprinter

  • Method: pretty :: a -> Doc ann

  • Behavior: prints the payment credential and staking in parentheses.

  • Class: Eq from PlutusTx, used on-chain

  • Method: (==) must be INLINABLE

On-chain Derivation:

  • makeIsDataIndexed: generates IsData instance with constructor index

  • makeLift: allows lifting Haskell values into on-chain code


6 πŸ“š Glossary

  • Address: On-chain identifier combining payment & staking credentials.

  • Credential: Either PubKeyCredential PubKeyHash or ScriptCredential ValidatorHash.

  • StakingCredential: Optional credential for staking rewards.

  • PubKeyHash: Hash of a public key.

  • ValidatorHash: Hash of a validator script.

  • INLINABLE: GHC pragma making function available for specialization.

  • makeIsDataIndexed: Template Haskell for on-chain data serialization.

  • makeLift: Template Haskell for lifting to on-chain.

  • newtype: Zero-cost wrapper around a single field type.

  • type: Synonym for another type.

  • data: Defines a new algebraic data type.

  • NFData: Class for deep evaluation (rnf).

  • Pretty: Class for pretty-printing values.

Last updated