Access to Smart contract functions of frontend without private key in react.js
Navigated by the Ethereum-based project using smart contracts written in JavaScript is one of the most common challenges when attempts are made to access certain functions from these contracts. In particular, if the intelligent contract methods of the contract do not require a private key to implementation, it is important that we find alternative methods to access these functions of frontend without the safety of frontend.
In this article we are immersed in the characteristics of his implementation with React.Js and understand why the current attitude can be thrown out errors such as the “unknown account” when drawing contract methods.
Understanding of strength contracts
Before we deal with the solution, we quickly check some basic concepts for intelligent contracts from Ethereum:
* Private keys: These are necessary to sign transactions, but they do not have to call certain functions (e.g. “Contractmethods”).
* Installation of contracts:
The contractual procedure includes the creation of an invoice in the Ethereum network and the signing of an installation message with the private key. This is necessary before the smart contract code is implemented.
* Virtual Machine (VM):
Each Ethereum Junction acts as a separate virtual machine and enables the self -implementation of intelligent contracts independently.
Why does the error come
The error “unknown account” usually occurs when you try to call up a function with the Ethereum contract without being properly signed by a private key. Here are some scenarios that can lead to this question:
* Not sufficient signature: If you try to draw “contractmethods”, make sure that you sign the transaction with the help of public and private keys.
* False contract address: Check twice whether the contract address to which you want to access is correct because it can affect the contract behavior.
Solution: Access intelligent contract functions without private keys
Overcoming these restrictions in the development of React.js applications with Solidity Smart:
1. Use a web3 directory
A popular solution for this is the use of a web3 directory such as web3.js or ethers.js to interact with the Ethereum network.
Example with web3.js:
`JavaScript
Import Web3 from 'Web3';
Const Web3 = New Web3 ();
// set the Ethereum network and the account
Const Ethnetwork = 'Mainnet';
Const yor account = '0x ... your account ---';
// establish a connection to the Ethereum network
Web3.eth.net.enableweb3Provider ();
Async function callcontractFunction () {
Attempt {
// Call the contract method without a private key
Const answer = waiting for web3.eth.call ({{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{
Sender: YourcCount,
Data:0x $ {contrantaddress}
Gas: '200000',
Arguments: [Functionnone],
});
console.log (answer);
} Catch (error) {
Console.error (error);
}
}
2. Complete the webhooks contract
Another approach is to use contract web books with which you can send events or notifications directly to the Ethereum network directly to the frontend.
`JavaScript
Import {contract, contractvent} from "ether";
Const contractdress = '0x ... your contract address ...');
Const Functionnone = 'Your function ...';
Asynchronized functional manual -Contractvent () {
Const event = waiting for contractdress. events [functolname];
Switch (event.Name) {
"Function called" case:
console.log ('called the specified function!');
Pause;
Standard:
Console.error (unknown function {event.name}!);
}
}
3. Use a directory like Ethhers.js
Ethers.