Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

create a default CIDv1 #19

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 44 additions & 0 deletions lib/cid.ex
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,48 @@ 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

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()
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
27 changes: 16 additions & 11 deletions test/cid_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -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