Skip to content

Commit

Permalink
update tests
Browse files Browse the repository at this point in the history
  • Loading branch information
isabellewei committed Oct 28, 2022
1 parent 63ee2ae commit 1a35832
Show file tree
Hide file tree
Showing 2 changed files with 160 additions and 18 deletions.
147 changes: 147 additions & 0 deletions packages/sdk/identity/src/odis/identifier.test.ts
Original file line number Diff line number Diff line change
@@ -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')
})
})
31 changes: 13 additions & 18 deletions packages/sdk/identity/src/odis/phone-number-identifier.test.ts
Original file line number Diff line number Diff line change
@@ -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'
Expand Down Expand Up @@ -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,
Expand All @@ -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)
})
})
Expand All @@ -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')
})
})

0 comments on commit 1a35832

Please sign in to comment.