def makeSignedTransaction(privateKey, outputTransactionHash, sourceIndex, scriptPubKey, outputs): myTxn_forSig = (makeRawTransaction(outputTransactionHash, sourceIndex, scriptPubKey, outputs) + "01000000" ) # hash code s256 = hashlib.sha256(hashlib.sha256( myTxn_forSig.decode('hex')).digest()).digest() sk = ecdsa.SigningKey.from_string(privateKey.decode('hex'), curve=ecdsa.SECP256k1) sig = sk.sign_digest( s256, sigencode=ecdsa.util.sigencode_der) + '\01' # 01 is hashtype pubKey = keyUtils.privateKeyToPublicKey(privateKey) scriptSig = utils.varstr(sig).encode('hex') + utils.varstr( pubKey.decode('hex')).encode('hex') signed_txn = makeRawTransaction(outputTransactionHash, sourceIndex, scriptSig, outputs) verifyTxnSignature(signed_txn) return signed_txn
def makeSignedTransaction(privateKey, outputTransactionHash, sourceIndex, scriptPubKey, outputs): myTxn_forSig = (makeRawTransaction(outputTransactionHash, sourceIndex, scriptPubKey, outputs) + "01000000" ) # hash code print "priv", privateKey print "tx", myTxn_forSig # signing with ecdsa module s256 = hashlib.sha256(hashlib.sha256( myTxn_forSig.decode('hex')).digest()).digest() sk = ecdsa.SigningKey.from_string(privateKey.decode('hex'), curve=ecdsa.SECP256k1) while True: sig = sk.sign_digest(s256, sigencode=ecdsa.util.sigencode_der) print is_bip66(binascii.hexlify(sig)) if IsLowDERSignature(bytearray.fromhex(binascii.hexlify(sig))): break sig = sig + '\01' # sig = pybitcointools_sig(s256, privateKey) + '01' # sig = sig.decode('hex') print "sig", len(sig), [binascii.hexlify(sig)] pubKey = keyUtils.privateKeyToPublicKey(privateKey, True) scriptSig = utils.varstr(sig).encode('hex') + utils.varstr( pubKey.decode('hex')).encode('hex') signed_txn = makeRawTransaction(outputTransactionHash, sourceIndex, scriptSig, outputs) print "signed_txn", signed_txn verifyTxnSignature(signed_txn) return signed_txn
def createAddress(private_key): # print 'null' # private_key ''.join(['%x' % random.randrange(16) for x in range(0, 64)]) public_key = keyUtils.privateKeyToPublicKey(private_key) address = keyUtils.keyToAddr(private_key) wif = keyUtils.privateKeyToWif(private_key) return public_key
def makeSignedTransaction( privateKey, outputTransactionHash, sourceIndex, scriptPubKey, outputs, net='main', compressed='no'): #Testnet myTxn_forSig = (makeRawTransaction(outputTransactionHash, sourceIndex, scriptPubKey, outputs) + b"01000000") # hash code #myTxn_forSig = codecs.decode(myTxn_forSig.encode('utf-8'),'hex') s256 = hashlib.sha256(hashlib.sha256( codecs.decode(myTxn_forSig,'hex') ).digest()).digest() sk = ecdsa.SigningKey.from_string(codecs.decode(privateKey.encode('utf-8'),'hex'), curve=ecdsa.SECP256k1) sig = sk.sign_digest(s256, sigencode=ecdsa.util.sigencode_der) + b'\x01' # 01 is hashtype pubKey = keyUtils.privateKeyToPublicKey(privateKey, net=net, compressed =compressed) pubKey2 = codecs.encode(keyUtils.privateKeyToPublicKey(privateKey, net= net,compressed ='no'),'hex').decode() print('pubKey : ', codecs.encode(pubKey,'hex').decode()) print('pubKey2: ', pubKey2) #scriptSig = utils.varstr(sig).encode('hex') + utils.varstr(pubKey.decode('hex')).encode('hex') scriptSig = codecs.encode(utils.varstr(sig),'hex').decode() + codecs.encode(utils.varstr(pubKey),'hex').decode() signed_txn = makeRawTransaction(outputTransactionHash, sourceIndex, scriptSig, outputs) print('compressed mst:',compressed) verifyTxnSignature(signed_txn.decode(),compressed=compressed, pubk=pubKey2) return signed_txn.decode()
def makeSignedTransaction(privateKey, outputTransactionHash, sourceIndex, scriptPubKey, outputs): myTxn_forSig = (makeRawTransaction(outputTransactionHash, sourceIndex, scriptPubKey, outputs) + "01000000") # hash code s256 = hashlib.sha256(hashlib.sha256(myTxn_forSig.decode('hex')).digest()).digest() sk = ecdsa.SigningKey.from_string(privateKey.decode('hex'), curve=ecdsa.SECP256k1) sig = sk.sign_digest(s256, sigencode=ecdsa.util.sigencode_der) + '\01' # 01 is hashtype pubKey = keyUtils.privateKeyToPublicKey(privateKey) scriptSig = utils.varstr(sig).encode('hex') + utils.varstr(pubKey.decode('hex')).encode('hex') signed_txn = makeRawTransaction(outputTransactionHash, sourceIndex, scriptSig, outputs) verifyTxnSignature(signed_txn) return signed_txn
def makeSignedTransaction(privateKey, outputTransactionHash, sourceIndex, scriptPubKey, outputs, net='main', compressed='no'): #Testnet myTxn_forSig = (makeRawTransaction(outputTransactionHash, sourceIndex, scriptPubKey, outputs) + b"01000000" ) # hash code #myTxn_forSig = codecs.decode(myTxn_forSig.encode('utf-8'),'hex') s256 = hashlib.sha256( hashlib.sha256(codecs.decode(myTxn_forSig, 'hex')).digest()).digest() sk = ecdsa.SigningKey.from_string(codecs.decode(privateKey.encode('utf-8'), 'hex'), curve=ecdsa.SECP256k1) sig = sk.sign_digest( s256, sigencode=ecdsa.util.sigencode_der) + b'\x01' # 01 is hashtype pubKey = keyUtils.privateKeyToPublicKey(privateKey, net=net, compressed=compressed) pubKey2 = codecs.encode( keyUtils.privateKeyToPublicKey(privateKey, net=net, compressed='no'), 'hex').decode() print('pubKey : ', codecs.encode(pubKey, 'hex').decode()) print('pubKey2: ', pubKey2) #scriptSig = utils.varstr(sig).encode('hex') + utils.varstr(pubKey.decode('hex')).encode('hex') scriptSig = codecs.encode(utils.varstr(sig), 'hex').decode() + codecs.encode( utils.varstr(pubKey), 'hex').decode() signed_txn = makeRawTransaction(outputTransactionHash, sourceIndex, scriptSig, outputs) print('compressed mst:', compressed) verifyTxnSignature(signed_txn.decode(), compressed=compressed, pubk=pubKey2) return signed_txn.decode()
print(' WIF:', utils.base58encode(utils.base256decode(result))) print(' WIF:', keyUtils.privateKeyToWif(private_key, net=net, compressed='yes')) print(' WIF:', keyUtils.privateKeyToWif(private_key, net=net, compressed='no')) #https://en.bitcoin.it/wiki/Technical_background_of_version_1_Bitcoin_addresses print('****************************************') print('***** Private key to Bitcoin Address ***') print('****************************************') print('0 - Private ECDSA Key') #private_key = '18E14A7B6A307F426A94F8114701E7C8E774E7F9A47E2C2035DB29A206321725' #private_key = '1E99423A4ED27608A15A2616A2B0E9E52CED330AC530EDCC32C8FFC6A526AEDD' print(' ', private_key) public_key = keyUtils.privateKeyToPublicKey(private_key, net=net) print('1 - Public ECDSA Key') print(' ', codecs.encode(public_key, 'hex').decode().upper()) print('2 - SHA-256 hash of 1') first_hash = hashlib.sha256(public_key).hexdigest() print(' ', first_hash) print('3 - RIPEMD-160 Hash of 2') ripemd160 = hashlib.new('ripemd160') ripemd160.update(hashlib.sha256(public_key).digest()) print(' ', ripemd160.hexdigest()) print('4 - Adding network bytes to 3') if (net == 'main'): nb = b'\0' else: nb = b'o' nb = bytes(((111, )))
keyUtils.addrHashToScriptPubKey("mwAnSj8gvAkDHbW5wTN67DRQETdmTVDdHz").decode() keyUtils.addrHashToScriptPubKey("moRsbz4GMe99KFSzq8XsSfkS2gmPfQ3GpC").decode() 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"))
#print(utils.base256decode( result )) print (' WIF:', utils.base58encode(utils.base256decode( result ))) print (' WIF:', keyUtils.privateKeyToWif( private_key , net=net, compressed='yes')) print (' WIF:', keyUtils.privateKeyToWif( private_key , net=net, compressed='no')) #https://en.bitcoin.it/wiki/Technical_background_of_version_1_Bitcoin_addresses print('****************************************') print('***** Private key to Bitcoin Address ***') print('****************************************') print('0 - Private ECDSA Key') #private_key = '18E14A7B6A307F426A94F8114701E7C8E774E7F9A47E2C2035DB29A206321725' #private_key = '1E99423A4ED27608A15A2616A2B0E9E52CED330AC530EDCC32C8FFC6A526AEDD' print(' ',private_key) public_key = keyUtils.privateKeyToPublicKey(private_key, net = net) print('1 - Public ECDSA Key') print(' ',codecs.encode(public_key,'hex').decode().upper()) print('2 - SHA-256 hash of 1') first_hash = hashlib.sha256(public_key).hexdigest() print (' ',first_hash) print('3 - RIPEMD-160 Hash of 2') ripemd160 = hashlib.new('ripemd160') ripemd160.update(hashlib.sha256(public_key).digest()) print(' ',ripemd160.hexdigest()) print('4 - Adding network bytes to 3') if (net == 'main'): nb = b'\0' else: nb = b'o' nb = bytes(((111,)))
## ECDSA ### #### Clave privada = secuencia aleatoria de 256 bits. Sin embargo se representa usando mas bits #### para poder detectar errores. Ver http://bitcoin.stackexchange.com/questions/3041/what-is-a-130-hex-character-public-key # Warning: this random function is not cryptographically strong and is just for example private_key = ''.join(['%x' % random.randrange(16) for x in range(0, 64)]) #private_key = "df2c58b37905768fd39402f74f577a2c4c27ff82dd7c8a3cc0312122dc6f081d" 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
my_list = [ 0, 17, 34, 51, 68, 85, 102, 119, -120, -103, -86, -69, -52, -35, -18, -1 ] digits = "0123456789abcdef" for nums in my_list: print(nums, nums & 0xff, nums & 0xff >> 4, (nums & 0xff) & 0xf, digits[nums & 0xff >> 4], digits[(nums & 0xff) & 0xf], 0xff >> 4, 0xf) keyUtils.addrHashToScriptPubKey("mwAnSj8gvAkDHbW5wTN67DRQETdmTVDdHz").decode() keyUtils.addrHashToScriptPubKey("moRsbz4GMe99KFSzq8XsSfkS2gmPfQ3GpC").decode() 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())