7.7.1 • Published 3 days ago

@stacks/connect v7.7.1

Weekly downloads
628
License
MIT
Repository
-
Last release
3 days ago

@stacks/connect npm

🐎 Getting Started

1. Add the dependency

Add the @stacks/connect dependency to your project using your favorite package manager. Some options below

npm install @stacks/connect
pnpm install @stacks/connect
yarn add @stacks/connect

2. Creating AppConfig and UserSession

Add a reusable UserSession instance to your project. This will allow your website to store authentication state in localStorage.

/* ./userSession.js */
import { AppConfig, UserSession } from '@stacks/connect';

const appConfig = new AppConfig(['store_write', 'publish_data']);
export const userSession = new UserSession({ appConfig }); // we will use this export from other files

3. Interacting with the wallet

"Connect" aka authentication (showConnect)

Connecting the wallet is a very simple form of authentication. This process gives the web-app information about a wallet account (selected by the user).

The snippet below lets your web-app trigger the wallet to open and authenticate an account. If no wallet is installed, an informational modal will be displayed in the web-app.

import { showConnect } from '@stacks/connect';
import { userSession } from './userSession';

const myAppName = 'My Stacks Web-App'; // shown in wallet pop-up
const myAppIcon = window.location.origin + '/my_logo.png'; // shown in wallet pop-up

showConnect({
  userSession, // `userSession` from previous step, to access storage
  appDetails: {
    name: myAppName,
    icon: myAppIcon,
  },
  onFinish: () => {
    window.location.reload(); // WHEN user confirms pop-up
  },
  onCancel: () => {
    console.log('oops'); // WHEN user cancels/closes pop-up
  },
});

Sending STX (openSTXTransfer)

Sending STX tokens is also possible through web-apps interacting with a user's wallet.

The snippet below will open the wallet to confirm and broadcast a smart-contract transaction. Here, we are sending 10000 micro-STX tokens to a recipient address.

import { openSTXTransfer } from '@stacks/connect';
import { StacksTestnet } from '@stacks/network';
import { AnchorMode, PostConditionMode } from '@stacks/transactions';
import { userSession } from './userSession';

openSTXTransfer({
  network: new StacksTestnet(), // which network to use; use `new StacksMainnet()` for mainnet
  anchorMode: AnchorMode.Any, // which type of block the tx should be mined in

  recipient: 'ST39MJ145BR6S8C315AG2BD61SJ16E208P1FDK3AK', // which address we are sending to
  amount: 10000, // tokens, denominated in micro-STX
  memo: 'Nr. 1337', // optional; a memo to help identify the tx

  onFinish: response => {
    // WHEN user confirms pop-up
    console.log(response.txid); // the response includes the txid of the transaction
  },
  onCancel: () => {
    // WHEN user cancels/closes pop-up
    console.log('User canceled');
  },
});

Calling Smart-Contracts (openContractCall)

Calling smart-contracts lets users interact with the blockchain through transactions.

The snippet below will open the wallet to confirm and broadcast a smart-contract transaction. Here, we are passing our pick Alice to an imaginary deployed voting smart-contract.

import { openContractCall } from '@stacks/connect';
import { StacksTestnet } from '@stacks/network';
import { AnchorMode, PostConditionMode, stringUtf8CV } from '@stacks/transactions';
import { userSession } from './userSession';

const pick = stringUtf8CV('Alice');

openContractCall({
  network: new StacksTestnet(),
  anchorMode: AnchorMode.Any, // which type of block the tx should be mined in

  contractAddress: 'ST39MJ145BR6S8C315AG2BD61SJ16E208P1FDK3AK',
  contractName: 'example-contract',
  functionName: 'vote',
  functionArgs: [pick],

  postConditionMode: PostConditionMode.Deny, // whether the tx should fail when unexpected assets are transferred
  postConditions: [], // for an example using post-conditions, see next example

  onFinish: response => {
    // WHEN user confirms pop-up
  },
  onCancel: () => {
    // WHEN user cancels/closes pop-up
  },
});

Sending transactions with post-conditions (openContractCall)

Consider the example above. Using post-conditions, a feature of the Stacks blockchain, we can ensure something happened after a transaction. Here, we could ensure that the recipient indeed receives a certain amount of STX.

import {
  PostConditionMode,
  FungibleConditionCode,
  makeStandardSTXPostCondition,
} from '@stacks/transactions';

// this post-condition ensures that our recipient receives at least 5000 STX tokens
const myPostCondition = makeStandardSTXPostCondition(
  'ST39MJ145BR6S8C315AG2BD61SJ16E208P1FDK3AK', // address of recipient
  FungibleConditionCode.GreaterEqual, // comparator
  5000000000 // relative amount to previous balance (denoted in micro-STX)
);

// passing to `openContractCall` options, e.g. modifying our previous example ...
  postConditionMode: PostConditionMode.Deny, // whether the tx should fail when unexpected assets are transferred
  postConditions: [ myPostCondition ],
// ...

For more examples on constructing different kinds of post-conditions read the Post-Conditions Guide of Stacks.js.

Post-Condition Modes

If post-conditions postConditions: [ ... ] are specified, they will ALWAYS be checked by blockchain nodes. If ANY conditions fails, the transaction will fail.

The Post-Condition Mode only relates to transfers of assets, which were not specified in the postConditions.

  • PostConditionMode.Deny fails the transaction if any unspecified assets are transferred
  • PostConditionMode.Allow allows unspecified assets to be transferred
  • In both cases, all postConditions are checked

🛠 Advanced

Opening a specific wallet

By default, @stacks/connect defers to the window.StacksProvider object to interact with wallets. However, if multiple wallets are installed, they might interfere with each other. To avoid this, you can specify which wallet to use in the wallet interaction methods.

// Only opens requests in Leather
authenticate({ ...opts }, LeatherProvider);
openPsbtRequestPopup({ ...opts }, LeatherProvider);
openProfileUpdateRequestPopup({ ...opts }, LeatherProvider);
openSignatureRequestPopup({ ...opts }, LeatherProvider);
openStructuredDataSignatureRequestPopup({ ...opts }, LeatherProvider);

🤔 Pitfalls

  • Connect can currently not set manual nonces, since this is not supported by wallets.
  • For some projects it might be necessary to also install the regenerator-runtime package. npm install --save-dev regenerator-runtime. This is a build issue of older versions of @stacks/connect.

📚 Method Parameters

A glossary of the most common options of openSTXTransfer and openContractCall

openSTXTransfer Required

DescriptionTypeExample
recipientThe recipient (STX principal) addressstring'ST39MJ145BR6S8C315AG2BD61SJ16E208P1FDK3AK'
amountThe amount (in micro-STX) to transferInteger (e.g. number, bigint)10000

openContractCall Required

DescriptionTypeExample
contractAddressThe (STX contract) address of the smart contractstring'ST39MJ145BR6S8C315AG2BD61SJ16E208P1FDK3AK'
contractNameThe contract namestring'example-contract'
functionNameThe contract function namestring'vote'
functionArgsThe contract function argumentsArray of Clarity Values[], [uintCV(100)]

Optional

DefaultDescriptionTypeExample
networkMainnetThe network to broadcast the transaction toStacksNetworknew StacksMainnet()
anchorModeAnyThe type of block the transaction should be mined inAnchorMode EnumAnchorMode.OnChainOnly
memoEmpty ''The memo field (used for additional data)string'a memo'
feeHandled by WalletThe transaction fee (the wallet will estimate fees as well)Integer (e.g. number, bigint)1000
postConditionModeDenyThe post condition mode, i.e. whether to allow unspecified asset transferPostConditionModePostConditionMode.Allow
postConditionsEmpty []The list of post conditions to check, regardless of postConditionModePostCondition[]
onFinishNo-opThe callback function to run after broadcasting the transactionFunction (receiving response)
onCancelNo-opThe callback function to run after the user cancels/closes the walletFunction
7.7.1

3 months ago

7.7.0

3 months ago

7.6.0

3 months ago

7.5.1

4 months ago

7.5.0

5 months ago

7.4.2

6 months ago

7.4.1

7 months ago

7.4.0

9 months ago

7.3.1

1 year ago

7.2.1

1 year ago

7.2.0

1 year ago

7.1.1

1 year ago

7.1.0

1 year ago

7.0.0

2 years ago

6.10.2

2 years ago

6.10.1

2 years ago

6.8.5

2 years ago

6.8.7

2 years ago

6.8.6

2 years ago

6.8.8

2 years ago

6.9.0

2 years ago

6.9.1

2 years ago

6.10.0

2 years ago

6.6.0

2 years ago

6.7.0

2 years ago

6.8.1

2 years ago

6.8.0

2 years ago

6.8.3

2 years ago

6.8.2

2 years ago

6.8.4

2 years ago

6.5.0

2 years ago

6.4.2

2 years ago

6.4.1

2 years ago

6.2.1

2 years ago

6.3.0

2 years ago

6.4.0

2 years ago

6.3.0-alpha.1

3 years ago

6.2.0

3 years ago

6.1.0

3 years ago

6.0.1

3 years ago

6.0.0

3 years ago

5.5.0

3 years ago

5.3.0

3 years ago

5.4.0

3 years ago

5.2.0

3 years ago

5.1.0

3 years ago

4.3.17

3 years ago

4.3.18

3 years ago

5.0.5

3 years ago

5.0.3

3 years ago

5.0.2

3 years ago

5.0.1

3 years ago

4.3.15

3 years ago

4.3.14

3 years ago

4.3.13

3 years ago

4.3.12

3 years ago

4.3.6

3 years ago

4.3.5

3 years ago

4.3.7

3 years ago

4.3.4

3 years ago

4.3.3

3 years ago

4.3.2

3 years ago

4.3.1

3 years ago

4.3.0

3 years ago

4.2.4

3 years ago

4.2.3

3 years ago

4.2.2

3 years ago

4.2.1

3 years ago

4.2.0

4 years ago

4.1.4

4 years ago

4.1.3

4 years ago

4.1.2

4 years ago

4.1.1

4 years ago

4.1.0

4 years ago

4.0.1

4 years ago

4.0.0

4 years ago

2.12.10

4 years ago