proofmode 0.9.0

Capture, share, and preserve verifiable photos and videos
Documentation
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
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
# This file was autogenerated by some hot garbage in the `uniffi` crate.
# Trust me, you don't want to mess with it!

# Common helper code.
#
# Ideally this would live in a separate .rb file where it can be unittested etc
# in isolation, and perhaps even published as a re-useable package.
#
# However, it's important that the details of how this helper code works (e.g. the
# way that different builtin types are passed across the FFI) exactly match what's
# expected by the rust code on the other side of the interface. In practice right
# now that means coming from the exact some version of `uniffi` that was used to
# compile the rust component. The easiest way to ensure this is to bundle the Ruby
# helpers directly inline like we're doing here.

require 'ffi'


module Proofmode
  def self.uniffi_in_range(i, type_name, min, max)
  raise TypeError, "no implicit conversion of #{i} into Integer" unless i.respond_to?(:to_int)
  i = i.to_int
  raise RangeError, "#{type_name} requires #{min} <= value < #{max}" unless (min <= i && i < max)
  i
end

def self.uniffi_utf8(v)
  raise TypeError, "no implicit conversion of #{v} into String" unless v.respond_to?(:to_str)
  v = v.to_str.encode(Encoding::UTF_8)
  raise Encoding::InvalidByteSequenceError, "not a valid UTF-8 encoded string" unless v.valid_encoding?
  v
end

def self.uniffi_bytes(v)
  raise TypeError, "no implicit conversion of #{v} into String" unless v.respond_to?(:to_str)
  v.to_str
end

  class RustBuffer < FFI::Struct
  layout :capacity, :uint64,
         :len,      :uint64,
         :data,     :pointer

  def self.alloc(size)
    return Proofmode.rust_call(:ffi_proofmode_rustbuffer_alloc, size)
  end

  def self.reserve(rbuf, additional)
    return Proofmode.rust_call(:ffi_proofmode_rustbuffer_reserve, rbuf, additional)
  end

  def free
    Proofmode.rust_call(:ffi_proofmode_rustbuffer_free, self)
  end

  def capacity
    self[:capacity]
  end

  def len
    self[:len]
  end

  def len=(value)
    self[:len] = value
  end

  def data
    self[:data]
  end

  def to_s
    "RustBuffer(capacity=#{capacity}, len=#{len}, data=#{data.read_bytes len})"
  end

  # The allocated buffer will be automatically freed if an error occurs, ensuring that
  # we don't accidentally leak it.
  def self.allocWithBuilder
    builder = RustBufferBuilder.new

    begin
      yield builder
    rescue => e
      builder.discard
      raise e
    end
  end

  # The RustBuffer will be freed once the context-manager exits, ensuring that we don't
  # leak it even if an error occurs.
  def consumeWithStream
    stream = RustBufferStream.new self

    yield stream

    raise RuntimeError, 'junk data left in buffer after consuming' if stream.remaining != 0
  ensure
    free
  end# The primitive String type.

  def self.allocFromString(value)
    RustBuffer.allocWithBuilder do |builder|
      builder.write value.encode('utf-8')
      return builder.finalize
    end
  end

  def consumeIntoString
    consumeWithStream do |stream|
      return stream.read(stream.remaining).force_encoding(Encoding::UTF_8)
    end
  end

  # The primitive Bytes type.

  def self.allocFromBytes(value)
    RustBuffer.allocWithBuilder do |builder|
      builder.write_Bytes(value)
      return builder.finalize
    end
  end

  def consumeIntoBytes
    consumeWithStream do |stream|
      return stream.readBytes
    end
  end

  def self.alloc_from_Timestamp(v)
    RustBuffer.allocWithBuilder do |builder|
      builder.write_Timestamp(v)
      return builder.finalize
    end
  end

  def consumeIntoTimestamp
    consumeWithStream do |stream|
      return stream.readTimestamp
    end
  end

  # The Record type DeviceInfo.

  def self.check_lower_TypeDeviceInfo(v)
    
    
    
    
    RustBuffer.check_lower_Optionalstring(v.imei)
  end

  def self.alloc_from_TypeDeviceInfo(v)
    RustBuffer.allocWithBuilder do |builder|
      builder.write_TypeDeviceInfo(v)
      return builder.finalize
    end
  end

  def consumeIntoTypeDeviceInfo
    consumeWithStream do |stream|
      return stream.readTypeDeviceInfo
    end
  end

  # The Record type LocationInfo.

  def self.check_lower_TypeLocationInfo(v)
    
    
    RustBuffer.check_lower_Optionalf64(v.altitude)
    RustBuffer.check_lower_Optionalf64(v.accuracy)
    
  end

  def self.alloc_from_TypeLocationInfo(v)
    RustBuffer.allocWithBuilder do |builder|
      builder.write_TypeLocationInfo(v)
      return builder.finalize
    end
  end

  def consumeIntoTypeLocationInfo
    consumeWithStream do |stream|
      return stream.readTypeLocationInfo
    end
  end

  # The Record type NetworkInfo.

  def self.check_lower_TypeNetworkInfo(v)
    
    RustBuffer.check_lower_Optionalstring(v.carrier)
    RustBuffer.check_lower_Optionalstring(v.cell_tower_id)
    RustBuffer.check_lower_Optionalstring(v.wifi_ssid)
  end

  def self.alloc_from_TypeNetworkInfo(v)
    RustBuffer.allocWithBuilder do |builder|
      builder.write_TypeNetworkInfo(v)
      return builder.finalize
    end
  end

  def consumeIntoTypeNetworkInfo
    consumeWithStream do |stream|
      return stream.readTypeNetworkInfo
    end
  end

  

  # The Optional<T> type for f64.

  def self.check_lower_Optionalf64(v)
    if not v.nil?
      
    end
  end

  def self.alloc_from_Optionalf64(v)
    RustBuffer.allocWithBuilder do |builder|
      builder.write_Optionalf64(v)
      return builder.finalize()
    end
  end

  def consumeIntoOptionalf64
    consumeWithStream do |stream|
      return stream.readOptionalf64
    end
  end

  # The Optional<T> type for string.

  def self.check_lower_Optionalstring(v)
    if not v.nil?
      
    end
  end

  def self.alloc_from_Optionalstring(v)
    RustBuffer.allocWithBuilder do |builder|
      builder.write_Optionalstring(v)
      return builder.finalize()
    end
  end

  def consumeIntoOptionalstring
    consumeWithStream do |stream|
      return stream.readOptionalstring
    end
  end

  # The Optional<T> type for bytes.

  def self.check_lower_Optionalbytes(v)
    if not v.nil?
      
    end
  end

  def self.alloc_from_Optionalbytes(v)
    RustBuffer.allocWithBuilder do |builder|
      builder.write_Optionalbytes(v)
      return builder.finalize()
    end
  end

  def consumeIntoOptionalbytes
    consumeWithStream do |stream|
      return stream.readOptionalbytes
    end
  end

  # The Optional<T> type for TypeDeviceInfo.

  def self.check_lower_OptionalTypeDeviceInfo(v)
    if not v.nil?
      RustBuffer.check_lower_TypeDeviceInfo(v)
    end
  end

  def self.alloc_from_OptionalTypeDeviceInfo(v)
    RustBuffer.allocWithBuilder do |builder|
      builder.write_OptionalTypeDeviceInfo(v)
      return builder.finalize()
    end
  end

  def consumeIntoOptionalTypeDeviceInfo
    consumeWithStream do |stream|
      return stream.readOptionalTypeDeviceInfo
    end
  end

  # The Optional<T> type for TypeLocationInfo.

  def self.check_lower_OptionalTypeLocationInfo(v)
    if not v.nil?
      RustBuffer.check_lower_TypeLocationInfo(v)
    end
  end

  def self.alloc_from_OptionalTypeLocationInfo(v)
    RustBuffer.allocWithBuilder do |builder|
      builder.write_OptionalTypeLocationInfo(v)
      return builder.finalize()
    end
  end

  def consumeIntoOptionalTypeLocationInfo
    consumeWithStream do |stream|
      return stream.readOptionalTypeLocationInfo
    end
  end

  # The Optional<T> type for TypeNetworkInfo.

  def self.check_lower_OptionalTypeNetworkInfo(v)
    if not v.nil?
      RustBuffer.check_lower_TypeNetworkInfo(v)
    end
  end

  def self.alloc_from_OptionalTypeNetworkInfo(v)
    RustBuffer.allocWithBuilder do |builder|
      builder.write_OptionalTypeNetworkInfo(v)
      return builder.finalize()
    end
  end

  def consumeIntoOptionalTypeNetworkInfo
    consumeWithStream do |stream|
      return stream.readOptionalTypeNetworkInfo
    end
  end

  # The Sequence<T> type for string.

  def self.check_lower_Sequencestring(v)
    v.each do |item|
      
    end
  end

  def self.alloc_from_Sequencestring(v)
    RustBuffer.allocWithBuilder do |builder|
      builder.write_Sequencestring(v)
      return builder.finalize()
    end
  end

  def consumeIntoSequencestring
    consumeWithStream do |stream|
      return stream.readSequencestring
    end
  end

  # The Map<T> type for string.

  def self.check_lower_MapStringString(v)
    v.each do |k, v|
      
      
    end
  end

  def self.alloc_from_MapStringString(v)
    RustBuffer.allocWithBuilder do |builder|
      builder.write_MapStringString(v)
      return builder.finalize
    end
  end

  def consumeIntoMapStringString
    consumeWithStream do |stream|
      return stream.readMapStringString
    end
  end
end

module UniFFILib
  class ForeignBytes < FFI::Struct
    layout :len,      :int32,
           :data,     :pointer

    def len
      self[:len]
    end

    def data
      self[:data]
    end

    def to_s
      "ForeignBytes(len=#{len}, data=#{data.read_bytes(len)})"
    end
  end
end

private_constant :UniFFILib
  
# Helper for structured reading of values from a RustBuffer.
class RustBufferStream

  def initialize(rbuf)
    @rbuf = rbuf
    @offset = 0
  end

  def remaining
    @rbuf.len - @offset
  end

  def read(size)
    raise InternalError, 'read past end of rust buffer' if @offset + size > @rbuf.len

    data = @rbuf.data.get_bytes @offset, size

    @offset += size

    data
  end

  def readF64
    unpack_from 8, 'G'
  end

  def readBool
    v = unpack_from 1, 'c'

    return false if v == 0
    return true if v == 1

    raise InternalError, 'Unexpected byte for Boolean type'
  end

  def readString
    size = unpack_from 4, 'l>'

    raise InternalError, 'Unexpected negative string length' if size.negative?

    read(size).force_encoding(Encoding::UTF_8)
  end

  def readBytes
    size = unpack_from 4, 'l>'

    raise InternalError, 'Unexpected negative byte string length' if size.negative?

    read(size).force_encoding(Encoding::BINARY)
  end

  # The Timestamp type.
  ONE_SECOND_IN_NANOSECONDS = 10**9

  def readTimestamp
    seconds = unpack_from 8, 'q>'
    nanoseconds = unpack_from 4, 'L>'

    # UniFFi conventions assume that nanoseconds part has to represent nanoseconds portion of
    # duration between epoch and the timestamp moment. Ruby `Time#tv_nsec` returns the number of
    # nanoseconds for the subsecond part, which is sort of opposite to "duration" meaning.
    # Hence we need to convert value returned by `Time#tv_nsec` back and forth with the following
    # logic:
    if seconds < 0 && nanoseconds != 0
      # In order to get duration nsec we shift by 1 second:
      nanoseconds = ONE_SECOND_IN_NANOSECONDS - nanoseconds

      # Then we compensate 1 second shift:
      seconds -= 1
    end

    Time.at(seconds, nanoseconds, :nanosecond, in: '+00:00').utc
  end

  # The Object type MobileCallbacks.

  def readTypeMobileCallbacks
    pointer = FFI::Pointer.new unpack_from 8, 'Q>'
    return MobileCallbacks.uniffi_allocate(pointer)
  end

  # The Record type DeviceInfo.

  def readTypeDeviceInfo
    DeviceInfo.new(
      manufacturer: readString,
      model: readString,
      os_version: readString,
      device_id: readString,
      imei: readOptionalstring
    )
  end

  # The Record type LocationInfo.

  def readTypeLocationInfo
    LocationInfo.new(
      latitude: readF64,
      longitude: readF64,
      altitude: readOptionalf64,
      accuracy: readOptionalf64,
      timestamp: readTimestamp
    )
  end

  # The Record type NetworkInfo.

  def readTypeNetworkInfo
    NetworkInfo.new(
      network_type: readString,
      carrier: readOptionalstring,
      cell_tower_id: readOptionalstring,
      wifi_ssid: readOptionalstring
    )
  end

  

  

  # The Error type ProofModeError

  def readTypeProofModeError
    variant = unpack_from 4, 'l>'
    
    if variant == 1
        return ProofModeError::Io.new(
            readString()
        )
    end
    if variant == 2
        return ProofModeError::Generation.new(
            readString()
        )
    end
    if variant == 3
        return ProofModeError::Check.new(
            readString()
        )
    end

    raise InternalError, 'Unexpected variant tag for TypeProofModeError'
  end
  

  # The Optional<T> type for f64.

  def readOptionalf64
    flag = unpack_from 1, 'c'

    if flag == 0
      return nil
    elsif flag == 1
      return readF64
    else
      raise InternalError, 'Unexpected flag byte for Optionalf64'
    end
  end

  # The Optional<T> type for string.

  def readOptionalstring
    flag = unpack_from 1, 'c'

    if flag == 0
      return nil
    elsif flag == 1
      return readString
    else
      raise InternalError, 'Unexpected flag byte for Optionalstring'
    end
  end

  # The Optional<T> type for bytes.

  def readOptionalbytes
    flag = unpack_from 1, 'c'

    if flag == 0
      return nil
    elsif flag == 1
      return readBytes
    else
      raise InternalError, 'Unexpected flag byte for Optionalbytes'
    end
  end

  # The Optional<T> type for TypeDeviceInfo.

  def readOptionalTypeDeviceInfo
    flag = unpack_from 1, 'c'

    if flag == 0
      return nil
    elsif flag == 1
      return readTypeDeviceInfo
    else
      raise InternalError, 'Unexpected flag byte for OptionalTypeDeviceInfo'
    end
  end

  # The Optional<T> type for TypeLocationInfo.

  def readOptionalTypeLocationInfo
    flag = unpack_from 1, 'c'

    if flag == 0
      return nil
    elsif flag == 1
      return readTypeLocationInfo
    else
      raise InternalError, 'Unexpected flag byte for OptionalTypeLocationInfo'
    end
  end

  # The Optional<T> type for TypeNetworkInfo.

  def readOptionalTypeNetworkInfo
    flag = unpack_from 1, 'c'

    if flag == 0
      return nil
    elsif flag == 1
      return readTypeNetworkInfo
    else
      raise InternalError, 'Unexpected flag byte for OptionalTypeNetworkInfo'
    end
  end

  # The Sequence<T> type for string.

  def readSequencestring
    count = unpack_from 4, 'l>'

    raise InternalError, 'Unexpected negative sequence length' if count.negative?

    items = []

    count.times do
      items.append readString
    end

    items
  end

  # The Map<T> type for string.

  def readMapStringString
    count = unpack_from 4, 'l>'
    raise InternalError, 'Unexpected negative map size' if count.negative?

    items = {}
    count.times do
      key = readString
      items[key] = readString
    end

    items
  end

  def unpack_from(size, format)
    raise InternalError, 'read past end of rust buffer' if @offset + size > @rbuf.len

    value = @rbuf.data.get_bytes(@offset, size).unpack format

    @offset += size

    # TODO: verify this
    raise 'more than one element!!!' if value.size > 1

    value[0]
  end
end

private_constant :RustBufferStream
  
# Helper for structured writing of values into a RustBuffer.
class RustBufferBuilder
  def initialize
    @rust_buf = RustBuffer.alloc 16
    @rust_buf.len = 0
  end

  def finalize
    rbuf = @rust_buf

    @rust_buf = nil

    rbuf
  end

  def discard
    return if @rust_buf.nil?

    rbuf = finalize
    rbuf.free
  end

  def write(value)
    reserve(value.bytes.size) do
      @rust_buf.data.put_array_of_char @rust_buf.len, value.bytes
    end
  end

  def write_F64(v)
    pack_into(8, 'G', v)
  end

  def write_Bool(v)
    pack_into(1, 'c', v ? 1 : 0)
  end

  def write_String(v)
    v = Proofmode::uniffi_utf8(v)
    pack_into 4, 'l>', v.bytes.size
    write v
  end

  def write_Bytes(v)
    v = Proofmode::uniffi_bytes(v)
    pack_into 4, 'l>', v.bytes.size
    write v
  end

  # The Timestamp type.
  ONE_SECOND_IN_NANOSECONDS = 10**9

  def write_Timestamp(v)
    seconds = v.tv_sec
    nanoseconds = v.tv_nsec

    # UniFFi conventions assume that nanoseconds part has to represent nanoseconds portion of
    # duration between epoch and the timestamp moment. Ruby `Time#tv_nsec` returns the number of
    # nanoseconds for the subsecond part, which is sort of opposite to "duration" meaning.
    # Hence we need to convert value returned by `Time#tv_nsec` back and forth with the following
    # logic:
    if seconds < 0 && nanoseconds != 0
      # In order to get duration nsec we shift by 1 second:
      nanoseconds = ONE_SECOND_IN_NANOSECONDS - nanoseconds

      # Then we compensate 1 second shift:
      seconds += 1
    end

    pack_into 8, 'q>', seconds
    pack_into 4, 'L>', nanoseconds
  end

  # The Object type MobileCallbacks.

  def write_TypeMobileCallbacks(obj)
    pointer = MobileCallbacks.uniffi_lower obj
    pack_into(8, 'Q>', pointer.address)
  end

  # The Record type DeviceInfo.

  def write_TypeDeviceInfo(v)
    self.write_String(v.manufacturer)
    self.write_String(v.model)
    self.write_String(v.os_version)
    self.write_String(v.device_id)
    self.write_Optionalstring(v.imei)
  end

  # The Record type LocationInfo.

  def write_TypeLocationInfo(v)
    self.write_F64(v.latitude)
    self.write_F64(v.longitude)
    self.write_Optionalf64(v.altitude)
    self.write_Optionalf64(v.accuracy)
    self.write_Timestamp(v.timestamp)
  end

  # The Record type NetworkInfo.

  def write_TypeNetworkInfo(v)
    self.write_String(v.network_type)
    self.write_Optionalstring(v.carrier)
    self.write_Optionalstring(v.cell_tower_id)
    self.write_Optionalstring(v.wifi_ssid)
  end

  

  # The Optional<T> type for f64.

  def write_Optionalf64(v)
    if v.nil?
      pack_into(1, 'c', 0)
    else
      pack_into(1, 'c', 1)
      self.write_F64(v)
    end
  end

  # The Optional<T> type for string.

  def write_Optionalstring(v)
    if v.nil?
      pack_into(1, 'c', 0)
    else
      pack_into(1, 'c', 1)
      self.write_String(v)
    end
  end

  # The Optional<T> type for bytes.

  def write_Optionalbytes(v)
    if v.nil?
      pack_into(1, 'c', 0)
    else
      pack_into(1, 'c', 1)
      self.write_Bytes(v)
    end
  end

  # The Optional<T> type for TypeDeviceInfo.

  def write_OptionalTypeDeviceInfo(v)
    if v.nil?
      pack_into(1, 'c', 0)
    else
      pack_into(1, 'c', 1)
      self.write_TypeDeviceInfo(v)
    end
  end

  # The Optional<T> type for TypeLocationInfo.

  def write_OptionalTypeLocationInfo(v)
    if v.nil?
      pack_into(1, 'c', 0)
    else
      pack_into(1, 'c', 1)
      self.write_TypeLocationInfo(v)
    end
  end

  # The Optional<T> type for TypeNetworkInfo.

  def write_OptionalTypeNetworkInfo(v)
    if v.nil?
      pack_into(1, 'c', 0)
    else
      pack_into(1, 'c', 1)
      self.write_TypeNetworkInfo(v)
    end
  end

  # The Sequence<T> type for string.

  def write_Sequencestring(items)
    pack_into(4, 'l>', items.size)

    items.each do |item|
      self.write_String(item)
    end
  end

  # The Map<T> type for string.

  def write_MapStringString(items)
    pack_into(4, 'l>', items.size)

    items.each do |k, v|
      write_String(k)
      self.write_String(v)
    end
  end

  private

  def reserve(num_bytes)
    if @rust_buf.len + num_bytes > @rust_buf.capacity
      @rust_buf = RustBuffer.reserve(@rust_buf, num_bytes)
    end

    yield

    @rust_buf.len += num_bytes
  end

  def pack_into(size, format, value)
    reserve(size) do
      @rust_buf.data.put_array_of_char @rust_buf.len, [value].pack(format).bytes
    end
  end
end

private_constant :RustBufferBuilder

  # Error definitions
  class RustCallStatus < FFI::Struct
  layout :code,    :int8,
         :error_buf, RustBuffer

  def code
    self[:code]
  end

  def error_buf
    self[:error_buf]
  end

  def to_s
    "RustCallStatus(code=#{self[:code]})"
  end
end

# These match the values from the uniffi::rustcalls module
CALL_SUCCESS = 0
CALL_ERROR = 1
CALL_PANIC = 2


module ProofModeError
  class Io < StandardError
    def initialize(message)
        @message = message
        super()
      end

    attr_reader :message
    

    def to_s
     "#{self.class.name}(message=#{@message.inspect})"
    end
  end
  class Generation < StandardError
    def initialize(message)
        @message = message
        super()
      end

    attr_reader :message
    

    def to_s
     "#{self.class.name}(message=#{@message.inspect})"
    end
  end
  class Check < StandardError
    def initialize(message)
        @message = message
        super()
      end

    attr_reader :message
    

    def to_s
     "#{self.class.name}(message=#{@message.inspect})"
    end
  end

end


# Map error modules to the RustBuffer method name that reads them
ERROR_MODULE_TO_READER_METHOD = {

  ProofModeError => :readTypeProofModeError,

}

private_constant :ERROR_MODULE_TO_READER_METHOD, :CALL_SUCCESS, :CALL_ERROR, :CALL_PANIC,
                 :RustCallStatus

def self.consume_buffer_into_error(error_module, rust_buffer)
  rust_buffer.consumeWithStream do |stream|
    reader_method = ERROR_MODULE_TO_READER_METHOD[error_module]
    return stream.send(reader_method)
  end
end

class InternalError < StandardError
end

def self.rust_call(fn_name, *args)
  # Call a rust function
  rust_call_with_error(nil, fn_name, *args)
end

def self.rust_call_with_error(error_module, fn_name, *args)
  # Call a rust function and handle errors
  #
  # Use this when the rust function returns a Result<>.  error_module must be the error_module that corresponds to that Result.


  # Note: RustCallStatus.new zeroes out the struct, which is exactly what we
  # want to pass to Rust (code=0, error_buf=RustBuffer(len=0, capacity=0,
  # data=NULL))
  status = RustCallStatus.new
  args << status

  result = UniFFILib.public_send(fn_name, *args)

  case status.code
  when CALL_SUCCESS
    result
  when CALL_ERROR
    if error_module.nil?
      status.error_buf.free
      raise InternalError, "CALL_ERROR with no error_module set"
    else
      raise consume_buffer_into_error(error_module, status.error_buf)
    end
  when CALL_PANIC
    # When the rust code sees a panic, it tries to construct a RustBuffer
    # with the message.  But if that code panics, then it just sends back
    # an empty buffer.
    if status.error_buf.len > 0
      raise InternalError, status.error_buf.consumeIntoString()
    else
      raise InternalError, "Rust panic"
    end
  else
    raise InternalError, "Unknown call status: #{status.code}"
  end
end

private_class_method :consume_buffer_into_error

  # This is how we find and load the dynamic library provided by the component.
# For now we just look it up by name.
module UniFFILib
  extend FFI::Library

  
  ffi_lib 'proofmode'
  

  attach_function :uniffi_proofmode_fn_clone_mobilecallbacks,
    [:pointer, RustCallStatus.by_ref],
    :pointer
  attach_function :uniffi_proofmode_fn_free_mobilecallbacks,
    [:pointer, RustCallStatus.by_ref],
    :void
  attach_function :uniffi_proofmode_fn_init_callback_vtable_mobilecallbacks,
    [:pointer, RustCallStatus.by_ref],
    :void
  attach_function :uniffi_proofmode_fn_method_mobilecallbacks_get_location,
    [:pointer, RustCallStatus.by_ref],
    RustBuffer.by_value
  attach_function :uniffi_proofmode_fn_method_mobilecallbacks_get_device_info,
    [:pointer, RustCallStatus.by_ref],
    RustBuffer.by_value
  attach_function :uniffi_proofmode_fn_method_mobilecallbacks_get_network_info,
    [:pointer, RustCallStatus.by_ref],
    RustBuffer.by_value
  attach_function :uniffi_proofmode_fn_method_mobilecallbacks_save_data,
    [:pointer, RustBuffer.by_value, RustBuffer.by_value, RustBuffer.by_value, RustCallStatus.by_ref],
    :int8
  attach_function :uniffi_proofmode_fn_method_mobilecallbacks_save_text,
    [:pointer, RustBuffer.by_value, RustBuffer.by_value, RustBuffer.by_value, RustCallStatus.by_ref],
    :int8
  attach_function :uniffi_proofmode_fn_method_mobilecallbacks_sign_data,
    [:pointer, RustBuffer.by_value, RustCallStatus.by_ref],
    RustBuffer.by_value
  attach_function :uniffi_proofmode_fn_method_mobilecallbacks_report_progress,
    [:pointer, RustBuffer.by_value, RustCallStatus.by_ref],
    :void
  attach_function :uniffi_proofmode_fn_func_check_files_mobile,
    [RustBuffer.by_value, :pointer, RustCallStatus.by_ref],
    :uint64
  attach_function :uniffi_proofmode_fn_func_generate_proof_mobile,
    [RustBuffer.by_value, RustBuffer.by_value, :pointer, RustCallStatus.by_ref],
    :uint64
  attach_function :uniffi_proofmode_fn_func_get_file_hash,
    [RustBuffer.by_value, RustCallStatus.by_ref],
    RustBuffer.by_value
  attach_function :uniffi_proofmode_fn_func_get_version,
    [RustCallStatus.by_ref],
    RustBuffer.by_value
  attach_function :ffi_proofmode_rustbuffer_alloc,
    [:uint64, RustCallStatus.by_ref],
    RustBuffer.by_value
  attach_function :ffi_proofmode_rustbuffer_from_bytes,
    [ForeignBytes, RustCallStatus.by_ref],
    RustBuffer.by_value
  attach_function :ffi_proofmode_rustbuffer_free,
    [RustBuffer.by_value, RustCallStatus.by_ref],
    :void
  attach_function :ffi_proofmode_rustbuffer_reserve,
    [RustBuffer.by_value, :uint64, RustCallStatus.by_ref],
    RustBuffer.by_value
  attach_function :uniffi_proofmode_checksum_func_check_files_mobile,
    [RustCallStatus.by_ref],
    :uint16
  attach_function :uniffi_proofmode_checksum_func_generate_proof_mobile,
    [RustCallStatus.by_ref],
    :uint16
  attach_function :uniffi_proofmode_checksum_func_get_file_hash,
    [RustCallStatus.by_ref],
    :uint16
  attach_function :uniffi_proofmode_checksum_func_get_version,
    [RustCallStatus.by_ref],
    :uint16
  attach_function :uniffi_proofmode_checksum_method_mobilecallbacks_get_location,
    [RustCallStatus.by_ref],
    :uint16
  attach_function :uniffi_proofmode_checksum_method_mobilecallbacks_get_device_info,
    [RustCallStatus.by_ref],
    :uint16
  attach_function :uniffi_proofmode_checksum_method_mobilecallbacks_get_network_info,
    [RustCallStatus.by_ref],
    :uint16
  attach_function :uniffi_proofmode_checksum_method_mobilecallbacks_save_data,
    [RustCallStatus.by_ref],
    :uint16
  attach_function :uniffi_proofmode_checksum_method_mobilecallbacks_save_text,
    [RustCallStatus.by_ref],
    :uint16
  attach_function :uniffi_proofmode_checksum_method_mobilecallbacks_sign_data,
    [RustCallStatus.by_ref],
    :uint16
  attach_function :uniffi_proofmode_checksum_method_mobilecallbacks_report_progress,
    [RustCallStatus.by_ref],
    :uint16
  attach_function :ffi_proofmode_uniffi_contract_version,
    [RustCallStatus.by_ref],
    :uint32
  
end

  # Public interface members begin here.

  
  
  # Record type DeviceInfo
class DeviceInfo
  attr_reader :manufacturer, :model, :os_version, :device_id, :imei

  def initialize(manufacturer:, model:, os_version:, device_id:, imei:)
    @manufacturer = manufacturer
    @model = model
    @os_version = os_version
    @device_id = device_id
    @imei = imei
  end

  def ==(other)
    if @manufacturer != other.manufacturer
      return false
    end
    if @model != other.model
      return false
    end
    if @os_version != other.os_version
      return false
    end
    if @device_id != other.device_id
      return false
    end
    if @imei != other.imei
      return false
    end

    true
  end
end
  
  # Record type LocationInfo
class LocationInfo
  attr_reader :latitude, :longitude, :altitude, :accuracy, :timestamp

  def initialize(latitude:, longitude:, altitude:, accuracy:, timestamp:)
    @latitude = latitude
    @longitude = longitude
    @altitude = altitude
    @accuracy = accuracy
    @timestamp = timestamp
  end

  def ==(other)
    if @latitude != other.latitude
      return false
    end
    if @longitude != other.longitude
      return false
    end
    if @altitude != other.altitude
      return false
    end
    if @accuracy != other.accuracy
      return false
    end
    if @timestamp != other.timestamp
      return false
    end

    true
  end
end
  
  # Record type NetworkInfo
class NetworkInfo
  attr_reader :network_type, :carrier, :cell_tower_id, :wifi_ssid

  def initialize(network_type:, carrier:, cell_tower_id:, wifi_ssid:)
    @network_type = network_type
    @carrier = carrier
    @cell_tower_id = cell_tower_id
    @wifi_ssid = wifi_ssid
  end

  def ==(other)
    if @network_type != other.network_type
      return false
    end
    if @carrier != other.carrier
      return false
    end
    if @cell_tower_id != other.cell_tower_id
      return false
    end
    if @wifi_ssid != other.wifi_ssid
      return false
    end

    true
  end
end
  

  
  

def self.check_files_mobile(file_paths, progress_callback)
    file_paths = file_paths.map { |v| Proofmode::uniffi_utf8(v) }
    RustBuffer.check_lower_Sequencestring(file_paths)
    
    progress_callback = progress_callback
    (MobileCallbacks.uniffi_check_lower progress_callback)
    
  result = Proofmode.rust_call_with_error(ProofModeError,:uniffi_proofmode_fn_func_check_files_mobile,RustBuffer.alloc_from_Sequencestring(file_paths),(MobileCallbacks.uniffi_lower progress_callback))
  return result.consumeIntoString
end


  
  

def self.generate_proof_mobile(media_data, metadata, callbacks)
    media_data = Proofmode::uniffi_bytes(media_data)
    
    
    metadata = metadata.each.with_object({}) { |(k, v), res| res[Proofmode::uniffi_utf8(k)] = Proofmode::uniffi_utf8(v) }
    RustBuffer.check_lower_MapStringString(metadata)
    
    callbacks = callbacks
    (MobileCallbacks.uniffi_check_lower callbacks)
    
  result = Proofmode.rust_call_with_error(ProofModeError,:uniffi_proofmode_fn_func_generate_proof_mobile,RustBuffer.allocFromBytes(media_data),RustBuffer.alloc_from_MapStringString(metadata),(MobileCallbacks.uniffi_lower callbacks))
  return result.consumeIntoString
end


  
  

def self.get_file_hash(media_data)
    media_data = Proofmode::uniffi_bytes(media_data)
    
    
  result = Proofmode.rust_call(:uniffi_proofmode_fn_func_get_file_hash,RustBuffer.allocFromBytes(media_data))
  return result.consumeIntoString
end


  
  

def self.get_version()
  result = Proofmode.rust_call(:uniffi_proofmode_fn_func_get_version,)
  return result.consumeIntoString
end


  

  
  class MobileCallbacks

  # A private helper for initializing instances of the class from a raw pointer,
  # bypassing any initialization logic and ensuring they are GC'd properly.
  def self.uniffi_allocate(pointer)
    pointer.autorelease = false
    inst = allocate
    inst.instance_variable_set :@pointer, pointer
    ObjectSpace.define_finalizer(inst, uniffi_define_finalizer_by_pointer(pointer, inst.object_id))
    return inst
  end

  # A private helper for registering an object finalizer.
  # N.B. it's important that this does not capture a reference
  # to the actual instance, only its underlying pointer.
  def self.uniffi_define_finalizer_by_pointer(pointer, object_id)
    Proc.new do |_id|
      Proofmode.rust_call(
        :uniffi_proofmode_fn_free_mobilecallbacks,
        pointer
      )
    end
  end

  # A private helper for lowering instances into a raw pointer.
  # This does an explicit typecheck, because accidentally lowering a different type of
  # object in a place where this type is expected, could lead to memory unsafety.
  def self.uniffi_check_lower(inst)
    if not inst.is_a? self
      raise TypeError.new "Expected a MobileCallbacks instance, got #{inst}"
    end
  end

  def uniffi_clone_pointer()
    return Proofmode.rust_call(
      :uniffi_proofmode_fn_clone_mobilecallbacks,
      @pointer
    )
  end

  def self.uniffi_lower(inst)
    return inst.uniffi_clone_pointer()
  end

  

  def get_location()
    result = Proofmode.rust_call(:uniffi_proofmode_fn_method_mobilecallbacks_get_location,uniffi_clone_pointer(),)
    return result.consumeIntoOptionalTypeLocationInfo
  end
  def get_device_info()
    result = Proofmode.rust_call(:uniffi_proofmode_fn_method_mobilecallbacks_get_device_info,uniffi_clone_pointer(),)
    return result.consumeIntoOptionalTypeDeviceInfo
  end
  def get_network_info()
    result = Proofmode.rust_call(:uniffi_proofmode_fn_method_mobilecallbacks_get_network_info,uniffi_clone_pointer(),)
    return result.consumeIntoOptionalTypeNetworkInfo
  end
  def save_data(hash, filename, data)
        hash = Proofmode::uniffi_utf8(hash)
        
        filename = Proofmode::uniffi_utf8(filename)
        
        data = Proofmode::uniffi_bytes(data)
        
    result = Proofmode.rust_call(:uniffi_proofmode_fn_method_mobilecallbacks_save_data,uniffi_clone_pointer(),RustBuffer.allocFromString(hash),RustBuffer.allocFromString(filename),RustBuffer.allocFromBytes(data))
    return 1 == result
  end
  def save_text(hash, filename, text)
        hash = Proofmode::uniffi_utf8(hash)
        
        filename = Proofmode::uniffi_utf8(filename)
        
        text = Proofmode::uniffi_utf8(text)
        
    result = Proofmode.rust_call(:uniffi_proofmode_fn_method_mobilecallbacks_save_text,uniffi_clone_pointer(),RustBuffer.allocFromString(hash),RustBuffer.allocFromString(filename),RustBuffer.allocFromString(text))
    return 1 == result
  end
  def sign_data(data)
        data = Proofmode::uniffi_bytes(data)
        
    result = Proofmode.rust_call(:uniffi_proofmode_fn_method_mobilecallbacks_sign_data,uniffi_clone_pointer(),RustBuffer.allocFromBytes(data))
    return result.consumeIntoOptionalbytes
  end
  def report_progress(message)
        message = Proofmode::uniffi_utf8(message)
        
      Proofmode.rust_call(:uniffi_proofmode_fn_method_mobilecallbacks_report_progress,uniffi_clone_pointer(),RustBuffer.allocFromString(message))
  end
  
  
end
  
end