Quiero entender cómo funciona Ethereum y quiero calcular la clave pública y la dirección de la billetera a partir de la clave privada.
He leído este tutorial: https://piyopiyo.medium.com/generating-an-ethereum-wallet-with-an-existing-private-key-5cda690a8eb8
Aquí está el código:
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);
Suena genial, pero tengo este error de tiempo de ejecución:
TypeError: Wallet.fromPrivateKey no es una función
Creo que esto se debe a que ethereumjs-wallet ha cambiado en las últimas versiones. Pero, ¿cómo puedo cargar una clave privada con la última versión?
Gracias
const wallet = Wallet['default'].fromPrivateKey(privateKeyBuffer);
reemplaza esto en linea5
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);
ethereumjs-wallet
no tiene una exportación por defecto. Puedes importar Wallet
así en Node.js:
const { Wallet } = require('ethereumjs-wallet')
Puede encontrar los documentos aquí: https://github.com/ethereumjs/ethereumjs-wallet#wallet-api
Wallet
elemento exportado. si lo miras, se llama "predeterminado"
ekkis
const wallet = Wallet.fromPrivateKey(privateKeyBuffer);
genera "TypeError: Wallet.fromPrivateKey no es una función"