4

What I have done
1. A running private blockchain.
2. written and deploy a smart contract.
3. smart contract works fine on my private blockchain

What I want
1. Only authorized account can access my contract, not by another one.
2. i want to maintain transaction log of contract, which account access that contract and which method that account accessed.

  • This is not clear. Do you want to restrict which accounts can modify a contract in some way? (Write permissions/guards) Do you want to restrict which accounts can read some data inside a contract (encryption/hashes). Or something else? – JackWinters Nov 28 '16 at 17:44
  • @JackWinters, Yes this is what i want "Do you want to restrict which accounts can modify a contract in some way?". – Imroz Nov 29 '16 at 5:15
2

So it sounds like you just need the two patterns: access restrictions and Events. The first throws if the caller is not on some list. The second adds events to the contract's event log. So if you just had one single user your contract would look something like:

contract SomeContract {

    //Assigned when contract is created so if you create it it is you
    address public owner = msg.sender; 

    event LogAccessAttempt(address account, string function, uint timestamp); // Event

    // This could be any function
    function someFunction()
    {
      // Do logging
      LogAccessAttempt(msg.sender,"someFunction",now);
      //only allow the owner to call this function
      if (msg.sender !=owner) throw;
      // do something else
    }

    // You can assign ownership using this function
    function changeOwner(address _newOwner)
    {
        LogAccessAttempt(msg.sender,"changeOwner",now);
        //only allow the owner to call this function
        if (msg.sender !=owner) throw;
        owner = _newOwner;
    }
}

As you have more functions this will probably get a little tedious so you could improve this by using modifiers and also you probably want multiple authorised accounts e.g

contract SomeContract {

mapping(address => bool) public users;   
users[msg.sender]=true;
modifier guardAndLog(string msg )
        {
            LogAccessAttempt(msg.sender,msg,now);
            if(!users[msg.sender])
               throw
            _;
        }

    function addUser(address _newUser)
    guardAndLog("someone tried to add a user")
    {
       users[_newUser]=true
    }

    function someFunction()
     guardAndLog("someone tried to access someFunction")
    {
    // Do something
    }

   }
  • What is "addrs" above code ? modifier guardAndLog(string msg ) { LogAccessAttempt(msg.sender,msg,now); for(i = 0; i < addrs.length; ++i) if(!users[msg.sender]) throw _; } @JackWinters – Imroz Nov 30 '16 at 7:49
  • Sorry that was a mistake have deleted the loop. – JackWinters Nov 30 '16 at 14:54
0

If you're talking about private transaction between two nodes, it's not possible with Ethereum. Check out JP Morgan's Quorum. https://github.com/jpmorganchase/quorum

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.