Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions sdk/core/src/utils/hash.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
import { createHash } from "crypto";

/**
* Generates a deterministic user identifier hash from the provided context data.
*
* The function computes a SHA-256 hash of the input buffer, then applies a RIPEMD-160 hash to the result. The final output is a hexadecimal string, left-padded with zeros to 40 characters and prefixed with "0x".
*
* @param userContextData - The buffer containing user context data to hash
* @returns A 40-character hexadecimal user identifier string prefixed with "0x"
*/
export function calculateUserIdentifierHash(userContextData: Buffer): string {
const sha256Hash = createHash("sha256")
.update(userContextData)
Expand Down
17 changes: 17 additions & 0 deletions sdk/core/src/utils/proof.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,14 @@ import { PublicSignals } from 'snarkjs';
import { discloseIndices } from './constants.js';
import { AttestationId } from 'src/types/types.js';

/**
* Returns the number of public signals containing revealed data for the specified attestation ID.
*
* Throws an error if the attestation ID is not supported.
*
* @param attestationId - The attestation ID for which to determine the number of revealed data public signals
* @returns The number of public signals corresponding to revealed data
*/
export function getRevealedDataPublicSignalsLength(attestationId: AttestationId): number {
switch (attestationId) {
case 1:
Expand All @@ -18,6 +26,15 @@ export const bytesCount: Record<AttestationId, number[]> = {
2: [31, 31, 31, 1],
};

/**
* Extracts and returns the revealed data bytes from the public signals for a given attestation ID.
*
* Iterates over the relevant public signals, unpacks each into its constituent bytes according to the attestation's byte structure, and accumulates all revealed bytes into a single array.
*
* @param attestationId - The attestation ID specifying the format of revealed data
* @param publicSignals - The array of public signals containing packed revealed data
* @returns An array of bytes representing the revealed data for the specified attestation
*/
export function getRevealedDataBytes(attestationId: AttestationId, publicSignals: PublicSignals): number[] {
let bytes: number[] = [];
for (let i = 0; i < getRevealedDataPublicSignalsLength(attestationId); i++) {
Expand Down
6 changes: 6 additions & 0 deletions sdk/core/src/utils/utils.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
import { unpackReveal } from "@selfxyz/common/utils/circuits/formatOutputs";

/**
* Unpacks a list of packed forbidden country codes into an array of 3-character country codes.
*
* @param forbiddenCountriesList_packed - An array of packed strings representing forbidden countries.
* @returns An array of 3-character country codes extracted from the packed input.
*/
export function unpackForbiddenCountriesList(forbiddenCountriesList_packed: string[]) {
const trimmed = unpackReveal(forbiddenCountriesList_packed, 'id');
const countries = [];
Expand Down
Loading