Amiri m.

asked • 03/11/25

Blockchains Assignment/Computer Science

Hello, I need help with a blockchains course. I would like to find a tutor who is available on a weekly basis until March. I will post my first task/code so that you get a feel for what kind of questions I will be asked. Thank you in advance.


Using the template provided at ipfs.py, create two functions

  1. "pin_to_ipfs()" which takes a Python dictionary, and stores the dictionary (as JSON) on IPFS. The function should return the Content Identifier (CID) of the data stored.
  2. "get_from_ipfs(cid)" which takes as input an IPFS CID and returns a Python dictionary containing the content. For this function, you may assume the content identified by the CID is valid JSON content. For example, you may assume that the CID does not refer to an image, video or other type of binary content that cannot be easily JSONified.

MY CODE:


import requests

import json


def pin_to_ipfs(data):

assert isinstance(data,dict), f"Error pin_to_ipfs expects a dictionary"

#YOUR CODE HERE

json_data = json.dumps(data)

url = "https://ipfs.infura.io:5001/api/v0/add"

headers = {

"Authorization": REMOVED API"

}

response = requests.post(url, headers=headers, files={"file": json_data})

response_data = response.json()

cid = response_data['Hash']


return cid


def get_from_ipfs(cid,content_type="json"):

assert isinstance(cid,str), f"get_from_ipfs accepts a cid in the form of a string"

#YOUR CODE HERE

url = f"https://ipfs.infura.io:5001/api/v0/cat?arg={cid}"

response = requests.get(url)

data = response.json()

assert isinstance(data,dict), f"get_from_ipfs should return a dict"

return data

Susan D.

Hi, please know Ask an Expert is a platform for academic questions. The following Help Center article may be helpful in answering your question: https://support.wyzant.com/hc/en-us/articles/209611913-How-do-I-find-a-tutor. If you need any assistance at any time, please contact our Customer Support Team at [email protected] and we will follow up as soon as we can.
Report

03/12/25

1 Expert Answer

By:

Still looking for help? Get the right answer, fast.

Ask a question for free

Get a free answer to a quick problem.
Most questions answered within 4 hours.

OR

Find an Online Tutor Now

Choose an expert and meet online. No packages or subscriptions, pay only for the time you need.