Skip to content

Commit

Permalink
Merge pull request #2372 from torusresearch/feat/openlogin-v5
Browse files Browse the repository at this point in the history
OpenLogin v5
  • Loading branch information
chaitanyapotti authored Oct 31, 2023
2 parents 69ad77f + eb3c2bf commit 098c7fa
Show file tree
Hide file tree
Showing 26 changed files with 1,445 additions and 1,737 deletions.
2,311 changes: 979 additions & 1,332 deletions package-lock.json

Large diffs are not rendered by default.

108 changes: 54 additions & 54 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,48 +30,48 @@
"test:e2e:mobile": "env MOBILE_ENV=true mocha --timeout=60000 ./test/e2e"
},
"dependencies": {
"@ethereumjs/common": "^3.2.0",
"@ethereumjs/tx": "^4.2.0",
"@ethereumjs/util": "^8.1.0",
"@ethereumjs/common": "^4.0.0",
"@ethereumjs/tx": "^5.0.0",
"@ethereumjs/util": "^9.0.0",
"@gtm-support/vue2-gtm": "^2.0.0",
"@metamask/contract-metadata": "^2.3.1",
"@metamask/eth-json-rpc-infura": "^8.1.1",
"@metamask/eth-json-rpc-middleware": "^11.0.1",
"@metamask/eth-json-rpc-provider": "^1.0.1",
"@metamask/eth-sig-util": "^6.0.0",
"@sentry/browser": "^7.64.0",
"@sentry/tracing": "^7.64.0",
"@sentry/vue": "^7.64.0",
"@toruslabs/broadcast-channel": "^7.0.0",
"@toruslabs/eccrypto": "^3.0.0",
"@toruslabs/http-helpers": "^4.0.0",
"@toruslabs/loglevel-sentry": "^5.0.0",
"@toruslabs/metadata-helpers": "^4.0.0",
"@toruslabs/openlogin": "^4.7.2",
"@toruslabs/openlogin-jrpc": "^4.7.2",
"@toruslabs/openlogin-session-manager": "^1.1.0",
"@toruslabs/openlogin-subkey": "^4.7.2",
"@toruslabs/openlogin-utils": "^4.7.0",
"@uniswap/default-token-list": "^11.5.0",
"@walletconnect/sign-client": "^2.10.2",
"@walletconnect/utils": "^2.10.2",
"@metamask/contract-metadata": "^2.4.0",
"@metamask/eth-json-rpc-infura": "^9.0.0",
"@metamask/eth-json-rpc-middleware": "^12.0.0",
"@metamask/eth-json-rpc-provider": "^2.2.0",
"@metamask/eth-sig-util": "^7.0.0",
"@sentry/browser": "^7.75.1",
"@sentry/tracing": "^7.75.1",
"@sentry/vue": "^7.75.1",
"@toruslabs/broadcast-channel": "^9.0.1",
"@toruslabs/eccrypto": "^4.0.0",
"@toruslabs/http-helpers": "^5.0.0",
"@toruslabs/loglevel-sentry": "^6.0.1",
"@toruslabs/metadata-helpers": "^5.0.0",
"@toruslabs/openlogin": "^5.3.0",
"@toruslabs/openlogin-jrpc": "^5.3.0",
"@toruslabs/openlogin-session-manager": "^3.0.0",
"@toruslabs/openlogin-subkey": "^5.0.0",
"@toruslabs/openlogin-utils": "^5.3.0",
"@uniswap/default-token-list": "^11.9.0",
"@walletconnect/sign-client": "^2.10.4",
"@walletconnect/utils": "^2.10.4",
"await-semaphore": "^0.1.3",
"bignumber.js": "^9.1.1",
"bignumber.js": "^9.1.2",
"bn.js": "^5.2.1",
"bowser": "^2.11.0",
"copy-to-clipboard": "^3.3.3",
"core-js": "^3.32.0",
"core-js": "^3.33.1",
"das-sdk": "^1.9.3",
"deepmerge": "^4.3.1",
"eth-block-tracker": "^7.1.0",
"eth-json-rpc-filters": "^6.0.0",
"eth-block-tracker": "^8.1.0",
"eth-json-rpc-filters": "^6.0.1",
"eth-query": "^2.1.2",
"eth-rpc-errors": "^4.0.3",
"ethers": "^6.7.1",
"ethers": "^6.8.0",
"ethjs-query": "^0.3.8",
"fast-json-patch": "^3.1.1",
"jsonschema": "^1.4.1",
"jwt-decode": "^3.1.2",
"jwt-decode": "^4.0.0",
"lodash": "^4.17.21",
"loglevel": "^1.8.1",
"pify": "^5.0.0",
Expand All @@ -97,67 +97,67 @@
"workbox-strategies": "^7.0.0"
},
"devDependencies": {
"@babel/core": "^7.22.10",
"@babel/eslint-parser": "^7.22.10",
"@babel/core": "^7.23.2",
"@babel/eslint-parser": "^7.22.15",
"@babel/eslint-plugin": "^7.22.10",
"@babel/plugin-proposal-class-properties": "^7.18.6",
"@babel/plugin-proposal-numeric-separator": "^7.18.6",
"@babel/preset-env": "^7.22.10",
"@babel/register": "^7.22.5",
"@sentry/cli": "^2.20.5",
"@babel/preset-env": "^7.23.2",
"@babel/register": "^7.22.15",
"@sentry/cli": "^2.21.2",
"@vue/cli-plugin-babel": "^5.0.8",
"@vue/cli-plugin-eslint": "^5.0.8",
"@vue/cli-plugin-pwa": "^5.0.8",
"@vue/cli-service": "^5.0.8",
"@vue/eslint-config-standard": "^8.0.1",
"assert": "^2.0.0",
"assert": "^2.1.0",
"codecov": "^3.8.3",
"cross-env": "^7.0.3",
"dotenv": "^16.3.1",
"empty-module": "0.0.2",
"eslint": "^8.47.0",
"eslint": "^8.52.0",
"eslint-config-airbnb-base": "^15.0.0",
"eslint-config-prettier": "^9.0.0",
"eslint-config-problems": "^8.0.0",
"eslint-config-standard": "^17.1.0",
"eslint-plugin-import": "^2.28.0",
"eslint-plugin-n": "^16.0.1",
"eslint-plugin-import": "^2.29.0",
"eslint-plugin-n": "^16.2.0",
"eslint-plugin-only-error": "^1.0.2",
"eslint-plugin-prettier": "^5.0.0",
"eslint-plugin-prettier": "^5.0.1",
"eslint-plugin-promise": "^6.1.1",
"eslint-plugin-simple-import-sort": "^10.0.0",
"eslint-plugin-switch-case": "^1.1.2",
"eslint-plugin-unicorn": "^47.0.0",
"eslint-plugin-vue": "^9.17.0",
"eslint-plugin-vue-scoped-css": "^2.5.0",
"eslint-plugin-unicorn": "^48.0.1",
"eslint-plugin-vue": "^9.18.1",
"eslint-plugin-vue-scoped-css": "^2.5.1",
"express": "^4.18.2",
"fibers": "^5.0.3",
"ganache": "^7.9.0",
"ganache": "^7.9.1",
"husky": "^8.0.3",
"jsdom": "^22.1.0",
"jsdom-global": "^3.0.2",
"lint-staged": "^14.0.0",
"lint-staged": "^15.0.2",
"lodash-es": "^4.17.21",
"luxon": "^3.4.0",
"luxon": "^3.4.3",
"mocha": "^10.2.0",
"nock": "13.3.2",
"node-fetch": "^3.3.2",
"nock": "13.3.6",
"nyc": "^15.1.0",
"prettier": "^3.0.2",
"prettier": "^3.0.3",
"process": "^0.11.10",
"puppeteer": "^21.0.3",
"sass": "~1.32.13",
"puppeteer": "^21.4.1",
"sass": "~1.69.5",
"sass-loader": "^13.3.2",
"sinon": "^15.2.0",
"stylus": "^0.59.0",
"stylus": "^0.60.0",
"stylus-loader": "^7.1.3",
"terser-webpack-plugin": "^5.3.9",
"through2": "^4.0.2",
"uglifyify": "^5.0.2",
"url": "^0.11.1",
"undici": "^5.27.0",
"url": "^0.11.3",
"vue-cli-plugin-vuetify": "^2.5.8",
"vue-cli-plugin-webpack-bundle-analyzer": "~4.0.0",
"vue-template-compiler": "^2.7.14",
"vue-template-compiler": "^2.7.15",
"vuetify-loader": "^1.9.2",
"worker-loader": "^3.0.8"
},
Expand Down
3 changes: 1 addition & 2 deletions scripts/importLocales.mjs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
/* eslint-disable unicorn/prefer-top-level-await */
/* eslint-disable import/no-extraneous-dependencies */

import fs from 'fs'
import fetch from 'node-fetch'
import path from 'path'

const args = process.argv.slice(2)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@
</template>

<script>
import { addHexPrefix, isValidPrivate, stripHexPrefix, toBuffer } from '@ethereumjs/util'
import { addHexPrefix, isValidPrivate, stripHexPrefix } from '@ethereumjs/util'
import BN from 'bn.js'
import log from 'loglevel'
Expand Down Expand Up @@ -129,7 +129,7 @@ export default {
return this.t('walletSettings.customKey.privateKeyCannotBeZero') // 'Private key cannot be 0'
}
const prefixed = addHexPrefix(v)
const buffer = toBuffer(prefixed)
const buffer = Buffer.from(prefixed.slice(2), 'hex')
if (!isValidPrivate(buffer)) {
return this.t('walletSettings.customKey.invalidPrivateKey') // 'Invalid private key'
}
Expand Down
4 changes: 2 additions & 2 deletions src/controllers/DecryptMessageManager.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { addHexPrefix, bufferToHex, stripHexPrefix } from '@ethereumjs/util'
import { addHexPrefix, bytesToHex, stripHexPrefix } from '@ethereumjs/util'
import { ethErrors } from 'eth-rpc-errors'
import EventEmitter from 'events'
import log from 'loglevel'
Expand Down Expand Up @@ -309,6 +309,6 @@ export default class DecryptMessageManager extends EventEmitter {
log.debug('Message was not hex encoded, interpreting as utf8.')
}

return bufferToHex(Buffer.from(data, 'utf8'))
return bytesToHex(Buffer.from(data, 'utf8'))
}
}
4 changes: 2 additions & 2 deletions src/controllers/MessageManager.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { bufferToHex } from '@ethereumjs/util'
import { bytesToHex } from '@ethereumjs/util'
import { keccak256 } from '@toruslabs/metadata-helpers'
import { ethErrors } from 'eth-rpc-errors'
import EventEmitter from 'events'
Expand Down Expand Up @@ -290,5 +290,5 @@ function normalizeMessageData(data) {
return data
}
// data is unicode, convert to hex
return bufferToHex(Buffer.from(data, 'utf8'))
return bytesToHex(Buffer.from(data, 'utf8'))
}
4 changes: 2 additions & 2 deletions src/controllers/PersonalMessageManager.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { addHexPrefix, bufferToHex, stripHexPrefix } from '@ethereumjs/util'
import { addHexPrefix, bytesToHex, stripHexPrefix } from '@ethereumjs/util'
import { ethErrors } from 'eth-rpc-errors'
import EventEmitter from 'events'
import log from 'loglevel'
Expand Down Expand Up @@ -290,6 +290,6 @@ export default class PersonalMessageManager extends EventEmitter {
log.debug('Message was not hex encoded, interpreting as utf8.', error)
}

return bufferToHex(Buffer.from(data, 'utf8'))
return bytesToHex(Buffer.from(data, 'utf8'))
}
}
2 changes: 1 addition & 1 deletion src/controllers/PreferencesController.js
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ class PreferencesController extends SafeEventEmitter {
const messageToSign = await this.getMessageForSigning(address)
if (!messageToSign.startsWith('Torus Signin')) throw new Error('Cannot sign on invalid message')
const bufferedMessage = Buffer.from(messageToSign, 'utf8')
const hashedMessage = hashPersonalMessage(bufferedMessage).toString('hex')
const hashedMessage = Buffer.from(hashPersonalMessage(bufferedMessage)).toString('hex')
const signedMessage = await this.signMessage(address, hashedMessage)
response = await this.api.post(
`${config.api}/auth/verify`,
Expand Down
8 changes: 4 additions & 4 deletions src/controllers/TorusKeyring.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { bufferToHex, ecsign, stripHexPrefix } from '@ethereumjs/util'
import { bigIntToBytes, ecsign, stripHexPrefix } from '@ethereumjs/util'
import { concatSig, decrypt, getEncryptionPublicKey, personalSign, signTypedData } from '@metamask/eth-sig-util'
import { Wallet } from 'ethers'
import { EventEmitter } from 'events'
Expand Down Expand Up @@ -57,13 +57,13 @@ export default class TorusKeyring extends EventEmitter {
newWallets.push(Wallet.createRandom())
}
this.wallets = [...this.wallets, ...newWallets]
const hexWallets = newWallets.map((w) => bufferToHex(w.address))
const hexWallets = newWallets.map((w) => w.address)
return hexWallets
}

// Not using
async getAccounts() {
return this.wallets.map((w) => bufferToHex(w.address))
return this.wallets.map((w) => w.address)
}

// tx is an instance of the ethereumjs-transaction class.
Expand All @@ -85,7 +85,7 @@ export default class TorusKeyring extends EventEmitter {
const privKey = this.getBufferPrivateKey(wallet.privateKey)

const messageSig = ecsign(Buffer.from(message, 'hex'), privKey)
const rawMessageSig = concatSig(messageSig.v, messageSig.r, messageSig.s)
const rawMessageSig = concatSig(Buffer.from(bigIntToBytes(messageSig.v)), Buffer.from(messageSig.r), Buffer.from(messageSig.s))
return rawMessageSig
}

Expand Down
11 changes: 9 additions & 2 deletions src/controllers/gas/GasFeeController.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
calculateTimeEstimate,
fetchEthGasPriceEstimate as defaultFetchEthGasPriceEstimate,
fetchGasEstimates as defaultFetchGasEstimates,
fetchGasEstimatesViaEthFeeHistory as defaultFetchGasEstimatesViaEthFeeHistory,
fetchLegacyGasPriceEstimates as defaultFetchLegacyGasPriceEstimates,
} from './gas-util'

Expand Down Expand Up @@ -59,6 +60,7 @@ class GasFeeController {
fetchGasEstimates = defaultFetchGasEstimates,
fetchEthGasPriceEstimate = defaultFetchEthGasPriceEstimate,
fetchLegacyGasPriceEstimates = defaultFetchLegacyGasPriceEstimates,
fetchGasEstimatesViaEthFeeHistory = defaultFetchGasEstimatesViaEthFeeHistory,
getCurrentNetworkEIP1559Compatibility,
getCurrentAccountEIP1559Compatibility,
getChainId,
Expand All @@ -72,6 +74,7 @@ class GasFeeController {
this.fetchGasEstimates = fetchGasEstimates
this.fetchEthGasPriceEstimate = fetchEthGasPriceEstimate
this.fetchLegacyGasPriceEstimates = fetchLegacyGasPriceEstimates
this.fetchGasEstimatesViaEthFeeHistory = fetchGasEstimatesViaEthFeeHistory
this.getProvider = getProvider

this.getCurrentNetworkEIP1559Compatibility = getCurrentNetworkEIP1559Compatibility
Expand Down Expand Up @@ -139,8 +142,12 @@ class GasFeeController {

try {
if (isEIP1559Compatible) {
// TODO: kovan is not working due to a bug in metamask api
const estimates = await this.fetchGasEstimates(this.EIP1559APIEndpoint.replace('<chain_id>', `${chainId}`))
let estimates
try {
estimates = await this.fetchGasEstimates(this.config.EIP1559APIEndpoint.replace('<chain_id>', `${chainId}`))
} catch {
estimates = await this.fetchGasEstimatesViaEthFeeHistory(this.ethQuery)
}
const { suggestedMaxPriorityFeePerGas, suggestedMaxFeePerGas } = estimates.medium
const estimatedGasFeeTimeBounds = this.getTimeEstimate(suggestedMaxPriorityFeePerGas, suggestedMaxFeePerGas)
newState = {
Expand Down
44 changes: 44 additions & 0 deletions src/controllers/gas/gas-util.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import BigNumber from 'bignumber.js'
import BN from 'bn.js'

import { decGWEIToHexWEI, hexWEIToDecGWEI } from '../../utils/conversionUtils'
Expand Down Expand Up @@ -32,6 +33,49 @@ export async function fetchGasEstimates(url) {
return normalizedEstimates
}

export async function fetchGasEstimatesViaEthFeeHistory(ethQuery) {
const noOfBlocks = 10
const newestBlock = 'latest'
// get the 10, 50 and 95th percentile of the tip fees from the last 10 blocks
const percentileValues = [10, 50, 95]
const feeHistory = await ethQuery.sendAsync({
method: 'eth_feeHistory',
params: [noOfBlocks, newestBlock, percentileValues],
})
// this is in hex wei
const finalBaseFeePerGas = feeHistory.baseFeePerGas.at(-1)
// this is in hex wei
const priorityFeeCalcs = feeHistory.reward.reduce(
(acc, curr) => ({
slow: acc.slow.plus(new BigNumber(curr[0], 16)),
average: acc.average.plus(new BigNumber(curr[1], 16)),
fast: acc.fast.plus(new BigNumber(curr[2], 16)),
}),
{ slow: new BigNumber(0), average: new BigNumber(0), fast: new BigNumber(0) }
)
return {
estimatedBaseFee: hexWEIToDecGWEI(finalBaseFeePerGas).toString(10),
high: {
maxWaitTimeEstimate: 30_000,
minWaitTimeEstimate: 15_000,
suggestedMaxFeePerGas: hexWEIToDecGWEI(priorityFeeCalcs.fast.plus(finalBaseFeePerGas).toString(16)).toString(),
suggestedMaxPriorityFeePerGas: hexWEIToDecGWEI(priorityFeeCalcs.fast.toString(16)).toString(),
},
medium: {
maxWaitTimeEstimate: 45_000,
minWaitTimeEstimate: 15_000,
suggestedMaxFeePerGas: hexWEIToDecGWEI(priorityFeeCalcs.average.plus(finalBaseFeePerGas).toString(16)).toString(),
suggestedMaxPriorityFeePerGas: hexWEIToDecGWEI(priorityFeeCalcs.average.toString(16)).toString(),
},
low: {
maxWaitTimeEstimate: 60_000,
minWaitTimeEstimate: 15_000,
suggestedMaxFeePerGas: hexWEIToDecGWEI(priorityFeeCalcs.slow.plus(finalBaseFeePerGas).toString(16)).toString(),
suggestedMaxPriorityFeePerGas: hexWEIToDecGWEI(priorityFeeCalcs.slow.toString(16)).toString(),
},
}
}

/**
* Hit the legacy MetaSwaps gasPrices estimate api and return the low, medium
* high values from that API.
Expand Down
Loading

0 comments on commit 098c7fa

Please sign in to comment.