[英]How to authenticate and send contract method using web3.js 1.0
I am confused about how I should be executing a contract's method using the web3 1.0 library.我对如何使用 web3 1.0 库执行合同方法感到困惑。
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.上面的代码是 node.js 中的一个 API 端点,所以我希望它以编程方式解锁或验证。
There is no method in web3.js 1.0 to unlock the account. web3.js 1.0 中没有解锁账户的方法。
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返回错误:方法net_version不存在/不可用
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.理想情况下,我想在我的代码示例中使用第一种方法,因为它是最干净的。
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.此代码允许我使用我创建的帐户(使用 web3.eth.accounts.create())中的私钥签署交易服务器端(node.js),并将签署的交易发送到网络,而无需解锁帐户。
I am using Geth 1.7.1我正在使用 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);
});
A way to be able to call your contract methods without having to sign the transaction explicitly is this (web3js 1.0.0):一种无需显式签署交易即可调用合约方法的方法是(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
})
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.如果您使用 infura 进行交易,则特别有用。 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" “web3”:“1.0.0-beta.30”
This is my implementation using "@truffle/hdwallet-provider": "^2.0.3",
"web3": "^1.6.1",
这是我使用"@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然后在send
方法中
contract.methods.safeMint(receiverAddress, itemUri).send({
from: NFT_MINTER_ACCOUNT,
});
in call
methods在call
方法中
contract.methods.balanceOf(address).call();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.