Class: CLURef

Inherits:
CLValue show all
Includes:
CLValueBytesParsers::CLURefBytesParser
Defined in:
lib/types/cl_uref.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from CLValueBytesParsers::CLURefBytesParser

#decode_base_16, #encode_base_16, #from_bytes

Methods inherited from CLValue

#to_hash

Constructor Details

#initialize(value = nil, access_rights = nil) ⇒ CLURef

value is an uint8array

Parameters:

  • value (Array<Integer>) (defaults to: nil)
  • access_rights (AccessRights) (defaults to: nil)


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
# File 'lib/types/cl_uref.rb', line 17

def initialize(value = nil, access_rights = nil)
  super()
  # raise ArgumentError.new('The length of URefAddr should be 32') unless value.length == 32
  # rescue ArgumentError => e
  #   p e.message
  begin
    raise ArgumentError.new('The length of URefAddr should be 32') unless value.length == 32
  rescue ArgumentError => e  
    e.message
  end
  begin
    raise ArgumentError.new('Unsupported AccessRights') unless AccessRights.has_value?(access_rights)
  rescue ArgumentError => e  
    e.message
  end
  # if value.length != 32
  #   raise ArgumentError.new('The length of URefAddr should be 32')
  # end
  # raise ArgumentError.new("Unsupported AccessRights") unless AccessRights.has_value?(access_rights)
  # if value.length != 32
  #   begin
  #     raise Error.new('The length of URefAddr should be 32')
  #   rescue StandardError => e
  #     puts e
  #   end
  # end
  # if !(AccessRights.has_value?(access_rights))
  #   raise Error.new('Unsupported AccessRights')
  # end
  @value =  value
  @access_rights = access_rights

end

Instance Attribute Details

#errObject

Returns the value of attribute err.



13
14
15
# File 'lib/types/cl_uref.rb', line 13

def err
  @err
end

Class Method Details

.from_json(json) ⇒ Object



113
114
115
116
117
118
119
# File 'lib/types/cl_uref.rb', line 113

def self.from_json(json)
  parsed = JSON.parse(json).deep_symbolize_keys
  bytes = CLValueBytesParsers::CLURefBytesParser.decode_base_16(parsed[:bytes])
  decoded = bytes[0...-1]
  access_rights = bytes.pop
  CLURef.new(decoded, access_rights)
end

.parse_uref_address(str) ⇒ Object

Raises:

  • (ArgumentError)


68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/types/cl_uref.rb', line 68

def self.parse_uref_address(str)
  raise ArgumentError.new("Expected a string value of \'uref-\' ") unless str.start_with?("uref-")
  raise ArgumentError.new("Expected a value of 3") unless str[0..str.length-1].split('-', 3).size == 3
  raise ArgumentError.new("Expected a value of 32") unless str[0..str.length-1].split('-', 3)[1].length/2 == 32

  arr = str[0..str.length-1].split('-', 3)
  prefix = arr[0]
  uref_addr = arr[1]
  suffix = arr[2]
  uref_byte_length = uref_addr.length / 2
  decoded_addr = CLValueBytesParsers::CLURefBytesParser.decode_base_16(uref_addr)
  access_rights = suffix.to_i(8)
  # puts decoded_addr.inspect
  # puts access_rights
  raise ArgumentError.new("The value of \'access_rights\' is out of range. It must be >= 0 and <= 7. Received #{access_rights}") unless suffix.to_i(10).between?(0, 7)
  CLURef.new(decoded_addr, access_rights)
end

.to_json(uref) ⇒ Object



102
103
104
105
106
107
108
109
110
111
# File 'lib/types/cl_uref.rb', line 102

def self.to_json(uref)
  decoded_addr = uref.get_value
  # p decoded_addr
  access_rights = uref.get_access_rights
  decoded_addr_with_access_rights = CLValueBytesParsers::CLURefBytesParser.to_bytes(uref)
  # p decoded_addr_with_access_rights
  encoded_addr_with_access_rights = CLValueBytesParsers::CLURefBytesParser.encode_base_16(decoded_addr_with_access_rights)
  # p encoded_addr_with_access_rights
  json = {"bytes": encoded_addr_with_access_rights, "cl_type": uref.get_cl_type}.to_json
end

Instance Method Details

#get_access_rightsObject



60
61
62
# File 'lib/types/cl_uref.rb', line 60

def get_access_rights
  @access_rights
end

#get_cl_typeObject



51
52
53
54
# File 'lib/types/cl_uref.rb', line 51

def get_cl_type
  @cl_type = CLURefType.new
  @cl_type.to_string
end

#get_sizeObject



64
65
66
# File 'lib/types/cl_uref.rb', line 64

def get_size
  @value.size
end

#get_valueObject



56
57
58
# File 'lib/types/cl_uref.rb', line 56

def get_value
  @value 
end

#to_bytes(str) ⇒ Object

Example: str = “uref-000102030405060708090a0b0c0d0e0f000102030405060708090a0b0c0d0e0f-007”;

Raises:

  • (ArgumentError)


87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/types/cl_uref.rb', line 87

def to_bytes(str)
  raise ArgumentError.new("Expected a string value of \'uref-\' ") unless str.start_with?("uref-")
  raise ArgumentError.new("Expected a value of 3") unless str[0..str.length-1].split('-', 3).size == 3
  raise ArgumentError.new("Expected a value of 32") unless str[0..str.length-1].split('-', 3)[1].length/2 == 32

  arr = str[0..str.length-1].split('-', 3)
  prefix = arr[0]
  uref_addr = arr[1]
  suffix = arr[2]
  uref_byte_length = uref_addr.length / 2
  access_rights = suffix.to_i(8)
  raise ArgumentError.new("The value of \'access_rights\' is out of range. It must be >= 0 and <= 7. Received #{access_rights}") unless suffix.to_i(10).between?(0, 7)
  uref_addr + [access_rights].pack("C*").unpack1("H*")
end