Пример #1
0
	def test_sendToDataPubKey(self):
		"Test the sendToDataPubKey function"

		with DummyTransaction():
			tx = bitcoinutils.sendToDataPubKey(
				self.bitcoind, "Hello", "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__', ('foo_tx', 3), {})])

			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], 0)
			script = tx_out.trace[0][1][1]
			self.assertEqual(script.elements,
				(OP.RETURN, "Hello"))

			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(), "foo_pub")

			key = crypto.Key()
			key.setPrivateKey("foo")
			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())
Пример #2
0
    def test_sendToDataPubKey(self):
        "Test the sendToDataPubKey function"

        with DummyTransaction():
            tx = bitcoinutils.sendToDataPubKey(self.bitcoind, "Hello",
                                               "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__', ('foo_tx', 3), {})])

            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], 0)
            script = tx_out.trace[0][1][1]
            self.assertEqual(script.elements, (OP.RETURN, "Hello"))

            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(), "foo_pub")

            key = crypto.Key()
            key.setPrivateKey("foo")
            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())
Пример #3
0
def make(args):
	if len(args) != 2:
		help(["make"])
		sys.exit(1)

	with open(args[0], "rb") as f:
		data = f.read()

	dataHash = crypto.SHA256(crypto.SHA256(data))
	print "Data hash: ", dataHash.encode("hex")

	fee =    10000 #0.1 mBTC = 0.0001 BTC
	connect()

	changeAddress = bitcoind.getNewAddress()
	changeHash = base58.decodeBase58Check(changeAddress, 0) # PUBKEY_ADDRESS = 0

	tx = bitcoinutils.sendToDataPubKey(bitcoind, dataHash, changeHash, fee)
	txID = tx.getTransactionID()[::-1].encode("hex")
	print "Transaction ID: ", txID

	bitcoind.sendRawTransaction(tx.serialize())

	print "Transaction is published. Now we wait for 3 confirmations..."
	confirmations = -1
	while confirmations < 3:
		time.sleep(5)
		try:
			newConfirmations = bitcoind.getTransaction(txID)["confirmations"]
		except KeyError:
			newConfirmations = 0
		if newConfirmations != confirmations:
			print "  %d confirmations" % newConfirmations
		confirmations = newConfirmations

	height = bitcoind.getBlockCount()
	for i in range(1000):
		transactionsInBlock = bitcoind.getTransactionHashesByBlockHeight(height)
		if txID in transactionsInBlock:
			break
		height -= 1
	if txID not in transactionsInBlock:
		raise Exception(
			"Something went wrong: transaction ID not found in the last 1000 blocks")

	print "Block height: ", height

	index = transactionsInBlock.index(txID)
	transactionsInBlock = [binascii.unhexlify(x)[::-1] for x in transactionsInBlock]
	merkleBranch, merkleRoot = getMerkleBranch(transactionsInBlock, index)

	blockInfo = bitcoind.getBlockInfoByBlockHeight(height)
	if blockInfo["merkleroot"] != merkleRoot[::-1].encode("hex"):
		raise Exception("Something went wrong: merkle root value mismatch")

	dt = datetime.utcfromtimestamp(blockInfo["time"])
	timeText = dt.strftime("%A %B %d %I:%M:%S %p %Y (UTC)")

	with open(args[1], "wb") as f:
		f.write("#Timestamp certificate for the file %s\n\n" % args[0])

		f.write("#Double-SHA256 of the file contents:\n")
		f.write("dataHash = %s\n\n" % dataHash.encode("hex"))

		f.write("#The timestamping transaction, containing the above hash:\n")
		f.write("transaction = %s\n\n" % tx.serialize().encode("hex"))

		f.write("#The double-SHA256 of the timestamping transaction:\n")
		f.write("#(This is the same as the transaction ID, with the byte order reversed)\n")
		f.write("transactionHash = %s\n\n" % tx.getTransactionID().encode("hex"))

		f.write("#The Merkle-branch of the timestamping transaction:\n")
		for i in range(len(merkleBranch)):
			left, right = merkleBranch[i]
			f.write("merkle_%d = %s, %s\n" % (i, left.encode("hex"), right.encode("hex")))
		f.write("\n")

		f.write("#The Merkle root of the block:\n")
		f.write("#(byte order is the reverse as typically shown in Bitcoin)\n")
		f.write("merkleRoot = %s\n\n" % merkleRoot.encode("hex"))

		f.write("#The block information:\n")
		f.write("blockHeight = %d\n" % height)
		f.write("blockHash = %s\n" % blockInfo["hash"])
		f.write("blockTime = %d\n\n" % blockInfo["time"])

		f.write("#The timestamp:\n")
		f.write("timestamp = %s\n" % timeText)
Пример #4
0
def make(args):
    if len(args) != 2:
        help(["make"])
        sys.exit(1)

    with open(args[0], "rb") as f:
        data = f.read()

    dataHash = crypto.SHA256(crypto.SHA256(data))
    print "Data hash: ", dataHash.encode("hex")

    fee = 10000  #0.1 mBTC = 0.0001 BTC
    connect()

    changeAddress = bitcoind.getNewAddress()
    changeHash = base58.decodeBase58Check(changeAddress,
                                          0)  # PUBKEY_ADDRESS = 0

    tx = bitcoinutils.sendToDataPubKey(bitcoind, dataHash, changeHash, fee)
    txID = tx.getTransactionID()[::-1].encode("hex")
    print "Transaction ID: ", txID

    bitcoind.sendRawTransaction(tx.serialize())

    print "Transaction is published. Now we wait for 3 confirmations..."
    confirmations = -1
    while confirmations < 3:
        time.sleep(5)
        try:
            newConfirmations = bitcoind.getTransaction(txID)["confirmations"]
        except KeyError:
            newConfirmations = 0
        if newConfirmations != confirmations:
            print "  %d confirmations" % newConfirmations
        confirmations = newConfirmations

    height = bitcoind.getBlockCount()
    for i in range(1000):
        transactionsInBlock = bitcoind.getTransactionHashesByBlockHeight(
            height)
        if txID in transactionsInBlock:
            break
        height -= 1
    if txID not in transactionsInBlock:
        raise Exception(
            "Something went wrong: transaction ID not found in the last 1000 blocks"
        )

    print "Block height: ", height

    index = transactionsInBlock.index(txID)
    transactionsInBlock = [
        binascii.unhexlify(x)[::-1] for x in transactionsInBlock
    ]
    merkleBranch, merkleRoot = getMerkleBranch(transactionsInBlock, index)

    blockInfo = bitcoind.getBlockInfoByBlockHeight(height)
    if blockInfo["merkleroot"] != merkleRoot[::-1].encode("hex"):
        raise Exception("Something went wrong: merkle root value mismatch")

    dt = datetime.utcfromtimestamp(blockInfo["time"])
    timeText = dt.strftime("%A %B %d %I:%M:%S %p %Y (UTC)")

    with open(args[1], "wb") as f:
        f.write("#Timestamp certificate for the file %s\n\n" % args[0])

        f.write("#Double-SHA256 of the file contents:\n")
        f.write("dataHash = %s\n\n" % dataHash.encode("hex"))

        f.write("#The timestamping transaction, containing the above hash:\n")
        f.write("transaction = %s\n\n" % tx.serialize().encode("hex"))

        f.write("#The double-SHA256 of the timestamping transaction:\n")
        f.write(
            "#(This is the same as the transaction ID, with the byte order reversed)\n"
        )
        f.write("transactionHash = %s\n\n" %
                tx.getTransactionID().encode("hex"))

        f.write("#The Merkle-branch of the timestamping transaction:\n")
        for i in range(len(merkleBranch)):
            left, right = merkleBranch[i]
            f.write("merkle_%d = %s, %s\n" %
                    (i, left.encode("hex"), right.encode("hex")))
        f.write("\n")

        f.write("#The Merkle root of the block:\n")
        f.write("#(byte order is the reverse as typically shown in Bitcoin)\n")
        f.write("merkleRoot = %s\n\n" % merkleRoot.encode("hex"))

        f.write("#The block information:\n")
        f.write("blockHeight = %d\n" % height)
        f.write("blockHash = %s\n" % blockInfo["hash"])
        f.write("blockTime = %d\n\n" % blockInfo["time"])

        f.write("#The timestamp:\n")
        f.write("timestamp = %s\n" % timeText)