Class: VChainClient::BlockstackClient
- Inherits:
-
Object
- Object
- VChainClient::BlockstackClient
- Defined in:
- lib/vchain_client/blockstack_client.rb
Constant Summary collapse
- MASTER_ECC_PUBLIC_KEY =
"MFYwEAYHKoZIzj0CAQYFK4EEAAoDQgAEXGOuvJT5hb/bR5y/JADHxJEEaQzuJUzw Xet0UYuBrILjHB9HcxFc+WwuCflIRWtRDsNfaY6Ra7j/cRYLeiocYA=="- @@recs_cache =
{}
- @@ecc_keys_cache =
{}
- @@rsa_keys_cache =
{}
Instance Method Summary collapse
- #checkBlockstackRecord(blockstack_id, type) ⇒ Object
- #checkFederativeServer(federative_server_id) ⇒ Object
- #checkValidator(validator_id) ⇒ Object
- #checkVerificator(verificator_id) ⇒ Object
- #getBlockstackRecord(blockstack_id, force_refresh = false) ⇒ Object
- #getPublicKeyECC(blockstack_id, force_refresh = false) ⇒ Object
- #getPublicKeyRSA(blockstack_id, force_refresh = false) ⇒ Object
-
#initialize(config) ⇒ BlockstackClient
constructor
A new instance of BlockstackClient.
Constructor Details
#initialize(config) ⇒ BlockstackClient
Returns a new instance of BlockstackClient.
15 16 17 18 19 |
# File 'lib/vchain_client/blockstack_client.rb', line 15 def initialize(config) @config = config @log = Log4r::Logger["vchain_client"] end |
Instance Method Details
#checkBlockstackRecord(blockstack_id, type) ⇒ Object
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 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 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 |
# File 'lib/vchain_client/blockstack_client.rb', line 96 def checkBlockstackRecord(blockstack_id, type) if @log.debug? @log.debug("[Blockstack.checkBlockstackRecord] input:") @log.debug("-> blockstack_id: #{blockstack_id}") @log.debug("-> type: #{type}") end record = nil begin record = self.getBlockstackRecord(blockstack_id) rescue => e if @log.error? @log.error("[Blockstack.checkBlockstackRecord] getBlockstackRecord raised exception:") @log.error("#{e.class}, #{e.message}") @log.error("-> blockstack_id: #{blockstack_id}") @log.error("-> type: #{type}") @log.error("--> blockstack_id: #{blockstack_id}") end raise e end if record == nil if @log.error? @log.error("[Blockstack.checkBlockstackRecord] failed to retrieve record") @log.error("-> blockstack_id: #{blockstack_id}") @log.error("-> type: #{type}") @log.error("--> blockstack_id: #{blockstack_id}") end return false end cryptoHelper = VChainClient::Crypto.new(@config) if @log.debug? @log.debug("[Blockstack.checkBlockstackRecord] Crypto initialized") end if record != nil if record.key?("ecc_pubkey") if record.key?("vchain_role") if record["vchain_role"] != type if @log.error? @log.error("[Blockstack.checkBlockstackRecord] type mismatch") @log.error("-> blockstack_id: #{blockstack_id}") @log.error("-> type: #{type}") @log.error("--> '"+ record["vchain_role"] +"'") end return false end if !record.key?("validator_blockstack_id") if @log.error? @log.error("[Blockstack.checkBlockstackRecord] record doesn't have 'validator_blockstack_id' field") @log.error("-> blockstack_id: #{blockstack_id}") @log.error("-> type: #{type}") end return false end validator_blockstack_id = record["validator_blockstack_id"] if @log.debug? @log.debug("[Blockstack.checkBlockstackRecord] record's vchain_role is '"+ record["vchain_role"] +"'") end if record["vchain_role"] != 'validator' if @log.debug? @log.debug("[Blockstack.checkBlockstackRecord] going to check validator for #{blockstack_id}, validator_id: #{validator_blockstack_id}") end begin if !self.checkValidator(validator_blockstack_id) if @log.error? @log.error("[Blockstack.checkBlockstackRecord] failed to check validator") @log.error("-> blockstack_id: #{blockstack_id}") @log.error("-> type: #{type}") @log.error("--> validator_blockstack_id: #{validator_blockstack_id}") end return false end rescue => e if @log.error? @log.error("[Blockstack.checkBlockstackRecord] checkValidator raised exception:") @log.error("#{e.class}, #{e.message}") @log.error("-> blockstack_id: #{blockstack_id}") @log.error("-> type: #{type}") @log.error("--> validator_blockstack_id: #{validator_blockstack_id}") end raise e end end validator_ecc_pub_key = nil if record["vchain_role"] != 'validator' begin validator_ecc_pub_key = self.getPublicKeyECC(validator_blockstack_id) rescue => e if @log.error? @log.error("[Blockstack.checkBlockstackRecord] getPublicKeyECC raised exception:") @log.error("#{e.class}, #{e.message}") @log.error("-> blockstack_id: #{blockstack_id}") @log.error("-> type: #{type}") @log.error("--> validator_blockstack_id: #{validator_blockstack_id}") end raise e end else validator_ecc_pub_key = MASTER_ECC_PUBLIC_KEY; end if validator_ecc_pub_key == nil if @log.error? @log.error("[Blockstack.checkBlockstackRecord] failed to retrieve public key:") @log.error("-> blockstack_id: #{blockstack_id}") @log.error("-> type: #{type}") @log.error("--> validator_blockstack_id: #{validator_blockstack_id}") end return false end # check client's sig version 1 client_sig = record["client_sig"] validator_sig = record["validator_sig"] client_sig_to_check = record["vchain_id"] + record["vchain_role"] + blockstack_id + record["ecc_pubkey"] + record["sig_version"]; validator_sig_to_check = record["vchain_id"] + record["vchain_role"] + blockstack_id + record["ecc_pubkey"] + record["sig_version"] + record["validator_vchain_id"] + validator_blockstack_id # client's sig versions 2 && 3 if record["sig_version"] == "2" || record["sig_version"] == "3" # need to retrieve RSA key if !record.key?("rsa_pubkey") if @log.error? @log.error("[Blockstack.checkBlockstackRecord] record doesn't have 'rsa_pubkey' field") @log.error("-> blockstack_id: #{blockstack_id}") @log.error("-> type: #{type}") end return false end if record["sig_version"] == "2" # sig version 2 client_sig = record["client_sig_v2"] validator_sig = record["validator_sig_v2"] client_sig_to_check = record["vchain_id"] + record["vchain_role"] + blockstack_id + record["ecc_pubkey"] + record["rsa_pubkey"] + record["sig_version"]; validator_sig_to_check = record["vchain_id"] + record["vchain_role"] + blockstack_id + record["ecc_pubkey"].gsub(/\n/, "") + record["rsa_pubkey"].gsub(/\n/, "") + record["sig_version"] + record["validator_vchain_id"] + validator_blockstack_id elsif record["sig_version"] == "3" # sig version 3 client_sig = record["client_sig_v3"] validator_sig = record["validator_sig_v3"] client_sig_to_check = record["vchain_id"] + record["vchain_role"] + blockstack_id + record["ecc_pubkey"].gsub(/\n/, "") + record["rsa_pubkey"].gsub(/\n/, "") + record["sig_version"]; validator_sig_to_check = record["vchain_id"] + record["vchain_role"] + blockstack_id + record["ecc_pubkey"].gsub(/\n/, "") + record["rsa_pubkey"].gsub(/\n/, "") + record["sig_version"] + record["validator_vchain_id"] + validator_blockstack_id end end begin if cryptoHelper.verifySignature(client_sig_to_check, client_sig, record["ecc_pubkey"]) # check validator's sig begin if cryptoHelper.verifySignature(validator_sig_to_check, validator_sig, validator_ecc_pub_key) return true; else if @log.error? @log.error("[Blockstack.checkBlockstackRecord] failed to verify validator_sig") @log.error("-> blockstack_id: #{blockstack_id}") @log.error("-> type: #{type}") @log.error("--> validator_sig_to_check: #{validator_sig_to_check}") @log.error("--> validator_sig: "+ Base64.encode64(record["validator_sig"])) @log.error("--> validator_ecc_pub_key: #{validator_ecc_pub_key}") end return false end rescue => e if @log.error? @log.error("[Blockstack.checkBlockstackRecord] verifySignature for validator_sig raised exception:") @log.error("#{e.class}, #{e.message}") @log.error("-> blockstack_id: #{blockstack_id}") @log.error("-> type: #{type}") @log.error("--> validator_sig_to_check: #{validator_sig_to_check}") @log.error("--> validator_sig: "+ Base64.encode64(record["validator_sig"])) @log.error("--> validator_ecc_pub_key: #{validator_ecc_pub_key}") end raise e end else if @log.error? @log.error("[Blockstack.checkBlockstackRecord] failed to verify client_sig") @log.error("-> blockstack_id: #{blockstack_id}") @log.error("-> type: #{type}") @log.error("--> client_sig_to_check: #{client_sig_to_check}") @log.error("--> client_sig: "+ Base64.encode64(record["client_sig"])) @log.error("--> ecc_pubkey: "+ record["ecc_pubkey"]) end return false end rescue => e if @log.error? @log.error("[Blockstack.checkBlockstackRecord] verifySignature for client_sig raised exception:") @log.error("#{e.class}, #{e.message}") @log.error("-> blockstack_id: #{blockstack_id}") @log.error("-> type: #{type}") @log.error("--> client_sig_to_check: #{client_sig_to_check}") @log.error("--> client_sig: "+ Base64.encode64(record["client_sig"])) @log.error("--> ecc_pubkey: "+ record["ecc_pubkey"]) end raise e end else if @log.error? @log.error("[Blockstack.checkBlockstackRecord] record doesn't have 'vchain_role' field") @log.error("-> blockstack_id: #{blockstack_id}") @log.error("-> type: #{type}") end end else if @log.error? @log.error("[Blockstack.checkBlockstackRecord] record doesn't have 'ecc_pubkey' field") @log.error("-> blockstack_id: #{blockstack_id}") @log.error("-> type: #{type}") end end else if @log.error? @log.error("[Blockstack.checkBlockstackRecord] record is null") @log.error("-> blockstack_id: #{blockstack_id}") @log.error("-> type: #{type}") end end return false end |
#checkFederativeServer(federative_server_id) ⇒ Object
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/vchain_client/blockstack_client.rb', line 21 def checkFederativeServer(federative_server_id) if @log.debug? @log.debug("[Blockstack.checkFederativeServer] input:") @log.debug("-> federative_server_id: #{federative_server_id}") end begin return self.checkBlockstackRecord(federative_server_id, 'federative_server') rescue => e if @log.error? @log.error("[Blockstack.checkFederativeServer] checkBlockstackRecord raised exception:") @log.error("#{e.class}, #{e.message}") @log.error("-> federative_server_id: #{federative_server_id}") @log.error("--> federative_server_id: #{federative_server_id}") @log.error("--> type: federative_server") end raise e end end |
#checkValidator(validator_id) ⇒ Object
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 |
# File 'lib/vchain_client/blockstack_client.rb', line 71 def checkValidator(validator_id) if @log.debug? @log.debug("[Blockstack.checkValidator] input:") @log.debug("-> validator_id: #{validator_id}") end begin return self.checkBlockstackRecord(validator_id, 'validator') rescue => e if @log.error? @log.error("[Blockstack.checkValidator] checkBlockstackRecord raised exception:") @log.error("#{e.class}, #{e.message}") @log.error("-> validator_id: #{validator_id}") @log.error("--> validator_id: #{validator_id}") @log.error("--> type: validator") end raise e end end |
#checkVerificator(verificator_id) ⇒ Object
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/vchain_client/blockstack_client.rb', line 46 def checkVerificator(verificator_id) if @log.debug? @log.debug("[Blockstack.checkVerificator] input:") @log.debug("-> verificator_id: #{verificator_id}") end begin return self.checkBlockstackRecord(verificator_id, 'verificator') rescue => e if @log.error? @log.error("[Blockstack.checkVerificator] checkBlockstackRecord raised exception:") @log.error("#{e.class}, #{e.message}") @log.error("-> verificator_id: #{verificator_id}") @log.error("--> verificator_id: #{verificator_id}") @log.error("--> type: verificator") end raise e end end |
#getBlockstackRecord(blockstack_id, force_refresh = false) ⇒ Object
508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 |
# File 'lib/vchain_client/blockstack_client.rb', line 508 def getBlockstackRecord(blockstack_id, force_refresh=false) if @log.debug? @log.debug("[Blockstack.getBlockstackRecord] input:") @log.debug("-> blockstack_id: #{blockstack_id}") end if !force_refresh if @@recs_cache.key?(blockstack_id) if @log.debug? @log.debug("[Blockstack.getBlockstackRecord] '#{blockstack_id}' is in a cache") end return @@recs_cache[blockstack_id] end end blockstack_path = @config["blockstack"]["path"] cmd_init = blockstack_path +" set_advanced_mode on" cmd = blockstack_path +" get_name_zonefile "+ blockstack_id if @log.debug? @log.debug("[Blockstack.getBlockstackRecord] using '#{blockstack_path}' as a Blockstack path") @log.debug("[Blockstack.getBlockstackRecord] running '#{cmd}'") end ret = nil begin `#{cmd_init}` ret = `#{cmd}` rescue => e if @log.error? @log.error("[Blockstack.getBlockstackRecord] cmd call '#{cmd}' raised exception:") @log.error("#{e.class}, #{e.message}") @log.error("-> blockstack_id: #{blockstack_id}") @log.error("--> '#{cmd}'") end raise e end if ret == nil if @log.error? @log.error("[Blockstack.getBlockstackRecord] failed to retrieve record from cmd call") @log.error("-> blockstack_id: #{blockstack_id}") @log.error("--> '#{cmd}'") end return nil end if @log.debug? @log.debug("[Blockstack.getBlockstackRecord] cmd call return:") @log.debug(ret) end ret = JSON.parse ret @log.debug(ret) lines = ret["zonefile"].split("\n") fz = {} lines.each { |line| recs = line.split(" ") if recs.size == 3 if recs[0] == "A1" || recs[0] == "A2" || recs[0] == "A3" || recs[0] == "A4" || recs[0] == "A5" || recs[0] == "A6" || recs[0] == "A7" || recs[0] == "A8" || recs[0] == "A9" || recs[0] == "A10" || recs[0] == "A11" fz[recs[0]] = recs[2][1..-2] end end } if @log.debug? @log.debug("[Blockstack.getBlockstackRecord] fz:") @log.debug(fz) end if fz.key?("A1") && fz.key?("A2") && fz.key?("A3") && fz.key?("A4") && fz.key?("A5") && fz.key?("A6") && fz.key?("A7") && fz.key?("A8") ecc_pubkey_aligned = fz["A1"] ecc_pubkey = ecc_pubkey_aligned[0..63] +"\n"+ ecc_pubkey_aligned[64..ecc_pubkey_aligned.length] rsa_pubkey = nil if fz["A7"] == "2" || fz["A7"] == "3" # sig versions 2 && 3 if !fz.key?("A9") if @log.error? @log.error("[Blockstack.getBlockstackRecord] no 'A9' field, sig ver is >1") @log.error("-> blockstack_id: #{blockstack_id}") end return nil end rsa_pubkey_aligned = fz["A9"] rsa_pubkey = rsa_pubkey_aligned[0..63] +"\n"+ rsa_pubkey_aligned[64..127] +"\n"+ rsa_pubkey_aligned[128..191] +"\n"+ rsa_pubkey_aligned[192..255] +"\n"+ rsa_pubkey_aligned[256..319] +"\n"+ rsa_pubkey_aligned[320..383] +"\n"+ rsa_pubkey_aligned[384..rsa_pubkey_aligned.length] end output = { "ecc_pubkey" => ecc_pubkey, "rsa_pubkey" => rsa_pubkey, "vchain_id" => fz["A2"], "validator_sig" => Base64.decode64(fz["A3"]), "validator_vchain_id" => fz["A4"], "validator_blockstack_id" => fz["A5"], "vchain_role" => fz["A6"], "sig_version" => fz["A7"], "client_sig" => Base64.decode64(fz["A8"]) } if fz["A7"] == "2" || fz["A7"] == "3" # sig versions 2 && 3 output["client_sig_v2"] = Base64.decode64(fz["A8"]) output["validator_sig_v2"] = Base64.decode64(fz["A3"]) if fz["A7"] == "3" # sig version 3 if !fz.key?("A10") || !fz.key?("A11") if @log.error? @log.error("[Blockstack.getBlockstackRecord] no 'A10' or 'A11' fields, sig ver = 3") @log.error("-> blockstack_id: #{blockstack_id}") end return nil end output["client_sig_v3"] = Base64.decode64(fz["A10"]) output["validator_sig_v3"] = Base64.decode64(fz["A11"]) end end if @log.debug? @log.debug("[Blockstack.getBlockstackRecord] output:") @log.debug(output) end @@recs_cache[blockstack_id] = output @@ecc_keys_cache[blockstack_id] = ecc_pubkey @@rsa_keys_cache[blockstack_id] = rsa_pubkey return output else if @log.error? @log.error("[Blockstack.getBlockstackRecord] no 'A*' field") @log.error("-> blockstack_id: #{blockstack_id}") @log.error("--> blockstack_id: #{blockstack_id}") end end return nil end |
#getPublicKeyECC(blockstack_id, force_refresh = false) ⇒ Object
380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 |
# File 'lib/vchain_client/blockstack_client.rb', line 380 def getPublicKeyECC(blockstack_id, force_refresh=false) if @log.debug? @log.debug("[Blockstack.getPublicKeyECC] input:") @log.debug("-> blockstack_id: #{blockstack_id}") end if !force_refresh if @@ecc_keys_cache.key?(blockstack_id) if @log.debug? @log.debug("[Blockstack.getPublicKeyECC] '#{blockstack_id}' is in a cache") end return @@ecc_keys_cache[blockstack_id] end end if @log.debug? @log.debug("[Blockstack.getPublicKeyECC] '#{blockstack_id}' is not in a cache yet") end begin record = self.getBlockstackRecord(blockstack_id, force_refresh); if record != nil if record.key?("ecc_pubkey") @@ecc_keys_cache[blockstack_id] = record["ecc_pubkey"] return record["ecc_pubkey"] else if @log.error? @log.error("[Blockstack.getPublicKeyECC] record '#{blockstack_id}' doesn't have 'ecc_pubkey' field") @log.error("-> blockstack_id: #{blockstack_id}") @log.error("--> blockstack_id: #{blockstack_id}") end end else if @log.error? @log.error("[Blockstack.getPublicKeyECC] failed to retrieve '#{blockstack_id}' record") @log.error("-> blockstack_id: #{blockstack_id}") @log.error("--> blockstack_id: #{blockstack_id}") end end rescue => e if @log.error? @log.error("[Blockstack.getPublicKeyECC] getBlockstackRecord raised exception:") @log.error("#{e.class}, #{e.message}") @log.error("-> blockstack_id: #{blockstack_id}") @log.error("--> blockstack_id: #{blockstack_id}") end raise e end return nil end |
#getPublicKeyRSA(blockstack_id, force_refresh = false) ⇒ Object
444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 |
# File 'lib/vchain_client/blockstack_client.rb', line 444 def getPublicKeyRSA(blockstack_id, force_refresh=false) if @log.debug? @log.debug("[Blockstack.getPublicKeyRSA] input:") @log.debug("-> blockstack_id: #{blockstack_id}") end if !force_refresh if @@rsa_keys_cache.key?(blockstack_id) if @log.debug? @log.debug("[Blockstack.getPublicKeyRSA] '#{blockstack_id}' is in a cache") end return @@rsa_keys_cache[blockstack_id] end end if @log.debug? @log.debug("[Blockstack.getPublicKeyRSA] '#{blockstack_id}' is not in a cache yet") end begin record = self.getBlockstackRecord(blockstack_id, force_refresh); if record != nil if record.key?("rsa_pubkey") @@rsa_keys_cache[blockstack_id] = record["rsa_pubkey"] return record["rsa_pubkey"] else if @log.error? @log.error("[Blockstack.getPublicKeyRSA] record '#{blockstack_id}' doesn't have 'rsa_pubkey' field") @log.error("-> blockstack_id: #{blockstack_id}") @log.error("--> blockstack_id: #{blockstack_id}") end end else if @log.error? @log.error("[Blockstack.getPublicKeyRSA] failed to retrieve '#{blockstack_id}' record") @log.error("-> blockstack_id: #{blockstack_id}") @log.error("--> blockstack_id: #{blockstack_id}") end end rescue => e if @log.error? @log.error("[Blockstack.getPublicKeyRSA] getBlockstackRecord raised exception:") @log.error("#{e.class}, #{e.message}") @log.error("-> blockstack_id: #{blockstack_id}") @log.error("--> blockstack_id: #{blockstack_id}") end raise e end return nil end |