Class: Sigma::Constant

Inherits:
Object
  • Object
show all
Extended by:
FFI::Library
Defined in:
lib/sigma/constant.rb

Overview

Ergo constant (evaluated) values

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#pointerObject

Returns the value of attribute pointer.



22
23
24
# File 'lib/sigma/constant.rb', line 22

def pointer
  @pointer
end

Class Method Details

.with_base_16(str) ⇒ Object



82
83
84
85
86
87
88
# File 'lib/sigma/constant.rb', line 82

def self.with_base_16(str)
  c_ptr = FFI::MemoryPointer.new(:pointer)
  error = ergo_lib_constant_from_base16(str, c_ptr)
  Util.check_error!(error)

  init(c_ptr)
end

.with_bytes(bytes) ⇒ Constant

Create from byte array

Parameters:

  • bytes (Array<uint8>)

    Array of unsigned 8-bit (0-255) integers

Returns:



37
38
39
40
41
42
43
44
45
# File 'lib/sigma/constant.rb', line 37

def self.with_bytes(bytes)
  c_ptr = FFI::MemoryPointer.new(:pointer)
  b_ptr = FFI::MemoryPointer.new(:uint8, bytes.size)
  b_ptr.write_array_of_uint8(bytes)
  error = ergo_lib_constant_from_bytes(b_ptr, bytes.size, c_ptr)
  Util.check_error!(error)

  init(c_ptr)
end

.with_ecpoint_bytes(bytes) ⇒ Constant

Parse raw EcPoint value from bytes and make ProveDlog constant

Parameters:

  • bytes (Array<uint8>)

    Array of unsigned 8-bit (0-255) integers

Returns:



50
51
52
53
54
55
56
57
58
# File 'lib/sigma/constant.rb', line 50

def self.with_ecpoint_bytes(bytes)
  c_ptr = FFI::MemoryPointer.new(:pointer)
  b_ptr = FFI::MemoryPointer.new(:uint8, bytes.size)
  b_ptr.write_array_of_uint8(bytes)
  error = ergo_lib_constant_from_ecpoint_bytes(b_ptr, bytes.size, c_ptr)
  Util.check_error!(error)

  init(c_ptr)
end

.with_ergo_box(ergo_box) ⇒ Constant

Create from ergo_box value

Parameters:

Returns:



27
28
29
30
31
32
# File 'lib/sigma/constant.rb', line 27

def self.with_ergo_box(ergo_box)
  c_ptr = FFI::MemoryPointer.new(:pointer)
  ergo_lib_constant_from_ergo_box(ergo_box.pointer, c_ptr)
  
  init(c_ptr)
end

.with_i32(int) ⇒ Constant

Create from 32-bit integer

Parameters:

  • int (Integer)

Returns:



63
64
65
66
67
68
69
# File 'lib/sigma/constant.rb', line 63

def self.with_i32(int)
  pointer = FFI::MemoryPointer.new(:pointer)
  error = ergo_lib_constant_from_i32(int, pointer)
  # TODO: This raises error even with valid output
  #Util.check_error!(error)
  init(pointer)
end

.with_i64(int) ⇒ Constant

Create from 64-bit integer

Parameters:

  • int (Integer)

Returns:



74
75
76
77
78
79
80
# File 'lib/sigma/constant.rb', line 74

def self.with_i64(int)
  pointer = FFI::MemoryPointer.new(:pointer)
  error = ergo_lib_constant_from_i64(int, pointer)
  # TODO: This raises error even with valid output
  #Util.check_error!(error)
  init(pointer)
end

.with_raw_pointer(constant_pointer) ⇒ Constant

Note:

A user of sigma_rb generally does not need to call this function

Takes ownership of an existing Constant Pointer.

Parameters:

  • pointer (FFI::MemoryPointer)

Returns:



94
95
96
# File 'lib/sigma/constant.rb', line 94

def self.with_raw_pointer(constant_pointer)
  init(constant_pointer)
end

Instance Method Details

#==(constant_two) ⇒ bool

Equality check for two Constants

Parameters:

Returns:

  • (bool)


129
130
131
# File 'lib/sigma/constant.rb', line 129

def ==(constant_two)
  ergo_lib_constant_eq(self.pointer, constant_two.pointer)
end

#to_base16_stringString

Encode as Base16-encoded ErgoTree serialized value or throw an error if serialization failed

Returns:

  • (String)

    Base-16 encoded ErgoTree serialized value



100
101
102
103
104
105
106
107
108
# File 'lib/sigma/constant.rb', line 100

def to_base16_string
  s_ptr = FFI::MemoryPointer.new(:pointer, 1)
  error = ergo_lib_constant_to_base16(self.pointer, s_ptr)
  Util.check_error!(error)
  s_ptr = s_ptr.read_pointer()
  str = s_ptr.read_string().force_encoding('UTF-8')
  Util.ergo_lib_delete_string(s_ptr)
  str
end

#to_i32Integer

Extract 32-bit integer value, throw error if wrong type

Returns:

  • (Integer)


112
113
114
115
116
# File 'lib/sigma/constant.rb', line 112

def to_i32
  res = ergo_lib_constant_to_i32(self.pointer)
  Util.check_error!(res[:error])
  res[:value]
end

#to_i64Integer

Extract 64-bit integer value, throw error if wrong type

Returns:

  • (Integer)


120
121
122
123
124
# File 'lib/sigma/constant.rb', line 120

def to_i64
  res = ergo_lib_constant_to_i64(self.pointer)
  Util.check_error!(res[:error])
  res[:value]
end