Short answer
msg.sender
does work in a view
function, although it is useless as an authorization scheme. The lookup tool you use should have a mechanism to set the sender.
Call vs Transaction
First, it's important to understand the difference between a call and a transaction.
It appears you're running a call
, which runs quickly and does not alter the state of the blockchain. msg.sender
is set in both a transaction and a call. In a transaction, it cannot be faked: you must have the private key associated with the given account. But in a call
, you are free to set the sender to any value you like.
Setting the Sender
How you set the sender depends on what tool you are using to call. That tool might be web3.js, web3.py, Mist, MyEtherWallet, MyCrypto, etc. They all have (or might not have!) a mechanism to set the sender in a call.
MyEtherWallet
In the comments, you mention MyEtherWallet specifically. In a quick search, I didn't find anything about how to set the sender. There is this unanswered question on ethereum.stackexchange that seems worth following, since it is asking roughly the same question: How to check msg.sender balance with MyEtherWallet contract
Contract Workarounds
is it possible to specify such settings for the contract?
There is no way to help someone set the sender from inside the contract. But you can supply a different method that takes an address as an argument. Then tools like MyEtherWallet will allow you to set the address of interest. For example:
function getLink(address account) public view returns(string){
if(tokenBalances[account] > 0){
return link;
}else{
return "You need to purchase a token at first...";
}
}
Hiding Data
It's worth noting that hiding data by checking msg.sender
is useless. Anyone can set a fake sender in a call (or directly inspect blockchain state). So, it's trivial to bypass this "protection."