-
Notifications
You must be signed in to change notification settings - Fork 375
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
15 changed files
with
194 additions
and
151 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 2 additions & 2 deletions
4
packages/docs/developer-resources/contractkit/reference/SUMMARY.md
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
4 changes: 2 additions & 2 deletions
4
...es/contractkit/reference/classes/_wrappers_attestations_.attestationswrapper.md
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
14 changes: 7 additions & 7 deletions
14
...ttesationservicecodeforsecurityrequest.md → ....attestationservicesecuritycoderequest.md
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
2 changes: 1 addition & 1 deletion
2
...cs/developer-resources/contractkit/reference/modules/_wrappers_attestations_.md
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.