App Wallet
Wallet for building transactions in your applications.
AppWallet
is useful for building other user wallets and fully customed applications's backend.
Generate Wallet
You can generate deterministic keys based on the Bitcoin BIP39. These mnemonic phrases allow you to recover your wallet.
Once you have your mnemonic phrase, you can use it to generate your deterministic keys. See Load AppWallet
in the following section on loading a mnemonic phrase. It will typically generate a series of private keys and corresponding public keys, which you can use to manage your cryptocurrencies.
import { AppWallet } from '@meshsdk/core'; const mnemonic = AppWallet.brew();
Generate new mnemonic phrases for your wallet
Load AppWallet
With Mesh, you can initialize a 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 { AppWallet } from '@meshsdk/core'; const wallet = new AppWallet({ 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.getPaymentAddress();
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 { AppWallet } from '@meshsdk/core'; const wallet = new AppWallet({ networkId: 0, fetcher: blockchainProvider, submitter: blockchainProvider, key: { type: 'cli', payment: '5820aaca553a7b95b38b5d9b82a5daa7a27ac8e34f3cf27152a978f4576520dd6503', stake: '582097c458f19a3111c3b965220b1bef7d548fd75bc140a7f0a4f080e03cce604f0e', }, });
Private keys
We can load wallet with private keys:
import { AppWallet } from '@meshsdk/core'; const wallet = new AppWallet({ networkId: 0, fetcher: blockchainProvider, submitter: blockchainProvider, key: { type: 'root', bech32: 'xprv1cqa46gk29plgkg98upclnjv5t425fcpl4rgf9mq2txdxuga7jfq5shk7np6l55nj00sl3m4syzna3uwgrwppdm0azgy9d8zahyf32s62klfyhe0ayyxkc7x92nv4s77fa0v25tufk9tnv7x6dgexe9kdz5gpeqgu', }, });
Get Payment Address
Get wallet's address. For multi-addresses wallet, it will return the first address. To choose other address, `accountIndex` can be specified.
const address = wallet.getPaymentAddress();
Load a wallet to try this endpoint.
Get Reward Address
Get wallet's reward address. For multi-addresses wallet, it will return the first address. To choose other address, `accountIndex` can be specified.
const address = wallet.getRewardAddress();
Load a wallet to try this endpoint.
Create & sign transactions
We can create transactions and sign it with the wallet. For this demo, we will mint an asset and send it to an address. Go to Transaction to learn more about building transactions.
import { Transaction, ForgeScript } from '@meshsdk/core'; import type { Mint, AssetMetadata } from '@meshsdk/core'; const walletAddress = wallet.getPaymentAddress(); const forgingScript = ForgeScript.withOneSignature(walletAddress); const assetMetadata1: AssetMetadata = { name: 'Mesh Token', image: 'ipfs://QmRzicpReutwCkM6aotuKjErFCUD213DpwPq6ByuzMJaua', mediaType: 'image/jpg', description: 'This NFT was minted by Mesh (https://meshjs.dev/).', }; const asset1: Mint = { assetName: 'MeshToken', assetQuantity: '1', metadata: assetMetadata1, label: '721', recipient: 'addr_test1vpvx0sacufuypa2k4sngk7q40zc5c4npl337uusdh64kv0c7e4cxr' }; const tx = new Transaction({ initiator: wallet }); tx.mintAsset(forgingScript, asset1); const unsignedTx = await tx.build(); const signedTx = await wallet.signTx(unsignedTx); const txHash = await wallet.submitTx(signedTx);
Load a wallet to try this endpoint.
Sign Data
Sign data allows you to sign a payload to identify the wallet ownership.
const address = wallet.getPaymentAddress(); const signature = wallet.signData(address, 'mesh');
Example of a response from the endpoint:
{ "signature": "845846a2012...f9119a18e8977d436385cecb08", "key": "a4010103272006215...b81a7f6ed4fa29cc7b33186c" }
Load a wallet to try this endpoint.