Class: DbchainClient::PublicKey

Inherits:
Object
  • Object
show all
Defined in:
lib/dbchain_client/key.rb

Instance Method Summary collapse

Constructor Details

#initialize(pub_key) ⇒ PublicKey

hex or raw public key



6
7
8
9
10
11
12
13
# File 'lib/dbchain_client/key.rb', line 6

def initialize(pub_key) # hex or raw public key
  if pub_key.instance_of?(Secp256k1::PublicKey)
    @public_key = pub_key
  else
    raw_pub_key = Secp256k1::Utils.decode_hex(pub_key)
    @public_key = Secp256k1::PublicKey.new(pubkey: raw_pub_key, raw: true)
  end
end

Instance Method Details

#addressObject



27
28
29
# File 'lib/dbchain_client/key.rb', line 27

def address
  @address ||= Mnemonics.public_key_to_address(public_key_hex)
end

#public_key_hexObject



15
16
17
# File 'lib/dbchain_client/key.rb', line 15

def public_key_hex
  @public_key.serialize.unpack('H*')[0]
end

#to_rawObject



19
20
21
# File 'lib/dbchain_client/key.rb', line 19

def to_raw
  @public_key.serialize
end

#to_sObject



23
24
25
# File 'lib/dbchain_client/key.rb', line 23

def to_s
  public_key_hex
end

#verify(message, signature) ⇒ Object



31
32
33
34
# File 'lib/dbchain_client/key.rb', line 31

def verify(message, signature)
  raw_sig = signature.raw
  @public_key.ecdsa_verify(message, raw_sig)
end