Use PEM file wallet
Code snippet was written on sdk-core v10.2.5
This is a practical example of loading a PEM file wallet and making it ready to be used in your application to sign & send transactions. Remember that this is used mainly in CLI tools. In web applications, you can use Signing providers. We will also provide some content about that.
In order to sign & send a transaction, we'll need 3 components:
UserSigner - to sign the transaction
Account - this will be the account sending the transaction
ApiNetworkProvider - this is will basically tell the chain where the transaction will be sent (optional, but for a quick start recommended)
Also, we'll need 3 packages in order to implement this
Get the UserSigner
Now, we need a method that will read the PEM key from the PEM file
Now, we can combine those 2 functions to get our UserSigner
Get the ApiNetworkProvider
In order to create an ApiNetworkProvider, we need to know the chain will use. Let's assume will use DEVNET.
The URL for other chains are:
testnet: https://testnet-api.multiversx.com
devnet: https://devnet-api.multiversx.com
mainnet: https://api.multiversx.com
These are public endpoints, and using them in production-ready apps isn't recommended. You can always use your custom architecture or 3rd party services. Then you would need to switch the endpoint or, even better, create your API provider. You can read more about it here: Extending the Network Providers.
Get the Account
We can also use that walletPemKey
we got earlier to create an Account object
Now, we can put all the above together and create a function that we can reuse in our app every time we need to sign & send a transaction
Code snippets taken & adapted from buildo-begins.
Generate PEM file wallet
One simple way to generate a PEM file wallet is using mxpy. In a terminal, run:
Remember to always fund your newly created wallet, for example, using the MultiversX Web Wallet, where you can also log in using your PEM file.
Derive PEM file
If you already have a wallet, you can also derive your PEM from the seed phrase using both mxpy and buildo-begins lib (based on JS SDK) mentioned above:
with mxpy:
mxpy --verbose wallet derive ./walletKey.pem --mnemonic
with buildo-begins
buildo-begins derive-pem
In both cases, you will need to provide the seed phrase likewords here some mnemonic words more words et cetera ...
Last updated