项目作者: 0zAND1z

项目描述 :
Golang based hack to experiment the uses of IPLD through the IPFS DAG operations using the go-ipfs-api package.
高级语言: Go
项目地址: git://github.com/0zAND1z/ipldcrud.git
创建时间: 2019-10-24T07:28:17Z
项目社区:https://github.com/0zAND1z/ipldcrud

开源协议:GNU Affero General Public License v3.0

下载


ipld-crud

A simple Golang based hack to experiment the uses of IPLD through the DAG operations using the go-ipfs-api package.

I have built a tiny client that receives key-value entries from user and stores it on IPFS DAG, returning an explorable URL to play with.

Usage

Here is a sample main.go file for your quick reference:

  1. package main
  2. import (
  3. "bufio"
  4. "encoding/json"
  5. "fmt"
  6. "os"
  7. ipldcrud "github.com/0zAND1z/ipldcrud"
  8. )
  9. func main() {
  10. sh := ipldcrud.InitShell("https://ipfs.infura.io:5001")
  11. keyValueMap := make(map[string]interface{})
  12. scanner := bufio.NewScanner(os.Stdin)
  13. fmt.Println("Enter value for the key field: ")
  14. scanner.Scan()
  15. inputKey := scanner.Text()
  16. fmt.Println("Enter value for value field: ")
  17. scanner.Scan()
  18. inputValue := scanner.Text()
  19. keyValueMap[inputKey] = inputValue
  20. // Converting into JSON object
  21. entryJSON, err := json.Marshal(keyValueMap)
  22. if err != nil {
  23. fmt.Println(err)
  24. }
  25. // Display the marshaled JSON object before sending it to IPFS
  26. jsonStr := string(entryJSON)
  27. fmt.Println("The JSON object of your key-value entry is:")
  28. fmt.Println(jsonStr)
  29. cid := ipldcrud.Set(sh, entryJSON)
  30. fmt.Println("CID: ", cid)
  31. // Fetch the details by reading the DAG for key "inputKey"
  32. fmt.Printf("READ: Value for key \"%s\" is: ", inputKey)
  33. res, err := ipldcrud.Get(sh, cid, inputKey)
  34. if err != nil {
  35. fmt.Println(err)
  36. }
  37. fmt.Println(res)
  38. }
  1. Once you run the main.go, a simple console pops up, asking you to enter a key and a value.

  2. After entering two string values, it will create a dag entry based on the input data. It will subsequently query the same data and return back the result along with the query results.

Tutorial

For a more detailed understanding of IPLD and how the code works, check out this article on SimpleAsWater.com!