scryptlib
Last updated
Was this helpful?
Last updated
Was this helpful?
Javascript/TypeScript SDK for integration of Radiant (RAD RXD) Blockchain Smart Contracts written in the sCrypt language.
PLEASE NOTE: This is a fork of scryptlib as a convenience that contains the patches to the included bsv.js lib directly Alternatively, the regular scryptlib may be used along with radjs radiantjs https://github.com/RadiantBlockchain/radjs (latest) or and not using the bundled bsv in scryptlib.
You can install scryptlib
in your project as below:
A smart contract is compiled to a locking script template. A contract function call is transformed to an unlocking script. Developers are responsible for setting the locking and unlocking scripts of a transaction properly before sending it to the Bitcoin network. This may include some actions described below:
Instantiate locking script: replace the constructor formal parameters, represented by placeholders in the locking script template, with actual parameters/arguments to form the complete locking script.
Assemble unlocking script: convert the arguments of a contract function call to script format and concatenate them to form the unlocking script.
By using scryptlib
, both scripts can be obtained with ease.
The compiler output results in a JSON file. It’s a representation used to build locking and unlocking scripts. We call this file a contract description file.
There are three ways to generate this file (named as xxx_desc.json
):
Use the function compile
programmatically:
compileAsync
is the asynchronous version of the function compile
Run npx
command in CLI:
All basic types of the sCrypt language have their corresponding javascript classes in scryptlib. In this way, the type of parameters could be checked and potential bugs can be detected before running.
int
new Int(1)
or number
or bigint
bool
new Bool(true)
or boolean
bytes
new Bytes('0001')
or new String("hello world 😊")
PubKey
new PubKey('0001')
PrivKey
new PrivKey(1)
Sig
new Sig('0001')
Ripemd160
new Ripemd160('0001')
Sha1
new Sha1('0001')
Sha256
new Sha256('0001')
SigHashType
new SigHashType('01')
SigHashPreimage
new SigHashPreimage('010001')
OpCodeType
new OpCodeType('76')
scryptlib uses javascript array to represent the array types of the sCrypt language.
Composite types, including structs and type aliases, are dynamically generated by buildTypeClasses
. When creating a structure, all members must specify values. Use dot to access structure members.
Structure and type aliases defined in sCrypt:
Access Structure and type aliases by SDK :
Library is another composite types. When the constructor parameter of the contract contains library, we need to create library through sdk.
Library defined in sCrypt:
Access Library by SDK :
As you can see, creating a library instance is similar to creating a contract instance. Sometimes the constructor parameters of the library may be generic types. At this time, the sdk will deduce the generic type based on the constructor arguments you pass.
Both deploying a contract and calling a contract function are achieved by sending a transaction. Generally speaking,
deploying a contract needs the locking script in the output of this transaction to be set properly;
calling a contract function needs the unlocking script in the input of this transaction to be set properly.
There are 2 steps.
You can use the description file to build a reflected contract class in Javascript/TypeScript like this:
To create an instance of the contract class, for example:
To get the locking script, use:
To get the unlocking script, just call the function and turn the result to bsv.Script
object, for example:
A useful method verify(txContext)
is provided for each contract function call. It would execute the function call with the given context locally. The txContext
argument provides some context information of the current transaction, needed only if signature is checked inside the contract.
It returns an object:
It usually appears in unit tests, like:
Use the initial state to instantiate the contract and read the state by accessing the properties of the contract instance.
Then use instance.getNewStateScript()
to get a locking script that includes the new state. It accepts an object as a parameter. Each key of the object is the name of a state property, and each value is the value of the state property. You should provide all state properties in the object.
You can also access the state of the contract by accessing the properties of the instance.
Assembly variables can be replaced with literal Script in ASM format using replace()
. Each variable is prefixed by its unique scope, namely, the contract and the function it is under.
In addition to using a constructor to create a contract, you can also use a raw transaction to construct it.
Some contracts use Bigint
to construct or unlock. but some browsers do not support Bigint
, such as IE11. In this case, we use strings to build Bigint
.
Use to compile manually;
make building transactions super easy.
sCrypt offers . Declare any property that is part of the state with a decorator @state
in a contract, for example:
You can also maintain state manually to, for example, optimize your contract or use customized state de/serialization .
You could find more examples using scryptlib
in the repository.