class Nem::Keypair

Attributes

private[R]
public[R]

Public Class Methods

generate(seed = SecureRandom.hex(32)) click to toggle source

@return [Nem::Keypair] new key pair

# File lib/nem/keypair.rb, line 27
def self.generate(seed = SecureRandom.hex(32))
  unless seed =~ /\A\h{64}\z/ || seed =~ /\A\h{66}\z/
    raise ArgumentError, 'PrivateKey is not valid!'
  end
  new(seed)
end
new(private_key) click to toggle source

@param [String] Hex Private Key

# File lib/nem/keypair.rb, line 8
def initialize(private_key)
  @private = private_key
  @public  = calc_public_key
end

Public Instance Methods

sign(data) click to toggle source

@param [String] Hex string @return [String] Signed hex string

# File lib/nem/keypair.rb, line 15
def sign(data)
  bin_data = Nem::Util::Convert.hex2bin(data)
  bin_signed = Nem::Util::Ed25519.signature_hash_unsafe(bin_data, bin_secret, bin_public)
  bin_signed.unpack('H*').first
end
verify_signature(signer, hash, apostille_hash) click to toggle source
# File lib/nem/keypair.rb, line 21
def verify_signature(signer, hash, apostille_hash)
  # TODO: support private apostille
  raise NotImplementedError, 'Not implemented private apostille'
end

Private Instance Methods

bin_public() click to toggle source
# File lib/nem/keypair.rb, line 48
def bin_public
  @bin_public ||= Nem::Util::Ed25519.publickey_hash_unsafe(bin_secret)
end
bin_secret() click to toggle source
# File lib/nem/keypair.rb, line 44
def bin_secret
  @bin_secret ||= Nem::Util::Convert.hex2bin_rev(fix_private_key(@private))
end
calc_public_key() click to toggle source
# File lib/nem/keypair.rb, line 40
def calc_public_key
  bin_public.unpack('H*').first
end
fix_private_key(key) click to toggle source
# File lib/nem/keypair.rb, line 36
def fix_private_key(key)
  "#{'0' * 64}#{key.sub(/^00/i, '')}"[-64, 64]
end