Skip to content

Commit

Permalink
Update wallet middleware errors (#34)
Browse files Browse the repository at this point in the history
* add eth-rpc-errors, use in wallet and fetch middlewares
  • Loading branch information
rekmarks authored Jun 23, 2020
1 parent f31c7dc commit 2eac97d
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 22 deletions.
19 changes: 12 additions & 7 deletions fetch.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const fetch = global.fetch || require('fetch-ponyfill')().fetch
const url = require('url')
const { errors: rpcErrors } = require('eth-json-rpc-errors')
const { ethErrors } = require('eth-rpc-errors')
const btoa = require('btoa')
const createAsyncMiddleware = require('json-rpc-engine/src/createAsyncMiddleware')

Expand Down Expand Up @@ -59,7 +59,7 @@ function checkForHttpErrors (fetchRes) {
// check for errors
switch (fetchRes.status) {
case 405:
throw rpcErrors.methodNotFound()
throw ethErrors.rpc.methodNotFound()

case 418:
throw createRatelimitError()
Expand All @@ -73,10 +73,16 @@ function checkForHttpErrors (fetchRes) {
function parseResponse (fetchRes, body) {
// check for error code
if (fetchRes.status !== 200) {
throw rpcErrors.internal(`Non-200 status code: '${fetchRes.status}'`, body)
throw ethErrors.rpc.internal({
message: `Non-200 status code: '${fetchRes.status}'`,
data: body,
})
}
// check for rpc error
if (body.error) throw rpcErrors.internal(body.error.toString(), body.error)
if (body.error) throw ethErrors.rpc.internal({
message: body.error.toString(),
data: body.error,
})
// return successful result
return body.result
}
Expand Down Expand Up @@ -137,14 +143,13 @@ function normalizeUrlFromParsed(parsedUrl) {
}

function createRatelimitError () {
let msg = `Request is being rate limited.`
return rpcErrors.internal(msg)
return ethErrors.rpc.internal({ message: `Request is being rate limited.` })
}

function createTimeoutError () {
let msg = `Gateway timeout. The request took too long to process. `
msg += `This can happen when querying logs over too wide a block range.`
return rpcErrors.internal(msg)
return ethErrors.rpc.internal({ message: msg })
}

function timeout(duration) {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@
"dependencies": {
"btoa": "^1.2.1",
"clone": "^2.1.1",
"eth-json-rpc-errors": "^1.0.1",
"eth-query": "^2.1.2",
"eth-rpc-errors": "^2.1.1",
"eth-sig-util": "^1.4.2",
"ethereumjs-block": "^1.6.0",
"ethereumjs-tx": "^1.3.7",
Expand Down
8 changes: 4 additions & 4 deletions test/wallet.js
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ function ethSignTest({ testLabel, address, accounts, fromAddressIsValid }) {
t.fail('should have validated that fromAddress is invalid')
}
} catch (err) {
if (!fromAddressIsValid && err.message.includes('WalletMiddleware - Invalid keyholder address.')) {
if (!fromAddressIsValid && err.message.includes('Invalid parameters: must provide an Ethereum address.')) {
t.pass('correctly errored on invalid sender.')
} else {
t.ifError(err)
Expand Down Expand Up @@ -224,7 +224,7 @@ function ethSignTypedDataTest({ testLabel, address, accounts, fromAddressIsValid
t.fail('should have validated that fromAddress is invalid')
}
} catch (err) {
if (!fromAddressIsValid && err.message.includes('WalletMiddleware - Invalid keyholder address.')) {
if (!fromAddressIsValid && err.message.includes('Invalid parameters: must provide an Ethereum address.')) {
t.pass('correctly errored on invalid sender.')
} else {
t.ifError(err)
Expand Down Expand Up @@ -262,7 +262,7 @@ function personalSignTest({ testLabel, address, accounts, fromAddressIsValid })
t.fail('should have validated that fromAddress is invalid')
}
} catch (err) {
if (!fromAddressIsValid && err.message.includes('WalletMiddleware - Invalid keyholder address.')) {
if (!fromAddressIsValid && err.message.includes('Invalid parameters: must provide an Ethereum address.')) {
t.pass('correctly errored on invalid sender.')
} else {
t.ifError(err)
Expand Down Expand Up @@ -298,7 +298,7 @@ function transactionTest({ testLabel, txParams, accounts, fromAddressIsValid })
t.fail('should have validated that fromAddress is invalid')
}
} catch (err) {
if (!fromAddressIsValid && err.message.includes('WalletMiddleware - Invalid keyholder address.')) {
if (!fromAddressIsValid && err.message.includes('Invalid parameters: must provide an Ethereum address.')) {
t.pass('correctly errored on invalid sender.')
} else {
t.ifError(err)
Expand Down
23 changes: 13 additions & 10 deletions wallet.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const createAsyncMiddleware = require('json-rpc-engine/src/createAsyncMiddleware')
const createScaffoldMiddleware = require('json-rpc-engine/src/createScaffoldMiddleware')
const sigUtil = require('eth-sig-util')
const { ethErrors } = require('eth-rpc-errors')

module.exports = function createWalletMiddleware(opts = {}) {
// parse + validate options
Expand All @@ -15,7 +16,7 @@ module.exports = function createWalletMiddleware(opts = {}) {
const processEncryptionPublicKey = opts.processEncryptionPublicKey

if (!getAccounts) {
throw new Error('WalletMiddleware - opts.getAccounts not provided')
throw new Error('opts.getAccounts is required')
}

return createScaffoldMiddleware({
Expand Down Expand Up @@ -55,7 +56,7 @@ module.exports = function createWalletMiddleware(opts = {}) {
async function sendTransaction(req, res) {

if (!processTransaction) {
throw new Error('WalletMiddleware - opts.processTransaction not provided')
throw ethErrors.rpc.methodNotSupported()
}

const txParams = req.params[0] || {}
Expand All @@ -70,7 +71,7 @@ module.exports = function createWalletMiddleware(opts = {}) {
async function ethSign(req, res) {

if (!processEthSignMessage) {
throw new Error('WalletMiddleware - opts.processEthSignMessage not provided')
throw ethErrors.rpc.methodNotSupported()
}

const address = await validateAndNormalizeKeyholder(req.params[0], req)
Expand All @@ -87,7 +88,7 @@ module.exports = function createWalletMiddleware(opts = {}) {
async function signTypedData (req, res) {

if (!processTypedMessage) {
throw new Error('WalletMiddleware - opts.processTypedMessage not provided')
throw ethErrors.rpc.methodNotSupported()
}

const message = req.params[0]
Expand All @@ -105,7 +106,7 @@ module.exports = function createWalletMiddleware(opts = {}) {
async function signTypedDataV3 (req, res) {

if (!processTypedMessageV3) {
throw new Error('WalletMiddleware - opts.processTypedMessage not provided')
throw ethErrors.rpc.methodNotSupported()
}

const address = await validateAndNormalizeKeyholder(req.params[0], req)
Expand All @@ -123,7 +124,7 @@ module.exports = function createWalletMiddleware(opts = {}) {
async function signTypedDataV4 (req, res) {

if (!processTypedMessageV4) {
throw new Error('WalletMiddleware - opts.processTypedMessage not provided')
throw ethErrors.rpc.methodNotSupported()
}

const address = await validateAndNormalizeKeyholder(req.params[0], req)
Expand All @@ -141,7 +142,7 @@ module.exports = function createWalletMiddleware(opts = {}) {
async function personalSign (req, res) {

if (!processPersonalMessage) {
throw new Error('WalletMiddleware - opts.processPersonalMessage not provided')
throw ethErrors.rpc.methodNotSupported()
}

// process normally
Expand Down Expand Up @@ -198,7 +199,7 @@ module.exports = function createWalletMiddleware(opts = {}) {
async function encryptionPublicKey (req, res) {

if (!processEncryptionPublicKey) {
throw new Error('WalletMiddleware - opts.processEncryptionPublicKey not provided')
throw ethErrors.rpc.methodNotSupported()
}

const address = await validateAndNormalizeKeyholder(req.params[0], req)
Expand All @@ -209,7 +210,7 @@ module.exports = function createWalletMiddleware(opts = {}) {
async function decryptMessage (req, res) {

if (!processDecryptMessage) {
throw new Error('WalletMiddleware - opts.processDecryptMessage not provided')
throw ethErrors.rpc.methodNotSupported()
}

const ciphertext = req.params[0]
Expand Down Expand Up @@ -249,7 +250,9 @@ module.exports = function createWalletMiddleware(opts = {}) {
return normalizedAddress
}
}
throw new Error('WalletMiddleware - Invalid keyholder address.')
throw ethErrors.rpc.invalidParams({
message: `Invalid parameters: must provide an Ethereum address.`
})
}
}

Expand Down
7 changes: 7 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2234,6 +2234,13 @@ eth-query@^2.0.2, eth-query@^2.1.0, eth-query@^2.1.2:
json-rpc-random-id "^1.0.0"
xtend "^4.0.1"

eth-rpc-errors@^2.1.1:
version "2.1.1"
resolved "https://registry.yarnpkg.com/eth-rpc-errors/-/eth-rpc-errors-2.1.1.tgz#00a7d6c8a9c864a8ab7d0356be20964e5bee4b13"
integrity sha512-MY3zAa5ZF8hvgQu1HOF9agaK5GgigBRGpTJ8H0oVlE0NqMu13CW6syyjLXdeIDCGQTbUeHliU1z9dVmvMKx1Tg==
dependencies:
fast-safe-stringify "^2.0.6"

eth-sig-util@2.2.0:
version "2.2.0"
resolved "https://registry.yarnpkg.com/eth-sig-util/-/eth-sig-util-2.2.0.tgz#769fa3d296b450f6618dedeefe076642c923a16f"
Expand Down

0 comments on commit 2eac97d

Please sign in to comment.