-
Notifications
You must be signed in to change notification settings - Fork 4
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
Decode #13 #17
Merged
Merged
Decode #13 #17
Changes from all commits
Commits
Show all changes
23 commits
Select commit
Hold shift + click to select a range
10084f1
rename Base58encode to just Base58 to reflect the fact that we can *d…
nelsonic 33d8705
convert any value to binary then encode as Base58 #14
nelsonic b70fc66
rename to exbase58
nelsonic b4cbb93
create .formatter.exs file to format code
nelsonic e9d2b05
add implementation of Base58.decode fixes #13
nelsonic 326a5fe
rename test
nelsonic 2d43962
bump package version to 1.0.0 (SemVer) includes decode function! #13
nelsonic 97b46bd
update dependencies
nelsonic 501e3e4
update module name to b58 https://github.com/dwyl/base58/issues/16
nelsonic 42fcf6b
rename module from Base58Encode to Base58 (as we now have a decode/1 …
nelsonic 9af3c7a
update docs in README.md with example usage for Base58.decode/1 #13
nelsonic 18771d5
close <div align="center"> to tidy up readme
nelsonic 19e066e
add base58 hero image in README.md
nelsonic f822d39
add elixir code formatting to example blocks in README.md
nelsonic ec2cdf6
fix module name in installation instructions :b58 #16
nelsonic 07b11c5
add note on encoding/decoding integer values #14
nelsonic 6ea4d4c
update version of ex_doc in mix.exs
nelsonic c184aea
create decode_to_int/1 to decode integer values #14
nelsonic 279fd7a
add note on encoding/decoding integer values to README.md #14
nelsonic c1a484b
fix markdown errors
nelsonic dca4ee7
Update to_char_list > to_charlist lib/base58.ex
nelsonic fdf7f23
fix example and use to_charlist, #17
SimonLab 9c23a62
Merge branch 'decode-issue#13' of github.com:dwyl/base58 into decode-…
SimonLab File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
# Used by "mix format" | ||
[ | ||
inputs: ["mix.exs", "{config,lib,test}/**/*.{ex,exs}"] | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,10 @@ | ||
defmodule Example do | ||
|
||
hello = "hello" | ||
helloBase58 = Base58Encode.encode(hello) | ||
helloBase58 = Base58.encode(hello) | ||
helloBase58Decode = Base58.decode(helloBase58) | ||
|
||
IO.puts("the \"#{hello}\" binary is represented as \"#{helloBase58}\" in base58") | ||
IO.puts("the \"#{helloBase58Decode}\" decoded is #{hello})") | ||
|
||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
defmodule Base58 do | ||
@moduledoc """ | ||
`Base58` provides two functions: `encode/1` and `decode/1`. | ||
`encode/1` takes an **Elixir binary** (_String, Number, etc._) | ||
and returns a Base58 encoded String. | ||
`decode/1` receives a Base58 encoded String and returns a binary. | ||
""" | ||
|
||
@doc """ | ||
## Examples | ||
|
||
iex> Base58.encode("hello") | ||
"Cn8eVZg" | ||
|
||
iex> Base58.encode(42) | ||
"4yP" | ||
""" | ||
def encode(""), do: "" | ||
def encode(binary) when is_binary(binary) do | ||
# see https://github.com/dwyl/base58/pull/3#discussion_r252291127 | ||
decimal = :binary.decode_unsigned(binary) | ||
|
||
if decimal == 0 do | ||
# see https://github.com/dwyl/base58/issues/5#issuecomment-459088540 | ||
leading_zeros(binary, "") | ||
else | ||
codes = get_codes(decimal, []) | ||
leading_zeros(binary, "") <> codes_to_string(codes) | ||
end | ||
end | ||
|
||
# If the parameter is not a binary convert it to binary before encode. | ||
def encode(int) when is_integer(int) do | ||
int | ||
|> Integer.to_string() | ||
|> encode() | ||
end | ||
|
||
def encode(val) when is_float(val) do | ||
val | ||
|> Float.to_string() | ||
|> encode() | ||
end | ||
|
||
def encode(atom) when is_atom(atom) do | ||
atom | ||
|> Atom.to_string() | ||
|> encode() | ||
end | ||
|
||
# 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 | ||
|
||
defp alphanum do | ||
[ '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F', | ||
'G', 'H', 'J', 'K', 'L', 'M', 'N', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', | ||
'X', 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'm', | ||
'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z' ] | ||
end | ||
|
||
# match codepoints to the alphabet of base58 | ||
defp codes_to_string(codes) do | ||
codes | ||
|> Enum.map(&Enum.at(alphanum(), &1)) | ||
|> Enum.join() | ||
end | ||
|
||
# convert leading zeros to "1" | ||
defp leading_zeros(<<0, rest::binary>>, acc) do | ||
leading_zeros(rest, acc <> "1") | ||
end | ||
|
||
defp leading_zeros(_bin, acc) do | ||
acc | ||
end | ||
|
||
@doc """ | ||
`decode/1` decodes the given Base58 string back to binary. | ||
## Examples | ||
|
||
iex> Base58.encode("hello") |> Base58.decode() | ||
"hello" | ||
""" | ||
@alnum ~c(123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz) | ||
|
||
def decode(""), do: "" # return empty string unmodified | ||
def decode("\0"), do: "" # treat null values as empty | ||
def decode(encoded), do: recurse(encoded |> to_charlist, 0) | ||
defp recurse([], acc), do: acc |> :binary.encode_unsigned() | ||
defp recurse([head | tail], acc) do | ||
recurse(tail, (acc * 58) + Enum.find_index(@alnum, &(&1 == head))) | ||
end | ||
|
||
@doc """ | ||
`decode_to_int/1` decodes the given Base58 string back to an Integer. | ||
## Examples | ||
|
||
iex> Base58.encode(42) |> Base58.decode_to_int() | ||
42 | ||
""" | ||
def decode_to_int(encoded) do | ||
decode(encoded) | ||
|> String.to_integer() | ||
end | ||
end |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,13 @@ | ||
defmodule Base58Encode.MixProject do | ||
defmodule Base58.MixProject do | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Rename #16 |
||
use Mix.Project | ||
|
||
def project do | ||
[ | ||
app: :b58, | ||
app: :B58, | ||
name: "b58", | ||
description: description(), | ||
package: package(), | ||
version: "0.1.1", | ||
version: "1.0.0", | ||
elixir: "~> 1.7", | ||
start_permanent: Mix.env() == :prod, | ||
deps: deps(), | ||
|
@@ -16,7 +16,7 @@ defmodule Base58Encode.MixProject do | |
coveralls: :test, | ||
"coveralls.json": :test | ||
], | ||
source_url: "https://github.com/dwyl/base58encode" | ||
source_url: "https://github.com/dwyl/base58" | ||
] | ||
end | ||
|
||
|
@@ -29,23 +29,24 @@ defmodule Base58Encode.MixProject do | |
|
||
# Run "mix help deps" to learn about dependencies. | ||
defp deps do | ||
[ {:excoveralls, "~> 0.10", only: :test}, | ||
[ | ||
{:excoveralls, "~> 0.12.3", only: :test}, | ||
{:basefiftyeight, "~> 0.1.0", only: :test}, | ||
{:stream_data, "~> 0.1", only: :test}, | ||
{:ex_doc, "~> 0.19.3", only: :dev} | ||
{:stream_data, "~> 0.4.3", only: :test}, | ||
{:ex_doc, "~> 0.21.3", only: :dev} | ||
] | ||
end | ||
|
||
defp description() do | ||
"Encode an Elixir binary to its base58 representation" | ||
"B58 lets you encode an Elixir binary to base58 and decode a base58 string." | ||
end | ||
|
||
defp package() do | ||
[ | ||
name: "b58", | ||
licenses: ["GNU GPL v2.0"], | ||
maintainers: ["dwyl"], | ||
links: %{"GitHub" => "https://github.com/dwyl/base58encode"} | ||
links: %{"GitHub" => "https://github.com/dwyl/base58"} | ||
] | ||
end | ||
end |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@SimonLab thanks for updating the example. 👍