Repository navigation
Expand file tree
/
Copy pathcommon.ts
More file actions
55 lines (50 loc) · 1.6 KB
/
Copy pathcommon.ts
File metadata and controls
55 lines (50 loc) · 1.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import { DID, Id, IdPosition, MerklizedRootPosition } from '@iden3/js-iden3-core';
import { SchemaMetadata } from '../schema-processor';
import { SubjectPosition } from '../verifiable';
/**
* Determines subject position
*
* @param {IdPosition} idPosition - index / none / value
* @returns {SubjectPosition}
*/
export const subjectPositionIndex = (idPosition: IdPosition): SubjectPosition => {
switch (idPosition) {
case IdPosition.Index:
return SubjectPosition.Index;
case IdPosition.Value:
return SubjectPosition.Value;
default:
return SubjectPosition.None;
}
};
/**
* Returns merklized root position based on schema serialization metadata and expected position
*
* @param {SchemaMetadata} [metadata] - schema metadata
* @param {MerklizedRootPosition} [position] - expected mt root position
* @returns {MerklizedRootPosition}
*/
export const defineMerklizedRootPosition = (
metadata?: SchemaMetadata,
position?: MerklizedRootPosition
): MerklizedRootPosition => {
if (!metadata?.serialization) {
return MerklizedRootPosition.None;
}
if (position != null && position !== MerklizedRootPosition.None) {
return position;
}
return MerklizedRootPosition.Index;
};
/**
* Returns profile DID based on did and profile nonce
*
* @param {DID} [did] - did from which profile will be derived
* @param {number} [profileNonce] - profile nonce
* @returns {DID}
*/
export const generateProfileDID = (did: DID, profileNonce?: number): DID => {
const id = DID.idFromDID(did);
const profile = Id.profileId(id, BigInt(profileNonce ?? 0));
return DID.parseFromId(profile);
};