How to use Ledger with near cli
Using Legder lets the user work with Near cli without Login. It’s a very safe and simple way the operate. The only con is that the near cli requires your signature and makes the operations a bit slower.
To operate with Ledger we have to use the following command:
near --useLedgerKey
With this command we tell near to Use Ledger for signing with given HD key path [string] [default: “44'/397'/0'/0'/1'”].
The first thing we have to learn is how to create an account from scratch and then link it to Ledger to be able to sign with it.
To start head to https://wallet.betanet.near.org/create to choose your username and click on create account:
near create-account <accountId> --useLedgerKey
In the next step you will be asked how you want to protect your account. Use Ledger option:
It will ask to connect your Ledger device with Near application on.
You can install the Near app on your Ledger following the link Near provides :)
After connecting Near Ledger app and clicking continue you will be asked to sign the transaction to create the acccount with your Ledger:
After signing your account with Ledger will be created! Congrats!
Signing transactions using your Ledger
To make it very easy we will do an example where we send Near tokens from address bonsfi.betanet to address agut.betanet:
near send <sender> <receiver> <amount> --useLedgerKey
You can use the following to see how the state of the account varied:
near state <accountName>
Once the transaction is started near cli will require your comfirmation in Ledger (every part of the transaction is carefully detailed):
You can see my example below:
And the transaction is:
Another very important point is to know how to login to your account with your Ledger. Please follow this steps and you will be set to sign transaction without requiring Ledger signature every time :)
The first thing you have to do is login to your account.
The process is the same as you do without Ledger but the last step asks for confirmation in Ledger. Consequently, before login you must have your Ledger connected and Near Ledger app running.
Then access Near in Ledger:
Open a shell window and set your network (I am using Betanet):
export NODE_ENV=betanet
And then start the login:
near login
Then a tab will open in your favourite browser:
You just have to allow the operation and you will be prompted with this next tab:
After confirming the Near will ask to sign the operation with your Ledger. At this point, if your Ledger is plugged you will have to sign from your Ledger device:
Then follow the steps in your Ledger and finally click approve:
After this last siganture you will be prompted with:
Now that you are logged in we will send tokens from my account (bonsfi.betanet) to another account (agut.betanet)
Then we use the command to send Near:
near send <sender> <receiver> <amount>
You can use the following to see how the state of the account varied:
near state <accountName>
You can see my example below:
And you can also see the transaction in the following link: