playground

The world is a playground.

Ethereum の秘密鍵から公開鍵を取得 (JavaScript)

今回使用するライブラリ

ライブラリのインストール

$ npm install ethereumjs-wallet --save
$ npm install ethereumjs-util --save

公開鍵から秘密鍵を取得するコード

var Wallet = require('ethereumjs-wallet');
var EthUtil = require('ethereumjs-util');

// 秘密鍵を使用して、wallet インスタンスを取得
const privateKeyBuffer = EthUtil.toBuffer('0x61ce8b95ca5fd6f55cd97ac60817777bdf64f1670e903758ce53efc32c3dffeb');
const wallet = Wallet.fromPrivateKey(privateKeyBuffer);

// ウォレットの公開鍵を取得
const publicKey = wallet.getPublicKeyString();                                                                                                                                                                                                                                                               
console.log(publicKey);

Note