Crypto
Cryptographic utilities.

Addresses

Address literals are prefixed by @.
1
variable admin : address = @tz1ZXf37ZNfVupt5GVyrPJ76q8kbjFuD2z7J
2
variable acontract : address = @KT19NxoYm9rjDQxz2gJiFSY8Z5Mjofq7rebs
Copied!
Addresses may be compared with the 6 comparison operators = <> < > <= >=.

Bytes

Bytes literals are prefixed by 0x.
1
variable bvalue : bytes = 0x050100000009617263686574797065 // "archetype" in bytes
Copied!

Expressions

The following operators are available:
  • concat
  • slice
1
effect {
2
var byt_concat : bytes = concat(0x12, 0xef);
3
var byt_slice : bytes = slice(0xabcdef01, 1, 2);
4
}
Copied!
Bytes values are comparable with = <> < > <= >= operators.

Hashing

Hashing functions are available: blake2b sha256 sha512 keccak and sha3 They convert a byte value to the hashed byte value.
1
var h = sha256(0x050100000009617263686574797065);
Copied!

pack, unpack

It is possible to convert any value from and to a bytes value with pack and unpack operators.
The unpack operator is parameterized with the expected output type using <T> syntax. It returns an value option and hence does not fail.
1
effect {
2
var bstr := pack("archetype"); // bstr is a bytes value
3
if getopt(unpack<string>(bstr)) = "archetype"
4
then transfer 1tz to coder;
5
}
Copied!

Keys and Signatures

In smart contracts, it may be necessary to assert the origin of an input data: for example an oracle may need to write a value based on which critical decisions will be made; it is then necessary to make sure that the data written in the contract is delivered by the oracle. The solution is to pass the signed version of the data along with the data.
It is possible to check whether a bytes value has been signed by a given key with the check_signature operator. It takes 3 arguments:
  1. 1.
    the public key of the one who supposedly signed the data (key type)
  2. 2.
    a signed data (signature type)
  3. 3.
    the bytes data (bytes type)
It returns true if and only if the encryption by the public key of the bytes data is equal to the signed data.
For example, the following enables an oracle to write a value outcome typed int in the contract:
1
archetype oraclesetvalue
2
3
variable outcome : int option = none
4
5
// oracle's public key
6
constant oracle : key = "edpkurLzuFFL1XyP3fed4u7MsgeywQoQmHM45Bz91PBzDvUjQ9bvdn"
7
8
entry setoutcome (packed_outcome : bytes, signed_outcome : signature) {
9
effect {
10
if check_signature(oracle, signed_outcome, packed_outcome) then (
11
outcome := unpack<int>(packed_outcome);
12
) else fail("not signed by oracle");
13
}
14
}
Copied!
Note that anyone can call the setoutcome entry. It is ok as long as they possess the data and the data signed by the oracle.
Note also that key literals use the string syntax.
Below is an instance of the above contract with the outcome value set to 42.
https://you.better-call.dev/carthagenet/KT1AiNKbVi6c74pyxMZJnK4yj2P9GxP2K2d2/operations
you.better-call.dev
Last modified 8mo ago
Export as PDF
Copy link
Edit on GitHub