How to authenticate and send contract method using web3.js 1.0
Asked Answered
A

4

10

I am confused about how I should be executing a contract's method using the web3 1.0 library.

This code works (so long as I manually unlock the account first):

var contract = new web3.eth.Contract(contractJson, contractAddress);
contract.methods
  .transfer("0x0e0479bC23a96F6d701D003c5F004Bb0f28e773C", 1000)
  .send({
    from: "0x2EBd0A4729129b45b23aAd4656b98026cf67650A"
  })
  .on('confirmation', (confirmationNumber, receipt) => {
    io.emit('confirmation', confirmationNumber);
  });

I get this error (if I don't unlock manually first):

Returned error: authentication needed: password or unlock

The above code is an API endpoint in node.js, so I want it to unlock or authenticate programmatically.

There is no method in web3.js 1.0 to unlock the account.

I also don't think this is necessary (at least that's what I am confused about). Since I am managing accounts, I know what the private key is.

I am thinking the transaction needs to be signed with the private key?? Is this correct? Is this effectively the same thing as "unlocking the account"?

I tried doing this:

var contract = new web3.eth.Contract(contractJson, contractAddress);

var tx = {
  from: "...{fromAddress -- address that has the private key below}",
  to: "...",
  value: ...
};

var signed = web3.eth.accounts.signTransaction(tx, 
  "...{privateKey}");

console.log(signed);

var promise = web3.eth.sendSignedTransaction(signed);

I get this error:

Returned error: The method net_version does not exist/is not available

What is the easiest way to authenticate and submit a transaction?

Ideally, I want to use the first approach in my code sample, as it is the cleanest.

Aquinas answered 6/10, 2017 at 17:34 Comment(3)
I'd use Parity, it creates a default account (with no password) running with ETH in it for development, here's an example I put together: github.com/leopoldjoy/react-ethereum-dapp-exampleSadesadella
That way you won't have to unlock the account at all. Here's an example of using a method: github.com/leopoldjoy/react-ethereum-dapp-example/blob/master/…Sadesadella
Thanks for your example. I am writing production code though, and need to operate on accounts programmatically created through the portal I am building. I managed to figure it out... hopefully that is the correct way of doing things.Aquinas
A
25

This code allows me to sign a transaction server-side (node.js) using the privateKey from the account I created (using web3.eth.accounts.create()), and send the signed transaction to the network without having to unlock the account.

I am using Geth 1.7.1

  var contract = new web3.eth.Contract(contractJson, contractAddress);
  var transfer = contract.methods.transfer("0x...", 490);
  var encodedABI = transfer.encodeABI();

  var tx = {
    from: "0x...",
    to: contractAddress,
    gas: 2000000,
    data: encodedABI
  }; 

  web3.eth.accounts.signTransaction(tx, privateKey).then(signed => {
    var tran = web3.eth.sendSignedTransaction(signed.rawTransaction);

    tran.on('confirmation', (confirmationNumber, receipt) => {
      console.log('confirmation: ' + confirmationNumber);
    });

    tran.on('transactionHash', hash => {
      console.log('hash');
      console.log(hash);
    });

    tran.on('receipt', receipt => {
      console.log('reciept');
      console.log(receipt);
    });

    tran.on('error', console.error);
  });
Aquinas answered 9/10, 2017 at 20:4 Comment(1)
Thank you for showing how to register for events, but how does one unregister from events?Doble
H
15

A way to be able to call your contract methods without having to sign the transaction explicitly is this (web3js 1.0.0):

const privateKey = 'e0f3440344e4814d0dea8a65c1b9c488bab4295571c72fb879f5c29c8c861937';
const account = web3.eth.accounts.privateKeyToAccount('0x' + privateKey);
web3.eth.accounts.wallet.add(account);
web3.eth.defaultAccount = account.address;

// ...
contract = new web3.eth.Contract(JSON_INTERFACE, address);
contract.methods.myMethod(myParam1, myParam2)
        .send({
            from: this.web3.eth.defaultAccount,
            gas: myConfig.gas,
            gasPrice: myConfig.gasPrice
        })
Heel answered 9/6, 2018 at 18:14 Comment(0)
R
4

Here's a complete example of how to sign a transaction without a local wallet account. Especially useful if you are using infura for the transaction. This was written for

'use strict';
const Web3 = require('web3');

const wsAddress = 'wss://rinkeby.infura.io/ws';
const contractJson = '(taken from solc or remix online compiler)';
const privateKey = '0xOOOX';
const contractAddress = '0xOOOX';
const walletAddress = '0xOOOX';

const webSocketProvider = new Web3.providers.WebsocketProvider(wsAddress);
const web3 = new Web3(new Web3.providers.WebsocketProvider(webSocketProvider));
const contract = new web3.eth.Contract(
  JSON.parse(contractJson),
  contractAddress
);
// change this to whatever contract method you are trying to call, E.G. SimpleStore("Hello World")
const query = contract.methods.SimpleStore('Hello World');
const encodedABI = query.encodeABI();
const tx = {
  from: walletAddress,
  to: contractAddress,
  gas: 2000000,
  data: encodedABI,
};

const account = web3.eth.accounts.privateKeyToAccount(privateKey);
console.log(account);
web3.eth.getBalance(walletAddress).then(console.log);

web3.eth.accounts.signTransaction(tx, privateKey).then(signed => {
  const tran = web3.eth
    .sendSignedTransaction(signed.rawTransaction)
    .on('confirmation', (confirmationNumber, receipt) => {
      console.log('=> confirmation: ' + confirmationNumber);
    })
    .on('transactionHash', hash => {
      console.log('=> hash');
      console.log(hash);
    })
    .on('receipt', receipt => {
      console.log('=> reciept');
      console.log(receipt);
    })
    .on('error', console.error);
});

Using

"web3": "1.0.0-beta.30"

Rowlett answered 11/3, 2018 at 1:42 Comment(0)
D
0

This is my implementation using "@truffle/hdwallet-provider": "^2.0.3", "web3": "^1.6.1",


function getWeb3Provider() {
  return new HDWalletProvider({
    privateKeys: [NFT_MINTER_ACCOUNT_PRIVATE_KEY],
    providerOrUrl: BSC_RPC_ENDPOINT,
  });
}

const web3 = new Web3(BSC_RPC_ENDPOINT);
const contract = new web3.eth.Contract(
      jsonContractABI as unknown as AbiItem[],
      NFT_CONTRACT_ADDRESS
    );
contract.setProvider(getWeb3Provider());

then in send methods

contract.methods.safeMint(receiverAddress, itemUri).send({
      from: NFT_MINTER_ACCOUNT,
    });

in call methods

contract.methods.balanceOf(address).call();
Darrondarrow answered 5/2, 2022 at 15:54 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.