generated from PolymeshAssociation/typescript-boilerplate
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: implement
authorizations.getReceived
in the Security Token
Also updated `getTickerReservations` to the latest polkadot changes
- Loading branch information
Showing
11 changed files
with
213 additions
and
42 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
import BigNumber from 'bignumber.js'; | ||
|
||
import { Entity } from '~/base'; | ||
import { Context } from '~/context'; | ||
import { Authorization } from '~/types'; | ||
|
||
export interface UniqueIdentifiers { | ||
authId: BigNumber; | ||
} | ||
|
||
export interface Params { | ||
targetDid: string; | ||
issuerDid: string; | ||
expiry: Date | null; | ||
data: Authorization; | ||
} | ||
|
||
/** | ||
* Represents a request made by an identity to another identity for some sort of authorization. This has multiple uses. For example, if Alice | ||
* wants to transfer ownership of her asset ALICETOKEN to Bob, an authorization request gets emitted to Bob, | ||
* who then has to accept it in order for the ownership transfer to be complete | ||
*/ | ||
export class AuthorizationRequest extends Entity<UniqueIdentifiers> { | ||
/** | ||
* @hidden | ||
* Check if a value is of type [[UniqueIdentifiers]] | ||
*/ | ||
public static isUniqueIdentifiers(identifier: unknown): identifier is UniqueIdentifiers { | ||
const { authId } = identifier as UniqueIdentifiers; | ||
|
||
return authId instanceof BigNumber; | ||
} | ||
|
||
/** | ||
* ID of the identity to which the request was emitted | ||
*/ | ||
public targetDid: string; | ||
|
||
/** | ||
* ID of the identity that emitted the request | ||
*/ | ||
public issuerDid: string; | ||
|
||
/** | ||
* authorization request data corresponding to type of authorization | ||
* | ||
* | Type | Data | | ||
* |----------------------------|--------| | ||
* | Attest Master Key Rotation | DID | | ||
* | Rotate Master Key | DID | | ||
* | Transfer Ticker | Ticker | | ||
* | Add MultiSig Signer | N/A | | ||
* | Transfer Token Ownership | Ticker | | ||
* | Join Identity | DID | | ||
* | Custom | Custom | | ||
* | No Data | N/A | | ||
*/ | ||
public data: Authorization; | ||
|
||
/** | ||
* date at which the authorization request expires and can no longer be accepted. | ||
* At this point, a new authorization request must be emitted. Null if the request never expires | ||
*/ | ||
public expiry: Date | null; | ||
|
||
/** | ||
* @hidden | ||
* internal identifier for the request (used to accept/reject/cancel) | ||
*/ | ||
private authId: BigNumber; | ||
|
||
/** | ||
* @hidden | ||
*/ | ||
public constructor(args: UniqueIdentifiers & Params, context: Context) { | ||
const { targetDid, issuerDid, expiry, data, ...identifiers } = args; | ||
|
||
super(identifiers, context); | ||
|
||
const { authId } = identifiers; | ||
|
||
this.targetDid = targetDid; | ||
this.issuerDid = issuerDid; | ||
this.authId = authId; | ||
this.expiry = expiry; | ||
this.data = data; | ||
} | ||
} |
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,47 @@ | ||
import { AuthorizationRequest } from '~/api/entities/AuthorizationRequest'; | ||
import { Namespace } from '~/base'; | ||
import { SignerType } from '~/types/internal'; | ||
import { | ||
authorizationDataToAuthorization, | ||
momentToDate, | ||
signatoryToSigner, | ||
signerToSignatory, | ||
u64ToBigNumber, | ||
} from '~/utils'; | ||
|
||
import { Identity } from './'; | ||
|
||
/** | ||
* Handles all Identity Authorization related functionality | ||
*/ | ||
export class Authorizations extends Namespace<Identity> { | ||
/** | ||
* Fetch all authorization requests for which this identity is the target | ||
*/ | ||
public async getReceived(): Promise<AuthorizationRequest[]> { | ||
const { | ||
context: { polymeshApi }, | ||
context, | ||
parent: { did }, | ||
} = this; | ||
|
||
const entries = await polymeshApi.query.identity.authorizations.entries( | ||
signerToSignatory({ type: SignerType.Identity, value: did }, context) | ||
); | ||
|
||
return entries.map(([, auth]) => { | ||
const { expiry, auth_id: authId, authorization_data: data, authorized_by: issuerDid } = auth; | ||
|
||
return new AuthorizationRequest( | ||
{ | ||
authId: u64ToBigNumber(authId), | ||
expiry: expiry.isSome ? momentToDate(expiry.unwrap()) : null, | ||
data: authorizationDataToAuthorization(data), | ||
targetDid: did, | ||
issuerDid: signatoryToSigner(issuerDid).value, | ||
}, | ||
context | ||
); | ||
}); | ||
} | ||
} |
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
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
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