Class: BlockIo::Key
- Inherits:
-
Object
- Object
- BlockIo::Key
- Defined in:
- lib/block_io.rb
Class Method Summary collapse
Instance Method Summary collapse
- #deterministicGenerateK(data, privkey, group = ECDSA::Group::Secp256k1) ⇒ Object
-
#initialize(privkey = nil, compressed = true) ⇒ Key
constructor
A new instance of Key.
- #isPositive(i) ⇒ Object
- #private_key ⇒ Object
- #public_key ⇒ Object
- #sign(data) ⇒ Object
Constructor Details
#initialize(privkey = nil, compressed = true) ⇒ Key
Returns a new instance of Key.
169 170 171 172 173 174 175 176 177 |
# File 'lib/block_io.rb', line 169 def initialize(privkey = nil, compressed = true) # the privkey must be in hex if at all provided @group = ECDSA::Group::Secp256k1 @private_key = privkey.to_i(16) || 1 + SecureRandom.random_number(group.order - 1) @public_key = @group.generator.multiply_by_scalar(@private_key) @compressed = compressed end |
Class Method Details
.from_passphrase(passphrase) ⇒ Object
209 210 211 212 213 214 215 216 217 218 219 |
# File 'lib/block_io.rb', line 209 def self.from_passphrase(passphrase) # create a private+public key pair from a given passphrase # think of this as your brain wallet. be very sure to use a sufficiently long passphrase # if you don't want a passphrase, just use Key.new and it will generate a random key for you raise Exception.new('Must provide passphrase at least 8 characters long.') if passphrase.nil? or passphrase.length < 8 hashed_key = Helper.sha256([passphrase].pack("H*")) # must pass bytes to sha256 return Key.new(hashed_key) end |
.from_wif(wif) ⇒ Object
221 222 223 224 225 226 227 228 229 230 231 232 |
# File 'lib/block_io.rb', line 221 def self.from_wif(wif) # returns a new key extracted from the Wallet Import Format provided # TODO check against checksum hexkey = Helper.decode_base58(wif) actual_key = hexkey[2...66] compressed = hexkey[2..hexkey.length].length-8 > 64 and hexkey[2..hexkey.length][64...66] == '01' return Key.new(actual_key, compressed) end |
Instance Method Details
#deterministicGenerateK(data, privkey, group = ECDSA::Group::Secp256k1) ⇒ Object
240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 |
# File 'lib/block_io.rb', line 240 def deterministicGenerateK(data, privkey, group = ECDSA::Group::Secp256k1) # returns a deterministic K -- RFC6979 hash = data.bytes.to_a x = [privkey.to_s(16)].pack("H*").bytes.to_a k = [] 32.times { k.insert(0, 0) } v = [] 32.times { v.insert(0, 1) } # step D k = OpenSSL::HMAC.digest(OpenSSL::Digest.new('sha256'), k.pack("C*"), [].concat(v).concat([0]).concat(x).concat(hash).pack("C*")).bytes.to_a # step E v = OpenSSL::HMAC.digest(OpenSSL::Digest.new('sha256'), k.pack("C*"), v.pack("C*")).bytes.to_a # puts "E: " + v.pack("C*").unpack("H*")[0] # step F k = OpenSSL::HMAC.digest(OpenSSL::Digest.new('sha256'), k.pack("C*"), [].concat(v).concat([1]).concat(x).concat(hash).pack("C*")).bytes.to_a # step G v = OpenSSL::HMAC.digest(OpenSSL::Digest.new('sha256'), k.pack("C*"), v.pack("C*")).bytes.to_a # step H2b (Step H1/H2a ignored) v = OpenSSL::HMAC.digest(OpenSSL::Digest.new('sha256'), k.pack("C*"), v.pack("C*")).bytes.to_a h2b = v.pack("C*").unpack("H*")[0] tNum = h2b.to_i(16) # step H3 while (!isPositive(tNum) or tNum >= group.order) do # k = crypto.HmacSHA256(Buffer.concat([v, new Buffer([0])]), k) k = OpenSSL::HMAC.digest(OpenSSL::Digest.new('sha256'), k.pack("C*"), [].concat(v).concat([0]).pack("C*")).bytes.to_a # v = crypto.HmacSHA256(v, k) v = OpenSSL::HMAC.digest(OpenSSL::Digest.new('sha256'), k.pack("C*"), v.pack("C*")).bytes.to_a # T = BigInteger.fromBuffer(v) tNum = v.pack("C*").unpack("H*")[0].to_i(16) end return tNum end |
#isPositive(i) ⇒ Object
234 235 236 237 238 |
# File 'lib/block_io.rb', line 234 def isPositive(i) sig = "!+-"[i <=> 0] return sig.eql?("+") end |
#private_key ⇒ Object
179 180 181 182 |
# File 'lib/block_io.rb', line 179 def private_key # returns private key in hex form return @private_key.to_s(16) end |
#public_key ⇒ Object
184 185 186 187 188 |
# File 'lib/block_io.rb', line 184 def public_key # returns the compressed form of the public key to save network fees (shorter scripts) return ECDSA::Format::PointOctetString.encode(@public_key, compression: @compressed).unpack("H*")[0] end |
#sign(data) ⇒ Object
190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 |
# File 'lib/block_io.rb', line 190 def sign(data) # signed the given hexadecimal string nonce = deterministicGenerateK([data].pack("H*"), @private_key) # RFC6979 signature = ECDSA.sign(@group, @private_key, data.to_i(16), nonce) # BIP0062 -- use lower S values only r, s = signature.components over_two = @group.order >> 1 # half of what it was s = @group.order - s if (s > over_two) signature = ECDSA::Signature.new(r, s) # DER encode this, and return it in hex form return ECDSA::Format::SignatureDerString.encode(signature).unpack("H*")[0] end |