def generate(secret = None): """ generate a new key object. if the secret argument is specified then generate a deterministic key, otherwise seed the key with a random number to retrieve a random key. """ if secret is None: return ssl.EC_KEY_generate_key(k) else: priv_key = ssl.BN_bin2bn(secret, 32, ssl.BN_new()) group = ssl.EC_KEY_get0_group(k) pub_key = ssl.EC_POINT_new(group) ctx = ssl.BN_CTX_new() if not ssl.EC_POINT_mul(group, pub_key, priv_key, None, None, ctx): lang_grunt.die( "could not derive public key from the supplied secret" # (don't print the secret, for security) ) ssl.EC_KEY_set_private_key(k, priv_key) ssl.EC_KEY_set_public_key(k, pub_key) ssl.EC_POINT_free(pub_key) ssl.BN_CTX_free(ctx) return k
################################################################################ # tests for correct address checksums ################################################################################ addresses = [ "16UwLL9Risc3QfPqBUvKofHmBQ7wMtjvM", "1JBrZXKwJiaYWxQiodh6MBZrbuf2JCftuP", "131TQefUE4w8ksk1N3FP4a2JHcgb3g3hZ7", ] for (i, address) in enumerate(addresses): if verbose: print """ ====================== test for correct address checksums %s =================== """ % i test = btc_grunt.valid_address_checksum(address, True) if test is not True: lang_grunt.die(test) else: if verbose: print "pass" ################################################################################ # tests for incorrect address checksums ################################################################################ # change the second character in each of the above addresses to a Z for i in range(len(addresses)): address_list = list(addresses[i]) address_list[1] = "Z" addresses[i] = "".join(address_list) for (i, address) in enumerate(addresses):
"1d7e-2" ], "tx_size": 3138, "tx_start_pos": 13174 } } } # get the actual result in a single csv string tx_metadata_csv = btc_grunt.tx_metadata_dict2csv(tx_metadata_dict) # now split the resulting csv string into a list - one line per element. this is # necessary to validate the test because the order of the csv lines is arbitrary tx_metadata_csv_list = tx_metadata_csv.split(os.linesep) desired_result = [ "23ab47f962e86d1849fe2e1bdc3e3e5e49373fd8082bbb3792d704eeeaaec40f," \ "4855-31,5,16648355,13174,3138,149912,,,[49f1-2,1d7e-2]", "23ab47a450dd4a8ba00f25041813e42dae7e29508d0ec94980344433088b2861," \ "386d-26,12,12945601,15984,259,183244,,,[,ad9b-1]", "23ab470debadb4dcbe0d78ecf802f3baaafe9924e9beef6e3f1e8303fe9f0664," \ "c9c7-3,3,128407591,731,193,142392,,,[d308-0,3bd9-15]", "23ab470debadb4dcbe0d78ecf802f3baaafe9924e9beef6e3f1e8303fe9f0664," \ "ffff-9,13,111111111,777,193,999999,,,[,]" ] # the order doesn't matter when comparing a set if set(tx_metadata_csv_list) == set(desired_result): print "pass" else: lang_grunt.die("fail: %s" % tx_metadata_csv)
# same seed as test 1 seed2 = "correct horse battery staple" const_hash_bin2 = btc_grunt.sha256(btc_grunt.ascii2bin(seed2)) const_hash_hex2 = btc_grunt.bin2hex(const_hash_bin2) ecdsa_ssl.generate(const_hash_bin2) pubkey_bin2 = ecdsa_ssl.get_pubkey() address2 = btc_grunt.pubkey2address(pubkey_bin2) if address1 == address2: if verbose: print "pass" else: lang_grunt.die( "fail. seed '%s' resulted in a different address this time. address 1:" " %s, address 2: %s" % ( seed2, address1, address2 ) ) ecdsa_ssl.reset() if verbose: print """ ################################################################################ # test 3: (openssl) sign a hash with a private key then verify it with the # public key ################################################################################ """ ecdsa_ssl.init() alice_seed3 = "super secret seed known only by alice"
"block_start_pos": 12945601, "tx_start_pos": 15984, "tx_size": 259, "block_height": 183244, "is_coinbase": None, "is_orphan": None, "spending_txs_list": [None, "ad9b-1"], } } btc_grunt.save_tx_data_to_disk(txhash2, save_data3) # verify that the file now contains the correct data existing_data_csv = btc_grunt.get_tx_metadata_csv(txhash1) # one tx per list item existing_data_dict = btc_grunt.tx_metadata_csv2dict(existing_data_csv) save_data_combined = copy.deepcopy(save_data1) save_data_combined.update(save_data2) expected_data_dict = {txhash1: save_data_combined, txhash2: save_data3} if existing_data_dict == expected_data_dict: print "pass" # clean up the directories since everything is fine shutil.rmtree(btc_grunt.tx_meta_dir) else: # do not clean up the directories, leave for investigation lang_grunt.die( "fail. expected: %s but got %s" % ( os.linesep.join(l.rstrip() for l in json.dumps(expected_data_dict, sort_keys=True, indent=4).splitlines()), os.linesep.join(l.rstrip() for l in json.dumps(existing_data_dict, sort_keys=True, indent=4).splitlines()), ) )
# the user provides addresses in a csv string, but we need a list if options.ADDRESSES is not None: options.ADDRESSES = btc_grunt.explode_addresses(options.ADDRESSES) # initialise the base directory for storing metadata btc_grunt.init_base_dir() btc_grunt.connect_to_rpc() if options.validate is not None: # raise exception if anything is invalid btc_grunt.validate_blockchain(options, inputs_have_been_sanitized) if options.OUTPUT_TYPE is not None: filtered_blocks = btc_grunt.extract_full_blocks( options, inputs_have_been_sanitized ) # filter the required data out of the blocks. returns either a dict of blocks, # a list of txs, or a list of address balances filtered_data = btc_grunt.final_results_filter(filtered_blocks, options) # convert the resulting data to the user-specified format formatted_data = btc_grunt.get_formatted_data(options, filtered_data) # if the user-specified option values result in no data then exit here if formatted_data is None: lang_grunt.die("no results") else: print formatted_data
], "tx_size": 259, "tx_start_pos": 15984 } }, # a real btc tx "23ab47f962e86d1849fe2e1bdc3e3e5e49373fd8082bbb3792d704eeeaaec40f": { "4855-31": { "block_height": 149912, "block_start_pos": 16648355, "blockfile_num": 5, "is_coinbase": None, "is_orphan": None, "spending_txs_list": [ "49f1-2", "1d7e-2" ], "tx_size": 3138, "tx_start_pos": 13174 } } } if tx_metadata_dict == desired_result: print "pass" else: lang_grunt.die("fail: %s" % os.linesep.join( l.rstrip() for l in json.dumps( tx_metadata_dict, sort_keys = True, indent = 4 ).splitlines() ))