Files
self/sdk/core/src/utils/proof.ts
Nesopie 72a7f8177d Fix/sdk (#652)
* fix: sdk build configs

* chore: SelfBackendVerifier (WIP)

* feat: add custom verification

* feat: consider destination chain in user defined data

* chore: export attestation id

* chore: export attestation id

* chore: export config storage

* chore: don't throw an error if the proof is not valid

* chore: trim abi and rm typechain types

* refactor

* chore: rm unnecessary exports

* 📝 Add docstrings to `fix/sdk` (#653)

Docstrings generation was requested by @remicolin.

* https://github.com/selfxyz/self/pull/652#issuecomment-2992046545

The following files were modified:

* `sdk/core/src/utils/hash.ts`
* `sdk/core/src/utils/proof.ts`
* `sdk/core/src/utils/utils.ts`

Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>

* review fixes

* chore: fix package.json cjs types

* chore: add minor changes to checks

* feat: add InMemoryConfigStore, allIds constant and verificationResult type

* chore: export Verification config

* feat: change the verification config types

* fix: throw issues early if verification config is null

* fix: update yarn.lock file

* chore: lint

* fix: rm ts expect error directive

* fix: contract tests

* use excluded countries instead forbidden countries list

* chore: change types in constnats

---------

Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>
2025-06-23 14:25:36 +02:00

55 lines
1.9 KiB
TypeScript

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:
return 93 / 31;
case 2:
return Math.ceil(94 / 31);
default:
throw new Error(`Invalid attestation ID: ${attestationId}`);
}
}
export const bytesCount: Record<AttestationId, number[]> = {
1: [31, 31, 31],
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++) {
let publicSignal = BigInt(
publicSignals[discloseIndices[attestationId].revealedDataPackedIndex + i]
);
for (let j = 0; j < bytesCount[attestationId][i]; j++) {
bytes.push(Number(publicSignal & 0xffn));
publicSignal = publicSignal >> 8n;
}
}
return bytes;
}