Class: AddressCodec::AddressCodec

Inherits:
XrpCodec show all
Defined in:
lib/address-codec/address_codec.rb

Constant Summary collapse

PREFIX_BYTES =
{
  main: [0x05, 0x44],
  test: [0x04, 0x93]
}
MAX_32_BIT_UNSIGNED_INT =
4294967295

Constants inherited from XrpCodec

XrpCodec::ACCOUNT_ID, XrpCodec::ACCOUNT_PUBLIC_KEY, XrpCodec::ED25519_SEED, XrpCodec::FAMILY_SEED, XrpCodec::NODE_PUBLIC

Instance Method Summary collapse

Methods inherited from XrpCodec

#decode_account_id, #decode_account_public, #decode_node_public, #decode_seed, #encode_account_id, #encode_account_public, #encode_node_public, #encode_seed, #valid_classic_address?

Methods inherited from Codec

#decode, #decode_checked, #encode, #encode_checked, #initialize

Constructor Details

This class inherits a constructor from AddressCodec::Codec

Instance Method Details

#classic_address_to_x_address(classic_address, tag, test) ⇒ String

Converts a classic address to an X-address.

Parameters:

  • classic_address (String)

    The classic XRPL address to convert.

  • tag (Integer, false, nil)

    The destination tag.

  • test (Boolean)

    Whether the address is for a test network.

Returns:

  • (String)

    The encoded X-address.



19
20
21
22
# File 'lib/address-codec/address_codec.rb', line 19

def classic_address_to_x_address(classic_address, tag, test)
   = (classic_address)
  encode_x_address(, tag, test)
end

#decode_x_address(x_address) ⇒ Hash

Decodes an X-address into its underlying account ID, tag, and network type.

Parameters:

  • x_address (String)

    The X-address string to decode.

Returns:

  • (Hash)

    A hash containing :account_id, :tag, and :test.



78
79
80
81
82
83
84
85
86
87
88
# File 'lib/address-codec/address_codec.rb', line 78

def decode_x_address(x_address)
  decoded = decode_checked(x_address)
  test = is_uint8_array_for_test_address(decoded)
   = decoded[2, 20]
  tag = tag_from_uint8_array(decoded)
  {
    account_id: ,
    tag: tag,
    test: test
  }
end

#encode_x_address(account_id, tag, test) ⇒ String

Encodes an account ID and tag into an X-address.

Parameters:

  • account_id (Array<Integer>)

    20 bytes for the account ID.

  • tag (Integer, false, nil)

    The destination tag.

  • test (Boolean)

    Whether the address is for a test network.

Returns:

  • (String)

    The encoded X-address.



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
# File 'lib/address-codec/address_codec.rb', line 29

def encode_x_address(, tag, test)
  if .length != 20
    # RIPEMD160 -> 160 Bits = 20 Bytes
    raise 'Account ID must be 20 bytes'
  end
  if tag && tag != false && tag > MAX_32_BIT_UNSIGNED_INT
    raise 'Invalid tag'
  end
  the_tag = tag || 0
  flag = (tag == false || tag.nil?) ? 0 : 1

  bytes = concat_args(
    test ? PREFIX_BYTES[:test] : PREFIX_BYTES[:main],
    ,
    [
      flag,
      the_tag & 0xff,
      (the_tag >> 8) & 0xff,
      (the_tag >> 16) & 0xff,
      (the_tag >> 24) & 0xff,
      0,
      0,
      0,
      0
    ]
  )

  encode_checked(bytes)
end

#valid_x_address?(x_address) ⇒ Boolean

Checks if a string is a valid X-address.

Parameters:

  • x_address (String)

    The X-address string to check.

Returns:

  • (Boolean)

    True if the X-address is valid, false otherwise.



93
94
95
96
97
98
99
100
# File 'lib/address-codec/address_codec.rb', line 93

def valid_x_address?(x_address)
  begin
    decode_x_address(x_address)
  rescue
    return false
  end
  true
end

#x_address_to_classic_address(x_address) ⇒ Hash

Converts an X-address to a classic address.

Parameters:

  • x_address (String)

    The X-address string to convert.

Returns:

  • (Hash)

    A hash containing :classic_address, :tag, and :test.



62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/address-codec/address_codec.rb', line 62

def x_address_to_classic_address(x_address)
  decoded = decode_x_address(x_address)
   = decoded[:account_id]
  tag = decoded[:tag]
  test = decoded[:test]
  classic_address = ()
  {
    classic_address: classic_address,
    tag: tag,
    test: test
  }
end