Method: CLURef#to_bytes

Defined in:
lib/types/cl_uref.rb

#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