Skip to content

Commit 98b223b

Browse files
authored
Merge branch 'master' into sg/patch-wasm-bindgen
2 parents d9c96f8 + f2118cb commit 98b223b

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

69 files changed

+22001
-2132
lines changed

.github/workflows/sonarqube.yml renamed to .github/workflows/_sonarqube.yml_

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,9 @@ jobs:
2020
# Disabling shallow clone is recommended for improving relevancy of reporting
2121
fetch-depth: 0
2222
- name: SonarQube Scan
23-
uses: sonarsource/[email protected]
23+
uses: sonarsource/[email protected]
24+
with:
25+
projectBaseDir: "rust"
2426
env:
2527
SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }}
2628
SONAR_HOST_URL: ${{ secrets.SONAR_HOST_URL }}

doc/getting-started/metadata_tx.ts

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
import * as CSL from "@emurgo/cardano-serialization-lib-nodejs";
2+
import { mnemonicToEntropy } from "bip39";
3+
import { Buffer } from "node:buffer";
4+
5+
const MNEMONIC = "key in your 24 words of your mnemonic here, words separated by spaces";
6+
const INPUT_HASH ="9fc9bb3ea1f2540ae870076e6543b5d804566a548db9da9e16c5271596e8dc9d"
7+
const INPUT_INDEX =1;
8+
const INPUT_AMOUNT = "113185492" ; //Lovelace on your UTXO
9+
const TO_ADDRESS="addr_test1your_address_in_bech32";
10+
const AMOUNT="2000000";
11+
12+
function harden(num: number): number {
13+
return 0x80000000 + num;
14+
}
15+
16+
//Step 1.========= Retrieve root key===============
17+
const entropy = mnemonicToEntropy(MNEMONIC);
18+
// retrieve root key in hex
19+
const rootKey = CSL.Bip32PrivateKey.from_bip39_entropy(
20+
Buffer.from(entropy, "hex"),
21+
Buffer.from("")
22+
);
23+
24+
// Retrieve child private key at m/1852H/1815H/0H/0/0 and m/1852H/1815H/0H/2/0
25+
const accountKey = rootKey
26+
.derive(harden(1852))
27+
.derive(harden(1815))
28+
.derive(harden(0));
29+
const utxoPrivKey = accountKey.derive(0).derive(0);
30+
const stakePrivKey = accountKey.derive(2).derive(0);
31+
const paymentKey = utxoPrivKey.to_raw_key();
32+
33+
// Retrieve payment credential + stake credential
34+
const addr = CSL.BaseAddress.new(
35+
CSL.NetworkInfo.testnet_preprod().network_id(),
36+
CSL.Credential.from_keyhash(utxoPrivKey.to_public().to_raw_key().hash()),
37+
CSL.Credential.from_keyhash(stakePrivKey.to_public().to_raw_key().hash()) ///not need for non-stake tx
38+
);
39+
40+
//Step 2.========= instantiate the tx builder with the Cardano protocol parameters - these may change later on===============
41+
const linearFee = CSL.LinearFee.new(
42+
CSL.BigNum.from_str("44"),
43+
CSL.BigNum.from_str("155381")
44+
);
45+
const txBuilderCfg = CSL.TransactionBuilderConfigBuilder.new()
46+
.fee_algo(linearFee)
47+
.pool_deposit(CSL.BigNum.from_str("500000000"))
48+
.key_deposit(CSL.BigNum.from_str("2000000"))
49+
.max_value_size(5000)
50+
.max_tx_size(16384)
51+
.coins_per_utxo_byte(CSL.BigNum.from_str("4310"))
52+
.build();
53+
54+
const txBuilder = CSL.TransactionBuilder.new(txBuilderCfg);
55+
56+
//Step 3.========= Define and add inputs to transaction=================
57+
const txInputsBuilder = CSL.TxInputsBuilder.new();
58+
txInputsBuilder.add_regular_input(addr.to_address(),
59+
CSL.TransactionInput.new(CSL.TransactionHash.from_hex(INPUT_HASH), INPUT_INDEX),
60+
CSL.Value.new(CSL.BigNum.from_str(INPUT_AMOUNT))
61+
);
62+
txBuilder.set_inputs(txInputsBuilder);
63+
64+
//Step 4.========= Define and add output to the tx to transaction========
65+
const DESTINATION_ADDRESS = CSL.Address.from_bech32(TO_ADDRESS);
66+
txBuilder.add_output(
67+
CSL.TransactionOutput.new(DESTINATION_ADDRESS,
68+
CSL.Value.new(CSL.BigNum.from_str(AMOUNT))
69+
),
70+
);
71+
72+
//Step 5.========= Define and add metadata tx to transaction========
73+
const metadata = CSL.GeneralTransactionMetadata.new();
74+
const metadataKey = CSL.BigNum.from_str("674");
75+
const metadataValue = CSL.encode_json_str_to_metadatum(
76+
JSON.stringify({ message: "hello metadata" }),
77+
CSL.MetadataJsonSchema.BasicConversions
78+
);
79+
metadata.insert(metadataKey, metadataValue);
80+
81+
// add metadata as auxiliary data to tx.
82+
const auxData = CSL.AuxiliaryData.new();
83+
auxData.set_metadata(metadata);
84+
txBuilder.set_auxiliary_data(auxData);
85+
86+
// calculate the min fee required and send any change to an address
87+
txBuilder.add_change_if_needed(addr.to_address());
88+
89+
90+
const tx = txBuilder.build_tx()
91+
const fixedTx = CSL.FixedTransaction.from_bytes(tx.to_bytes());
92+
let txHash = fixedTx.transaction_hash();
93+
94+
// Step 6.=========Make and add vkey witness if it necessary========
95+
const vkeyWitness = CSL.make_vkey_witness(txHash,paymentKey);
96+
fixedTx.add_vkey_witness(vkeyWitness)
97+
98+
// Step 7.=========Serialize Transaction to hex========
99+
const txSerialized = fixedTx.to_hex();
100+
console.log("Transaction serialized:", txSerialized);
101+
102+
103+
104+

doc/getting-started/minint_nft.ts

Lines changed: 171 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,171 @@
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

Comments
 (0)