Types
Last updated
Was this helpful?
Last updated
Was this helpful?
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 .
bool
: The possible values are constants true
and false
.
Operators:
!
(logical negation)
&&
(logical conjunction, “and”)
||
(logical disjunction, “or”)
==
(equality)
!=
(inequality)
int
: Signed integer of 64 bit size.
Operators:
Comparisons: <=
, <
, ==
, !=
, >=
, >
(all evaluate to bool
)
Arithmetic operators: +
, -
, unary -
, *
, /
, %
(modulo).
Note the clear lack of the **
(exponentiation) operator as well as any bitwise operators.
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.
string
: UTF8-encoded byte sequence.
Operators:
+
(concatenation)
==
(equality)
!=
(inequality)
Members:
length
: Number of characters in the string.
split(int)
: Splits the string at the specified index and returns a tuple with the two resulting strings.
reverse()
: Reverses the string.
bytes
: Byte 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:
+
(concatenation)
==
(equality)
!=
(inequality)
Members:
length
: Number of bytes in the sequence.
split(int)
: Splits the byte sequence at the specified index and returns a tuple with the two resulting byte sequences.
reverse()
: Reverses the byte sequence.
Some byte sequences hold specific meanings inside Bitcoin Cash contracts. These have been granted their own types separate from the regular bytes
type.
pubkey
: Byte sequence representing a public key. Generally 33 bytes long.
Operators:
==
(equality)
!=
(inequality)
sig
: Byte sequence representing a transaction signature. Generally 65 bytes long.
Operators:
==
(equality)
!=
(inequality)
datasig
: Byte sequence representing a data signature. Generally 64 bytes long.
Operators:
==
(equality)
!=
(inequality)
Arrays are not assignable and can only be used with the checkMultisig
function using the following syntax:
Tuples are the type that is returned when calling the split
member function on a string
or bytes
type. Their first or second element can be accessed through an indexing syntax similar to other languages:
It is also possible to assign both sides of the tuple at once with a destructuring syntax:
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.
int
bytes, bool
bool
int
string
bytes
bytes
sig, pubkey, int
pubkey
bytes
bytes
sig
bytes
bytes
datasig
bytes
bytes