首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何使用web3.js 1.0认证和发送合同方法

如何使用web3.js 1.0认证和发送合同方法
EN

Stack Overflow用户
提问于 2017-10-07 01:34:39
回答 4查看 12.5K关注 0票数 8

我对如何使用web3 1.0库执行约定的方法感到困惑。

此代码可以工作(只要我先手动解锁帐户):

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

我得到这个错误(如果我不先手动解锁):

返回错误:需要身份验证:密码或解锁

上面的代码是node.js中的一个API端点,所以我希望它以编程方式解锁或进行身份验证。

web3.js 1.0中没有解锁账号的方法。

我也不认为这是必要的(至少这是我感到困惑的地方)。因为我在管理账户,所以我知道私钥是什么。

我认为交易需要使用私钥进行签名??这是正确的吗?这实际上等同于“解锁帐户”吗?

我试过这样做:

代码语言:javascript
复制
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);

我得到了这个错误:

返回错误:方法net_version不存在/不可用

验证和提交事务的最简单方法是什么?

理想情况下,我希望在我的代码样本中使用第一种方法,因为它是最干净的。

EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2017-10-10 04:04:16

这段代码允许我使用我创建的帐户(使用web3.eth.accounts.create())中的privateKey对事务服务器端(node.js)进行签名,并将签名的事务发送到网络,而无需解锁该帐户。

我使用的是Geth 1.7.1

代码语言:javascript
复制
  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);
  });
票数 23
EN

Stack Overflow用户

发布于 2018-06-10 02:14:59

无需对事务进行显式签名即可调用合约方法的一种方法是(web3js 1.0.0):

代码语言:javascript
复制
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
        })
票数 12
EN

Stack Overflow用户

发布于 2018-03-11 09:42:19

下面是一个完整的示例,说明如何在没有本地钱包帐户的情况下对事务进行签名。如果您正在使用infura进行事务处理,则尤其有用。这篇文章是为

代码语言:javascript
复制
'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);
});

使用

"web3":"1.0.0-beta.30"

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/46611117

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档