Tradehub.Wallet (Tradehub v0.1.7) View Source

This module aims to signing, generating, and interacting with a Tradehub account.

Link to this section Summary

Functions

Look for the wallet address based on the given mnemonic phrase within a network.

Look for the wallet address based on the given public key within a network.

Look for the wallet address of based on the given public key within a network.

Generate a new Tradehub wallet

Encode a map to JSON with all of the keys in alphabetical order (nested included).

Open a wallet based on its mnemonic.

Open a wallet based on its private key.

Look for the private key based on the given mnemonic phrase.

Look for the public key based on the given mnemonic phrase.

Look for the public key based on the given private key phrase.

Sign the given message by using a wallet private key, and verify the signed messaged by using the wallet public key.

Link to this section Types

Specs

t() :: %Tradehub.Wallet{
  address: String.t(),
  mnemonic: String.t(),
  network: atom(),
  private_key: String.t(),
  public_key: String.t()
}

Link to this section Functions

Link to this function

address_from_mnemonic(mnemonic, network \\ :testnet)

View Source

Specs

address_from_mnemonic(String.t(), atom()) ::
  {:ok, String.t()} | {:error, String.t()}

Look for the wallet address based on the given mnemonic phrase within a network.

Examples

iex> Tradehub.Wallet.address_from_mnemonic("wrist coyote fuel wet evil tag shoot yellow morning history visit mosquito")
{:ok, "tswth174cz08dmgluavwcz2suztvydlptp4a8f8t5h4t"}

iex> Tradehub.Wallet.address_from_mnemonic("wrist coyote fuel wet evil tag shoot yellow morning history visit mosquito", :mainnet)
{:ok, "swth174cz08dmgluavwcz2suztvydlptp4a8fru98vw"}

iex> Tradehub.Wallet.address_from_mnemonic("wrost coyote fuel wet evil tag shoot yellow morning history visit mosquito")
{:error, "Invalid mnemonic"}
Link to this function

address_from_private_key(private_key, network \\ :testnet)

View Source

Specs

address_from_private_key(String.t() | bitstring(), atom()) ::
  {:ok, String.t()} | {:error, String.t()}

Look for the wallet address based on the given public key within a network.

Examples

iex> Tradehub.Wallet.address_from_private_key("151f85d41358f56d14bec4846c5370a3ae4f34decba71d48feac75ecbf6c8ca1")
{:ok, "tswth174cz08dmgluavwcz2suztvydlptp4a8f8t5h4t"}

iex> Tradehub.Wallet.address_from_private_key(<<21, 31, 133, 212, 19, 88, 245, 109, 20, 190, 196, 132, 108, 83, 112, 163, 174, 79, 52, 222, 203, 167, 29, 72, 254, 172, 117, 236, 191, 108, 140, 161>>)
{:ok, "tswth174cz08dmgluavwcz2suztvydlptp4a8f8t5h4t"}

iex> Tradehub.Wallet.address_from_private_key("151f85d41358f56d14bec4846c5370a3ae4f34decba71d48feac75ecbf6c8ca1", :mainnet)
{:ok, "swth174cz08dmgluavwcz2suztvydlptp4a8fru98vw"}
Link to this function

address_from_public_key(public_key, network \\ :testnet)

View Source

Specs

address_from_public_key(String.t(), atom()) ::
  {:ok, String.t()} | {:error, String.t()}

Look for the wallet address of based on the given public key within a network.

Examples

iex> Tradehub.Wallet.address_from_public_key("02e6193b57b672df29997fe495d78b4fd3eaae9daae0a5e2803129e2c21b504e23")
{:ok, "tswth174cz08dmgluavwcz2suztvydlptp4a8f8t5h4t"}

iex> Tradehub.Wallet.address_from_public_key(<<2, 230, 25, 59, 87, 182, 114, 223, 41, 153, 127, 228, 149, 215, 139, 79, 211, 234, 174, 157, 170, 224, 165, 226, 128, 49, 41, 226, 194, 27, 80, 78, 35>>)
{:ok, "tswth174cz08dmgluavwcz2suztvydlptp4a8f8t5h4t"}

iex> Tradehub.Wallet.address_from_public_key("02e6193b57b672df29997fe495d78b4fd3eaae9daae0a5e2803129e2c21b504e23", :mainnet)
{:ok, "swth174cz08dmgluavwcz2suztvydlptp4a8fru98vw"}
Link to this function

create_wallet(network \\ :testnet)

View Source

Specs

create_wallet(atom()) :: t()

Generate a new Tradehub wallet

Examples

iex> Tradehub.Wallet.create_wallet

iex> Tradehub.Wallet.create_wallet(:testnet)
Link to this function

encode_object_in_alphanumeric_key_order(obj)

View Source

Encode a map to JSON with all of the keys in alphabetical order (nested included).

Examples

iex> Tradehub.Wallet.encode_object_in_alphanumeric_key_order(%{b: 1, c: 2, a: 3, d: 4})
"{\"a\":3,\"b\":1,\"c\":2,\"d\":4}"

iex> Tradehub.Wallet.encode_object_in_alphanumeric_key_order(%{b: %{e: 1, f: 2}, c: 2, a: 3, d: 4})
"{\"a\":3,\"b\":{\"e\":1,\"f\":2},\"c\":2,\"d\":4}"

iex> Tradehub.Wallet.encode_object_in_alphanumeric_key_order("")
"\"\""
Link to this function

from_mnemonic(mnemonic, network \\ :testnet)

View Source

Specs

from_mnemonic(String.t(), atom()) :: {:ok, t()} | {:error, String.t()}

Open a wallet based on its mnemonic.

Examples

iex> {:ok, wallet} = Tradehub.Wallet.from_mnemonic("wrist coyote fuel wet evil tag shoot yellow morning history visit mosquito")
iex> wallet.address
"tswth174cz08dmgluavwcz2suztvydlptp4a8f8t5h4t"

iex> {:ok, wallet} = Tradehub.Wallet.from_mnemonic("wrist coyote fuel wet evil tag shoot yellow morning history visit mosquito", :mainnet)
iex> wallet.address
"swth174cz08dmgluavwcz2suztvydlptp4a8fru98vw"
Link to this function

from_private_key(private_key, network \\ :testnet)

View Source

Specs

from_private_key(String.t(), atom()) :: {:ok, t()} | {:error, String.t()}

Open a wallet based on its private key.

Examples

iex> Tradehub.Wallet.from_private_key("151f85d41358f56d14bec4846c5370a3ae4f34decba71d48feac75ecbf6c8ca1")
Link to this function

private_key_from_mnemonic(mnemonic)

View Source

Specs

private_key_from_mnemonic(String.t()) ::
  {:ok, bitstring()} | {:error, String.t()}

Look for the private key based on the given mnemonic phrase.

Examples

iex> Tradehub.Wallet.private_key_from_mnemonic("wrist coyote fuel wet evil tag shoot yellow morning history visit mosquito")
{:ok, <<21, 31, 133, 212, 19, 88, 245, 109, 20, 190, 196, 132, 108, 83, 112, 163, 174, 79, 52, 222, 203, 167, 29, 72, 254, 172, 117, 236, 191, 108, 140, 161>>}

iex> Tradehub.Wallet.private_key_from_mnemonic("clumb twenty either puppy thank liquid vital rigid tide tragic flash elevator")
{:error, "Invalid mnemonic"}
Link to this function

public_key_from_mnemonic(mnemonic)

View Source

Specs

public_key_from_mnemonic(String.t()) :: {:ok, String.t()} | {:error, String.t()}

Look for the public key based on the given mnemonic phrase.

Examples

iex> Tradehub.Wallet.public_key_from_mnemonic("wrist coyote fuel wet evil tag shoot yellow morning history visit mosquito")
{:ok, <<2, 230, 25, 59, 87, 182, 114, 223, 41, 153, 127, 228, 149, 215, 139, 79, 211, 234, 174, 157, 170, 224, 165, 226, 128, 49, 41, 226, 194, 27, 80, 78, 35>>}
Link to this function

public_key_from_private_key(private_key)

View Source

Specs

public_key_from_private_key(String.t() | bitstring()) ::
  {:ok, String.t()} | {:error, String.t()}

Look for the public key based on the given private key phrase.

Examples

iex> Tradehub.Wallet.public_key_from_private_key("151f85d41358f56d14bec4846c5370a3ae4f34decba71d48feac75ecbf6c8ca1")
{:ok, <<2, 230, 25, 59, 87, 182, 114, 223, 41, 153, 127, 228, 149, 215, 139, 79, 211, 234, 174, 157, 170, 224, 165, 226, 128, 49, 41, 226, 194, 27, 80, 78, 35>>}

Specs

sign(map(), %Tradehub.Wallet{
  address: term(),
  mnemonic: term(),
  network: term(),
  private_key: term(),
  public_key: term()
}) :: {:ok, String.t()} | {:error, String.t()}

Sign the given message by using a wallet private key, and verify the signed messaged by using the wallet public key.

Due to the nature of blockchain, the message will sign by the curve digital signature algorithm (ECDSA), with curve is secp256k1 and the hash algorithm is sha256.

Examples

iex> wallet = Tradehub.Wallet.create_wallet()
iex> Tradehub.Wallet.sign(%{message: "hello world"}, wallet)