用户应该如何与智能合约交互?

How should users interact with a smart contract?

部署智能合约时,如何让我自己的平台(移动或网络)中的用户与其交互? 假设我有以下合同:

contract Test {
    event Log(address addr);

    function logMe () public {
        Log(msg.sender);
    }
}

为了使用它,我必须有权访问用户的私钥和 public 密钥。是否可以让用户通过他们自己的账户与区块链交互而不需要他们的凭据?

是的。通常这是工作的方式是你有一个托管的网络应用程序说使用 javascript (查看 web3:https://github.com/ethereum/web3.js/) to interact with your smart contract. The user navigates to your web app and then their account is connected so that they can send requests to your contract (for understanding how they connect their account look into metamask: https://metamask.io/ or running an ethereum node e.g. geth/parity). Here is a good tutorial that explains the workflow ive loosely described using a contract development framework called Truffle: http://truffleframework.com/tutorials/pet-shop.

编辑:所以回答你关于凭据的问题,不,你不必拥有他们的凭据。

首先,如果您尝试使用 Remix. And you have used the created API to your created contract, You are actually interacting with your contract with web. You can skim this video to see how to deploy and use call functions inside your contract. I encourage you to watch this video and do its tutorial here 在区块链上部署合约。

如果你想调用函数(Public),你可以通过三个步骤使用你的合约。

第一步:在区块链上部署你的合约并找到你的 ABI 和合约地址。例如,如果您使用 remix 部署合约,您可以通过单击编译选项卡中的详细信息来查看这些信息。

第二步:使用 web3 并将其注入您的网络浏览器(安装 Metamask 然后您已经将 web3 注入浏览器)

第三步:通过设置您从第 1 步获得的 web3 提供程序和 ABI 以及合约地址,创建一个类似于实例的合约 API。

第四步:调用你的合约函数。

这是确保已注入 web3 并且已连接到正确的区块链(测试网/主网)的方法

var Web3 = require('web3');

if (typeof web3 !== 'undefined') {
     // Use Mist/MetaMask's provider
     console.log('Web3 exist!')
     console.log(web3)
     web3 = new Web3(web3.currentProvider);

     web3.version.getNetwork((err, netId) => {
      switch (netId) {
        case "1":
          console.log('This is mainnet')
          break
        case "2":
          console.log('This is the deprecated Morden test network.')
          break
        case "3":
          console.log('This is the ropsten test network.')
          break
        default:
          console.log('This is an unknown network.')
      }
    })

 } else {
     console.log('No web3? You should consider trying MetaMask!')
     // fallback - use your fallback strategy (local node / hosted node + in-dapp id mgmt / fail)
     web3 = new Web3(new Web3.providers.HttpProvider("http://localhost:8545"));
 }

您可以通过以下方式与已部署的合约进行交互。

 var fooContract = web3.eth.contract( YOUR_ABI, (err, ctr) => { return ctr} );
     web3.eth.defaultAccount = web3.eth.accounts[0];
     $scope.accounts = web3.eth.accounts;

     console.log(web3.eth.defaultAccount);
    var CONTRACT = fooContract.at('YOUR_Deployed_contract_ADDRESS',(err, ctr)=>{
      return ctr;
    } ) 

现在您可以轻松地使用 CONTRACT 变量来调用其 public 函数。

通话看起来像这样:

CONTRACT.contractFunction(params)

PS:如有任何问题请联系我(一言难尽post)!