Class: Ethereum::PublicKey

Inherits:
Object show all
Defined in:
lib/ethereum/public_key.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(raw) ⇒ PublicKey

Returns a new instance of PublicKey.



8
9
10
# File 'lib/ethereum/public_key.rb', line 8

def initialize(raw)
  @raw = raw
end

Instance Attribute Details

#rawObject (readonly)

Returns the value of attribute raw.



6
7
8
# File 'lib/ethereum/public_key.rb', line 6

def raw
  @raw
end

Instance Method Details

#decode(fmt = nil) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/ethereum/public_key.rb', line 33

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.decode_hex(raw)).decode :bin_compressed
  when :bin_electrum
    [BaseConvert.decode(raw[0,32], 256), BaseConvert.decode(raw[32,32], 256)]
  when :hex_electrum
    [BaseConvert.decode(raw[0,64], 16), BaseConvert.decode(raw[64,128], 16)]
  else
    raise FormatError, "Invalid format!"
  end
end

#encode(fmt) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/ethereum/public_key.rb', line 12

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)}"
  when :bin_electrum
    "#{BaseConvert.encode(value[0], 256, 32)}#{BaseConvert.encode(value[1], 256, 32)}"
  when :hex_electrum
    "#{BaseConvert.encode(value[0], 16, 64)}#{BaseConvert.encode(value[1], 16, 64)}"
  else
    raise FormatError, "Invalid format!"
  end
end

#formatObject

Raises:



65
66
67
68
69
70
71
72
73
74
75
# File 'lib/ethereum/public_key.rb', line 65

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])
  return :bin_electrum if raw.size == 64
  return :hex_electrum if raw.size == 128

  raise FormatError, "Pubkey is not in recognized format"
end

#to_address(extended = false) ⇒ Object



82
83
84
85
# File 'lib/ethereum/public_key.rb', line 82

def to_address(extended=false)
  bytes = Utils.keccak256(encode(:bin)[1..-1])[-20..-1]
  Address.new(bytes).to_bytes(extended)
end

#to_bitcoin_address(magicbyte = 0) ⇒ Object



77
78
79
80
# File 'lib/ethereum/public_key.rb', line 77

def to_bitcoin_address(magicbyte=0)
  bytes = encode(:bin)
  Utils.bytes_to_base58_check Utils.hash160(bytes), magicbyte
end

#valueObject



61
62
63
# File 'lib/ethereum/public_key.rb', line 61

def value
  @value ||= decode
end