Javascript

使用現有私鑰生成乙太坊錢包

  • October 6, 2021

我想了解乙太坊是如何工作的,我想從私鑰計算公鑰和錢包地址。

我已閱讀本教程:https ://piyopiyo.medium.com/generating-an-ethereum-wallet-with-an-existing-private-key-5cda690a8eb8

這是程式碼:

var Wallet = require('ethereumjs-wallet');
var EthUtil = require('ethereumjs-util');
const privateKeyString = '0x...MyPrivateKey...';
const privateKeyBuffer = EthUtil.toBuffer(privateKeyString);
const wallet = Wallet.fromPrivateKey(privateKeyBuffer);
const publicKey = wallet.getPublicKeyString();
console.log(publicKey);
const address = wallet.getAddressString();
console.log(address);

聽起來不錯,但我有這個執行時錯誤:

TypeError:Wallet.fromPrivateKey 不是函式

我認為這是因為 ethereumjs-wallet 在最新版本中發生了變化。但是我如何載入最新版本的私鑰?

謝謝

const wallet = Wallet['default'].fromPrivateKey(privateKeyBuffer);

在第 5 行替換它

var Wallet = require('ethereumjs-wallet');
var EthUtil = require('ethereumjs-util');
const privateKeyString = '0x...MyPrivateKey...';//your privateKey
const privateKeyBuffer = EthUtil.toBuffer(privateKeyString);
const wallet = Wallet['default'].fromPrivateKey(privateKeyBuffer);
const publicKey = wallet.getPublicKeyString();
console.log(publicKey);
const address = wallet.getAddressString();
console.log(address);

引用自:https://ethereum.stackexchange.com/questions/96732