Skip to content

Commit

Permalink
Address PR feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
i1skn committed Nov 20, 2020
1 parent 8fb1277 commit 16d71d8
Show file tree
Hide file tree
Showing 15 changed files with 194 additions and 151 deletions.
12 changes: 8 additions & 4 deletions packages/celotool/src/lib/attestation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { Address, CeloTransactionParams, ContractKit, OdisUtils } from '@celo/co
import { AuthSigner } from '@celo/contractkit/lib/identity/odis/query'
import {
ActionableAttestation,
AttesationServiceRevealRequest,
AttestationsWrapper
} from '@celo/contractkit/lib/wrappers/Attestations'
import { AttestationUtils, PhoneNumberUtils } from '@celo/utils'
Expand Down Expand Up @@ -74,12 +75,15 @@ export async function requestAttestationsFromIssuers(
): Promise<RequestAttestationError[]> {
return concurrentMap(5, attestationsToReveal, async (attestation) => {
try {
const response = await attestations.revealPhoneNumberToIssuer(
phoneNumber,
const revealRequest: AttesationServiceRevealRequest = {
account,
attestation.issuer,
issuer: account,
phoneNumber,
salt: pepper,
}
const response = await attestations.revealPhoneNumberToIssuer(
attestation.attestationServiceURL,
pepper
revealRequest
)
if (!response.ok) {
return {
Expand Down
6 changes: 3 additions & 3 deletions packages/contractkit/src/wrappers/Attestations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ export interface AttesationServiceRevealRequest {
securityCodePrefix?: string
}

export interface AttesationServiceCodeForSecurityRequest {
export interface AttestationServiceSecurityCodeRequest {
account: Address
phoneNumber: string
issuer: string
Expand Down Expand Up @@ -578,9 +578,9 @@ export class AttestationsWrapper extends BaseWrapper<Attestations> {
/**
* Returns attestation code for provided security code from validator's attestation service
* @param serviceURL: validator's attestation service URL
* @param body: onServiceCodeForSecurityRequest
* @param body: AttestationServiceCodeForSecurityRequest
*/
getAttestationForSecurityCode(serviceURL: string, body: AttesationServiceCodeForSecurityRequest) {
getAttestationForSecurityCode(serviceURL: string, body: AttestationServiceSecurityCodeRequest) {
const urlParams = new URLSearchParams({ ...body })
return fetch(appendPath(serviceURL, 'get_attestations') + '?' + urlParams, {
method: 'GET',
Expand Down
4 changes: 2 additions & 2 deletions packages/docs/SUMMARY.md
Original file line number Diff line number Diff line change
Expand Up @@ -262,8 +262,8 @@
- [AttestationState](developer-resources/contractkit/reference/enums/_wrappers_attestations_.attestationstate.md)
- [AttestationsWrapper](developer-resources/contractkit/reference/classes/_wrappers_attestations_.attestationswrapper.md)
- [ActionableAttestation](developer-resources/contractkit/reference/interfaces/_wrappers_attestations_.actionableattestation.md)
- [AttesationServiceCodeForSecurityRequest](developer-resources/contractkit/reference/interfaces/_wrappers_attestations_.attesationservicecodeforsecurityrequest.md)
- [AttesationServiceRevealRequest](developer-resources/contractkit/reference/interfaces/_wrappers_attestations_.attesationservicerevealrequest.md)
- [AttestationServiceSecurityCodeRequest](developer-resources/contractkit/reference/interfaces/_wrappers_attestations_.attestationservicesecuritycoderequest.md)
- [AttestationServiceStatusResponse](developer-resources/contractkit/reference/interfaces/_wrappers_attestations_.attestationservicestatusresponse.md)
- [AttestationStat](developer-resources/contractkit/reference/interfaces/_wrappers_attestations_.attestationstat.md)
- [AttestationStateForIssuer](developer-resources/contractkit/reference/interfaces/_wrappers_attestations_.attestationstateforissuer.md)
Expand Down Expand Up @@ -486,8 +486,8 @@
- [UnlockableWallet](developer-resources/contractkit/reference/interfaces/_wallets_wallet_.unlockablewallet.md)
- [Wallet](developer-resources/contractkit/reference/interfaces/_wallets_wallet_.wallet.md)
- [ActionableAttestation](developer-resources/contractkit/reference/interfaces/_wrappers_attestations_.actionableattestation.md)
- [AttesationServiceCodeForSecurityRequest](developer-resources/contractkit/reference/interfaces/_wrappers_attestations_.attesationservicecodeforsecurityrequest.md)
- [AttesationServiceRevealRequest](developer-resources/contractkit/reference/interfaces/_wrappers_attestations_.attesationservicerevealrequest.md)
- [AttestationServiceSecurityCodeRequest](developer-resources/contractkit/reference/interfaces/_wrappers_attestations_.attestationservicesecuritycoderequest.md)
- [AttestationServiceStatusResponse](developer-resources/contractkit/reference/interfaces/_wrappers_attestations_.attestationservicestatusresponse.md)
- [AttestationStat](developer-resources/contractkit/reference/interfaces/_wrappers_attestations_.attestationstat.md)
- [AttestationStateForIssuer](developer-resources/contractkit/reference/interfaces/_wrappers_attestations_.attestationstateforissuer.md)
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 8 additions & 4 deletions packages/env-tests/src/shared/attestation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { Address, CeloTransactionParams, ContractKit, OdisUtils } from '@celo/co
import { AuthSigner } from '@celo/contractkit/lib/identity/odis/query'
import {
ActionableAttestation,
AttesationServiceRevealRequest,
AttestationsWrapper,
} from '@celo/contractkit/lib/wrappers/Attestations'
import { AttestationUtils, PhoneNumberUtils } from '@celo/utils'
Expand Down Expand Up @@ -85,12 +86,15 @@ export async function requestAttestationsFromIssuers(
): Promise<RequestAttestationError[]> {
return concurrentMap(5, attestationsToReveal, async (attestation) => {
try {
const response = await attestations.revealPhoneNumberToIssuer(
phoneNumber,
const revealRequest: AttesationServiceRevealRequest = {
account,
attestation.issuer,
issuer: account,
phoneNumber,
salt: pepper,
}
const response = await attestations.revealPhoneNumberToIssuer(
attestation.attestationServiceURL,
pepper
revealRequest
)
if (!response.ok) {
return {
Expand Down
22 changes: 22 additions & 0 deletions packages/mobile/src/identity/securityCode.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { extractSecurityCodeWithPrefix, getSecurityCodePrefix } from 'src/identity/securityCode'

describe(getSecurityCodePrefix, () => {
it('should compute correct hash', () => {
expect(getSecurityCodePrefix('0x000000000000000000000008')).toEqual('8')
// 0xf7f551752A78Ce650385B58364225e5ec18D96cB -> 1415591498931780605110544902041322891412830525131
expect(getSecurityCodePrefix('0xf7f551752A78Ce650385B58364225e5ec18D96cB')).toEqual('1')
})
})

describe(extractSecurityCodeWithPrefix, () => {
it('should extract 8 digit code', () => {
expect(extractSecurityCodeWithPrefix('<#> Celo security code: 51365977 5yaJvJcZt2P')).toEqual(
'51365977'
)
})
it('should NOT extract not 8 digit code', () => {
expect(extractSecurityCodeWithPrefix('<#> Celo security code: 5136597 5yaJvJcZt2P')).toEqual(
null
)
})
})
103 changes: 103 additions & 0 deletions packages/mobile/src/identity/securityCode.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
import {
ActionableAttestation,
AttestationServiceSecurityCodeRequest,
AttestationsWrapper,
} from '@celo/contractkit/lib/wrappers/Attestations'
import { PhoneNumberHashDetails } from '@celo/contractkit/src/identity/odis/phone-number-identifier'
import { Address } from '@celo/utils/src/address'
import BigNumber from 'bignumber.js'
import Logger from 'src/utils/Logger'

const TAG = 'identity/securityCode'

function hashAddressToSingleDigit(address: Address): number {
return new BigNumber(address.toLowerCase()).modulo(10).toNumber()
}

export function getSecurityCodePrefix(issuerAddress: Address) {
return `${hashAddressToSingleDigit(issuerAddress)}`
}

export function extractSecurityCodeWithPrefix(message: string) {
const matches = message.match('\\s(\\d{8})\\s')
if (matches && matches.length === 2) {
return matches[1]
}
return null
}

export async function getAttestationCodeForSecurityCode(
attestationsWrapper: AttestationsWrapper,
phoneHashDetails: PhoneNumberHashDetails,
account: string,
attestations: ActionableAttestation[],
securityCodeWithPrefix: string
) {
const securityCodePrefix = parseInt(securityCodeWithPrefix[0], 10)
const lookupAttestations = attestations.filter(
(attestation) => hashAddressToSingleDigit(attestation.issuer) === securityCodePrefix
)

for (const attestation of lookupAttestations) {
// Try to recover the full attestation code from the matching issuer's attestation service
try {
const message = await requestValidator(
attestationsWrapper,
account,
phoneHashDetails,
attestation,
securityCodeWithPrefix.substr(1) // remove prefix
)
return message
} catch (e) {
Logger.warn(
TAG + '@getAttestationCodeForSecurityCode',
'Getting attestation code for security code error: ',
e
)
continue
}
}
}

async function requestValidator(
attestationsWrapper: AttestationsWrapper,
account: string,
phoneHashDetails: PhoneNumberHashDetails,
attestation: ActionableAttestation,
securityCode: string
) {
const issuer = attestation.issuer
Logger.debug(
TAG + '@getAttestationCodeFromSecurityCode',
`Revealing an attestation for issuer: ${issuer}`
)
try {
const requestBody: AttestationServiceSecurityCodeRequest = {
account,
issuer: attestation.issuer,
phoneNumber: phoneHashDetails.e164Number,
salt: phoneHashDetails.pepper,
securityCode,
}

const response = await attestationsWrapper.getAttestationForSecurityCode(
attestation.attestationServiceURL,
requestBody
)
const { ok, status } = response
const body = await response.json()
if (ok && body.attestationCode) {
return body.attestationCode
}

throw new Error(`Error getting security code for ${issuer}. Status code: ${status}`)
} catch (error) {
Logger.error(
TAG + '@getAttestationCodeFromSecurityCode',
`get for issuer ${issuer} failed`,
error
)
return false
}
}
22 changes: 0 additions & 22 deletions packages/mobile/src/identity/utils.test.ts

This file was deleted.

13 changes: 0 additions & 13 deletions packages/mobile/src/identity/utils.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { Address } from '@celo/utils/src/address'
import BigNumber from 'bignumber.js'

export function isUserBalanceSufficient(
Expand All @@ -21,15 +20,3 @@ export const removeKeyFromMapping = (mapping: { [key: string]: string }, keyToRe
}
return newMapping
}

export function hashAddressToSingleDigit(address: Address): number {
return new BigNumber(address.toLowerCase()).modulo(10).toNumber()
}

export function extractShortSecurityCodeMessage(message: string) {
const matches = message.match('\\s(\\d{8})\\s')
if (matches && matches.length === 2) {
return matches[1]
}
return null
}
Loading

0 comments on commit 16d71d8

Please sign in to comment.