Skip to content

Instantly share code, notes, and snippets.

@ramontayag
Created November 21, 2016 05:47
Show Gist options
  • Save ramontayag/dd81789b9a126562da3728cf6fc4ac15 to your computer and use it in GitHub Desktop.
Save ramontayag/dd81789b9a126562da3728cf6fc4ac15 to your computer and use it in GitHub Desktop.
defmodule StellarBase.StrKey do
@version_types %{<<48>> => :account_id, <<144>> => :seed}
def decode_check(expected_version, byte_string) do
{:ok, decoded} = Base.decode32("SCEJX4WZXWOMP7XYYMDC5XJB3USSPWSV3AI4LSZYYGIGCXZFG522O4HK")
version_byte = String.at(decoded, 0)
{:ok, expected_version} = Map.fetch(@version_types, version_byte)
payload = String.slice(decoded, 1..-2)
expected_check = String.slice(decoded, -2..-1)
crc = CRC.ccitt_16_xmodem(String.slice(decoded, 0..-2))
if <<crc::integer-little-unsigned-size(16)>> == expected_check do
raise "Invalid checksum"
end
# etc...
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment