Building BSV smart contracts with sCrypt

BSV Blockchain recently held its annual DevCon on 20 May 2024 at the London ExCel Convention Centre. The DevCon aimed to empower developers to leverage the BSV blockchain as an infrastructure for novel applications, services and businesses.

The event featured a presentation by Mihael Šinkec, Software Developer at sCrypt, where he showcased why sCrypt is the most popular coding language in the BSV ecosystem.

sCrypt makes it easier for developers to create, deploy, and manage smart contracts on the BSV blockchain. This innovative approach not only simplifies the development process but also enhances the security and reliability of programming.

Understanding Smart Contracts in BSV

Šinkec explained that while smart contracts are typically associated with Ethereum, Bitcoin has had this capability since its inception. However, he remarked that there are fundamental differences in their underlying models:

BSV (based on the original Bitcoin protocol) uses the UTXO (Unspent Transaction Output) model, while Ethereum employs the account model. In Ethereum, smart contracts are independent entities with their own addresses. In contrast, BSV’s smart contracts address change as they move from one output to another.

A smart contract, as Šinkec explained, is essentially a program embedded within a transaction that sets rules for the movement of funds on the blockchain. While a typical transaction on the blockchain validates a signature, functioning as a rudimentary smart contract, Bitcoin’s scripting language allows for more sophisticated programming.

The challenge, however, lies in the difficulty of writing these programs directly in the low level language of Bitcoin script. sCrypt addresses this challenge with its high-level, developer-friendly language.

sCrypt: Built on TypeScript

The flagship product of sCrypt is its smart contracting language, an embedded domain-specific language (DSL) built into TypeScript. This integration ensures seamless compatibility with other TypeScript code, easing adoption for developers already familiar with JavaScript and TypeScript. The type safety provided by TypeScript is particularly advantageous in financial programming, where unforeseen conditions can lead to significant issues. Initially, sCrypt was a separate language, but its transition to TypeScript allows developers to leverage their existing knowledge without learning a new syntax.

Šinkec highlighted a few advantages of the TypeScript integration:

  • Consistent method functionality on and off the blockchain
  • Simplified debugging with familiar TypeScript tools
  • Easier understanding and troubleshooting of compiled Bitcoin script

Furthermore, sCrypt abstracts Bitcoin’s UTXO model in a manner that mirrors Solidity, Ethereum’s smart contract language, thereby reducing the cognitive load for developers. Šinkec explained that Bitcoin transactions consist of inputs and outputs, where outputs contain coins and a program (the smart contract or locking script). This program specifies the conditions for unlocking the outputs, such as verifying a signature against a public key in a standard payment.

The mechanics of transactions and smart contracts

Šinkec simplified locking and unlocking scripts by comparing the locking script to a function 𝑓, which represents the smart contract. The unlocking script, found within an input, provides arguments to this function. If the function returns true after checking the inputs, the funds are unlocked. Otherwise, the transaction is rejected by miners. This mechanism ensures that only valid transactions are processed.

Regular payments, known as P2PKH (Pay to Public Key Hash), involve a standard locking script with a few opcodes. However, more complex scripts can be much larger, spanning several kilobytes and involving substantial sums of money. Without tools like sCrypt, developing and managing these intricate scripts can quickly become overwhelming.

Table of content

Latest News

Stay in the loop

News, tips, guides, and industry best practices

Useful eBooks to download

Expand your knowledge about BSV and blockchain technology.

Ready to add blockchain solutions to your business or government agency?

Send us a message and let us know about your needs.
Please contact [email protected]

Join Our
Community

Stay updated with the BSV Blockchain's latest news and
events.
Subscribe to our weekly newsletter.