Class: CiBlockIo::Key
- Inherits:
-
Object
- Object
- CiBlockIo::Key
- Defined in:
- lib/ci_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
177 178 179 180 181 182 183 184 185 |
# File 'lib/ci_block_io.rb', line 177 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
217 218 219 220 221 222 223 224 225 226 227 |
# File 'lib/ci_block_io.rb', line 217 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
229 230 231 232 233 234 235 236 237 238 239 240 |
# File 'lib/ci_block_io.rb', line 229 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
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 287 288 289 290 291 292 293 294 |
# File 'lib/ci_block_io.rb', line 248 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
242 243 244 245 246 |
# File 'lib/ci_block_io.rb', line 242 def isPositive(i) sig = "!+-"[i <=> 0] return sig.eql?("+") end |
#private_key ⇒ Object
187 188 189 190 |
# File 'lib/ci_block_io.rb', line 187 def private_key # returns private key in hex form return @private_key.to_s(16) end |
#public_key ⇒ Object
192 193 194 195 196 |
# File 'lib/ci_block_io.rb', line 192 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
198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 |
# File 'lib/ci_block_io.rb', line 198 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 |