-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
69 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
import type { Claim, SignedClaim } from '../types'; | ||
import type { NodeIdEncoded } from '../../ids/types'; | ||
import * as ids from '../../ids'; | ||
import * as claimsUtils from '../utils'; | ||
import * as tokensUtils from '../../tokens/utils'; | ||
import * as validationErrors from '../../validation/errors'; | ||
import * as utils from '../../utils'; | ||
|
||
/** | ||
* Asserts that a node is apart of a network | ||
*/ | ||
interface ClaimNetworkNode extends Claim { | ||
typ: 'ClaimNetworkNode'; | ||
iss: NodeIdEncoded; | ||
sub: NodeIdEncoded; | ||
} | ||
|
||
|
||
function assertClaimNetworkNode( | ||
claimNetworkNode: unknown, | ||
): asserts claimNetworkNode is ClaimNetworkNode { | ||
if (!utils.isObject(claimNetworkNode)) { | ||
throw new validationErrors.ErrorParse('must be POJO'); | ||
} | ||
if (claimNetworkNode['typ'] !== 'ClaimNetworkNode') { | ||
throw new validationErrors.ErrorParse( | ||
'`typ` property must be `ClaimNetworkNode`', | ||
); | ||
} | ||
if ( | ||
claimNetworkNode['iss'] == null || | ||
ids.decodeNodeId(claimNetworkNode['iss']) == null | ||
) { | ||
throw new validationErrors.ErrorParse( | ||
'`iss` property must be an encoded node ID', | ||
); | ||
} | ||
if ( | ||
claimNetworkNode['sub'] == null || | ||
ids.decodeNodeId(claimNetworkNode['sub']) == null | ||
) { | ||
throw new validationErrors.ErrorParse( | ||
'`sub` property must be an encoded node ID', | ||
); | ||
} | ||
} | ||
|
||
function parseClaimNetworkNode(claimNetworkNodeEncoded: unknown): ClaimNetworkNode { | ||
const claimNetworkNode = claimsUtils.parseClaim(claimNetworkNodeEncoded); | ||
assertClaimNetworkNode(claimNetworkNode); | ||
return claimNetworkNode; | ||
} | ||
|
||
function parseSignedClaimNetworkNode( | ||
signedClaimNetworkNodeEncoded: unknown, | ||
): SignedClaim<ClaimNetworkNode> { | ||
const signedClaim = tokensUtils.parseSignedToken(signedClaimNetworkNodeEncoded); | ||
assertClaimNetworkNode(signedClaim.payload); | ||
return signedClaim as SignedClaim<ClaimNetworkNode>; | ||
} | ||
|
||
|
||
export { | ||
assertClaimNetworkNode, | ||
parseClaimNetworkNode, | ||
parseSignedClaimNetworkNode, | ||
}; | ||
|
||
export type { ClaimNetworkNode }; |