forked from OKEAMAH/js-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.ts
More file actions
84 lines (76 loc) · 2.37 KB
/
utils.ts
File metadata and controls
84 lines (76 loc) · 2.37 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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
import { BytesHelper, checkBigIntInField, SchemaHash } from '@iden3/js-iden3-core';
import { Merklizer } from '@iden3/js-jsonld-merklization';
import { calculateCoreSchemaHash, fillCoreClaimSlot } from '../verifiable';
/**
* SwapEndianness swaps the endianness of the value encoded in buf. If buf is
* Big-Endian, the result will be Little-Endian and vice-versa.
*
* @param {Uint8Array} buf - bytes to swap
* @returns Uint8Array - swapped bytes
*/
export const swapEndianness = (buf: Uint8Array): Uint8Array => buf.reverse();
/**
* FieldToByteArray convert fields to byte representation based on type
*
* @param {unknown} field - field to convert
* @returns Uint8Array
*/
export function fieldToByteArray(field: unknown): Uint8Array {
let bigIntField: bigint;
if (typeof field === 'string') {
bigIntField = BigInt(field);
} else if (typeof field === 'number') {
bigIntField = BigInt(Math.trunc(field));
} else {
throw new Error('field type is not supported');
}
return BytesHelper.intToBytes(bigIntField);
}
/**
* checks if data fills into slot capacity ()
*
* @param {Uint8Array} slot - current slot data
* @param {Uint8Array} newData - new slot data
* @returns boolean
*/
export function dataFillsSlot(slot: Uint8Array, newData: Uint8Array): boolean {
return checkBigIntInField(BytesHelper.bytesToInt(Uint8Array.from([...slot, ...newData])));
}
/**
* check if byte data is in Q field
*
* @param {Uint8Array} data - bytes payload
* @returns boolean
*/
export function checkDataInField(data: Uint8Array): boolean {
return checkBigIntInField(BytesHelper.bytesToInt(data));
}
/**
*
* @deprecated The method should not be used. Use calculateCoreSchemaHash from verifiable.
* Calculates schema hash
*
* @param {Uint8Array} schemaId
* @returns {*} {SchemaHash}
*/
export const createSchemaHash = (schemaId: Uint8Array): SchemaHash => {
return calculateCoreSchemaHash(schemaId);
};
/**
*
* @deprecated The method should not be used. Use fillCoreClaimSlot from verifiable.
* checks if data can fill the slot
*
* @param {Uint8Array} slotData - slot data
* @param {Merklizer} mz - merklizer
* @param {string} path - path
* @returns {void}
*/
export const fillSlot = async (
slotData: Uint8Array,
mz: Merklizer,
path: string
): Promise<void> => {
return fillCoreClaimSlot(slotData, mz, path);
};
export const credentialSubjectKey = 'credentialSubject';