diff --git a/dagger.nimble b/dagger.nimble index 6bea9c0c..5422c4eb 100644 --- a/dagger.nimble +++ b/dagger.nimble @@ -4,5 +4,6 @@ description = "Dagger Storage Network" license = "MIT" requires "nim >= 1.4.2 & < 2.0.0" +requires "libp2p >= 0.0.2 & < 0.1.0" requires "chronos >= 2.5.2 & < 3.0.0" requires "asynctest >= 0.2.1 & < 0.3.0" diff --git a/dagger/merkledag.nim b/dagger/merkledag.nim index 4466aba7..50441411 100644 --- a/dagger/merkledag.nim +++ b/dagger/merkledag.nim @@ -1,3 +1,10 @@ +import pkg/libp2p/multihash + +export multihash + type MerkleDag* = object data*: seq[byte] + +proc rootHash*(dag: MerkleDag): MultiHash = + MultiHash.digest("sha2-256", dag.data).get() diff --git a/tests/dagger/testMerkleDag.nim b/tests/dagger/testMerkleDag.nim new file mode 100644 index 00000000..c18768b7 --- /dev/null +++ b/tests/dagger/testMerkleDag.nim @@ -0,0 +1,11 @@ +import std/unittest +import pkg/dagger/merkledag + +suite "Merkle DAG": + + test "has a root hash": + let dag1 = MerkleDag(data: @[1'u8, 2'u8, 3'u8]) + let dag2 = MerkleDag(data: @[4'u8, 5'u8, 6'u8]) + let dag3 = MerkleDag(data: @[4'u8, 5'u8, 6'u8]) + check dag1.rootHash != dag2.rootHash + check dag2.rootHash == dag3.rootHash diff --git a/tests/testAll.nim b/tests/testAll.nim index bdf422a3..50d4f33a 100644 --- a/tests/testAll.nim +++ b/tests/testAll.nim @@ -1,3 +1,4 @@ +import ./dagger/testMerkleDag import ./dagger/testChunking import ./dagger/testDagger