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 buildScriptSig(privateKey, doubleSHA256_RawTransaction, hashtype): sk = ecdsa.SigningKey.from_string(privateKey.decode('hex'), curve=ecdsa.SECP256k1) # 01 is hashtype sig = sk.sign_digest(doubleSHA256_RawTransaction, sigencode=ecdsa.util.sigencode_der) + hashtype.decode('hex') pubKey = publicKey.getECDAPublicKeyWithPrefix(publicKey.BITCOIN_PROTOCOL_PUBLIC_KEY_PREFIX, privateKey) scriptSig = utils.varstr(sig).encode('hex') + utils.varstr(pubKey.decode('hex')).encode('hex') return scriptSig
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()
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 sendVersionMsg(self): version = Connector.VERSION services = 1 timestamp = int(time.time()) addrRecv = b'\x00' * 26 addrFrom = b'\x00' * 26 nonce = struct.unpack('<Q', os.urandom(8))[0] userAgent = utils.varstr('') startHeight = 0 payload = struct.pack('<LQQ26s26sQsL', version, services, timestamp, addrRecv, addrFrom, nonce, userAgent, startHeight) self.sock.send(Connector.createMsg('version', payload))
def buildVersionMessage(networkType, portNumber): # This format of Network Address applies to version message because it # does not have the timestamp in the prefix def formatVersionNetworkAddress(timestamp, service, ipAddressV4, portNumber): ipAddressV4ByteString = ("".join([ ("%02x" % int(octet)) for octet in ipAddressV4.split(".") ])).decode("hex") networkAddress = [] networkAddress.append(struct.pack("<Q", service)) networkAddress.append(struct.pack("<10sH", "", 0xffff)) networkAddress.append(struct.pack(">4sH", ipAddressV4ByteString, portNumber)) formattedNetworkAddress = "".join(networkAddress) return (formattedNetworkAddress) # Version: Identifies protocol version being used by the node protocolVersion = 70001 #print protocolVersion.encode("hex") # Service: 1 - NODE_NETWORK service = 1 timestamp = int(time.time()) addressReceived = formatVersionNetworkAddress(timestamp, service, "127.0.0.1", portNumber) addressFrom = formatVersionNetworkAddress(timestamp, service, "127.0.0.1", portNumber) nonce = random.getrandbits(64) userAgent = utils.varstr("") startHeight = 0 relay = True versionMessagePayload = [] versionMessagePayload.append(struct.pack("<LQQ", protocolVersion, service, timestamp)) versionMessagePayload.append(struct.pack("<26s26s", addressReceived, addressFrom)) versionMessagePayload.append(struct.pack("<Q",nonce)) versionMessagePayload.append(struct.pack("<s",userAgent)) versionMessagePayload.append(struct.pack("<L",startHeight)) versionMessagePayload.append(struct.pack("<?",relay)) formattedVersionMessagePayload = "".join(versionMessagePayload) return buildMessage(networkType, "version", formattedVersionMessagePayload)
def getVersionMsg(): version = 60002 services = 1 timestamp = int(time.time()) addr_me = utils.netaddr(socket.inet_aton("127.0.0.1"), 8333) addr_you = utils.netaddr(socket.inet_aton("127.0.0.1"), 8333) nonce = random.getrandbits(64) sub_version_num = utils.varstr('') start_height = 0 payload = struct.pack('<LQQ26s26sQsL', version, services, timestamp, addr_me, addr_you, nonce, sub_version_num, start_height) return makeMessage(magic, 'version', payload)
def buildVersionMessage(networkType, portNumber): # This format of Network Address applies to version message because it # does not have the timestamp in the prefix def formatVersionNetworkAddress(timestamp, service, ipAddressV4, portNumber): ipAddressV4ByteString = ("".join([ ("%02x" % int(octet)) for octet in ipAddressV4.split(".") ])).decode("hex") networkAddress = [] networkAddress.append(struct.pack("<Q", service)) networkAddress.append(struct.pack("<10sH", "", 0xffff)) networkAddress.append( struct.pack(">4sH", ipAddressV4ByteString, portNumber)) formattedNetworkAddress = "".join(networkAddress) return (formattedNetworkAddress) # Version: Identifies protocol version being used by the node protocolVersion = 70001 #print protocolVersion.encode("hex") # Service: 1 - NODE_NETWORK service = 1 timestamp = int(time.time()) addressReceived = formatVersionNetworkAddress(timestamp, service, "127.0.0.1", portNumber) addressFrom = formatVersionNetworkAddress(timestamp, service, "127.0.0.1", portNumber) nonce = random.getrandbits(64) userAgent = utils.varstr("") startHeight = 0 relay = True versionMessagePayload = [] versionMessagePayload.append( struct.pack("<LQQ", protocolVersion, service, timestamp)) versionMessagePayload.append( struct.pack("<26s26s", addressReceived, addressFrom)) versionMessagePayload.append(struct.pack("<Q", nonce)) versionMessagePayload.append(struct.pack("<s", userAgent)) versionMessagePayload.append(struct.pack("<L", startHeight)) versionMessagePayload.append(struct.pack("<?", relay)) formattedVersionMessagePayload = "".join(versionMessagePayload) return buildMessage(networkType, "version", formattedVersionMessagePayload)
def getVersionMsg(): version = 70002 services = 1 timestamp = int(time.time()) addr_me = utils.netaddr(socket.inet_aton("127.0.0.1"), 8333) addr_you = utils.netaddr(socket.inet_aton("127.0.0.1"), 8333) nonce = random.getrandbits(64) sub_version_num = utils.varstr('') start_height = 0 payload = struct.pack('<LQQ26s26sQsL', version, services, timestamp, addr_me, addr_you, nonce, sub_version_num, start_height) return makeMessage(magic, 'version', payload)
def getVersionMsg(): # version(4), services(8), timestamp(8), addr_me(26), addr_you(26), nonce(8) # sub_version_num (var_str), start_height(4) version = 60002 services = 1 timestamp = int(time.time()) #addr_me = utils.netaddr(socket.inet_aton("127.0.0.1"), 8333) #addr_you = utils.netaddr(socket.inet_aton("127.0.0.1"), 8333) addr_me = utils.netaddr(socket.inet_aton("127.0.0.1"), 19000) addr_you = utils.netaddr(socket.inet_aton("127.0.0.1"), 19001) nonce = random.getrandbits(64) sub_version_num = utils.varstr(b'') start_height = 0 payload = struct.pack('<LQQ26s26sQsL', version, services, timestamp, addr_me, addr_you, nonce, sub_version_num, start_height) return makeMessage(magic, 'version', payload)
scriptPubKeyPreviousOutput = keyUtils.addrHashToScriptPubKey(bitcoin_address) transaction_no_firmada = txnUtils.makeRawTransaction(output_transaction_hash , sourceIndex, scriptPubKeyPreviousOutput, outputs) + "01000000" # hash code #ENREDO => como firmar la transaccion si la transaccion ya tiene la firma... #Primero se reemplaza ScriptSig por ScriptPubKey luego se calcula la firma de #todo esto y se reemplaza ScriptPubKey #Armando la transaccion firmada s256 = hashlib.sha256(hashlib.sha256(transaction_no_firmada.decode('hex')).digest()).digest() #Doble hash (curioso :)) sk = ecdsa.SigningKey.from_string(private_key.decode('hex'), curve=ecdsa.SECP256k1) sig = sk.sign_digest(s256, sigencode=ecdsa.util.sigencode_der) + '\01' # 01 is hashtype pubKey = keyUtils.privateKeyToPublicKey(private_key) scriptSig = utils.varstr(sig).encode('hex') + utils.varstr(pubKey.decode('hex')).encode('hex') # <sig> <PubKey> #Se reemplaza ScriptPubKey por scriptSig en el input signed_txn = txnUtils.makeRawTransaction(output_transaction_hash, sourceIndex, scriptSig, outputs) #Chequeando que esta todo bien txnUtils.verifyTxnSignature(signed_txn) print "Transaccion firmada, lista para ser publicada: " + signed_txn #Publicar la transaccion #http://btc.blockr.io/tx/push #O cliente bitcoin