This submit was first printed on Medium.
Good contracts haven’t any information of the skin world and need to depend on oracles to import exterior knowledge basically. We’ve got proven two methods to import knowledge from oracles earlier than, primarily based on Rabin signature and ECDSA. On this article, we present it’s attainable to entry a particular sort of exterior knowledge, i.e., knowledge on the blockchain (reminiscent of block headers and transactions), within the absence of oracles, whereas nonetheless sustaining knowledge integrity. By permitting good contracts to entry on-chain knowledge with minimal belief, it opens infinite alternatives for all new sorts of good contracts on Bitcoin.
Entry block headers
The Bitcoin blockchain consists of a sequence of blocks, because the title counsel. A block has two components: a block header and transactions.

A block header comprises the metadata of the block, with six fields as proven beneath.

It’s value noting that bitcoin headers are a part of Bitcoin’s proof-of-work consensus algorithm. Extra particularly, the hash of a serialized block header mustn’t exceed the problem goal (i.e., the variety of main zeros). Because of trustless nature of proof of work, this can be very expensive to supply a sound block header, particularly when the problem is excessive. However it is extremely straightforward to verify if a given block header is legitimate. That is precisely how we import a block header into a sensible contract proven beneath, with out counting on any oracles.
Blockchain Contract
isBlockHeaderValid() at Line 22 checks if a block header is legitimate. bits2Target() at Line 31 calculates the problem goal from a compact type (a 4-byte discipline sometimes known as nBits). We merely hash the block header at Line 23 and ensure it meets the problem goal at Line 27.
Faux block headers
We additionally verify the problem goal is not any bigger than the blockchainTarget parameter at Line 27, to regulate the problem of manufacturing a faux block header. In any other case, an attacker can simply create a block header, whose hash meets the goal of problem inside (e.g., solely has 2 main zeros). As with many different elements of Bitcoin reminiscent of 0-conf, the safety of importing block headers this manner is financial, not merely technical. This implies in follow, it’s crucial {that a} good contract counting on an actual block header mustn’t lock extra cash than it prices to supply a faux header.
Entry transactions
As soon as a block header is accessible, we are able to simply entry any transaction within the block. It’s because the block header comprises the foundation of the Merkle tree of all of the transactions. Much like SPV, we move the transaction and its Merkle path into a sensible contract and confirm it matches the foundation hash within the block header. txInBlock() at Line 17 demonstrates this.

A case examine: Utilizing blockchain to generate random numbers
Basically, it’s thought-about a tough drawback to generate pseudo-random numbers in a blockchain securely and pretty, because the blockchain is each deterministic and clear. We leverage blockchain knowledge, particularly the nonce discipline of a block header, because the supply of entropy.
Alice and Bob each lock similar quantity of bitcoins into the next contract. As soon as the transaction containing the contract is broadcasted, will probably be mined right into a future block. Relying on the nonce of the block, which is tough to foretell and could be considered random, a winner is decided and takes all locked bitcoins.
BlockchainPRNG Contract
Line 17 and 20 use the OP_PUSH_TX method to get the txid of the transaction containing the contract. Line 23 verifies the block header is respectable and Line 26 verifies the earlier transaction is in it. If the nonce discipline is odd, Alice wins; in any other case, Bob wins.
Abstract
We’ve got proven find out how to entry blockchain knowledge in Bitcoin smart contracts with minimal belief. Since a serialized bitcoin header is barely 80-bytes lengthy and Merkle proof scales logarithmically, this method is extraordinarily environment friendly (similar as SPV).
We’ve got additionally proven an instance to make use of the blockchain knowledge to generate pseudo-random numbers. That is solely the start of what’s attainable, which we are going to discover in future posts. Keep tuned.
Acknowledgements
This work is impressed by the work of Ying Chen now at Cambridge Cryptographic.
Watch: CoinGeek New York presentation, Good Contracts & Computation on Bitcoin
New to Bitcoin? Take a look at CoinGeek’s Bitcoin for Beginners part, the last word useful resource information to be taught extra about Bitcoin—as initially envisioned by Satoshi Nakamoto—and blockchain.