Module: Neb::Secp256k1

Defined in:
lib/neb/secp256k1.rb

Defined Under Namespace

Classes: InvalidPrivateKey

Constant Summary collapse

P =

Elliptic curve parameters

2**256 - 2**32 - 977
N =
115792089237316195423570985008687907852837564279074904382605163141518161494337
A =
0
B =
7
Gx =
55066263022277343669578718895168534326250603453777594175500187360389116729240
Gy =
32670510020758816978083085130507043184471273380659243275938904335757337482424
G =
[Gx, Gy].freeze
SECP256K1 =
1

Class Method Summary collapse

Class Method Details

.priv_to_pub(priv) ⇒ Object



37
38
39
40
41
42
# File 'lib/neb/secp256k1.rb', line 37

def priv_to_pub(priv)
  priv = PrivateKey.new(priv)
  privkey = ::Secp256k1::PrivateKey.new(privkey: priv.encode(:bin), raw: true)
  pubkey = privkey.pubkey
  PublicKey.new(pubkey.serialize).encode(priv.format)
end

.sign(msg, priv) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/neb/secp256k1.rb', line 22

def sign(msg, priv)
  priv = PrivateKey.new(priv)
  privkey = ::Secp256k1::PrivateKey.new(privkey: priv.encode(:bin), raw: true)
  signature = privkey.ecdsa_recoverable_serialize(
    privkey.ecdsa_sign_recoverable(msg, raw: true)
  )

  # v = signature[1]
  # r = Utils.bin_to_hex(signature[0][0,32])
  # s = Utils.bin_to_hex(signature[0][32,32])
  # puts v, r, s

  signature[0] << signature[1]
end