Skip to content

Commit d9d021c

Browse files
committed
feat: replace address with ens name as id
1 parent 0be4ba6 commit d9d021c

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

74 files changed

+1105
-1182
lines changed

.eslintignore

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
package.json
22
**/dist
33
**/build
4-
**/*.json
4+
**/*.json
5+
**/coverage
6+
**/dist.*

.prettierignore

+6-1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,7 @@
11
**/dist
2-
**/build
2+
**/build
3+
**/coverage
4+
**/dist.*
5+
**/typechain
6+
**/artifacts
7+
**/*.json

packages/backend/src/auth.test.ts

+5-27
Original file line numberDiff line numberDiff line change
@@ -20,35 +20,13 @@ describe('Auth', () => {
2020

2121
describe('getChallenge', () => {
2222
describe('schema', () => {
23-
it('Returns 400 if schema is invalid', async () => {
24-
const app = express();
25-
app.use(bodyParser.json());
26-
app.use(auth());
27-
28-
app.locals.db = {
29-
getSession: async (accountAddress: string) => ({
30-
challenge: '123',
31-
}),
32-
};
33-
34-
app.locals.storeSession = async (
35-
accountAddress: string,
36-
session: any,
37-
) => {
38-
return (_: any, __: any, ___: any) => {};
39-
};
40-
41-
const { status } = await request(app).get('/890').send();
42-
43-
expect(status).toBe(400);
44-
});
4523
it('Returns 200 if schema is valid', async () => {
4624
const app = express();
4725
app.use(bodyParser.json());
4826
app.use(auth());
4927

5028
app.locals.db = {
51-
getSession: async (accountAddress: string) => ({
29+
getSession: async (ensName: string) => ({
5230
challenge: '123',
5331
}),
5432
setSession: async (_: string, __: any) => {
@@ -73,7 +51,7 @@ describe('Auth', () => {
7351
app.use(auth());
7452

7553
app.locals.db = {
76-
getSession: async (accountAddress: string) => ({
54+
getSession: async (ensName: string) => ({
7755
challenge: '123',
7856
}),
7957
setSession: async (_: string, __: any) => {
@@ -88,7 +66,7 @@ describe('Auth', () => {
8866
const signature = await wallet.signMessage('123');
8967

9068
const { status } = await request(app).post(`/1234`).send({
91-
signature,
69+
signature: 123,
9270
});
9371

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

10179
app.locals.db = {
102-
getSession: async (accountAddress: string) => ({
80+
getSession: async (ensName: string) => ({
10381
challenge: '123',
10482
}),
10583
setSession: async (_: string, __: any) => {
@@ -127,7 +105,7 @@ describe('Auth', () => {
127105
app.use(auth());
128106

129107
app.locals.db = {
130-
getSession: async (accountAddress: string) => ({
108+
getSession: async (ensName: string) => ({
131109
challenge: 'my-Challenge',
132110
signedUserProfile: {
133111
profile: {

packages/backend/src/auth.ts

+16-21
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,23 @@
11
import * as Lib from 'dm3-lib/dist.backend';
22
import express from 'express';
33
import cors from 'cors';
4-
import { ethers } from 'ethers';
54
import { WithLocals } from './types';
65

76
const getChallengeSchema = {
87
type: 'object',
98
properties: {
10-
address: { type: 'string' },
9+
ensName: { type: 'string' },
1110
},
12-
required: ['address'],
11+
required: ['ensName'],
1312
additionalProperties: false,
1413
};
1514

1615
const createNewSessionTokenParamsSchema = {
1716
type: 'object',
1817
properties: {
19-
address: { type: 'string' },
18+
ensName: { type: 'string' },
2019
},
21-
required: ['address'],
20+
required: ['ensName'],
2221
additionalProperties: false,
2322
};
2423

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

4039
router.get(
41-
'/:address',
40+
'/:ensName',
4241
async (req: express.Request & { app: WithLocals }, res, next) => {
4342
try {
43+
const ensName = Lib.account.normalizeEnsName(
44+
req.params.ensName,
45+
);
4446
const schemaIsValid = Lib.validateSchema(
4547
getChallengeSchema,
4648
req.params,
4749
);
4850

49-
if (
50-
!schemaIsValid ||
51-
!ethers.utils.isAddress(req.params.address)
52-
) {
51+
if (!schemaIsValid) {
5352
return res.send(400);
5453
}
5554

56-
const account = Lib.external.formatAddress(req.params.address);
57-
5855
const challenge = await Lib.delivery.createChallenge(
5956
req.app.locals.db.getSession,
6057
req.app.locals.db.setSession,
61-
account,
58+
ensName,
6259
);
6360

6461
res.json({
@@ -71,9 +68,12 @@ export default () => {
7168
);
7269

7370
router.post(
74-
'/:address',
71+
'/:ensName',
7572
async (req: express.Request & { app: WithLocals }, res, next) => {
7673
try {
74+
const ensName = Lib.account.normalizeEnsName(
75+
req.params.ensName,
76+
);
7777
const paramsAreValid = Lib.validateSchema(
7878
createNewSessionTokenParamsSchema,
7979
req.params,
@@ -86,20 +86,15 @@ export default () => {
8686

8787
const schemaIsValid = paramsAreValid && bodyIsValid;
8888

89-
if (
90-
!schemaIsValid ||
91-
!ethers.utils.isAddress(req.params.address)
92-
) {
89+
if (!schemaIsValid) {
9390
return res.send(400);
9491
}
9592

96-
const account = Lib.external.formatAddress(req.params.address);
97-
9893
const token = await Lib.delivery.createNewSessionToken(
9994
req.app.locals.db.getSession,
10095
req.app.locals.db.setSession,
10196
req.body.signature,
102-
account,
97+
ensName,
10398
);
10499

105100
res.json({

packages/backend/src/delivery.test.ts

+30-75
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import bodyParser from 'body-parser';
2-
import { ethers } from 'ethers';
32
import express from 'express';
43
import request from 'supertest';
54
import auth from './auth';
@@ -25,18 +24,22 @@ describe('Delivery', () => {
2524
const app = express();
2625
app.use(bodyParser.json());
2726
app.use(delivery());
28-
(app.locals.keys = {
29-
signing: keysA.signingKeyPair,
30-
encryption: keysA.encryptionKeyPair,
27+
(app.locals.web3Provider = {
28+
resolveName: async () =>
29+
'0x99C19AB10b9EC8aC6fcda9586E81f6B73a298870',
3130
}),
31+
(app.locals.keys = {
32+
signing: keysA.signingKeyPair,
33+
encryption: keysA.encryptionKeyPair,
34+
}),
3235
(app.locals.redisClient = {
3336
exists: (_: any) => false,
3437
});
3538

3639
const token = await createAuthToken();
3740

3841
app.locals.db = {
39-
getSession: async (accountAddress: string) =>
42+
getSession: async (ensName: string) =>
4043
Promise.resolve({
4144
challenge: 'my-Challenge',
4245
signedUserProfile: {
@@ -54,10 +57,7 @@ describe('Delivery', () => {
5457
};
5558

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

6767
expect(status).toBe(200);
6868
});
69-
it('Returns 400 if schema is invalid', async () => {
70-
const app = express();
71-
app.use(bodyParser.json());
72-
app.use(delivery());
73-
74-
const token = await createAuthToken();
75-
76-
app.locals.db = {
77-
getSession: async (accountAddress: string) => ({
78-
challenge: '123',
79-
token,
80-
}),
81-
setSession: async (_: string, __: any) => {
82-
return (_: any, __: any, ___: any) => {};
83-
},
84-
getPending: (_: any) => [],
85-
};
86-
87-
const { status } = await request(app)
88-
.get('/messages/01234/contact/5679')
89-
.set({
90-
authorization: `Bearer ${token}`,
91-
})
92-
93-
.send();
94-
95-
expect(status).toBe(400);
96-
});
9769
});
9870

9971
describe('getPendingMessages', () => {
@@ -107,11 +79,15 @@ describe('Delivery', () => {
10779
sMembers: (_: any) => [],
10880
del: (_: any) => {},
10981
};
82+
app.locals.web3Provider = {
83+
resolveName: async () =>
84+
'0x99C19AB10b9EC8aC6fcda9586E81f6B73a298870',
85+
};
11086

11187
const token = await createAuthToken();
11288

11389
app.locals.db = {
114-
getSession: async (accountAddress: string) => ({
90+
getSession: async (ensName: string) => ({
11591
challenge: '123',
11692
token,
11793
}),
@@ -134,39 +110,6 @@ describe('Delivery', () => {
134110

135111
expect(status).toBe(200);
136112
});
137-
it('Returns 400 if schema is invalid', async () => {
138-
const app = express();
139-
app.use(bodyParser.json());
140-
app.use(delivery());
141-
142-
app.locals.redisClient = {
143-
exists: (_: any) => false,
144-
sMembers: (_: any) => [],
145-
del: (_: any) => {},
146-
};
147-
148-
const token = await createAuthToken();
149-
150-
app.locals.db = {
151-
getSession: async (accountAddress: string) => ({
152-
challenge: '123',
153-
token,
154-
}),
155-
setSession: async (_: string, __: any) => {
156-
return (_: any, __: any, ___: any) => {};
157-
},
158-
};
159-
160-
const { status } = await request(app)
161-
.post('/messages/1234/pending')
162-
.set({
163-
authorization: `Bearer ${token}`,
164-
})
165-
166-
.send();
167-
168-
expect(status).toBe(400);
169-
});
170113
});
171114

172115
describe('syncAcknoledgment', () => {
@@ -183,11 +126,15 @@ describe('Delivery', () => {
183126
hGetAll: () => ['123', '456'],
184127
zRemRangeByScore: (_: any, __: any, ___: any) => 0,
185128
};
129+
app.locals.web3Provider = {
130+
resolveName: async () =>
131+
'0x99C19AB10b9EC8aC6fcda9586E81f6B73a298870',
132+
};
186133

187134
const token = await createAuthToken();
188135

189136
app.locals.db = {
190-
getSession: async (accountAddress: string) => ({
137+
getSession: async (ensName: string) => ({
191138
challenge: '123',
192139
token,
193140
}),
@@ -226,11 +173,15 @@ describe('Delivery', () => {
226173
sMembers: (_: any) => [],
227174
del: (_: any) => {},
228175
};
176+
app.locals.web3Provider = {
177+
resolveName: async () =>
178+
'0x99C19AB10b9EC8aC6fcda9586E81f6B73a298870',
179+
};
229180

230181
const token = await createAuthToken();
231182

232183
app.locals.db = {
233-
getSession: async (accountAddress: string) => ({
184+
getSession: async (ensName: string) => ({
234185
challenge: '123',
235186
token,
236187
}),
@@ -263,11 +214,15 @@ describe('Delivery', () => {
263214
sMembers: (_: any) => [],
264215
del: (_: any) => {},
265216
};
217+
app.locals.web3Provider = {
218+
resolveName: async () =>
219+
'0x99C19AB10b9EC8aC6fcda9586E81f6B73a298870',
220+
};
266221

267222
const token = await createAuthToken();
268223

269224
app.locals.db = {
270-
getSession: async (accountAddress: string) => ({
225+
getSession: async (ensName: string) => ({
271226
challenge: '123',
272227
token,
273228
}),
@@ -303,7 +258,7 @@ const createAuthToken = async () => {
303258
};
304259

305260
app.locals.db = {
306-
getSession: async (accountAddress: string) => ({
261+
getSession: async (ensName: string) => ({
307262
challenge: 'my-Challenge',
308263
signedUserProfile: {
309264
profile: {

0 commit comments

Comments
 (0)