Mesh LogoMesh

Minting Application

Mint assets with MeshWallet on Node.js.

System setup

1. Visual Studio Code

Download and install Visual Studio Code.

2. Node.js

Install the Long-Term Support (LTS) version of Node.js.

Project setup

Create a new folder and initialize a Node.js project:

npm init

Install typescript and Mesh:

npm install --dev typescript && npm install @meshsdk/core

Initialize TypeScript:

npx tsc --init

Open tsconfig.json and define the configurations:

{
  ...
  "target": "ESNext",
  "module": "ESNext",
  "moduleResolution": "Node",
  "outDir": "dist",
  ...
}

Open package.json and add the configurations:

{
  ...
  "type": "module",
  "scripts": {
    "start": "tsc && node ./dist/main.js"
  }
  ...
}

Build the minting transaction

1. Create list of NFT's metadata

Create metadata.ts and define the NFT metadata:

export const metadata: { [assetName: string]: any } = {
  MeshToken01: {
    name: "Mesh Token 1",
    image: "ipfs://QmRzicpReutwCkM6aotuKjErFCUD213DpwPq6ByuzMJaua",
    mediaType: "image/jpg",
    description: "Just a purple coin.",
    artist: "This NFT was minted by Mesh (https://meshjs.dev/).",
  },
  MeshToken02: {
    name: "Mesh Token 2",
    image: "ipfs://QmRzicpReutwCkM6aotuKjErFCUD213DpwPq6ByuzMJaua",
    mediaType: "image/jpg",
    description: "This is suppose to be a gold coin.",
    artist: "This NFT was minted by Mesh (https://meshjs.dev/).",
  },
  MeshToken03: {
    name: "Mesh Token 3",
    image: "ipfs://QmRzicpReutwCkM6aotuKjErFCUD213DpwPq6ByuzMJaua",
    mediaType: "image/jpg",
    description: "A coin with a M on it.",
    artist: "This NFT was minted by Mesh (https://meshjs.dev/).",
  },
};

2. Create a list of recipients

Create recipients.ts and specify the recipients:

export const recipients: { [recipient: string]: string } = {
  addr_test1vpvx0sacufuypa2k4sngk7q40zc5c4npl337uusdh64kv0c7e4cxr:
    "MeshToken01",
  addr_test1qqlcxawu4gxarenqvdqyw0tqyjy69mrgsmfqhm6h65jwm4vvldqg2n2p8y4kyjm8sqfyg0tpq9042atz0fr8c3grjmyscxry4r:
    "MeshToken02",
  addr_test1qq5tay78z9l77vkxvrvtrv70nvjdk0fyvxmqzs57jg0vq6wk3w9pfppagj5rc4wsmlfyvc8xs7ytkumazu9xq49z94pqzl95zt:
    "MeshToken03",
};

3. Create main.ts and import the packages:

Create main.ts and import the required packages and files:

import {
  MeshWallet,
  Transaction,
  ForgeScript,
  BlockfrostProvider,
  resolveTxHash,
} from '@meshsdk/core';
import type { Mint, AssetMetadata } from '@meshsdk/core';


import { metadata } from './metadata.js';
import { recipients } from './recipients.js';

4. Define variables

Define minting variables. Use your own wallet to mint your own collection. This example uses:

const demoCLIKey = {
  paymentSkey:
    '5820aaca553a7b95b38b5d9b82a5daa7a27ac8e34f3cf27152a978f4576520dd6503',
  stakeSkey:
    '582097c458f19a3111c3b965220b1bef7d548fd75bc140a7f0a4f080e03cce604f0e',
};
const networkId = 0;
const blockfrostKey = 'BLOCKFROST_KEY_HERE';

5. Build the minting transaction

This guide builds a minting transaction, but the process applies to any transaction. Learn more about Transaction.

Initialize a blockchain provider. This guide uses BlockfrostProvider:

const provider = new BlockfrostProvider(blockfrostKey);

Initialize MeshWallet and its forging script. This example uses CLI generated keys, but you can load your wallet with a private key or mnemonic phrase. Learn more about MeshWallet.

const wallet = new MeshWallet({
  networkId: networkId,
  fetcher: provider,
  submitter: provider,
  key: {
    type: 'cli',
    payment: demoCLIKey.paymentSkey,
    stake: demoCLIKey.stakeSkey,
  },
});


const walletAddress = wallet.getPaymentAddress();
const forgingScript = ForgeScript.withOneSignature(walletAddress);

Create a new Transaction, loop through each recipient, and mint assets with mintAsset (Learn more about minting transactions):

const tx = new Transaction({ initiator: wallet });
for (let recipient in recipients) {
  const recipientAddress = recipient;
  const assetName = recipients[recipient];
  const assetMetadata: AssetMetadata = metadata[assetName];
  const asset: Mint = {
    assetName: assetName,
    assetQuantity: '1',
    metadata: assetMetadata,
    label: '721',
    recipient: recipientAddress
  };
  tx.mintAsset(forgingScript, asset);
}

Sign and submit the transaction:

const unsignedTx = await tx.build();
const signedTx = await wallet.signTx(unsignedTx, false);
const txHash = await wallet.submitTx(signedTx);

Execute the script:

npm start

A successful transaction returns a transaction hash, mints multiple assets, and sends them to multiple recipients.