Class: AddressCodec::AddressCodec
- 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
-
#classic_address_to_x_address(classic_address, tag, test) ⇒ String
Converts a classic address to an X-address.
-
#decode_x_address(x_address) ⇒ Hash
Decodes an X-address into its underlying account ID, tag, and network type.
-
#encode_x_address(account_id, tag, test) ⇒ String
Encodes an account ID and tag into an X-address.
-
#valid_x_address?(x_address) ⇒ Boolean
Checks if a string is a valid X-address.
-
#x_address_to_classic_address(x_address) ⇒ Hash
Converts an X-address to a classic address.
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.
19 20 21 22 |
# File 'lib/address-codec/address_codec.rb', line 19 def classic_address_to_x_address(classic_address, tag, test) account_id = decode_account_id(classic_address) encode_x_address(account_id, tag, test) end |
#decode_x_address(x_address) ⇒ Hash
Decodes an X-address into its underlying account ID, tag, and network type.
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) account_id = decoded[2, 20] tag = tag_from_uint8_array(decoded) { account_id: 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.
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(account_id, tag, test) if account_id.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], account_id, [ 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.
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.
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) account_id = decoded[:account_id] tag = decoded[:tag] test = decoded[:test] classic_address = encode_account_id(account_id) { classic_address: classic_address, tag: tag, test: test } end |