Tezex.Crypto (tezex v6.0.0)

Copy Markdown View Source

Provides cryptographic functions for Tezos blockchain operations.

This module includes functions to:

  • Check Tezos signed messages
  • Derive public key hashes (pkh) from public keys
  • Verify that a public key corresponds to a wallet address (public key hash)
  • Sign messages and operations
  • Encode and decode various Tezos-specific data formats

Summary

Types

Represents a private key parameter. It can be either a binary string (the private key itself) or a tuple containing the private key and a passphrase.

Represents the result of a signature verification operation. It can be either :ok if the signature is valid, or an error tuple with a specific reason.

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

Validate an originated account address

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

Types

privkey_param()

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

Represents a private key parameter. It can be either a binary string (the private key itself) or a tuple containing the private key and a passphrase.

signature_verification_result()

@type signature_verification_result() ::
  :ok
  | {:error,
     :address_mismatch
     | :invalid_pubkey_format
     | :invalid_signature
     | :bad_signature}

Represents the result of a signature verification operation. It can be either :ok if the signature is valid, or an error tuple with a specific reason.

Functions

check_address(address, pubkey)

@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}

check_signature(address, signature, message, pubkey)

@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}

decode_signature(sig)

@spec decode_signature(binary()) :: {:error, :invalid_signature} | {:ok, binary()}

decode_signature!(sig)

@spec decode_signature!(binary()) :: binary()

derive_address(pubkey)

@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}

encode_pubkey(pkh, hex_pubkey)

@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

extract_pubkey(pubkey)

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

@spec sign(privkey_param(), binary(), binary()) :: nonempty_binary()

sign_message(privkey_param, bytes)

@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"

sign_operation(privkey_param, bytes)

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

Sign an operation using 0x03 as watermark

validate_address(address)

@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
iex> validate_address("tz4NWRt3aFyFU2Ydah917Eehxv6uf97j8tpZ")
:ok

validate_originated_address(address)

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

Validate an originated account address

Examples

iex> validate_originated_address("KT1VbT8n6YbrzPSjdAscKfJGDDNafB5yHn1H")
:ok
iex> validate_originated_address("KT1VbT8n6YbrzPSjdAscKfJGDDNafB5yHn1X")
{:error, :invalid_checksum}
iex> validate_originated_address("KT1VbT8n6YbrzPSjdAscKfJGDDNafB5yHn10")
{:error, :invalid_base58}
iex> validate_originated_address("KT1VbT8n6YbrzPSjdAscKfJGDDNafB")
{:error, :invalid_length}
iex> validate_originated_address("tz3bPFa6mGv8m4Ppn7w5KSDyAbEPwbJNpC9p")
{:error, :invalid_prefix}

verify_signature(signature, msg, pubkey)

@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