From cd565a2323f6f255ebbd387167ecfc875938abc5 Mon Sep 17 00:00:00 2001 From: SimonLab Date: Wed, 23 Jan 2019 16:47:07 +0000 Subject: [PATCH 1/2] create a default CIDv1, #11 --- lib/cid.ex | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/lib/cid.ex b/lib/cid.ex index 2ac2bb3..eb9a310 100644 --- a/lib/cid.ex +++ b/lib/cid.ex @@ -35,4 +35,39 @@ defmodule Cid do |> Enum.map(fn (x) -> Map.get(input_map, x) end) |> Enum.join("") end + + def encodeCid(data) do + # CIDv1 is multibase - cid version - multicontent - multihash + # with base58btc - version 1 - protobuf - multihash (ie CIDv0) + + # define all these value in hex, then concat them then encode with base58btc to get the cid + + # table value is 0xz ie 57 in decimal (the last value of the charecter set in base58) + # see https://github.com/multiformats/multibase/blob/master/multibase.csv + base58btc = Integer.to_string(57, 16) + # 0 or 1 + version1 = Integer.to_string(1, 16) + # protobuf is 0x50 in table https://github.com/multiformats/multicodec/blob/master/table.csv + protobuf = Integer.to_string(83,16) + + multihash = get_multihash(data) + + base58btc <> version1 <> protobuf <> multihash + end + + def get_multihash(data) do + data + |> hash() + |> multihash() + |> encode() + end + + + + def hash(data), do: :crypto.hash(:sha256, data) + + def multihash(digest), do: Multihash.encode(:sha2_256, digest) + + def encode({:ok, multihash}), do: Base.encode16(multihash, case: :lower) + end From 288783644fd7316cc7cfe8275f65c527d6363e94 Mon Sep 17 00:00:00 2001 From: Robert Francis Date: Thu, 24 Jan 2019 13:50:58 +0000 Subject: [PATCH 2/2] beginnings of creating our own CIDv1 function --- lib/cid.ex | 35 ++++++++++++++++++++++------------- test/cid_test.exs | 27 ++++++++++++++++----------- 2 files changed, 38 insertions(+), 24 deletions(-) diff --git a/lib/cid.ex b/lib/cid.ex index eb9a310..524d5e1 100644 --- a/lib/cid.ex +++ b/lib/cid.ex @@ -44,30 +44,39 @@ defmodule Cid do # table value is 0xz ie 57 in decimal (the last value of the charecter set in base58) # see https://github.com/multiformats/multibase/blob/master/multibase.csv - base58btc = Integer.to_string(57, 16) - # 0 or 1 - version1 = Integer.to_string(1, 16) - # protobuf is 0x50 in table https://github.com/multiformats/multicodec/blob/master/table.csv - protobuf = Integer.to_string(83,16) - - multihash = get_multihash(data) - - base58btc <> version1 <> protobuf <> multihash + # base58btc = Integer.to_string(57, 16) + # # 0 or 1 + # version1 = Integer.to_string(1, 16) + # # protobuf is 0x50 in table https://github.com/multiformats/multicodec/blob/master/table.csv + # protobuf = Integer.to_string(83,16) + # + # multihash = get_multihash(data) + # + # base58btc <> version1 <> protobuf <> multihash + + v1 = <<1>> + # protob = "P" # binary for "P" is <<80>> + raw = "U" # binary for "U" is <<85>> + {:ok, mh} = + data + |> hash() + |> multihash() + + concat = v1 <> raw <> mh + encode(concat) # convert this to base58 and then prefix "z" end def get_multihash(data) do data |> hash() |> multihash() - |> encode() + # |> encode() end - - def hash(data), do: :crypto.hash(:sha256, data) def multihash(digest), do: Multihash.encode(:sha2_256, digest) def encode({:ok, multihash}), do: Base.encode16(multihash, case: :lower) - + def encode(binary), do: Base.encode16(binary, case: :lower) end diff --git a/test/cid_test.exs b/test/cid_test.exs index ab6c192..e5b58ba 100644 --- a/test/cid_test.exs +++ b/test/cid_test.exs @@ -2,17 +2,22 @@ defmodule CidTest do use ExUnit.Case doctest Cid - test "Creates a deterministic Content ID from Elixir String" do - assert Cid.make("Elixir") == "NSqJspBr2u1F6z1DhcR2cnQAxLdQZBLk" - end - - test "Create a CID from a Map" do - map = %{cat: "Meow", dog: "Woof", fox: "What Does The Fox Say?"} - assert Cid.make(map) == "GdrVnsLSdxRphXgQgNsmq1FDyRXAySXT" - end + # test "Creates a deterministic Content ID from Elixir String" do + # assert Cid.make("Elixir") == "NSqJspBr2u1F6z1DhcR2cnQAxLdQZBLk" + # end + # + # test "Create a CID from a Map" do + # map = %{cat: "Meow", dog: "Woof", fox: "What Does The Fox Say?"} + # assert Cid.make(map) == "GdrVnsLSdxRphXgQgNsmq1FDyRXAySXT" + # end + # + # test "Cid.make(\"hello world\")" do + # assert Cid.make("hello world") == "MJ7MSJwS1utMxA9QyQLytNDtd5RGnx6m" + # end - test "Cid.make(\"hello world\")" do - assert Cid.make("hello world") == "MJ7MSJwS1utMxA9QyQLytNDtd5RGnx6m" + test "test encodeCid function" do + str = "Hello World\n" + Cid.encodeCid(str) + |> IO.inspect(label: "result of test") end - end