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)
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
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
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