package tezos-protocol-005-PsBABY5H

  1. Overview
  2. Docs
Tezos protocol 005-PsBABY5H package

Install

Dune Dependency

Authors

Maintainers

Sources

tezos-octez-v20.1.tag.bz2
sha256=ddfb5076eeb0b32ac21c1eed44e8fc86a6743ef18ab23fff02d36e365bb73d61
sha512=d22a827df5146e0aa274df48bc2150b098177ff7e5eab52c6109e867eb0a1f0ec63e6bfbb0e3645a6c2112de3877c91a17df32ccbff301891ce4ba630c997a65

doc/src/tezos_raw_protocol_005_PsBABY5H/legacy_script_support_repr.ml.html

Source file legacy_script_support_repr.ml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
(*****************************************************************************)
(*                                                                           *)
(* Open Source License                                                       *)
(* Copyright (c) 2018 Dynamic Ledger Solutions, Inc. <contact@tezos.com>     *)
(* Copyright (c) 2019 Nomadic Labs <contact@nomadic-labs.com>                *)
(* Copyright (c) 2019 Cryptium Labs <contact@cryptium-labs.com>              *)
(*                                                                           *)
(* Permission is hereby granted, free of charge, to any person obtaining a   *)
(* copy of this software and associated documentation files (the "Software"),*)
(* to deal in the Software without restriction, including without limitation *)
(* the rights to use, copy, modify, merge, publish, distribute, sublicense,  *)
(* and/or sell copies of the Software, and to permit persons to whom the     *)
(* Software is furnished to do so, subject to the following conditions:      *)
(*                                                                           *)
(* The above copyright notice and this permission notice shall be included   *)
(* in all copies or substantial portions of the Software.                    *)
(*                                                                           *)
(* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR*)
(* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,  *)
(* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL   *)
(* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER*)
(* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING   *)
(* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER       *)
(* DEALINGS IN THE SOFTWARE.                                                 *)
(*                                                                           *)
(*****************************************************************************)

let manager_script_code: Script_repr.lazy_expr =
  let open Micheline in
  let open Michelson_v1_primitives in
  Script_repr.lazy_expr @@ strip_locations @@
  Seq (0, [
      Prim (0, K_parameter, [
          Prim (0, T_or, [
              Prim (0, T_lambda, [
                  Prim (0, T_unit, [], []);
                  Prim (0, T_list, [
                      Prim (0, T_operation, [], [])
                    ], [])
                ], ["%do"]);
              Prim (0, T_unit, [], ["%default"])
            ], [])
        ], []);
      Prim (0, K_storage, [
          Prim (0, T_key_hash, [], [])
        ], []);
      Prim (0, K_code, [
          Seq (0, [
              Seq (0, [
                  Seq (0, [
                      Prim (0, I_DUP, [], []);
                      Prim (0, I_CAR, [], []);
                      Prim (0, I_DIP, [
                          Seq (0, [
                              Prim (0, I_CDR, [], [])
                            ])
                        ], [])
                    ])
                ]);
              Prim (0, I_IF_LEFT, [
                  Seq (0, [
                      Prim (0, I_PUSH, [
                          Prim (0, T_mutez, [], []);
                          Int (0, Z.zero)
                        ], []);
                      Prim (0, I_AMOUNT, [], []);
                      Seq (0, [
                          Seq (0, [
                              Prim (0, I_COMPARE, [], []);
                              Prim (0, I_EQ, [], [])
                            ]);
                          Prim (0, I_IF, [
                              Seq (0, []);
                              Seq (0, [
                                  Seq (0, [
                                      Prim (0, I_UNIT, [], []);
                                      Prim (0, I_FAILWITH, [], [])
                                    ])
                                ])
                            ], [])
                        ]);
                      Seq (0, [
                          Prim (0, I_DIP, [
                              Seq (0, [
                                  Prim (0, I_DUP, [], [])
                                ])
                            ], []);
                          Prim (0, I_SWAP, [], [])
                        ]);
                      Prim (0, I_IMPLICIT_ACCOUNT, [], []);
                      Prim (0, I_ADDRESS, [], []);
                      Prim (0, I_SENDER, [], []);
                      Seq (0, [
                          Seq (0, [
                              Prim (0, I_COMPARE, [], []);
                              Prim (0, I_EQ, [], [])
                            ]);
                          Prim (0, I_IF, [
                              Seq (0, []);
                              Seq (0, [
                                  Seq (0, [
                                      Prim (0, I_UNIT, [], []);
                                      Prim (0, I_FAILWITH, [], [])
                                    ])
                                ])
                            ], [])
                        ]);
                      Prim (0, I_UNIT, [], []);
                      Prim (0, I_EXEC, [], []);
                      Prim (0, I_PAIR, [], [])
                    ]);
                  Seq (0, [
                      Prim (0, I_DROP, [], []);
                      Prim (0, I_NIL, [
                          Prim (0, T_operation, [], [])
                        ], []);
                      Prim (0, I_PAIR, [], [])
                    ])
                ], [])
            ])
        ], [])
    ])

(* Find the toplevel expression with a given prim type from list,
   because they can be in arbitrary order. *)
let find_toplevel toplevel exprs =
  let open Micheline in
  let rec iter toplevel = function
    | (Prim (_, prim, _, _) as found) :: _
      when String.equal toplevel (Michelson_v1_primitives.string_of_prim prim) ->
        Some found
    | _ :: rest ->
        iter toplevel rest
    | [] ->
        None in
  iter (Michelson_v1_primitives.string_of_prim toplevel) exprs

let add_do:
  manager_pkh: Signature.Public_key_hash.t ->
  script_code: Script_repr.lazy_expr ->
  script_storage: Script_repr.lazy_expr ->
  (Script_repr.lazy_expr * Script_repr.lazy_expr) tzresult Lwt.t =
  fun ~manager_pkh ~script_code ~script_storage ->
  let open Micheline in
  let open Michelson_v1_primitives in
  Lwt.return (Script_repr.force_decode script_code) >>=? fun (script_code_expr, _gas_cost) ->
  Lwt.return (Script_repr.force_decode script_storage) >>|? fun (script_storage_expr, _gas_cost) ->
  let storage_expr = root script_storage_expr in
  match root script_code_expr with
  | Seq (_, toplevel)
    -> begin
        match find_toplevel K_parameter toplevel,
              find_toplevel K_storage toplevel,
              find_toplevel K_code toplevel with
          Some (Prim (_, K_parameter, [
              Prim (_, parameter_type, parameter_expr, parameter_annot)
            ], prim_param_annot)),
          Some (Prim (_, K_storage, [
              Prim (_, code_storage_type, code_storage_expr, code_storage_annot)
            ], k_storage_annot)),
          Some (Prim (_, K_code, [code_expr], code_annot)) ->
            (* Note that we intentionally don't deal with potential duplicate entrypoints in this migration as there already might be some in contracts that we don't touch. *)

            let migrated_code =
              Seq (0, [
                  Prim (0, K_parameter, [
                      Prim (0, T_or, [
                          Prim (0, T_lambda, [
                              Prim (0, T_unit, [], []);
                              Prim (0, T_list, [
                                  Prim (0, T_operation, [], [])
                                ], [])
                            ], ["%do"]);
                          Prim (0, parameter_type, parameter_expr, "%default" :: parameter_annot)
                        ], [])
                    ], prim_param_annot);
                  Prim (0, K_storage, [
                      Prim (0, T_pair, [
                          Prim (0, T_key_hash, [], []);
                          Prim (0, code_storage_type, code_storage_expr, code_storage_annot)
                        ], [])
                    ], k_storage_annot);
                  Prim (0, K_code, [
                      Seq (0, [
                          Prim (0, I_DUP, [], []);
                          Prim (0, I_CAR, [], []);
                          Prim (0, I_IF_LEFT, [
                              Seq (0, [
                                  Prim (0, I_PUSH, [
                                      Prim (0, T_mutez, [], []);
                                      Int (0, Z.zero)
                                    ], []);
                                  Prim (0, I_AMOUNT, [], []);
                                  Seq (0, [
                                      Seq (0, [
                                          Prim (0, I_COMPARE, [], []);
                                          Prim (0, I_EQ, [], [])
                                        ]);
                                      Prim (0, I_IF, [
                                          Seq (0, []);
                                          Seq (0, [
                                              Seq (0, [
                                                  Prim (0, I_UNIT, [], []);
                                                  Prim (0, I_FAILWITH, [], [])
                                                ])
                                            ])
                                        ], [])
                                    ]);
                                  Seq (0, [
                                      Prim (0, I_DIP, [
                                          Seq (0, [
                                              Prim (0, I_DUP, [], [])
                                            ])
                                        ], []);
                                      Prim (0, I_SWAP, [], [])
                                    ]);
                                  Prim (0, I_CDR, [], []);
                                  Prim (0, I_CAR, [], []);
                                  Prim (0, I_IMPLICIT_ACCOUNT, [], []);
                                  Prim (0, I_ADDRESS, [], []);
                                  Prim (0, I_SENDER, [], []);
                                  Seq (0, [
                                      Prim (0, I_COMPARE, [], []);
                                      Prim (0, I_NEQ, [], []);
                                      Prim (0, I_IF, [
                                          Seq (0, [
                                              Prim (0, I_SENDER, [], []);
                                              Prim (0, I_PUSH, [
                                                  Prim (0, T_string, [], []);
                                                  String (0, "Only the owner can operate.")
                                                ], []);
                                              Prim (0, I_PAIR, [], []);
                                              Prim (0, I_FAILWITH, [], [])
                                            ]);
                                          Seq (0, [
                                              Prim (0, I_UNIT, [], []);
                                              Prim (0, I_EXEC, [], []);
                                              Prim (0, I_DIP, [
                                                  Seq (0, [
                                                      Prim (0, I_CDR, [], [])
                                                    ])
                                                ], []);
                                              Prim (0, I_PAIR, [], [])
                                            ])
                                        ], [])
                                    ])
                                ]);
                              Seq (0, [
                                  Prim (0, I_DIP, [
                                      Seq (0, [
                                          Prim (0, I_CDR, [], []);
                                          Prim (0, I_DUP, [], []);
                                          Prim (0, I_CDR, [], [])
                                        ])
                                    ], []);
                                  Prim (0, I_PAIR, [], []);

                                  code_expr;

                                  Prim (0, I_SWAP, [], []);
                                  Prim (0, I_CAR, [], []);
                                  Prim (0, I_SWAP, [], []);
                                  Seq (0, [
                                      Seq (0, [
                                          Prim (0, I_DUP, [], []);
                                          Prim (0, I_CAR, [], []);
                                          Prim (0, I_DIP, [
                                              Seq (0, [
                                                  Prim (0, I_CDR, [], [])
                                                ])
                                            ], [])
                                        ])
                                    ]);
                                  Prim (0, I_DIP, [
                                      Seq (0, [
                                          Prim (0, I_SWAP, [], []);
                                          Prim (0, I_PAIR, [], [])
                                        ])
                                    ], []);
                                  Prim (0, I_PAIR, [], [])
                                ])
                            ], [])
                        ])
                    ], code_annot)
                ])
            in
            let migrated_storage = Prim (0, D_Pair, [
                (* Instead of
                   `String (0, Signature.Public_key_hash.to_b58check manager_pkh)`
                   the storage is written as unparsed with [Optimized] *)
                Bytes (0, Data_encoding.Binary.to_bytes_exn Signature.Public_key_hash.encoding manager_pkh) ;
                storage_expr
              ], []) in
            Script_repr.lazy_expr @@ strip_locations migrated_code,
            Script_repr.lazy_expr @@ strip_locations migrated_storage
        | _ ->
            script_code, script_storage
      end
  | _ ->
      script_code, script_storage



let add_set_delegate:
  manager_pkh: Signature.Public_key_hash.t ->
  script_code: Script_repr.lazy_expr ->
  script_storage: Script_repr.lazy_expr ->
  (Script_repr.lazy_expr * Script_repr.lazy_expr) tzresult Lwt.t =
  fun ~manager_pkh ~script_code ~script_storage ->
  let open Micheline in
  let open Michelson_v1_primitives in
  Lwt.return (Script_repr.force_decode script_code) >>=? fun (script_code_expr, _gas_cost) ->
  Lwt.return (Script_repr.force_decode script_storage) >>|? fun (script_storage_expr, _gas_cost) ->
  let storage_expr = root script_storage_expr in
  match root script_code_expr with
  | Seq (_, toplevel)
    -> begin
        match find_toplevel K_parameter toplevel,
              find_toplevel K_storage toplevel,
              find_toplevel K_code toplevel with
          Some (Prim (_, K_parameter, [
              Prim (_, parameter_type, parameter_expr, parameter_annot)
            ], prim_param_annot)),
          Some (Prim (_, K_storage, [
              Prim (_, code_storage_type, code_storage_expr, code_storage_annot)
            ], k_storage_annot)),
          Some (Prim (_, K_code, [code_expr], code_annot)) ->
            (* Note that we intentionally don't deal with potential duplicate entrypoints in this migration as there already might be some in contracts that we don't touch. *)

            let migrated_code =
              Seq (0, [
                  Prim (0, K_parameter, [
                      Prim (0, T_or, [
                          Prim (0, T_or, [
                              Prim (0, T_key_hash, [], ["%set_delegate"]);
                              Prim (0, T_unit, [], ["%remove_delegate"])
                            ], []);
                          Prim (0, parameter_type, parameter_expr, "%default" :: parameter_annot)
                        ], [])
                    ], prim_param_annot);
                  Prim (0, K_storage, [
                      Prim (0, T_pair, [
                          Prim (0, T_key_hash, [], []);
                          Prim (0, code_storage_type, code_storage_expr, code_storage_annot)
                        ], [])
                    ], k_storage_annot);
                  Prim (0, K_code, [
                      Seq (0, [
                          Prim (0, I_DUP, [], []);
                          Prim (0, I_CAR, [], []);
                          Prim (0, I_IF_LEFT, [
                              Seq (0, [
                                  Prim (0, I_PUSH, [
                                      Prim (0, T_mutez, [], []);
                                      Int (0, Z.zero)
                                    ], []);
                                  Prim (0, I_AMOUNT, [], []);
                                  Seq (0, [
                                      Seq (0, [
                                          Prim (0, I_COMPARE, [], []);
                                          Prim (0, I_EQ, [], [])
                                        ]);
                                      Prim (0, I_IF, [
                                          Seq (0, []);
                                          Seq (0, [
                                              Seq (0, [
                                                  Prim (0, I_UNIT, [], []);
                                                  Prim (0, I_FAILWITH, [], [])
                                                ])
                                            ])
                                        ], [])
                                    ]);
                                  Seq (0, [
                                      Prim (0, I_DIP, [
                                          Seq (0, [
                                              Prim (0, I_DUP, [], [])
                                            ])
                                        ], []);
                                      Prim (0, I_SWAP, [], [])
                                    ]);
                                  Prim (0, I_CDR, [], []);
                                  Prim (0, I_CAR, [], []);
                                  Prim (0, I_IMPLICIT_ACCOUNT, [], []);
                                  Prim (0, I_ADDRESS, [], []);
                                  Prim (0, I_SENDER, [], []);
                                  Seq (0, [
                                      Prim (0, I_COMPARE, [], []);
                                      Prim (0, I_NEQ, [], []);
                                      Prim (0, I_IF, [
                                          Seq (0, [
                                              Prim (0, I_SENDER, [], []);
                                              Prim (0, I_PUSH, [
                                                  Prim (0, T_string, [], []);
                                                  String (0, "Only the owner can operate.")
                                                ], []);
                                              Prim (0, I_PAIR, [], []);
                                              Prim (0, I_FAILWITH, [], [])
                                            ]);
                                          Seq (0, [
                                              Prim (0, I_DIP, [
                                                  Seq (0, [
                                                      Prim (0, I_CDR, [], []);
                                                      Prim (0, I_NIL, [
                                                          Prim (0, T_operation, [], [])
                                                        ], [])
                                                    ])
                                                ], []);
                                              Prim (0, I_IF_LEFT, [
                                                  Seq (0, [
                                                      Prim (0, I_SOME, [], []);
                                                      Prim (0, I_SET_DELEGATE, [], []);
                                                      Prim (0, I_CONS, [], []);
                                                      Prim (0, I_PAIR, [], [])
                                                    ]);
                                                  Seq (0, [
                                                      Prim (0, I_DROP, [], []);
                                                      Prim (0, I_NONE, [
                                                          Prim (0, T_key_hash, [], [])
                                                        ], []);
                                                      Prim (0, I_SET_DELEGATE, [], []);
                                                      Prim (0, I_CONS, [], []);
                                                      Prim (0, I_PAIR, [], [])
                                                    ])
                                                ], [])
                                            ])
                                        ], [])
                                    ])
                                ]);
                              Seq (0, [
                                  Prim (0, I_DIP, [
                                      Seq (0, [
                                          Prim (0, I_CDR, [], []);
                                          Prim (0, I_DUP, [], []);
                                          Prim (0, I_CDR, [], [])
                                        ])
                                    ], []);
                                  Prim (0, I_PAIR, [], []);

                                  code_expr;

                                  Prim (0, I_SWAP, [], []);
                                  Prim (0, I_CAR, [], []);
                                  Prim (0, I_SWAP, [], []);
                                  Seq (0, [
                                      Seq (0, [
                                          Prim (0, I_DUP, [], []);
                                          Prim (0, I_CAR, [], []);
                                          Prim (0, I_DIP, [
                                              Seq (0, [
                                                  Prim (0, I_CDR, [], [])
                                                ])
                                            ], [])
                                        ])
                                    ]);
                                  Prim (0, I_DIP, [
                                      Seq (0, [
                                          Prim (0, I_SWAP, [], []);
                                          Prim (0, I_PAIR, [], [])
                                        ])
                                    ], []);
                                  Prim (0, I_PAIR, [], [])
                                ])
                            ], [])
                        ])
                    ], code_annot)
                ])
            in
            let migrated_storage = Prim (0, D_Pair, [
                (* Instead of
                   `String (0, Signature.Public_key_hash.to_b58check manager_pkh)`
                   the storage is written as unparsed with [Optimized] *)
                Bytes (0, Data_encoding.Binary.to_bytes_exn Signature.Public_key_hash.encoding manager_pkh) ;
                storage_expr
              ], []) in
            Script_repr.lazy_expr @@ strip_locations migrated_code,
            Script_repr.lazy_expr @@ strip_locations migrated_storage
        | _ ->
            script_code, script_storage
      end
  | _ ->
      script_code, script_storage

let has_default_entrypoint expr =
  let open Micheline in
  let open Michelson_v1_primitives in
  match Script_repr.force_decode expr with
  | Error _ -> false
  | Ok (expr, _) ->
      match root expr with
      | Seq (_, toplevel) -> begin
          match find_toplevel K_parameter toplevel with
          | Some (Prim (_, K_parameter, [ _ ], [ "%default" ])) -> false
          | Some (Prim (_, K_parameter, [ parameter_expr ], _)) ->
              let rec has_default = function
                | Prim (_, T_or, [ l ; r ], annots) ->
                    List.exists (String.equal "%default") annots || has_default l || has_default r
                | Prim (_, _, _, annots) ->
                    List.exists (String.equal "%default") annots
                | _ -> false
              in
              has_default parameter_expr
          | Some _ | None -> false
        end
      | _ -> false

let add_root_entrypoint
  : script_code: Script_repr.lazy_expr -> Script_repr.lazy_expr tzresult Lwt.t
  = fun ~script_code ->
    let open Micheline in
    let open Michelson_v1_primitives in
    Lwt.return (Script_repr.force_decode script_code) >>|? fun (script_code_expr, _gas_cost) ->
    match root script_code_expr with
    | Seq (_, toplevel) ->
        let migrated_code =
          Seq (0, List.map (function
              | Prim (_, K_parameter, [ parameter_expr ], _) ->
                  Prim (0, K_parameter, [ parameter_expr ], [ "%root" ])
              | Prim (_, K_code, exprs, annots) ->
                  let rec rewrite_self = function
                    | Int _ | String _ | Bytes _ | Prim (_, I_CREATE_CONTRACT, _, _) as leaf -> leaf
                    | Prim (_, I_SELF, [], annots) ->
                        Prim (0, I_SELF, [], "%root" :: annots)
                    | Prim (_, name, args, annots) ->
                        Prim (0, name, List.map rewrite_self args, annots)
                    | Seq (_, args) ->
                        Seq (0, List.map rewrite_self args) in
                  Prim (0, K_code, List.map rewrite_self exprs, annots)
              | other -> other)
              toplevel) in
        Script_repr.lazy_expr @@ strip_locations migrated_code
    | _ ->
        script_code
OCaml

Innovation. Community. Security.