xrpl-ruby 0.0.3 → 0.2.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/lib/address-codec/address_codec.rb +1 -0
- data/lib/address-codec/codec.rb +1 -1
- data/lib/binary-codec/binary_codec.rb +38 -0
- data/lib/binary-codec/enums/constants.rb +8 -0
- data/lib/binary-codec/enums/definitions.json +3183 -0
- data/lib/binary-codec/enums/definitions.rb +78 -0
- data/lib/binary-codec/enums/fields.rb +102 -0
- data/lib/binary-codec/serdes/binary_parser.rb +145 -0
- data/lib/binary-codec/serdes/binary_serializer.rb +82 -0
- data/lib/binary-codec/serdes/bytes_list.rb +36 -0
- data/lib/binary-codec/types/account_id.rb +79 -0
- data/lib/binary-codec/types/amount.rb +284 -0
- data/lib/binary-codec/types/blob.rb +32 -0
- data/lib/binary-codec/types/currency.rb +105 -0
- data/lib/binary-codec/types/hash.rb +105 -0
- data/lib/binary-codec/types/serialized_type.rb +133 -0
- data/lib/binary-codec/types/st_object.rb +60 -0
- data/lib/binary-codec/types/uint.rb +53 -0
- data/lib/binary-codec/utilities.rb +80 -0
- data/lib/core/core.rb +38 -3
- data/lib/xrpl-ruby.rb +8 -1
- metadata +20 -3
@@ -0,0 +1,53 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module BinaryCodec
|
4
|
+
|
5
|
+
class Uint < ComparableSerializedType
|
6
|
+
class << self
|
7
|
+
attr_reader :width
|
8
|
+
end
|
9
|
+
|
10
|
+
def initialize(byte_buf = nil)
|
11
|
+
@bytes = byte_buf || Array.new(self.class.width, 0)
|
12
|
+
end
|
13
|
+
|
14
|
+
def self.from(value)
|
15
|
+
return value if value.is_a?(self)
|
16
|
+
|
17
|
+
if value.is_a?(String)
|
18
|
+
return new(int_to_bytes(value.to_i, width))
|
19
|
+
end
|
20
|
+
|
21
|
+
if value.is_a?(Integer)
|
22
|
+
return new(int_to_bytes(value, width))
|
23
|
+
end
|
24
|
+
|
25
|
+
raise StandardError, "Cannot construct #{self} from the value given"
|
26
|
+
end
|
27
|
+
|
28
|
+
def value_of
|
29
|
+
@bytes.reduce(0) { |acc, byte| (acc << 8) + byte }
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
class Uint8 < Uint
|
34
|
+
# Uint8 is a 1-byte unsigned integer
|
35
|
+
@width = 1
|
36
|
+
end
|
37
|
+
|
38
|
+
class Uint16 < Uint
|
39
|
+
# Uint16 is a 2-byte unsigned integer
|
40
|
+
@width = 2
|
41
|
+
end
|
42
|
+
|
43
|
+
class Uint32 < Uint
|
44
|
+
# Uint32 is a 4-byte unsigned integer
|
45
|
+
@width = 4
|
46
|
+
end
|
47
|
+
|
48
|
+
class Uint64 < Uint
|
49
|
+
# Uint64 is an 8-byte unsigned integer
|
50
|
+
@width = 8
|
51
|
+
end
|
52
|
+
|
53
|
+
end
|
@@ -0,0 +1,80 @@
|
|
1
|
+
module BinaryCodec
|
2
|
+
|
3
|
+
# Write an 8-bit unsigned integer
|
4
|
+
def self.write_uint8(array, value, offset = 0)
|
5
|
+
array[offset] = value & 0xFF
|
6
|
+
end
|
7
|
+
|
8
|
+
# Read an unsigned 16-bit integer in big-endian format
|
9
|
+
def self.read_uint16be(array, offset = 0)
|
10
|
+
(array[offset] << 8) + array[offset + 1]
|
11
|
+
end
|
12
|
+
|
13
|
+
# Write a 16-bit unsigned integer in big-endian format
|
14
|
+
def self.write_uint16be(array, value, offset = 0)
|
15
|
+
array[offset] = (value >> 8) & 0xFF
|
16
|
+
array[offset + 1] = value & 0xFF
|
17
|
+
end
|
18
|
+
|
19
|
+
# Read an unsigned 32-bit integer in big-endian format
|
20
|
+
def self.read_uint32be(array, offset = 0)
|
21
|
+
(array[offset] << 24) + (array[offset + 1] << 16) +
|
22
|
+
(array[offset + 2] << 8) + array[offset + 3]
|
23
|
+
end
|
24
|
+
|
25
|
+
# Write an unsigned 32-bit integer to a buffer in big-endian format
|
26
|
+
def self.write_uint32be(buffer, value, offset = 0)
|
27
|
+
buffer[offset] = (value >> 24) & 0xFF
|
28
|
+
buffer[offset + 1] = (value >> 16) & 0xFF
|
29
|
+
buffer[offset + 2] = (value >> 8) & 0xFF
|
30
|
+
buffer[offset + 3] = value & 0xFF
|
31
|
+
end
|
32
|
+
|
33
|
+
# Compare two byte arrays
|
34
|
+
def self.equal(array1, array2)
|
35
|
+
return false unless array1.length == array2.length
|
36
|
+
array1 == array2
|
37
|
+
end
|
38
|
+
|
39
|
+
# Compare two arrays of any type
|
40
|
+
def self.compare(array1, array2)
|
41
|
+
raise 'Cannot compare arrays of different length' if array1.length != array2.length
|
42
|
+
|
43
|
+
array1.each_with_index do |value, i|
|
44
|
+
return 1 if value > array2[i]
|
45
|
+
return -1 if value < array2[i]
|
46
|
+
end
|
47
|
+
|
48
|
+
0
|
49
|
+
end
|
50
|
+
|
51
|
+
# Compares two 8-bit aligned arrays
|
52
|
+
def self.compare8(array1, array2)
|
53
|
+
compare(array1, array2)
|
54
|
+
end
|
55
|
+
|
56
|
+
# Compares two 16-bit aligned arrays
|
57
|
+
def self.compare16(array1, array2)
|
58
|
+
raise 'Array lengths must be even for 16-bit alignment' unless (array1.length % 2).zero? && (array2.length % 2).zero?
|
59
|
+
|
60
|
+
array1.pack('C*').unpack('n*') <=> array2.pack('C*').unpack('n*')
|
61
|
+
end
|
62
|
+
|
63
|
+
# Compares two 32-bit aligned arrays
|
64
|
+
def self.compare32(array1, array2)
|
65
|
+
raise 'Array lengths must be divisible by 4 for 32-bit alignment' unless (array1.length % 4).zero? && (array2.length % 4).zero?
|
66
|
+
|
67
|
+
array1.pack('C*').unpack('N*') <=> array2.pack('C*').unpack('N*')
|
68
|
+
end
|
69
|
+
|
70
|
+
# Determine if an array is 16-bit aligned
|
71
|
+
def self.aligned16?(array)
|
72
|
+
(array.length % 2).zero?
|
73
|
+
end
|
74
|
+
|
75
|
+
# Determine if an array is 32-bit aligned
|
76
|
+
def self.aligned32?(array)
|
77
|
+
(array.length % 4).zero?
|
78
|
+
end
|
79
|
+
|
80
|
+
end
|
data/lib/core/core.rb
CHANGED
@@ -1,21 +1,40 @@
|
|
1
1
|
# @!attribute
|
2
2
|
require_relative 'base_x'
|
3
3
|
require_relative 'base_58_xrp'
|
4
|
+
require 'securerandom'
|
5
|
+
|
6
|
+
def random_bytes(size)
|
7
|
+
SecureRandom.random_bytes(size).bytes
|
8
|
+
end
|
4
9
|
|
5
10
|
def bytes_to_hex(bytes)
|
6
11
|
bytes.pack('C*').unpack1('H*').upcase
|
7
12
|
end
|
8
|
-
|
9
13
|
def hex_to_bytes(hex)
|
14
|
+
raise ArgumentError, 'Invalid hex string' unless valid_hex?(hex)
|
10
15
|
[hex].pack('H*').bytes
|
11
16
|
end
|
12
17
|
|
18
|
+
def bin_to_hex(bin)
|
19
|
+
bin.unpack("H*").first.upcase
|
20
|
+
end
|
21
|
+
|
13
22
|
def hex_to_bin(hex)
|
23
|
+
raise ArgumentError, 'Invalid hex string' unless valid_hex?(hex)
|
14
24
|
[hex].pack("H*")
|
15
25
|
end
|
16
26
|
|
17
|
-
def
|
18
|
-
|
27
|
+
def hex_to_string(hex, encoding = 'utf-8')
|
28
|
+
raise ArgumentError, 'Invalid hex string' unless valid_hex?(hex)
|
29
|
+
hex_to_bin(hex).force_encoding(encoding).encode('utf-8')
|
30
|
+
end
|
31
|
+
|
32
|
+
def string_to_hex(string)
|
33
|
+
string.unpack1('H*').upcase
|
34
|
+
end
|
35
|
+
|
36
|
+
def valid_hex?(str)
|
37
|
+
str =~ /\A[0-9a-fA-F]*\z/ && str.length.even?
|
19
38
|
end
|
20
39
|
|
21
40
|
def check_byte_length(bytes, expected_length)
|
@@ -34,4 +53,20 @@ end
|
|
34
53
|
|
35
54
|
def is_scalar?(val)
|
36
55
|
val.is_a?(Numeric)
|
56
|
+
end
|
57
|
+
|
58
|
+
def int_to_bytes(number, width = 1, byteorder = :big)
|
59
|
+
bytes = []
|
60
|
+
while number > 0
|
61
|
+
bytes << (number & 0xFF) # Extract the lowest 8 bits (1 byte)
|
62
|
+
number >>= 8 # Shift the number 8 bits to the right
|
63
|
+
end
|
64
|
+
|
65
|
+
# Ensure the result has at least `width` bytes (pad with zeroes if necessary)
|
66
|
+
while bytes.size < width
|
67
|
+
bytes << 0
|
68
|
+
end
|
69
|
+
|
70
|
+
bytes.reverse! if byteorder == :big
|
71
|
+
bytes
|
37
72
|
end
|
data/lib/xrpl-ruby.rb
CHANGED
@@ -4,4 +4,11 @@ require_relative 'core/core'
|
|
4
4
|
|
5
5
|
require_relative 'address-codec/codec'
|
6
6
|
require_relative 'address-codec/xrp_codec'
|
7
|
-
require_relative 'address-codec/address_codec'
|
7
|
+
require_relative 'address-codec/address_codec'
|
8
|
+
|
9
|
+
require_relative 'binary-codec/enums/fields'
|
10
|
+
require_relative 'binary-codec/enums/definitions'
|
11
|
+
require_relative 'binary-codec/serdes/binary_parser'
|
12
|
+
require_relative 'binary-codec/serdes/bytes_list'
|
13
|
+
require_relative 'binary-codec/types/serialized_type'
|
14
|
+
require_relative 'binary-codec/types/hash'
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: xrpl-ruby
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.4
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Alexander Busse
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2025-05-20 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -64,6 +64,23 @@ files:
|
|
64
64
|
- lib/address-codec/address_codec.rb
|
65
65
|
- lib/address-codec/codec.rb
|
66
66
|
- lib/address-codec/xrp_codec.rb
|
67
|
+
- lib/binary-codec/binary_codec.rb
|
68
|
+
- lib/binary-codec/enums/constants.rb
|
69
|
+
- lib/binary-codec/enums/definitions.json
|
70
|
+
- lib/binary-codec/enums/definitions.rb
|
71
|
+
- lib/binary-codec/enums/fields.rb
|
72
|
+
- lib/binary-codec/serdes/binary_parser.rb
|
73
|
+
- lib/binary-codec/serdes/binary_serializer.rb
|
74
|
+
- lib/binary-codec/serdes/bytes_list.rb
|
75
|
+
- lib/binary-codec/types/account_id.rb
|
76
|
+
- lib/binary-codec/types/amount.rb
|
77
|
+
- lib/binary-codec/types/blob.rb
|
78
|
+
- lib/binary-codec/types/currency.rb
|
79
|
+
- lib/binary-codec/types/hash.rb
|
80
|
+
- lib/binary-codec/types/serialized_type.rb
|
81
|
+
- lib/binary-codec/types/st_object.rb
|
82
|
+
- lib/binary-codec/types/uint.rb
|
83
|
+
- lib/binary-codec/utilities.rb
|
67
84
|
- lib/core/base_58_xrp.rb
|
68
85
|
- lib/core/base_x.rb
|
69
86
|
- lib/core/core.rb
|
@@ -87,7 +104,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
87
104
|
- !ruby/object:Gem::Version
|
88
105
|
version: '0'
|
89
106
|
requirements: []
|
90
|
-
rubygems_version: 3.
|
107
|
+
rubygems_version: 3.4.19
|
91
108
|
signing_key:
|
92
109
|
specification_version: 4
|
93
110
|
summary: A Ruby library to interact with the XRP Ledger (XRPL) blockchain
|