简体   繁体   English

我的智能合约将在哪里进行交易

[英]Where would a transaction take place in my Smart Contract

I am making a simple smart contract that is essentially a ledger that people can sign with a string (UI to come) and then pass onto a next person (using their ether address). 我正在制作一个简单的智能合约,该合约本质上是一个分类帐,人们可以用字符串签名(UI来),然后传递给下一个人(使用其以太地址)。 I just wanted to create something that could be passed from person to person while recording its journey. 我只想创建一些可以在记录其旅程时在人与人之间传递的东西。

The logic of 'transferring' the signing-ledger is all done within the smart contract, with it existing as a mapping and the key value of an address changing from 0 to 1 signifying ownership (1 person owning it at a time). “转移”签名分类帐的逻辑全部在智能合约中完成,它以映射的形式存在,并且地址的键值从0变为1表示所有权(一次拥有1个人)。 Because there's no actual transfer of ether/actual monetary value, where would actual ethereum transactions be occurring in this contract? 因为没有以太币/实际货币价值的实际转移,所以该合约中实际的以太坊交易将发生在哪里?

Also to sign the ledger I verify that the public key signing it has 1 as its value in the mapping, but how would I check that that person owns that public key (using a private key)? 另外,要对分类帐进行签名,我要验证签名它的公钥在映射中的值为1,但是我将如何检查该人拥有该公钥(使用私钥)呢?

my solidity code so far: 我到目前为止的代码:

pragma solidity ^0.4.4;

contract Pass{
mapping(address => bool) ownership;
mapping(address => string) notes;

constructor(address genesis) public {
    ownership[genesis] = true; //this address starts with it
}

function checkOwnership(address p) public view returns(bool){
    if(ownership[p]){
        return true;
    }
    return false;
}

function sign(string signedNote) public returns(uint){ // 1 on success 0 on fail
    if(checkOwnership(msg.sender)){ //if msg.sender owns the note
        notes[msg.sender] = signedNote;
        return 1;
    }
    return 0;
}

function pass(address recipient) public returns(uint){ // 1 on success 0 on fail
    if(checkOwnership(msg.sender)){ //if msg.sender owns the note
        ownership[msg.sender] = 0;
        ownership[recipient] = 1;
        return 1;
    }
    return 0;
}

function viewNotes(address participant) public returns(string){ // signed note on success nothing on fail
    if(notes[participant] !== 0){
        return (notes(participant));   
    }
}

} }

The logic of 'transferring' the signing-ledger is all done within the smart contract, with it existing as a mapping and the key value of an address changing from 0 to 1 signifying ownership (1 person owning it at a time) “转移”签名分类帐的逻辑全部在智能合约中完成,它以映射的形式存在,并且地址的键值从0变为1表示所有权(一次拥有1个人)

This is a common pattern known as the owner pattern. 这是一种常见的模式,称为所有者模式。 You can simplify this by simply keeping track of a single owner address and updating that, instead of using a mapping, since you only care about the current owner. 您只需跟踪单个所有者地址并更新它即可,而不用使用映射,因为您只关心当前所有者,因此可以简化此操作。 Something as simple as: 像这样简单:

address public owner;

modifier onlyOwner() {
    require(msg.sender == owner);
    _;
}

constructor() {
    owner = msg.sender;
}

function transferOwnership(address _owner) onlyOwner {
    owner = _owner;
}

Open Zeppelin has a more complete implementation of ownership here . Open Zeppelin 在这里拥有所有权的更完整实施。

Because there's no actual transfer of ether/actual monetary value, where would actual ethereum transactions be occurring in this contract? 因为没有以太币/实际货币价值的实际转移,所以该合约中实际的以太坊交易将发生在哪里?

Transactions do not require ether movement. 交易不需要以太币移动。 You can call any public/external function on your contract with a 0 value transaction, and pass data to it. 您可以使用零值交易调用合同上的任何公共/外部函数,并将数据传递给该函数。 The transaction will execute on the blockchain like any other, and run the code you invoke within the contract. 交易将像其他任何交易一样在区块链上执行,并运行您在合同内调用的代码。

Also to sign the ledger I verify that the public key signing it has 1 as its value in the mapping, but how would I check that that person owns that public key (using a private key)? 另外,要对分类帐进行签名,我要验证签名它的公钥在映射中的值为1,但是我将如何检查该人拥有该公钥(使用私钥)呢?

You are already checking is msg.sender matches the whitelisted address. 您已经在检查msg.sender与列入白名单的地址匹配。 For basic transactions, this means that the transaction has been signed by the address in msg.sender ( msg.sender can also be a contract, if another contract calls yours. To check who actually signed the transaction in all scenarios, you must use tx.origin ). 对于基本交易,这意味着该交易已通过msg.sender中的地址进行了签名( msg.sender也可以是一个合同,如果另一个合同与您进行了联系。要检查在所有情况下是谁实际签署了该交易,则必须使用tx.origin )。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM