Basic Syntax
Last updated
Last updated
Introduction to the basic grammar and rules of the Scrypt language.
Reference: https://scryptdoc.readthedocs.io/en/latest/
Each contract has only one constructor, which is used to initialize member variables. If no constructor is specified, a default constructor will be generated to initialize all members.
The require
function specifies contract conditions and accepts a boolean parameter. If the parameter is false, the contract execution will be interrupted, and it will return a failure.
When the parameter is true, the verification will pass.
Each contract has at least one public function. This function does not return a value (void return). It can be considered the locking script or the entry point of the entire contract. Only when all the require
conditions in the public function are met and end normally, can the contract be unlocked.
In the case of multiple public functions, the contract can be considered to have multiple unlocking methods, each capable of unlocking the output.
Bool: true or false
Int: Signed integers of any length in decimal or hexadecimal
Byte: Hexadecimal format starting with b and single quotes, or UTF-8 characters enclosed in double quotes
Array type: separated by commas, with a fixed length
Arrays can use the repeat
function to set all values to the same.
Arrays can be indexed using indices starting from 0. If out of bounds, the contract will fail.
Two arrays are considered equal if they have the same number of elements and each element is the same.
A struct is a group of member variables, which can be basic data structures or nested structs.
When using arrays, members can be accessed directly using the dot (.) notation.
Type inference: The auto
keyword can automatically infer basic data types.
Type alias: You can assign aliases to specific data types.
Generics can be defined in libraries and structs. When using generics, the corresponding type must be explicitly declared.
Domain types are subtypes of basic types, mainly used for types related to Bitcoin transactions. Using domain types can enhance type safety.
PublicKey: PubKey(b'0200112233445566778899aabbccddeeffffeeddccbbaa99887766554433221100');
Sig: Sig( b'3045022100b71be3f1dc001e0a1ad65ed84e7a5a0bfe48325f2146ca1d677cf15e96e8b80302206d74605e8234eae3d4980fcd7b2fdc1c5b9374f0ce71dea38707fccdbd28cf7e41');
Ripemd160: Ripemd160(b'0011223344556677889999887766554433221100');
PubKeyHash: PubKeyHash(b'0011223344556677889999887766554433221100');
Sha1: Sha1(b'0011223344556677889999887766554433221100');
Sha256: Sha256(b'00112233445566778899aabbccddeeffffeeddccbbaa99887766554433221100');
SigHashType: SigHashType(b'01'); SigHashType s = SigHash.ALL | SigHash.ANYONECANPAY;
SigHashPreimage: SigHashPreimage(b'0100000028bcef7e73248aa273db19d73');
OpCodeType: OpCode.OP_DUP + OpCode.OP_ADD;
PrivKey: PrivKey(0x00112233445566778899aabbccddeeffffeeddccbbaa99887766554433221100);
Constants cannot be modified once declared.
The if
condition is used to control the program logic flow. Besides bool, int, and bytes can also use if
. Int 0 and byte b'', b'00' are considered false.
The exit
method ends the function logic early. The parameter can be true or false.
Three or more asterisks (*) will insert an OP_CODESEPARATOR operation, which affects the signature calculation.
Default: no keyword required
Private
Public: only applies to functions
Default
Yes
Yes
No
Private
Yes
No
No
Public
Yes
Yes
Yes