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.
import { Dijets, BinTools, Buffer, BN } from "dijets"
let bintools = BinTools.getInstance()
let myNetworkID = 12345 //default id 1 is for Dijets Mainnet, we want to override that for local network id
let myBlockchainID = "2HWXDiFaT9JtRjgZM9nWyZdLDVWczpg7BrSQnqpue846vABZqB" // The Value Chain blockchainID on this network
let djt = new dijets.Dijets(
"localhost",
9650,
"http",
myNetworkID,
myBlockchainID
)
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.
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.
let newAddress1 = myKeychain.makeKey() //returns a Buffer for the address
You may also import your existing private key into the KeyChain using the Buffer:
let mypk = bintools.djtDeserialize( "24jUJ9vZexUM6expyMcT48LBx27k1m7xpraoV62oSQAHdziao5" ) //returns a Buffer let newAddress2 = myKeychain.importKey(mypk) //returns a Buffer for the address
… or you can import it using Dijets serialized string:
let mypk = "24jUJ9vZexUM6expyMcT48LBx27k1m7xpraoV62oSQAHdziao5" 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.
let addresses = myKeychain.getAddresses(); //returns an array of Buffers for the addresses let addressStrings = myKeychain.getAddressStrings(); //returns an array of strings for the addresses let exists = myKeychain.hasKey(newAddress1); //returns true if the address is managed 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.
let address = keypair.getAddress() //returns Buffer let addressString = keypair.getAddressString() //returns string let pubk = keypair.getPublicKey() //returns Buffer let pubkstr = keypair.getPublicKeyString() //returns a CB58 encoded string let privk = keypair.getPrivateKey() //returns Buffer let privkstr = keypair.getPrivateKeyString() //returns a CB58 encoded string keypair.generateKey() //creates a new random KeyPair let mypk = "24jUJ9vZexUM6expyMcT48LBx27k1m7xpraoV62oSQAHdziao5" let successul = keypair.importKey(mypk) //returns boolean if private key imported successfully let message = Buffer.from("azidapiyu") let signature = keypair.sign(message) //returns a Buffer with the signature let signerPubk = keypair.recover(message, signature) 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
- 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.
export const NetworkIDToHRP = {
1: "dijets",
5: "test",
1337: "custom",
12345: "local",
}
Change the HRP of the bech32 address by passing in a different networkID when instantiating Dijets
.
// mainnet
const networkID = 1
const dijets = new Dijets(undefined, undefined, undefined, networkID)
// [ 'X-dijets1j2j0vzttatv73gr7j4tnd7rp4el3ngcyjy0pre' ]
// [ 'X-dijets19rknw8l0grnfunjrzwxlxync6zrlu33y2jxhrg' ]
// testnet
const networkID = 5
const dijets = new Dijets(undefined, undefined, undefined, networkID)
// [ 'X-test1j2j0vzttatv73gr7j4tnd7rp4el3ngcy7kt70x' ]
// [ 'X-test19rknw8l0grnfunjrzwxlxync6zrlu33yxqzg0h' ]
// custom
const networkID = 1337 // also networkID = 0
const dijets = new Dijets(undefined, undefined, undefined, networkID)
// [ 'X-custom1j2j0vzttatv73gr7j4tnd7rp4el3ngcyp7amyv' ]
// [ 'X-custom19rknw8l0grnfunjrzwxlxync6zrlu33yeg5dya' ]
// local
const networkID = 12345
const dijets = new Dijets(undefined, undefined, undefined, networkID)
// [ 'X-local1j2j0vzttatv73gr7j4tnd7rp4el3ngcythj8q3' ]
// [ 'X-local19rknw8l0grnfunjrzwxlxync6zrlu33ynpm3qq' ]