Class: BinaryCodec::AccountId
- Inherits:
-
Hash160
- Object
- SerializedType
- ComparableSerializedType
- Hash
- Hash160
- BinaryCodec::AccountId
- Defined in:
- lib/binary-codec/types/account_id.rb
Instance Attribute Summary
Attributes inherited from SerializedType
Class Method Summary collapse
-
.from(value) ⇒ AccountId
Creates a new AccountId instance from a value.
-
.from_base58(value) ⇒ AccountId
Creates an AccountId instance from a base58 address.
Instance Method Summary collapse
-
#initialize(bytes = nil) ⇒ AccountId
constructor
A new instance of AccountId.
-
#to_base58 ⇒ String
Returns the base58 representation of the account ID.
- #to_json(_definitions = nil, _field_name = nil) ⇒ Object
Methods inherited from Hash
#compare_to, from_parser, #nibblet, width
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, #value_of
Constructor Details
#initialize(bytes = nil) ⇒ AccountId
Returns a new instance of AccountId.
7 8 9 |
# File 'lib/binary-codec/types/account_id.rb', line 7 def initialize(bytes = nil) super(bytes) end |
Class Method Details
.from(value) ⇒ AccountId
Creates a new AccountId instance from a value.
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/binary-codec/types/account_id.rb', line 14 def self.from(value) return value if value.is_a?(AccountId) if value.is_a?(String) return new if value.empty? if valid_hex?(value) return 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
Creates an AccountId instance from a base58 address.
33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/binary-codec/types/account_id.rb', line 33 def self.from_base58(value) address_codec = AddressCodec::AddressCodec.new 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 new(address_codec.decode_account_id(value)) end |
Instance Method Details
#to_base58 ⇒ String
Returns the base58 representation of the account ID.
54 55 56 57 |
# File 'lib/binary-codec/types/account_id.rb', line 54 def to_base58 address_codec = AddressCodec::AddressCodec.new address_codec.encode_account_id(@bytes) end |
#to_json(_definitions = nil, _field_name = nil) ⇒ Object
48 49 50 |
# File 'lib/binary-codec/types/account_id.rb', line 48 def to_json(_definitions = nil, _field_name = nil) to_base58 end |