我计划接受不了解'公/私钥和以太坊'的人们的数据,我想将此数据交易到我已经在以太坊区块链上部署的智能合约。
我无法使用web3,因为用户将没有任何以太坊钱包。在我的情况下,此条件是必需的。
如何将数据放入合同:示例方案:与get()和set()的Greeter合同。
我只是想在每次将一些数据发布到服务器时自动运行脚本,以便将此数据处理到set()中的Greeter合同中。始终使用相同的公钥/私钥对。
任何建议或虚拟脚本都会有所帮助。
您在这里:
const express = require("express");
const ethers = require("ethers");
const fs = require("fs");
const app = express();
const port = 3000;
// The contract ABI
let abi = [
{
constant: true,
inputs: [],
name: "get",
outputs: [{ internalType: "uint256", name: "", type: "uint256" }],
payable: false,
stateMutability: "view",
type: "function"
},
{
constant: false,
inputs: [{ internalType: "uint256", name: "x", type: "uint256" }],
name: "set",
outputs: [],
payable: false,
stateMutability: "nonpayable",
type: "function"
}
];
// Connect to contract
// https://goerli.etherscan.io/address/0x00530Aae5fDFa2C4cFe21Df5Fbe1B2213fe8f551#code
let provider = ethers.getDefaultProvider("goerli");
let contractAddress = "0x00530Aae5fDFa2C4cFe21Df5Fbe1B2213fe8f551";
let contract = new ethers.Contract(contractAddress, abi, provider);
app.get("/:message", async (req, res) => {
try {
userInput = req.params.message;
console.log("User input:", userInput); // if it prints 'favicon.ico', that's normal
userNumber = parseInt(userInput);
// Make sure user input is a number
if (isNaN(userNumber)) {
return res.send("Please only send number").end();
}
// Do not store private key in code
let privateKey = fs
.readFileSync("private-key.txt")
.toString()
.trim();
let wallet = new ethers.Wallet(privateKey, provider);
let contractWithSigner = contract.connect(wallet);
let tx = await contractWithSigner.set(userNumber);
res.send(
`Tx with number '${userNumber}' has been sent to contract.\nHash: ${tx.hash}.`
);
// When the tx has been mined, you can verify it here:
// https://goerli.etherscan.io/address/0x00530aae5fdfa2c4cfe21df5fbe1b2213fe8f551#readContract
} catch (error) {
return next(error);
}
});
app.listen(port, () => console.log(`App is listening on port ${port}!`));
请参阅this repository以获取完整的源代码。可以找到合同here。