Keys
Private keys protect Bitcoins, enable secure message signing, among other vital roles in the BSV ecosystem. Public keys enable ownership tracking, message attestation, and attribution for signed messages, among other use-cases. In this low-level tutorial, we will learn how to generate, serialize and deserialize public and private keys using the functions provided by the SDK.
Getting Set Up
We'll be making use of two SDK modules in this guide. Make sure you've installed the @bsv/sdk
package in your project, then import the modules we'll be using:
PrivateKey - this class will enable to you create a new PrivateKey from various sources such as random, hex string, and WIP. It can also transform a private key into a public key.
Utils - the SDK provides several helpful utility functions such as
toArray
and andtoUTF8
which allow you to serialize and deserialize data as needed.
Generating and Deserializing Keys
First, we will learn how to generate new cryptographic keys to be used within your applications. Unless generating a new private key from random, this will most often involve deserializing a key from various types the most common being such as hex, WIF, and binary.
Here is an example of how this can be done:
We can then transform these to find the corresponding public key as follows:
Serializing Keys
Sometimes you will want to convert private / public keys into a format that can be more easily transported in your applications such as hex or binary.
Let's explore the available functions the SDK provides.
Private Keys
Serialize a private key into hex and binary:
Public Keys
Public keys can be serialized as well, and include helper functions to serialize into common formats such as an address or DER.
Now you should be able to manage cryptographic keys with the SDK, including generating, serializing, and deserializing both private and public keys. These skills are important for using low-level cryptographic keys, ensuring you have the necessary tools to leverage the security benefits they offer and to implement advanced cryptographic functions within your applications.
Last updated