Skip to content

Commit

Permalink
Add local signature server & update scripts
Browse files Browse the repository at this point in the history
  • Loading branch information
raducristianpopa committed Dec 19, 2023
1 parent 073278f commit 120982c
Show file tree
Hide file tree
Showing 15 changed files with 939 additions and 106 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/pr-checks.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ jobs:
uses: ./.github/actions/setup

- name: Build
run: pnpm build:${{ matrix.browser }}
run: pnpm build ${{ matrix.browser }}

- name: Upload artifacts
uses: actions/[email protected]
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ To build the extension for production, use the following command:

To build the extension for Firefox, use the following command:

`pnpm build:firefox`
`pnpm build firefox`

This command transpiles the TypeScript code and generates a production-ready build of the extension in the dist
directory.
Expand Down
99 changes: 99 additions & 0 deletions local-signatures/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
import {
createHeaders,
Headers,
loadBase64Key,
RequestLike,
} from '@interledger/http-signature-utils'
import Koa from 'koa'
import bodyParser from 'koa-bodyparser'

interface Context<TResponseBody = unknown>
extends Koa.ParameterizedContext<Koa.DefaultState, Koa.DefaultContext, TResponseBody> {}

interface GenerateSignatureRequestBody extends RequestLike {}

function validateBody(body: any): body is GenerateSignatureRequestBody {
return !!body.headers && !!body.method && !!body.url
}

async function validatePath(ctx: Context, next: Koa.Next): Promise<void> {
if (ctx.path !== '/') {
ctx.status = 404
} else {
await next()
}
}

async function validateMethod(ctx: Context, next: Koa.Next): Promise<void> {
if (ctx.method !== 'POST') {
ctx.status = 405
} else {
await next()
}
}

async function createHeadersHandler(ctx: Context<Headers>): Promise<void> {
const { body } = ctx.request

if (!validateBody(body)) {
ctx.throw('Invalid request body', 400)
}

let privateKey: ReturnType<typeof loadBase64Key>

try {
privateKey = loadBase64Key(BASE64_PRIVATE_KEY)
} catch {
ctx.throw('Not a valid private key', 400)
}

if (privateKey === undefined) {
ctx.throw('Not an Ed25519 private key', 400)
}

const headers = await createHeaders({
request: body,
privateKey,
keyId: KEY_ID,
})

delete headers['Content-Length']
delete headers['Content-Type']

ctx.body = headers
}

const PORT = 3000
const BASE64_PRIVATE_KEY =
'LS0tLS1CRUdJTiBQUklWQVRFIEtFWS0tLS0tCk1DNENBUUF3QlFZREsyVndCQ0lFSUUvVlJTRVUzYS9CTUE2cmhUQnZmKzcxMG10YWlmbkF6SzFsWGpDK0QrSTkKLS0tLS1FTkQgUFJJVkFURSBLRVktLS0tLQ=='
const KEY_ID = 'f0ac2190-54d5-47c8-b061-221e7068d823'

const app = new Koa<Koa.DefaultState, Context>()

app.use(bodyParser())
app.use(validatePath)
app.use(validateMethod)
app.use(createHeadersHandler)

// app.use(async (ctx: Context<Headers>) => {
// const { body } = ctx.request
// if (!validateBody(body)) {
// ctx.throw('Invalid request body', 400)
// }

// const headers = await createHeaders({
// request: body,
// privateKey: BASE64_PRIVATE_KEY,
// keyId: KEY_ID,
// })

// delete headers['Content-Length']
// delete headers['Content-Type']

// ctx.body = headers
// })

app.listen(3000, () => {
// eslint-disable-next-line no-console
console.log(`Local signatures server started on port ${PORT}`)
})
25 changes: 11 additions & 14 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,24 +9,15 @@
"url": "https://github.com/interledger/web-monetization-extension"
},
"scripts": {
"dev:chrome": "NODE_ENV=development TARGET=chrome webpack",
"dev:firefox": "NODE_ENV=development TARGET=firefox webpack",
"dev:opera": "NODE_ENV=development TARGET=opera webpack",
"dev:edge": "NODE_ENV=development TARGET=edge webpack",
"profile:chrome": "NODE_ENV=profile TARGET=chrome webpack",
"profile:firefox": "NODE_ENV=profile TARGET=firefox webpack",
"profile:opera": "NODE_ENV=profile TARGET=opera webpack",
"profile:edge": "NODE_ENV=profile TARGET=edge webpack",
"build:chrome": "NODE_ENV=production TARGET=chrome webpack",
"build:firefox": "NODE_ENV=production TARGET=firefox webpack",
"build:opera": "NODE_ENV=production TARGET=opera webpack",
"build:edge": "NODE_ENV=production TARGET=edge webpack",
"build": "concurrently \"pnpm:build:*\"",
"analyze": "sh ./scripts/analyze.sh",
"build": "sh ./scripts/build.sh",
"dev": "sh ./scripts/dev.sh",
"lint": "concurrently \"lint:*\"",
"lint:fix": "eslint --ext js,jsx,ts,tsx, src --fix",
"lint:eslint": "eslint . --ext .js,.ts,.tsx --max-warnings 0 --ignore-path .gitignore",
"lint:prettier": "prettier \"**/*.(md|json|yml)\" --ignore-path .gitignore --check",
"lint:type": "tsc --noEmit",
"local-signatures": "pnpm tsx --watch ./local-signatures/index.ts",
"test": "jest --maxWorkers=2",
"ci:test": "run-s \" test --ci --reporters=\"default\" --reporters=\"github-actions\" \""
},
Expand All @@ -46,7 +37,6 @@
"sass-loader": "^13.3.2",
"style-loader": "^3.3.3",
"terser-webpack-plugin": "^5.3.9",
"ts-node": "^10.9.1",
"uuid": "^9.0.1",
"webextension-polyfill": "^0.10.0",
"webpack": "^5.88.2",
Expand All @@ -56,12 +46,15 @@
"zip-webpack-plugin": "^4.0.1"
},
"devDependencies": {
"@interledger/http-signature-utils": "^2.0.0",
"@testing-library/jest-dom": "^6.1.3",
"@testing-library/react": "^14.0.0",
"@types/chrome": "^0.0.244",
"@types/inboxsdk": "^2.0.11",
"@types/jest": "^29.5.5",
"@types/jquery": "^3.5.18",
"@types/koa": "^2.13.12",
"@types/koa-bodyparser": "^4.3.12",
"@types/lodash": "^4.14.197",
"@types/node": "^20.8.4",
"@types/react": "^18.2.21",
Expand Down Expand Up @@ -94,8 +87,12 @@
"jest-chrome": "^0.8.0",
"jest-environment-jsdom": "^29.7.0",
"jest-transform-stub": "^2.0.0",
"koa": "^2.14.2",
"koa-bodyparser": "^4.4.1",
"prettier": "^3.0.3",
"ts-loader": "^9.4.4",
"ts-node": "^10.9.2",
"tsx": "^4.6.2",
"typescript": "^5.2.2",
"webpack-cli": "^5.1.4"
},
Expand Down
Loading

0 comments on commit 120982c

Please sign in to comment.