Class: Neb::PublicKey
- Inherits:
-
Object
- Object
- Neb::PublicKey
- Defined in:
- lib/neb/public_key.rb
Instance Attribute Summary collapse
-
#raw ⇒ Object
readonly
Returns the value of attribute raw.
Instance Method Summary collapse
- #decode(fmt = nil) ⇒ Object
- #encode(fmt) ⇒ Object
- #format ⇒ Object
-
#initialize(raw) ⇒ PublicKey
constructor
A new instance of PublicKey.
- #to_address ⇒ Object
- #to_address_obj ⇒ Object
-
#to_s ⇒ Object
skip the type flag, 04.
- #value ⇒ Object
Constructor Details
#initialize(raw) ⇒ PublicKey
Returns a new instance of PublicKey.
8 9 10 |
# File 'lib/neb/public_key.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/public_key.rb', line 6 def raw @raw end |
Instance Method Details
#decode(fmt = nil) ⇒ Object
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/neb/public_key.rb', line 34 def decode(fmt = nil) fmt ||= format case fmt when :decimal raw when :bin [BaseConvert.decode(raw[1, 32], 256), BaseConvert.decode(raw[33, 32], 256)] when :bin_compressed x = BaseConvert.decode raw[1, 32], 256 m = x*x*x + Secp256k1::A*x + Secp256k1::B n = Utils.mod_exp(m, (Secp256k1::P+1)/4, Secp256k1::P) q = (n + raw[0].ord) % 2 y = q == 1 ? (Secp256k1::P - n) : n [x, y] when :hex [BaseConvert.decode(raw[2, 64], 16), BaseConvert.decode(raw[66, 64], 16)] when :hex_compressed PublicKey.new(Utils.hex_to_bin(raw)).decode :bin_compressed else raise FormatError, "Invalid format!" end end |
#encode(fmt) ⇒ Object
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/neb/public_key.rb', line 17 def encode(fmt) case fmt when :decimal value when :bin "\x04#{BaseConvert.encode(value[0], 256, 32)}#{BaseConvert.encode(value[1], 256, 32)}" when :bin_compressed "#{(2+(value[1]%2)).chr}#{BaseConvert.encode(value[0], 256, 32)}" when :hex "04#{BaseConvert.encode(value[0], 16, 64)}#{BaseConvert.encode(value[1], 16, 64)}" when :hex_compressed "0#{2+(value[1]%2)}#{BaseConvert.encode(value[0], 16, 64)}" else raise FormatError, "Invalid format!" end end |
#format ⇒ Object
62 63 64 65 66 67 68 69 70 |
# File 'lib/neb/public_key.rb', line 62 def format return :decimal if raw.is_a?(Array) return :bin if raw.size == 65 && raw[0] == "\x04" return :hex if raw.size == 130 && raw[0, 2] == '04' return :bin_compressed if raw.size == 33 && "\x02\x03".include?(raw[0]) return :hex_compressed if raw.size == 66 && %w(02 03).include?(raw[0,2]) raise FormatError, "Pubkey is not in recognized format" end |
#to_address ⇒ Object
82 83 84 |
# File 'lib/neb/public_key.rb', line 82 def to_address to_address_obj.to_s end |
#to_address_obj ⇒ Object
72 73 74 75 76 77 78 79 80 |
# File 'lib/neb/public_key.rb', line 72 def to_address_obj bytes = [ BaseConvert.encode(Constant::ADDRESS_PREFIX, 256, 1), BaseConvert.encode(Constant::NORMAL_TYPE, 256, 1), Utils.hash160(encode(:bin)) ].join Address.new(bytes) end |
#to_s ⇒ Object
skip the type flag, 04
13 14 15 |
# File 'lib/neb/public_key.rb', line 13 def to_s encode(:hex)[2..-1] end |
#value ⇒ Object
58 59 60 |
# File 'lib/neb/public_key.rb', line 58 def value @value ||= decode end |