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

import (
Jeromy's avatar
Jeromy committed
4 5
	"fmt"

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

Jeromy's avatar
Jeromy committed
10
// Block is a singular block of data in ipfs
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
11 12 13 14 15
type Block struct {
	Multihash mh.Multihash
	Data      []byte
}

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

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

func (b *Block) String() string {
	return fmt.Sprintf("[Block %s]", b.Key())
}