Skip to content

Commit

Permalink
add tests and update Readme, #1
Browse files Browse the repository at this point in the history
  • Loading branch information
SimonLab committed Jan 29, 2019
1 parent b85613b commit c12bae6
Show file tree
Hide file tree
Showing 5 changed files with 98 additions and 6 deletions.
9 changes: 9 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -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)
40 changes: 40 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -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
36 changes: 30 additions & 6 deletions lib/base58encode.ex
Original file line number Diff line number Diff line change
@@ -1,21 +1,45 @@
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)
if rest == 0 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',
Expand All @@ -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

Expand Down
18 changes: 18 additions & 0 deletions test/base58_encode_test.exs
Original file line number Diff line number Diff line change
@@ -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
1 change: 1 addition & 0 deletions test/test_helper.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ExUnit.start()

0 comments on commit c12bae6

Please sign in to comment.