Skip to main content

Wallet Extended

The wallet extended service leverages Anvil's infrastructure to retrieve information about all the addresses associated with a wallet.

getAuthPayload()

To authenticate the wallet, the user must sign a payload (using the signTx function) that our backend will subsequently verify in our auth function. The app name can be chosen arbitrarily and can be any value of your preference, as long as you ensure consistency with it throughout the process.

import { anvil } from "@ada-anvil/sdk-server";

try {
await anvil.walletExt.getAuthPayload({ app: "YOUR_APP_NAME" });
// Output: '4279207369676e696e67207468697320746f6b...'
} catch {
console.log("YOUR ERROR HANDLING");
}

auth()

Once the user has signed the payload, you are required to send the signature to our backend for authentication. If the authentication process is successful, the endpoint will return the stake address and your app name as the result. At this point, the user is considered authenticated.

Address comparisons

Since a stake address can be associated with multiple addresses, it is crucial to compare the stake address rather than the wallet address itself. In the event that the stake address of a wallet changes, it is necessary to repeat the authentication process.

import { anvil } from "@ada-anvil/sdk-server";

try {
await anvil.walletExt.auth({
signature: "YOUR_SIGNATURE",
key: "YOUR_OPTIONNAL_KEY",
app: "YOUR_APP_NAME",
});
// Output: {stake: 'stake1u95qwe6f8srqhuv8jt7...', app: 'YOUR_APP_NAME'}
} catch {
console.log("YOUR ERROR HANDLING");
}

getAccountBalanceByPolicies()

The function returns an object containing key-value pairs, where the key represents the policy ID, and the value represents a mapping of the asset ID to its corresponding quantity for all addresses of the stake address.

import { anvil } from "@ada-anvil/sdk-server";

try {
await anvil.walletExt.getAccountBalanceByPolicies({ stakeAddress: "YOUR_STAKE_ADDRESS" });
// Output: { "cardano" : { "lovelace": 300000000 }, "1af660e4c58514a2f0ea167deca340381e55bed4aea60bc09c211417": { "the_asset_name_03": 1 } }
} catch {
console.log("YOUR ERROR HANDLING");
}

getAssets()

This function retrieves the assets and their metadata in the utxos. If you need to retrieve the assets for the entire wallet, please use the getAccountAssets() function.

import { anvil } from "@ada-anvil/sdk-server";

try {
await anvil.walletExt.getAssets({
utxos: [],
policies: ["YOUR_POLICY"],
});
// Output: [
// {
// "id": 755013,
// "unit": "1af660e4c58514a2f0ea167deca340381e55bed4aea60bc09c211417416e76696c5465737431303037",
// "policyId": "1af660e4c58514a2f0ea167deca340381e55bed4aea60bc09c211417",
// "assetName": "AnvilTest1007",
// "metadata": { ... }
// }
// ]
} catch {
console.log("YOUR ERROR HANDLING");
}

getAccountAssets()

This function retrieves the assets and their metadata owned by the stake address.

Account balance

If your objective is only to obtain the quantity of each asset without the metadata, it is recommended to use the getAccountBalanceByPolicies() function instead.

import { anvil } from "@ada-anvil/sdk-server";

try {
await anvil.walletExt.getAccountAssets({
policies: ["YOUR_POLICY"],
stakeAddress: "YOUR_STAKE_ADDRESS",
});
// Output:
// [
// {
// id: 755013,
// unit: "1af660e4c58514a2f0ea167deca340381e55bed4aea60bc09c211417416e76696c5465737431303037",
// policyId: "1af660e4c58514a2f0ea167deca340381e55bed4aea60bc09c211417",
// assetName: "AnvilTest1007",
// metadata: { ... },
// },
// ]
} catch {
console.log("YOUR ERROR HANDLING");
}

sendAssets()

This function enables you to send lovelace, FT (fungible tokens), and/or NFT (non-fungible tokens). It supports various address formats such as bech32, hexa, or ada handle addresses. Additionally, you can specify multiple destination addresses and include multiple asset types within the same transaction.

Sign & Submit

This function generates the transaction to be signed by the user. After obtaining the signature, you will need to submit both the transaction and the signature using the submit function.

import { anvil } from "@ada-anvil/sdk-server";

try {
await anvil.walletExt.sendAssets({
outputs: [{ assets: [{ unit: "YOUR_UNIT", quantity: 1 }], address: "YOUR_ADDRESS" }],
});
// Output: 'f530193a136...'
} catch {
console.log("YOUR ERROR HANDLING");
}

submit()

The submit function necessitates both the original transaction and the wallet's signature for further processing. You can obtain the signature by prompting the user to sign the transaction on the front-end using the signTx function.

import { anvil } from "@ada-anvil/sdk-server";

try {
await anvil.walletExt.submit({ signature: "YOUR_SIG", transaction: "YOUR_TX" });
// Output:
// {
// transactionHash: "f530193a136...";
// submittedTx: "19f5303a1f...";
// }
} catch {
console.log("YOUR ERROR HANDLING");
}