Created
March 23, 2022 18:25
-
-
Save sungkhum/ed72eb1f74a044cde77dc7d6b6d2b9ba to your computer and use it in GitHub Desktop.
DeSo NFT Auto Mint and Transfer from CSV using ArWeave Image Storage
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Script to mint DeSo NFTs from CSV file using ArWeave image storage | |
import arweave | |
import logging | |
from arweave.transaction_uploader import get_uploader, logger | |
import requests | |
from PIL import Image | |
import os, tempfile, zipfile | |
import csv | |
import shutil | |
import deso | |
from deso.Sign import Sign_Transaction | |
import time | |
import json | |
import urllib3 | |
# Load the CSV file with certificate information | |
# Three columns - Public Key of User you are tranferring NFT to, Body text of NFT, Image file name you will use | |
filename = "certs.csv" | |
# Directory where images are stored (relative to this script) | |
directory = 'tempimage' | |
# ArWeave Wallet JSON Data | |
wallet_data = {"kty":"RSA","n":"YOURARWEAVEWALLETHERE"} | |
''' | |
DeSo SEEDHEX should always be kept private. It has access to your complete wallet. It's kinda like | |
seed phrase. This is why writing methods in backend isn't a good practice until we have derived keys. | |
You can only automate your own account and can't have user authorisation. It is recommended to use test account while using write methods. | |
You can find the seedHex of your account in your browser storage. Just open https://node.deso.org/ > Dev tools > Application > Storage > Local Storage > https://identity.deso.org > users > Select the public key with which you want to post > seedHex | |
''' | |
# DeSo Seed Hex of minting account | |
DeSoSeedHex = 'YOURSEEDHEXHERE' | |
#DeSo Public Key of minting account | |
DeSoPublicKey = 'BC1YOURPUBLICKEYHERE' | |
# DeSo API URL | |
APIURL = 'https://bitclout.com/api/v0/' | |
# Initializing the titles and rows list | |
fields = [] | |
rows = [] | |
# Reading csv file | |
with open(filename, 'r') as csvfile: | |
# Creating a csv reader object | |
csvreader = csv.reader(csvfile) | |
# Extracting field names through first row | |
fields = next(csvreader) | |
# Extracting each data row one by one | |
for row in csvreader: | |
rows.append(row) | |
for row in rows: | |
# DeSo Public Key of user that we will transfer the certificate NFT to | |
transfer_public_key = str(row[0]) | |
# Body text of the NFT we will mint | |
nftBody = str(row[1]) | |
# Image file name stored in local directory | |
image = str(row[2]) | |
print("Minting and transferring NFT to: " + transfer_public_key) | |
wallet_file_path = "" | |
#wallet = arweave.Wallet(wallet_file_path) | |
wallet = arweave.Wallet.from_data(wallet_data) | |
with open(directory + "/" + image, "rb", buffering=0) as file_handler: | |
tx = arweave.Transaction(wallet, file_handler=file_handler, file_path=directory + "/" + image) | |
tx.add_tag('Content-Type', 'image/gif') | |
tx.sign() | |
uploader = get_uploader(tx, file_handler) | |
while not uploader.is_complete: | |
uploader.upload_chunk() | |
logger.info("{}% complete, {}/{}".format( | |
uploader.pct_complete, uploader.uploaded_chunks, uploader.total_chunks | |
)) | |
tx.send() | |
image_id = str(tx.id) | |
transaction_id = wallet.get_last_transaction_id() | |
arweaveURL = 'https://'+ transaction_id[1:] +'.arweave.net/' + image_id | |
print("Image URL: " + arweaveURL) | |
# Now we will post and mint the certificate on DeSo | |
post = deso.Post(DeSoSeedHex, DeSoPublicKey) | |
postResponse = post.send(nftBody, | |
imageUrl=[arweaveURL]) | |
postHashHex = postResponse["postHashHex"] | |
# Wait for the post to post | |
time.sleep(5) | |
print("Post Hash: " + postHashHex) | |
# Now mint the post as an NFT | |
status = post.mint(postHashHex, isForSale=False, minBidDeSo=1, copy = 1, creatorRoyality=0, coinHolderRoyality=0) | |
if status == 200: | |
print(f"NFT is live at https://diamondapp.com/nft/{postHashHex}") | |
else: | |
print(status) | |
# Wait for NFT to mint | |
time.sleep(5) | |
# Now transfer the NFT to the user | |
encoded_body = json.dumps({ | |
"SenderPublicKeyBase58Check": str(DeSoPublicKey), | |
"ReceiverPublicKeyBase58Check" : str(transfer_public_key), | |
"NFTPostHashHex" : postHashHex, | |
"SerialNumber" : 1, | |
"MinFeeRateNanosPerKB": 1000 | |
}) | |
try: | |
http = urllib3.PoolManager() | |
response = http.request('POST', APIURL + 'transfer-nft', | |
headers={'Content-Type': 'application/json'}, | |
body=encoded_body) | |
except Exception as e: | |
print(e) | |
print("done transferring") | |
print(response.status) | |
thedata = json.loads(response.data) | |
transactionHex = thedata["TransactionHex"] | |
signedTransactionHex = Sign_Transaction(DeSoSeedHex, transactionHex) # txn signature | |
submitPayload = {"TransactionHex": signedTransactionHex} | |
endpointURL = APIURL + 'submit-transaction' | |
submitResponse = requests.post(endpointURL, json=submitPayload) | |
if submitResponse.status_code == 200: | |
print("Done") | |
else: | |
print(submitResponse.json()) | |
print("Done signing transaction") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment