Mesh Wallet
Mesh Wallet provides a set of APIs to interact with the blockchain. This wallet is compatible with Mesh transaction builders.
Whether you are building a minting script, or an application that requires multi-signature, MeshWallet
is all you need to get started.
Initialize Wallet
This API enables applications to load and initialize a wallet connection. It provides access to the wallet's capabilities, such as signing transactions, submitting transactions, and querying blockchain data.
The wallet connection is established securely, ensuring that sensitive operations are handled by the wallet and not exposed to the application directly. This is crucial for maintaining security and user trust.
Applications can use this functionality to integrate wallet features seamlessly, enabling blockchain interactions without requiring users to manage private keys manually.
You can initialize Mesh Wallet with:
- Mnemonic Phrases
- Private Keys
- Cardano CLI Generated Keys
- Address (Read Only Wallet)
First, we initialize a Provider, which we will assign provider to the fetcher
and submitter
.
import { MaestroProvider } from '@meshsdk/core';
const provider = new MaestroProvider({
network: 'Preprod',
apiKey: '<Your-API-Key>', // Get yours by visiting https://docs.gomaestro.org/.
turboSubmit: false
});
import { BlockfrostProvider } from '@meshsdk/core';
const provider = new BlockfrostProvider('<BLOCKFROST_API_KEY>');
import { KoiosProvider } from '@meshsdk/core';
const provider = new KoiosProvider('<api|preview|preprod|guild>', '<token>');
import { U5CProvider } from "@meshsdk/core";
const provider = new U5CProvider({
url: "http://localhost:5005U5c",
headers: {
"dmtr-api-key": "<api-key>",
},
});
Menmonice Phrases
We can load wallet with menmonice phrases:
import { MeshWallet } from '@meshsdk/core';
const wallet = new MeshWallet({
networkId: 0, // 0: testnet, 1: mainnet
fetcher: provider,
submitter: provider,
key: {
type: 'mnemonic',
words: ["solution","solution","solution","solution","solution","solution","solution","solution","solution","solution","solution","solution","solution","solution","solution","solution","solution","solution","solution","solution","solution","solution","solution","solution"],
},
});
With the wallet
loaded, you can sign transactions, we will see how to do this in the next section, for now lets get the wallet's address:
const address = wallet.getChangeAddress();
Private Keys
We can load wallet with private keys:
import { MeshWallet } from '@meshsdk/core';
const wallet = new MeshWallet({
networkId: 0, // 0: testnet, 1: mainnet
fetcher: provider,
submitter: provider,
key: {
type: 'root',
bech32: 'xprv1cqa46gk29plgkg98upclnjv5t425fcpl4rgf9mq2txdxuga7jfq5shk7np6l55nj00sl3m4syzna3uwgrwppdm0azgy9d8zahyf32s62klfyhe0ayyxkc7x92nv4s77fa0v25tufk9tnv7x6dgexe9kdz5gpeqgu',
},
});
Cardano CLI generated skeys
We can load wallet with CLI generated keys by providing the skey
generated by Cardano CLI. There are two files generated by Cardano CLI, by default it is named signing.skey
and stake.skey
. Opening the signing.skey
file it should contains:
{
"type": "PaymentSigningKeyShelley_ed25519",
"description": "Payment Signing Key",
"cborHex": "5820aaca553a7b95b38b5d9b82a5daa7a27ac8e34f3cf27152a978f4576520dd6503"
}
We can get the cborHex
from the signing.skey
file, and load wallet with Cardano CLI generated skeys. Stake key is optional, but without it, you cannot sign staking transactions.
import { MeshWallet } from '@meshsdk/core';
const wallet = new MeshWallet({
networkId: 0, // 0: testnet, 1: mainnet
fetcher: provider,
submitter: provider,
key: {
type: 'cli',
payment: '5820aaca553a7b95b38b5d9b82a5daa7a27ac8e34f3cf27152a978f4576520dd6503',
stake: '582097c458f19a3111c3b965220b1bef7d548fd75bc140a7f0a4f080e03cce604f0e',
},
});
Address
We can load wallet with address, this is useful for read-only wallets. A read-only wallet can only query the blockchain, it cannot sign transactions. This is useful for monitoring wallets. We can load wallet with the address
type:
import { MeshWallet } from '@meshsdk/core';
const wallet = new MeshWallet({
networkId: 0, // 0: testnet, 1: mainnet
fetcher: provider,
key: {
type: 'address',
address: 'addr_test1qpvx0sacufuypa2k4sngk7q40zc5c4npl337uusdh64kv0uafhxhu32dys6pvn6wlw8dav6cmp4pmtv7cc3yel9uu0nq93swx9',
},
});
Initialize Wallet
After creating the wallet, we need to initialize it. This will initialize the cryptography library.
await wallet.init()
With the wallet
loaded, you can sign transactions, we will see how to do this in the next section, for now lets get the wallet's address:
const address = wallet.getChangeAddress();
Generate Wallet
You can generate deterministic keys based on the Bitcoin BIP39
. These mnemonic phrases are essential for recovering your wallet and ensuring secure access to your funds.
const mnemonic = MeshWallet.brew();
Once you have your mnemonic phrase, you can use it to generate deterministic keys. These keys include a series of private and public keys, which are crucial for managing your cryptocurrencies securely.
Alternatively, you can generate private keys directly by passing true
to the brew
function. This approach is useful for scenarios where you need immediate access to private keys without mnemonic phrases.
const privatekey = MeshWallet.brew(true);
Get Balance
This API returns a comprehensive list of all assets in the wallet, including lovelace. Each asset is represented as an object with the following properties:
unit
: A unique identifier for the asset, often used for display purposes.quantity
: The amount of the asset held in the wallet.
Example response:
[
{
"unit": "lovelace",
"quantity": "796105407"
},
{
"unit": "0f5560dbc05282e05507aedb02d823d9d9f0e583cce579b81f9d1cd8",
"quantity": "1"
},
{
"unit": "9c8e9da7f81e3ca90485f32ebefc98137c8ac260a072a00c4aaf142d4d657368546f6b656e",
"quantity": "2"
},
]
Get Change Address
This API returns an address owned by the wallet that should be used as a change address. A change address is essential during transaction creation to ensure leftover assets are securely returned to the connected wallet.
The change address helps maintain the integrity of transactions by preventing asset loss and ensuring proper allocation of funds.
Get Change Address
Get address that should be used for transaction's change
const changeAddress = await wallet.getChangeAddress();
Get Collateral
This API retrieves a list of UTXOs (unspent transaction outputs) controlled by the wallet that are suitable for use as collateral inputs in transactions involving Plutus script inputs. Collateral UTXOs are pure ADA-only UTXOs required to meet the specified ADA value target.
If the target cannot be met, an error message explaining the issue will be returned. Wallets may return UTXOs exceeding the target value but must never return UTXOs below the specified value.
This API accepts the addressType
parameter, where you can specify the type of address you want to get. The available options are:
- payment (default)
- enterprise
Get Collateral
Get list of UTXOs that used as collateral inputs for transactions with plutus script inputs
const collateralUtxos = await wallet.getCollateral();
Example response:
[
{
"input": {
"outputIndex": 1,
"txHash": "ff8d1e97c60989b4f...02ee937595ad741ff597af1"
},
"output": {
"address": "addr_test1qzm...z0fr8c3grjmysm5e6yx",
"amount": [ { "unit": "lovelace", "quantity": "5000000" } ]
}
}
]
Get Network ID
This API returns the network ID of the currently connected account. The network ID indicates the environment in which the wallet is operating:
0
: Testnet1
: Mainnet
Other network IDs may be returned by wallets, but these are not governed by CIP-30. The network ID remains consistent unless the connected account changes.
Get Reward Addresses
This API retrieves a list of reward addresses owned by the wallet. Reward addresses are stake addresses used to receive rewards from staking activities.
Reward addresses typically start with the stake
prefix, making them easily identifiable. These addresses are essential for tracking staking rewards and managing staking operations.
Example response:
[
"stake_test1uzx0ksy9f4qnj2mzfdncqyjy84sszh64w43853nug5pedjgytgke9"
]
Get Unused Addresses
This API retrieves a list of unused addresses controlled by the wallet. Unused addresses are wallet-controlled addresses that have not been involved in any transactions.
Unused addresses are important for maintaining privacy and security in transactions. They can be used for new transactions without revealing previous activity.
Get Unused Addresses
Get addresses that are unused
const unusedAddresses = wallet.getUnusedAddresses();
Example response:
[
"addr_test1qzk9x08mtre4jp8f7j8zu8802...r8c3grjmys7fl22c",
"addr_test1qrmf35xyw2petfr0e0p4at0r7...8sc3grjmysm73dk8",
"addr_test1qq6ts58hdaasd2q78fdjj0arm...i8c3grjmys85k8mf",
]
Get Used Addresses
This API retrieves a list of used addresses controlled by the wallet. Used addresses are wallet-controlled addresses that have been involved in transactions.
Tracking used addresses is essential for analyzing transaction history and managing wallet activity. These addresses provide insights into past transactions.
Example response:
[
"addr_test1qzk9x08mtre4jp8f7j8zu8802...r8c3grjmys7fl88a",
"addr_test1qrmf35xyw2petfr0e0p4at0r7...8sc3grjmysm76gt3",
"addr_test1qq6ts58hdaasd2q78fdjj0arm...i8c3grjmys85dn39",
]
Get UTXOs
This API retrieves a list of all UTXOs (unspent transaction outputs) controlled by the wallet. UTXOs are essential for constructing transactions and managing wallet balances.
Each UTXO includes details such as the transaction hash, output index, address, and amount. These details are crucial for identifying and utilizing unspent outputs.
This API accepts the addressType parameter, where you can specify the type of address you want to get. The available options are:
- payment (default)
- enterprise
Example response:
[
{
"input": {
"outputIndex": 0,
"txHash": "16dcbb1f93b4f9d5e...9106c7b121463c210ba"
},
"output": {
"address": "addr_test1qzag7whju08xwrq...z0fr8c3grjmysgaw9y8",
"amount": [
{
"unit": "lovelace",
"quantity": "1314550"
},
{
"unit": "f05c91a850...3d824d657368546f6b656e3032",
"quantity": "1"
}
]
}
}
]
Sign Data
This API allows applications to request the signing of arbitrary data using the private keys managed by the connected wallet. This is useful for verifying the authenticity of data or creating cryptographic proofs.
The wallet ensures that the signing process is secure and that private keys are not exposed during the operation. The signed data can be used for various purposes, such as authentication, data integrity checks, or blockchain interactions.
This functionality is essential for applications that require secure and verifiable data signing capabilities.
Sign Data
Define a payload and sign it with wallet.
Payload: mesh
const signature = await wallet.signData('mesh');
Continue reading this guide to learn how to verify the signature.
Example response:
{
"signature": "845846a2012...f9119a18e8977d436385cecb08",
"key": "a4010103272006215...b81a7f6ed4fa29cc7b33186c"
}
Sign Transaction
This API enables applications to request the signing of a transaction using the private keys managed by the connected wallet. Signing a transaction is a critical step in ensuring its authenticity and authorization.
The wallet ensures that the transaction is signed securely, preventing unauthorized access to private keys. Once signed, the transaction can be submitted to the blockchain network for processing.
This functionality is vital for applications that need to interact with the blockchain securely, as it delegates sensitive operations to the wallet.
Sign Transaction
Create a transaction and sign it
Check out Transaction to learn more on how to use this API.
const signedTx = await wallet.signTx(tx, partialSign?);
Submit Transaction
This API allows applications to request the submission of a signed transaction through the connected wallet. The wallet will attempt to send the transaction to the blockchain network.
If the transaction is successfully submitted, the wallet returns the transaction ID, which can be used by the application to track its status on the blockchain. In case of an error during submission, the wallet provides error messages or failure details.
This functionality is essential for applications that rely on wallet integration to handle transaction submission securely and efficiently.
Submit Transaction
Submit a signed transaction with wallet
Check out Transaction to learn more on how to use this API.
const txHash = await wallet.submitTx(signedTx);
Create Collateral UTXO
Collateral is a monetary guarantee provided by the user to ensure the integrity of smart contracts and compensate nodes in case phase-2 validation fails. It is specified during transaction construction by adding collateral inputs to the transaction.
The total balance in the UTXOs corresponding to these inputs represents the transaction's collateral amount. If the contract executes successfully, the collateral remains safe. This mechanism ensures that contracts are carefully designed and thoroughly tested.
const txhash = await wallet.createCollateral();
Get Assets
This API retrieves a list of assets in the wallet, excluding lovelace. Each asset is represented as an object with the following properties:
unit
: A unique identifier for the asset.policyId
: The policy ID associated with the asset.assetName
: The name of the asset.fingerprint
: A unique fingerprint for the asset.quantity
: The amount of the asset held in the wallet.
Example response:
[
{
"unit": "1207329a668cf5c42b80a220a8c85d5e82ac0b6f5ecedda4c07a8acc4d657368486f6e6f72546f6b656e2d3530343935",
"policyId": "1207329a668cf5c42b80a220a8c85d5e82ac0b6f5ecedda4c07a8acc",
"assetName": "Mesh Token Of Appreciation",
"fingerprint": "asset1dw74h0w0meqg9cxkc9sezp8zqcxu8nl93fzfpz",
"quantity": "1"
}
{
"unit": "9c8e9da7f81e3ca90485f32ebefc98137c8ac260a072a00c4aaf142d4d657368546f6b656e",
"policyId": "9c8e9da7f81e3ca90485f32ebefc98137c8ac260a072a00c4aaf142d",
"assetName": "MeshToken",
"fingerprint": "asset177e7535dclmkkph8ewt9fsghllkwmpspa3n98p",
"quantity": "10"
}
]
Get Lovelace
This API retrieves the lovelace balance in the wallet. Lovelace is the smallest unit of ADA, where 1 ADA equals 1,000,000 lovelace.
Knowing the lovelace balance is essential for managing wallet funds and performing transactions.
Get Lovelace
Get lovelace balance in the connected wallet
const lovelace = await wallet.getLovelace();
Get Policy IDs
This API retrieves a list of policy IDs from all assets in the wallet. A policy ID is a unique identifier that groups assets under a common policy.
Policy IDs are useful for querying assets associated with specific policies. For example, you can use a policy ID to retrieve all assets belonging to that policy.
Get Policy IDs
Get a list of policy IDs from all assets in wallet
const policyIds = await wallet.getPolicyIds();
Example response:
[
"0f5560dbc05282e05507aedb02d823d9d9f0e583cce579b81f9d1cd8",
"5bed9e89299c69d9a54bbc82d88aa5a86698b2b7b9d0ed030fc4b0ff",
"9c8e9da7f81e3ca90485f32ebefc98137c8ac260a072a00c4aaf142d",
]
Get a Collection of Assets
This API retrieves a list of assets associated with a specific policy ID. A policy ID is a unique identifier that groups assets under a common policy.
If no assets in the wallet belong to the specified policy ID, an empty list is returned. To query for available policy IDs, use wallet.getPolicyIds()
.
Get a Collection of Assets
Get a list of assets belonging to the policy ID
Policy ID: d9312da562da182b02322fd8acb536f37eb9d29fba7c49dc17255527
const assets = await wallet.getPolicyIdAssets('d9312da562da182b02322fd8acb536f37eb9d29fba7c49dc17255527');
Get DRep
The DRep ID is a unique identifier for the user's wallet. It consists of three components:
publicKey
: The public key associated with the wallet.publicKeyHash
: A hash of the public key for verification purposes.dRepIDCip105
: The bech32 encoding of the DRep ID.
Example response:
{
"publicKey": "6984e406dd81...39e43d798fe1a89ab",
"publicKeyHash": "9f7f4b78...df83bd227e943e9808450",
"dRepIDCip105": "drep1vz0h7jmc...0axqgg5q4dls5u"
}