From 5793783330c60c1360381631fd698e3bee819dbb Mon Sep 17 00:00:00 2001 From: Jeromy Date: Wed, 8 Feb 2017 11:33:10 -0800 Subject: [PATCH] Fix non-canonical imports via dag put License: MIT Signed-off-by: Jeromy --- core/commands/dag/dag.go | 32 +++++++++++++++++++++++++++++++- merkledag/merkledag.go | 2 +- package.json | 4 ++-- test/sharness/t0053-dag.sh | 7 +++++++ 4 files changed, 41 insertions(+), 4 deletions(-) diff --git a/core/commands/dag/dag.go b/core/commands/dag/dag.go index a1cf79dde5dd..f607c9c2f1da 100644 --- a/core/commands/dag/dag.go +++ b/core/commands/dag/dag.go @@ -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" ) @@ -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: @@ -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) + } +} diff --git a/merkledag/merkledag.go b/merkledag/merkledag.go index 5cce1a84f186..ae8d71cfe28c 100644 --- a/merkledag/merkledag.go +++ b/merkledag/merkledag.go @@ -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" ) diff --git a/package.json b/package.json index 91b1b65293fe..0a01c8082633 100644 --- a/package.json +++ b/package.json @@ -272,9 +272,9 @@ }, { "author": "whyrusleeping", - "hash": "QmWEtx4zwWCtdhnfSqPoUTKwkwaSzQPBaZH2PPBzuTeZni", + "hash": "QmT1B6cKXnMMki8nbuhrnLuiU32HLvwi6xe99bJ79482UK", "name": "go-ipld-cbor", - "version": "1.1.7" + "version": "1.1.8" }, { "author": "lgierth", diff --git a/test/sharness/t0053-dag.sh b/test/sharness/t0053-dag.sh index 7ea6df5af800..09b8c023079d 100755 --- a/test/sharness/t0053-dag.sh +++ b/test/sharness/t0053-dag.sh @@ -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