Summary
- A keypair is a matching pair of public key and secret key.
- The public key is used as an “address” that points to an account on the Nexis Native Chain network. A public key can be shared with anyone.
- The secret key is used to verify authority over the account. As the name suggests, you should always keep secret keys secret.
@nexis-network/web3.js
provides helper functions for creating a brand new keypair, or for constructing a keypair using an existing secret key.
Lesson
Symmetric and Asymmetric Cryptography
‘Cryptography’ the study of hiding information. There are two main types of cryptography you’ll encounter day to day: Symmetric Cryptography is where the same key is used to encrypt and decrypt. It’s hundreds of years old and has been used by everyone from the ancient Egyptians to Queen Elizabeth I. There’s a variety of symmetric cryptography algorithms, but the most common you’ll see today are AES and Chacha20. Asymmetric Cryptography- Asymmetric cryptography - also called ‘public key cryptography’ was developed in the 1970s. In asymmetric cryptography, participants have pairs of keys (or keypairs). Each keypair consists of a secret key and a public key. Asymmetric encryption works differently from symmetric encryption, and can do different things:
- Encryption: if it’s encrypted with a public key, only the secret key from the same keypair can be used to read it
- Signatures: if it’s encrypted with a secret key, the public key from the same keypair can be used to prove the secret key holder signed it.
- You can even use asymmetric cryptography to work out a good key for symmetric cryptography! This is called key exchange, where you use your public keys and the recipient’s public key to come up with a ‘session’ key.
- There’s a variety of asymmetric cryptography algorithms, but the most common you’ll see today are variants of ECC or RSA.
- Your bank card has a secret key inside it that’s used to sign transactions. Your bank can confirm you made the transaction by checking them with the matching public key.
- Websites include a public key in their certificate. Your browser will use this public key to encrypt the data (like personal information, login details, and credit card numbers) it sends to the web page. The website has the matching private key so that the website can read the data.
- Your electronic passport was signed by the country that issued it to ensure the passport isn’t forged. The electronic passport gates can confirm this using the public key of your issuing country.
- The messaging apps on your phone use key exchange to make a session key.
Nexis Native Chain uses public keys as addresses
-
The public key is used as an “address” that points to an account on the
Nexis Native Chain network. Even friendly names - like
example.sol
- point to addresses likedDCQNnDmNbFVi8cQhKAgXhyhXeJ625tvwsunRyRc7c8
- The secret key is used to verify authority over that keypair. If you have the secret key for an address, you control the tokens inside that address. For this reason, as the name suggests, you should always keep secret keys secret.
Using @nexis-network/web3.js to make a keypair
You can use the Nexis Native Chain blockchain from either the browser or node.js with the@nexis-network/web3.js
npm module. Set up a project how you normally would, then
use npm
to install @nexis-network/web3.js
Keypair.generate()
function from
@nexis-network/web3.js
:
Since the keypair can be regenerated from the secret key, we usually only store
the secret key, and restore the keypair from the secret key.Additionally, since the secret key gives authority over the address, we don’t
store secret keys in source code. Instead, we:
- Put secret keys in a
.env
file - Add
.env
to.gitignore
so the.env
file is not committed.
Loading an existing keypair
If you already have a keypair you’d like to use, you can load aKeypair
from
an existing secret key stored in the filesystem or an .env
file. In node.js,
the @nexis-network-developers/helpers
npm package includes some extra functions:
- To use an
.env
file usegetKeypairFromEnvironment()
- To use a Nexis Native Chain CLI file use
getKeypairFromFile()
Lab
Installation
Make a new directory, install TypeScript, Nexis Native Chain web3.js and esrun:generate-keypair.ts
npx esrun generate-keypair.ts
. You should see the text:
Keypair
has a publicKey
and secretKey
property. Update the file:
npx esrun generate-keypair.ts
. You should see the text:
Loading an existing keypair from an .env file
To ensure that your secret key stays secure, we recommend injecting the secret key using a.env
file:
Make a new file called .env
with the contents of the key you made earlier:
generate-keypair.ts
:
npx esrun generate-keypair.ts
. You should see the following result: