diff --git a/blocks.go b/blocks.go index 9bf556f..b87cf5a 100644 --- a/blocks.go +++ b/blocks.go @@ -1,6 +1,7 @@ package blocks import ( + "errors" "fmt" mh "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-multihash" @@ -18,6 +19,16 @@ func NewBlock(data []byte) *Block { return &Block{Data: data, Multihash: u.Hash(data)} } +func NewBlockWithHash(data []byte, h mh.Multihash) (*Block, error) { + if u.Debug { + chk := u.Hash(data) + if string(chk) != string(h) { + return nil, errors.New("Data did not match given hash!") + } + } + return &Block{Data: data, Multihash: h}, nil +} + // Key returns the block's Multihash as a Key value. func (b *Block) Key() u.Key { return u.Key(b.Multihash)