From 701c6c58daec53a499c3b56603e90246319a539c Mon Sep 17 00:00:00 2001 From: Aleksandr Penskoi Date: Sat, 11 Jun 2022 00:51:28 +0300 Subject: [PATCH 1/2] Rewriting 'Mint you own NFT' in script, update readme. --- README.md | 137 ++++++++++++----------- flake.nix | 11 +- plutus-use-cases | 2 +- scripts/mint-nft.sh | 225 ++++++++++++++++++++++++++++++++++++++ scripts/prepare-wallet.sh | 21 ++++ 5 files changed, 326 insertions(+), 70 deletions(-) create mode 100755 scripts/mint-nft.sh create mode 100755 scripts/prepare-wallet.sh diff --git a/README.md b/README.md index 743c12b..f08cb07 100644 --- a/README.md +++ b/README.md @@ -69,73 +69,80 @@ Ensure that Nami is set to Testnet, that you have some Test Ada, and that you've ### Optional: Mint your own NFTs -This process will be simplified in the future. +#### Start plutus-chain-index +Set environment variables: + +``` shell +$ pwd +.../seabug +$ export CARDANO_NODE_SOCKET_PATH=$PWD/data/cardano-node/ipc/node.socket +$ mkdir -p chain-index +$ export CHAIN_INDEX_PATH=$PWD/chain-index/chain-index.sqlite +``` + +Fix permission problem for `node.socket` (if you receive error like: `plutus-chain-index: Network.Socket.connect: : permission denied (Permission denied)`): + +``` shell +$ sudo chmod 0666 $CARDANO_NODE_SOCKET_PATH +``` + +Building and run plutus-chain-index from the source: + +``` +$ cd .. +$ git clone git@github.com:input-output-hk/plutus-apps.git +$ cd plutus-apps +$ nix build -f default.nix plutus-chain-index +$ result/bin/plutus-chain-index start-index --network-id 1097911063 --db-path $CHAIN_INDEX_PATH/chain-index.sqlite --socket-path $CARDANO_NODE_SOCKET_PATH +``` + +The index should be synced for minting. + +#### Prepare wallet + +``` shell +$ cd seabug +$ nix develop +$ scripts/prepare-wallet.sh +new wallet generated: +address: addr_test1vp3tywa08qjjj7mplzmwjs9kmes0ce3ud5da3x0wppu5e9qgxqhps +PHK: 62b23baf3825297b61f8b6e940b6de60fc663c6d1bd899ee08794c94 +file: payment.addr +file: payment.vkey +file: pab/signing-keys/signing-key-62b23baf3825297b61f8b6e940b6de60fc663c6d1bd899ee08794c94.skey +``` + +Add some Ada to your wallet: +- by Nami wallet +- or by [Faucet](https://testnets.cardano.org/en/testnets/cardano/tools/faucet/) + +Check the result: + ```shell -$ # Setup server admin token, password: seabug -$ PGPASSWORD=seabug psql -U seabug -h localhost -c "INSERT INTO admin_token(token) VALUES ('ADMIN_TOKEN')" - -$ # Upload image -$ curl --location --request POST "locahost:8008" \ - -F "image=@./cat123.png" \ - -F "title=Cat Cat number 123" \ - -F "description=Cat eating piece of cheese" \ - -H "Authorization: ADMIN_TOKEN" - -$ # Get IPFS CID, replace SHA_256_HASH with hash from previous response, note "ipfsHash" -$ curl --location --request GET 'localhost:8008' \ - | jq 'to_entries[] | select (.value.sha256hash=="SHA_256_HASH")' - -$ # Convert CID, replace "IPFS_HASH" with hash from previous response, note the result -$ ipfs cid format -b=base36 IPFS_HASH - -$ # Configure keys for BPI -$ cd plutus-use-cases/mlabs -$ cardano-cli address build \ - --payment-verification-key-file payment.vkey \ - --out-file payment.addr \ - --testnet-magic 1097911063 -$ cardano-cli address key-gen \ - --verification-key-file payment.vkey \ - --signing-key-file payment.skey -$ mkdir -p pab/signing-keys -$ cat payment.addr -$ mv payment.skey pab/signing-keys/signing-key-PKH_HERE.skey - -$ # Start BPI, note "minting-policy", it will be used later -$ nix develop -L -c cabal run efficient-nft-pab - -$ # In other console -$ # Mint underlying CNFTs, replace "CONVERTED_CID" with the result of `ipfs` command -$ curl --location --request POST 'localhost:3003/api/contract/activate' \ - --header 'Content-Type: application/json' \ - --data-raw ' - { - "caID": { - "tag":"MintCnft", - "contents":[ - { - "mc'"'"'name":"Cat number 123", - "mc'"'"'description":"Cat eating piece of cheese", - "mc'"'"'image":"ipfs://CONVERTED_CID", - "mc'"'"'tokenName":"cat-123" # This should be hex encoded (without 0x) - } - ] - } - }' - -$ # Go back to previous terminal and stop BPI -$ # Replace "CURRENCY_SYMBOL" in /efficient-nft-pab/Main.hs with currency symbol from BPI log -$ # Restart BPI, note "seabug-mint-request" -$ nix develop -L -c cabal run efficient-nft-pab - -$ # Mint SeaBug NFT -$ curl --location --request POST 'localhost:3003/api/contract/activate' - --header 'Content-Type: application/json' \ - --data-raw 'INSERT_seabug-mint-request_HERE' - -$ cd ../cardano-transaction-lib -$ # Replace value of "mintingPolicy1" in seabug_contracts/MintingPolicy.js with policy noted from BPI +$ cd seabug +$ cardano-cli query utxo --testnet-magic 1097911063 --address $(cat payment.addr) + TxHash TxIx Amount +-------------------------------------------------------------------------------------- +ed11c8765d764852d049cd1a2239524ade0c6057a3a51146dc8c9d7bcbe008e0 0 100000000 lovelace + TxOutDatumNone +``` + +#### Mint your own NFT + +If you have an image: + +``` shell +$ cd seabug +$ scripts/mint-nft.sh 'image.jpeg' 'Title' 'Description' 'Token name' +``` + +The script take some time to work, especially if you don't use effitient_nft_pab before (`cd plutus-use-cases/mlabs && nix develop -c cabal run efficient-nft-pab --disable-optimisation`). + +If you already uploaded the image to nft.storage and have IPFC_CID (you can get it from nft.storage web interface). + +``` shell +$ cd seabug +$ scripts/mint-nft.sh 'image.jpeg' 'Title' 'Description' 'Token name' k2cwueaf1ew3nr2gq2rw83y13m2f5jpg8uyymn66kr8ogeglrwcou5u8 ``` ## Components diff --git a/flake.nix b/flake.nix index a40369e..cda3a1a 100644 --- a/flake.nix +++ b/flake.nix @@ -18,12 +18,15 @@ in rec { devShell = pkgs.mkShell { nativeBuildInputs = with pkgs; [ - postgresql - jq + arion.packages.${system}.arion + cardano-node.packages.${system}.cardano-cli curl + httpie ipfs - cardano-node.packages.${system}.cardano-cli - arion.packages.${system}.arion + jq + postgresql + shfmt + expect ]; }; }); diff --git a/plutus-use-cases b/plutus-use-cases index 9c8e366..4eaed09 160000 --- a/plutus-use-cases +++ b/plutus-use-cases @@ -1 +1 @@ -Subproject commit 9c8e36652bb3f04d23da3c85482997de9c2a87fe +Subproject commit 4eaed0951641a9a5f1017f53558dc2898cf2bb52 diff --git a/scripts/mint-nft.sh b/scripts/mint-nft.sh new file mode 100755 index 0000000..7d1132d --- /dev/null +++ b/scripts/mint-nft.sh @@ -0,0 +1,225 @@ +#!/bin/bash + +set -e + +if [ $# != 4 ] && [ $# != 5 ]; then + echo "Arguments: <DESCRIPTION> <TOKEN_NAME> [<IPFC_CID>]" + exit 1 +fi + +IMAGE=$1 +TITLE=$2 +DESC=$3 +TOKEN_NAME=$4 +IPFC_CID=$5 + +echo IMAGE: $IMAGE +echo TITLE: $TITLE +echo DESC: $DESC +echo TOKEN_NAME: $TOKEN_NAME +echo IPFC_CID: $IPFC_CID + +TOKEN_NAME_BASE64=$(echo -n $TOKEN_NAME | od -A n -t x1 | tr -d " \n") +echo TOKEN_NAME_BASE64: $TOKEN_NAME_BASE64 + +PKH=$(cardano-cli address key-hash --payment-verification-key-file plutus-use-cases/mlabs/payment.vkey) +echo PKH: $PKH + +# enviroment variables +SEABUG_ADMIN_TOKEN=ADMIN_TOKEN +TESTNET_MAGIC=1097911063 +export PGPASSWORD=seabug +export CARDANO_NODE_SOCKET_PATH=$PWD/data/cardano-node/ipc/node.socket + +############################################################ +# Prepare +############################################################ + +# Setup server admin token, password: seabug +psql -U seabug -h localhost -q -c "INSERT INTO admin_token(token) VALUES ('$SEABUG_ADMIN_TOKEN') ON CONFLICT DO NOTHING" + +############################################################ +# Functions +############################################################ + +get_ipfs_hash() { + local IMAGE_HASH=$1 + http GET localhost:8008/images | + jq -r "to_entries[] | select (.value.sha256hash == \"$IMAGE_HASH\") | .value.ipfsHash" +} + +efficient_nft_pab() { + cd plutus-use-cases/mlabs + rm -fv ../../efficient_nft_pab_out + if [ -z $CURRENCY ]; then + echo "> unbuffer nix develop -c cabal run efficient-nft-pab --disable-optimisation -- --pkh $PKH --auth-pkh $PKH --token \"$TOKEN_NAME\" | tee ../../efficient_nft_pab_out" + unbuffer nix develop -c cabal run efficient-nft-pab --disable-optimisation -- --pkh $PKH --auth-pkh $PKH --token "$TOKEN_NAME" >../../efficient_nft_pab_out + else + echo "> unbuffer nix develop -c cabal run efficient-nft-pab --disable-optimisation -- --pkh $PKH --auth-pkh $PKH --token "$TOKEN_NAME" --currency $CURRENCY | tee ../../efficient_nft_pab_out" + unbuffer nix develop -c cabal run efficient-nft-pab --disable-optimisation -- --pkh $PKH --auth-pkh $PKH --token "$TOKEN_NAME" --currency $CURRENCY >../../efficient_nft_pab_out + fi +} + +wait_up_efficient_nft_pab() { + sleep 1 + echo '>' wait_up_efficient_nft_pab... + while [ -z "$(rg 'Starting BotPlutusInterface server' efficient_nft_pab_out)" ] && [ ! -z "$(jobs)" ]; do + echo -n . + sleep 1 + done + sleep 3 + if [ ! -z "$(rg 'Network.Socket.bind: resource busy' efficient_nft_pab_out)" ]; then + echo "For some reason efficient_nft_pab already run, kill it" + exit 1 + fi + echo '>' wait_up_efficient_nft_pab...ok +} + +mint_cnft_request() { + local IPFC_CID=$1 + http POST localhost:3003/api/contract/activate \ + caID[tag]=MintCnft \ + caID[contents][0]["mc'name"]="$TITLE" \ + caID[contents][0]["mc'description"]="$DESC" \ + caID[contents][0]["mc'image"]="ipfs://$IPFC_CID" \ + caID[contents][0]["mc'tokenName"]="$TOKEN_NAME_BASE64" -v +} + +mint_request() { + http POST 'localhost:3003/api/contract/activate' \ + caID[tag]=Mint \ + caID[contents][0][unAssetClass][0][unCurrencySymbol]="$CURRENCY" \ + caID[contents][0][unAssetClass][1][unTokenName]="$TOKEN_NAME" \ + caID[contents][1]["mp'fakeAuthor"][unPaymentPubKeyHash][getPubKeyHash]="$PKH" \ + caID[contents][1]["mp'feeVaultKeys"]:=[] \ + caID[contents][1]["mp'price"]:=100000000 \ + caID[contents][1]["mp'daoShare"]:=500 \ + caID[contents][1]["mp'owner"][0][unPaymentPubKeyHash][getPubKeyHash]="$PKH" \ + caID[contents][1]["mp'owner"][1]:=null \ + caID[contents][1]["mp'lockLockupEnd"][getSlot]:=5 \ + caID[contents][1]["mp'authorShare"]:=1000 \ + caID[contents][1]["mp'lockLockup"]:=5 -v +} + +wait_balance_tx_efficient_nft_pab() { + echo '>' wait_balance_tx_efficient_nft_pab... + while [ -z "$(rg BalanceTxResp efficient_nft_pab_out)" ] && [ ! -z "$(jobs)" ]; do + echo -n . + sleep 1 + done + echo '>' wait_balance_tx_efficient_nft_pab...ok +} + +kill_bg_jobs() { + echo '>' kill bg jobs... + sleep 5 + killall efficient-nft-p + kill $(jobs -p) + while [ ! -z $(jobs -p) ]; do + sleep 1 + echo -n . + done + echo '>' kill bg jobs...ok +} + +query_utxo() { + cardano-cli query utxo --testnet-magic $TESTNET_MAGIC --address $(cat plutus-use-cases/mlabs/payment.addr) +} + +############################################################ +# upload image +############################################################ + +query_utxo + +if [ -z $IPFC_CID ]; then + echo '>' Image upload... + BUF=$( + http --form POST localhost:8008/admin/upload_image \ + "Authorization:$SEABUG_ADMIN_TOKEN" \ + "files@$IMAGE" \ + "title=$TITLE" \ + "description=$DESC" \ + --pretty none + ) + IMAGE_HASH=$(echo -n "$BUF" | rg '^\{' | jq -r '.sha256hash') + if [ -z "$IMAGE_HASH" ] || [ "$IMAGE_HASH" = "null" ]; then + echo Upload image error: $BUF + exit 1 + fi + echo '>' Image upload...ok + echo '>' IMAGE_HASH: $IMAGE_HASH + IPFS_HASH=$(get_ipfs_hash $IMAGE_HASH) + echo '>' IPFS_HASH: $IPFS_HASH + IPFS_CID=$(ipfs cid format -b base36 $IPFS_HASH) + echo '>' IPFS_CID: $IPFS_CID +fi + +############################################################ +# mint cnft +############################################################ + +echo '>>>' mint cnft + +echo '>' Run efficient-nft-pab... +efficient_nft_pab & +wait_up_efficient_nft_pab +echo '>' Run efficient-nft-pab...ok + +echo '>' Run mint_cnft_request... +mint_cnft_request $IPFC_CID +wait_balance_tx_efficient_nft_pab +echo '>' Run mint_cnft_request...ok + +kill_bg_jobs + +BALANCE_TX_RESP=$(cat efficient_nft_pab_out | rg BalanceTxResp) +echo '>' BALANCE_TX_RESP: $BALANCE_TX_RESP + +export CURRENCY=$(echo -n $BALANCE_TX_RESP | sed -E "s/^.*txMint = Value \(Map \[\(([^,]+).*/\1/") +echo '>' CURRENCY: $CURRENCY + +query_utxo + +echo '>' sleep 30 for minting work +sleep 30 + +query_utxo + +############################################################ +# mint nft +############################################################ + +echo '>>>' mint nft + +cp -v efficient_nft_pab_out efficient_nft_pab_out0 + +echo '>' Run efficient-nft-pab... +efficient_nft_pab & +wait_up_efficient_nft_pab +echo '>' Run efficient-nft-pab...ok + +echo '>' Run mint_request... +mint_request +wait_balance_tx_efficient_nft_pab +echo '>' Run mint_request...ok + +kill_bg_jobs + +BALANCE_TX_RESP=$(cat efficient_nft_pab_out | rg BalanceTxResp) +echo '>' BALANCE_TX_RESP: $BALANCE_TX_RESP + +MINTING_POLICY=$(cat efficient_nft_pab_out | rg minting-policy | sed -e 's/minting-policy: //' | jq -r .getMintingPolicy) +echo '>' MINTING_POLICY: $MINTING_POLICY + +echo '>' patch seabug_contracts/Seabug/MintingPolicy.js +sed -i "3s/\".*\"/\"$MINTING_POLICY\"/" cardano-transaction-lib/seabug_contracts/Seabug/MintingPolicy.js + +query_utxo + +echo '>' sleep 30 for minting work +sleep 30 + +query_utxo + +echo mint-nft ended diff --git a/scripts/prepare-wallet.sh b/scripts/prepare-wallet.sh new file mode 100755 index 0000000..6692ca7 --- /dev/null +++ b/scripts/prepare-wallet.sh @@ -0,0 +1,21 @@ +#!/bin/bash + +set -e +TESTNET_MAGIC=1097911063 + +cd plutus-use-cases/mlabs +cardano-cli address key-gen --verification-key-file payment.vkey --signing-key-file payment.skey +cardano-cli address build --payment-verification-key-file payment.vkey --out-file payment.addr --testnet-magic $TESTNET_MAGIC + +PHK=$(cardano-cli address key-hash --payment-verification-key-file payment.vkey) + +mkdir -p pab/signing-keys +mv payment.skey pab/signing-keys/signing-key-$PHK.skey + +echo new wallet generated: +echo address: $(cat payment.addr) +echo PHK: $PHK + +echo file: $(ls payment.addr) +echo file: $(ls payment.vkey) +echo file: $(ls pab/signing-keys/signing-key-$PHK.skey) From 7cb4fe9d213a76e69acc516aed010a6bf761137e Mon Sep 17 00:00:00 2001 From: Aleksandr Penskoi <aleksandr@mlabs.city> Date: Tue, 21 Jun 2022 18:19:16 +0300 Subject: [PATCH 2/2] Add base image for nft-market-place (fix ssl problem) --- arion-compose.nix | 1 + 1 file changed, 1 insertion(+) diff --git a/arion-compose.nix b/arion-compose.nix index cd734ef..6bc1f92 100644 --- a/arion-compose.nix +++ b/arion-compose.nix @@ -115,6 +115,7 @@ in { [ "${toString ./.}/data/postgres-data:/var/lib/postgresql/data" ]; }; nft-marketplace-server.service = { + image = "alpine"; command = [ "${nft-marketplace-server}/bin/nft-marketplace-server" "--db-connection"