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) ⇒ Object



13
14
15
16
# File 'lib/address-codec/address_codec.rb', line 13

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

#decode_x_address(x_address) ⇒ Object



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

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) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/address-codec/address_codec.rb', line 18

def encode_x_address(, tag, test)
  if .length != 20
    # RIPEMD160 -> 160 Bits = 20 Bytes
    raise 'Account ID must be 20 bytes'
  end
  if 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

Returns:

  • (Boolean)


73
74
75
76
77
78
79
80
# File 'lib/address-codec/address_codec.rb', line 73

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) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/address-codec/address_codec.rb', line 48

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