def getSignableTxn(parsed, compressed='no'): first, sig, pub, rest = parsed #inputAddr = utils.base58CheckDecode(keyUtils.pubKeyToAddr(pub.decode())) print('first: ', first, 'sig: ', sig, 'pub: ', pub, 'rest: ', rest) #if (compressed=='yes'): # inputAddr = codecs.encode(utils.base58CheckDecode(keyUtils.pubKeyToAddr('03' + pub[2:66],net= 'test', compressed='yes')),'hex').decode() # print('pubk ','03' + pub[2:66] ) #else: # inputAddr = codecs.encode(utils.base58CheckDecode(keyUtils.pubKeyToAddr(pub)),'hex').decode() inputAddr = codecs.encode( utils.base58CheckDecode(keyUtils.pubKeyToAddr(pub)), 'hex').decode() #inputAddr = codecs.encode(utils.base58CheckDecode('moyDyvi7VeAhZnGEWtvE62PoDdmoRXRRkf'),'hex').decode() print('pub uncompressed: ', keyUtils.pubKeyToAddr(pub, net='test')) print('pub compressed: ', keyUtils.pubKeyToAddr('03' + pub[2:66], net='test')) print("inputAddr:", keyUtils.pubKeyToAddr('02' + pub[2:66], net='test')) print( 'a:', codecs.encode( utils.base58CheckDecode('1MMMMSUb1piy2ufrSguNUdFmAcvqrQF8M5'), 'hex').decode()) print( 'b:', codecs.encode( utils.base58CheckDecode('muwc2rRij1XuJZ5JqsevtjCvqMw9CenJfK'), 'hex').decode()) print("inputAddr:", inputAddr) #inputAddr = 'msZwQEA3dYTXDEUjHgfXkGSkLXpfEpLZEA' #print(codecs.encode(inputAddr,'hex').decode()) #return first + "1976a914" + inputAddr.encode('hex') + "88ac" + rest + "01000000" return first.encode('utf-8') + b"1976a914" + inputAddr.encode( 'utf-8') + b"88ac" + rest.encode('utf-8') + b"01000000"
def getSignableTxn(parsed, compressed='no'): first, sig, pub, rest = parsed #inputAddr = utils.base58CheckDecode(keyUtils.pubKeyToAddr(pub.decode())) print('first: ', first, 'sig: ', sig, 'pub: ', pub, 'rest: ', rest) #if (compressed=='yes'): # inputAddr = codecs.encode(utils.base58CheckDecode(keyUtils.pubKeyToAddr('03' + pub[2:66],net= 'test', compressed='yes')),'hex').decode() # print('pubk ','03' + pub[2:66] ) #else: # inputAddr = codecs.encode(utils.base58CheckDecode(keyUtils.pubKeyToAddr(pub)),'hex').decode() inputAddr = codecs.encode(utils.base58CheckDecode(keyUtils.pubKeyToAddr(pub)),'hex').decode() #inputAddr = codecs.encode(utils.base58CheckDecode('moyDyvi7VeAhZnGEWtvE62PoDdmoRXRRkf'),'hex').decode() print('pub uncompressed: ', keyUtils.pubKeyToAddr(pub,net='test')) print('pub compressed: ', keyUtils.pubKeyToAddr('03' + pub[2:66],net='test')) print("inputAddr:", keyUtils.pubKeyToAddr('02' +pub[2:66],net= 'test')) print('a:', codecs.encode(utils.base58CheckDecode('1MMMMSUb1piy2ufrSguNUdFmAcvqrQF8M5'),'hex').decode()) print('b:', codecs.encode(utils.base58CheckDecode('muwc2rRij1XuJZ5JqsevtjCvqMw9CenJfK'),'hex').decode()) print("inputAddr:", inputAddr) #inputAddr = 'msZwQEA3dYTXDEUjHgfXkGSkLXpfEpLZEA' #print(codecs.encode(inputAddr,'hex').decode()) #return first + "1976a914" + inputAddr.encode('hex') + "88ac" + rest + "01000000" return first.encode('utf-8') + b"1976a914" + inputAddr.encode('utf-8') + b"88ac" + rest.encode('utf-8') + b"01000000"
def getSignableTxn(parsed): first, sig, pub, rest = parsed inputAddr = utils.base58CheckDecode(keyUtils.pubKeyToAddr(pub)) return first + "1976a914" + inputAddr.encode( 'hex') + "88ac" + rest + "01000000"
def getSignableTxn(parsed): first, sig, pub, rest = parsed inputAddr = utils.base58CheckDecode(keyUtils.pubKeyToAddr(pub)) return first + "1976a914" + inputAddr.encode('hex') + "88ac" + rest + "01000000"
base58 = base58encode(rawHexToDecimal(s)) #Convert to base58 #Count the zeros numOfZeros = 0 for c in s: if (c == "0"): numOfZeros+=1 return ("1" * numOfZeros) + base58 #Adds the base58 ones to pad it out # https://en.bitcoin.it/wiki/Wallet_import_format def privateKeyToWif(key_hex): result = base58CheckEncode(0x80, key_hex.decode('hex')) print("Base58check received raw hex for both parameters and returned " + str(len(result)) + " long, were the type was " +str(type(result))) return result wif = privateKeyToWif(private_key) print("WIF key is " + str(len(wif)) + " long " + str(type(wif)) + " content: " + str(wif)) #Getting out address from our public key def pubKeyToAddr(s): return base58CheckEncode(0, hash160(s)) Address = keyUtils.pubKeyToAddr(public_key) print("Address is " + str(len(Address)) + " long " + str(type(Address)) + " content: " + str(Address)) ##
checksum = hashlib.sha256(hashlib.sha256(s).digest()).digest()[0:4] print(' ', hashlib.sha256(hashlib.sha256(s).digest()).hexdigest()[0:8]) print('8 - Adding 7 at the end of 4') result = s + checksum print(' ', codecs.encode(result, 'hex').decode().upper()) resultstr = codecs.encode(result, 'hex').decode().upper() print('9 - Base58 encoding of 8') leadingZeros = utils.countLeadingChars(result, 0) print(' ', '1' * leadingZeros + utils.base58encode(utils.base256decode(result))) print( ' Addr: ', keyUtils.pubKeyToAddr(codecs.encode( keyUtils.privateKeyToPublicKey(private_key, net=net, compressed=compressed), 'hex').decode(), net=net, compressed=compressed)) print( ' Addr: ', keyUtils.pubKeyToAddr(codecs.encode( keyUtils.privateKeyToPublicKey(private_key, net=net, compressed='yes'), 'hex').decode(), net=net, compressed='yes')) print( ' Addr: ', keyUtils.pubKeyToAddr(codecs.encode( keyUtils.privateKeyToPublicKey(private_key, net=net, compressed='no'), 'hex').decode(), net=net,
k=keyUtils.wifToPrivateKey("5J3mBbAH58CpQ3Y5RNJpUKPE62SQ5tfcvU2JpbnkeyhfsYB1Jcn") print('privk ', codecs.encode(k,'hex').decode().upper()) print('pk', keyUtils.privateKeyToPublicKey(codecs.encode(k,'hex').decode()) ) pk = keyUtils.privateKeyToPublicKey(codecs.encode(k,'hex').decode()) print('pubk ', codecs.encode(pk, 'hex').decode() ) print('addr ', keyUtils.keyToAddr("754580de93eea21579441b58e0c9b09f54f6005fc71135f5cfac027394b22caa")) k=keyUtils.wifToPrivateKey("KzTg2wn6Z8s7ai5NA9MVX4vstHRsqP26QKJCzLg4JvFrp6mMaGB9") print('privk ', codecs.encode(k,'hex').decode().upper()) #print('pk', keyUtils.privateKeyToPublicKey(codecs.encode(k,'hex').decode()) ) pk = keyUtils.privateKeyToPublicKey(codecs.encode(k,'hex').decode()) print('pubk ', codecs.encode(pk, 'hex').decode() ) print('addr ', keyUtils.pubKeyToAddr("0328592df0ad9de33919e38caa7ff567b708699f9c2f67d8e91ff7185f1e8158b3")) k=keyUtils.wifToPrivateKey("cQmNq39nKXu1FbTE4uCX2PfpE4Q3K3Svxf9gAZEeRWTQSZYvLrTQ") print('privk ', codecs.encode(k,'hex').decode().upper()) #print('pk', keyUtils.privateKeyToPublicKey(codecs.encode(k,'hex').decode()) ) pk = keyUtils.privateKeyToPublicKey(codecs.encode(k,'hex').decode(),net='test') print('pubk ', codecs.encode(pk, 'hex').decode() ) print('addr ', keyUtils.pubKeyToAddr("037c8c83ca259ce05554ab7dfe7c46482b9595c5a092f592d961b686a1c4768c82", net='test')) print("abcdef",type("abcdef")) print( "abcdef".encode("utf-8"), type("abcdef".encode("utf-8"))) print( codecs.decode("abcdef".encode("utf-8"), "hex"), ) print( codecs.encode("abcdef".encode("utf-8"), "hex"), ) print(codecs.encode(codecs.decode("abcdef".encode("utf-8"), "hex"), "hex"))
print 'C library' from Crypto.Hash import RIPEMD hash = RIPEMD.new() pkHashStep00 = hashlib.sha256(public_key.decode('hex')).digest() hash.update(pkHashStep00) cdigest = hash.digest() pkHashStep01 = utils.base58CheckEncode(0, cdigest) print 'MyTransaction0: pkHash calculated from clib(publickey) {}'.format( pkHashStep01) print '' print '' print '' print '' print 'Method 1' pkHash10 = keyUtils.pubKeyToAddr(public_key) print 'MyTransaction1: pkHash calulated using KeyUtils lib {}'.format(pkHash10) print '' print '' print '' print 'Method 2' pkHashStep20 = hashlib.sha256(public_key.decode('hex')).digest() ripemd1602 = hashlib.new('ripemd160') ripemd1602.update(pkHashStep20) ripemd160digest = ripemd1602.digest() pkHashStep21 = utils.base58CheckEncode(0, ripemd160digest) print 'MyTransaction2: pkHash calculated from ripemd160 hashlib {}'.format( pkHashStep21) print ''
print('4 - Adding network bytes to 3') if (net == 'main'): nb = b'\0' else: nb = b'o' nb = bytes(((111,))) s = nb + ripemd160.digest() print(' ',codecs.encode(s, "hex").decode().upper()) print('5 - SHA-256 hash of 4') first_hash = hashlib.sha256(s).hexdigest() print (' ',first_hash) print('6 - SHA-256 hash of 5') second_hash = hashlib.sha256(codecs.decode(first_hash.encode("utf-8"), "hex")).hexdigest() print (' ',second_hash.upper()) print('7 - First four bytes of 6') checksum = hashlib.sha256(hashlib.sha256(s).digest()).digest()[0:4] print (' ',hashlib.sha256(hashlib.sha256(s).digest()).hexdigest()[0:8]) print('8 - Adding 7 at the end of 4') result = s + checksum print (' ',codecs.encode(result,'hex').decode().upper()) resultstr = codecs.encode(result,'hex').decode().upper() print('9 - Base58 encoding of 8') leadingZeros = utils.countLeadingChars(result, 0) print ( ' ', '1' * leadingZeros + utils.base58encode(utils.base256decode( result ))) print (' Addr: ' , keyUtils.pubKeyToAddr(codecs.encode(keyUtils.privateKeyToPublicKey(private_key, net = net, compressed=compressed),'hex').decode(), net = net,compressed=compressed)) print (' Addr: ' , keyUtils.pubKeyToAddr(codecs.encode(keyUtils.privateKeyToPublicKey(private_key, net = net, compressed='yes'),'hex').decode(), net = net, compressed='yes')) print (' Addr: ' , keyUtils.pubKeyToAddr(codecs.encode(keyUtils.privateKeyToPublicKey(private_key, net = net, compressed='no'),'hex').decode(), net = net, compressed='no')) print (' Addr: ' , keyUtils.pubKeyToAddr("025c0de3b9c8ab18dd04e3511243ec2952002dbfadc864b9628910169d9b9b00ec", net = net, compressed='yes')) print('pubk ',codecs.encode(keyUtils.privateKeyToPublicKey(private_key, net = net, compressed='yes'),'hex').decode())
print "Clave privada en formato hexadecimal: " + str(private_key) wif_private_key = keyUtils.privateKeyToWif(private_key) print "Clave privada en formato WIF: " + str(wif_private_key) ### Clave publica desde la clave privada public_key = keyUtils.privateKeyToPublicKey(private_key) print "Clave publica en formato hexadecimal: " + str(public_key) print "Nota: la clave publica es dos veces mas larga (un poquito mas) que la clave privada" print "|private_key| = " + str(len(private_key)) print "|public_key| = " + str(len(public_key)) #### Direccion desde la clave publica bitcoin_address = keyUtils.pubKeyToAddr(public_key) print "Direccion bitcoin: " + bitcoin_address ### Comparar con brainwallet # brainwallet.org ### Enviar dinero a esa direccion ### Transacciones ###################################################################### # Concepto #https://lh4.googleusercontent.com/-FX_lwaangsI/UuNVjoFa4jI/AAAAAAAAWZU/NMeJZDHe6EA/s800/transaction-diagram.png #Formato transaccion #http://www.righto.com/2014/02/bitcoins-hard-way-using-raw-bitcoin.html => Structure of the example Bitcoin transaction. #Script
#392B964E911955ED50E4E368A9476BC3F9DCC134280E15636430EB91145DAB739F0D68B82CF33003379D885A0B212AC95E9CDDFD2D391807934D25995468BC55 pubKey = keyUtils.privateKeyToPublicKey(privateKey) print "Sender's Public Key: ", pubKey #public keys is hashed ripemd160 = hashlib.new('ripemd160') ripemd160.update(hashlib.sha256(pubKey.decode('hex')).digest()) digest = (ripemd160.digest()).encode('hex') print "Digest:", digest #167c74f7491fe552ce9e1912810a984355b8ee07 #scriptPubKey from hash public key scriptPubKey = '76a914' + digest + '88ac' #public key is hashed and then base58 encoded publKeyToAddress = utils.base58CheckEncode(0, digest.decode('hex')) pubKeyToAddress = keyUtils.pubKeyToAddr(pubKey) print "publKeyToAddress: ", publKeyToAddress #133txdxQmwECTmXqAr9RWNHnzQ175jGb7e print "pubKeyToAddress: ", pubKeyToAddress #133txdxQmwECTmXqAr9RWNHnzQ175jGb7e signed_txn = txnUtils.makeSignedTransaction( privateKey, outputTransactionHash, 0, scriptPubKey, [[00001, keyUtils.addrHashToScriptPubKey(addrHash)]]) txnBuffer.append(signed_txn) #fo = open("txnBuffer.txt","wb") #for item in txnBuffer: # fo.write("%s\n" % item) #itemlist = ['a','b','c'] with open('txnBuffer.txt', 'wb') as fp:
print( 'addr ', keyUtils.keyToAddr( "754580de93eea21579441b58e0c9b09f54f6005fc71135f5cfac027394b22caa")) k = keyUtils.wifToPrivateKey( "KzTg2wn6Z8s7ai5NA9MVX4vstHRsqP26QKJCzLg4JvFrp6mMaGB9") print('privk ', codecs.encode(k, 'hex').decode().upper()) #print('pk', keyUtils.privateKeyToPublicKey(codecs.encode(k,'hex').decode()) ) pk = keyUtils.privateKeyToPublicKey(codecs.encode(k, 'hex').decode()) print('pubk ', codecs.encode(pk, 'hex').decode()) print( 'addr ', keyUtils.pubKeyToAddr( "0328592df0ad9de33919e38caa7ff567b708699f9c2f67d8e91ff7185f1e8158b3")) k = keyUtils.wifToPrivateKey( "cQmNq39nKXu1FbTE4uCX2PfpE4Q3K3Svxf9gAZEeRWTQSZYvLrTQ") print('privk ', codecs.encode(k, 'hex').decode().upper()) #print('pk', keyUtils.privateKeyToPublicKey(codecs.encode(k,'hex').decode()) ) pk = keyUtils.privateKeyToPublicKey(codecs.encode(k, 'hex').decode(), net='test') print('pubk ', codecs.encode(pk, 'hex').decode()) print( 'addr ', keyUtils.pubKeyToAddr( "037c8c83ca259ce05554ab7dfe7c46482b9595c5a092f592d961b686a1c4768c82", net='test'))