Exemplo n.º 1
0
def verifyMultiSigSignature(tx, inputIndex, pubKeys, key, signature):
	"""
	Verify a signature for a transaction that spends a 2-of-2 multi-signature
	output.

	Arguments:
	tx: Transaction; the transaction
	inputIndex: int; the index of the transaction input to which a signature
	            applies
	pubKeys: sequence of str; the public keys
	         2 <= len(pubKeys) <= 16
	key: Key; the public key used for signing (should correspond to one of the
	     keys in pubKeys)
	signature: str; the signature, including the hash type

	Return value:
	bool; indicates whether the signature is correct (True) or not (False)

	Exceptions:
	Exception: signature verification failed
	"""

	hashType = struct.unpack('B', signature[-1])[0] #uint8_t
	if hashType != 1: #SIGHASH_ALL
		return False
	signature = signature[:-1]
	scriptPubKey = Script.multiSigPubKey(pubKeys)
	bodyHash = tx.getSignatureBodyHash(inputIndex, scriptPubKey, hashType)
	return key.verify(bodyHash, signature)
Exemplo n.º 2
0
def makeSpendMultiSigTransaction(outputHash, outputIndex, amount, toHash, fee):
	"""
	Make an (non-signed) transaction to send funds from a 2-of-2 multi-signature
	output to a standard Bitcoin scriptPubKey ("send to Bitcoin address").
	The transaction is returned, and is NOT published on the Bitcoin network by
	this function.

	Arguments:
	outputHash: str; the transaction ID of the previous output transaction
	            Note that the byte order is the reverse as shown in Bitcoin.
	outputIndex: int; the index of the output in the previous output transaction
	amount: int; the amount to be sent, including the fee (in Satoshi).
	        This must be equal to the amount in the input.
	toHash: str; the SHA256- and RIPEMD160-hashed public key of the receiver
	        (equivalent to the bitcoin address)
	fee: int; the transaction fee (in Satoshi)

	Return value:
	Transaction; the transaction that sends funds as specified.
	"""

	tx = Transaction(
		tx_in = [TxIn(outputHash, outputIndex)],
		tx_out = [TxOut(amount-fee, Script.standardPubKey(toHash))]
		)

	return tx
Exemplo n.º 3
0
def verifyMultiSigSignature(tx, inputIndex, pubKeys, key, signature):
    """
	Verify a signature for a transaction that spends a 2-of-2 multi-signature
	output.

	Arguments:
	tx: Transaction; the transaction
	inputIndex: int; the index of the transaction input to which a signature
	            applies
	pubKeys: sequence of str; the public keys
	         2 <= len(pubKeys) <= 16
	key: Key; the public key used for signing (should correspond to one of the
	     keys in pubKeys)
	signature: str; the signature, including the hash type

	Return value:
	bool; indicates whether the signature is correct (True) or not (False)

	Exceptions:
	Exception: signature verification failed
	"""

    hashType = struct.unpack('B', signature[-1])[0]  #uint8_t
    if hashType != 1:  #SIGHASH_ALL
        return False
    signature = signature[:-1]
    scriptPubKey = Script.multiSigPubKey(pubKeys)
    bodyHash = tx.getSignatureBodyHash(inputIndex, scriptPubKey, hashType)
    return key.verify(bodyHash, signature)
Exemplo n.º 4
0
def makeSpendMultiSigTransaction(outputHash, outputIndex, amount, toHash, fee):
    """
	Make an (non-signed) transaction to send funds from a 2-of-2 multi-signature
	output to a standard Bitcoin scriptPubKey ("send to Bitcoin address").
	The transaction is returned, and is NOT published on the Bitcoin network by
	this function.

	Arguments:
	outputHash: str; the transaction ID of the previous output transaction
	            Note that the byte order is the reverse as shown in Bitcoin.
	outputIndex: int; the index of the output in the previous output transaction
	amount: int; the amount to be sent, including the fee (in Satoshi).
	        This must be equal to the amount in the input.
	toHash: str; the SHA256- and RIPEMD160-hashed public key of the receiver
	        (equivalent to the bitcoin address)
	fee: int; the transaction fee (in Satoshi)

	Return value:
	Transaction; the transaction that sends funds as specified.
	"""

    tx = Transaction(
        tx_in=[TxIn(outputHash, outputIndex)],
        tx_out=[TxOut(amount - fee, Script.standardPubKey(toHash))])

    return tx
Exemplo n.º 5
0
def sendToMultiSigPubKey(bitcoind, amount, toPubKey1, toPubKey2, changeHash, fee):
	"""
	Make a transaction to send funds from ourself to a 2-of-2 multi-signature
	scriptPubKey. The transaction is returned, and is NOT published on the
	Bitcoin network by this function.

	Arguments:
	bitcoind: Bitcoind; the bitcoin daemon from which to retrieve this information
	amount: int; the amount to be sent, not including the fee (in Satoshi)
	toPubKey1: str; the first public key
	toPubKey2: str; the second public key
	changeHash: str; the SHA256- and RIPEMD160-hashed public key to which any
	            change should be sent (equivalent to the bitcoin address)
	fee: int; the transaction fee (in Satoshi)

	Return value:
	Transaction; the transaction that sends funds as specified.

	Exceptions:
	Exception: insufficient funds
	"""

	totalIn, inputs = getInputsForAmount(bitcoind, amount+fee)
	change = totalIn - fee - amount

	#print "%d -> %d, %d, %d" % (totalIn, amount, change, fee)

	tx = Transaction(
		tx_in = [
			TxIn(x[0], x[1])
			for x in inputs
			],
		tx_out = [
			TxOut(amount, Script.multiSigPubKey([toPubKey1, toPubKey2])),
			TxOut(change, Script.standardPubKey(changeHash))
			]
		)

	for i in range(len(inputs)):
		scriptPubKey = Script.deserialize(inputs[i][2])
		key = Key()
		key.setPrivateKey(inputs[i][3])
		tx.signInput(i, scriptPubKey, [None, key.getPublicKey()], [key])

	return tx
Exemplo n.º 6
0
def sendToDataPubKey(bitcoind, data, changeHash, fee):
	"""
	Make a transaction to publish data on the block chain with an OP_RETURN
	output. No funds are sent to the OP_RETURN output: everything goes to fee
	and to the change address. The transaction is returned, and
	is NOT published on the Bitcoin network by this function.

	Arguments:
	bitcoind: Bitcoind; the bitcoin daemon from which to retrieve this information
	data: str; the data to be included in the scriptPubKey (max. 40 bytes)
	changeHash: str; the SHA256- and RIPEMD160-hashed public key to which any
	            change should be sent (equivalent to the bitcoin address)
	fee: int; the transaction fee (in Satoshi)

	Return value:
	Transaction; the transaction that sends funds as specified.

	Exceptions:
	Exception: insufficient funds
	"""

	totalIn, inputs = getInputsForAmount(bitcoind, fee)
	change = totalIn - fee

	print "%d -> %d, %d" % (totalIn, change, fee)

	tx = Transaction(
		tx_in = [
			TxIn(x[0], x[1])
			for x in inputs
			],
		tx_out = [
			TxOut(0, Script.dataPubKey(data)),
			TxOut(change, Script.standardPubKey(changeHash))
			]
		)

	for i in range(len(inputs)):
		scriptPubKey = Script.deserialize(inputs[i][2])
		key = Key()
		key.setPrivateKey(inputs[i][3])
		tx.signInput(i, scriptPubKey, [None, key.getPublicKey()], [key])

	return tx
Exemplo n.º 7
0
def sendToMultiSigPubKey(bitcoind, amount, toPubKey1, toPubKey2, changeHash,
                         fee):
    """
	Make a transaction to send funds from ourself to a 2-of-2 multi-signature
	scriptPubKey. The transaction is returned, and is NOT published on the
	Bitcoin network by this function.

	Arguments:
	bitcoind: Bitcoind; the bitcoin daemon from which to retrieve this information
	amount: int; the amount to be sent, not including the fee (in Satoshi)
	toPubKey1: str; the first public key
	toPubKey2: str; the second public key
	changeHash: str; the SHA256- and RIPEMD160-hashed public key to which any
	            change should be sent (equivalent to the bitcoin address)
	fee: int; the transaction fee (in Satoshi)

	Return value:
	Transaction; the transaction that sends funds as specified.

	Exceptions:
	Exception: insufficient funds
	"""

    totalIn, inputs = getInputsForAmount(bitcoind, amount + fee)
    change = totalIn - fee - amount

    #print "%d -> %d, %d, %d" % (totalIn, amount, change, fee)

    tx = Transaction(tx_in=[TxIn(x[0], x[1]) for x in inputs],
                     tx_out=[
                         TxOut(amount,
                               Script.multiSigPubKey([toPubKey1, toPubKey2])),
                         TxOut(change, Script.standardPubKey(changeHash))
                     ])

    for i in range(len(inputs)):
        scriptPubKey = Script.deserialize(inputs[i][2])
        key = Key()
        key.setPrivateKey(inputs[i][3])
        tx.signInput(i, scriptPubKey, [None, key.getPublicKey()], [key])

    return tx
Exemplo n.º 8
0
def sendToDataPubKey(bitcoind, data, changeHash, fee):
    """
	Make a transaction to publish data on the block chain with an OP_RETURN
	output. No funds are sent to the OP_RETURN output: everything goes to fee
	and to the change address. The transaction is returned, and
	is NOT published on the Bitcoin network by this function.

	Arguments:
	bitcoind: Bitcoind; the bitcoin daemon from which to retrieve this information
	data: str; the data to be included in the scriptPubKey (max. 40 bytes)
	changeHash: str; the SHA256- and RIPEMD160-hashed public key to which any
	            change should be sent (equivalent to the bitcoin address)
	fee: int; the transaction fee (in Satoshi)

	Return value:
	Transaction; the transaction that sends funds as specified.

	Exceptions:
	Exception: insufficient funds
	"""

    totalIn, inputs = getInputsForAmount(bitcoind, fee)
    change = totalIn - fee

    print "%d -> %d, %d" % (totalIn, change, fee)

    tx = Transaction(tx_in=[TxIn(x[0], x[1]) for x in inputs],
                     tx_out=[
                         TxOut(0, Script.dataPubKey(data)),
                         TxOut(change, Script.standardPubKey(changeHash))
                     ])

    for i in range(len(inputs)):
        scriptPubKey = Script.deserialize(inputs[i][2])
        key = Key()
        key.setPrivateKey(inputs[i][3])
        tx.signInput(i, scriptPubKey, [None, key.getPublicKey()], [key])

    return tx
Exemplo n.º 9
0
def signMultiSigTransaction(tx, inputIndex, pubKeys, key):
	"""
	Create a signature for a transaction that spends a 2-of-2 multi-signature
	output. The signature is returned, and is NOT inserted in the transaction.

	Arguments:
	tx: Transaction; the transaction
	outputIndex: int; the index of the transaction input to which a signature
	             applies
	pubKeys: sequence of str; the public keys
	         2 <= len(pubKeys) <= 16
	key: Key; the private key to be used for signing (should correspond to one
	     of the keys in pubKeys)

	Return value:
	str; the signature, including the hash type
	"""

	hashType = 1 #SIGHASH_ALL
	scriptPubKey = Script.multiSigPubKey(pubKeys)
	bodyHash = tx.getSignatureBodyHash(inputIndex, scriptPubKey, hashType)
	return key.sign(bodyHash) + struct.pack('B', hashType) #uint8_t
Exemplo n.º 10
0
def signMultiSigTransaction(tx, inputIndex, pubKeys, key):
    """
	Create a signature for a transaction that spends a 2-of-2 multi-signature
	output. The signature is returned, and is NOT inserted in the transaction.

	Arguments:
	tx: Transaction; the transaction
	outputIndex: int; the index of the transaction input to which a signature
	             applies
	pubKeys: sequence of str; the public keys
	         2 <= len(pubKeys) <= 16
	key: Key; the private key to be used for signing (should correspond to one
	     of the keys in pubKeys)

	Return value:
	str; the signature, including the hash type
	"""

    hashType = 1  #SIGHASH_ALL
    scriptPubKey = Script.multiSigPubKey(pubKeys)
    bodyHash = tx.getSignatureBodyHash(inputIndex, scriptPubKey, hashType)
    return key.sign(bodyHash) + struct.pack('B', hashType)  #uint8_t