def pubkeys_to_basic_stealth_address(scan_pubkey, spend_pubkey, magic_byte=42): # magic_byte = 42 for mainnet, 43 for testnet. hex_scankey = main.encode_pubkey(scan_pubkey, 'hex_compressed') hex_spendkey = main.encode_pubkey(spend_pubkey, 'hex_compressed') hex_data = '00{0:066x}01{1:066x}0100'.format(int(hex_scankey, 16), int(hex_spendkey, 16)) addr = main.hex_to_b58check(hex_data, magic_byte) return addr
privKey = btc.random_key() # 256 bit Random number를 생성한다 dPrivKey = btc.decode_privkey(privKey, 'hex') # 16진수 문자열을 10진수 숫자로 변환한다 if dPrivKey < btc.N: # secp256k1 의 N 보다 작으면 OK break # 개인키로 공개키를 생성한다. pubKey = btc.privkey_to_pubkey(privKey) # 공개키로 지갑 주소를 생성한다. (mainnet 용) address1 = btc.pubkey_to_address(pubKey, 0) # 공개키로 160-bit public key hash를 생성한다 pubHash160 = btc.hash160(btc.encode_pubkey(pubKey, 'bin')) # 160-bit public key hash로 지갑 주소를 생성한다. (위의 address와 동일하다) address2 = btc.hex_to_b58check(pubHash160, 0) # 지갑 주소를 160-bit public key hash로 변환한다. (위의 pubHash160과 동일하다) pubHash1601 = btc.b58check_to_hex(address2) # 공개키로 testnet용 지갑 주소를 생성한다 address3 = btc.pubkey_to_address(pubKey, 0x6f) # 결과 확인 print("\n\n개인키 : ", privKey) print("개인키 --> 공개키 : ", pubKey) print("\n공개키 --> 지갑주소 (1. mainet 용) : ", address1) print("공개키 --> 공개키 해시 : ", pubHash160) print("\n공개키 해시 --> 지갑주소 (2. mainet 용) : ", address2) print("지갑주소 --> 공개키 해시 : ", pubHash1601) print("지갑주소 (Testnet 용) :", address3)
def history(*args): # Valid input formats: history([addr1, addr2,addr3], "btc") # history(addr1, addr2, addr3, "testnet") if len(args) == 0 or (len(args)==1 and args[0] in ('testnet','btc')): return [] addrs, network = parse_addr_args(*args) if network == "btc": txs = [] for addr in addrs: offset = 0 while 1: gathered = False while not gathered: try: data = make_request('https://blockchain.info/address/%s?format=json&offset=%s' % (addr, offset)) gathered = True except Exception as e: try: sys.stderr.write(e.read().strip()) except: sys.stderr.write(str(e)) gathered = False try: jsonobj = json.loads(data) except: raise Exception("Failed to decode data: "+data) txs.extend(jsonobj["txs"]) if len(jsonobj["txs"]) < 50: break offset += 50 sys.stderr.write("Fetching more transactions... "+str(offset)+'\n') outs = {} for tx in txs: for o in tx["out"]: if o.get('addr', None) in addrs: key = str(tx["tx_index"])+':'+str(o["n"]) outs[key] = { "address": o["addr"], "value": o["value"], "output": tx["hash"]+':'+str(o["n"]), "block_height": tx.get("block_height", None) } for tx in txs: # if output is spent adds "spend": "spending_TxID:i" for i, inp in enumerate(tx["inputs"]): if "prev_out" in inp: if inp["prev_out"].get("addr", None) in addrs: key = str(inp["prev_out"]["tx_index"]) + \ ':'+str(inp["prev_out"]["n"]) if outs.get(key): outs[key]["spend"] = tx["hash"] + ':' + str(i) return [outs[k] for k in outs] elif network == "testnet": txs = [] # using https://api.biteasy.com/blockchain/v1/transactions?address=_ADDR_ for addr in addrs: # or offset = 0 while 1: gathered = False while not gathered: try: data = make_request("%s/txs/?address=%s" % (BET_URL, addr)) gathered = True except Exception as e: try: sys.stderr.write(e.read().strip()) except: sys.stderr.write(str(e)) gathered = False try: jsonobj = json.loads(data) except: raise Exception("Failed to decode data: " + data) txs.extend(jsonobj['txs']) #assert addr == jsonobj.get("addrStr"), "Tx data doesn't match address %s" % addr #if len(jsonobj['data']['address']['inout_count_total']) >= 300: # because records=300 # break #offset += 100 #sys.stderr.write("Fetching more transactions... " + str(offset) + '\n') outs = {} from bitcoin.main import hex_to_b58check, btc_to_satoshi for tx in txs: for o in tx.get("vout"): if o.get('scriptPubKey', None) and hex_to_b58check(o.get("scriptPubKey"])["hex"], 111) == addr: key = str(tx["time"]) + ':' + str(o["n"]) outs[key] = { "address": addr, "value": btc_to_satoshi(o["value"]), "output": "{0}:{1}".format(tx["txid"], str(o["n"])), } blkhash = tx.get("blockhash") try: bheight = get_block_height(blkhash, 'testnet') outs[key].update({"block_height": int(bheight)}) except: sys.stderr.write("Couldn't get blockheight for %s.\nUsing blocktime instead" % blkhash) outs[key].update({"block_time": tx.get("time")})
privKey = btc.b58check_to_hex(privKeyWIF) # 개인키로 공개키를 생성한다. pubKey = btc.privkey_to_pubkey(privKey) # 공개키로 160-bit public key hash를 생성한다 pubHash160 = btc.hash160(binascii.unhexlify(pubKey)) # P2SH용 스크립트를 생성한다. script = OP_0 + length + public key hash script = '00' + '14' + pubHash160 # 160 비트 스크립트 해시를 계산한다 scriptHash = btc.hash160(binascii.unhexlify(script)) # 스크립트 해시로 지갑 주소를 생성한다. addr = btc.hex_to_b58check(scriptHash, 0x05) # BIP-173 주소를 생성한다. # 참조: https://github.com/sipa/bech32/blob/master/ref/python/segwit_addr.py witprog = btc.bin_hash160(binascii.unhexlify(pubKey)) bech32Addr = bech32.encode('bc', 0, witprog) # 결과를 확인한다 print("Decode WIF = ", wifDecode) print("Private Key = ", privKey) print("Public Key = ", pubKey) print("Public Key Hash = ", pubHash160) print("Script = ", script) print("ScriptHash = ", scriptHash) print("Address = ", addr) print("Bech32 Address = ", bech32Addr)