Module: CryptoCommons

Defined in:
lib/crypto_commons.rb,
lib/crypto_commons/version.rb

Constant Summary collapse

VERSION =
"0.0.1"

Class Method Summary collapse

Class Method Details

.address(private_key) ⇒ Object



46
47
48
49
50
51
52
53
54
# File 'lib/crypto_commons.rb', line 46

def self.address(private_key)
  hash160 = public_key_hash160(private_key)

  prefix = "00"
  checksum = CheckSum.checksum(prefix + hash160)
  address = Base58.base58_encode(prefix + hash160 + checksum)

  return address
end

.public_key(private_key) ⇒ Object

Private Key To Public Key, takes an Integer as input CryptoCommons.to_public_key(76)



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/crypto_commons.rb', line 19

def self.public_key(private_key)
  point = EllipticCurve.multiply(private_key, $g)

  # convert to hexadecimal
  x = point[:x].to_s(16).rjust(64, "0")
  y = point[:y].to_s(16).rjust(64, "0")

  # compressed public key format
  if (point[:y] % 2 == 0)
    prefix = "02"
  else
    prefix = "03"
  end

  prefix + x
end

.public_key_hash160(private_key) ⇒ Object

Private Key To Public Key Hash, takes an Integer as input CryptoCommons.to_public_key_hash160(76)



38
39
40
41
42
43
44
# File 'lib/crypto_commons.rb', line 38

def self.public_key_hash160(private_key)
  publickey = public_key(private_key)
  binary = [publickey].pack("H*")
  sha256 = Digest::SHA256.digest(binary)
  ripemd160 = Digest::RMD160.digest(sha256)
  return ripemd160.unpack("H*")[0]
end

.wif(private_key) ⇒ Object

Private key to Wallet Import Format, takes a Hex input



11
12
13
14
15
# File 'lib/crypto_commons.rb', line 11

def self.wif(private_key)
  extended = "80" + private_key + "01"
  extendedchecksum = extended + CheckSum.checksum(extended)
  wif = Base58.base58_encode(extendedchecksum)
end