Good Contracts on Bitcoin have been proven to be extra highly effective and versatile than beforehand thought. But, there are nonetheless two grave limitations:
We suggest a novel strategy to deal with each limitations concurrently, referred to as Pay to Contract Hash (P2CH). We use NFT for example to exhibit the way it works. It’s also relevant to different crypto belongings corresponding to native bitcoins and fungible tokens.
In our previous NFT contract, we preserve a desk mapping every token to its proprietor’s handle. The proprietor possesses a non-public key that may authorize the switch of the token.
Beneath P2CH, the proprietor may also be a contract’s handle, outlined because the hash of its script. The hash acts because the distinctive ID of the contract and is used to reference the contract.
Particularly, hash160 is used, the identical hashing algorithm used to hash a bitcoin public key into an handle. A contract hash seems the identical as an everyday bitcoin handle, since they’re each 20 bytes. The brand new NFT contract seems to be like the next:
This is identical NFT contract as earlier than, apart from the
else department. It handles the switch when a token is managed by a contract, as an alternative of a person’s personal key.
We have to make sure the contract is unlocked in one other enter of the identical transaction, as we now have achieved in inter-contract call.
In an instance as Determine 2 exhibits, the token referenced in enter 0 must validate enter 1 is referencing the anticipated contract in the identical transaction tx2.
Line 20-22 get the id of the transaction containing the contract (tx1 within the diagram) and the output containing it (output 0 in tx1).
Line 24 validates the complete transaction matches the txid.
Line 26 parses the contract transaction to get the contract’s full script and hashes it and matches it in opposition to the proprietor “handle” at Line 27.
One instance contract could possibly be the next:
It may be spent if and provided that a given handle is paid a certain quantity. Utilizing this contract, we will promote a token at a given value atomically in three steps:
A full code instance may be discovered here.
It’s price noting the contract may be arbitrary and may be developed by a 3rd social gathering independently. The contract doesn’t need to be recognized upfront. This implies the token is infinitely extensible (limitation 1). Additionally, it’s compact and solely handles P2CH that applies to any contract (limitation 2).
As well as, they’re other ways to sample match a contract. We use a hash of the complete contract script. Components of the script may also be used, e.g., the code part of a stateful contract.