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
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.
@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
@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}
@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 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}
@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
@spec sign(privkey_param(), binary(), binary()) :: nonempty_binary()
@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"
@spec sign_operation(privkey_param(), binary()) :: nonempty_binary()
Sign an operation using 0x03 as watermark
@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
@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 that signature is a valid signature for message signed with the private key corresponding to public key pubkey