Skip to content

Commit

Permalink
wip: claimNetworkNode payload
Browse files Browse the repository at this point in the history
  • Loading branch information
amydevs committed Jul 22, 2024
1 parent fcaf4ce commit 40cfc83
Showing 1 changed file with 69 additions and 0 deletions.
69 changes: 69 additions & 0 deletions src/claims/payloads/claimNetworkNode.ts
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 };

0 comments on commit 40cfc83

Please sign in to comment.