LoginSignup
4
0

More than 3 years have passed since last update.

ニーモニックから秘密鍵を取得してみた

Posted at

みなさん、MetaMaskはお使いでしょうか?
すごい便利なのですがニーモニックでのインポートが出来ないんですよね。
とある Ethereum のアカウントを移行しようと思ったときに少し困ったのでメモです。

実装

早速ですが以下のライブラリを使用しました。
https://github.com/justinchou/ethereum-mnemonic-privatekey-utils

まずはクローンしてきて、必要なライブラリをインストールしていきます。

$ git clone https://github.com/justinchou/ethereum-mnemonic-privatekey-utils.git
$ cd /ethereum-mnemonic-privatekey-utils
$ npm install

続いてコードを書いていきます。
と言いたいところですが、すでにサンプルがあるのでそれを利用しましょう。
サンプルファイルの名前は demo.js になります。

demo.js
const pkutils = require('./index');

pkutils.debug = false;

const mnemonic = 'this is your mnemonic words do not show this words other people';
const password = 'this is a totally long password';

console.log('mnemonic               : %s', mnemonic);
console.log('password               : %s', password);

const privateKeyGen = pkutils.getPrivateKeyFromMnemonic(mnemonic);
console.log('pkey from mnemonic     : 0x%s', privateKeyGen);

const keystore = pkutils.getKeystoreFromPrivateKey(privateKeyGen, password);
console.log('\nkey store              : %j\n', keystore);

const privateKeyParsed = pkutils.getPrivateKeyFromKeystore(keystore, password);
console.log('pkey from keystore     : 0x%s', privateKeyParsed);

const account = keystore.address;
console.log('account address        : 0x%s', account);

mnemonicにはご自分のニーモニックを入力してください。
試しにこのスクリプトを実行すると以下のような出力になると思います。

$ node demo.js
mnemonic               : this is your mnemonic words do not show this words other people
password               : this is a totally long password
pkey from mnemonic     : 0x1a50c342ee19eeed0d29dad2f9d37ff06690b7e6fd764e23de94296ec02074fc

key store              : {"address":"d0c53d7d802fbcf5a8162b0ebf1e6aca66202e33","crypto":{"cipher":"aes-128-ctr","ciphertext":"ece9cb20184c907916e61c66b8070569bec61f0c92ed9f33eda9303066906b52","cipherparams":{"iv":"fe93ca7db03671a77849fb5926727bed"},"mac":"be432d8e2b21ce619b97dee0073281c8ffd1e451e661b4e3f61fbad1f3316a3d","kdf":"pbkdf2","kdfparams":{"c":262144,"dklen":32,"prf":"hmac-sha256","salt":"65b7e9eabeb6db63ed4193b4e863d2ce1e826f66ac8394c198f570a5c26be9af"}},"id":"38812fce-3ff8-4a84-b135-fe2b120785ef","version":3}

pkey from keystore     : 0x1a50c342ee19eeed0d29dad2f9d37ff06690b7e6fd764e23de94296ec02074fc
account address        : 0xd0c53d7d802fbcf5a8162b0ebf1e6aca66202e33

この中の pkey from mnemonic が秘密鍵となりますので、後はMetaMaskに突っ込むだけですね!

4
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
0