简体   繁体   中英

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). 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). 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)?

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)

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 .

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)?

You are already checking is msg.sender matches the whitelisted address. 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 ).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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