-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(@ew-did-registry/proxyidenity): send tx through identity
- Loading branch information
Showing
11 changed files
with
2,160 additions
and
828 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2,618 changes: 1,829 additions & 789 deletions
2,618
packages/proxyIdentity/build/contracts/OfferableIdentity.json
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
121 changes: 121 additions & 0 deletions
121
packages/proxyIdentity/src/offerableIdentityOperator.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |
Oops, something went wrong.