Skip to content

Commit

Permalink
feat(@ew-did-registry/proxyidentity): cancel identity offer
Browse files Browse the repository at this point in the history
  • Loading branch information
JGiter committed Mar 19, 2021
1 parent 9930171 commit 6f5d78f
Show file tree
Hide file tree
Showing 13 changed files with 19,946 additions and 3,145 deletions.
9,351 changes: 8,902 additions & 449 deletions package-lock.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@
"prettier": "2.0.4",
"prettier-plugin-solidity": "1.0.0-alpha.44",
"run-with-testrpc": "0.3.1",
"truffle": "5.1.16",
"truffle": "^5.2.4",
"ts-node": "8.6.2",
"tsify": "4.0.1",
"typedoc": "0.17.4",
Expand Down
3 changes: 2 additions & 1 deletion packages/claims/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"types": "./dist/index.d.ts",
"scripts": {
"test": "../../node_modules/.bin/mocha -r ts-node/register test/**/*.test.ts",
"test-rpc": "../../node_modules/.bin/run-with-testrpc -m \"candy maple cake sugar pudding cream honey rich smooth crumble sweet treat\" --port 8544 --accounts 20 --networkId=9 --gasLimit=10000000 \"npm run test \"",
"test-rpc": "run-with-testrpc -m \"candy maple cake sugar pudding cream honey rich smooth crumble sweet treat\" --port 8544 --accounts 20 --networkId=9 --gasLimit=10000000 \"npm run test \"",
"compile": "../../node_modules/.bin/tsc",
"lint": "../../node_modules/.bin/eslint src/**/*.ts",
"fix": "../../node_modules/.bin/eslint src/**/*.ts --fix"
Expand Down Expand Up @@ -41,6 +41,7 @@
"sjcl": "npm:[email protected]"
},
"devDependencies": {
"run-with-testrpc": "0.3.1",
"ganache-cli": "6.9.1"
}
}
4 changes: 4 additions & 0 deletions packages/did-ethr-resolver/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,9 @@
"@ew-did-registry/did-resolver-interface": "0.2.0",
"@ew-did-registry/keys": "0.2.0",
"ethers": "4.0.45"
},
"devDependencies": {
"run-with-testrpc": "0.3.1",
"ganache-cli": "6.9.1"
}
}
4 changes: 4 additions & 0 deletions packages/did-ipfs-store/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,9 @@
"@ew-did-registry/did-store-interface": "0.2.0",
"bl": "^4.0.2",
"ipfs-http-client": "^43.0.0"
},
"devDependencies": {
"run-with-testrpc": "0.3.1",
"ganache-cli": "6.9.1"
}
}
6 changes: 5 additions & 1 deletion packages/did-registry/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"types": "./dist/index.d.ts",
"scripts": {
"test": "../../node_modules/.bin/mocha -r ts-node/register test/**/*.test.ts",
"test-rpc": "../../node_modules/.bin/run-with-testrpc -m \"candy maple cake sugar pudding cream honey rich smooth crumble sweet treat\" --port 8544 --accounts 20 --networkId=9 --gasLimit=10000000 \"npm run test \"",
"test-rpc": "run-with-testrpc -m \"candy maple cake sugar pudding cream honey rich smooth crumble sweet treat\" --port 8544 --accounts 20 --networkId=9 --gasLimit=10000000 \"npm run test \"",
"compile": "../../node_modules/.bin/tsc",
"lint": "../../node_modules/.bin/eslint src/**/*.ts",
"fix": "../../node_modules/.bin/eslint src/**/*.ts --fix"
Expand Down Expand Up @@ -38,5 +38,9 @@
"@ew-did-registry/jwt": "0.2.0",
"@ew-did-registry/keys": "0.2.0",
"@ew-did-registry/proxyidentity": "0.2.0"
},
"devDependencies": {
"run-with-testrpc": "0.3.1",
"ganache-cli": "6.9.1"
}
}
4,359 changes: 3,404 additions & 955 deletions packages/proxyIdentity/build/contracts/IdentityManager.json

Large diffs are not rendered by default.

9,289 changes: 7,576 additions & 1,713 deletions packages/proxyIdentity/build/contracts/OfferableIdentity.json

Large diffs are not rendered by default.

18 changes: 13 additions & 5 deletions packages/proxyIdentity/contracts/IdentityManager.sol
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@ pragma solidity 0.8.0;


contract IdentityManager {
event IdentityCreated(address identity, address owner, uint at);
event IdentityOffered(address identity, address offeredTo, uint at);
event IdentityTransferred(address identity, address owner, uint at);
event OfferRejected(address identity, address offeredTo, uint at);
event IdentityCreated(address identity, address owner, uint256 at);
event IdentityOffered(address identity, address offeredTo, uint256 at);
event IdentityTransferred(address identity, address owner, uint256 at);
event IdentityOfferRejected(address identity, address offeredTo, uint256 at);
event IdentityOfferCanceled(address identity, address oferedto, uint256 at);

modifier isIdentity(address sender, address identity) {
require(
Expand Down Expand Up @@ -40,6 +41,13 @@ contract IdentityManager {
external
isIdentity(msg.sender, identity)
{
emit OfferRejected(identity, offeredTo, block.timestamp);
emit IdentityOfferRejected(identity, offeredTo, block.timestamp);
}

function identityOfferCanceled(address identity, address offeredTo)
external
isIdentity(msg.sender, identity)
{
emit IdentityOfferCanceled(identity, offeredTo, block.timestamp);
}
}
16 changes: 10 additions & 6 deletions packages/proxyIdentity/contracts/offerableIdentity.sol
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,13 @@ import "./IdentityManager.sol";

contract OfferableIdentity {
address public owner;
string public id;
address manager;

address public offeredTo;

event TransactionSent(bytes data, address to, uint256 value);

constructor(string memory _id, address _owner, address _manager) public {
id = _id;
constructor(address _owner, address _manager) {
owner = _owner;
manager = _manager;
IdentityManager(manager).identityCreated(address(this), owner);
Expand All @@ -35,16 +33,22 @@ contract OfferableIdentity {
IdentityManager(manager).identityOffered(address(this), offeredTo);
}

function accept() external isOfferedTo {
function acceptOffer() external isOfferedTo {
owner = offeredTo;
IdentityManager(manager).identityAccepted(address(this), offeredTo);
closeOffer();
}

function reject() external isOfferedTo {
function rejectOffer() external isOfferedTo {
IdentityManager(manager).identityRejected(address(this), offeredTo);
closeOffer();
}

function cancelOffer() external isOwner {
closeOffer();

IdentityManager(manager).identityOfferCanceled(address(this), offeredTo);
}

function closeOffer() internal {
offeredTo = address(0);
Expand All @@ -59,7 +63,7 @@ contract OfferableIdentity {
uint256 len = data.length;
// solium-disable-next-line security/no-inline-assembly
assembly {
success := call(gas, to, value, add(data, 0x20), len, 0, 0)
success := call(gas(), to, value, add(data, 0x20), len, 0, 0)
}
emit TransactionSent(_data, to, value);
}
Expand Down
10 changes: 6 additions & 4 deletions packages/proxyIdentity/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@
"description": "Implementation of contract representing ERC1056 identity",
"main": "dist/src/index.js",
"scripts": {
"compile": "tsc",
"compile": "../../node_modules/.bin/tsc",
"test-rpc": "run-with-testrpc -m \"candy maple cake sugar pudding cream honey rich smooth crumble sweet treat\" --port 8544 --accounts 20 --networkId=9 --gasLimit=10000000 \"npm run test-ganache\" ",
"test-ganache": "mocha -r ts-node/register test/*test.ts"
"test-ganache": "mocha -r ts-node/register test/*test.ts",
"compile:contracts": "../../node_modules/.bin/truffle compile"
},
"author": {
"name": "EnergyWeb",
Expand All @@ -25,10 +26,11 @@
"devDependencies": {
"@openzeppelin/contracts": "^2.5.0",
"multi-token-standard": "^0.8.13",
"run-with-testrpc": "0.3.1",
"chai": "4.2.0",
"chai-arrays": "2.2.0",
"chai-as-promised": "7.1.1",
"mocha": "7.1.0"
"mocha": "7.1.0",
"run-with-testrpc": "0.3.1",
"ganache-cli": "6.9.1"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ export function offerableIdentityOperatorTestSuite(this: Suite): void {
owner = withKey(withProvider(signerFromKeys(new Keys()), provider), walletPubKey);
ownerAddr = await owner.getAddress();

identity = await identityFactory.deploy(id, ownerAddr, manager.address);
identity = await identityFactory.deploy(ownerAddr, manager.address);
did = `did:${Methods.Erc1056}:${identity.address}`;

await provider.getSigner(0).sendTransaction({
Expand Down
27 changes: 18 additions & 9 deletions packages/proxyIdentity/test/offerable.identity.testSuite.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@ import chaiAsPromised from 'chai-as-promised';
chai.use(chaiAsPromised);
chai.should();

const emptyAddress = `0x${'0'.repeat(40)}`;

export function identityTestSuite(): void {
const id = '123';
let identity: Contract;
let identityFactory: ContractFactory;
let manager: Contract;
Expand All @@ -26,7 +27,7 @@ export function identityTestSuite(): void {

describe('[Identity Creation]', async () => {
before(async () => {
identity = await identityFactory.deploy(id, ownerAddr, manager.address);
identity = await identityFactory.deploy(ownerAddr, manager.address);
});

it('Identity should notify manager when created', async () => {
Expand Down Expand Up @@ -55,7 +56,7 @@ export function identityTestSuite(): void {
});

beforeEach(async () => {
identity = await identityFactory.deploy(id, ownerAddr, manager.address);
identity = await identityFactory.deploy(ownerAddr, manager.address);

await identity.connect(owner).functions.offer(receiverAddr);
});
Expand All @@ -79,21 +80,21 @@ export function identityTestSuite(): void {
});
});

await identity.connect(receiver).accept();
await identity.connect(receiver).acceptOffer();

expect(await event).to.deep.equal({ offered: identity.address, offeredTo: receiverAddr });
expect(await identity.owner()).equal(receiverAddr);
});

it('Rejecting offer should remain identity owner', async () => {
const event = new Promise((resolve) => {
manager.on('OfferRejected', (offered, offeredTo) => {
manager.removeAllListeners('OfferRejected');
manager.on('IdentityOfferRejected', (offered, offeredTo) => {
manager.removeAllListeners('IdentityOfferRejected');
resolve({ offered, offeredTo });
});
});

await identity.connect(receiver).reject();
await identity.connect(receiver).rejectOffer();

expect(await event).to.deep.equal({ offered: identity.address, offeredTo: receiverAddr });
expect(await identity.owner()).equal(ownerAddr);
Expand All @@ -102,13 +103,21 @@ export function identityTestSuite(): void {
it('Can\'t accept identity offered to other', async () => {
const nonReceiver = provider.getSigner(3);

return (identity.connect(nonReceiver).accept()).should.be.rejected;
return (identity.connect(nonReceiver).acceptOffer()).should.be.rejected;
});

it('Can\'t reject identity offered to other', async () => {
const nonReceiver = provider.getSigner(3);

return (identity.connect(nonReceiver).reject()).should.be.rejected;
return (identity.connect(nonReceiver).rejectOffer()).should.be.rejected;
});

it('Offer can be canceled', async () => {
expect(await identity.offeredTo()).equal(receiverAddr);

await identity.connect(owner).cancelOffer();

expect(await identity.offeredTo()).equal(emptyAddress);
});
});
}

0 comments on commit 6f5d78f

Please sign in to comment.