Class: BTC::WIF
Overview
Private key in Wallet Import Format (WIF aka Sipa format).
Examples: 5KQntKuhYWSRXNq... or L3p8oAcQTtuokSC...
Constant Summary
collapse
- KEY_LENGTH =
32
Constants included
from Opcodes
Opcodes::OPCODE_NAME_TO_VALUE, Opcodes::OPCODE_VALUE_TO_NAME, Opcodes::OP_0, Opcodes::OP_0NOTEQUAL, Opcodes::OP_1, Opcodes::OP_10, Opcodes::OP_11, Opcodes::OP_12, Opcodes::OP_13, Opcodes::OP_14, Opcodes::OP_15, Opcodes::OP_16, Opcodes::OP_1ADD, Opcodes::OP_1NEGATE, Opcodes::OP_1SUB, Opcodes::OP_2, Opcodes::OP_2DIV, Opcodes::OP_2DROP, Opcodes::OP_2DUP, Opcodes::OP_2MUL, Opcodes::OP_2OVER, Opcodes::OP_2ROT, Opcodes::OP_2SWAP, Opcodes::OP_3, Opcodes::OP_3DUP, Opcodes::OP_4, Opcodes::OP_5, Opcodes::OP_6, Opcodes::OP_7, Opcodes::OP_8, Opcodes::OP_9, Opcodes::OP_ABS, Opcodes::OP_ADD, Opcodes::OP_AND, Opcodes::OP_BOOLAND, Opcodes::OP_BOOLOR, Opcodes::OP_CAT, Opcodes::OP_CHECKLOCKTIMEVERIFY, Opcodes::OP_CHECKMULTISIG, Opcodes::OP_CHECKMULTISIGVERIFY, Opcodes::OP_CHECKSIG, Opcodes::OP_CHECKSIGVERIFY, Opcodes::OP_CODESEPARATOR, Opcodes::OP_DEPTH, Opcodes::OP_DIV, Opcodes::OP_DROP, Opcodes::OP_DUP, Opcodes::OP_ELSE, Opcodes::OP_ENDIF, Opcodes::OP_EQUAL, Opcodes::OP_EQUALVERIFY, Opcodes::OP_FALSE, Opcodes::OP_FROMALTSTACK, Opcodes::OP_GREATERTHAN, Opcodes::OP_GREATERTHANOREQUAL, Opcodes::OP_HASH160, Opcodes::OP_HASH256, Opcodes::OP_IF, Opcodes::OP_IFDUP, Opcodes::OP_INVALIDOPCODE, Opcodes::OP_INVERT, Opcodes::OP_LEFT, Opcodes::OP_LESSTHAN, Opcodes::OP_LESSTHANOREQUAL, Opcodes::OP_LSHIFT, Opcodes::OP_MAX, Opcodes::OP_MIN, Opcodes::OP_MOD, Opcodes::OP_MUL, Opcodes::OP_NEGATE, Opcodes::OP_NIP, Opcodes::OP_NOP, Opcodes::OP_NOP1, Opcodes::OP_NOP10, Opcodes::OP_NOP2, Opcodes::OP_NOP3, Opcodes::OP_NOP4, Opcodes::OP_NOP5, Opcodes::OP_NOP6, Opcodes::OP_NOP7, Opcodes::OP_NOP8, Opcodes::OP_NOP9, Opcodes::OP_NOT, Opcodes::OP_NOTIF, Opcodes::OP_NUMEQUAL, Opcodes::OP_NUMEQUALVERIFY, Opcodes::OP_NUMNOTEQUAL, Opcodes::OP_OR, Opcodes::OP_OVER, Opcodes::OP_PICK, Opcodes::OP_PUSHDATA1, Opcodes::OP_PUSHDATA2, Opcodes::OP_PUSHDATA4, Opcodes::OP_RESERVED, Opcodes::OP_RESERVED1, Opcodes::OP_RESERVED2, Opcodes::OP_RETURN, Opcodes::OP_RIGHT, Opcodes::OP_RIPEMD160, Opcodes::OP_ROLL, Opcodes::OP_ROT, Opcodes::OP_RSHIFT, Opcodes::OP_SHA1, Opcodes::OP_SHA256, Opcodes::OP_SIZE, Opcodes::OP_SUB, Opcodes::OP_SUBSTR, Opcodes::OP_SWAP, Opcodes::OP_TOALTSTACK, Opcodes::OP_TRUE, Opcodes::OP_TUCK, Opcodes::OP_VER, Opcodes::OP_VERIF, Opcodes::OP_VERIFY, Opcodes::OP_VERNOTIF, Opcodes::OP_WITHIN, Opcodes::OP_XOR
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
Methods inherited from Address
#data, #hash, #mainnet?, mux_parse_raw_data, #network, #p2pkh?, #p2sh?, parse, parse_raw_data, register_class, #testnet?, #to_s, #version
Constructor Details
#initialize(string: nil, data: nil, network: nil, _raw_data: nil, private_key: nil, key: nil, public_key_compressed: nil) ⇒ WIF
Usage:
- WIF.new(string: ...)
- WIF.new(private_key: ..., public_key_compressed: true|false, network: ...)
- WIF.new(key: ...)
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
|
# File 'lib/btcruby/wif.rb', line 47
def initialize(string: nil,
data: nil,
network: nil,
_raw_data: nil,
private_key: nil,
key: nil,
public_key_compressed: nil)
if key
raise ArgumentError, "Key must contain private_key to be exported in WIF" if !key.private_key
private_key = key.private_key
if public_key_compressed == nil
public_key_compressed = key.public_key_compressed
end
network ||= key.network
end
if string
if data || private_key || key || (public_key_compressed != nil) || network
raise ArgumentError, "Cannot specify individual attributes when decoding WIF from string"
end
_raw_data ||= Base58.data_from_base58check(string)
if _raw_data.bytesize != (1 + KEY_LENGTH) && _raw_data.bytesize != (2 + KEY_LENGTH)
raise FormatError, "Raw WIF data should have size #{1 + KEY_LENGTH}(+1), but it is #{_raw_data.bytesize} instead"
end
@base58check_string = string
@data = _raw_data[1, KEY_LENGTH]
@public_key_compressed = (_raw_data.bytesize == (2 + KEY_LENGTH))
@version = _raw_data.bytes.first
@network = nil
elsif data ||= private_key
if data.bytesize != KEY_LENGTH
raise FormatError, "Failed to create WIF: data should have size #{KEY_LENGTH}, but it is #{data.bytesize} instead"
end
@base58check_string = nil
@data = data
@public_key_compressed = public_key_compressed
if @public_key_compressed == nil
@public_key_compressed = false end
@version = nil
@network = network
else
raise ArgumentError, "Either data or string must be provided"
end
end
|
Instance Attribute Details
#public_key_compressed ⇒ Object
Returns the value of attribute public_key_compressed.
18
19
20
|
# File 'lib/btcruby/wif.rb', line 18
def public_key_compressed
@public_key_compressed
end
|
Class Method Details
.mainnet_version ⇒ Object
10
11
12
|
# File 'lib/btcruby/wif.rb', line 10
def self.mainnet_version
128
end
|
.testnet_version ⇒ Object
14
15
16
|
# File 'lib/btcruby/wif.rb', line 14
def self.testnet_version
239
end
|
Instance Method Details
#==(other) ⇒ Object
Also known as:
eql?
35
36
37
38
39
40
|
# File 'lib/btcruby/wif.rb', line 35
def ==(other)
return false if !other
self.data == other.data &&
self.version == other.version &&
self.public_key_compressed == other.public_key_compressed
end
|
#data_for_base58check_encoding ⇒ Object
93
94
95
96
97
98
99
|
# File 'lib/btcruby/wif.rb', line 93
def data_for_base58check_encoding
data = BTC::Data.data_from_bytes([self.version]) + @data
if @public_key_compressed
data += BTC::Data.data_from_bytes([0x01])
end
return data
end
|
#inspect ⇒ Object
101
102
103
|
# File 'lib/btcruby/wif.rb', line 101
def inspect
%{#<#{self.class}:#{to_s} privkey:#{BTC.to_hex(data)} (#{@public_key_compressed ? '' : 'un'}compressed pubkey)>}
end
|
#key ⇒ Object
23
24
25
|
# File 'lib/btcruby/wif.rb', line 23
def key
BTC::Key.new(private_key: self.private_key, public_key_compressed: @public_key_compressed, network: self.network)
end
|
#private_key ⇒ Object
27
28
29
|
# File 'lib/btcruby/wif.rb', line 27
def private_key
@data
end
|
#public_address ⇒ Object
31
32
33
|
# File 'lib/btcruby/wif.rb', line 31
def public_address
self.key.address(network: self.network)
end
|
#public_key_compressed? ⇒ Boolean
19
20
21
|
# File 'lib/btcruby/wif.rb', line 19
def public_key_compressed?
@public_key_compressed
end
|