Wallet Providers: HDWalletProvider
Let’s start with understanding wallets…
Wallets are applications that allow you to interact with your Ethereum account. Think of it like a bankless internet banking application. Your wallet allows you to read your balance, send transactions and connect to apps.
Your wallet is only a tool for managing your coin account. That means you can swap wallet providers at any time.
Wallets don’t have custody of your funds, you do have. They’re just a tool for managing what’s really yours.
Wallet provider
“An entity that provides a virtual currency wallet (i.e., a means (software application or another mechanism) for holding, storing and transferring coins or other assets). A wallet holds the user’s private keys, which allow the user to spend virtual currency allocated to the virtual currency address in the blockchain.
HDWalletProvider
HD Wallet-enabled Web3 provider. Use it to sign transactions for addresses derived from a 12 or 24 word mnemonic.
Another benefit of using HDWalletProvider is that it helps to send our private and public key information along with our code while developing smart contracts, and it allows us to make deployments on the code without the need for another application.
Spending money maliciously from someone’s wallet is prevented by signing the transactions. In this case, we have a key pair; public and private. The public key is used as our wallet’s address and is freely shareable, while the private key is used to sign transactions. This means we can only spend money in a wallet if we have the associated private key.
General Usage
By default, the HDWalletProvider will use the address of the first address that's generated from the mnemonic. If you pass in a specific index, it'll use that address instead.
Customize
You can customize hdwallet-provider
with options below.
Parameters:
This provider can be used wherever a Web3 provider is needed. It can be used not only with Truffle but also with other development environments. Here we have an example :
const HDWalletProvider = require("@truffle/hdwallet-provider");
const Web3 = require("web3");
const mnemonicPhrase = "mountains supernatural bird..."; // 12 word mnemonic
provider = new HDWalletProvider({
mnemonic: mnemonicPhrase,
providerOrUrl: "http://localhost:8545",
addressIndex: 5
});