Skip to content

Commit

Permalink
feat(@ew-did-registry/proxyidenity): send tx through identity
Browse files Browse the repository at this point in the history
  • Loading branch information
JGiter committed Mar 12, 2021
1 parent ff41f27 commit cb631cf
Show file tree
Hide file tree
Showing 11 changed files with 2,160 additions and 828 deletions.
1 change: 0 additions & 1 deletion packages/did-ethr-resolver/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
"@ew-did-registry/did": "0.1.0",
"@ew-did-registry/did-resolver-interface": "0.1.0",
"@ew-did-registry/keys": "0.1.0",
"@ew-did-registry/proxyidentity": "0.1.0",
"ethers": "4.0.45"
}
}
4 changes: 2 additions & 2 deletions packages/did-ethr-resolver/src/implementations/operator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ export class Operator extends Resolver implements IOperator {

private address: string;

private readonly _owner: IdentityOwner;
protected readonly _owner: IdentityOwner;

/**
* @param { IdentityOwner } owner - entity which controls document updatable by this operator
Expand All @@ -60,7 +60,7 @@ export class Operator extends Resolver implements IOperator {
this._keys.publicKey = owner.publicKey;
}

private async getAddress(): Promise<string> {
protected async getAddress(): Promise<string> {
return this.address || (this.address = await this._owner.getAddress());
}

Expand Down
30 changes: 15 additions & 15 deletions packages/proxyIdentity/build/contracts/IdentityManager.json
Original file line number Diff line number Diff line change
Expand Up @@ -572,10 +572,10 @@
"name": "require",
"nodeType": "Identifier",
"overloadedDeclarations": [
282,
283
304,
305
],
"referencedDeclaration": 283,
"referencedDeclaration": 305,
"src": "358:7:0",
"typeDescriptions": {
"typeIdentifier": "t_function_require_pure$_t_bool_$_t_string_memory_ptr_$returns$__$",
Expand Down Expand Up @@ -769,7 +769,7 @@
"name": "msg",
"nodeType": "Identifier",
"overloadedDeclarations": [],
"referencedDeclaration": 279,
"referencedDeclaration": 301,
"src": "554:3:0",
"typeDescriptions": {
"typeIdentifier": "t_magic_message",
Expand Down Expand Up @@ -991,7 +991,7 @@
"name": "msg",
"nodeType": "Identifier",
"overloadedDeclarations": [],
"referencedDeclaration": 279,
"referencedDeclaration": 301,
"src": "720:3:0",
"typeDescriptions": {
"typeIdentifier": "t_magic_message",
Expand Down Expand Up @@ -1213,7 +1213,7 @@
"name": "msg",
"nodeType": "Identifier",
"overloadedDeclarations": [],
"referencedDeclaration": 279,
"referencedDeclaration": 301,
"src": "887:3:0",
"typeDescriptions": {
"typeIdentifier": "t_magic_message",
Expand Down Expand Up @@ -1435,7 +1435,7 @@
"name": "msg",
"nodeType": "Identifier",
"overloadedDeclarations": [],
"referencedDeclaration": 279,
"referencedDeclaration": 301,
"src": "1058:3:0",
"typeDescriptions": {
"typeIdentifier": "t_magic_message",
Expand Down Expand Up @@ -1977,10 +1977,10 @@
"name": "require",
"nodeType": "Identifier",
"overloadedDeclarations": [
282,
283
304,
305
],
"referencedDeclaration": 283,
"referencedDeclaration": 305,
"src": "358:7:0",
"typeDescriptions": {
"typeIdentifier": "t_function_require_pure$_t_bool_$_t_string_memory_ptr_$returns$__$",
Expand Down Expand Up @@ -2174,7 +2174,7 @@
"name": "msg",
"nodeType": "Identifier",
"overloadedDeclarations": [],
"referencedDeclaration": 279,
"referencedDeclaration": 301,
"src": "554:3:0",
"typeDescriptions": {
"typeIdentifier": "t_magic_message",
Expand Down Expand Up @@ -2396,7 +2396,7 @@
"name": "msg",
"nodeType": "Identifier",
"overloadedDeclarations": [],
"referencedDeclaration": 279,
"referencedDeclaration": 301,
"src": "720:3:0",
"typeDescriptions": {
"typeIdentifier": "t_magic_message",
Expand Down Expand Up @@ -2618,7 +2618,7 @@
"name": "msg",
"nodeType": "Identifier",
"overloadedDeclarations": [],
"referencedDeclaration": 279,
"referencedDeclaration": 301,
"src": "887:3:0",
"typeDescriptions": {
"typeIdentifier": "t_magic_message",
Expand Down Expand Up @@ -2840,7 +2840,7 @@
"name": "msg",
"nodeType": "Identifier",
"overloadedDeclarations": [],
"referencedDeclaration": 279,
"referencedDeclaration": 301,
"src": "1058:3:0",
"typeDescriptions": {
"typeIdentifier": "t_magic_message",
Expand Down Expand Up @@ -2981,7 +2981,7 @@
},
"networks": {},
"schemaVersion": "3.0.16",
"updatedAt": "2021-03-11T08:41:52.638Z",
"updatedAt": "2021-03-11T15:47:13.996Z",
"devdoc": {
"methods": {}
},
Expand Down
2,618 changes: 1,829 additions & 789 deletions packages/proxyIdentity/build/contracts/OfferableIdentity.json

Large diffs are not rendered by default.

15 changes: 15 additions & 0 deletions packages/proxyIdentity/contracts/offerableIdentity.sol
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ contract OfferableIdentity {

address public offeredTo;

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

constructor(string memory _id, address _owner, address _manager) public {
id = _id;
owner = _owner;
Expand Down Expand Up @@ -47,4 +49,17 @@ contract OfferableIdentity {
function closeOffer() internal {
offeredTo = address(0);
}

function sendTransaction(bytes memory _data, address to, uint256 value)
public
returns (bool success)
{
bytes memory data = _data;
uint256 len = data.length;
// solium-disable-next-line security/no-inline-assembly
assembly {
success := call(gas, to, value, add(data, 0x20), len, 0, 0)
}
emit TransactionSent(_data, to, value);
}
}
5 changes: 4 additions & 1 deletion packages/proxyIdentity/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,10 @@
},
"license": "ISC",
"dependencies": {
"ethers": "4.0.45"
"ethers": "4.0.45",
"@ew-did-registry/did-resolver-interface": "0.1.0",
"@ew-did-registry/did-ethr-resolver": "0.1.0",
"@ew-did-registry/keys": "0.1.0"
},
"devDependencies": {
"@openzeppelin/contracts": "^2.5.0",
Expand Down
1 change: 1 addition & 0 deletions packages/proxyIdentity/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ import * as proxyBuild from '../build/contracts/OfferableIdentity.json';
import * as erc1056Build from '../constants/ERC1056.json';

export { proxyBuild, erc1056Build };
export { OfferableIdenitytOperator as ProxyOperator } from './offerableIdentityOperator';
121 changes: 121 additions & 0 deletions packages/proxyIdentity/src/offerableIdentityOperator.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
import {
Contract, Event, utils, providers,
} from 'ethers';
import {
DIDAttribute,
IUpdateData,
RegistrySettings,
IdentityOwner,
} from '@ew-did-registry/did-resolver-interface';
import { Operator } from '@ew-did-registry/did-ethr-resolver';
import { abi as identityAbi } from '../build/contracts/OfferableIdentity.json';
import { abi as erc1056Abi } from '../constants/ERC1056.json';

const { BigNumber, Interface, formatBytes32String } = utils;
const { PublicKey, ServicePoint } = DIDAttribute;

export class OfferableIdenitytOperator extends Operator {
private identity: Contract;

/**
*
* @param owner - Owner of the idenity
* @param settings - Settings to connect to Ethr registry
* @param identityAddr {string} - address of controlled offerable identity
*/
constructor(owner: IdentityOwner, settings: RegistrySettings, identityAddr: string) {
super(owner, settings);
this.identity = new Contract(identityAddr, identityAbi, owner);
}

async revokeDelegate(): Promise<boolean> {
throw new Error('Not supported');
}

/* eslint-disable @typescript-eslint/no-unused-vars */
async changeOwner(
identityDID: string,
newOwnerDid: string,
): Promise<boolean> {
throw new Error('Not supported');
}
/* eslint-enable @typescript-eslint/no-unused-vars */


/**
* @todo
*/
async deactivate(): Promise<void> {
throw new Error('Not supported yet');
}

protected async getAddress(): Promise<string> {
return this.identity.address;
}

async offer(offerTo: string): Promise<boolean> {
await this.identity.offer(
new Interface(identityAbi).functions.offer.encode([offerTo]),
this.settings.address,
0,
);
return true;
}

async revokeAttribute(
identityDID: string,
attributeType: DIDAttribute,
updateData: IUpdateData,
): Promise<boolean> {
const [, , identityAddress] = identityDID.split(':');
const attribute = this._composeAttributeName(attributeType, updateData);
const bytesType = formatBytes32String(attribute);
const bytesValue = this._hexify(updateData.value);
const params = [identityAddress, bytesType, bytesValue];

try {
const data = new Interface(erc1056Abi).functions.revokeAttribute.encode(params);
await this.identity
.sendTransaction(data, this.settings.address, 0)
.then((tx: providers.TransactionResponse) => tx.wait());
} catch (error) {
throw new Error(error);
}
return true;
}

protected async _sendTransaction(
method: Function,
did: string,
didAttribute: DIDAttribute,
updateData: IUpdateData,
validity?: number,
): Promise<utils.BigNumber> {
const identity = this._parseDid(did);
const attributeName = this._composeAttributeName(didAttribute, updateData);
const bytesOfAttribute = formatBytes32String(attributeName);
const bytesOfValue = this._hexify(
didAttribute === PublicKey || didAttribute === ServicePoint
? updateData.value
: updateData.delegate,
);
const validityValue = validity !== undefined ? validity.toString() : '';
const params = [identity, bytesOfAttribute, bytesOfValue, validityValue];
let methodName: string;
if (didAttribute === DIDAttribute.PublicKey || didAttribute === DIDAttribute.ServicePoint) {
methodName = 'setAttribute';
} else {
methodName = 'addDelegate';
}
const data = new Interface(erc1056Abi).functions[methodName].encode(params);
let event: Event;
try {
const tx = await this.identity.sendTransaction(data, this.settings.address, 0);
const receipt = await tx.wait();
event = receipt.events.find((e: Event) => e.event === 'TransactionSent');
return new BigNumber(event.blockNumber);
} catch (e) {
throw new Error(e);
}
}
}
Loading

0 comments on commit cb631cf

Please sign in to comment.