Skip to main content

Examples

An extensive collection of examples is available in the GitHub repository. Below we discuss a few of these examples in more details and go through their functionality. These examples focus mainly on the CashScript syntax, while the Examples page in the sdk section focuses more on the use of the SDK.

Transfer With Timeout

One interesting use case of Bitcoin Cash is using it for paper tips. With paper tips, you send a small amount of money to an address, and print the corresponding private key on a piece of paper. Then you can hand out these pieces of paper as a tip or gift to people in person. In practice, however, people might not know what to do with these gifts or they might lose or forget about it.

As an alternative, a smart contract can be used for these kinds of gifts. This smart contract allows the recipient to claim their gift at any time, but if they don't claim it in time, the sender can reclaim it.

pragma cashscript ^0.10.0;

contract TransferWithTimeout(pubkey sender, pubkey recipient, int timeout) {
// Require recipient's signature to match
function transfer(sig recipientSig) {
require(checkSig(recipientSig, recipient));
}

// Require timeout time to be reached and sender's signature to match
function timeout(sig senderSig) {
require(checkSig(senderSig, sender));
require(tx.time >= timeout);
}
}

For an example of how to put this contract to use in a JavaScript application, see the SDK examples page

HTLC

pragma cashscript ^0.2.0;

contract HTLC(
pubkey sender,
pubkey recipient,
int expiration,
bytes32 hash
) {
// Require the preimage to match the stored hash and signature to match
function complete(bytes preimage, sig s) {
require(sha256(preimage) == hash);
require(checkSig(s, recipient));
}

// Require time to be after expiration and signature to match
function cancel(sig s) {
require(tx.time >= expiration);
require(checkSig(s, sender));
}
}