Class: Neb::Address
- Inherits:
-
Object
- Object
- Neb::Address
- Defined in:
- lib/neb/address.rb
Instance Attribute Summary collapse
-
#raw ⇒ Object
readonly
Returns the value of attribute raw.
Class Method Summary collapse
Instance Method Summary collapse
- #checksum ⇒ Object
- #decode(fmt = nil) ⇒ Object
- #encode(fmt) ⇒ Object
- #format ⇒ Object
-
#initialize(raw) ⇒ Address
constructor
A new instance of Address.
- #to_s ⇒ Object
- #value ⇒ Object
Constructor Details
#initialize(raw) ⇒ Address
Returns a new instance of Address.
8 9 10 |
# File 'lib/neb/address.rb', line 8 def initialize(raw) @raw = raw end |
Instance Attribute Details
#raw ⇒ Object (readonly)
Returns the value of attribute raw.
6 7 8 |
# File 'lib/neb/address.rb', line 6 def raw @raw end |
Class Method Details
.is_validate?(address) ⇒ Boolean
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/neb/address.rb', line 70 def is_validate?(address) addr = Utils.base58_to_binary(address) return false if addr.length != Constant::ADDRESS_LENGTH preifx = BaseConvert.decode(addr[0], 256) type = BaseConvert.decode(addr[1], 256) return false if preifx != Constant::ADDRESS_PREFIX return false if type != Constant::NORMAL_TYPE && type != Constant::CONTRACT_TYPE content = addr[0..21] checksum = addr[-4..-1] ActiveSupport::SecurityUtils.secure_compare( Utils.keccak256(content)[0, 4], checksum ) end |
Instance Method Details
#checksum ⇒ Object
20 21 22 |
# File 'lib/neb/address.rb', line 20 def checksum Utils.keccak256(encode(:bin))[0, 4] end |
#decode(fmt = nil) ⇒ Object
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/neb/address.rb', line 41 def decode(fmt = nil) fmt ||= format case fmt when :decimal raw when :bin BaseConvert.decode(raw, 256) when :bin_extended BaseConvert.decode(raw[0, 22], 256) when :hex BaseConvert.decode(Utils.base58_to_binary(raw), 256) when :hex_extended BaseConvert.decode(Utils.base58_to_binary(raw)[0, 22], 256) else raise FormatError, "Invalid format!" end end |
#encode(fmt) ⇒ Object
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/neb/address.rb', line 24 def encode(fmt) case fmt when :decimal raw when :bin BaseConvert.encode(value, 256, 22) when :bin_extended "#{BaseConvert.encode(value, 256, 22)}#{checksum}" when :hex Utils.binary_to_base58(BaseConvert.encode(value, 256, 22)) when :hex_extended Utils.binary_to_base58("#{BaseConvert.encode(value, 256, 22)}#{checksum}") else raise ArgumentError, "invalid format: #{fmt}" end end |
#format ⇒ Object
60 61 62 63 64 65 66 |
# File 'lib/neb/address.rb', line 60 def format return :decimal if raw.is_a?(Numeric) return :bin if raw.size == 22 return :bin_extended if raw.size == 26 return :hex if raw.size == 30 return :hex_extended if raw.size == 35 end |
#to_s ⇒ Object
16 17 18 |
# File 'lib/neb/address.rb', line 16 def to_s encode(:hex_extended) end |
#value ⇒ Object
12 13 14 |
# File 'lib/neb/address.rb', line 12 def value @value ||= decode end |