View Source Tezex.Crypto (tezex v1.0.0-rc4)
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).
Link to this section 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
Verify that signature
is a valid signature for message
signed with the private key corresponding to public key pubkey
Link to this section 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
Examples
iex> pubkey = "sppk7aBerAEA6tv4wzg6FnK7i5YrGtEGFVvNjWhc2QX8bhzpouBVFSW"
iex> Tezex.Crypto.check_address("tz2BC83pvEAag6r2ZV7kPghNAbjFoiqhCvZx", pubkey)
:ok
iex> Tezex.Crypto.check_address("tz1burnburnburnburnburnburnburjAYjjX", pubkey)
{:error, :address_mismatch}
iex> Tezex.Crypto.check_address("tz2BC83pvEAag6r2ZV7kPghNAbjFoiqhCvZx", "x" <> pubkey)
{:error, :invalid_pubkey_format}
@spec check_signature(binary(), binary(), binary(), binary()) :: :ok | {:error, :address_mismatch | :invalid_pubkey_format | :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
Examples
iex> address = "tz2BC83pvEAag6r2ZV7kPghNAbjFoiqhCvZx"
iex> address_b = "tz1burnburnburnburnburnburnburjAYjjX"
iex> signature = "spsig1ZNQaUKNERZSiEiNviqa5EAPkcNASXhfkXtxRatZTDZAnUB4Ra2Jus8b1oEpFnPx8Z6g28pd8vK3R8nPK29JDU5FiSLH5T"
iex> message = "05010000007154657a6f73205369676e6564204d6573736167653a207369676e206d6520696e20617320747a32424338337076454161673672325a56376b5067684e41626a466f69716843765a78206f6e206f626a6b742e636f6d20617420323032312d31302d30345431383a35393a31332e3939305a"
iex> public_key = "sppk7aBerAEA6tv4wzg6FnK7i5YrGtEGFVvNjWhc2QX8bhzpouBVFSW"
iex> Tezex.Crypto.check_signature(address, signature, message, public_key)
:ok
iex> Tezex.Crypto.check_signature(address_b, signature, message, public_key)
{:error, :address_mismatch}
iex> Tezex.Crypto.check_signature(address, signature, "", public_key)
{:error, :bad_signature}
iex> Tezex.Crypto.check_signature(address, "x" <> signature, "", public_key)
{:error, :invalid_pubkey_format}
@spec derive_address(nonempty_binary()) ::
{:ok, nonempty_binary()} | {:error, :invalid_pubkey_format}
Derive public key hash (Tezos wallet address) from public key
examples
Examples
iex> Tezex.Crypto.derive_address("edpktsPhZ8weLEXqf4Fo5FS9Qx8ZuX4QpEBEwe63L747G8iDjTAF6w")
{:ok, "tz1LKpeN8ZSSFNyTWiBNaE4u4sjaq7J1Vz2z"}
iex> Tezex.Crypto.derive_address("sppk7aBerAEA6tv4wzg6FnK7i5YrGtEGFVvNjWhc2QX8bhzpouBVFSW")
{:ok, "tz2BC83pvEAag6r2ZV7kPghNAbjFoiqhCvZx"}
iex> Tezex.Crypto.derive_address("p2pk65yRxCX65k6qRPrbqGWvfW5JnLB1p3dn1oM5o9cyqLKPPhJaBMa")
{:ok, "tz3bPFa6mGv8m4Ppn7w5KSDyAbEPwbJNpC9p"}
iex> Tezex.Crypto.derive_address("_p2pk65yRxCX65k6qRPrbqGWvfW5JnLB1p3dn1oM5o9cyqLKPPhJaBMa")
{:error, :invalid_pubkey_format}
iex> Tezex.Crypto.derive_address("p2pk65yRxCX65k6")
{:error, :invalid_pubkey_format}
Verify that signature
is a valid signature for message
signed with the private key corresponding to public key pubkey