diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 0000000..0ae8c69 --- /dev/null +++ b/.travis.yml @@ -0,0 +1,9 @@ +language: elixir +elixir: + - 1.8 +env: + - MIX_ENV=test +script: + - mix test +after_success: + - bash <(curl -s https://codecov.io/bash) diff --git a/README.md b/README.md index d0fe1ae..52ad33a 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,42 @@ # elixir-base58-encode https://hex.pm/packages/b58 + + +This module provide the `encode/1` function which takes a binary and returns +its base58 representation + +1. Add the package to you dependencies + + ``` + defp deps do + [b58: "~> 0.1.0"] + end + ``` + + and run `mix deps.get` + +2. Call the `encode` fundtion with a binary as parameter: + + ``` + > Base58Encode.encode("foo") + > "bQbp" + ``` + + if the parameter is not a binary the function will return `:error` + + ``` + > Base58Encode.encode(42) + > "bQbp" + ``` + +The alphabet used for base58 is: +`123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz` + +For more information about the implementation of this module see the issue 1: +[How to encode a string to base58](https://github.com/dwyl/base58encode/issues/1) + +Read the following for more information about binary in Elixir: +https://elixir-lang.org/getting-started/binaries-strings-and-char-lists.html + +Wikipedia page for base58: +https://en.wikipedia.org/wiki/Base56 diff --git a/lib/base58encode.ex b/lib/base58encode.ex index 2f9786d..fe6cee8 100644 --- a/lib/base58encode.ex +++ b/lib/base58encode.ex @@ -1,11 +1,34 @@ defmodule Base58Encode do + @moduledoc """ + Provides the encode function which takes a binary and return it's + reprentation in base58 + """ - def encode(binary) do - {decimal, _} = Integer.parse(Base.encode16(binary), 16) - codes = get_codes(decimal, []) - codes_to_string(codes) + @doc """ + ## Examples + + iex> Base58Encode.encode("hello") + "Cn8eVZg" + + iex> Base58Encode.encode(42) + :error + """ + + def encode(binary) when is_binary(binary) do + case Integer.parse(Base.encode16(binary), 16) do + :error -> :error + + {decimal, _} -> + codes = get_codes(decimal, []) + codes_to_string(codes) + end end + # If the parameter is not a binary, return an error + def encode(_), do: :error + + +# return a list of codes (codepoint of base58) defp get_codes(int, codes) do rest = div(int, 58) code = rem(int, 58) @@ -13,9 +36,10 @@ defmodule Base58Encode do [code | codes] else get_codes(rest, [code | codes]) - end + end end + # match codepoints to the alphabet of base58 defp codes_to_string(codes) do alphabet = [ '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'J', 'K', 'L', @@ -25,7 +49,7 @@ defmodule Base58Encode do 's', 't', 'u', 'v', 'w', 'x', 'y', 'z' ] codes - |> Enum.map(fn c -> Enum.at(alphabet, c) end) + |> Enum.map(&(Enum.at(alphabet, &1))) |> Enum.join() end diff --git a/test/base58_encode_test.exs b/test/base58_encode_test.exs new file mode 100644 index 0000000..f537fd8 --- /dev/null +++ b/test/base58_encode_test.exs @@ -0,0 +1,18 @@ +defmodule Base58EncodeTest do + use ExUnit.Case + doctest Base58Encode + + describe "Testing encode function" do + test "returns base58 for the string foo" do + assert "bQbp" == Base58Encode.encode("foo") + end + + test "returns error if parameter is not a binary" do + assert :error == Base58Encode.encode(23) + end + + test "returns z when binary is represented by 57" do + assert "z" == Base58Encode.encode(<<57>>) + end + end +end diff --git a/test/test_helper.exs b/test/test_helper.exs new file mode 100644 index 0000000..869559e --- /dev/null +++ b/test/test_helper.exs @@ -0,0 +1 @@ +ExUnit.start()