Agregar billetera usando web3.eth.accounts.wallet.add

En web3 0.20.1, estoy usando Infura para implementar un contrato en Rinkeby. Esto probablemente requiere que agregue una billetera web3.accountspara proporcionar el gas necesario.

Cuando ejecuté el siguiente código de nodejs,

Web3 = require('web3')
var web3 = new Web3(new Web3.providers.HttpProvider("https://rinkeby.infura.io/nyxynyx-api-key"))
var privateKey = 'nyxynyx-private-key'
web3.eth.accounts.wallet.add("0x" + privateKey);

var contractCode = '60606-contract-code-here';
var abi = [{"constant":false,"inputs":[{"name":"givenNumber","type":"uint8"}],"name":"setNumber","outputs":[],"type":"function"},{"constant":true,"inputs":[{"name":"givenNumber","type":"uint8"}],"name":"guessNumber","outputs":[{"name":"","type":"bool"}],"type":"function"},{"anonymous":false,"inputs":[{"indexed":false,"name":"from","type":"address"},{"indexed":false,"name":"value","type":"uint256"}],"name":"Deposit","type":"event"},{"anonymous":false,"inputs":[],"name":"SetNumber","type":"event"}];
GuessNumber = web3.eth.contract(abi)
var contractInstance = GuessNumber.new({from: web3.eth.accounts[0], gas: 200000, data: '0x' + contractCode});

se encuentra el siguiente error:

web3.eth.accounts.wallet.add("0x" + privateKey);
                        ^

TypeError: Cannot read property 'add' of undefined

¿Es esto porque web3.eth.accounts.wallet.addes una función web3 v1.0? Si es así, ¿cuál es el código equivalente necesario en v0.20.1?

Respuestas (4)

Primero cree un objeto de billetera. el primer parámetro es el número de cuentas que desea y el segundo parámetro es una matriz aleatoria de 32 bytes como una entropía

var wallet = w3.eth.accounts.wallet.create(0, utils.entropy())

luego agregue una nueva cuenta a su billetera creada con

var newAccount = w3.eth.accounts.privateKeyToAccount(textPrivateKey.text)
wallet.add(newAccount)

Web3 v1.0 es muy superior en mi experiencia, por lo que recomiendo cambiar cuando sea conveniente.

Otra solución a tu problema.

También podemos usar este método para agregar billetera en nuestro eth.wallets

Puntos a tener en cuenta: -

  1. Estamos tomando los valores de la interfaz y el código de bytes de otro archivo que tiene compilado nuestro código de contrato.
  2. hdwalletprovider ingrese su dirección mnemotécnica de 12 palabras y rinkeby

`const hdwalletprovider=require('truffle-hdwallet-provider'); const Web3=requerir('web3'); const {interfaz,código de bytes}=requerir('./compilar');

const provider=new hdwalletprovider(
    'hello blue cat eng cook name version brave entire sail trumpet scrub', // 12 word mnemonic
    'https://rinkeby.infura.io/v3/5d5eaf76b6a84578a245e058b870a8aa' );

const web3=new Web3(provider);

const deploy=async() => {
    const accounts= await web3.eth.getAccounts();
    console.log(accounts[0]);

   const result=await new web3.eth.Contract(JSON.parse(interface))
   .deploy({data : bytecode})
   .send({gas: '10000000', from: accounts[0] });

   console.log(result.options.address);
};

deploy();`

Si está usando v1.0, intente usar web3.eth.accounts.create(\*Passcode here*\);para crear nuevas billeteras