Home

Manage Value Chain Keys

DijetsJS comes with its own Value Chain VM Keychain. This KeyChain is used in the functions of the API, enabling them to sign using keys it's registered. The first step in this process is to create an instance of DijetsJS connected to our Dijets platform endpoint of choice.


_14
import { Dijets, BinTools, Buffer, BN } from "dijets"
_14
_14
let bintools = BinTools.getInstance()
_14
_14
let myNetworkID = 12345 //default id 1 is for Dijets Mainnet, we want to override that for local network id
_14
let myBlockchainID = "2HWXDiFaT9JtRjgZM9nWyZdLDVWczpg7BrSQnqpue846vABZqB" // The Value Chain blockchainID on this network
_14
let djt = new dijets.Dijets(
_14
"localhost",
_14
9650,
_14
"http",
_14
myNetworkID,
_14
myBlockchainID
_14
)
_14
let xchain = djt.XChain() //returns a reference to the Value Chain used by DijetsJS

Accessing the Keychain#

The KeyChain is accessed through the Value Chain and can be referenced directly or through a reference variable.


_10
let myKeychain = xchain.keyChain()

This exposes the instance of the class VMKeyChain which is created when the Value Chain API is created. At present, this supports secp256k1 curve for ECDSA key pairs.

Creating Value Chain Key Pairs#

The KeyChain has the ability to create new Keypairs for you and return the address associated with the key pair.


_10
let newAddress1 = myKeychain.makeKey() //returns a Buffer for the address

You may also import your existing private key into the KeyChain using the Buffer:


_10
let mypk = bintools.djtDeserialize(
_10
"24jUJ9vZexUM6expyMcT48LBx27k1m7xpraoV62oSQAHdziao5"
_10
) //returns a Buffer
_10
let newAddress2 = myKeychain.importKey(mypk) //returns a Buffer for the address

… or you can import it using Dijets serialized string:


_10
let mypk = "24jUJ9vZexUM6expyMcT48LBx27k1m7xpraoV62oSQAHdziao5"
_10
let newAddress2 = myKeychain.importKey(mypk) //returns a Buffer for the address

Working with Keychains#

The Value Chain's KeyChain has standardised key management capabilities. The following functions are available on any KeyChain that implements this interface.


_10
let addresses = myKeychain.getAddresses(); //returns an array of Buffers for the addresses
_10
let addressStrings = myKeychain.getAddressStrings(); //returns an array of strings for the addresses
_10
let exists = myKeychain.hasKey(newAddress1); //returns true if the address is managed
_10
let keypair = myKeychain.getKey(newAddress1); //returns the KeyPair class

Working with Keypairs#

The Value Chain's keypair has standardised keypair functionality. The following operations are available on any keypair that implements this interface.


_19
let address = keypair.getAddress() //returns Buffer
_19
let addressString = keypair.getAddressString() //returns string
_19
_19
let pubk = keypair.getPublicKey() //returns Buffer
_19
let pubkstr = keypair.getPublicKeyString() //returns a CB58 encoded string
_19
_19
let privk = keypair.getPrivateKey() //returns Buffer
_19
let privkstr = keypair.getPrivateKeyString() //returns a CB58 encoded string
_19
_19
keypair.generateKey() //creates a new random KeyPair
_19
_19
let mypk = "24jUJ9vZexUM6expyMcT48LBx27k1m7xpraoV62oSQAHdziao5"
_19
let successul = keypair.importKey(mypk) //returns boolean if private key imported successfully
_19
_19
let message = Buffer.from("azidapiyu")
_19
let signature = keypair.sign(message) //returns a Buffer with the signature
_19
_19
let signerPubk = keypair.recover(message, signature)
_19
let isValid = keypair.verify(message, signature) //returns a boolean

Encode Bech32 Addresses#

Both the Value Chain and the Method Chain use Bech32 to encode addresses. Dijets Utility Chain also uses Bech32 to encode addresses for importing and exporting assets however in general, the ethereum virtual machine instance always uses hex encoding for addresses. Ex: 0x46f3e64E4e3f5a46Eaf5c292301c6174B9B646Bf.

Each Bech32 address is composed of the following components

  1. A Human-Readable Part (HRP) i.e. dijets/local/custom etc
  2. The number 1 is a separator (the last digit 1 seen is considered the separator).
  3. Base-32 encoded string for the data part of the address (the 20-byte address itself).
  4. A 6-character base-32 encoded error correction code using the BCH algorithm.

For example the following Bech32 address, X-dijets19rknw8l0grnfunjrzwxlxync6zrlu33y2jxhrg, is composed like so:

  1. HRP: dijets
  2. Separator: 1
  3. Address: 9rknw8l0grnfunjrzwxlxync6zrlu33y
  4. Checksum: 2jxhrg

Depending on the networkID which is passed in when instantiating Dijets the encoded addresses will have a distinctive HRP per each network.

  • 1 - X-dijets19rknw8l0grnfunjrzwxlxync6zrlu33y2jxhrg
  • 5 - X-test19rknw8l0grnfunjrzwxlxync6zrlu33ypmtvnh
  • 1337 - X-custom19rknw8l0grnfunjrzwxlxync6zrlu33yeg5dya
  • 12345 - X-local19rknw8l0grnfunjrzwxlxync6zrlu33ynpm3qq

Here's the mapping of networkID to bech32 HRP.


_10
export const NetworkIDToHRP = {
_10
1: "dijets",
_10
5: "test",
_10
1337: "custom",
_10
12345: "local",
_10
}

Change the HRP of the bech32 address by passing in a different networkID when instantiating Dijets.


_10
// mainnet
_10
const networkID = 1
_10
const dijets = new Dijets(undefined, undefined, undefined, networkID)
_10
_10
// [ 'X-dijets1j2j0vzttatv73gr7j4tnd7rp4el3ngcyjy0pre' ]
_10
// [ 'X-dijets19rknw8l0grnfunjrzwxlxync6zrlu33y2jxhrg' ]


_10
// testnet
_10
const networkID = 5
_10
const dijets = new Dijets(undefined, undefined, undefined, networkID)
_10
_10
// [ 'X-test1j2j0vzttatv73gr7j4tnd7rp4el3ngcy7kt70x' ]
_10
// [ 'X-test19rknw8l0grnfunjrzwxlxync6zrlu33yxqzg0h' ]


_10
// custom
_10
const networkID = 1337 // also networkID = 0
_10
const dijets = new Dijets(undefined, undefined, undefined, networkID)
_10
_10
// [ 'X-custom1j2j0vzttatv73gr7j4tnd7rp4el3ngcyp7amyv' ]
_10
// [ 'X-custom19rknw8l0grnfunjrzwxlxync6zrlu33yeg5dya' ]


_10
// local
_10
const networkID = 12345
_10
const dijets = new Dijets(undefined, undefined, undefined, networkID)
_10
_10
// [ 'X-local1j2j0vzttatv73gr7j4tnd7rp4el3ngcythj8q3' ]
_10
// [ 'X-local19rknw8l0grnfunjrzwxlxync6zrlu33ynpm3qq' ]