Added secret to MessageTribute.sol

This commit is contained in:
Richard Ramos 2018-03-26 17:01:23 -04:00
parent 752d025097
commit 87e643bb30
2 changed files with 88 additions and 26 deletions

View File

@ -32,6 +32,7 @@ contract MessageTribute {
uint256 blockNum;
uint256 timestamp;
Fee fee;
bytes32 hashedSecret;
}
mapping(address => mapping(address => Audience)) audienceRequested;
@ -63,8 +64,8 @@ contract MessageTribute {
}
}
function isFriend(address _friend) public view returns(bool) {
return friendIndex[keccak256(_friend, msg.sender)] > 0;
function areFriends(address sourceAccount, address accountToCheck) public view returns(bool) {
return friendIndex[keccak256(sourceAccount, accountToCheck)] > 0;
}
function setRequiredTribute(address _to, uint _amount, bool _isTribute, bool _isPermanent) public {
@ -101,7 +102,7 @@ contract MessageTribute {
event AudienceCancelled(address indexed from, address indexed to);
event AudienceGranted(address indexed from, address indexed to, bool approve);
function requestAudience(address _from)
function requestAudience(address _from, bytes32 hashedSecret)
public
{
Fee memory f = getFee(_from);
@ -109,7 +110,7 @@ contract MessageTribute {
require(audienceRequested[_from][msg.sender].blockNum == 0);
AudienceRequested(_from, msg.sender);
audienceRequested[_from][msg.sender] = Audience(block.number, now, f);
audienceRequested[_from][msg.sender] = Audience(block.number, now, f, hashedSecret);
balances[msg.sender] -= f.amount;
}
@ -126,11 +127,13 @@ contract MessageTribute {
delete audienceRequested[_from][msg.sender];
}
function grantAudience(address _to, bool _approve) public {
function grantAudience(address _to, bool _approve, bytes32 secret) public {
Audience storage aud = audienceRequested[msg.sender][_to];
require(aud.blockNum > 0);
require(aud.hashedSecret == keccak256(msg.sender, _to, _approve, secret));
AudienceGranted(msg.sender, _to, _approve);
@ -138,6 +141,7 @@ contract MessageTribute {
uint256 amount = aud.fee.amount;
delete audienceRequested[msg.sender][_to];
clearFee(msg.sender, _to);
if (isTribute) {

View File

@ -1,8 +1,15 @@
const TestUtils = require("../utils/testUtils.js");
const MessageTributeUtils = require("../utils/messageTributeUtils.js");
const identityJson = require('../dist/contracts/Identity.json');
const idUtils = require('../utils/identityUtils.js');
describe('MessageTribute', function() {
let identityFactory;
let identity;
let accounts;
let identities = [];
let idInstances = [];
let SNT;
this.timeout(0);
@ -11,6 +18,10 @@ describe('MessageTribute', function() {
this.timeout(0);
EmbarkSpec.deployAll({
"IdentityFactory": {
args: ["0xaaaa"],
gas: 5000000
},
"MiniMeTokenFactory": {},
"MiniMeToken": {
"args": [
@ -26,22 +37,46 @@ describe('MessageTribute', function() {
"MessageTribute": {
"args": ["$MiniMeToken"]
}
}, (_accounts) => {
}, async (_accounts) => {
accounts = _accounts;
SNT = MiniMeToken;
SNT = MiniMeToken;
Promise.all([
SNT.methods.generateTokens(accounts[0], 5000).send(),
SNT.methods.generateTokens(accounts[1], 5000).send(),
SNT.methods.generateTokens(accounts[2], 5000).send(),
SNT.methods.generateTokens(accounts[3], 5000).send(),
SNT.methods.generateTokens(accounts[4], 5000).send(),
SNT.methods.generateTokens(accounts[5], 5000).send(),
SNT.methods.generateTokens(accounts[6], 5000).send(),
SNT.methods.generateTokens(accounts[7], 5000).send(),
SNT.methods.generateTokens(accounts[8], 5000).send(),
SNT.methods.generateTokens(accounts[9], 5000).send()
identities[0] = new web3.eth.Contract(identityJson.abi, (await IdentityFactory.methods.createIdentity().send({from: accounts[0]})).events.IdentityCreated.returnValues.instance, {from: accounts[0]});
/* identities[1] = new web3.eth.Contract(identityJson.abi, (await IdentityFactory.methods.createIdentity().send({from: accounts[1]})).events.IdentityCreated.returnValues.instance);
identities[2] = new web3.eth.Contract(identityJson.abi, (await IdentityFactory.methods.createIdentity().send({from: accounts[2]})).events.IdentityCreated.returnValues.instance);
identities[3] = new web3.eth.Contract(identityJson.abi, (await IdentityFactory.methods.createIdentity().send({from: accounts[3]})).events.IdentityCreated.returnValues.instance);
identities[4] = new web3.eth.Contract(identityJson.abi, (await IdentityFactory.methods.createIdentity().send({from: accounts[4]})).events.IdentityCreated.returnValues.instance);
identities[5] = new web3.eth.Contract(identityJson.abi, (await IdentityFactory.methods.createIdentity().send({from: accounts[5]})).events.IdentityCreated.returnValues.instance);
identities[6] = new web3.eth.Contract(identityJson.abi, (await IdentityFactory.methods.createIdentity().send({from: accounts[6]})).events.IdentityCreated.returnValues.instance);
identities[7] = new web3.eth.Contract(identityJson.abi, (await IdentityFactory.methods.createIdentity().send({from: accounts[7]})).events.IdentityCreated.returnValues.instance);
identities[8] = new web3.eth.Contract(identityJson.abi, (await IdentityFactory.methods.createIdentity().send({from: accounts[8]})).events.IdentityCreated.returnValues.instance);
identities[9] = new web3.eth.Contract(identityJson.abi, (await IdentityFactory.methods.createIdentity().send({from: accounts[9]})).events.IdentityCreated.returnValues.instance);
*/
try {
await identities[0].methods.execute(identities[0].options.address, 0, idUtils.encode.addKey(accounts[2], idUtils.purposes.ACTION, idUtils.types.ADDRESS)).send({from: accounts[0]})
} catch(Er){
console.log(Er);
}
Promise.all([
// identities[1].methods.execute(identities[1].options.address, 0, idUtils.encode.addKey(accounts[1], idUtils.purposes.ACTION, idUtils.types.ADDRESS)).send({from: accounts[1]}),
SNT.methods.generateTokens(identities[0].options.address, 5000).send(),
SNT.methods.generateTokens(identities[1].options.address, 5000).send(),
SNT.methods.generateTokens(identities[2].options.address, 5000).send(),
SNT.methods.generateTokens(identities[3].options.address, 5000).send(),
SNT.methods.generateTokens(identities[4].options.address, 5000).send(),
SNT.methods.generateTokens(identities[5].options.address, 5000).send(),
SNT.methods.generateTokens(identities[6].options.address, 5000).send(),
SNT.methods.generateTokens(identities[7].options.address, 5000).send(),
SNT.methods.generateTokens(identities[8].options.address, 5000).send(),
SNT.methods.generateTokens(identities[9].options.address, 5000).send()
])
.then(() => {
console.log(" - Added balances");
@ -51,33 +86,56 @@ describe('MessageTribute', function() {
});
it("Adding friends", async () => {
/* let tx = await identities[0].methods.execute(
MessageTribute.options.address,
0,
MessageTributeUtils.encode.addFriends([accounts[1], accounts[2]])
).send({from: accounts[0]});
console.log(tx);
*/
await MessageTribute.methods.addFriends([accounts[1], accounts[2]]).send({from: accounts[0]});
await MessageTribute.methods.addFriends([accounts[3]]).send({from: accounts[1]});
await MessageTribute.methods.addFriends([accounts[4]]).send({from: accounts[1]});
await MessageTribute.methods.addFriends([accounts[5]]).send({from: accounts[1]});
assert.equal(
await MessageTribute.methods.isFriend(accounts[1]).call({from: accounts[0]}),
await MessageTribute.methods.areFriends(identities[0].options.address, identities[1].options.address).call(),
true,
accounts[1] + " must be a friend of " + accounts[0]);
identities[1].options.address + " must be a friend of " + identities[0].options.address);
assert.equal(
await MessageTribute.methods.isFriend(accounts[3]).call({from: accounts[1]}),
await MessageTribute.methods.areFriends(identities[1].options.address, identities[3].options.address).call(),
true,
accounts[3] + " must be a friend of " + accounts[1]);
identities[3].options.address + " must be a friend of " + identities[1].options.address);
assert.equal(
await MessageTribute.methods.isFriend(accounts[4]).call({from: accounts[0]}),
await MessageTribute.methods.areFriends(identities[0].options.address, identities[4].options.address).call(),
false,
accounts[4] + " must not be a friend of " + accounts[0]);
identities[4].options.address + " must not be a friend of " + identities[0].options.address);
assert.equal(
await MessageTribute.methods.isFriend(accounts[2]).call({from: accounts[1]}),
await MessageTribute.methods.areFriends(identities[1].options.address, identities[2].options.address).call(),
false,
accounts[2] + " must not be a friend of " + accounts[1]);
identities[2].options.address + " must not be a friend of " + identities[1].options.address);
});
it("Removing friends", async () => {
await MessageTribute.methods.removeFriends([accounts[3]]).send({from: accounts[1]});