Skip to content
This repository has been archived by the owner on Sep 26, 2019. It is now read-only.

simple account permissioning #1409

Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ pragma solidity >=0.4.0 <0.6.0;
// DO NOT USE THIS CONTRACT IN PRODUCTION APPLICATIONS

contract SimpleAccountPermissioning {
address[] private whitelist;
mapping (address => bool) private whitelist;

function transactionAllowed(
address sender,
Expand All @@ -16,22 +16,12 @@ contract SimpleAccountPermissioning {
return whitelistContains(sender);
}
function addAccount(address account) public {
whitelist.push(account);
whitelist[account] = true;
}
function removeAccount(address account) public {
for (uint256 i = 0; i < whitelist.length; i++) {
if (whitelist[i] == account) {
whitelist[i] = whitelist[whitelist.length - 1];
whitelist.length --;
}
}
whitelist[account] = false;
}
function whitelistContains(address account) public view returns(bool) {
for (uint256 i = 0; i < whitelist.length; i++) {
if (whitelist[i] == account) {
return true;
}
}
return false;
return whitelist[account];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ pragma solidity >=0.4.0 <0.6.0;
// THIS CONTRACT IS FOR TESTING PURPOSES ONLY
// DO NOT USE THIS CONTRACT IN PRODUCTION APPLICATIONS

contract SimplePermissioning {
contract SimpleNodePermissioning {
struct Enode {
bytes32 enodeHigh;
bytes32 enodeLow;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
var SimplePermissioning = artifacts.require("SimplePermissioning");
var SimpleNodePermissioning = artifacts.require("SimpleNodePermissioning");
var SimpleAccountPermissioning = artifacts.require("SimpleAccountPermissioning");

module.exports = function(deployer) {
deployer.deploy(SimplePermissioning);
deployer.deploy(SimpleNodePermissioning);
deployer.deploy(SimpleAccountPermissioning);
};
Original file line number Diff line number Diff line change
Expand Up @@ -10,38 +10,34 @@ var gasLimit = 77;
var payload = "0x1234";

contract('Permissioning: Accounts', () => {
describe('Function: permissioning', () => {

it('Should NOT permit any transaction when acount whitelist is empty', async () => {
proxy = await TestPermissioning.new();
let permitted = await proxy.transactionAllowed(address1, address2, value, gasPrice, gasLimit, payload);
assert.equal(permitted, false, 'expected tx NOT permitted');
});

it('Should add an account to the whitelist and then permit that node', async () => {
await proxy.addAccount(address1);
let permitted = await proxy.transactionAllowed(address1, address2, value, gasPrice, gasLimit, payload);
assert.equal(permitted, true, 'added address1: expected tx1 to be permitted');

// await another
await proxy.addAccount(address2);
permitted = await proxy.transactionAllowed(address2, address1, value, gasPrice, gasLimit, payload);
assert.equal(permitted, true, 'added address2: expected tx2 to be permitted');

// first one still permitted
permitted = await proxy.transactionAllowed(address1, address2, value, gasPrice, gasLimit, payload);
assert.equal(permitted, true, 'expected tx from address1 to still be permitted');
});

it('Should remove an account from the whitelist and then NOT permit that account to send tx', async () => {
await proxy.removeAccount(address2);
let permitted = await proxy.transactionAllowed(address2, address1, value, gasPrice, gasLimit, payload);
assert.equal(permitted, false, 'expected removed account (address2) NOT permitted to send tx');

// first one still permitted
permitted = await proxy.transactionAllowed(address1, address2, value, gasPrice, gasLimit, payload);
assert.equal(permitted, true, 'expected tx from address1 to still be permitted');
});
it('Should NOT permit any transaction when acount whitelist is empty', async () => {
Copy link
Contributor

@Errorific Errorific May 9, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sp acount

proxy = await TestPermissioning.new();
let permitted = await proxy.transactionAllowed(address1, address2, value, gasPrice, gasLimit, payload);
assert.equal(permitted, false, 'expected tx NOT permitted');
});

it('Should add an account to the whitelist and then permit that node', async () => {
await proxy.addAccount(address1);
let permitted = await proxy.transactionAllowed(address1, address2, value, gasPrice, gasLimit, payload);
assert.equal(permitted, true, 'added address1: expected tx1 to be permitted');

// await another
await proxy.addAccount(address2);
permitted = await proxy.transactionAllowed(address2, address1, value, gasPrice, gasLimit, payload);
assert.equal(permitted, true, 'added address2: expected tx2 to be permitted');

// first one still permitted
permitted = await proxy.transactionAllowed(address1, address2, value, gasPrice, gasLimit, payload);
assert.equal(permitted, true, 'expected tx from address1 to still be permitted');
});

it('Should remove an account from the whitelist and then NOT permit that account to send tx', async () => {
await proxy.removeAccount(address2);
let permitted = await proxy.transactionAllowed(address2, address1, value, gasPrice, gasLimit, payload);
assert.equal(permitted, false, 'expected removed account (address2) NOT permitted to send tx');

// first one still permitted
permitted = await proxy.transactionAllowed(address1, address2, value, gasPrice, gasLimit, payload);
assert.equal(permitted, true, 'expected tx from address1 to still be permitted');
});
});
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const TestPermissioning = artifacts.require('SimplePermissioning.sol');
const TestPermissioning = artifacts.require('SimpleNodePermissioning.sol');
var proxy;

var node1High = "0x9bd359fdc3a2ed5df436c3d8914b1532740128929892092b7fcb320c1b62f375";
Expand All @@ -12,52 +12,48 @@ var node2Host = "0x596c3d8914b1532fdc3a2ed5df439bd3";
var node2Port = 30304;

contract('Permissioning: Nodes', () => {
describe('Function: permissioning', () => {

it('Should NOT permit any node when none have been added', async () => {
proxy = await TestPermissioning.new();
let permitted = await proxy.enodeAllowed(node1High, node1Low, node1Host, node1Port);
assert.equal(permitted, false, 'expected node NOT permitted');
});

it('Should compute key', async () => {
let key1 = await proxy.computeKey(node1High, node1Low, node1Host, node1Port);
let key2 = await proxy.computeKey(node1High, node1Low, node1Host, node1Port);
assert.equal(key1, key2, "computed keys should be the same");

let key3 = await proxy.computeKey(node1High, node1Low, node1Host, node2Port);
assert(key3 != key2, "keys for different ports should be different");
});

it('Should add a node to the whitelist and then permit that node', async () => {
await proxy.addEnode(node1High, node1Low, node1Host, node1Port);
let permitted = await proxy.enodeAllowed(node1High, node1Low, node1Host, node1Port);
assert.equal(permitted, true, 'expected node added to be permitted');

// await another
await proxy.addEnode(node2High, node2Low, node2Host, node2Port);
permitted = await proxy.enodeAllowed(node2High, node2Low, node2Host, node2Port);
assert.equal(permitted, true, 'expected node 2 added to be permitted');

// first one still permitted
permitted = await proxy.enodeAllowed(node1High, node1Low, node1Host, node1Port);
assert.equal(permitted, true, 'expected node 1 added to be permitted');
});

it('Should allow a connection between 2 added nodes', async () => {
let permitted = await proxy.connectionAllowed(node1High, node1Low, node1Host, node1Port, node2High, node2Low, node2Host, node2Port);
assert.equal(permitted, '0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff', 'expected 2 added nodes to work as source <> destination');
});

it('Should remove a node from the whitelist and then NOT permit that node', async () => {
await proxy.removeEnode(node1High, node1Low, node1Host, node1Port);
let permitted = await proxy.enodeAllowed(node1High, node1Low, node1Host, node1Port);
assert.equal(permitted, false, 'expected removed node NOT permitted');

permitted = await proxy.connectionAllowed(node1High, node1Low, node1Host, node1Port, node2High, node2Low, node2Host, node2Port);
assert.equal(permitted, '0x7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff', 'expected source disallowed since it was removed');

});
it('Should NOT permit any node when none have been added', async () => {
proxy = await TestPermissioning.new();
let permitted = await proxy.enodeAllowed(node1High, node1Low, node1Host, node1Port);
assert.equal(permitted, false, 'expected node NOT permitted');
});

it('Should compute key', async () => {
let key1 = await proxy.computeKey(node1High, node1Low, node1Host, node1Port);
let key2 = await proxy.computeKey(node1High, node1Low, node1Host, node1Port);
assert.equal(key1, key2, "computed keys should be the same");

let key3 = await proxy.computeKey(node1High, node1Low, node1Host, node2Port);
assert(key3 != key2, "keys for different ports should be different");
});

it('Should add a node to the whitelist and then permit that node', async () => {
await proxy.addEnode(node1High, node1Low, node1Host, node1Port);
let permitted = await proxy.enodeAllowed(node1High, node1Low, node1Host, node1Port);
assert.equal(permitted, true, 'expected node added to be permitted');

// await another
await proxy.addEnode(node2High, node2Low, node2Host, node2Port);
permitted = await proxy.enodeAllowed(node2High, node2Low, node2Host, node2Port);
assert.equal(permitted, true, 'expected node 2 added to be permitted');

// first one still permitted
permitted = await proxy.enodeAllowed(node1High, node1Low, node1Host, node1Port);
assert.equal(permitted, true, 'expected node 1 added to be permitted');
});

it('Should allow a connection between 2 added nodes', async () => {
let permitted = await proxy.connectionAllowed(node1High, node1Low, node1Host, node1Port, node2High, node2Low, node2Host, node2Port);
assert.equal(permitted, '0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff', 'expected 2 added nodes to work as source <> destination');
});

it('Should remove a node from the whitelist and then NOT permit that node', async () => {
await proxy.removeEnode(node1High, node1Low, node1Host, node1Port);
let permitted = await proxy.enodeAllowed(node1High, node1Low, node1Host, node1Port);
assert.equal(permitted, false, 'expected removed node NOT permitted');

permitted = await proxy.connectionAllowed(node1High, node1Low, node1Host, node1Port, node2High, node2Low, node2Host, node2Port);
assert.equal(permitted, '0x7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff', 'expected source disallowed since it was removed');

});
});
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public void setUp() {
bootnode = bootnode("bootnode");
forbiddenNode = node("forbidden-node");
allowedNode = node("allowed-node");
permissionedNode = permissionedNode("pemissioned-node");
permissionedNode = permissionedNode("permissioned-node");

permissionedCluster.start(bootnode, forbiddenNode, allowedNode, permissionedNode);

Expand Down