From 8ae1a5d5c32f2cc2eadef24f04e6a84e418bb077 Mon Sep 17 00:00:00 2001 From: Fedor Indutny <79877362+indutny-signal@users.noreply.github.com> Date: Thu, 3 Oct 2024 16:40:00 -0700 Subject: [PATCH] backup: export getBackupId/getBackupLevel for node --- node/Native.d.ts | 2 ++ node/ts/test/ZKGroup-test.ts | 6 ++++++ .../backups/BackupAuthCredentialPresentation.ts | 15 +++++++++++++++ rust/bridge/shared/src/zkgroup.rs | 4 ++-- 4 files changed, 25 insertions(+), 2 deletions(-) diff --git a/node/Native.d.ts b/node/Native.d.ts index ecfe10262d..4db316b15a 100644 --- a/node/Native.d.ts +++ b/node/Native.d.ts @@ -153,6 +153,8 @@ export function AuthCredentialPresentation_GetUuidCiphertext(presentationBytes: export function AuthCredentialWithPniResponse_CheckValidContents(bytes: Buffer): void; export function AuthCredentialWithPni_CheckValidContents(bytes: Buffer): void; export function BackupAuthCredentialPresentation_CheckValidContents(presentationBytes: Buffer): void; +export function BackupAuthCredentialPresentation_GetBackupId(presentationBytes: Buffer): Buffer; +export function BackupAuthCredentialPresentation_GetBackupLevel(presentationBytes: Buffer): number; export function BackupAuthCredentialPresentation_Verify(presentationBytes: Buffer, now: Timestamp, serverParamsBytes: Buffer): void; export function BackupAuthCredentialRequestContext_CheckValidContents(contextBytes: Buffer): void; export function BackupAuthCredentialRequestContext_GetRequest(contextBytes: Buffer): Buffer; diff --git a/node/ts/test/ZKGroup-test.ts b/node/ts/test/ZKGroup-test.ts index a38bb07076..5b894dbdd0 100644 --- a/node/ts/test/ZKGroup-test.ts +++ b/node/ts/test/ZKGroup-test.ts @@ -749,6 +749,12 @@ describe('ZKGroup', () => { ); assert.equal(backupLevel, credential.getBackupLevel()); assertArrayEquals(SERIALIZED_BACKUP_ID, credential.getBackupId()); + + const presentation = credential.present( + serverSecretParams.getPublicParams() + ); + assert.equal(backupLevel, presentation.getBackupLevel()); + assertArrayEquals(SERIALIZED_BACKUP_ID, presentation.getBackupId()); }); it('testIntegration', () => { diff --git a/node/ts/zkgroup/backups/BackupAuthCredentialPresentation.ts b/node/ts/zkgroup/backups/BackupAuthCredentialPresentation.ts index bba801f68b..883cf72633 100644 --- a/node/ts/zkgroup/backups/BackupAuthCredentialPresentation.ts +++ b/node/ts/zkgroup/backups/BackupAuthCredentialPresentation.ts @@ -7,6 +7,7 @@ import ByteArray from '../internal/ByteArray'; import * as Native from '../../../Native'; import GenericServerSecretParams from '../GenericServerSecretParams'; +import BackupLevel from './BackupLevel'; export default class BackupAuthCredentialPresentation extends ByteArray { private readonly __type?: never; @@ -25,4 +26,18 @@ export default class BackupAuthCredentialPresentation extends ByteArray { serverParams.contents ); } + + getBackupId(): Buffer { + return Native.BackupAuthCredentialPresentation_GetBackupId(this.contents); + } + + getBackupLevel(): BackupLevel { + const n: number = Native.BackupAuthCredentialPresentation_GetBackupLevel( + this.contents + ); + if (!(n in BackupLevel)) { + throw new TypeError(`Invalid BackupLevel ${n}`); + } + return n; + } } diff --git a/rust/bridge/shared/src/zkgroup.rs b/rust/bridge/shared/src/zkgroup.rs index 6a44f5d374..1cbe04f530 100644 --- a/rust/bridge/shared/src/zkgroup.rs +++ b/rust/bridge/shared/src/zkgroup.rs @@ -1024,14 +1024,14 @@ fn BackupAuthCredentialPresentation_Verify( presentation.verify(now, &server_params) } -#[bridge_fn(ffi = false, node = false)] +#[bridge_fn(ffi = false)] fn BackupAuthCredentialPresentation_GetBackupId(presentation_bytes: &[u8]) -> [u8; 16] { let presentation = bincode::deserialize::(presentation_bytes) .expect("should have been parsed previously"); presentation.backup_id() } -#[bridge_fn(ffi = false, node = false)] +#[bridge_fn(ffi = false)] fn BackupAuthCredentialPresentation_GetBackupLevel(presentation_bytes: &[u8]) -> u8 { let presentation = bincode::deserialize::(presentation_bytes) .expect("should have been parsed previously");