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
accept
and remove
in the AuthorizationRequest Entity
- Loading branch information
Showing
8 changed files
with
135 additions
and
7 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
import { AuthorizationRequest } from '~/api/entities'; | ||
import { Procedure } from '~/base'; | ||
import { authTargetToAuthIdentifier, numberToU64 } from '~/utils'; | ||
|
||
export interface ConsumeParams { | ||
accept: boolean; | ||
} | ||
|
||
export type ConsumeAuthorizationRequestsParams = ConsumeParams & { | ||
authRequests: AuthorizationRequest[]; | ||
}; | ||
|
||
const isLive = ({ expiry }: AuthorizationRequest): boolean => | ||
expiry === null || expiry > new Date(); | ||
|
||
/** | ||
* @hidden | ||
*/ | ||
export async function prepareConsumeAuthorizationRequests( | ||
this: Procedure<ConsumeAuthorizationRequestsParams>, | ||
args: ConsumeAuthorizationRequestsParams | ||
): Promise<void> { | ||
const { | ||
context: { | ||
polymeshApi: { tx }, | ||
}, | ||
context, | ||
} = this; | ||
const { accept, authRequests } = args; | ||
|
||
const liveRequests = authRequests.filter(isLive); | ||
|
||
if (accept) { | ||
const requestIds = liveRequests.map(({ authId }) => numberToU64(authId, context)); | ||
this.addTransaction(tx.identity.batchAcceptAuthorization, {}, requestIds); | ||
} else { | ||
const authIdentifiers = liveRequests.map(({ authId, targetDid }) => | ||
authTargetToAuthIdentifier({ authId, did: targetDid }, context) | ||
); | ||
this.addTransaction(tx.identity.batchRemoveAuthorization, {}, authIdentifiers); | ||
} | ||
} | ||
|
||
/** | ||
* @hidden | ||
*/ | ||
export function isAuthorized( | ||
this: Procedure<ConsumeAuthorizationRequestsParams>, | ||
{ authRequests, accept }: ConsumeAuthorizationRequestsParams | ||
): boolean { | ||
const { did } = this.context.getCurrentIdentity(); | ||
|
||
return authRequests.filter(isLive).every(({ targetDid, issuerDid }) => { | ||
let condition = did === targetDid; | ||
if (!accept) { | ||
condition = condition || did === issuerDid; | ||
} | ||
|
||
return condition; | ||
}); | ||
} | ||
|
||
export const consumeAuthorizationRequests = new Procedure( | ||
prepareConsumeAuthorizationRequests, | ||
isAuthorized | ||
); |
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