|
| 1 | +import { |
| 2 | + LinearFee, |
| 3 | + PrivateKey, |
| 4 | + BigNum, |
| 5 | + TransactionBuilderConfigBuilder, |
| 6 | + TransactionBuilder, |
| 7 | + NativeScripts, |
| 8 | + NativeScript, |
| 9 | + ScriptPubkey, |
| 10 | + ScriptAll, |
| 11 | + AssetName, |
| 12 | + Int, |
| 13 | + TransactionOutputBuilder, |
| 14 | + TransactionWitnessSet, |
| 15 | + Vkeywitnesses, |
| 16 | + make_vkey_witness, |
| 17 | + Transaction, |
| 18 | + Bip32PrivateKey, |
| 19 | + BaseAddress, |
| 20 | + NetworkInfo, |
| 21 | + Credential, |
| 22 | + FixedTransaction, |
| 23 | + TransactionInput, |
| 24 | + TransactionHash, |
| 25 | + Value, |
| 26 | +} from "@emurgo/cardano-serialization-lib-nodejs-gc"; |
| 27 | +import { mnemonicToEntropy } from "bip39"; |
| 28 | +import { Buffer } from "node:buffer"; |
| 29 | +import cbor from "cbor"; |
| 30 | + |
| 31 | +const mintNft = async ( |
| 32 | + privateKey, |
| 33 | + policyPrivateKey, |
| 34 | + assetName, |
| 35 | + description, |
| 36 | + imageUrl, |
| 37 | + mediaType, |
| 38 | + tx_hash, |
| 39 | + tx_index, |
| 40 | + amount |
| 41 | + ) => |
| 42 | + { |
| 43 | + //==============Retrieve publicKey, addr, policyPubKey, PolicyAddr from private keys =============== |
| 44 | + const publicKey = privateKey.to_public(); |
| 45 | + const addr = BaseAddress.new( |
| 46 | + NetworkInfo.testnet_preprod().network_id(), |
| 47 | + Credential.from_keyhash(publicKey.hash()), |
| 48 | + Credential.from_keyhash(publicKey.hash()) |
| 49 | + ).to_address(); |
| 50 | + |
| 51 | + const policyPubKey = policyPrivateKey.to_public(); |
| 52 | + const policyKeyHash = policyPubKey.hash(); |
| 53 | + const policyAddr = BaseAddress.new( |
| 54 | + NetworkInfo.testnet_preprod().network_id(), |
| 55 | + Credential.from_keyhash(policyPubKey), |
| 56 | + Credential.from_keyhash(policyPubKey) |
| 57 | + ).to_address(); |
| 58 | + |
| 59 | + |
| 60 | + |
| 61 | + //==============Sets transaction parameters like fees, deposits, and size limits================ |
| 62 | + const linearFee = LinearFee.new( |
| 63 | + BigNum.from_str("44"), |
| 64 | + BigNum.from_str("155381") |
| 65 | + ); |
| 66 | + const txBuilderCfg = TransactionBuilderConfigBuilder.new() |
| 67 | + .fee_algo(linearFee) |
| 68 | + .pool_deposit(BigNum.from_str("500000000")) |
| 69 | + .key_deposit(BigNum.from_str("2000000")) |
| 70 | + .max_value_size(5000) |
| 71 | + .max_tx_size(16384) |
| 72 | + .coins_per_utxo_byte(BigNum.from_str("4310")) |
| 73 | + .build(); |
| 74 | + const txBuilder = TransactionBuilder.new(txBuilderCfg); |
| 75 | + const scripts = NativeScripts.new(); |
| 76 | + |
| 77 | + //==============add key hash script so only people with policy key can mint assets using this policyId |
| 78 | + const keyHashScript = NativeScript.new_script_pubkey( |
| 79 | + ScriptPubkey.new(policyKeyHash) |
| 80 | + ); |
| 81 | + scripts.add(keyHashScript); |
| 82 | + |
| 83 | + const mintScript = NativeScript.new_script_all( |
| 84 | + ScriptAll.new(scripts) |
| 85 | + ); |
| 86 | + |
| 87 | + const paymentKeyHash = BaseAddress.from_address(addr) |
| 88 | + .payment_cred() |
| 89 | + .to_keyhash(); |
| 90 | + |
| 91 | + txBuilder.add_key_input( |
| 92 | + paymentKeyHash, |
| 93 | + TransactionInput.new( |
| 94 | + TransactionHash.from_hex(tx_hash), |
| 95 | + tx_index |
| 96 | + ), |
| 97 | + Value.new(BigNum.from_str(amount)) |
| 98 | + ); |
| 99 | + |
| 100 | + txBuilder.add_mint_asset_and_output_min_required_coin( |
| 101 | + mintScript, |
| 102 | + AssetName.new(Buffer.from(assetName)), |
| 103 | + Int.new_i32(1), |
| 104 | + TransactionOutputBuilder.new().with_address(addr).next() |
| 105 | + ); |
| 106 | + const policyId = Buffer.from(mintScript.hash().to_bytes()).toString("hex"); |
| 107 | + const metadata = { |
| 108 | + [policyId]: { |
| 109 | + [assetName]: { |
| 110 | + name: assetName, |
| 111 | + description, |
| 112 | + image: imageUrl, |
| 113 | + mediaType, |
| 114 | + }, |
| 115 | + }, |
| 116 | + }; |
| 117 | + |
| 118 | + console.log(`METADATA: ${JSON.stringify(metadata, null, 4)}`); |
| 119 | + |
| 120 | + txBuilder.add_json_metadatum( |
| 121 | + BigNum.from_str("721"), |
| 122 | + JSON.stringify(metadata) |
| 123 | + ); |
| 124 | + |
| 125 | + txBuilder.add_change_if_needed(addr); |
| 126 | + |
| 127 | + const tx = txBuilder.build_tx(); |
| 128 | + const fixedTx = FixedTransaction.from_bytes(tx.to_bytes()); |
| 129 | + let txHash = fixedTx.transaction_hash(); |
| 130 | + |
| 131 | + console.log(`TX_HASH: ${txHash.to_hex()}`); |
| 132 | + |
| 133 | + //==============Add signatures===================== |
| 134 | + fixedTx.sign_and_add_vkey_signature(privateKey); |
| 135 | + fixedTx.sign_and_add_vkey_signature(policyPrivateKey); |
| 136 | + |
| 137 | + const unsignedTx = txBuilder.build_tx(); |
| 138 | + |
| 139 | + const signedTx = fixedTx.to_hex(); |
| 140 | + console.log(`Minting NFT siged tx: ${signedTx}`) |
| 141 | +}; |
| 142 | + |
| 143 | +try { |
| 144 | + const privateKey = PrivateKey.from_bech32( |
| 145 | + "ed25519e_sk1your_key" |
| 146 | + ); |
| 147 | + const policyPrivateKey = PrivateKey.from_bech32( |
| 148 | + "ed25519e_sk1your_key" |
| 149 | + ); |
| 150 | + |
| 151 | + //==============Select UTXO that is probably big enough to pay the tx fee=============== |
| 152 | + const tx_hash="5a4925b330916e62307766802f5af4ce8b234c27de8271a901086c08733da0f1"; |
| 153 | + const tx_index="1"; |
| 154 | + const amount="31009807"; |
| 155 | + await mintNft( |
| 156 | + privateKey, |
| 157 | + { |
| 158 | + privateKey: policyPrivateKey |
| 159 | + }, |
| 160 | + "CSL_101", // assetName |
| 161 | + "Description about NFT", // description |
| 162 | + "ipfs://QmSUfz3aeFjufYo9RnQauBaoQhGD27BwMYzZSvtsJ714BP", // image url |
| 163 | + "image/jpeg", // mediaType |
| 164 | + tx_hash, |
| 165 | + tx_index, |
| 166 | + amount |
| 167 | +); |
| 168 | +} |
| 169 | +catch (err) { |
| 170 | + console.error(`failed to mint nft: ${err.toString()}`); |
| 171 | +} |
0 commit comments