Method: Klay::Signature#verify
- Defined in:
- lib/klay/signature.rb
#verify(blob, signature, public_key, chain_id = Chain::CYPRESS) ⇒ Boolean
Verifies a signature for a given public key or address.
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 |
# File 'lib/klay/signature.rb', line 116 def verify(blob, signature, public_key, chain_id = Chain::CYPRESS) recovered_key = nil if blob.instance_of? Array or blob.instance_of? Hash # recover Array from sign_typed_data recovered_key = recover_typed_data blob, signature, chain_id elsif blob.instance_of? String and blob.encoding != Encoding::ASCII_8BIT # recover message from personal_sign recovered_key = personal_recover blob, signature, chain_id elsif blob.instance_of? String and (Util.is_hex? blob or blob.encoding == Encoding::ASCII_8BIT) # if nothing else, recover from arbitrary signature recovered_key = recover blob, signature, chain_id end # raise if we cannot determine the data format raise SignatureError, "Unknown data format to verify: #{blob}" if recovered_key.nil? if public_key.instance_of? Address # recovering using an Klay::Address address = public_key.to_s recovered_address = Util.public_key_to_address(recovered_key).to_s return address == recovered_address elsif public_key.instance_of? Secp256k1::PublicKey # recovering using an Secp256k1::PublicKey public_hex = Util.bin_to_hex public_key.uncompressed return public_hex == recovered_key elsif public_key.size == 42 # recovering using an address String address = Address.new(public_key).to_s recovered_address = Util.public_key_to_address(recovered_key).to_s return address == recovered_address elsif public_key.size == 130 # recovering using an uncompressed public key String return public_key == recovered_key else # raise if we cannot determine the public key format used raise SignatureError, "Invalid public key or address supplied #{public_key}!" end end |