From 1a35832f1c4b2bce07e35e4fb8ce95c0a1e0371e Mon Sep 17 00:00:00 2001 From: Isabelle Wei Date: Thu, 27 Oct 2022 20:22:38 -0400 Subject: [PATCH] update tests --- .../sdk/identity/src/odis/identifier.test.ts | 147 ++++++++++++++++++ .../src/odis/phone-number-identifier.test.ts | 31 ++-- 2 files changed, 160 insertions(+), 18 deletions(-) create mode 100644 packages/sdk/identity/src/odis/identifier.test.ts diff --git a/packages/sdk/identity/src/odis/identifier.test.ts b/packages/sdk/identity/src/odis/identifier.test.ts new file mode 100644 index 00000000000..79baa8e43ee --- /dev/null +++ b/packages/sdk/identity/src/odis/identifier.test.ts @@ -0,0 +1,147 @@ +import { WasmBlsBlindingClient } from './bls-blinding-client' +import { + getBlindedIdentifier, + getBlindedIdentifierSignature, + getOnchainIdentifier, + getOnchainIdentifierFromSignature, + getPepperFromThresholdSignature, + IdentifierType, +} from './identifier' +import { AuthenticationMethod, EncryptionKeySigner, ErrorMessages, ServiceContext } from './query' + +jest.mock('./bls-blinding-client', () => { + // tslint:disable-next-line:no-shadowed-variable + class WasmBlsBlindingClient { + blindMessage = (m: string) => m + unblindAndVerifyMessage = (m: string) => m + } + return { + WasmBlsBlindingClient, + } +}) + +const mockOffchainIdentifier = '+14155550000' +const mockAccount = '0x0000000000000000000000000000000000007E57' +const expectedIdentifierHash = '0xf3ddadd1f488cdd42b9fa10354fdcae67c303ce182e71b30855733b50dce8301' +const expectedPepper = 'nHIvMC9B4j2+H' + +const serviceContext: ServiceContext = { + odisUrl: 'https://mockodis.com', + odisPubKey: + '7FsWGsFnmVvRfMDpzz95Np76wf/1sPaK0Og9yiB+P8QbjiC8FV67NBans9hzZEkBaQMhiapzgMR6CkZIZPvgwQboAxl65JWRZecGe5V3XO4sdKeNemdAZ2TzQuWkuZoA', +} +const endpoint = serviceContext.odisUrl + '/getBlindedMessageSig' +const rawKey = '41e8e8593108eeedcbded883b8af34d2f028710355c57f4c10a056b72486aa04' + +const authSigner: EncryptionKeySigner = { + authenticationMethod: AuthenticationMethod.ENCRYPTION_KEY, + rawKey, +} + +describe(getOnchainIdentifier, () => { + afterEach(() => { + fetchMock.reset() + }) + + describe('Retrieves a pepper correctly', () => { + it('Using EncryptionKeySigner', async () => { + fetchMock.mock(endpoint, { + success: true, + combinedSignature: '0Uj+qoAu7ASMVvm6hvcUGx2eO/cmNdyEgGn0mSoZH8/dujrC1++SZ1N6IP6v2I8A', + }) + + const blsBlindingClient = new WasmBlsBlindingClient(serviceContext.odisPubKey) + const base64BlindedMessage = await getBlindedIdentifier( + mockOffchainIdentifier, + blsBlindingClient + ) + const base64BlindSig = await getBlindedIdentifierSignature( + mockAccount, + authSigner, + serviceContext, + base64BlindedMessage + ) + const base64UnblindedSig = await blsBlindingClient.unblindAndVerifyMessage(base64BlindSig) + + await expect( + getOnchainIdentifier( + mockOffchainIdentifier, + IdentifierType.PHONE_NUMBER, + mockAccount, + authSigner, + serviceContext + ) + ).resolves.toMatchObject({ + offchainIdentifier: mockOffchainIdentifier, + pepper: expectedPepper, + identifierHash: expectedIdentifierHash, + unblindedSignature: base64UnblindedSig, + }) + }) + + it('Preblinding the of-chain identifier', async () => { + fetchMock.mock(endpoint, { + success: true, + combinedSignature: '0Uj+qoAu7ASMVvm6hvcUGx2eO/cmNdyEgGn0mSoZH8/dujrC1++SZ1N6IP6v2I8A', + }) + + const blsBlindingClient = new WasmBlsBlindingClient(serviceContext.odisPubKey) + const base64BlindedMessage = await getBlindedIdentifier( + mockOffchainIdentifier, + blsBlindingClient + ) + + const base64BlindSig = await getBlindedIdentifierSignature( + mockAccount, + authSigner, + serviceContext, + base64BlindedMessage + ) + + const identifierHashDetails = await getOnchainIdentifierFromSignature( + mockOffchainIdentifier, + IdentifierType.PHONE_NUMBER, + base64BlindSig, + blsBlindingClient + ) + + expect(identifierHashDetails.identifierHash).toEqual(expectedIdentifierHash) + expect(identifierHashDetails.pepper).toEqual(expectedPepper) + }) + }) + + it('Throws quota error', async () => { + fetchMock.mock(endpoint, 403) + + await expect( + getOnchainIdentifier( + mockOffchainIdentifier, + IdentifierType.PHONE_NUMBER, + mockAccount, + authSigner, + serviceContext + ) + ).rejects.toThrow(ErrorMessages.ODIS_QUOTA_ERROR) + }) + + it('Throws auth error', async () => { + fetchMock.mock(endpoint, 401) + await expect( + getOnchainIdentifier( + mockOffchainIdentifier, + IdentifierType.PHONE_NUMBER, + mockAccount, + authSigner, + serviceContext + ) + ).rejects.toThrow(ErrorMessages.ODIS_AUTH_ERROR) + }) +}) + +describe(getPepperFromThresholdSignature, () => { + it('Hashes sigs correctly', () => { + const base64Sig = 'vJeFZJ3MY5KlpI9+kIIozKkZSR4cMymLPh2GHZUatWIiiLILyOcTiw2uqK/LBReA' + const signature = Buffer.from(base64Sig, 'base64') + expect(getPepperFromThresholdSignature(signature)).toBe('piWqRHHYWtfg9') + }) +}) diff --git a/packages/sdk/identity/src/odis/phone-number-identifier.test.ts b/packages/sdk/identity/src/odis/phone-number-identifier.test.ts index b8734fe5926..cf697bb465c 100644 --- a/packages/sdk/identity/src/odis/phone-number-identifier.test.ts +++ b/packages/sdk/identity/src/odis/phone-number-identifier.test.ts @@ -1,10 +1,12 @@ import { WasmBlsBlindingClient } from './bls-blinding-client' import { - getBlindedPhoneNumber, - getBlindedPhoneNumberSignature, - getPepperFromThresholdSignature, + getBlindedIdentifier, + getBlindedIdentifierSignature, + getOnchainIdentifierFromSignature, + IdentifierType, +} from './identifier' +import { getPhoneNumberIdentifier, - getPhoneNumberIdentifierFromSignature, isBalanceSufficientForSigRetrieval, } from './phone-number-identifier' import { AuthenticationMethod, EncryptionKeySigner, ErrorMessages, ServiceContext } from './query' @@ -59,8 +61,8 @@ describe(getPhoneNumberIdentifier, () => { }) const blsBlindingClient = new WasmBlsBlindingClient(serviceContext.odisPubKey) - const base64BlindedMessage = await getBlindedPhoneNumber(mockE164Number, blsBlindingClient) - const base64BlindSig = await getBlindedPhoneNumberSignature( + const base64BlindedMessage = await getBlindedIdentifier(mockE164Number, blsBlindingClient) + const base64BlindSig = await getBlindedIdentifierSignature( mockAccount, authSigner, serviceContext, @@ -85,22 +87,23 @@ describe(getPhoneNumberIdentifier, () => { }) const blsBlindingClient = new WasmBlsBlindingClient(serviceContext.odisPubKey) - const base64BlindedMessage = await getBlindedPhoneNumber(mockE164Number, blsBlindingClient) + const base64BlindedMessage = await getBlindedIdentifier(mockE164Number, blsBlindingClient) - const base64BlindSig = await getBlindedPhoneNumberSignature( + const base64BlindSig = await getBlindedIdentifierSignature( mockAccount, authSigner, serviceContext, base64BlindedMessage ) - const phoneNumberHashDetails = await getPhoneNumberIdentifierFromSignature( + const phoneNumberHashDetails = await getOnchainIdentifierFromSignature( mockE164Number, + IdentifierType.PHONE_NUMBER, base64BlindSig, blsBlindingClient ) - expect(phoneNumberHashDetails.phoneHash).toEqual(expectedPhoneHash) + expect(phoneNumberHashDetails.identifierHash).toEqual(expectedPhoneHash) expect(phoneNumberHashDetails.pepper).toEqual(expectedPepper) }) }) @@ -120,11 +123,3 @@ describe(getPhoneNumberIdentifier, () => { ).rejects.toThrow(ErrorMessages.ODIS_AUTH_ERROR) }) }) - -describe(getPepperFromThresholdSignature, () => { - it('Hashes sigs correctly', () => { - const base64Sig = 'vJeFZJ3MY5KlpI9+kIIozKkZSR4cMymLPh2GHZUatWIiiLILyOcTiw2uqK/LBReA' - const signature = Buffer.from(base64Sig, 'base64') - expect(getPepperFromThresholdSignature(signature)).toBe('piWqRHHYWtfg9') - }) -})