sCrypt
is an embedded Domain Specific Language
(eDSL) based on TypeScript for writing smart contracts on Bitcoin SV. Embedded
means that it is a language inside another language. sCrypt
is strictly a subset of TypeScript, so all sCrypt
code is valid TypeScript, but not vice versa.
We choose TypeScript as the host language because it provides an easy, familiar language (JavaScript), but with type safety, making it easy to get started writing safe smart contracts. There is no need to learn a new programming language or tools, if you are already familiar with TypeScript/JavaScript. If you're new to TypeScript, check out this helpful introductory video.
Smart contracts on Bitcoin are based on the UTXO model, which is very different from an account model like Ethereum used.
Each bitcoin transaction consists of some inputs and outputs. An output contains:
locking script
).while an input contains:
unlocking script
).An Unspent Transaction Output (UTXO) is an output not consumed in any transaction yet. The low-level bytecode/opcode is called Bitcoin Script, which is interpreted by the Bitcoin Virtual Machine (BVM).
In the example above, we have two transactions, each having one input (in green) and one output (in red). And the transaction on the right spends the one on the left.
The locking script can be regarded as a boolean function f
that specifies conditions to spend the bitcoins in the UTXO, acting as a lock (thus the name "locking").
The unlocking script in turns provides the function arguments that makes f
evaluates to true
, i.e., the "key" (also called witness) needed to unlock.
Only when the “key” in an input matches previous output’s “lock”, it can spend bitcoins contained in the output.
In a regular Bitcoin payment to a Bitcoin address, the locking script is Pay To Pubkey Hash (P2PKH). It checks the spender has the right private key corresponding to the address so she can produce a valid signature in the unlocking script. The expressive Script enables the locking script to specify arbitrarily more complex spending conditions than simple P2PKH, i.e., Bitcoin smart contracts.
sCrypt
work?sCrypt
is a high-level language to be compiled into Bitcoin Script. The resulting assembly-like scripts could be used as locking scripts when building transactions.