Class: BinaryCodec::AccountId

Inherits:
Hash160 show all
Defined in:
lib/binary-codec/types/account_id.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Hash

#compare_to, from_parser, #nibblet

Methods inherited from ComparableSerializedType

#compare_to, #eq, #gt, #gte, #lt, #lte

Methods inherited from SerializedType

from_bytes, from_hex, from_json, from_parser, get_type_by_name, #to_byte_sink, #to_bytes, #to_hex, #to_s

Constructor Details

#initialize(bytes = nil) ⇒ AccountId

Returns a new instance of AccountId.



14
15
16
# File 'lib/binary-codec/types/account_id.rb', line 14

def initialize(bytes = nil)
    super(bytes || Array.new(20, 0))
end

Instance Attribute Details

#bytesObject (readonly)

Returns the value of attribute bytes.



10
11
12
# File 'lib/binary-codec/types/account_id.rb', line 10

def bytes
  @bytes
end

Class Method Details

.from(value) ⇒ AccountID

Defines how to construct an AccountID

Parameters:

  • value (AccountID, String)

    Either an existing AccountID, a hex string, or a base58 r-Address

Returns:

  • (AccountID)

    An AccountID object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/binary-codec/types/account_id.rb', line 26

def self.from(value)
    if value.is_a?(AccountId)
        return value
    end

    if value.is_a?(String)
        return AccountId.new if value.empty?

        if valid_hex?(value)
            return AccountId.new(hex_to_bytes(value))
        else
            return from_base58(value)
        end
    end

    raise 'Cannot construct AccountID from the value provided'
end

.from_base58(value) ⇒ AccountID

Defines how to build an AccountID from a base58 r-Address

Parameters:

  • value (String)

    A base58 r-Address

Returns:

  • (AccountID)

    An AccountID object



48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/binary-codec/types/account_id.rb', line 48

def self.from_base58(value)
    if @address_codec.valid_x_address?(value)
        classic = @address_codec.x_address_to_classic_address(value)

        if classic[:tag] != false
            raise 'Only allowed to have tag on Account or Destination'
        end

        value = classic[:classic_address]
    end

    AccountId.new(@address_codec.(value))
end

Instance Method Details

#to_base58String

Defines how to encode AccountID into a base58 address

Returns:

  • (String)

    The base58 string defined by this.bytes



72
73
74
75
# File 'lib/binary-codec/types/account_id.rb', line 72

def to_base58
    address_codec = AddressCodec::AddressCodec.new
    address_codec.(@bytes)
end

#to_jsonString

Overload of to_json

Returns:

  • (String)

    The base58 string for this AccountID



65
66
67
# File 'lib/binary-codec/types/account_id.rb', line 65

def to_json
    to_base58
end