Class BTC::Key encapsulates a pair of public and private keys (or a public key only) and provides methods to sign messages and verify signatures.
Private key is a 256-bit integer encoded in a 32-byte big-endian binary string (always padded with zeros from the left).
Private key is used to sign messages. Private key can be nil
for public-only BTC::Key instances.
Public key is a 33-byte or 65-byte binary string encoding coordinates of a point on the elliptic curve secp256k1. Public key is used to verify signatures.
Public key can be compressed (33 bytes) or uncompressed (65 bytes). Compressed public key contains only an X coordinate of a point and recovers Y coordinate with extra arithmetic operations. Uncompressed public key contains both X and Y coordinates. Both versions are equally capable of verifying signatures, but resulting addresses are different (because hashes are different). BIP32 standard uses only compressed public key for compactness and consistency.
If the BTC::Key instance contains only a public key, it can only verify signatures, but cannot create them.
Returns true
if data representing a private key is within a valid range for elliptic curve secp256k1.
Returns true
if the raw binary public key is valid and well-formed.
Accepts both compressed and uncompressed public keys.
Logs detailed info using BTC::Diagnostics.
Returns true
if the script signature is valid.
If verify_lower_s
is true
(it is by default), then this method also verifies if the signature is canonical.
Validates script signature and normalizes it if needed.
Returns nil
if script signature is not valid (e.g. DER encoding is invalid, or hash type is invalid).
Returns script_sig
if it is a valid, canonical signature.
Returns a normalized ("lower s") version of script_sig
if it is non-canonical.
Returns a sig
or a normalized version of sig
.
Assumes signature is a valid plain DER-encoded ECDSA signature, otherwise raises BTCError
.
Returns a new BTC::Key
instance with a random private_key
.
If public_key_compressed
is not specified, true
is used.
If network
is not specified, BTC::Network.default is used.
Returns a new BTC::Key
instance with a given binary private_key
.
If private_key
is not within a valid range, raises FormatError
.
If public_key_compressed
is not specified, true
is used.
If network
is not specified, BTC::Network.default is used.
Returns a new BTC::Key
instance with a given binary public_key
.
This instance does not contain a private key and can only be used to verify signatures.
If public_key
is not a valid compressed or uncompressed public key, raises FormatError
.
If network
is not specified, BTC::Network.default is used.
Returns a new BTC::Key
instance with a private key decoded from WIF string.
If wif
is not a valid WIF-encoded string, raises FormatError
.
Public key compression and network
attributes are inherited from WIF.
Returns a binary DER-encoded ECDSA signature for a given binary string hash
(typically 32-byte long).
Signature is normalized and uses deterministic nonce k
based on private key and the hash
.
Raises ArgumentError
if receiver's private key is nil
.
Returns true
if the binary DER-encoded signature
for a given binary string hash
is correctly verified
against the public key of the receiver.
Returns true
or false
depending on whether public key is compressed.
Returns a new BTC::Key
instance with public_key_compressed
set to true
.
Returns a new BTC::Key
instance with public_key_compressed
set to false
.
Returns a raw 32-byte private key or nil
if it is a public-only key.
Returns a raw 33- or 65-byte public key depending on public_key_compressed
value.
Returns a raw compressed 33-byte public key.
Returns a raw uncompressed 65-byte public key.
Returns a BTC::Network instance used by address
method (mainnet or testnet).
Returns a BTC::PublicKeyAddress instance with a given network
.
If network
is not specified, self.network
is used.
Returns private_key
encoded in WIF.
Returns nil if private_key
is nil.
If network
is not specified, self.network
is used.
Returns a new BTC::Key
instance with the same private_key
, public_key
and network
.
Returns true
if both instances have equal private and public keys.