Skip to content

Azguard Wallet client fully compatible with Aztec.js' Wallet interface

License

Notifications You must be signed in to change notification settings

AzguardWallet/aztec-wallet

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

9 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

@azguardwallet/aztec-wallet

GitHub License NPM Version NPM Downloads

Azguard Wallet client fully compatible with Aztec.js' Wallet interface, enabling seamless integration.

How to use

Install the package:

npm install @azguardwallet/aztec-wallet

Connect the wallet:

import { AztecWallet } from "@azguardwallet/aztec-wallet";

// the simplest way
const wallet = await AztecWallet.connect();

// or you can additionally provide the dapp metadata and the chain
const wallet = await AztecWallet.connect(
    {
        name: "My Dapp",
        description: "The best dapp in the world",
        logo: "...",
        url: "..."
    },
    "devnet" // or "sandbox", or CAIP-string like "aztec:604129785"
);

Then use this wallet for interaction with Aztec.js:

import { AztecAddress } from '@aztec/aztec.js/addresses';
import { SponsoredFeePaymentMethod } from '@aztec/aztec.js/fee';
import { createAztecNodeClient } from '@aztec/aztec.js/node';
import { TokenContract, TokenContractArtifact } from '@aztec/noir-contracts.js/Token';

const accounts = await wallet.getAccounts();
const address = accounts[0].item;

const tokenAddress = AztecAddress.fromString("0x...");
const tokenContract = TokenContract.at(tokenAddress, wallet);

// register token contract in user's PXE

const node = createAztecNodeClient("https://next.devnet.aztec-labs.com");
const tokenInstance = await node.getContract(tokenAddress);
await wallet.registerContract(tokenInstance, TokenContractArtifact);

// get token private balance

const prvBalance = await tokenContract.methods
    .balance_of_private(address)
    .simulate({from: address});

console.log("Private balance", prvBalance);

// get token public balance

const pubBalance = await tokenContract.methods
    .balance_of_public(address)
    .simulate({from: address});

console.log("Public balance", pubBalance);

// send token

const feeOptions = {
    paymentMethod: new SponsoredFeePaymentMethod(
        AztecAddress.fromString("0x..."),
    ),
};

const txReceipt = await tokenContract.methods
    .transfer(AztecAddress.fromString("0x..."), 100000000n)
    .send({from: address, fee: feeOptions});

console.log("Tx hash", txReceipt.txHash);

That's pretty much it! See more Aztec.js examples at https://docs.aztec.network.

Connection state

When Azguard user confirms connection from a dapp (AztecWallet.connect()), a dapp session is opened, and while this session is active, the dapp is allowed to interact with the wallet. If the dapp session is expired or closed (either by the dapp or by the user), the interaction is no longer allowed.

Even though the AztecWallet client reconnects automatically, you might want to additionally track the connection state. You can do the following:

// connect the wallet
// (it's lazily called under the hood, so you don't have to call it manually,
// unless you want to connect immediately)
await wallet.connect();

// check if the wallet is connected
console.log(wallet.connected);

// track connection/disconnection events
wallet.onConnected.addHandler(() => console.log("Azguard connected"));
wallet.onDisconnected.addHandler(() => console.log("Azguard disconnected"));

// in the end you can disconnect and close the session
await wallet.disconnect();

Support channels

If you have any questions, feel free to contact us in:

Cheers! 🍺

About

Azguard Wallet client fully compatible with Aztec.js' Wallet interface

Resources

License

Stars

Watchers

Forks

Packages

 
 
 

Contributors