Last active
April 1, 2021 09:35
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* 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") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"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