View Source Tezex.Crypto (tezex v2.0.0)

A set of functions to check Tezos signed messages, derive a pkh from a pubkey, verify that a public key corresponds to a wallet address (public key hash).

Summary

Functions

Verify that address is the public key hash of pubkey.

Verify that address is the public key hash of pubkey and that signature is a valid signature for message signed with the private key corresponding to public key pubkey.

Derive public key hash (Tezos wallet address) from public key

Encode a raw public key

Sign the hexadecimal/Micheline representation of a string, Micheline encoding is done when bytes do not start with "0501".

Sign an operation using 0x03 as watermark

Validate an implicit account address

Verify that signature is a valid signature for message signed with the private key corresponding to public key pubkey

Types

@type privkey_param() :: binary() | {privkey :: binary(), passphrase :: binary()}

Functions

Link to this function

check_address(address, pubkey)

View Source
@spec check_address(nonempty_binary(), nonempty_binary()) ::
  :ok | {:error, :address_mismatch | :invalid_pubkey_format}

Verify that address is the public key hash of pubkey.

Examples

iex> pubkey = "sppk7aBerAEA6tv4wzg6FnK7i5YrGtEGFVvNjWhc2QX8bhzpouBVFSW"
iex> check_address("tz2BC83pvEAag6r2ZV7kPghNAbjFoiqhCvZx", pubkey)
:ok
iex> check_address("tz1burnburnburnburnburnburnburjAYjjX", pubkey)
{:error, :address_mismatch}
iex> check_address("tz2BC83pvEAag6r2ZV7kPghNAbjFoiqhCvZx", "x" <> pubkey)
{:error, :invalid_pubkey_format}
Link to this function

check_signature(address, signature, message, pubkey)

View Source
@spec check_signature(binary(), binary(), binary(), binary()) ::
  :ok
  | {:error,
     :address_mismatch
     | :invalid_pubkey_format
     | :invalid_signature
     | :bad_signature}

Verify that address is the public key hash of pubkey and that signature is a valid signature for message signed with the private key corresponding to public key pubkey.

Examples

iex> address = "tz2BC83pvEAag6r2ZV7kPghNAbjFoiqhCvZx"
iex> address_b = "tz1burnburnburnburnburnburnburjAYjjX"
iex> signature = "spsig1ZNQaUKNERZSiEiNviqa5EAPkcNASXhfkXtxRatZTDZAnUB4Ra2Jus8b1oEpFnPx8Z6g28pd8vK3R8nPK29JDU5FiSLH5T"
iex> message = "05010000007154657a6f73205369676e6564204d6573736167653a207369676e206d6520696e20617320747a32424338337076454161673672325a56376b5067684e41626a466f69716843765a78206f6e206f626a6b742e636f6d20617420323032312d31302d30345431383a35393a31332e3939305a"
iex> public_key = "sppk7aBerAEA6tv4wzg6FnK7i5YrGtEGFVvNjWhc2QX8bhzpouBVFSW"
iex> check_signature(address, signature, message, public_key)
:ok
iex> check_signature(address_b, signature, message, public_key)
{:error, :address_mismatch}
iex> check_signature(address, signature, "", public_key)
{:error, :bad_signature}
iex> check_signature(address, "x" <> signature, "", public_key)
{:error, :invalid_signature}
@spec decode_signature(binary()) :: {:error, :invalid_signature} | {:ok, binary()}
@spec decode_signature!(binary()) :: binary()
@spec derive_address(nonempty_binary()) ::
  {:ok, nonempty_binary()} | {:error, :invalid_pubkey_format}

Derive public key hash (Tezos wallet address) from public key

Examples

iex> derive_address("edpktsPhZ8weLEXqf4Fo5FS9Qx8ZuX4QpEBEwe63L747G8iDjTAF6w")
{:ok, "tz1LKpeN8ZSSFNyTWiBNaE4u4sjaq7J1Vz2z"}
iex> derive_address("sppk7aBerAEA6tv4wzg6FnK7i5YrGtEGFVvNjWhc2QX8bhzpouBVFSW")
{:ok, "tz2BC83pvEAag6r2ZV7kPghNAbjFoiqhCvZx"}
iex> derive_address("p2pk65yRxCX65k6qRPrbqGWvfW5JnLB1p3dn1oM5o9cyqLKPPhJaBMa")
{:ok, "tz3bPFa6mGv8m4Ppn7w5KSDyAbEPwbJNpC9p"}
iex> derive_address("_p2pk65yRxCX65k6qRPrbqGWvfW5JnLB1p3dn1oM5o9cyqLKPPhJaBMa")
{:error, :invalid_pubkey_format}
iex> derive_address("p2pk65yRxCX65k6")
{:error, :invalid_pubkey_format}
Link to this function

encode_pubkey(pkh, hex_pubkey)

View Source
@spec encode_pubkey(nonempty_binary(), nonempty_binary()) ::
  :error | {:ok, nonempty_binary()}

Encode a raw public key

Examples

iex> encode_pubkey("tz1LPggcEZincSDQJUXrskwJPif4aJhWxMjd", "52d396892b2489ec91406f83680b172b5ca42f606128cd4c44ea4d09d31aa524")
{:ok, "edpkuGhdSSNgqT92Gwoxt9vV3TmpQE93TtQSn5kkyULCh7cfeQFTno"}
iex> encode_pubkey("tz1LPggcEZincSDQJUXrskwJPif4aJhWxMjd", "foo")
:error
Link to this function

sign(privkey_param, bytes, watermark \\ <<>>)

View Source
@spec sign(privkey_param(), binary(), binary()) :: nonempty_binary()
Link to this function

sign_message(privkey_param, bytes)

View Source
@spec sign_message(privkey_param(), binary()) :: nonempty_binary()

Sign the hexadecimal/Micheline representation of a string, Micheline encoding is done when bytes do not start with "0501".

Examples

iex> encoded_private_key = "spsk24EJohZHJkZnWEzj3w9wE7BFARpFmq5WAo9oTtqjdJ2t4pyoB3"
iex> sign_message(encoded_private_key, "foo")
"spsig1Uyadmsz75zND5qDjSAteir1NGCEuPaxNnT8QmXkwCJkuzWUyxKqKsjSx3nU4uj2nk8t31VFFhQ4YsPKdZ9ghA2d2fe9HF"
iex> msg = Tezex.Micheline.pack("foo", :string)
"050100000003666f6f"
iex> sign_message(encoded_private_key, msg)
"spsig1Uyadmsz75zND5qDjSAteir1NGCEuPaxNnT8QmXkwCJkuzWUyxKqKsjSx3nU4uj2nk8t31VFFhQ4YsPKdZ9ghA2d2fe9HF"
Link to this function

sign_operation(privkey_param, bytes)

View Source
@spec sign_operation(privkey_param(), binary()) :: nonempty_binary()

Sign an operation using 0x03 as watermark

Link to this function

validate_address(address)

View Source
@spec validate_address(nonempty_binary()) ::
  :ok
  | {:error,
     :invalid_base58 | :invalid_checksum | :invalid_length | :invalid_prefix}

Validate an implicit account address

Examples

iex> validate_address("tz1L9r8mWmRpndRhuvMCWESLGSVeFzQ9NAWx")
{:error, :invalid_checksum}
iex> validate_address("tz3bPFa6mGv8m4Ppn7w5KSD00bEPwbJNpC9p")
{:error, :invalid_base58}
iex> validate_address("tz1L9r8mWmRpndRhuvMCWESLGSVeFz")
{:error, :invalid_length}
iex> validate_address("tp3bPFa6mGv8m4Ppn7w5KSDyAbEPwbJNpC9p")
{:error, :invalid_prefix}
iex> validate_address("tz3bPFa6mGv8m4Ppn7w5KSDyAbEPwbJNpC9p")
:ok
Link to this function

verify_signature(signature, message, pubkey)

View Source
@spec verify_signature(binary(), binary(), binary()) :: boolean()

Verify that signature is a valid signature for message signed with the private key corresponding to public key pubkey