Skip to content

Commit

Permalink
feat: replace address with ens name as id
Browse files Browse the repository at this point in the history
  • Loading branch information
hai-ko committed Jan 27, 2023
1 parent 0be4ba6 commit d9d021c
Show file tree
Hide file tree
Showing 74 changed files with 1,105 additions and 1,182 deletions.
4 changes: 3 additions & 1 deletion .eslintignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
package.json
**/dist
**/build
**/*.json
**/*.json
**/coverage
**/dist.*
7 changes: 6 additions & 1 deletion .prettierignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,7 @@
**/dist
**/build
**/build
**/coverage
**/dist.*
**/typechain
**/artifacts
**/*.json
32 changes: 5 additions & 27 deletions packages/backend/src/auth.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,35 +20,13 @@ describe('Auth', () => {

describe('getChallenge', () => {
describe('schema', () => {
it('Returns 400 if schema is invalid', async () => {
const app = express();
app.use(bodyParser.json());
app.use(auth());

app.locals.db = {
getSession: async (accountAddress: string) => ({
challenge: '123',
}),
};

app.locals.storeSession = async (
accountAddress: string,
session: any,
) => {
return (_: any, __: any, ___: any) => {};
};

const { status } = await request(app).get('/890').send();

expect(status).toBe(400);
});
it('Returns 200 if schema is valid', async () => {
const app = express();
app.use(bodyParser.json());
app.use(auth());

app.locals.db = {
getSession: async (accountAddress: string) => ({
getSession: async (ensName: string) => ({
challenge: '123',
}),
setSession: async (_: string, __: any) => {
Expand All @@ -73,7 +51,7 @@ describe('Auth', () => {
app.use(auth());

app.locals.db = {
getSession: async (accountAddress: string) => ({
getSession: async (ensName: string) => ({
challenge: '123',
}),
setSession: async (_: string, __: any) => {
Expand All @@ -88,7 +66,7 @@ describe('Auth', () => {
const signature = await wallet.signMessage('123');

const { status } = await request(app).post(`/1234`).send({
signature,
signature: 123,
});

expect(status).toBe(400);
Expand All @@ -99,7 +77,7 @@ describe('Auth', () => {
app.use(auth());

app.locals.db = {
getSession: async (accountAddress: string) => ({
getSession: async (ensName: string) => ({
challenge: '123',
}),
setSession: async (_: string, __: any) => {
Expand Down Expand Up @@ -127,7 +105,7 @@ describe('Auth', () => {
app.use(auth());

app.locals.db = {
getSession: async (accountAddress: string) => ({
getSession: async (ensName: string) => ({
challenge: 'my-Challenge',
signedUserProfile: {
profile: {
Expand Down
37 changes: 16 additions & 21 deletions packages/backend/src/auth.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,23 @@
import * as Lib from 'dm3-lib/dist.backend';
import express from 'express';
import cors from 'cors';
import { ethers } from 'ethers';
import { WithLocals } from './types';

const getChallengeSchema = {
type: 'object',
properties: {
address: { type: 'string' },
ensName: { type: 'string' },
},
required: ['address'],
required: ['ensName'],
additionalProperties: false,
};

const createNewSessionTokenParamsSchema = {
type: 'object',
properties: {
address: { type: 'string' },
ensName: { type: 'string' },
},
required: ['address'],
required: ['ensName'],
additionalProperties: false,
};

Expand All @@ -38,27 +37,25 @@ export default () => {
router.use(cors());

router.get(
'/:address',
'/:ensName',
async (req: express.Request & { app: WithLocals }, res, next) => {
try {
const ensName = Lib.account.normalizeEnsName(
req.params.ensName,
);
const schemaIsValid = Lib.validateSchema(
getChallengeSchema,
req.params,
);

if (
!schemaIsValid ||
!ethers.utils.isAddress(req.params.address)
) {
if (!schemaIsValid) {
return res.send(400);
}

const account = Lib.external.formatAddress(req.params.address);

const challenge = await Lib.delivery.createChallenge(
req.app.locals.db.getSession,
req.app.locals.db.setSession,
account,
ensName,
);

res.json({
Expand All @@ -71,9 +68,12 @@ export default () => {
);

router.post(
'/:address',
'/:ensName',
async (req: express.Request & { app: WithLocals }, res, next) => {
try {
const ensName = Lib.account.normalizeEnsName(
req.params.ensName,
);
const paramsAreValid = Lib.validateSchema(
createNewSessionTokenParamsSchema,
req.params,
Expand All @@ -86,20 +86,15 @@ export default () => {

const schemaIsValid = paramsAreValid && bodyIsValid;

if (
!schemaIsValid ||
!ethers.utils.isAddress(req.params.address)
) {
if (!schemaIsValid) {
return res.send(400);
}

const account = Lib.external.formatAddress(req.params.address);

const token = await Lib.delivery.createNewSessionToken(
req.app.locals.db.getSession,
req.app.locals.db.setSession,
req.body.signature,
account,
ensName,
);

res.json({
Expand Down
105 changes: 30 additions & 75 deletions packages/backend/src/delivery.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import bodyParser from 'body-parser';
import { ethers } from 'ethers';
import express from 'express';
import request from 'supertest';
import auth from './auth';
Expand All @@ -25,18 +24,22 @@ describe('Delivery', () => {
const app = express();
app.use(bodyParser.json());
app.use(delivery());
(app.locals.keys = {
signing: keysA.signingKeyPair,
encryption: keysA.encryptionKeyPair,
(app.locals.web3Provider = {
resolveName: async () =>
'0x99C19AB10b9EC8aC6fcda9586E81f6B73a298870',
}),
(app.locals.keys = {
signing: keysA.signingKeyPair,
encryption: keysA.encryptionKeyPair,
}),
(app.locals.redisClient = {
exists: (_: any) => false,
});

const token = await createAuthToken();

app.locals.db = {
getSession: async (accountAddress: string) =>
getSession: async (ensName: string) =>
Promise.resolve({
challenge: 'my-Challenge',
signedUserProfile: {
Expand All @@ -54,10 +57,7 @@ describe('Delivery', () => {
};

const { status } = await request(app)
.get(
// eslint-disable-next-line max-len
'/messages/0x99C19AB10b9EC8aC6fcda9586E81f6B73a298870/contact/0x99C19AB10b9EC8aC6fcda9586E81f6B73a298870',
)
.get('/messages/alice.eth/contact/bob.eth')
.set({
authorization: `Bearer ${token}`,
})
Expand All @@ -66,34 +66,6 @@ describe('Delivery', () => {

expect(status).toBe(200);
});
it('Returns 400 if schema is invalid', async () => {
const app = express();
app.use(bodyParser.json());
app.use(delivery());

const token = await createAuthToken();

app.locals.db = {
getSession: async (accountAddress: string) => ({
challenge: '123',
token,
}),
setSession: async (_: string, __: any) => {
return (_: any, __: any, ___: any) => {};
},
getPending: (_: any) => [],
};

const { status } = await request(app)
.get('/messages/01234/contact/5679')
.set({
authorization: `Bearer ${token}`,
})

.send();

expect(status).toBe(400);
});
});

describe('getPendingMessages', () => {
Expand All @@ -107,11 +79,15 @@ describe('Delivery', () => {
sMembers: (_: any) => [],
del: (_: any) => {},
};
app.locals.web3Provider = {
resolveName: async () =>
'0x99C19AB10b9EC8aC6fcda9586E81f6B73a298870',
};

const token = await createAuthToken();

app.locals.db = {
getSession: async (accountAddress: string) => ({
getSession: async (ensName: string) => ({
challenge: '123',
token,
}),
Expand All @@ -134,39 +110,6 @@ describe('Delivery', () => {

expect(status).toBe(200);
});
it('Returns 400 if schema is invalid', async () => {
const app = express();
app.use(bodyParser.json());
app.use(delivery());

app.locals.redisClient = {
exists: (_: any) => false,
sMembers: (_: any) => [],
del: (_: any) => {},
};

const token = await createAuthToken();

app.locals.db = {
getSession: async (accountAddress: string) => ({
challenge: '123',
token,
}),
setSession: async (_: string, __: any) => {
return (_: any, __: any, ___: any) => {};
},
};

const { status } = await request(app)
.post('/messages/1234/pending')
.set({
authorization: `Bearer ${token}`,
})

.send();

expect(status).toBe(400);
});
});

describe('syncAcknoledgment', () => {
Expand All @@ -183,11 +126,15 @@ describe('Delivery', () => {
hGetAll: () => ['123', '456'],
zRemRangeByScore: (_: any, __: any, ___: any) => 0,
};
app.locals.web3Provider = {
resolveName: async () =>
'0x99C19AB10b9EC8aC6fcda9586E81f6B73a298870',
};

const token = await createAuthToken();

app.locals.db = {
getSession: async (accountAddress: string) => ({
getSession: async (ensName: string) => ({
challenge: '123',
token,
}),
Expand Down Expand Up @@ -226,11 +173,15 @@ describe('Delivery', () => {
sMembers: (_: any) => [],
del: (_: any) => {},
};
app.locals.web3Provider = {
resolveName: async () =>
'0x99C19AB10b9EC8aC6fcda9586E81f6B73a298870',
};

const token = await createAuthToken();

app.locals.db = {
getSession: async (accountAddress: string) => ({
getSession: async (ensName: string) => ({
challenge: '123',
token,
}),
Expand Down Expand Up @@ -263,11 +214,15 @@ describe('Delivery', () => {
sMembers: (_: any) => [],
del: (_: any) => {},
};
app.locals.web3Provider = {
resolveName: async () =>
'0x99C19AB10b9EC8aC6fcda9586E81f6B73a298870',
};

const token = await createAuthToken();

app.locals.db = {
getSession: async (accountAddress: string) => ({
getSession: async (ensName: string) => ({
challenge: '123',
token,
}),
Expand Down Expand Up @@ -303,7 +258,7 @@ const createAuthToken = async () => {
};

app.locals.db = {
getSession: async (accountAddress: string) => ({
getSession: async (ensName: string) => ({
challenge: 'my-Challenge',
signedUserProfile: {
profile: {
Expand Down
Loading

0 comments on commit d9d021c

Please sign in to comment.