def test_sendToMultiSigPubKey(self):
        "Test the sendToMultiSigPubKey function"

        with DummyTransaction():
            tx = bitcoinutils.sendToMultiSigPubKey(self.bitcoind, 40, "toPK1",
                                                   "toPK2", "changeHash", 3)

            self.assertEqual(len(tx.trace), 2)

            #Constructor, with tx_in and tx_out:
            self.assertEqual(tx.trace[0][0], "__init__")
            self.assertEqual(tx.trace[0][1], tuple())
            self.assertEqual(len(tx.trace[0][2]), 2)
            self.assertEqual(len(tx.trace[0][2]["tx_in"]), 1)
            self.assertEqual(len(tx.trace[0][2]["tx_out"]), 2)

            tx_in = tx.trace[0][2]["tx_in"][0]
            self.assertEqual(tx_in.trace, [('__init__', ('foobar_tx', 1), {})])

            tx_out = tx.trace[0][2]["tx_out"][0]
            self.assertEqual(len(tx_out.trace), 1)
            self.assertEqual(tx_out.trace[0][0], "__init__")
            self.assertEqual(len(tx_out.trace[0][1]), 2)
            self.assertEqual(tx_out.trace[0][1][0], 40)
            script = tx_out.trace[0][1][1]
            self.assertEqual(
                script.elements,
                [OP.TWO, "toPK1", "toPK2", OP.TWO, OP.CHECKMULTISIG])

            tx_out = tx.trace[0][2]["tx_out"][1]
            self.assertEqual(len(tx_out.trace), 1)
            self.assertEqual(tx_out.trace[0][0], "__init__")
            self.assertEqual(len(tx_out.trace[0][1]), 2)
            self.assertEqual(tx_out.trace[0][1][0], 7)
            script = tx_out.trace[0][1][1]
            self.assertEqual(script.elements,
                             (OP.DUP, OP.HASH160, "changeHash", OP.EQUALVERIFY,
                              OP.CHECKSIG))

            #Transaction signing:
            self.assertEqual(tx.trace[1][0], "signInput")
            self.assertEqual(len(tx.trace[1][1]), 4)
            self.assertEqual(tx.trace[1][1][0], 0)
            scriptPubKey = tx.trace[1][1][1]
            self.assertEqual(scriptPubKey.serialize(), "foobar_pub")

            key = crypto.Key()
            key.setPrivateKey("foobar")
            self.assertEqual(tx.trace[1][1][2], [None, key.getPublicKey()])
            self.assertEqual(len(tx.trace[1][1][3]), 1)
            self.assertEqual(tx.trace[1][1][3][0].getPrivateKey(),
                             key.getPrivateKey())
	def test_sendToMultiSigPubKey(self):
		"Test the sendToMultiSigPubKey function"

		with DummyTransaction():
			tx = bitcoinutils.sendToMultiSigPubKey(
				self.bitcoind, 40, "toPK1", "toPK2", "changeHash", 3)

			self.assertEqual(len(tx.trace), 2)

			#Constructor, with tx_in and tx_out:
			self.assertEqual(tx.trace[0][0], "__init__")
			self.assertEqual(tx.trace[0][1], tuple())
			self.assertEqual(len(tx.trace[0][2]), 2)
			self.assertEqual(len(tx.trace[0][2]["tx_in"]), 1)
			self.assertEqual(len(tx.trace[0][2]["tx_out"]), 2)

			tx_in = tx.trace[0][2]["tx_in"][0]
			self.assertEqual(tx_in.trace,
				[('__init__', ('foobar_tx', 1), {})])

			tx_out = tx.trace[0][2]["tx_out"][0]
			self.assertEqual(len(tx_out.trace), 1)
			self.assertEqual(tx_out.trace[0][0], "__init__")
			self.assertEqual(len(tx_out.trace[0][1]), 2)
			self.assertEqual(tx_out.trace[0][1][0], 40)
			script = tx_out.trace[0][1][1]
			self.assertEqual(script.elements,
				[OP.TWO, "toPK1", "toPK2", OP.TWO, OP.CHECKMULTISIG])

			tx_out = tx.trace[0][2]["tx_out"][1]
			self.assertEqual(len(tx_out.trace), 1)
			self.assertEqual(tx_out.trace[0][0], "__init__")
			self.assertEqual(len(tx_out.trace[0][1]), 2)
			self.assertEqual(tx_out.trace[0][1][0], 7)
			script = tx_out.trace[0][1][1]
			self.assertEqual(script.elements,
				(OP.DUP, OP.HASH160, "changeHash", OP.EQUALVERIFY, OP.CHECKSIG))

			#Transaction signing:
			self.assertEqual(tx.trace[1][0], "signInput")
			self.assertEqual(len(tx.trace[1][1]), 4)
			self.assertEqual(tx.trace[1][1][0], 0)
			scriptPubKey = tx.trace[1][1][1]
			self.assertEqual(scriptPubKey.serialize(), "foobar_pub")

			key = crypto.Key()
			key.setPrivateKey("foobar")
			self.assertEqual(tx.trace[1][1][2], [None, key.getPublicKey()])
			self.assertEqual(len(tx.trace[1][1][3]), 1)
			self.assertEqual(tx.trace[1][1][3][0].getPrivateKey(), key.getPrivateKey())
print key1.getPublicKey().encode("hex")
print key2.getPublicKey().encode("hex")

print base58.encodeBase58Check(RIPEMD160(SHA256(key1.getPublicKey())), 0)
print base58.encodeBase58Check(RIPEMD160(SHA256(key2.getPublicKey())), 0)

amount = int(100000 * float(raw_input("Amount to be transferred (mBTC): ")))
fee = 10000 #0.1 mBTC

outputHash = binascii.unhexlify(raw_input("Input hash (empty: create multisig): "))[::-1]

if outputHash == "":
	tx = sendToMultiSigPubKey(d, amount,
		key1.getPublicKey(),
		key2.getPublicKey(),
		keyHash2,
		fee=fee)
else:
	outputIndex = 0

	tx = makeSpendMultiSigTransaction(outputHash, outputIndex, amount, keyHash1, fee)

	sig1 = signMultiSigTransaction(
		tx, outputIndex, [key1.getPublicKey(), key2.getPublicKey()], key1)
	sig2 = signMultiSigTransaction(
		tx, outputIndex, [key1.getPublicKey(), key2.getPublicKey()], key2)

	applyMultiSigSignatures(tx, sig1, sig2)