Resolvers

Helpful functions for converting Cardano objects

Resolve DataHash

resolveDataHash converts datum into hash. Getting the hash is useful when you need to query for the UTXO that contain the assets you need for your transaction's input.

Explore Transaction to learn more about designing Datum, and learn how to query for UTXOs containing the datum hash.

import { resolveDataHash } from '@meshsdk/core';
import type { Data } from '@meshsdk/core';
const datum: Data = 'supersecretdatum';
const dataHash = resolveDataHash(datum);

Resolve Fingerprint

resolveFingerprint takes policy ID and asset name, and return asset fingerprint based on CIP-14.

import { resolveFingerprint } from '@meshsdk/core';
const hash = resolveFingerprint(
  '426117329844ccb3b0ba877220ff06a5bdf21eab3fb33e2f3a3f8e69',
  'meshtoken'
);

Resolve Native Script Hash

Converts NativeScript into hash.

import { resolveNativeScriptHash, resolvePaymentKeyHash, resolveSlotNo } from '@meshsdk/core';

const keyHash = resolvePaymentKeyHash('addr1v9vx0sacufuypa2k4sngk7q40zc5c4npl337uusdh64kv0c93pyfx');

let oneYearFromNow = new Date();
oneYearFromNow.setFullYear(oneYearFromNow.getFullYear() + 1);
const slot = resolveSlotNo('mainnet', oneYearFromNow.getTime());

const nativeScript: NativeScript = {
  type: 'all',
  scripts: [
    {
      type: 'before',
      slot: slot,
    },
    {
      type: 'sig',
      keyHash: keyHash,
    },
  ],
};

const hash = resolveNativeScriptHash(nativeScript);

Resolve Payment Key Hash

Provide an address, and resolvePaymentKeyHash will return the pub key hash of the payment key. This key hash is useful for building the NativeScript.

import { resolvePaymentKeyHash } from '@meshsdk/core';
const hash = resolvePaymentKeyHash('addr_test1vpvx0sacufuypa2k4sngk7q40zc5c4npl337uusdh64kv0c7e4cxr');

Resolve Script Address

Provide the Plutus script in CBOR, and resolvePlutusScriptAddress will return a bech32 address of the script.

For example, we can get the address of the always succeed smart contract.

import { resolvePlutusScriptAddress } from '@meshsdk/core';
import type { PlutusScript } from '@meshsdk/core';

const script: PlutusScript = {
  code: '4e4d01000033222220051200120011',
  version: 'V1',
};
const address = resolvePlutusScriptAddress(script, 0);

Resolve Plutus Script Hash

Provide the Plutus script address, and resolveScriptHash will return a script hash. This script hash can be use for building minting transaction with Plutus contract.

import { resolvePlutusScriptHash } from '@meshsdk/core';
const hash = resolvePlutusScriptHash('addr_test1wpnlxv2xv9a9ucvnvzqakwepzl9ltx7jzgm53av2e9ncv4sysemm8');

Resolve Private Key

Provide the mnemonic phrases and resolvePrivateKey will return a private key.

import { resolvePrivateKey } from '@meshsdk/core';
const dataHash = resolvePrivateKey(["solution","solution","solution","solution","solution","solution","solution","solution","solution","solution","solution","solution","solution","solution","solution","solution","solution","solution","solution","solution","solution","solution","solution","solution"]);

Resolve Stake Address

Provide a wallet address, and resolveRewardAddress will return a staking address in bech32 format.

import { resolveRewardAddress } from '@meshsdk/core';
const rewardAddress = resolveRewardAddress('addr_test1qzl2r3fpmav0fmh0vrry0e0tmzxxqwv32sylnlty2jj8dwg636sfudakhsh65qggs4ttjjsk8fuu3fkd65uaxcxv0tfqv3z0y3');

Resolve Stake Key Hash

Provide a stake address, and resolveStakeKeyHash will return the pub key hash of the stake address. This key hash is useful for building the NativeScript.

import { resolveStakeKeyHash } from '@meshsdk/core';
const hash = resolveStakeKeyHash('stake1u93r8fsv43jyuw84yv4xwzfmka5sms5u5karqjysw2jszaq2kapyl');

Resolve Transaction Hash

Provide a cborTx, resolveTxHash will return the transaction hash.

import { resolveTxHash } from '@meshsdk/core';
const tx = new Transaction({ initiator: wallet });
tx.sendLovelace(demoAddresses.testnet, '1500000');
const unsignedTx = await tx.build();
const hash1 = resolveTxHash(unsignedTx);
const signedTx = await wallet.signTx(unsignedTx, false);
const hash2 = resolveTxHash(signedTx);
const txHash = await wallet.submitTx(signedTx);
// txHash == hash1 == hash2

Resolve Slot Number

With resolveSlotNo, you can get the current slot number with:

import { resolveSlotNo } from '@meshsdk/core';
const slot = resolveSlotNo('mainnet');

You can also provide date in milliseconds to get slots in the past or the future. For example, get the slot number 1 year from now:

import { resolveSlotNo } from '@meshsdk/core';
let oneYearFromNow = new Date();
oneYearFromNow.setFullYear(oneYearFromNow.getFullYear() + 1);
const slot = resolveSlotNo('mainnet', oneYearFromNow.getTime());

Resolve Epoch Number

With resolveEpochNo, you can get the current epoch with:

import { resolveEpochNo } from '@meshsdk/core';
const epoch = resolveEpochNo('mainnet');

You can also provide date in milliseconds to get epoch in the past or the future. For example, get the epoch 1 year from now:

import { resolveEpochNo } from '@meshsdk/core';
let oneYearFromNow = new Date();
oneYearFromNow.setFullYear(oneYearFromNow.getFullYear() + 1);
const epoch = resolveEpochNo('mainnet', oneYearFromNow.getTime());