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
You can initialize Mesh Wallet with:
- mnemonic phrases
- Cardano CLI generated keys
- private keys
Lets import a blockchain provider:
import { BlockfrostProvider } from '@meshsdk/core'; const blockchainProvider = new BlockfrostProvider('<BLOCKFROST_API_KEY>');
Mnemonic phrases
We can load wallet with mnemonic phrases:
import { MeshWallet } from '@meshsdk/core'; const wallet = new MeshWallet({ networkId: 0, fetcher: blockchainProvider, submitter: blockchainProvider, 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, fetcher: blockchainProvider, submitter: blockchainProvider, key: { type: 'root', bech32: 'xprv1cqa46gk29plgkg98upclnjv5t425fcpl4rgf9mq2txdxuga7jfq5shk7np6l55nj00sl3m4syzna3uwgrwppdm0azgy9d8zahyf32s62klfyhe0ayyxkc7x92nv4s77fa0v25tufk9tnv7x6dgexe9kdz5gpeqgu', }, });
Generate Wallet
You can generate deterministic keys based on the Bitcoin BIP39. These mnemonic phrases allow you to recover your wallet.
const mnemonic = MeshWallet.brew();
Once you have your mnemonic phrase, you can use it to generate your deterministic keys. It will typically generate a series of private keys and corresponding public keys, which you can use to manage your cryptocurrencies.
You can also generate private keys directly by adding true in the brew function.
const privatekey = MeshWallet.brew(true);
Generate new mnemonic phrases for your wallet
const mnemonic = MeshWallet.brew();
Get Balance
Returns a list of assets in the wallet. This API will return every assets in the wallet. Each asset is an object with the following properties:
- A unit is provided to display asset's name on the user interface.
- A quantity is provided to display asset's quantity on the user interface.
Example:
[ { "unit": "lovelace", "quantity": "796105407" }, { "unit": "0f5560dbc05282e05507aedb02d823d9d9f0e583cce579b81f9d1cd8", "quantity": "1" }, { "unit": "9c8e9da7f81e3ca90485f32ebefc98137c8ac260a072a00c4aaf142d4d657368546f6b656e", "quantity": "2" }, ]
Get all assets in the connected wallet
const balance = await wallet.getBalance();
Get Change Address
Returns an address owned by the wallet that should be used as a change address to return leftover assets during transaction creation back to the connected wallet.
Get address that should be used for transaction's change
const changeAddress = await wallet.getChangeAddress();
Get Collateral
This function shall return a list of one or more UTXOs (unspent transaction outputs) controlled by the wallet that are required to reach AT LEAST the combined ADA value target specified in amount AND the best suitable to be used as collateral inputs for transactions with plutus script inputs (pure ADA-only UTXOs).
If this cannot be attained, an error message with an explanation of the blocking problem shall be returned. NOTE: wallets are free to return UTXOs that add up to a greater total ADA value than requested in the amount parameter, but wallets must never return any result where UTXOs would sum up to a smaller total ADA value, instead in a case like that an error message must be returned.
Example of a response returned by this endpoint:
[ { "input": { "outputIndex": 1, "txHash": "ff8d1e97c60989b4f...02ee937595ad741ff597af1" }, "output": { "address": "addr_test1qzm...z0fr8c3grjmysm5e6yx", "amount": [ { "unit": "lovelace", "quantity": "5000000" } ] } } ]
Get list of UTXOs that used as collateral inputs for transactions with plutus script inputs
const collateralUtxos = await wallet.getCollateral();
Get Network ID
Returns the network ID of the currently connected account. 0
is testnet and 1
is mainnet but other networks can possibly be returned by wallets. Those other network ID values are not governed by CIP-30. This result will stay the same unless the connected account has changed.
Get currently connected network
const networkId = await wallet.getNetworkId();
Get Reward Addresses
Returns a list of reward addresses owned by the wallet. A reward address is a stake address that is used to receive rewards from staking, generally starts from `stake` prefix. Example:
[ "stake_test1uzx0ksy9f4qnj2mzfdncqyjy84sszh64w43853nug5pedjgytgke9" ]
Get stake addresses
const rewardAddresses = await wallet.getRewardAddresses();
Get Unused Addresses
Returns a list of unused addresses controlled by the wallet. For example:
[ "addr_test1qzk9x08mtre4jp8f7j8zu8802...r8c3grjmys7fl22c", "addr_test1qrmf35xyw2petfr0e0p4at0r7...8sc3grjmysm73dk8", "addr_test1qq6ts58hdaasd2q78fdjj0arm...i8c3grjmys85k8mf", ]
Get addresses that are unused
const unusedAddresses = await wallet.getUnusedAddresses();
Get Used Addresses
Returns a list of used addresses controlled by the wallet. For example:
[ "addr_test1qzk9x08mtre4jp8f7j8zu8802...r8c3grjmys7fl88a", "addr_test1qrmf35xyw2petfr0e0p4at0r7...8sc3grjmysm76gt3", "addr_test1qq6ts58hdaasd2q78fdjj0arm...i8c3grjmys85dn39", ]
Get addresses that are used
const usedAddresses = await wallet.getUsedAddresses();
Get UTXOs
Return a list of all UTXOs (unspent transaction outputs) controlled by the wallet. For example:
[ { "input": { "outputIndex": 0, "txHash": "16dcbb1f93b4f9d5e...9106c7b121463c210ba" }, "output": { "address": "addr_test1qzag7whju08xwrq...z0fr8c3grjmysgaw9y8", "amount": [ { "unit": "lovelace", "quantity": "1314550" }, { "unit": "f05c91a850...3d824d657368546f6b656e3032", "quantity": "1" } ] } } ]
Get UTXOs of the connected wallet
const utxos = await wallet.getUtxos();
Sign Data
This endpoint utilizes the CIP-8 - Message Signing to sign arbitrary data, to verify the data was signed by the owner of the private key.
Here, we get the first wallet's address with wallet.getUsedAddresses()
, alternativelly you can use reward addresses (getRewardAddresses()
) too. It's really up to you as the developer which address you want to use in your application.
Example of a response from the endpoint:
{ "signature": "845846a2012...f9119a18e8977d436385cecb08", "key": "a4010103272006215...b81a7f6ed4fa29cc7b33186c" }
Continue reading this guide to learn how to verify the signature.
Use connected wallet to sign a payload
const signature = await wallet.signData('mesh');
Sign Transaction
Requests user to sign the provided transaction (tx
). The wallet should ask the user for permission, and if given, try to sign the supplied body and return a signed transaction. partialSign
should be true
if the transaction provided requires multiple signatures.
Use connected wallet to sign a transaction
const signedTx = await wallet.signTx(tx, partialSign?);
Check out Transaction to learn more on how to use this API.
Submit Transaction
As wallets should already have this ability to submit transaction, we allow dApps to request that a transaction be sent through it. If the wallet accepts the transaction and tries to send it, it shall return the transaction ID for the dApp to track. The wallet can return error messages or failure if there was an error in sending it.
Use connected wallet to submit a transaction
const txHash = await wallet.submitTx(signedTx);
Check out Transaction to learn more on how to use this API.
Create Collateral UTXO
Collateral is used to guarantee that nodes are compensated for their work in case phase-2 validation fails. Thus, collateral is the monetary guarantee a user gives to assure that the contract has been carefully designed and thoroughly tested. The collateral amount is specified at the time of constructing the transaction. Not directly, but by adding collateral inputs to the transaction. The total balance in the UTXOs corresponding to these specially marked inputs is the transaction’s collateral amount. If the user fulfills the conditions of the guarantee, and a contract gets executed, the collateral is safe.
Create a new UTXO that can be used as collateral in a transaction.
const txhash = await wallet.createCollateral();
Get Assets
Returns a list of assets in wallet excluding lovelace, example:
[ { "unit": "1207329a668cf5c42b80a220a8c85d5e82ac0b6f5ecedda4c07a8acc4d657368486f6e6f72546f6b656e2d3530343935", "policyId": "1207329a668cf5c42b80a220a8c85d5e82ac0b6f5ecedda4c07a8acc", "assetName": "Mesh Token Of Appreciation", "fingerprint": "asset1dw74h0w0meqg9cxkc9sezp8zqcxu8nl93fzfpz", "quantity": "1" } { "unit": "9c8e9da7f81e3ca90485f32ebefc98137c8ac260a072a00c4aaf142d4d657368546f6b656e", "policyId": "9c8e9da7f81e3ca90485f32ebefc98137c8ac260a072a00c4aaf142d", "assetName": "MeshToken", "fingerprint": "asset177e7535dclmkkph8ewt9fsghllkwmpspa3n98p", "quantity": "10" } ]
Get assets in the connected wallet
const assets = await wallet.getAssets();
Get Lovelace
Return the lovelace balance in wallet. 1 ADA = 1000000 lovelace.
Get amount of ADA in connected wallet
const lovelace = await wallet.getLovelace();
Get Policy IDs
Return a list of assets' policy ID. An example response would be:
[ "0f5560dbc05282e05507aedb02d823d9d9f0e583cce579b81f9d1cd8", "5bed9e89299c69d9a54bbc82d88aa5a86698b2b7b9d0ed030fc4b0ff", "9c8e9da7f81e3ca90485f32ebefc98137c8ac260a072a00c4aaf142d", ]
Get a list of policy IDs from all assets in wallet
const policyIds = await wallet.getPolicyIds();
Get a Collection of Assets
Returns a list of assets from a policy ID. If no assets in wallet belongs to the policy ID, an empty list is returned. Query for a list of assets' policy ID with wallet.getPolicyIds()
.
Get a list of assets belonging to the policy ID
const assets = await wallet.getPolicyIdAssets('d9312da562da182b02322fd8acb536f37eb9d29fba7c49dc17255527');