diff --git a/gitlab-pages/docs/language-basics/tezos-specific.md b/gitlab-pages/docs/language-basics/tezos-specific.md new file mode 100644 index 000000000..ff1cf6644 --- /dev/null +++ b/gitlab-pages/docs/language-basics/tezos-specific.md @@ -0,0 +1,80 @@ +--- +id: tezos-specific +title: Tezos Domain-Specific Operations +--- + +LIGO is a language for writing Tezos smart contracts. It would be a little odd if +it didn't have any Tezos specific functions. This page will tell you about them. + +## Pack and Unpack + +Michelson provides the `PACK` and `UNPACK` instructions for data serialization. +`PACK` converts Michelson data structures to a binary format, and `UNPACK` +reverses it. This functionality can be accessed from within LIGO. + +> ⚠️ There should be a message here about the dangers of using `UNPACK` on untrusted +data, perhaps about executable code? + + + + +```pascaligo +function id_string (const p : string) : option(string) is block { + const packed : bytes = bytes_pack(p) ; +} with (bytes_unpack(packed): option(string)) +``` + + +```cameligo +let id_string (p: string) : string option = + let packed: bytes = Bytes.pack p in + ((Bytes.unpack packed): string option) +``` + + +```reasonligo +let id_string = (p: string) : option(string) => { + let packed : bytes = Bytes.pack(p); + ((Bytes.unpack(packed)): option(string)); +}; +``` + + + +## Hashing Keys + + + + +```pascaligo +function check_hash_key (const kh1 : key_hash; const k2 : key) : bool * key_hash is block { + var ret : bool := False ; + var kh2 : key_hash := crypto_hash_key(k2) ; + if kh1 = kh2 then ret := True else skip; +} with (ret, kh2) +``` + + +```cameligo +let check_hash_key (kh1, k2: key_hash * key) : bool * key_hash = + let kh2 : key_hash = Crypto.hash_key k2 in + if kh1 = kh2 + then (true, kh2) + else (false, kh2) +``` + + +```reasonligo +let check_hash_key = (kh1_k2: (key_hash, key)) : (bool, key_hash) => { + let kh1, k2 = kh1_k2; + let kh2 : key_hash = Crypto.hash_key(k2); + if (kh1 == kh2) { + (true, kh2); + } + else { + (false, kh2); + } +}; +``` + + diff --git a/gitlab-pages/website/sidebars.json b/gitlab-pages/website/sidebars.json index 00fe28d25..674fa7cb2 100644 --- a/gitlab-pages/website/sidebars.json +++ b/gitlab-pages/website/sidebars.json @@ -11,7 +11,8 @@ "language-basics/loops", "language-basics/unit-option-pattern-matching", "language-basics/maps-records", - "language-basics/sets-lists-touples" + "language-basics/sets-lists-touples", + "language-basics/tezos-specific" ], "Advanced": [ "advanced/timestamps-addresses",