一起来读源码230-Ipfs 第三方库:go-cid

摘要:

一个自描述地址项目

详情:

go-cid

定义全局变量:

编码器类型:

    "v0":                                  DagProtobuf, //0x70
    "raw":                                Raw, //
    "protobuf":                         DagProtobuf,
    "cbor":                               DagCBOR,
    "libp2p-key":                      Libp2pKey,
    "git-raw":                            GitRaw,
    "eth-block":                        EthBlock, //以太坊块
    "eth-block-list":                   EthBlockList,
    "eth-tx-trie":                        EthTxTrie,
    "eth-tx":                               EthTx, //0x93 以太坊传输块
    "eth-tx-receipt-trie":             EthTxReceiptTrie,
    "eth-tx-receipt":                   EthTxReceipt,
    "eth-state-trie":                    EthStateTrie,
    "eth-account-snapshot":     EthAccountSnapshot,
    "eth-storage-trie":                EthStorageTrie,
    "bitcoin-block":                    BitcoinBlock,
    "bitcoin-tx":                          BitcoinTx,
    "zcash-block":                      ZcashBlock,
    "zcash-tx":                           ZcashTx,
    "decred-block":                    DecredBlock,
    "decred-tx":                          DecredTx,
    "dash-block":                        DashBlock,
    "dash-tx":                             DashTx,
    "fil-commitment-unsealed":  FilCommitmentUnsealed,
    "fil-commitment-sealed":      FilCommitmentSealed,

 

定义全局函数:

func Fuzz(data []byte) int //测试cid的相关转换函数是否有效

func NewCidV0(mhash mh.Multihash) Cid // 新建一个cid包裹的multihash(推荐优先使用NewCidV1)

func NewCidV1(codecType uint64, mhash mh.Multihash) Cid // 新建一个cid包裹的multihash

func Parse(v interface{}) (Cid, error) //将对象解析成cid

func Decode(v string) (Cid, error)//str解码成Cid

func ExtractEncoding(v string) (mbase.Encoding, error) // 提取编码器

func Cast(data []byte) (Cid, error) // byte数组转cid (数据格式:版本 编码器类型 multihash)

func PrefixFromBytes(buf []byte) (Prefix, error) // 二进制数组转前缀

func CidFromBytes(data []byte) (int, Cid, error) // 二进制数组转cid

func NewPrefixV0(mhType uint64) Prefix //new一个v0版本的前缀

func NewPrefixV1(codecType uint64, mhType uint64) Prefix //new一个v1版本的前缀

 

定义接口:

type Builder interface { //构造器

    Sum(data []byte)(Cid, error)

    GetCodec() uint64

    WithCodec(uint64) Builder

}

 

定义类:

//v0版本构建器

type V0Builder struct{}

func(p V0Builder) Sum(data []byte)(Cid, error)

func (p V0Builder) GetCodec() uint64

func (p V0Builder) WithCodec(c uint64) Builder

//v1版本构建器

type V1Builder struct{

    Codec uint64

    MhType uint64

    MhLength int

}

func (p V1Builder) Sum(data []byte) (Cid, error)

func (p V1Builder) GetCodec() uint64

func (p V1Builder) WithCodec(c uint64) Builder

// 前缀
type Prefix struct {
    Version  uint64 //版本
    Codec    uint64 //编解码器
    MhType   uint64 //multihash版本
    MhLength int    //multihash长
}

func (p Prefix) Sum(data []byte) (Cid, error) // 与二进制数组求和并返回一个cid

func (p Prefix) Bytes() []byte // 返回前缀的二进制数组

func(p Prefix) GetCodec() uint64

func(p Prefix) WithCodec(c uint64) Builder

//自描述地址

type Cid struct{

    str string

}

func (c Cid) Defined() bool //是否已被定义

func (c *Cid) UnmarshalBinary(data []byte) error // 解析二进制数组

func (c *Cid) UnmarshalText(text []byte) error // 解析文本

func (c Cid) Version() uint64// 返回cid版本 0或1

func (c Cid) Type() uint64 // 返回cid类型

func (c Cid) String() string // 返回 版本0:b58str 版本1:b32str

func (c Cid) StringOfBase(base mbase.Encoding) (string, error) // 返回str的进制

func (c Cid) Encode(base mbase.Encoder) string // cid编码成str

func (c Cid) Hash() mh.Multihash // 返回cid的hash

func (c Cid) Bytes() []byte // 返回cid的二进制数据

func (c Cid) ByteLen() int // 返回cid的二进制数据长度

func (c Cid) WriteBytes(w io.Writer) (int, error)  // 写入二进制数据

func (c Cid) MarshalBinary() ([]byte, error) // 格式化二进制

func (c Cid) MarshalText() ([]byte, error)  // 格式化文本

func (c Cid) Equals(o Cid) bool // 判断2个cid是否相等

func (c *Cid) UnmarshalJSON(b []byte) error  // 反格式化json转为cid

func (c Cid) MarshalJSON() ([]byte, error) // 格式化json

func (c Cid) KeyString() string // 返回string类型的cid内容

func (c Cid) Loggable() map[string]interface{}  // 导出map["cid"]cid 这种格式

func (c Cid) Prefix() Prefix // 返回前缀

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值