Skip to content

Commit

Permalink
fix: Adjust tests a little and remove the cloudflare sign thing.
Browse files Browse the repository at this point in the history
  • Loading branch information
richtera committed Nov 4, 2024
1 parent 0a27f2d commit a00efcb
Show file tree
Hide file tree
Showing 8 changed files with 401 additions and 28 deletions.
15 changes: 10 additions & 5 deletions jest.config.cjs
Original file line number Diff line number Diff line change
@@ -1,14 +1,19 @@
const esModules = ['@lukso/lsp-smart-contracts', '@lukso/lsp-factory.js'].join(
'|'
)
const esModules = [
'@lukso/lsp-smart-contracts',
'@lukso/lsp-factory.js',
'@web3-onboard',
'nanoid',
'@lukso/web3-onboard-config',
'@tsndr/cloudflare-worker-jwt',
].join('|')

module.exports = {
transform: {
'^.+\\.vue$': '@vue/vue3-jest',
'^.+\\.tsx?$': ['ts-jest', { tsconfig: '<rootDir>/tsconfig.json' }],
'^.+\\.(t|j)sx?$': ['ts-jest', { tsconfig: '<rootDir>/tsconfig.json' }],
},
setupFilesAfterEnv: ['<rootDir>/tests/setup-env.ts'],
transformIgnorePatterns: [`/node_modules/(?!${esModules})`],
transformIgnorePatterns: [`/node_modules/(?!(${esModules}))`],
testEnvironment: 'jsdom',
moduleFileExtensions: ['js', 'ts', 'json', 'vue'],
moduleNameMapper: {
Expand Down
3 changes: 3 additions & 0 deletions jest.setup.cjs
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
module.exports = async () => {
process.env.TZ = 'UTC'

global.crypto = require('isomorphic-webcrypto')
console.log(global.crypto)
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
"@lukso/up-provider": "^0.1.3",
"@lukso/web3-onboard-config": "1.1.2",
"@pinata/sdk": "^2.1.0",
"@tsndr/cloudflare-worker-jwt": "^2.5.3",
"@types/isomorphic-fetch": "^0.0.39",
"@walletconnect/ethereum-provider": "2.16.0",
"@walletconnect/sign-client": "2.16.0",
Expand All @@ -41,6 +40,7 @@
"filesize": "10.0.12",
"https-browserify": "1.0.0",
"isomorphic-fetch": "^3.0.0",
"isomorphic-webcrypto": "^2.3.8",
"siwe": "1.1.6",
"tslib": "^2.6.2",
"vue": "^3.4.21",
Expand Down
4 changes: 4 additions & 0 deletions src/components/endpoints/__tests__/Assets.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ jest.mock('@/compositions/useErc20', () => ({
}),
}))

jest.mock('@/helpers/env', () => ({
PUBLIC_API_SHARED_SECRET: '123',
}))

jest.mock('@/compositions/useWeb3Connection', () => ({
__esModule: true,
default: () => ({
Expand Down
70 changes: 62 additions & 8 deletions src/services/ipfs/authenticated-formdata-client.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,65 @@
import FormData from 'form-data'

import crypto from 'isomorphic-webcrypto'
import { FormDataPostHeaders } from '@/services/ipfs/formdata-base-client'
import { CustomHeaderFormDataUploader } from '@/services/ipfs/ipfs-formdata-clients'
import { PUBLIC_API_SHARED_SECRET } from '@/helpers/env'

async function sign(data: any, key: string) {
return crypto.subtle
.importKey(
'jwk', //can be "jwk" or "raw"
{
//this is an example jwk key, "raw" would be an ArrayBuffer
kty: 'oct',
k: key,
alg: 'HS256',
ext: true,
},
{
//this is the algorithm options
name: 'HMAC',
hash: { name: 'SHA-256' }, //can be "SHA-1", "SHA-256", "SHA-384", or "SHA-512"
//length: 256, //optional, if you want your key length to differ from the hash function's block length
},
true, //whether the key is extractable (i.e. can be used in exportKey)
['sign', 'verify'] //can be any combination of "sign" and "verify"
)
.then((key: any) => {
const jsonString = JSON.stringify(data)
const encodedData = new TextEncoder().encode(jsonString)
return crypto.subtle.sign(
{
name: 'HMAC',
},
key, //from generateKey or importKey above
encodedData //ArrayBuffer of data you want to sign
)
})
.then((token: any) => {
const u8 = new Uint8Array(token)
const str = String.fromCharCode.apply(
undefined,
u8 as unknown as number[]
)
return btoa(str)
})
}

/** local implementation of createToken to create a signd JWT token */
async function createToken(payload: any, key: string): Promise<string> {
const header = { typ: 'JWT', alg: 'HS256' }

const segments = [
encodeURIComponent(btoa(JSON.stringify(header))),
encodeURIComponent(btoa(JSON.stringify(payload))),
]

const footer = await sign(segments.join('.'), key)

segments.push(footer)

return segments.join('.')
}
export class AuthenticatedFormDataUploader extends CustomHeaderFormDataUploader {
// eslint-disable-next-line @typescript-eslint/no-unused-vars
async getHeaders(dataContent: FormData, meta?: FormDataPostHeaders) {
Expand All @@ -14,15 +70,13 @@ export class AuthenticatedFormDataUploader extends CustomHeaderFormDataUploader
return `ipfs://${result.IpfsHash}`
}
async getToken(): Promise<string> {
const sign = await import('@tsndr/cloudflare-worker-jwt').then(
m => m.sign || m.default?.sign
)

if (!PUBLIC_API_SHARED_SECRET) {
return ''
}
const now = Date.now()
const secret = PUBLIC_API_SHARED_SECRET
return await sign(
return await createToken(
{ iss: 'extension', iat: now / 1000, exp: (now + 120_000) / 1000 },
secret || ''
PUBLIC_API_SHARED_SECRET || ''
)
}
}
3 changes: 2 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@
"@/*": ["src/*"]
},
"types": ["vite/client", "jest", "node", "@testing-library/jest-dom"],
"lib": ["esnext", "dom", "dom.iterable", "scripthost"]
"lib": ["esnext", "dom", "dom.iterable", "scripthost"],
"isolatedModules": true
},
"include": [
"src/**/*.d.ts",
Expand Down
2 changes: 1 addition & 1 deletion tsconfig.node.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"compilerOptions": {
"composite": true,
"module": "ESNext",
"module": "esnext",
"moduleResolution": "Node",
"allowSyntheticDefaultImports": true
},
Expand Down
Loading

0 comments on commit a00efcb

Please sign in to comment.