@perawallet/connect
TypeScript icon, indicating that this package has built-in type declarations

1.3.5 • Public • Published

Pera Connect Cover Image

@perawallet/connect

JavaScript SDK for integrating Pera Wallet to web applications. For more detailed information, please check our Pera Connect Docs.

Getting Started

Learn how to integrate with your JavaScript application

Learn how to Sign Transactions

Try it out using CodeSandbox

Example Applications

Expand details

Quick Start

Let's start with installing @perawallet/connect

npm install --save @perawallet/connect
// Connect handler
peraWallet
  .connect()
  .then((newAccounts) => {
    // Setup the disconnect event listener
    peraWallet.connector?.on("disconnect", handleDisconnectWalletClick);

    setAccountAddress(newAccounts[0]);
  })
  .catch((error) => {
    // You MUST handle the reject because once the user closes the modal, peraWallet.connect() promise will be rejected.
    // For the async/await syntax you MUST use try/catch
    if (error?.data?.type !== "CONNECT_MODAL_CLOSED") {
      // log the necessary errors
    }
  });

If you don't want the user's account information to be lost by the dApp when the user closes the browser with user’s wallet connected to the dApp, you need to handle the reconnect session status. You can do this in the following way.

// On the every page refresh
peraWallet.reconnectSession().then((accounts) => {
  // Setup the disconnect event listener
  peraWallet.connector?.on("disconnect", handleDisconnectWalletClick);

  if (accounts.length) {
    setAccountAddress(accounts[0]);
  }
});

After that you can sign transaction with this way

// Single Transaction
try {
  const signedTxn = await peraWallet.signTransaction([singleTxnGroups]);
} catch (error) {
  console.log("Couldn't sign Opt-in txns", error);
}

Options

option default value
chainId 4160 416001, 416002, 416003 , 4160 optional
shouldShowSignTxnToast true boolean optional
compactMode false boolean optional

chainId

Determines which Algorand network your dApp uses.

MainNet: 416001

TestNet: 416002

BetaNet: 416003

All Networks: 4160

shouldShowSignTxnToast

Group 48096937

It's enabled by default but in some cases, you may not need the toast message (e.g. you already have signing guidance for users). To disable it, use the shouldShowSignTxnToast option.

compactMode

It offers a compact UI optimized for smaller screens, with a minimum resolution of 400x400 pixels.

Methods

PeraWalletConnect.connect(): Promise<string[]>

Starts the initial connection flow and returns the array of account addresses.

PeraWalletConnect.reconnectSession(): Promise<string[]>

Reconnects to the wallet if there is any active connection and returns the array of account addresses.

PeraWalletConnect.disconnect(): Promise<void | undefined>

Disconnects from the wallet and resets the related storage items.

PeraWalletConnect.platform: PeraWalletPlatformType

Returns the platform of the active session. Possible responses: mobile | web | null

PeraWalletConnect.isConnected: boolean

Checks if there's any active session regardless of platform. Possible responses: true | false

PeraWalletConnect.isPeraDiscoverBrowser: boolean

Checks if it is on Pera Discover Browser. Possible responses: true | false

PeraWalletConnect.signTransaction(txGroups: SignerTransaction[][], signerAddress?: string): Promise<Uint8Array[]>

Starts the sign process and returns the signed transaction in Uint8Array

PeraWalletConnect.signData(data: PeraWalletArbitraryData[], signer: string): Promise<Uint8Array[]>

Starts the signing process for arbitrary data signing and returns the signed data in Uint8Array. Uses signBytes method of algosdk behind the scenes. signer should be a valid Algorand address that exists in the user's wallet.

See example
const signedData: Uint8Array[] = await peraWallet.signData([
  {
    data: new Uint8Array(Buffer.from(`timestamp//${Date.now()}`)),
    message: "Timestamp confirmation"
  },
  {
    data: new Uint8Array(Buffer.from(`agent//${navigator.userAgent}`)),
    message: "User agent confirmation"
  }
], "SAHBJDRHHRR72JHTWSXZR5VHQQUVC7S757TJZI656FWSDO3TZZWV3IGJV4");

Customizing Style

You can override the z-index using the .pera-wallet-modal class so that the modal does not conflict with another component on your application.

.pera-wallet-modal {
  // The default value of z-index is 10. You can lower and raise it as much as you want.
  z-index: 11;
}

Your app name on Pera Wallet

By default, the connect wallet drawer on Pera Wallet gets the app name from document.title.

In some cases, you may want to customize it. You can achieve this by adding a meta tag to your HTML between the head tag.

<meta name="name" content="My dApp" />

Contributing

All contributions are welcomed! To get more information about the details, please read the contribution guide first.

Versions

Current Tags

VersionDownloads (Last 7 Days)Tag
1.3.5914latest

Version History

VersionDownloads (Last 7 Days)Published
1.3.5914
1.3.44,413
1.3.342
1.3.20
1.3.135
1.2.41
1.2.327
1.2.221
1.2.10
1.2.00
1.1.12,496
1.1.05
1.0.712
1.0.60
1.0.50
1.0.40
1.0.30
1.0.20
1.0.10
1.0.01
0.2.32
0.2.20
0.2.10
0.2.00
0.1.60
0.1.50
0.1.40
0.1.30
0.1.20
0.1.10
0.1.00
0.0.121
0.0.110
0.0.100
0.0.90
0.0.80
0.0.70
0.0.60
0.0.50
0.0.40
0.0.30
0.0.20
0.0.10

Package Sidebar

Install

npm i @perawallet/connect

Weekly Downloads

7,971

Version

1.3.5

License

ISC

Unpacked Size

337 kB

Total Files

136

Last publish

Collaborators

  • yasincaliskan
  • yigitguler
  • taylanpince