Class: Neb::PrivateKey
- Inherits:
-
Object
- Object
- Neb::PrivateKey
- Defined in:
- lib/neb/private_key.rb
Instance Attribute Summary collapse
-
#raw ⇒ Object
readonly
Returns the value of attribute raw.
Class Method Summary collapse
Instance Method Summary collapse
- #decode(fmt = nil) ⇒ Object
- #encode(fmt) ⇒ Object
- #format ⇒ Object
-
#initialize(raw) ⇒ PrivateKey
constructor
A new instance of PrivateKey.
- #to_address ⇒ Object
- #to_address_obj ⇒ Object
- #to_pubkey ⇒ Object
- #to_pubkey_obj ⇒ Object
- #to_s ⇒ Object
- #value ⇒ Object
Constructor Details
#initialize(raw) ⇒ PrivateKey
Returns a new instance of PrivateKey.
8 9 10 |
# File 'lib/neb/private_key.rb', line 8 def initialize(raw) @raw = raw end |
Instance Attribute Details
#raw ⇒ Object (readonly)
Returns the value of attribute raw.
6 7 8 |
# File 'lib/neb/private_key.rb', line 6 def raw @raw end |
Class Method Details
.random ⇒ Object
85 86 87 |
# File 'lib/neb/private_key.rb', line 85 def random new(Utils.random_bytes) end |
Instance Method Details
#decode(fmt = nil) ⇒ Object
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/neb/private_key.rb', line 35 def decode(fmt = nil) fmt ||= format case fmt when :decimal raw when :bin BaseConvert.decode(raw, 256) when :bin_compressed BaseConvert.decode(raw[0, 32], 256) when :hex BaseConvert.decode(raw, 16) when :hex_compressed BaseConvert.decode(raw[0, 64], 16) else raise ArgumentError, "WIF does not represent privkey" end end |
#encode(fmt) ⇒ Object
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/neb/private_key.rb', line 16 def encode(fmt) return self.class.new(value).encode(fmt) unless raw.is_a?(Numeric) case fmt when :decimal raw when :bin BaseConvert.encode(raw, 256, 32) when :bin_compressed "#{BaseConvert.encode(raw, 256, 32)}\x01" when :hex BaseConvert.encode(raw, 16, 64) when :hex_compressed "#{BaseConvert.encode(raw, 16, 64)}01" else raise ArgumentError, "invalid format: #{fmt}" end end |
#format ⇒ Object
58 59 60 61 62 63 64 |
# File 'lib/neb/private_key.rb', line 58 def format return :decimal if raw.is_a?(Numeric) return :bin if raw.size == 32 return :bin_compressed if raw.size == 33 return :hex if raw.size == 64 return :hex_compressed if raw.size == 66 end |
#to_address ⇒ Object
79 80 81 |
# File 'lib/neb/private_key.rb', line 79 def to_address to_address_obj.to_s end |
#to_address_obj ⇒ Object
75 76 77 |
# File 'lib/neb/private_key.rb', line 75 def to_address_obj PublicKey.new(to_pubkey_obj.encode(:bin)).to_address end |
#to_pubkey ⇒ Object
66 67 68 |
# File 'lib/neb/private_key.rb', line 66 def to_pubkey to_pubkey_obj.to_s end |
#to_pubkey_obj ⇒ Object
70 71 72 73 |
# File 'lib/neb/private_key.rb', line 70 def to_pubkey_obj raise ValidationError, "Invalid private key" if value >= Secp256k1::N PublicKey.new(Secp256k1.priv_to_pub(encode(:bin))) end |
#to_s ⇒ Object
12 13 14 |
# File 'lib/neb/private_key.rb', line 12 def to_s encode(:hex) end |
#value ⇒ Object
54 55 56 |
# File 'lib/neb/private_key.rb', line 54 def value @value ||= decode end |