Skip to content
This repository has been archived by the owner on Nov 7, 2023. It is now read-only.

Update DataVault to use Encryption #39

Merged
merged 3 commits into from
Jan 14, 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
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
"typescript": "~4.0.5"
},
"devDependencies": {
"@rsksmart/ipfs-cpinner-client": "^0.1.1-beta.3",
"@rsksmart/ipfs-cpinner-client": "0.1.1-beta.7",
"@rsksmart/rlogin": "0.0.1-beta.3",
"@testing-library/jest-dom": "^5.11.4",
"@testing-library/react": "^11.1.0",
Expand All @@ -44,7 +44,7 @@
"@types/jest": "^26.0.15",
"@types/jwt-decode": "^3.1.0",
"@types/node": "^12.0.0",
"@types/react": "^16.9.53",
"@types/react": "^17.0.0",
"@types/react-dom": "^16.9.8",
"@types/react-redux": "^7.1.11",
"@types/redux-logger": "^3.0.8",
Expand Down
59 changes: 37 additions & 22 deletions src/app/state/operations/datavault.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Dispatch } from 'react'
import DataVaultWebClient from '@rsksmart/ipfs-cpinner-client'
import DataVaultWebClient, { AuthManager, EncryptionManager } from '@rsksmart/ipfs-cpinner-client'

import { createDidFormat } from '../../../formatters'
import { addContentToKey, DataVaultContent, receiveKeyData, removeContentfromKey, swapContentById, receiveStorageInformation, DataVaultStorageState, DataVaultKey } from '../reducers/datavault'
import { getDataVault } from '../../../config/getConfig'
Expand All @@ -12,31 +13,32 @@ import { CreateContentResponse } from '@rsksmart/ipfs-cpinner-client/lib/types'
* @param chainId chainId of the network to create the DID
*/
export const createClient = (provider: any, address: string, chainId: number) => {
const dataVaultConfig = <{serviceUrl: string, serviceDid: string} | null> getDataVault(chainId)
const serviceUrl = getDataVault()
const did = createDidFormat(address, chainId)

return dataVaultConfig
? new DataVaultWebClient({
serviceUrl: dataVaultConfig.serviceUrl,
serviceDid: dataVaultConfig.serviceDid,
did: createDidFormat(address, chainId),
rpcPersonalSign: (data: string) => provider.request({ method: 'personal_sign', params: [address, data] })
})
: null
const personalSign = (data: string) => provider.request({ method: 'personal_sign', params: [address, data] })
const decrypt = (hexCypher: string) => provider.request({ method: 'eth_decrypt', params: [hexCypher, address] })
const getEncryptionPublicKey = () => provider.request({ method: 'eth_getEncryptionPublicKey', params: [address] })

return new DataVaultWebClient({
serviceUrl,
authManager: new AuthManager({ did, serviceUrl, personalSign }),
encryptionManager: new EncryptionManager({ getEncryptionPublicKey, decrypt })
})
}

/**
* Get all keys and data from the datavault
* @param client DataVault client
* @param did DID of the user, required for getting individual key content
*/
export const getDataVaultContent = (client: DataVaultWebClient, did: string) => (dispatch: Dispatch<any>) =>
export const getDataVaultContent = (client: DataVaultWebClient) => (dispatch: Dispatch<any>) =>
client.getKeys()
.then((keys: string[]) =>
keys.forEach((key: string) =>
client.get({ did, key })
client.get({ key })
.then((content: any) => content as DataVaultContent[])
.then((content: DataVaultContent[]) => dispatch(receiveKeyData({ key, content })))
)
.then((content: DataVaultContent[]) => dispatch(receiveKeyData({ key, content }))))
)

/**
Expand Down Expand Up @@ -71,14 +73,6 @@ export const swapDataVaultContent = (client: DataVaultWebClient, key: string, co
client.swap({ key, content, id })
.then(() => dispatch(swapContentById({ key, id, content })))

/**
* Returns storage information from DataVault
* @param client DataVault client
*/
export const getStorageInformation = (client: DataVaultWebClient) => (dispatch: Dispatch<any>) =>
client.getStorageInformation()
.then((storage: DataVaultStorageState) => dispatch(receiveStorageInformation({ storage })))

/**
* Helper function that loops through DataVault items and decides if key & value should be created, updated, or removed
* @param client DataVault Client
Expand All @@ -105,3 +99,24 @@ export const modifyMultipleItems = (client: DataVaultWebClient, values: DataVaul

return Promise.all(promiseArray)
}

/**
* Start the Data Vault Client
* Request StorageInformation first, which will save the access token with the DataVault Service
* in localStorage. Then request the content from the data vault.
* @param provider Web3 Provider
* @param address User Address
* @param chainId ChainId
* @param callback Function
*/
export const dataVaultStart = (provider: any, address: string, chainId: number, callback?: any) => (dispatch: Dispatch<any>) => {
const client = createClient(provider, address, chainId)

client.getStorageInformation()
.then((storage: DataVaultStorageState) => {
dispatch(receiveStorageInformation({ storage }))
dispatch(getDataVaultContent(client))
callback(client)
})
.catch((err: any) => callback(null, err))
}
11 changes: 4 additions & 7 deletions src/app/state/operations/identity.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { Dispatch } from 'react'

import { getAccountAndNetwork } from '../../../ethrpc'
import { rLogin } from '../../../features/rLogin'

import { changeAccount, changeChainId } from '../reducers/identity'
import { resolveDidDocument } from './ethrdid'
import { getBalance, getTokenList } from './defi'
import { createClient, getDataVaultContent, getStorageInformation } from './datavault'
import { createDidFormat } from '../../../formatters'
import { dataVaultStart } from './datavault'

/**
* Login into web3 provider via rLogin
Expand All @@ -25,11 +25,8 @@ export const login = (context: any) => (dispatch: Dispatch<any>) =>
dispatch(getTokenList(provider, parseInt(chainId), address))
dispatch(getBalance(provider, address))

const dataVaultClient = createClient(provider, address, chainId)
context.setDvClient(dataVaultClient)

dataVaultClient && dispatch(getDataVaultContent(dataVaultClient, createDidFormat(address, chainId)))
dataVaultClient && dispatch(getStorageInformation(dataVaultClient))
const callback = (dvClient: any, _error: any) => context.setDvClient(dvClient)
dispatch(dataVaultStart(provider, address, chainId, callback))
})
})
.catch((err: string) => console.log('rLogin Error', err))
1 change: 0 additions & 1 deletion src/config/config.ethereum.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,5 @@
"ethrDid": "0xdca7ef03e98e0dc2b855be647c39abe984fcf21b",
"rpcUrl": "https://mainnet.infura.io/v3/8043bb2cf99347b1bfadfb233c5325c0",
"balanceName": "ETH",
"dataVault": null,
"coinGeckoIds": "ethereum"
}
3 changes: 3 additions & 0 deletions src/config/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"dataVaultUrl": "https://identity.staging.rifcomputing.net/v0"
}
1 change: 0 additions & 1 deletion src/config/config.local.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,5 @@
"ethrDid": "",
"rpcUrl": "http://127.0.0.1:7545",
"balanceName": "RBTC",
"dataVault": null,
"coinGeckoIds": null
}
1 change: 0 additions & 1 deletion src/config/config.mainnet.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,5 @@
"ethrDid": "0xdca7ef03e98e0dc2b855be647c39abe984fcf21b",
"rpcUrl": "https://did.rsk.co:4444",
"balanceName": "RBTC",
"dataVault": null,
"coinGeckoIds": "bitcoin,rif-token"
}
4 changes: 0 additions & 4 deletions src/config/config.testnet.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,5 @@
"ethrDid": "0xdca7ef03e98e0dc2b855be647c39abe984fcf21b",
"rpcUrl": "https://did.testnet.rsk.co:4444",
"balanceName": "TRBTC",
"dataVault": {
"serviceDid": "did:ethr:rsk:testnet:0x285B30492a3F444d78f75261A35cB292Fc8F41A6",
"serviceUrl": "https://identity.staging.rifcomputing.net"
},
"coinGeckoIds": "rif-token,bitcoin"
}
4 changes: 2 additions & 2 deletions src/config/getConfig.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
/* eslint-disable no-unused-vars */
import AllNetworks from './config.json'
import Ethereum from './config.ethereum.json'
import Mainnet from './config.mainnet.json'
import Testnet from './config.testnet.json'
Expand All @@ -7,7 +8,6 @@ import Local from './config.local.json'
export enum SETTINGS {
ETHR_DID_CONTRACT = 'ethrDid',
RPC_URL = 'rpcUrl',
DATAVAULT = 'dataVault',
BALANCE_NAME = 'balanceName',
COIN_GECKO_IDS = 'coinGeckoIds'
}
Expand All @@ -23,6 +23,6 @@ const getSetting = (chainId: number, setting: SETTINGS) => {

export const getDIDRegistryAddress = (chainId: number) => getSetting(chainId, SETTINGS.ETHR_DID_CONTRACT)
export const getRPCUrl = (chainId: number) => getSetting(chainId, SETTINGS.RPC_URL)
export const getDataVault = (chainId: number) => getSetting(chainId, SETTINGS.DATAVAULT)
export const getDataVault = () => AllNetworks.dataVaultUrl
export const getBalanceName = (chainId: number) => getSetting(chainId, SETTINGS.BALANCE_NAME) as string
export const getCoinGeckoIdsForNetwork = (chainId: number) => getSetting(chainId, SETTINGS.COIN_GECKO_IDS) as string
Loading