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.
_14import { Dijets, BinTools, Buffer, BN } from "dijets"_14_14let bintools = BinTools.getInstance()_14_14let myNetworkID = 12345 //default id 1 is for Dijets Mainnet, we want to override that for local network id_14let myBlockchainID = "2HWXDiFaT9JtRjgZM9nWyZdLDVWczpg7BrSQnqpue846vABZqB" // The Value Chain blockchainID on this network_14let djt = new dijets.Dijets(_14 "localhost",_14 9650,_14 "http",_14 myNetworkID,_14 myBlockchainID_14)_14let 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.
_10let 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.
_10let newAddress1 = myKeychain.makeKey() //returns a Buffer for the address
You may also import your existing private key into the KeyChain using the Buffer:
_10let mypk = bintools.djtDeserialize(_10 "24jUJ9vZexUM6expyMcT48LBx27k1m7xpraoV62oSQAHdziao5"_10) //returns a Buffer_10let newAddress2 = myKeychain.importKey(mypk) //returns a Buffer for the address
… or you can import it using Dijets serialized string:
_10let mypk = "24jUJ9vZexUM6expyMcT48LBx27k1m7xpraoV62oSQAHdziao5"_10let 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.
_10let addresses = myKeychain.getAddresses(); //returns an array of Buffers for the addresses_10let addressStrings = myKeychain.getAddressStrings(); //returns an array of strings for the addresses_10let exists = myKeychain.hasKey(newAddress1); //returns true if the address is managed_10let 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.
_19let address = keypair.getAddress() //returns Buffer_19let addressString = keypair.getAddressString() //returns string_19_19let pubk = keypair.getPublicKey() //returns Buffer_19let pubkstr = keypair.getPublicKeyString() //returns a CB58 encoded string_19_19let privk = keypair.getPrivateKey() //returns Buffer_19let privkstr = keypair.getPrivateKeyString() //returns a CB58 encoded string_19_19keypair.generateKey() //creates a new random KeyPair_19_19let mypk = "24jUJ9vZexUM6expyMcT48LBx27k1m7xpraoV62oSQAHdziao5"_19let successul = keypair.importKey(mypk) //returns boolean if private key imported successfully_19_19let message = Buffer.from("azidapiyu")_19let signature = keypair.sign(message) //returns a Buffer with the signature_19_19let signerPubk = keypair.recover(message, signature)_19let 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
- A Human-Readable Part (HRP) i.e. dijets/local/custom etc
- The number
1
is a separator (the last digit1
seen is considered the separator). - Base-32 encoded string for the data part of the address (the 20-byte address itself).
- 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:
- HRP:
dijets
- Separator:
1
- Address:
9rknw8l0grnfunjrzwxlxync6zrlu33y
- 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-
dijets
19rknw8l0grnfunjrzwxlxync6zrlu33y2jxhrg - 5 - X-
test
19rknw8l0grnfunjrzwxlxync6zrlu33ypmtvnh - 1337 - X-
custom
19rknw8l0grnfunjrzwxlxync6zrlu33yeg5dya - 12345 - X-
local
19rknw8l0grnfunjrzwxlxync6zrlu33ynpm3qq
Here's the mapping of networkID
to bech32 HRP.
_10export 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_10const networkID = 1_10const dijets = new Dijets(undefined, undefined, undefined, networkID)_10_10// [ 'X-dijets1j2j0vzttatv73gr7j4tnd7rp4el3ngcyjy0pre' ]_10// [ 'X-dijets19rknw8l0grnfunjrzwxlxync6zrlu33y2jxhrg' ]
_10// testnet_10const networkID = 5_10const dijets = new Dijets(undefined, undefined, undefined, networkID)_10_10// [ 'X-test1j2j0vzttatv73gr7j4tnd7rp4el3ngcy7kt70x' ]_10// [ 'X-test19rknw8l0grnfunjrzwxlxync6zrlu33yxqzg0h' ]
_10// custom_10const networkID = 1337 // also networkID = 0_10const dijets = new Dijets(undefined, undefined, undefined, networkID)_10_10// [ 'X-custom1j2j0vzttatv73gr7j4tnd7rp4el3ngcyp7amyv' ]_10// [ 'X-custom19rknw8l0grnfunjrzwxlxync6zrlu33yeg5dya' ]
_10// local_10const networkID = 12345_10const dijets = new Dijets(undefined, undefined, undefined, networkID)_10_10// [ 'X-local1j2j0vzttatv73gr7j4tnd7rp4el3ngcythj8q3' ]_10// [ 'X-local19rknw8l0grnfunjrzwxlxync6zrlu33ynpm3qq' ]