-
Notifications
You must be signed in to change notification settings - Fork 37
/
cid.go
49 lines (41 loc) · 1.32 KB
/
cid.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
package abi
import (
cid "github.com/ipfs/go-cid"
mh "github.com/multiformats/go-multihash"
)
var (
// HashFunction is the default hash function for computing CIDs.
//
// This is currently Blake2b-256.
HashFunction = uint64(mh.BLAKE2B_MIN + 31)
// When producing a CID for an IPLD block less than or equal to CIDInlineLimit
// bytes in length, the identity hash function will be used instead of
// HashFunction. This will effectively "inline" the block into the CID, allowing
// it to be extracted directly from the CID with no disk/network operations.
//
// This is currently -1 for "disabled".
//
// This is exposed for testing. Do not modify unless you know what you're doing.
CIDInlineLimit = -1
)
type cidBuilder struct {
codec uint64
}
func (cidBuilder) WithCodec(c uint64) cid.Builder {
return cidBuilder{codec: c}
}
func (b cidBuilder) GetCodec() uint64 {
return b.codec
}
func (b cidBuilder) Sum(data []byte) (cid.Cid, error) {
hf := HashFunction
if len(data) <= CIDInlineLimit {
hf = mh.IDENTITY
}
return cid.V1Builder{Codec: b.codec, MhType: hf}.Sum(data)
}
// CidBuilder is the default CID builder for Filecoin.
//
// - The default codec is CBOR. This can be changed with CidBuilder.WithCodec.
// - The default hash function is 256bit blake2b.
var CidBuilder cid.Builder = cidBuilder{codec: cid.DagCBOR}