Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Use account address when giving CIP8 profile access #354

Merged
merged 4 commits into from
May 12, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 15 additions & 8 deletions packages/mobile/src/account/profileInfo.test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { normalizeAddressWith0x } from '@celo/utils/lib/address'
import RNFS from 'react-native-fs'
import { expectSaga } from 'redux-saga-test-plan'
import { call, select } from 'redux-saga/effects'
Expand All @@ -10,6 +11,7 @@ import {
uploadNameAndPicture,
} from 'src/account/profileInfo'
import { isProfileUploadedSelector, nameSelector, pictureSelector } from 'src/account/selectors'
import { walletToAccountAddressSelector } from 'src/identity/reducer'
import { DEK, retrieveOrGeneratePepper } from 'src/pincode/authentication'
import { getContractKit, getWallet } from 'src/web3/contracts'
import { getAccountAddress, getConnectedUnlockedAccount } from 'src/web3/saga'
Expand Down Expand Up @@ -128,11 +130,14 @@ describe(uploadNameAndPicture, () => {
})

describe(giveProfileAccess, () => {
const recipients = [mockAccount2]
const walletAddress = mockAccount2
const accountAddress = '0xTEST'
const walletToAccountAddress = { [normalizeAddressWith0x(walletAddress)]: accountAddress }

it('gives profile access successfully', async () => {
await expectSaga(giveProfileAccess, recipients)
await expectSaga(giveProfileAccess, walletAddress)
.provide([
[select(walletToAccountAddressSelector), walletToAccountAddress],
[call(getOffchainWrapper), null],
[call(getConnectedUnlockedAccount), mockAccount],
[select(nameSelector), mockName],
Expand All @@ -141,14 +146,15 @@ describe(giveProfileAccess, () => {
])
.run()

expect(mockNameAllowAccess).toBeCalledWith(recipients)
expect(mockPictureAllowAccess).toBeCalledWith(recipients)
expect(mockNameAllowAccess).toBeCalledWith([accountAddress])
expect(mockPictureAllowAccess).toBeCalledWith([accountAddress])
})

it('handles error when fails to give recipient access to name', async () => {
mockNameAllowAccess.mockReturnValueOnce(Error('error'))
await expectSaga(giveProfileAccess, recipients)
await expectSaga(giveProfileAccess, walletAddress)
.provide([
[select(walletToAccountAddressSelector), walletToAccountAddress],
[call(getOffchainWrapper), null],
[call(getConnectedUnlockedAccount), mockAccount],
[select(nameSelector), mockName],
Expand All @@ -157,14 +163,15 @@ describe(giveProfileAccess, () => {
])
.run()
.catch((error) => {
expect(error).toEqual(Error(`Unable to give ${recipients} access to name`))
expect(error).toEqual(Error(`Unable to give ${walletAddress} access to name`))
})
})

it('handles error when fails to give recipient access to picture', async () => {
mockPictureAllowAccess.mockReturnValueOnce(Error('error'))
await expectSaga(giveProfileAccess, recipients)
await expectSaga(giveProfileAccess, walletAddress)
.provide([
[select(walletToAccountAddressSelector), walletToAccountAddress],
[call(getOffchainWrapper), null],
[call(getConnectedUnlockedAccount), mockAccount],
[select(nameSelector), mockName],
Expand All @@ -173,7 +180,7 @@ describe(giveProfileAccess, () => {
])
.run()
.catch((error) => {
expect(error).toEqual(Error(`Unable to give ${recipients} access to picture`))
expect(error).toEqual(Error(`Unable to give ${walletAddress} access to picture`))
})
})
})
Expand Down
27 changes: 16 additions & 11 deletions packages/mobile/src/account/profileInfo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { call, put, select } from 'redux-saga/effects'
import { profileUploaded } from 'src/account/actions'
import { isProfileUploadedSelector, nameSelector, pictureSelector } from 'src/account/selectors'
import UploadServiceDataWrapper from 'src/account/UploadServiceDataWrapper'
import { walletToAccountAddressSelector, WalletToAccountAddressType } from 'src/identity/reducer'
import { DEK, retrieveOrGeneratePepper } from 'src/pincode/authentication'
import { extensionToMimeType, getDataURL, saveRecipientPicture } from 'src/utils/image'
import Logger from 'src/utils/Logger'
Expand Down Expand Up @@ -90,33 +91,36 @@ export function* uploadNameAndPicture() {
}

// this function gives permission to the recipient to view the user's profile info
export function* giveProfileAccess(recipientAddresses: string[]) {
export function* giveProfileAccess(walletAddress: string) {
// TODO: check if key for recipient already exists, skip if yes
try {
// TODO: check if key for recipient already exists, skip if yes
const walletToAccountAddress: WalletToAccountAddressType = yield select(
walletToAccountAddressSelector
)
const accountAddress =
walletToAccountAddress[normalizeAddressWith0x(walletAddress)] ?? walletAddress

const offchainWrapper: UploadServiceDataWrapper = yield call(getOffchainWrapper)
const nameAccessor = new PrivateNameAccessor(offchainWrapper)
let writeError = yield call([nameAccessor, 'allowAccess'], recipientAddresses)
let writeError = yield call([nameAccessor, 'allowAccess'], [accountAddress])
if (writeError) {
Logger.error(TAG + '@giveProfileAccess', writeError)
return
}

const pictureUri = yield select(pictureSelector)
if (pictureUri) {
const pictureAccessor = new PrivatePictureAccessor(offchainWrapper)
writeError = yield call([pictureAccessor, 'allowAccess'], recipientAddresses)
writeError = yield call([pictureAccessor, 'allowAccess'], [accountAddress])
if (writeError) {
Logger.error(TAG + '@giveProfileAccess', writeError)
return
}
}
// not throwing error, because possibility the recipient doesn't have a registered DEK

Logger.info(TAG + '@giveProfileAccess', 'uploaded symmetric keys for ' + recipientAddresses)
Logger.info(TAG + '@giveProfileAccess', 'uploaded symmetric keys for ' + accountAddress)
} catch (error) {
Logger.error(
TAG + '@giveProfileAccess',
'error when giving access to ' + recipientAddresses,
error
)
Logger.error(TAG + '@giveProfileAccess', 'error when giving access to ' + walletAddress, error)
}
}

Expand Down Expand Up @@ -165,6 +169,7 @@ export function* getOffchainWrapper(addAccount = false) {

const contractKit = yield call(getContractKit)
const account: Address = yield call(getAccountAddress)
Logger.info(TAG, 'uploading information for', account)
const offchainWrapper = new UploadServiceDataWrapper(
contractKit,
toChecksumAddress(account),
Expand Down
2 changes: 1 addition & 1 deletion packages/mobile/src/send/saga.ts
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ function* sendPayment(
amount: amount.toString(),
currency,
})
yield call(giveProfileAccess, [recipientAddress])
yield call(giveProfileAccess, recipientAddress)
} catch (error) {
Logger.error(`${TAG}/sendPayment`, 'Could not send payment', error)
ValoraAnalytics.track(SendEvents.send_tx_error, { error: error.message })
Expand Down
10 changes: 8 additions & 2 deletions packages/mobile/src/web3/dataEncryptionKey.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,12 @@ import { OdisUtils } from '@celo/identity'
import { AuthSigner } from '@celo/identity/lib/odis/query'
import { FetchError, TxError } from '@celo/komencikit/src/errors'
import { KomenciKit } from '@celo/komencikit/src/kit'
import { ensureLeading0x, eqAddress, hexToBuffer } from '@celo/utils/lib/address'
import {
ensureLeading0x,
eqAddress,
hexToBuffer,
normalizeAddressWith0x,
} from '@celo/utils/lib/address'
import { CURRENCY_ENUM } from '@celo/utils/lib/currencies'
import { compressedPubKey, deriveDek } from '@celo/utils/lib/dataEncryptionKey'
import * as bip39 from 'react-native-bip39'
Expand Down Expand Up @@ -63,7 +68,8 @@ export function* doFetchDataEncryptionKey(walletAddress: string) {
const walletToAccountAddress: WalletToAccountAddressType = yield select(
walletToAccountAddressSelector
)
const accountAddress = walletToAccountAddress[walletAddress] ?? walletAddress
const accountAddress =
walletToAccountAddress[normalizeAddressWith0x(walletAddress)] ?? walletAddress
const dek: string = yield call(accountsWrapper.getDataEncryptionKey, accountAddress)
yield put(updateAddressDekMap(accountAddress, dek || null))
return !dek ? null : hexToBuffer(dek)
Expand Down