项目模块要求:将通道结构体序列化后上传到ipfs,并可以按照hash地址找回json
package main
import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
shell "github.com/ipfs/go-ipfs-api"
)
var sh *shell.Shell
//交易结构体(未来的通道)
type Transaction struct {
Person1 string `json:"person1,omitempty" xml:"person1"`
Person2 string `json:"person2,omitempty" xml:"person2"`
Person1money string `json:"person1Money,omitempty" xml:"person1Money"`
Person2money string `json:"person2Money,omitempty" xml:"person2Money"`
}
//数据上传到ipfs
func UploadIPFS(str string) string {
sh = shell.NewShell("localhost:5001")
hash, err := sh.Add(bytes.NewBufferString(str))
if err != nil {
fmt.Println("上传ipfs时错误:", err)
}
return hash
}
//从ipfs下载数据
func CatIPFS(hash string) string {
sh = shell.NewShell("localhost:5001")
read, err := sh.Cat(hash)
if err != nil {
fmt.Println(err)
}
body, err := ioutil.ReadAll(read)
return string(body)
}
//通道序列化
func marshalStruct(transaction Transaction) []b


2244

被折叠的 条评论
为什么被折叠?



