Wolfram Computation Meets Knowledge

Using IPFS, Filecoin and the Wolfram Language to Build a Unified Decentralized Services Interface

点击此处阅读该文章的简体中文版 »

Klicken Sie hier, um diesen Post auf Deutsch zu lesen »

Haga clic aquí para leer esta publicación en español »

Using IPFS, Filecoin and the Wolfram Language to Build a Unified Decentralized Services Interface

As part of Wolfram’s core goal of a unified blockchain interface, Wolfram Blockchain Labs (WBL) works to give developers direct Wolfram Language access to a range of blockchains and decentralized technologies. Today, we’re excited to announce a collaboration with IPFS and Filecoin, some of the core building blocks of Web3 (or the “decentralized” web). In addition to Wolfram Language integration with IPFS and the Filecoin blockchain, this unique collaboration lets developers leverage storage, peer-to-peer networking and other protocols to complement their existing applications or new decentralized applications, all from Wolfram technologies such as our Wolfram Language, the Wolfram Cloud and Wolfram Notebooks.

Combining these pieces enables novel computational possibilities for research, education, business, finance and beyond. With our partners at the Max Planck Digital Library, we have already developed a great example for researchers (you’ll see it in this blog post)—and we have our eyes fixed on future possibilities—so let’s dive into the details.

IPFS, Filecoin & libp2p: Foundational Web3 Technologies

IPFS, Filecoin and libp2p have had a foundational impact on the Web3 movement to decentralize the internet with vital technologies.

Complementing IPFS is Filecoin, an open-source cloud storage marketplace, protocol and cryptocurrency. It offers long-term file storage through a powerful and dynamic decentralized cloud storage solution. Stakeholders on the Filecoin blockchain network can “rent” their open storage space in exchange for client storage fees and block rewards, which perpetuate the long-term storage capacity of the network.

The list of useful protocols goes on: libp2p for building peer-to-peer network applications; Multiformats for future-proofing systems with self-described file formats; and the IPLD ecosystem of formats and data structures for building fully decentralized applications. Wolfram will have more updates as our developers continue building integrations.

The best way to illustrate the ease and significance of the IPFS ecosystem and the power of the Wolfram Language integration is with real-world examples.

Upload a file to IPFS:

ExternalStorageUpload
&#10005

ExternalStorageUpload["ExampleData/spikey2.png", 
 ExternalStorageBase -> "IPFS"]

Download a file from IPFS:

file = ExternalStorageDownload
&#10005

file = ExternalStorageDownload[
ExternalStorageObject[
  "QmXpif6oKinoP8CwPMCJPqoJNwvEfzbH1cJg6yZkBLJwKu", 
Association[
   "ExternalStorageBase" -> "IPFS", 
    "FileHash" -> "c9ccd63d95043f5d639a76b932dba6c9"]]]

Import the file:

Import
&#10005

Import[file]

bloxberg Certify & Verify Use Case

Preparing the Data and Uploading to IPFS

In this example, we’ll take a PDF file of Satoshi Nakamoto’s whitepaper “Bitcoin: A Peer-to-Peer Electronic Cash System” and prepare it to be sent to the bloxberg Certify smart contract. Additionally, we extend the functionality by uploading the file to IPFS and creating a JSON metadata file that is also uploaded to IPFS, containing the CID of the PDF file.

Hash the research document:

filePath = FileNameJoin
&#10005

filePath = FileNameJoin[{NotebookDirectory[], "bitcoin.pdf"}]

fileHash = FileHash
&#10005

fileHash = FileHash[filePath, "SHA256", "HexString"]

Get the Unix timestamp:

timestamp = ToString
&#10005

timestamp = ToString[UnixTime[Now]];

Upload the research file to IPFS:

eso = ExternalStorageUpload
&#10005

eso = ExternalStorageUpload[filePath, ExternalStorageBase -> "IPFS"]

Create the document and author metadata:

metadata = <|
   "Title" -> "Bitcoin: A Peer-to-Peer Electronic Cash System", 
   "Author" -> "Satoshi Nakamoto", "Affiliation" -> "bitcoin.org", 
   "FileCID" -> eso
&#10005

metadata = <|
   "Title" -> "Bitcoin: A Peer-to-Peer Electronic Cash System", 
   "Author" -> "Satoshi Nakamoto", "Affiliation" -> "bitcoin.org", 
   "FileCID" -> eso["CID"] |>;

metadata // Dataset
&#10005

metadata // Dataset

metadataFilePath = 
 Export
&#10005

metadataFilePath = 
 Export[FileNameJoin[{NotebookDirectory[], "metadata.json"}], 
  metadata, "JSON"]

Upload the metadata to IPFS:

metadataESO = 
 ExternalStorageUpload
&#10005

metadataESO = 
 ExternalStorageUpload[metadataFilePath, 
  ExternalStorageBase -> "IPFS"]

metadataCID = metadataESO
&#10005

metadataCID = metadataESO["CID"]

Submitting Data to the bloxberg Certify Smart Contract

In this section, we will create the transaction with all the required data and submit it to the bloxberg blockchain to certify it using the Certify smart contract. The private keys below have been published for reader understanding.

Initialize some variables for the transaction:

key = PrivateKey
&#10005

key = PrivateKey[
Association[
   "Type" -> "EllipticCurve", "CurveName" -> "secp256k1", 
    "PublicCurvePoint" -> {
     27112075690151519677987005437167591271987576467999908152870704903\
750742697008, 
      3534019055898665520072520949331176238451788939602461575265997766\
3077697880714}, 
    "PrivateMultiplier" -> 8676843423833923876056101813405014181278722\
7642250635023819795985067687832300, "Compressed" -> False, 
    "PublicByteArray" -> ByteArray[{4, 59, 240, 228, 27, 20, 0, 72, 
      116, 89, 142, 194, 128, 17, 192, 43, 187, 15, 70, 187, 147, 254,
       186, 247, 198, 148, 96, 23, 200, 48, 179, 228, 48, 78, 33, 214,
       201, 224, 235, 43, 178, 189, 71, 151, 166, 253, 217, 43, 132, 
      24, 39, 185, 92, 213, 198, 0, 46, 244, 85, 165, 18, 11, 158, 
      130, 138}], 
    "PrivateByteArray" -> ByteArray[{191, 213, 49, 131, 159, 167, 125,
       243, 62, 158, 234, 92, 248, 2, 42, 136, 221, 127, 168, 24, 172,
       124, 117, 37, 61, 155, 34, 38, 150, 88, 106, 236}]]];

address = 
 BlockchainKeyEncode
&#10005

address = 
 BlockchainKeyEncode[key, "Address", BlockchainBase -> "bloxberg"]

txCount = 
 BlockchainAddressData
&#10005

txCount = 
 BlockchainAddressData[address, "TransactionCount", 
  BlockchainBase -> "bloxberg"]

Prepare the transaction:

tx = BlockchainTransaction
&#10005

tx = BlockchainTransaction[<|"TransactionCount" -> txCount, 
   "Address" -> "E5a9654C7e190701016EBf18206020bf16D8Beab", 
   "FunctionCall" -> <|
     "Function" -> 
      Typed["createData", {"string", "bool", "string", 
         "string"} -> {"bytes32"}], 
     "Inputs" -> {fileHash, True, metadataCID, timestamp}|>, 
   "GasPrice" -> Quantity[1, "Brox"], "BlockchainBase" -> "bloxberg"|>]

Sign the transaction:

txSigned = BlockchainTransactionSign
&#10005

txSigned = BlockchainTransactionSign[tx, key]

Submit the transaction:

txSubmitted = BlockchainTransactionSubmit
&#10005

txSubmitted = BlockchainTransactionSubmit[txSigned]

txid = txSubmitted
&#10005

txid = txSubmitted["TransactionID"]

Check the transaction:

Dataset
&#10005

Dataset[BlockchainTransactionData[txid, BlockchainBase -> "bloxberg"],
  HiddenItems -> {"EventList" -> True}, ItemSize -> {50}]

Verifying the Data Certified in the bloxberg Blockchain

In this section, we call the bloxberg Verify smart contract to get the data, and by using the JSON metadata, get the original PDF file from IPFS.

Get the verification data:

eventsList = 
  BlockchainTransactionData
&#10005

eventsList = 
  BlockchainTransactionData[txid, "EventList", 
   BlockchainBase -> "Bloxberg"];

verifyData = 
  BlockchainContractValue
&#10005

verifyData = 
  BlockchainContractValue[
   eventsList[[1]]["Address"], <|
    "Function" -> 
     Typed["retrieveResearchData", {"bytes32"} -> {"string", "string",
         "bytes32", "string", "bool"}], 
    "Inputs" -> eventsList[[1]]["Data"]|>, 
   BlockchainBase -> "Bloxberg"];

Dataset
&#10005

Dataset[Association["StringCheckSum" -> verifyData[[1]], 
  "IPFS" -> verifyData[[2]], "ResearchID" -> verifyData[[3]], 
  "Timestamp" -> FromUnixTime[ToExpression[verifyData[[4]]]], 
  "DataExists" -> verifyData[[5]]]]

Get the IPFS metadata CID:

ipfsCID = verifyData
&#10005

ipfsCID = verifyData[[2]]

Download the metadata from IPFS:

verifyMetadataFile = 
 ExternalStorageDownload
&#10005

verifyMetadataFile = 
 ExternalStorageDownload[ipfsCID, ExternalStorageBase -> "IPFS"]

Import the metadata:

verifyMetadata = Import
&#10005

verifyMetadata = Import[verifyMetadataFile, "RawJSON"];

Dataset
&#10005

Dataset[verifyMetadata]

Download the research document:

ExternalStorageDownload
&#10005

ExternalStorageDownload[verifyMetadata["FileCID"]]

Stay Tuned: Making an Impact in Web3

We believe the next phase of development for the internet is a movement toward decentralization of websites and services. Wolfram Language integration with IPFS and Filecoin will expand the range of applications, services and analyses people can create, regardless of their experience with programming. We plan to expand our collaboration with IPFS to include an integration of the Filecoin blockchain, using Filecoin for storage, symbolic manipulation of data from IPFS and Filecoin, analytics examples, educational features and more.

In the future, we expect that there will be examples across a number of different areas and users:

  • Data analysis: taking data from Filecoin and IPFS and conducting analyses on the documents, all through the Wolfram Language
  • NFTs: using Wolfram Notebooks, the Wolfram Language and connections to blockchains to create, download and use NFTs
  • Academia: storing data for open and understandable use, and sophisticated sharing of research
  • Business: working with Wolfram Technical Consulting to explore advanced prototypes and explorations
  • Developers: building advanced applications using computational capabilities

We are on the cusp of highlighting the usefulness of IPFS and blockchains with an excellent example around NFTs that will be fun, exciting and FREE!

Connect with Wolfram Blockchain Labs to find out about integrating your blockchain into the Wolfram Language. Connect with Wolfram Technical Consulting to start a blockchain project.

Comments

Join the discussion

!Please enter your comment (at least 5 characters).

!Please enter your name.

!Please enter a valid email address.