Skip to content

Commit

Permalink
Fix non-canonical imports via dag put
Browse files Browse the repository at this point in the history
License: MIT
Signed-off-by: Jeromy <jeromyj@gmail.com>
  • Loading branch information
whyrusleeping committed Feb 8, 2017
1 parent 8c07404 commit 5793783
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 4 deletions.
32 changes: 31 additions & 1 deletion core/commands/dag/dag.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@ package dagcmd
import (
"fmt"
"io"
"io/ioutil"
"strings"

cmds "github.com/ipfs/go-ipfs/commands"
path "github.com/ipfs/go-ipfs/path"

node "gx/ipfs/QmRSU5EqqWVZSNdbU51yXmVoF1uNw3JgTNB6RaiL7DZM16/go-ipld-node"
ipldcbor "gx/ipfs/QmWEtx4zwWCtdhnfSqPoUTKwkwaSzQPBaZH2PPBzuTeZni/go-ipld-cbor"
ipldcbor "gx/ipfs/QmT1B6cKXnMMki8nbuhrnLuiU32HLvwi6xe99bJ79482UK/go-ipld-cbor"
cid "gx/ipfs/QmcTcsTvfaeEBRFo1TkFgT8sRmgi1n1LTZpecfVP8fzpGD/go-cid"
)

Expand Down Expand Up @@ -78,6 +79,21 @@ into an object of the specified format.
return
}

res.SetOutput(&OutputObject{Cid: c})
return
case "raw":
nd, err := convertRawToType(fi, format)
if err != nil {
res.SetError(err, cmds.ErrNormal)
return
}

c, err := n.DAG.Add(nd)
if err != nil {
res.SetError(err, cmds.ErrNormal)
return
}

res.SetOutput(&OutputObject{Cid: c})
return
default:
Expand Down Expand Up @@ -141,3 +157,17 @@ func convertJsonToType(r io.Reader, format string) (node.Node, error) {
return nil, fmt.Errorf("unknown target format: %s", format)
}
}

func convertRawToType(r io.Reader, format string) (node.Node, error) {
switch format {
case "cbor", "dag-cbor":
data, err := ioutil.ReadAll(r)
if err != nil {
return nil, err
}

return ipldcbor.Decode(data)
default:
return nil, fmt.Errorf("unsupported target format for raw input: %s", format)
}
}
2 changes: 1 addition & 1 deletion merkledag/merkledag.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (

node "gx/ipfs/QmRSU5EqqWVZSNdbU51yXmVoF1uNw3JgTNB6RaiL7DZM16/go-ipld-node"
logging "gx/ipfs/QmSpJByNKFX1sCsHBEp3R73FL4NF6FnQTEGyNAXHm2GS52/go-log"
ipldcbor "gx/ipfs/QmWEtx4zwWCtdhnfSqPoUTKwkwaSzQPBaZH2PPBzuTeZni/go-ipld-cbor"
ipldcbor "gx/ipfs/QmT1B6cKXnMMki8nbuhrnLuiU32HLvwi6xe99bJ79482UK/go-ipld-cbor"
cid "gx/ipfs/QmcTcsTvfaeEBRFo1TkFgT8sRmgi1n1LTZpecfVP8fzpGD/go-cid"
)

Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -272,9 +272,9 @@
},
{
"author": "whyrusleeping",
"hash": "QmWEtx4zwWCtdhnfSqPoUTKwkwaSzQPBaZH2PPBzuTeZni",
"hash": "QmT1B6cKXnMMki8nbuhrnLuiU32HLvwi6xe99bJ79482UK",
"name": "go-ipld-cbor",
"version": "1.1.7"
"version": "1.1.8"
},
{
"author": "lgierth",
Expand Down
7 changes: 7 additions & 0 deletions test/sharness/t0053-dag.sh
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,13 @@ test_dag_cmd() {
echo "{\"data\":\"CAISBGZvbwoYBA==\",\"links\":[]}" > cat_exp &&
test_cmp cat_exp cat_out
'

test_expect_success "non-canonical cbor input is normalized" '
DATA="\xa2\x65\x61\x6c\x69\x6e\x6b\xd8\x2a\x58\x25\x00\x01\x71\x12\x20\x65\x96\x50\xfc\x34\x43\xc9\x16\x42\x80\x48\xef\xc5\xba\x45\x58\xdc\x86\x35\x94\x98\x0a\x59\xf5\xcb\x3c\x4d\x84\x86\x7e\x6d\x31\x63\x61\x72\x72\x80"
HASH=$(echo "$DATA" | ipfs dag put --format=cbor --input-enc=raw) &&
test $HASH = "zdpuAyaAXrG12ax4EWuMxvFqBQuzwMGTW8C7NXAotCrcTnSXP" ||
test_fsh echo $HASH
'
}

# should work offline
Expand Down

0 comments on commit 5793783

Please sign in to comment.