Construct, decode transactions and work with Clarity smart contracts on the Stacks blockchain.
The Stacks authentication process enables secure user sign-in for web apps by generating and handling encrypted authentication requests. It involves setting up an app domain, configuring permissions, and creating a UserSession to manage user data.
In this example we construct a contract-call transaction with a post condition. We have set the validateWithAbi option to true, so the makeContractCall builder will attempt to fetch this contracts ABI from the specified Stacks network, and validate that the provided functionArgs match what is described in the ABI. This should help you avoid constructing invalid contract-call transactions. If you would prefer to provide your own ABI instead of fetching it from the network, the validateWithABI option also accepts ClarityABI objects, which can be constructed from ABI files like so:
To generate a sponsored transaction, first create and sign the transaction as the origin. The sponsored property in the options object must be set to true.
To generate a multi-sig transaction, first create an unsigned transaction.
The numSignatures and publicKeys properties in the options object must be set:
import{
makeUnsignedSTXTokenTransfer,
createStacksPrivateKey,
deserializeTransaction,
pubKeyfromPrivKey,
publicKeyToString,
TransactionSigner,
standardPrincipalCV,
BytesReader,
AnchorMode,
}from'@stacks/transactions';
constrecipient=standardPrincipalCV('SP3FGQ8...');
constamount=2500000n;
constfee=0n;
constmemo='test memo';
// private keys of the participants in the transaction
publicKeys:pubKeyStrings,// public key string array with >= numSignatures elements
anchorMode:AnchorMode.Any,
});
constserializedTx=transaction.serialize();
This transaction payload can be passed along to other participants to sign. In addition to
meeting the numSignatures requirement, the public keys of the parties who did not sign the
transaction must be appended to the signature.
// deserialize and sign transaction
constbytesReader=newBytesReader(serializedTx);
// Partially signed or unsigned multi-sig tx can be deserialized to add the required signatures
Building transactions that call functions in deployed clarity contracts requires you to construct valid Clarity Values to pass to the function as arguments. The Clarity type system contains the following types:
(tuple (key-name-0 key-type-0) (key-name-1 key-type-1) ...) : a typed tuple with named fields.
(list max-len entry-type) : a list of maximum length max-len, with entries of type entry-type
(response ok-type err-type) : object used by public functions to commit their changes or abort. May be returned or used by other functions as well, however, only public functions have the commit/abort behavior.
(optional some-type) : an option type for objects that can either be (some value) or none
(buff max-len) : byte buffer or maximum length max-len.
principal : object representing a principal (whether a contract principal or standard principal).
bool : boolean value (true or false)
int : signed 128-bit integer
uint: unsigned 128-bit integer
This library contains Typescript types and classes that map to the Clarity types, in order to make it easy to construct well-typed Clarity values in Javascript. These types all extend the abstract class ClarityValue.
import{
trueCV,
falseCV,
noneCV,
someCV,
intCV,
uintCV,
standardPrincipalCV,
contractPrincipalCV,
responseErrorCV,
responseOkCV,
listCV,
tupleCV,
bufferCV,
}from'@stacks/transactions';
import{ utf8ToBytes }from'@stacks/common';
// construct boolean clarity values
constt=trueCV();
constf=falseCV();
// construct optional clarity values
constnothing=noneCV();
constsomething=someCV(t);
// construct a buffer clarity value from an existing byte array
If you develop in Typescript, the type checker can help prevent you from creating wrongly-typed Clarity values. For example, the following code won't compile since in Clarity lists are homogeneous, meaning they can only contain values of a single type. It is important to include the type variable BooleanCV in this example, otherwise the typescript type checker won't know which type the list is of and won't enforce homogeneity.
Warning
The Stacks blockchain's post-condition processor can NOT check ownership.
It checks whether or not a principal will send or will not send an NFT.
Post-conditions can NOT verify anything about the recipient of an asset.
If you want to verify conditions about asset recipients, you will need to use Clarity.