Module: Nano::Key
Overview
The key module is responsible for handling the key and seed based operations in the Nano module.
Instance Method Summary collapse
- #derive_address(public_key, prefix = "nano_") ⇒ Object
- #derive_public_key(input) ⇒ Object
-
#derive_secret_key(seed, index) ⇒ Object
Derive a secret key from a seed, given an index.
-
#generate_seed ⇒ Object
Generates a 32 byte seed as a hexidecimal string.
Instance Method Details
#derive_address(public_key, prefix = "nano_") ⇒ Object
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/nano/key.rb', line 53 def derive_address(public_key, prefix = "nano_") is_public_key = Nano::Check.is_key?(public_key) raise ArgumentError, "Incorrect key type" unless is_public_key is_valid_prefix = prefix == "nano_" || prefix == "xrb_" raise ArgumentError, "Invalid prefix" unless is_valid_prefix public_key_bytes = Nano::Utils.hex_to_bytes public_key public_key_enc = Nano::Base32.encode public_key_bytes pk_bin = Nano::Utils.hex_to_bin(public_key) checksum = Blake2b.hex(pk_bin, Blake2b::Key.none, 5) checksum_bytes = Nano::Utils.hex_to_bytes(checksum).reverse enc_chk = Nano::Base32.encode(checksum_bytes) "#{prefix}#{public_key_enc}#{enc_chk}" end |
#derive_public_key(input) ⇒ Object
39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/nano/key.rb', line 39 def derive_public_key(input) is_secret_key = Nano::Check.is_key?(input) account = Nano::Account.from_address(input) is_address = !account.nil? raise ArgumentError, "Incorrect input" unless is_secret_key || is_address if is_secret_key res = NanocurrencyExt.public_key(Nano::Utils.hex_to_bin(input)) Nano::Utils.bin_to_hex(res).upcase else account.public_key end end |
#derive_secret_key(seed, index) ⇒ Object
Derive a secret key from a seed, given an index.
27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/nano/key.rb', line 27 def derive_secret_key(seed, index) raise( ArgumentError, "Seed is invalid" ) unless Nano::Check.is_seed_valid?(seed) raise( ArgumentError, "Index is invalid" ) unless Nano::Check.is_index_valid?(index) seed_bin = Nano::Utils.hex_to_bin(seed) Blake2b.hex(seed_bin + [index].pack('L>'), Blake2b::Key.none, 32).upcase end |
#generate_seed ⇒ Object
Generates a 32 byte seed as a hexidecimal string. Cryptographically secure. return [String] A 64 length string of hexidecimal.
18 19 20 |
# File 'lib/nano/key.rb', line 18 def generate_seed SecureRandom.hex(32) end |