Types
CashScript is a statically typed language, which means that the type of each variable needs to be specified. Types can also be implicitly or explicitly cast to other types. For a quick reference of the various casting possibilities, see Type Casting.
Boolean
bool
: The possible values are constants true
and false
.
Operators:
!
(logical negation)==
(equality)!=
(inequality)
The operators ||
and &&
don't apply common short-circuiting rules. This means that in the expression f(x) || g(y)
, g(y)
will still be executed even if f(x)
evaluates to true.
Integer
int
: Signed integer of 32 bit size.
Operators:
- Comparisons:
<=
,<
,==
,!=
,>=
,>
(all evaluate tobool
) - Arithmetic operators:
+
,-
, unary-
,*
,/
Note the lack of the /
(division), %
(modulo), and **
(exponentiation) operator as well as any bitwise operators.
Over- & Underflows
The maximum range for 32-bit integers is -2147483647
to 2147483647
, operations exceeding these limits will fail the transaction. So operations like summation, subtraction and multiplication should take into account these boundary cases with over- or underflows.
Contract authors should always consider whether +
, -
and *
operations can cause under- or overflows and how this would impact contract security.
Date Parsing
Dates and times are always represented as integers. To get the UTC timestamp of a date use the built-in parser to avoid any potential errors. This will take a date in the format date("YYYY-MM-DDThh:mm:ss")
and convert it to an integer timestamp.
Example
int timestamp = date("2021-02-17T01:30:00");
require(timestamp == 1613554200);
String
string
: UTF8-encoded byte sequence.
Operators:
==
(equality)!=
(inequality)
Members:
length
: Number of characters in the string.
Bytes
bytes
: Byte sequence. Prefixed with 0x
to indicate hexadecimal sequence. Can optionally be bound to a byte length by specifying e.g. bytes4
, bytes32
, bytes64
. It is also possible to use byte
as an alias for bytes1
.
Operators:
==
(equality)!=
(inequality)&
(bitwise AND)|
(bitwise OR)^
(bitwise XOR)
Members:
length
: Number of bytes in the sequence.
Example
bytes mintingCapability = 0x02;
bytes noCapability = 0x;
Bytes types with semantic meaning
Some byte sequences hold specific meanings inside Bitcoin Cash contracts. These have been granted their own types, separate from the regular bytes
type.
Public Key
pubkey
: Byte sequence representing a public key. Generally 33 bytes long.
Operators:
==
(equality)!=
(inequality)
Transaction Signature
sig
: Byte sequence representing a transaction signature. Generally 65 bytes long.
Operators:
==
(equality)!=
(inequality)
Data Signature
datasig
: Byte sequence representing a data signature. Generally 64 bytes long.
Operators:
==
(equality)!=
(inequality)
Array
Arrays are not assignable and can only be used with the checkMultisig
function using the following syntax:
checkMultisig([sig1, sig2], [pk1, pk2, pk3]);
Type Casting
Type casting can be done both explicitly and implicitly as illustrated below. pubkey
, sig
and datasig
can be implicitly cast to bytes
, meaning they can be used anywhere where you would normally use a bytes
type. Explicit type casting can be done with a broader range of types, but is still limited. The syntax of this explicit type casting is illustrated below. Note that you can also cast to bounded bytes
types.
See the following table for information on which types can be cast to other which other types.
Type | Implicitly castable to | Explicitly castable to |
---|---|---|
int | bool | |
bool | int | |
string | bytes | |
bytes | sig, pubkey | |
pubkey | bytes | bytes |
sig | bytes | bytes |
datasig | bytes | bytes |