Class: Ethereum::PrivateKey
Instance Attribute Summary collapse
-
#raw ⇒ Object
readonly
Returns the value of attribute raw.
Instance Method Summary collapse
- #decode(fmt = nil) ⇒ Object
- #encode(fmt, vbyte = 0) ⇒ Object
- #format ⇒ Object
-
#initialize(raw) ⇒ PrivateKey
constructor
A new instance of PrivateKey.
- #to_address(extended = false) ⇒ Object
- #to_bitcoin_address ⇒ Object
- #to_pubkey ⇒ Object
- #value ⇒ Object
Constructor Details
#initialize(raw) ⇒ PrivateKey
Returns a new instance of PrivateKey.
8 9 10 |
# File 'lib/ethereum/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/ethereum/private_key.rb', line 6 def raw @raw 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 53 54 55 56 |
# File 'lib/ethereum/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) when :wif BaseConvert.decode Utils.base58_check_to_bytes(raw), 256 when :wif_compressed BaseConvert.decode Utils.base58_check_to_bytes(raw)[0,32], 256 else raise ArgumentError, "WIF does not represent privkey" end end |
#encode(fmt, vbyte = 0) ⇒ Object
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/ethereum/private_key.rb', line 12 def encode(fmt, vbyte=0) return self.class.new(value).encode(fmt, vbyte) 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" when :wif Utils.bytes_to_base58_check(encode(:bin), 128+vbyte) when :wif_compressed Utils.bytes_to_base58_check(encode(:bin_compressed), 128+vbyte) else raise ArgumentError, "invalid format: #{fmt}" end end |
#format ⇒ Object
62 63 64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/ethereum/private_key.rb', line 62 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 bytes = Utils.base58_check_to_bytes raw return :wif if bytes.size == 32 return :wif_compressed if bytes.size == 33 raise FormatError, "WIF does not represent privkey" end |
#to_address(extended = false) ⇒ Object
87 88 89 |
# File 'lib/ethereum/private_key.rb', line 87 def to_address(extended=false) PublicKey.new(to_pubkey).to_address(extended) end |
#to_bitcoin_address ⇒ Object
83 84 85 |
# File 'lib/ethereum/private_key.rb', line 83 def to_bitcoin_address PublicKey.new(to_pubkey).to_bitcoin_address end |
#to_pubkey ⇒ Object
76 77 78 79 80 81 |
# File 'lib/ethereum/private_key.rb', line 76 def to_pubkey raise ValidationError, "Invalid private key" if value >= Secp256k1::N fmt = format.to_s.sub(/wif/, 'hex').to_sym PublicKey.new(Secp256k1.priv_to_pub(encode(:bin))).encode(fmt) end |
#value ⇒ Object
58 59 60 |
# File 'lib/ethereum/private_key.rb', line 58 def value @value ||= decode end |