Reference
Core
Connect

Connect

micro-stacks/connect is the module that deals with interactions between wallets and your application. This means if you need to implement authentication or transaction signing, this library is playing a role in that.

More often than not, you as an app developer will never need to use this package directly. If you are building a React application, you should check out the docs on Authentication and Transactions.

Authentication

micro-stacks/connect exports an authenticate method that takes a few parameters:

AuthOptions

The authOptions object contains data specific to your application. This includes things like appDetails which is an object that contains the name and icon fields.

Additionally, you are able to pass some callbacks that happen at different events during the authentication process: onFinish and onCancel.

StorageAdapter

The storageAdapter parameter is to instruct where and how to save the session for your users. The default value for this is localStorage.

Serialize

This is the serialize function that will run when saving the authentication session. Default is JSON.stringify.

Transaction signing

micro-stacks/connect exports a number of functions to construct the tokens that will be sent to the wallet of your users for them to sign.

openTransactionPopup

import { openTransactionPopup } from 'micro-stacks/connect';
import type { FinishedTxData } from 'micro-stacks/connect';


function onFinish(data: FinishedTxData){
  console.log('finished!');
  console.log(data);
}

function onCancel(errorMessage: string){
  console.log('something went wrong!');
  console.log(errorMessage);
}

async function handleTxSigning(token: string){
  // promise will resolve when finished or canceled
  await openTransactionPopup({ token, onFinish, onCancel })
}

makeStxTransferToken

This method creates a STX token transfer token which would be sent to a wallet for a user to sign with their private keys.

makeContractCallToken

This method is used to construct the token that is sent over to the wallet for a user to sign.

import { bytesToHex, utf8ToBytes } from 'micro-stacks/common';
import { bufferCV, uintCV } from 'micro-stacks/clarity';
import { getRandomBytes, hashRipemd160 } from 'micro-stacks/crypto';
import { hashSha256 } from 'micro-stacks/crypto-sha';

// import our method
import { makeContractCallToken } from 'micro-stacks/connect';

async function createBnsPreorderToken(): Promise<string> {
    const privateKey = bytesToHex(getRandomBytes()); // this would be the appPrivateKey
    const salt = 'asdadasdasda';
    const fqn = 'jude.id';
    const sha256 = hashSha256(utf8ToBytes(`${fqn}${salt}`));
    const hash160 = hashRipemd160(sha256);

    // create the token
    const token = await makeContractCallToken({
      privateKey,
      functionName: 'name-preorder',
      contractAddress: 'SP000000000000000000002Q6VF78',
      contractName: 'bns',
      functionArgs: [bufferCV(hash160), uintCV(200000)],
    });

    return token
}