Skip to content

Commit

Permalink
refactor: global middlewares signer and logger
Browse files Browse the repository at this point in the history
  • Loading branch information
pikonha committed May 27, 2024
1 parent c3ebb87 commit e05a8fb
Show file tree
Hide file tree
Showing 7 changed files with 91 additions and 87 deletions.
2 changes: 1 addition & 1 deletion packages/client/src/write.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ const _ = (async () => {
address: resolverAddr,
functionName: 'register',
abi: dbABI,
args: [namehash(publicAddress), 99999999n],
args: [namehash(publicAddress), 9999999999n],
})
} catch (err) {
const data = getRevertErrorData(err)
Expand Down
19 changes: 8 additions & 11 deletions packages/gateway/cmd/database.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ import {
} from '../src/handlers'
import { abi } from '../src/abi'
import { PostgresRepository } from '../src/repositories/postgres'
import { NewServer } from '../src/server'
import { withLogger, withSigner } from '../src/middlewares'
import { OwnershipValidator } from '../src/services'
import * as ccip from '@blockful/ccip-server'

config({
path: process.env.ENV_FILE || '../env',
Expand All @@ -41,7 +41,12 @@ const _ = (async () => {
const repo = new PostgresRepository(dbclient)
const validator = new OwnershipValidator(repo)

const app = NewServer(
const server = new ccip.Server()
server.app.use(withSigner(privateKey as Hex))
server.app.use(withLogger({ abi, debug: process.env.DEBUG === 'true' }))

server.add(
abi,
withQuery(), // required for Universal Resolver integration
withGetText(repo),
withSetText(repo, validator),
Expand All @@ -50,18 +55,10 @@ const _ = (async () => {
withGetContentHash(repo),
withSetContentHash(repo, validator),
withRegisterDomain(repo, validator),
).makeApp(
'/',
withLogger({ abi, debug: process.env.DEBUG === 'true' }),
withSigner(privateKey as Hex, [
'function text(bytes32 node, string key)',
'function addr(bytes32 node)',
'function contenthash(bytes32 node)',
]),
)

const port = process.env.PORT || 3000
app.listen(port, () => {
server.makeApp('/').listen(port, () => {
console.log(`Gateway bound to port ${port}.`)
})
})()
12 changes: 10 additions & 2 deletions packages/gateway/src/middlewares/logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,16 @@ export function withLogger({
}

return (req: Request, res: Response, next: NextFunction) => {
const callData =
req.method === 'GET' ? req.params?.callData : req.body?.data
let callData = req.body?.data
if (req.method === 'GET') {
// fetches de calldata from the url according to the EIP-3668 (/:sender/:calldata.json)
const data = /\/\w+\/(\w+)\.json/g.exec(req.url) || []
callData = data.length > 1 ? data[1] : ''
}
if (!callData) {
return next()
}

const func = decodeFunctionData({ abi: parseAbi(abi), data: callData })

next()
Expand Down
9 changes: 2 additions & 7 deletions packages/gateway/src/middlewares/signer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import {
Hex,
encodeAbiParameters,
parseAbiParameters,
toFunctionSelector,
} from 'viem'
import { privateKeyToAccount } from 'viem/accounts'

Expand All @@ -18,18 +17,14 @@ import { privateKeyToAccount } from 'viem/accounts'
* @param {string[]} function signatures that the middleware should be applied to
* @return {*}
*/
export function withSigner(privateKey: Hex, selectors: string[]) {
export function withSigner(privateKey: Hex) {
return mung.jsonAsync(
async (body: Record<string, unknown>, req: HTTPRequest) => {
const sender = req.method === 'GET' ? req.params.sender : req.body.sender
const callData =
req.method === 'GET' ? req.params.callData : req.body.data

if (
!callData ||
!selectors.map(toFunctionSelector).includes(callData.slice(0, 10)) ||
body.data === '0x'
) {
if (!callData || body.data === '0x' || !body.ttl) {
return body
}

Expand Down
21 changes: 0 additions & 21 deletions packages/gateway/src/server.ts

This file was deleted.

70 changes: 40 additions & 30 deletions packages/gateway/test/api.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,9 @@ import {
} from 'viem'
import { generatePrivateKey, privateKeyToAddress } from 'viem/accounts'
import request from 'supertest'
import * as ccip from '@blockful/ccip-server'

import { NewServer, abi as serverAbi } from '../src/server'
import { abi as serverAbi } from '../src/abi'
import { serializeTypedSignature, signData } from './helper'
import {
withGetAddr,
Expand All @@ -32,7 +33,7 @@ import {
withSetText,
} from '../src/handlers'
import { InMemoryRepository } from '../src/repositories'
import { withSigner, makeMessageHash } from '../src/middlewares'
import { withSigner, makeMessageHash, withLogger } from '../src/middlewares'
import { Domain } from '../src/entities'
import { OwnershipValidator } from '../src/services'

Expand Down Expand Up @@ -74,7 +75,9 @@ describe('Gateway API', () => {
const contenthash =
'0x1e583a944ea6750b0904b8f95a72f593f070ecac52e8d5bc959fa38d745a3909' // blockful

const app = NewServer(withSetContentHash(repo, validator)).makeApp('/')
const server = new ccip.Server()
server.add(serverAbi, withSetContentHash(repo, validator))
const app = server.makeApp('/')

const args = [domain.node, contenthash]
const data = encodeFunctionData({
Expand Down Expand Up @@ -110,10 +113,10 @@ describe('Gateway API', () => {
})

it('should handle GET contenthash', async () => {
const app = NewServer(withGetContentHash(repo)).makeApp(
'/',
withSigner(privateKey, ['function contenthash(bytes32 node)']),
)
const server = new ccip.Server()
server.app.use(withSigner(privateKey))
server.add(serverAbi, withGetContentHash(repo))
const app = server.makeApp('/')

const calldata = encodeFunctionData({
abi,
Expand Down Expand Up @@ -156,7 +159,9 @@ describe('Gateway API', () => {
it('should handle request for set new text', async () => {
const key = 'company'
const value = 'blockful'
const app = NewServer(withSetText(repo, validator)).makeApp('/')
const server = new ccip.Server()
server.add(serverAbi, withSetText(repo, validator))
const app = server.makeApp('/')

const args = [domain.node, key, value]
const data = encodeFunctionData({
Expand Down Expand Up @@ -203,7 +208,9 @@ describe('Gateway API', () => {
value: 'blockful',
},
])
const app = NewServer(withSetText(repo, validator)).makeApp('/')
const server = new ccip.Server()
server.add(serverAbi, withSetText(repo, validator))
const app = server.makeApp('/')

const args = [domain.node, key, value]
const data = encodeFunctionData({
Expand Down Expand Up @@ -249,10 +256,10 @@ describe('Gateway API', () => {
value,
},
])
const app = NewServer(withGetText(repo)).makeApp(
'/',
withSigner(privateKey, ['function text(bytes32 node, string key)']),
)
const server = new ccip.Server()
server.app.use(withSigner(privateKey))
server.add(serverAbi, withGetText(repo))
const app = server.makeApp('/')

const calldata = encodeFunctionData({
abi,
Expand Down Expand Up @@ -290,10 +297,11 @@ describe('Gateway API', () => {
})

it('should handle GET request for not existing text', async () => {
const app = NewServer(withGetText(repo)).makeApp(
'/',
withSigner(privateKey, ['function text(bytes32 node, string key)']),
)
const server = new ccip.Server()
server.app.use(withSigner(privateKey))
server.app.use(withLogger({ abi: serverAbi }))
server.add(serverAbi, withGetText(repo))
const app = server.makeApp('/')

const calldata = encodeFunctionData({
abi,
Expand All @@ -315,7 +323,9 @@ describe('Gateway API', () => {

it('should handle set request for setAddr on ethereum', async () => {
const address = privateKeyToAddress(privateKey)
const app = NewServer(withSetAddr(repo, validator)).makeApp('/')
const server = new ccip.Server()
server.add(serverAbi, withSetAddr(repo, validator))
const app = server.makeApp('/')

const args = [domain.node, address]
const data = encodeFunctionData({
Expand Down Expand Up @@ -359,10 +369,10 @@ describe('Gateway API', () => {
},
])
const address = privateKeyToAddress(privateKey)
const app = NewServer(withSetAddr(repo, validator)).makeApp(
'/',
withSigner(privateKey, []),
)
const server = new ccip.Server()
server.app.use(withSigner(privateKey))
server.add(serverAbi, withSetAddr(repo, validator))
const app = server.makeApp('/')

const args = [domain.node, address]
const data = encodeFunctionData({
Expand Down Expand Up @@ -406,10 +416,10 @@ describe('Gateway API', () => {
address,
},
])
const app = NewServer(withGetAddr(repo)).makeApp(
'/',
withSigner(privateKey, ['function addr(bytes32 node)']),
)
const server = new ccip.Server()
server.app.use(withSigner(privateKey))
server.add(serverAbi, withGetAddr(repo))
const app = server.makeApp('/')

const calldata = encodeFunctionData({
abi,
Expand Down Expand Up @@ -447,10 +457,10 @@ describe('Gateway API', () => {
})

it('should handle GET request for invalid address ', async () => {
const app = NewServer(withGetAddr(repo)).makeApp(
'/',
withSigner(privateKey, ['function addr(bytes32 node)']),
)
const server = new ccip.Server()
server.app.use(withSigner(privateKey))
server.add(serverAbi, withGetAddr(repo))
const app = server.makeApp('/')

const calldata = encodeFunctionData({
abi,
Expand Down
Loading

0 comments on commit e05a8fb

Please sign in to comment.