Class: AddressCodec::XrpCodec

Inherits:
Codec
  • Object
show all
Defined in:
lib/address-codec/xrp_codec.rb

Direct Known Subclasses

AddressCodec

Constant Summary collapse

ACCOUNT_ID =

base58 encodings: xrpl.org/base58-encodings.html

0
ACCOUNT_PUBLIC_KEY =

Account address (20 bytes)

0x23
FAMILY_SEED =

Account public key (33 bytes)

0x21
NODE_PUBLIC =

33; Seed value (for secret keys) (16 bytes)

0x1c
ED25519_SEED =

28; Validation public key (33 bytes)

[0x01, 0xe1, 0x4b].freeze

Instance Method Summary collapse

Methods inherited from Codec

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

Constructor Details

This class inherits a constructor from AddressCodec::Codec

Instance Method Details

#decode_account_id(account_id) ⇒ Array<Integer>

Decodes an account ID string into its underlying bytes.

Parameters:

  • account_id (String)

    The account ID string to decode.

Returns:

  • (Array<Integer>)

    The decoded bytes.



55
56
57
58
# File 'lib/address-codec/xrp_codec.rb', line 55

def ()
  opts = { versions: [], expected_length: 20 }
  decode(, opts)[:bytes]
end

#decode_account_public(base58string) ⇒ Array<Integer>

Decodes an account public key string into its underlying bytes.

Parameters:

  • base58string (String)

    The account public key string to decode.

Returns:

  • (Array<Integer>)

    The decoded bytes.



87
88
89
90
# File 'lib/address-codec/xrp_codec.rb', line 87

def (base58string)
  opts = { versions: [], expected_length: 33 }
  decode(base58string, opts)[:bytes]
end

#decode_node_public(base58string) ⇒ Array<Integer>

Decodes a node public key string into its underlying bytes.

Parameters:

  • base58string (String)

    The node public key string to decode.

Returns:

  • (Array<Integer>)

    The decoded bytes.



63
64
65
66
# File 'lib/address-codec/xrp_codec.rb', line 63

def decode_node_public(base58string)
  opts = { versions: [NODE_PUBLIC], expected_length: 33 }
  decode(base58string, opts)[:bytes]
end

#decode_seed(seed, opts = { version_types: ['ed25519', 'secp256k1'], versions: [ED25519_SEED, FAMILY_SEED], expected_length: 16 }) ⇒ Hash

Decodes a seed string into its underlying bytes and type.

Parameters:

  • seed (String)

    The seed string to decode.

  • opts (Hash) (defaults to: { version_types: ['ed25519', 'secp256k1'], versions: [ED25519_SEED, FAMILY_SEED], expected_length: 16 })

    Options for decoding (e.g., :versions, :version_types, :expected_length).

Returns:

  • (Hash)

    The decoded data including version, bytes, and type.



36
37
38
39
40
41
42
# File 'lib/address-codec/xrp_codec.rb', line 36

def decode_seed(seed, opts = {
      version_types: ['ed25519', 'secp256k1'],
      versions: [ED25519_SEED, FAMILY_SEED],
      expected_length: 16
    })
  decode(seed, opts)
end

#encode_account_id(bytes) ⇒ String

Encodes a byte array into an account ID string.

Parameters:

  • bytes (Array<Integer>)

    20 bytes for the account ID.

Returns:

  • (String)

    The encoded account ID string.



47
48
49
50
# File 'lib/address-codec/xrp_codec.rb', line 47

def (bytes)
  opts = { versions: [], expected_length: 20 }
  encode(bytes, opts)
end

#encode_account_public(bytes) ⇒ String

Encodes a byte array into an account public key string.

Parameters:

  • bytes (Array<Integer>)

    33 bytes for the account public key.

Returns:

  • (String)

    The encoded account public key string.



79
80
81
82
# File 'lib/address-codec/xrp_codec.rb', line 79

def (bytes)
  opts = { versions: [], expected_length: 33 }
  encode(bytes, opts)
end

#encode_node_public(bytes) ⇒ String

Encodes a byte array into a node public key string.

Parameters:

  • bytes (Array<Integer>)

    33 bytes for the node public key.

Returns:

  • (String)

    The encoded node public key string.



71
72
73
74
# File 'lib/address-codec/xrp_codec.rb', line 71

def encode_node_public(bytes)
  opts = { versions: [NODE_PUBLIC], expected_length: 33 }
  encode(bytes, opts)
end

#encode_seed(entropy, type = nil) ⇒ String

Encodes entropy into a seed string.

Parameters:

  • entropy (Array<Integer>)

    16 bytes of entropy.

  • type (String, nil) (defaults to: nil)

    The seed type (‘ed25519’ or ‘secp256k1’).

Returns:

  • (String)

    The encoded seed string.



18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/address-codec/xrp_codec.rb', line 18

def encode_seed(entropy, type = nil)
  unless check_byte_length(entropy, 16)
    raise 'entropy must have length 16'
  end

  opts = {
    expected_length: 16,
    versions: type == 'ed25519' ? ED25519_SEED : [FAMILY_SEED]
  }

  # prefixes entropy with version bytes
  encode(entropy, opts)
end

#valid_classic_address?(address) ⇒ Boolean

Checks if a string is a valid classic XRPL address.

Parameters:

  • address (String)

    The address string to check.

Returns:

  • (Boolean)

    True if the address is valid, false otherwise.



95
96
97
98
99
100
101
102
# File 'lib/address-codec/xrp_codec.rb', line 95

def valid_classic_address?(address)
  begin
    (address)
  rescue
    return false
  end
  true
end