Skip to content

Instantly share code, notes, and snippets.

@VictorNS69
Last active April 1, 2021 09:35
Show Gist options
  • Save VictorNS69/ef0f8a07b6b8e69fcca6673403cdc843 to your computer and use it in GitHub Desktop.
Save VictorNS69/ef0f8a07b6b8e69fcca6673403cdc843 to your computer and use it in GitHub Desktop.
How to get the public key from a keystore private key in JavaScript
/* Example of how to obtain the public key through
* a private key using a keystore
*/
/* Requires needed. You have to install the following
* packages
* npm install [email protected] [email protected]
*/
const Wallet = require('ethereumjs-wallet')
const keythereum = require('keythereum')
const fs = require('fs')
// Account's password.
const password = "Passw0rd" // Add the account password if needed
// Load keystores
let keyData = fs.readFileSync('./keystore.json')
let keystoreData = JSON.parse(keyData)
// Obtain the private key account1 from the keystore
let account1PrivK
try{
account1PrivK = keythereum.recover(Buffer.from(password), keystoreData)
}catch(error){
console.log("ERROR: ", error)
}
// Get the public key
let account1Puk = Wallet.default.fromPrivateKey(account1PrivK)
console.log("Public key:", account1Puk.getPublicKeyString(),"\n")
{
"address": "6e3976aeaa3a59e4af51783cc46ee0ffabc5dc11",
"crypto": {
"cipher": "aes-128-ctr",
"ciphertext": "463a0bc2146023ac4b85f4e3675c338facb0a09c4f83f5f067e2d36c87a0c35e",
"cipherparams": {
"iv": "d731f9793e33b3574303a863c7e68520"
},
"kdf": "scrypt",
"kdfparams": {
"dklen": 32,
"n": 262144,
"p": 1,
"r": 8,
"salt": "876f3ca79af1ec9b77f181cbefc45a2f392cb8eb99fe8b3a19c79d62e12ed173"
},
"mac": "230bf3451a7057ae6cf77399e6530a88d60a8f27f4089cf0c07319f1bf9844b3"
},
"id": "9277b6ec-6c04-4356-9e1c-dee015f459c5",
"version": 3
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment