@cryptotaxi247 / kubo / commits / 0f234d76a

dag: add option to specify hash

License: MIT Signed-off-by: Łukasz Magiera <magik6k@gmail.com>

Łukasz Magiera committed Aug 9, 2017 at 19:25 UTC 0f234d76ae6906159c2a5c79694f51b805ac1136
6 files changed +63 -38
core/commands/dag/dag.go
+18 -1
@@ -3,6 +3,7 @@ package dagcmd
3 import (
4 "fmt"
5 "io"
6 + "math"
7 "strings"
8
9 cmds "github.com/ipfs/go-ipfs/commands"
@@ -11,6 +12,7 @@ import (
12 pin "github.com/ipfs/go-ipfs/pin"
13
14 cid "gx/ipfs/QmTprEaAA2A9bst5XH7exuyi5KzNMK3SEDNN8rBDnKWcUS/go-cid"
15 + mh "gx/ipfs/QmU9a9NV9RdPNwZQDYd5uKsm6N6LJLSvLbywDDYFbaaC6P/go-multihash"
16 )
17
18 var DagCmd = &cmds.Command{
@@ -48,6 +50,7 @@ into an object of the specified format.
50 cmds.StringOption("format", "f", "Format that the object will be added as.").Default("cbor"),
51 cmds.StringOption("input-enc", "Format that the input object will be.").Default("json"),
52 cmds.BoolOption("pin", "Pin this object when adding.").Default(false),
53 + cmds.StringOption("hash", "Hash function to use").Default(""),
54 },
55 Run: func(req cmds.Request, res cmds.Response) {
56 n, err := req.InvocContext().GetNode()
@@ -64,17 +67,31 @@ into an object of the specified format.
67
68 ienc, _, _ := req.Option("input-enc").String()
69 format, _, _ := req.Option("format").String()
70 + hash, _, err := req.Option("hash").String()
71 dopin, _, err := req.Option("pin").Bool()
72 if err != nil {
73 res.SetError(err, cmds.ErrNormal)
74 return
75 }
76
77 + // mhType tells inputParser which hash should be used. MaxUint64 means 'use
78 + // default hash' (sha256 for cbor, sha1 for git..)
79 + mhType := uint64(math.MaxUint64)
80 +
81 + if hash != "" {
82 + var ok bool
83 + mhType, ok = mh.Names[hash]
84 + if !ok {
85 + res.SetError(fmt.Errorf("%s in not a valid multihash name", hash), cmds.ErrNormal)
86 + return
87 + }
88 + }
89 +
90 if dopin {
91 defer n.Blockstore.PinLock().Unlock()
92 }
93
77 - nds, err := coredag.ParseInputs(ienc, format, fi)
94 + nds, err := coredag.ParseInputs(ienc, format, fi, mhType, -1)
95 if err != nil {
96 res.SetError(err, cmds.ErrNormal)
97 return
core/coreapi/unixfs_test.go
+3 -2
@@ -4,6 +4,7 @@ import (
4 "bytes"
5 "context"
6 "io"
7 + "math"
8 "strings"
9 "testing"
10
@@ -16,7 +17,7 @@ import (
17 config "github.com/ipfs/go-ipfs/repo/config"
18 testutil "github.com/ipfs/go-ipfs/thirdparty/testutil"
19 unixfs "github.com/ipfs/go-ipfs/unixfs"
19 - cbor "gx/ipfs/QmXgUVPAxjMLZSyxx818YstJJAoRg3nyPWENmBLVzLtoax/go-ipld-cbor"
20 + cbor "gx/ipfs/QmeebqVZeEXBqJ2B4urQWfdhwRRPm84ajnCo8x8pfwbsPM/go-ipld-cbor"
21 )
22
23 // `echo -n 'hello, world!' | ipfs add`
@@ -277,7 +278,7 @@ func TestLsNonUnixfs(t *testing.T) {
278 t.Error(err)
279 }
280
280 - nd, err := cbor.WrapObject(map[string]interface{}{"foo": "bar"})
281 + nd, err := cbor.WrapObject(map[string]interface{}{"foo": "bar"}, math.MaxUint64, -1)
282 if err != nil {
283 t.Fatal(err)
284 }
core/coredag/cbor.go new
+32
@@ -0,0 +1,32 @@
1 +package coredag
2 +
3 +import (
4 + "io"
5 + "io/ioutil"
6 +
7 + node "gx/ipfs/QmYNyRZJBUYPNrLszFmrBrPJbsBh2vMsefz5gnDpB5M1P6/go-ipld-format"
8 + ipldcbor "gx/ipfs/QmeebqVZeEXBqJ2B4urQWfdhwRRPm84ajnCo8x8pfwbsPM/go-ipld-cbor"
9 +)
10 +
11 +func cborJSONParser(r io.Reader, mhType uint64, mhLen int) ([]node.Node, error) {
12 + nd, err := ipldcbor.FromJson(r, mhType, mhLen)
13 + if err != nil {
14 + return nil, err
15 + }
16 +
17 + return []node.Node{nd}, nil
18 +}
19 +
20 +func cborRawParser(r io.Reader, mhType uint64, mhLen int) ([]node.Node, error) {
21 + data, err := ioutil.ReadAll(r)
22 + if err != nil {
23 + return nil, err
24 + }
25 +
26 + nd, err := ipldcbor.Decode(data, mhType, mhLen)
27 + if err != nil {
28 + return nil, err
29 + }
30 +
31 + return []node.Node{nd}, nil
32 +}
core/coredag/dagtransl.go
+7 -32
@@ -3,14 +3,12 @@ package coredag
3 import (
4 "fmt"
5 "io"
6 - "io/ioutil"
6
8 - ipldcbor "gx/ipfs/QmXgUVPAxjMLZSyxx818YstJJAoRg3nyPWENmBLVzLtoax/go-ipld-cbor"
7 node "gx/ipfs/QmYNyRZJBUYPNrLszFmrBrPJbsBh2vMsefz5gnDpB5M1P6/go-ipld-format"
8 )
9
10 // DagParser is function used for parsing stream into Node
13 -type DagParser func(r io.Reader) ([]node.Node, error)
11 +type DagParser func(r io.Reader, mhType uint64, mhLen int) ([]node.Node, error)
12
13 // FormatParsers is used for mapping format descriptors to DagParsers
14 type FormatParsers map[string]DagParser
@@ -36,8 +34,8 @@ var defaultRawParsers = FormatParsers{
34
35 // ParseInputs uses DefaultInputEncParsers to parse io.Reader described by
36 // input encoding and format to an instance of ipld Node
39 -func ParseInputs(ienc, format string, r io.Reader) ([]node.Node, error) {
40 - return DefaultInputEncParsers.ParseInputs(ienc, format, r)
37 +func ParseInputs(ienc, format string, r io.Reader, mhType uint64, mhLen int) ([]node.Node, error) {
38 + return DefaultInputEncParsers.ParseInputs(ienc, format, r, mhType, mhLen)
39 }
40
41 // AddParser adds DagParser under give input encoding and format
@@ -53,39 +51,16 @@ func (iep InputEncParsers) AddParser(ienv, format string, f DagParser) {
51
52 // ParseInputs parses io.Reader described by input encoding and format to
53 // an instance of ipld Node
56 -func (iep InputEncParsers) ParseInputs(ienc, format string, r io.Reader) ([]node.Node, error) {
57 - pset, ok := iep[ienc]
54 +func (iep InputEncParsers) ParseInputs(ienc, format string, r io.Reader, mhType uint64, mhLen int) ([]node.Node, error) {
55 + parsers, ok := iep[ienc]
56 if !ok {
57 return nil, fmt.Errorf("no input parser for %q", ienc)
58 }
59
62 - parser, ok := pset[format]
60 + parser, ok := parsers[format]
61 if !ok {
62 return nil, fmt.Errorf("no parser for format %q using input type %q", format, ienc)
63 }
64
67 - return parser(r)
68 -}
69 -
70 -func cborJSONParser(r io.Reader) ([]node.Node, error) {
71 - nd, err := ipldcbor.FromJson(r)
72 - if err != nil {
73 - return nil, err
74 - }
75 -
76 - return []node.Node{nd}, nil
77 -}
78 -
79 -func cborRawParser(r io.Reader) ([]node.Node, error) {
80 - data, err := ioutil.ReadAll(r)
81 - if err != nil {
82 - return nil, err
83 - }
84 -
85 - nd, err := ipldcbor.Decode(data)
86 - if err != nil {
87 - return nil, err
88 - }
89 -
90 - return []node.Node{nd}, nil
65 + return parser(r, mhType, mhLen)
66 }
merkledag/merkledag.go
+1 -1
@@ -11,8 +11,8 @@ import (
11
12 cid "gx/ipfs/QmTprEaAA2A9bst5XH7exuyi5KzNMK3SEDNN8rBDnKWcUS/go-cid"
13 blocks "gx/ipfs/QmVA4mafxbfH5aEvNz8fyoxC6J1xhAtw88B4GerPznSZBg/go-block-format"
14 - ipldcbor "gx/ipfs/QmXgUVPAxjMLZSyxx818YstJJAoRg3nyPWENmBLVzLtoax/go-ipld-cbor"
14 node "gx/ipfs/QmYNyRZJBUYPNrLszFmrBrPJbsBh2vMsefz5gnDpB5M1P6/go-ipld-format"
15 + ipldcbor "gx/ipfs/QmeebqVZeEXBqJ2B4urQWfdhwRRPm84ajnCo8x8pfwbsPM/go-ipld-cbor"
16 )
17
18 // TODO: We should move these registrations elsewhere. Really, most of the IPLD
package.json
+2 -2
@@ -261,9 +261,9 @@
261 },
262 {
263 "author": "whyrusleeping",
264 - "hash": "QmXgUVPAxjMLZSyxx818YstJJAoRg3nyPWENmBLVzLtoax",
264 + "hash": "QmeebqVZeEXBqJ2B4urQWfdhwRRPm84ajnCo8x8pfwbsPM",
265 "name": "go-ipld-cbor",
266 - "version": "1.2.7"
266 + "version": "1.2.8"
267 },
268 {
269 "author": "lgierth",