I got through the minting NFT with Namiwallet.
But is there any way I can mint NFT by Plutus script because as I check the minting function add_mint_asset_and_output_min_required_coin only accept Native Script as the first param?
The code below show how I mint NFT with Native Script
const NFTIndex = 2;
const txBuilder = getTxBuilder();
const hexInputUtxos = await cardanoApi.getUtxos();
// the key hash will be needed for our policy id
let wasmKeyHash;
// add utxos for amount
const txInputsBuilder = CardanoWasm.TxInputsBuilder.new();
for (let i = 0; i < hexInputUtxos.length; i++) {
const wasmUtxo = CardanoWasm.TransactionUnspentOutput.from_bytes(hexToBytes(hexInputUtxos[i]));
txInputsBuilder.add_input(wasmUtxo.output().address(), wasmUtxo.input(), wasmUtxo.output().amount());
if (i == 0) {
wasmKeyHash = CardanoWasm.BaseAddress.from_address(wasmUtxo.output().address()).payment_cred().to_keyhash();
}
}
txBuilder.set_inputs(txInputsBuilder);
// Add the keyhash script to ensure the NFT can only be minted by the corresponding wallet
const keyHashScript = CardanoWasm.NativeScript.new_script_pubkey(CardanoWasm.ScriptPubkey.new(wasmKeyHash));
const ttl = getTtl();
// We then need to add a timelock to ensure the NFT won't be minted again after the given expiry slot
const timelock = CardanoWasm.TimelockExpiry.new(ttl);
const timelockScript = CardanoWasm.NativeScript.new_timelock_expiry(timelock);
// Then the policy script is an "all" script of these two scripts
const scripts = CardanoWasm.NativeScripts.new();
scripts.add(timelockScript);
scripts.add(keyHashScript);
const changeAddress = await cardanoApi.getChangeAddress();
const wasmChangeAddress = CardanoWasm.Address.from_bytes(hexToBytes(changeAddress));
const baseAddress = CardanoWasm.BaseAddress.from_address(wasmChangeAddress);
// const policyScript = CardanoWasm.NativeScript.new_script_all(CardanoWasm.ScriptAll.new(scripts));
const scriptPubKey = CardanoWasm.ScriptPubkey.new(baseAddress.payment_cred().to_keyhash());
const pubKeyScript = CardanoWasm.NativeScript.new_script_pubkey(scriptPubKey);
const policyScripts = CardanoWasm.NativeScripts.new();
policyScripts.add(pubKeyScript);
console.log('policyScripts :>> ', policyScripts);
const policyId = Buffer.from(pubKeyScript.hash(0).to_bytes()).toString('hex');
console.log('policyId :>> ', policyId);
const metadataObj = {
[policyId]: {
['NFT' + NFTIndex.toString()]: {
description: 'Test2',
name: 'Test token2',
id: NFTIndex.toString(),
image: 'ipfs://QmUb8fW7qm1zCLhiKLcFH9yTCZ3hpsuKdkTgKmC8iFhxV8',
},
},
};
let outputBuilder = CardanoWasm.TransactionOutputBuilder.new();
outputBuilder = outputBuilder.with_address(wasmChangeAddress);
txBuilder.add_mint_asset_and_output_min_required_coin(
pubKeyScript,
CardanoWasm.AssetName.new(Buffer.from('NFT' + NFTIndex.toString(), 'utf8')),
CardanoWasm.Int.new_i32(1),
outputBuilder.next(),
);
txBuilder.set_ttl(ttl);
txBuilder.add_json_metadatum(CardanoWasm.BigNum.from_str('721'), JSON.stringify(metadataObj));
txBuilder.add_change_if_needed(wasmChangeAddress);
const unsignedTransactionHex = bytesToHex(txBuilder.build_tx().to_bytes());
cardanoApi
.signTx(unsignedTransactionHex)
.then((witnessSetHex) => {
console.log('witnessSetHex :>> ', witnessSetHex);
const witnessSet = CardanoWasm.TransactionWitnessSet.from_bytes(hexToBytes(witnessSetHex));
witnessSet.set_native_scripts(policyScripts);
const tx = CardanoWasm.Transaction.from_bytes(hexToBytes(unsignedTransactionHex));
const transaction = CardanoWasm.Transaction.new(tx.body(), witnessSet, tx.auxiliary_data());
const transactionHex = bytesToHex(transaction.to_bytes());
setTxHex(transactionHex);
console.log('transactionHex :>> ', transactionHex);
})
.catch((error) => {
console.error(error);
});