{
  "type": "script",
  "title": "Build Certificates",
  "description": "Builds stake registration, deregistration and delegation certificates for the three kinds of stake credential: a key derived from the current wallet, a native script and a Plutus script. Nothing is submitted: the certificates are exported so they can be inspected or reused in a transaction. The target pool of each delegation is exposed as a flat argument, and checked with assert() before anything is built",
  "require": {
    "not": {
      "walletTypeIn": [
        "extension"
      ]
    }
  },
  "args": {
    "user-stake-pool-key-hash": "9eb76bb591d689dd06304c77c0df7871df76e768577e26af10d32b0c",
    "treasury-stake-pool-key-hash": "ff23a2a1c17e0230a68c824c37a088e0abf9f6522f473036719f3460",
    "dex-stake-pool-key-hash": "6d83dc3cc3d0c30628029cb81b111486ab90b5bddbc8ddde94675173"
  },
  "run": {
    "argumentGuards": {
      "type": "macro",
      "run": {
        "user-stake-pool-key-hash-must-be-56-chars": "{ assert( and(eq(get('args.user-stake-pool-key-hash'),truncate(get('args.user-stake-pool-key-hash'),56,0,'')),neq(get('args.user-stake-pool-key-hash'),truncate(get('args.user-stake-pool-key-hash'),55,0,''))) ,'user-stake-pool-key-hash must be exactly 28 bytes (56 hex chars)' , get('args') ) }",
        "user-stake-pool-key-hash-must-not-be-bech32": "{ assert( neq(truncate(get('args.user-stake-pool-key-hash'),4,0,''),'pool') ,'user-stake-pool-key-hash must be the raw hex pool key hash, not a bech32 pool id starting with \"pool1\"' , get('args') ) }",
        "treasury-stake-pool-key-hash-must-be-56-chars": "{ assert( and(eq(get('args.treasury-stake-pool-key-hash'),truncate(get('args.treasury-stake-pool-key-hash'),56,0,'')),neq(get('args.treasury-stake-pool-key-hash'),truncate(get('args.treasury-stake-pool-key-hash'),55,0,''))) ,'treasury-stake-pool-key-hash must be exactly 28 bytes (56 hex chars)' , get('args') ) }",
        "treasury-stake-pool-key-hash-must-not-be-bech32": "{ assert( neq(truncate(get('args.treasury-stake-pool-key-hash'),4,0,''),'pool') ,'treasury-stake-pool-key-hash must be the raw hex pool key hash, not a bech32 pool id starting with \"pool1\"' , get('args') ) }",
        "dex-stake-pool-key-hash-must-be-56-chars": "{ assert( and(eq(get('args.dex-stake-pool-key-hash'),truncate(get('args.dex-stake-pool-key-hash'),56,0,'')),neq(get('args.dex-stake-pool-key-hash'),truncate(get('args.dex-stake-pool-key-hash'),55,0,''))) ,'dex-stake-pool-key-hash must be exactly 28 bytes (56 hex chars)' , get('args') ) }",
        "dex-stake-pool-key-hash-must-not-be-bech32": "{ assert( neq(truncate(get('args.dex-stake-pool-key-hash'),4,0,''),'pool') ,'dex-stake-pool-key-hash must be the raw hex pool key hash, not a bech32 pool id starting with \"pool1\"' , get('args') ) }"
      }
    },
    "derivations": {
      "type": "deriveKeys",
      "keys": {
        "Bob": {
          "name": "Bob",
          "kind": "stake",
          "accountIndex": 34,
          "addressIndex": 423
        }
      }
    },
    "dependencies": {
      "type": "script",
      "run": {
        "nativeScript01": {
          "type": "nativeScript",
          "script": {
            "scriptHex": "82018382041a0005d90d8200581c111fc9cc6fd34bd2783001b3fbf7b456beb93a38d24a1e06542dc6fb82051a0008e909"
          }
        },
        "smartContract01": {
          "type": "plutusScript",
          "script": {
            "lang": "plutus_v2",
            "scriptHex": "4e4d01000033222220051200120011"
          }
        }
      }
    },
    "registrationCerts": {
      "type": "script",
      "exportAs": "RegistrationCerts",
      "run": {
        "User": {
          "type": "certificate",
          "cert": {
            "kind": "StakeRegistration",
            "pubKeyHashHex": "{get('cache.derivations.Bob.pubKeyHashHex')}"
          }
        },
        "Treasury": {
          "type": "certificate",
          "cert": {
            "kind": "StakeRegistration",
            "scriptHashHex": "{get('cache.dependencies.nativeScript01.scriptHashHex')}"
          }
        },
        "Dex": {
          "type": "certificate",
          "cert": {
            "kind": "StakeRegistration",
            "scriptHashHex": "{get('cache.dependencies.smartContract01.scriptHashHex')}"
          }
        }
      }
    },
    "deregistrationCerts": {
      "type": "script",
      "exportAs": "DeregistrationCerts",
      "run": {
        "User": {
          "type": "certificate",
          "cert": {
            "kind": "StakeDeregistration",
            "pubKeyHashHex": "{get('cache.derivations.Bob.pubKeyHashHex')}"
          }
        },
        "Treasury": {
          "type": "certificate",
          "cert": {
            "kind": "StakeDeregistration",
            "scriptHashHex": "{get('cache.dependencies.nativeScript01.scriptHashHex')}"
          }
        },
        "Dex": {
          "type": "certificate",
          "cert": {
            "kind": "StakeDeregistration",
            "scriptHashHex": "{get('cache.dependencies.smartContract01.scriptHashHex')}"
          }
        }
      }
    },
    "delegationCerts": {
      "type": "script",
      "exportAs": "DelegationCerts",
      "args": "{get('args')}",
      "run": {
        "User": {
          "type": "certificate",
          "cert": {
            "kind": "StakeDelegation",
            "pubKeyHashHex": "{get('cache.derivations.Bob.pubKeyHashHex')}",
            "poolKeyHashHex": "{get('args.user-stake-pool-key-hash')}"
          }
        },
        "Treasury": {
          "type": "certificate",
          "cert": {
            "kind": "StakeDelegation",
            "scriptHashHex": "{get('cache.dependencies.nativeScript01.scriptHashHex')}",
            "poolKeyHashHex": "{get('args.treasury-stake-pool-key-hash')}"
          }
        },
        "Dex": {
          "type": "certificate",
          "cert": {
            "kind": "StakeDelegation",
            "scriptHashHex": "{get('cache.dependencies.smartContract01.scriptHashHex')}",
            "poolKeyHashHex": "{get('args.dex-stake-pool-key-hash')}"
          }
        }
      }
    }
  }
}