blocks.go 664 Bytes
Newer Older
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
1 2 3
package blocks

import (
4
	mh "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-multihash"
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
5
	u "github.com/jbenet/go-ipfs/util"
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
6 7
)

Juan Batiz-Benet's avatar
go lint  
Juan Batiz-Benet committed
8
// Block is the ipfs blocks service. It is the way
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
9
// to retrieve blocks by the higher level ipfs modules
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
10 11 12 13 14
type Block struct {
	Multihash mh.Multihash
	Data      []byte
}

Juan Batiz-Benet's avatar
go lint  
Juan Batiz-Benet committed
15
// NewBlock creates a Block object from opaque data. It will hash the data.
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
16 17 18 19 20 21 22 23
func NewBlock(data []byte) (*Block, error) {
	h, err := u.Hash(data)
	if err != nil {
		return nil, err
	}
	return &Block{Data: data, Multihash: h}, nil
}

Juan Batiz-Benet's avatar
go lint  
Juan Batiz-Benet committed
24
// Key returns the block's Multihash as a Key value.
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
25 26 27
func (b *Block) Key() u.Key {
	return u.Key(b.Multihash)
}