lib/bitcoin/key.rb in bitcoin-ruby-0.0.11 vs lib/bitcoin/key.rb in bitcoin-ruby-0.0.12
- old
+ new
@@ -5,10 +5,14 @@
# Elliptic Curve key as used in bitcoin.
class Key
attr_reader :key
+ MIN_PRIV_KEY_MOD_ORDER = 0x01
+ # Order of secp256k1's generator minus 1.
+ MAX_PRIV_KEY_MOD_ORDER = 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEBAAEDCE6AF48A03BBFD25E8CD0364140
+
# Generate a new keypair.
# Bitcoin::Key.generate
def self.generate(opts={compressed: true})
k = new(nil, nil, opts); k.generate; k
end
@@ -253,9 +257,11 @@
set_pub(Bitcoin::OpenSSL_EC.regenerate_key(priv)[1], @pubkey_compressed)
end
# Set +priv+ as the new private key (converting from hex).
def set_priv(priv)
+ value = priv.to_i(16)
+ raise 'private key is not on curve' unless MIN_PRIV_KEY_MOD_ORDER <= value && value <= MAX_PRIV_KEY_MOD_ORDER
@key.private_key = OpenSSL::BN.from_hex(priv)
end
# Set +pub+ as the new public key (converting from hex).
def set_pub(pub, compressed = nil)