@abtnode/auth 1.6.12 → 1.6.16

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/lib/auth.js CHANGED
@@ -13,12 +13,13 @@ const verifySignature = require('./util/verify-signature');
13
13
  const {
14
14
  createPassport,
15
15
  createPassportVC,
16
- createPassportSvg,
17
16
  upsertToPassports,
18
17
  createUserPassport,
19
18
  getRoleFromLocalPassport,
20
19
  } = require('./passport');
21
20
 
21
+ const createPassportSvg = require('./util/create-passport-svg');
22
+
22
23
  const messages = {
23
24
  description: {
24
25
  en: 'Login with your DID Wallet',
@@ -177,18 +178,21 @@ const getTeamInfo = async ({ node, nodeInfo, teamDid }) => {
177
178
  let name;
178
179
  let wallet;
179
180
  let description;
181
+ let passportColor;
180
182
 
181
183
  if (teamDid === nodeInfo.did) {
182
184
  name = nodeInfo.name;
183
185
  description = nodeInfo.description;
184
186
  wallet = getNodeWallet(nodeInfo.sk);
185
187
  type = 'node';
188
+ passportColor = 'default';
186
189
  } else {
187
190
  const blocklet = await node.getBlocklet({ did: teamDid, attachRuntimeInfo: false });
188
191
  const blockletInfo = getBlockletInfo(blocklet, nodeInfo.sk);
189
192
  name = blockletInfo.name;
190
193
  description = blockletInfo.description;
191
194
  wallet = blockletInfo.wallet;
195
+ passportColor = blockletInfo.passportColor;
192
196
  type = 'blocklet';
193
197
  }
194
198
 
@@ -197,6 +201,7 @@ const getTeamInfo = async ({ node, nodeInfo, teamDid }) => {
197
201
  name,
198
202
  description,
199
203
  wallet,
204
+ passportColor,
200
205
  did: teamDid,
201
206
  };
202
207
  };
@@ -255,7 +260,7 @@ const createInvitationRequest = async ({ node, nodeInfo, teamDid, inviteId, loca
255
260
  throw new Error('The invitation does not exist or has been used');
256
261
  }
257
262
 
258
- const { name: issuerName, wallet: issuerWallet } = await getTeamInfo({ node, nodeInfo, teamDid });
263
+ const { name: issuerName, wallet: issuerWallet, passportColor } = await getTeamInfo({ node, nodeInfo, teamDid });
259
264
 
260
265
  const passport = await createPassport({
261
266
  name: inviteInfo.role,
@@ -270,7 +275,13 @@ const createInvitationRequest = async ({ node, nodeInfo, teamDid, inviteId, loca
270
275
  type: 'mime:text/plain',
271
276
  display: JSON.stringify({
272
277
  type: 'svg',
273
- content: createPassportSvg({ issuer: issuerName, title: passport.title, issuerDid: issuerWallet.address }),
278
+ content: createPassportSvg({
279
+ issuer: issuerName,
280
+ title: passport.title,
281
+ issuerDid: issuerWallet.address,
282
+ ownerName: 'Your Name',
283
+ preferredColor: passportColor,
284
+ }),
274
285
  }),
275
286
  };
276
287
  };
@@ -294,11 +305,18 @@ const handleInvitationResponse = async ({
294
305
  const claim = claims.find((x) => x.type === 'signature');
295
306
  verifySignature(claim, userDid, userPk, locale);
296
307
 
297
- const { name: issuerName, wallet: issuerWallet, type: issuerType } = await getTeamInfo({ node, nodeInfo, teamDid });
308
+ const {
309
+ name: issuerName,
310
+ wallet: issuerWallet,
311
+ type: issuerType,
312
+ passportColor,
313
+ } = await getTeamInfo({ node, nodeInfo, teamDid });
298
314
 
299
315
  const inviteInfo = await node.processInvitation({ teamDid, inviteId });
300
316
  const { remark } = inviteInfo;
301
317
 
318
+ const profile = claims.find((x) => x.type === 'profile');
319
+
302
320
  const vcParams = {
303
321
  issuerName,
304
322
  issuerWallet,
@@ -316,6 +334,8 @@ const handleInvitationResponse = async ({
316
334
  teamDid,
317
335
  }),
318
336
  types: teamDid === nodeInfo.did ? [NFT_TYPE_NODE_PASSPORT] : [],
337
+ ownerProfile: profile,
338
+ preferredColor: passportColor,
319
339
  };
320
340
 
321
341
  if (issuerType === 'node') {
@@ -327,8 +347,6 @@ const handleInvitationResponse = async ({
327
347
  const role = getRoleFromLocalPassport(get(vc, 'credentialSubject.passport'));
328
348
  const passport = createUserPassport(vc, { role });
329
349
 
330
- const profile = claims.find((x) => x.type === 'profile');
331
-
332
350
  const user = await getUser(node, teamDid, userDid);
333
351
 
334
352
  if (user) {
@@ -391,14 +409,14 @@ const createIssuePassportRequest = async ({ node, nodeInfo, teamDid, id, locale
391
409
  if (!id) {
392
410
  throw new Error('The issuance id does not exist');
393
411
  }
394
-
395
412
  const list = await node.getPassportIssuances({ teamDid });
396
413
  const issuanceInfo = list.find((x) => x.id === id);
397
414
  if (!issuanceInfo) {
398
415
  throw new Error('The issuance does not exist or has been used');
399
416
  }
400
417
 
401
- const { name: issuerName, wallet: issuerWallet } = await getTeamInfo({ node, nodeInfo, teamDid });
418
+ const { name: issuerName, wallet: issuerWallet, passportColor } = await getTeamInfo({ node, nodeInfo, teamDid });
419
+ const user = await getUser(node, teamDid, issuanceInfo.ownerDid);
402
420
 
403
421
  const passport = await createPassport({
404
422
  name: issuanceInfo.name,
@@ -413,7 +431,14 @@ const createIssuePassportRequest = async ({ node, nodeInfo, teamDid, id, locale
413
431
  type: 'mime:text/plain',
414
432
  display: JSON.stringify({
415
433
  type: 'svg',
416
- content: createPassportSvg({ issuer: issuerName, title: passport.title, issuerDid: issuerWallet.address }),
434
+ content: createPassportSvg({
435
+ issuer: issuerName,
436
+ title: passport.title,
437
+ issuerDid: issuerWallet.address,
438
+ ownerName: get(user, 'fullName', 'Your Name'),
439
+ ownerAvatarUrl: get(user, 'avatar', ''),
440
+ preferredColor: passportColor,
441
+ }),
417
442
  }),
418
443
  };
419
444
  };
@@ -448,7 +473,12 @@ const handleIssuePassportResponse = async ({
448
473
  );
449
474
  }
450
475
 
451
- const { name: issuerName, wallet: issuerWallet, type: issuerType } = await getTeamInfo({ node, nodeInfo, teamDid });
476
+ const {
477
+ name: issuerName,
478
+ wallet: issuerWallet,
479
+ type: issuerType,
480
+ passportColor,
481
+ } = await getTeamInfo({ node, nodeInfo, teamDid });
452
482
 
453
483
  // get issuanceInfo from session
454
484
  const list = await node.getPassportIssuances({ teamDid });
@@ -476,6 +506,8 @@ const handleIssuePassportResponse = async ({
476
506
  teamDid,
477
507
  }),
478
508
  types: teamDid === nodeInfo.did ? [NFT_TYPE_NODE_PASSPORT] : [],
509
+ ownerProfile: user,
510
+ preferredColor: passportColor,
479
511
  };
480
512
 
481
513
  if (issuerType === 'node') {
@@ -9,7 +9,6 @@ const get = require('lodash/get');
9
9
  const logger = require('./logger');
10
10
  const { messages, getUser, checkWalletVersion, getPassportStatusEndpoint } = require('./auth');
11
11
  const {
12
- createPassportSvg,
13
12
  createPassport,
14
13
  createPassportVC,
15
14
  upsertToPassports,
@@ -18,6 +17,8 @@ const {
18
17
  } = require('./passport');
19
18
  const verifySignature = require('./util/verify-signature');
20
19
 
20
+ const createPassportSvg = require('./util/create-passport-svg');
21
+
21
22
  const TEAM_TYPES = {
22
23
  BLOCKLET: 'blocklet',
23
24
  NODE: 'node',
@@ -28,6 +29,7 @@ const getTeamInfo = async ({ type, node, req }) => {
28
29
  let issuerDid;
29
30
  let issuerName;
30
31
  let issuerWallet;
32
+ let passportColor;
31
33
 
32
34
  const info = await node.getNodeInfo();
33
35
  if (type === TEAM_TYPES.NODE) {
@@ -35,13 +37,15 @@ const getTeamInfo = async ({ type, node, req }) => {
35
37
  issuerDid = info.did;
36
38
  issuerName = info.name;
37
39
  issuerWallet = getNodeWallet(info.sk);
40
+ passportColor = 'default';
38
41
  } else if (TEAM_TYPES.BLOCKLET) {
39
42
  teamDid = req.headers['x-blocklet-did'];
40
43
  const blocklet = await node.getBlocklet({ did: teamDid, attachRuntimeInfo: false });
41
- const { wallet } = getBlockletInfo(blocklet, info.sk);
42
- issuerDid = wallet.address;
44
+ const blockletInfo = getBlockletInfo(blocklet, info.sk);
45
+ issuerDid = blockletInfo.wallet.address;
43
46
  issuerName = blocklet.meta.title || blocklet.meta.name;
44
- issuerWallet = wallet;
47
+ issuerWallet = blockletInfo.wallet;
48
+ passportColor = blockletInfo.passportColor;
45
49
  } else {
46
50
  throw new Error('createLostPassportListRoute: unknown type');
47
51
  }
@@ -51,6 +55,7 @@ const getTeamInfo = async ({ type, node, req }) => {
51
55
  issuerDid,
52
56
  issuerName,
53
57
  issuerWallet,
58
+ passportColor,
54
59
  };
55
60
  };
56
61
 
@@ -132,7 +137,6 @@ const createLostPassportListRoute = ({ node, type }) => ({
132
137
  const createLostPassportIssueRoute = ({ node, type, authServicePrefix }) => ({
133
138
  action: 'lost-passport-issue',
134
139
  authPrincipal: false,
135
-
136
140
  claims: [
137
141
  {
138
142
  authPrincipal: async ({ extraParams }) => {
@@ -145,10 +149,11 @@ const createLostPassportIssueRoute = ({ node, type, authServicePrefix }) => ({
145
149
  },
146
150
  {
147
151
  signature: async ({ extraParams, context: { request, abtwallet } }) => {
148
- const { locale, passportName } = extraParams;
152
+ const { locale, passportName, receiverDid } = extraParams;
149
153
  checkWalletVersion({ abtwallet, locale });
150
154
 
151
- const { teamDid, issuerDid, issuerName } = await getTeamInfo({ node, req: request, type });
155
+ const { teamDid, issuerDid, issuerName, passportColor } = await getTeamInfo({ node, req: request, type });
156
+ const user = await getUser(node, teamDid, receiverDid);
152
157
 
153
158
  const passport = await createPassport({
154
159
  name: passportName,
@@ -163,7 +168,14 @@ const createLostPassportIssueRoute = ({ node, type, authServicePrefix }) => ({
163
168
  type: 'mime:text/plain',
164
169
  display: JSON.stringify({
165
170
  type: 'svg',
166
- content: createPassportSvg({ issuer: issuerName, title: passport.title, issuerDid }),
171
+ content: createPassportSvg({
172
+ issuer: issuerName,
173
+ title: passport.title,
174
+ issuerDid,
175
+ ownerName: user.fullName || '',
176
+ ownerAvatarUrl: user.avatar || '',
177
+ preferredColor: passportColor,
178
+ }),
167
179
  }),
168
180
  };
169
181
  },
@@ -173,7 +185,7 @@ const createLostPassportIssueRoute = ({ node, type, authServicePrefix }) => ({
173
185
  onAuth: async ({ claims, userDid, userPk, extraParams, baseUrl, req }) => {
174
186
  const { locale = 'en', receiverDid, passportName } = extraParams;
175
187
 
176
- const { teamDid, issuerDid, issuerName, issuerWallet } = await getTeamInfo({ node, req, type });
188
+ const { teamDid, issuerDid, issuerName, issuerWallet, passportColor } = await getTeamInfo({ node, req, type });
177
189
  const statusEndpointBaseUrl = getStatusEndpointBaseUrl(type, baseUrl, authServicePrefix);
178
190
 
179
191
  // Verify signature
@@ -232,6 +244,8 @@ const createLostPassportIssueRoute = ({ node, type, authServicePrefix }) => ({
232
244
  teamDid,
233
245
  }),
234
246
  types: [],
247
+ ownerProfile: user,
248
+ preferredColor: passportColor,
235
249
  };
236
250
 
237
251
  if (type === TEAM_TYPES.NODE) {
package/lib/passport.js CHANGED
@@ -4,6 +4,7 @@ const Joi = require('joi');
4
4
  const pick = require('lodash/pick');
5
5
  const { create: createVC } = require('@arcblock/vc');
6
6
  const { ROLES, NFT_TYPE_GENERAL_PASSPORT, PASSPORT_STATUS } = require('@abtnode/constant');
7
+ const createPassportSvg = require('./util/create-passport-svg');
7
8
 
8
9
  const SPEC_VERSION = '1.0.0';
9
10
 
@@ -48,31 +49,17 @@ const createPassport = async ({ name, node, locale = 'en', teamDid, endpoint } =
48
49
  return passport;
49
50
  };
50
51
 
51
- const createPassportSvg = ({
52
- issuer,
53
- title,
54
- issuerDid,
55
- }) => `<svg width="317" height="200" viewBox="0 0 317 200" fill="none" xmlns="http://www.w3.org/2000/svg">
56
- <rect x="0.5" y="0.5" width="316" height="199" rx="7.5" fill="url(#paint0_linear)"/>
57
- <path d="M26.1973 155H25.0723V146.469H26.1973V155ZM31.7812 153.318C31.7812 153.025 31.6699 152.799 31.4473 152.639C31.2285 152.475 30.8438 152.334 30.293 152.217C29.7461 152.1 29.3105 151.959 28.9863 151.795C28.666 151.631 28.4277 151.436 28.2715 151.209C28.1191 150.982 28.043 150.713 28.043 150.4C28.043 149.881 28.2617 149.441 28.6992 149.082C29.1406 148.723 29.7031 148.543 30.3867 148.543C31.1055 148.543 31.6875 148.729 32.1328 149.1C32.582 149.471 32.8066 149.945 32.8066 150.523H31.7168C31.7168 150.227 31.5898 149.971 31.3359 149.756C31.0859 149.541 30.7695 149.434 30.3867 149.434C29.9922 149.434 29.6836 149.52 29.4609 149.691C29.2383 149.863 29.127 150.088 29.127 150.365C29.127 150.627 29.2305 150.824 29.4375 150.957C29.6445 151.09 30.0176 151.217 30.5566 151.338C31.0996 151.459 31.5391 151.604 31.875 151.771C32.2109 151.939 32.459 152.143 32.6191 152.381C32.7832 152.615 32.8652 152.902 32.8652 153.242C32.8652 153.809 32.6387 154.264 32.1855 154.607C31.7324 154.947 31.1445 155.117 30.4219 155.117C29.9141 155.117 29.4648 155.027 29.0742 154.848C28.6836 154.668 28.377 154.418 28.1543 154.098C27.9355 153.773 27.8262 153.424 27.8262 153.049H28.9102C28.9297 153.412 29.0742 153.701 29.3438 153.916C29.6172 154.127 29.9766 154.232 30.4219 154.232C30.832 154.232 31.1602 154.15 31.4062 153.986C31.6562 153.818 31.7812 153.596 31.7812 153.318ZM37.9688 153.318C37.9688 153.025 37.8574 152.799 37.6348 152.639C37.416 152.475 37.0312 152.334 36.4805 152.217C35.9336 152.1 35.498 151.959 35.1738 151.795C34.8535 151.631 34.6152 151.436 34.459 151.209C34.3066 150.982 34.2305 150.713 34.2305 150.4C34.2305 149.881 34.4492 149.441 34.8867 149.082C35.3281 148.723 35.8906 148.543 36.5742 148.543C37.293 148.543 37.875 148.729 38.3203 149.1C38.7695 149.471 38.9941 149.945 38.9941 150.523H37.9043C37.9043 150.227 37.7773 149.971 37.5234 149.756C37.2734 149.541 36.957 149.434 36.5742 149.434C36.1797 149.434 35.8711 149.52 35.6484 149.691C35.4258 149.863 35.3145 150.088 35.3145 150.365C35.3145 150.627 35.418 150.824 35.625 150.957C35.832 151.09 36.2051 151.217 36.7441 151.338C37.2871 151.459 37.7266 151.604 38.0625 151.771C38.3984 151.939 38.6465 152.143 38.8066 152.381C38.9707 152.615 39.0527 152.902 39.0527 153.242C39.0527 153.809 38.8262 154.264 38.373 154.607C37.9199 154.947 37.332 155.117 36.6094 155.117C36.1016 155.117 35.6523 155.027 35.2617 154.848C34.8711 154.668 34.5645 154.418 34.3418 154.098C34.123 153.773 34.0137 153.424 34.0137 153.049H35.0977C35.1172 153.412 35.2617 153.701 35.5312 153.916C35.8047 154.127 36.1641 154.232 36.6094 154.232C37.0195 154.232 37.3477 154.15 37.5938 153.986C37.8438 153.818 37.9688 153.596 37.9688 153.318ZM44.3789 154.373C43.957 154.869 43.3379 155.117 42.5215 155.117C41.8457 155.117 41.3301 154.922 40.9746 154.531C40.623 154.137 40.4453 153.555 40.4414 152.785V148.66H41.5254V152.756C41.5254 153.717 41.916 154.197 42.6973 154.197C43.5254 154.197 44.0762 153.889 44.3496 153.271V148.66H45.4336V155H44.4023L44.3789 154.373ZM49.7168 155.117C48.8574 155.117 48.1582 154.836 47.6191 154.273C47.0801 153.707 46.8105 152.951 46.8105 152.006V151.807C46.8105 151.178 46.9297 150.617 47.168 150.125C47.4102 149.629 47.7461 149.242 48.1758 148.965C48.6094 148.684 49.0781 148.543 49.582 148.543C50.4062 148.543 51.0469 148.814 51.5039 149.357C51.9609 149.9 52.1895 150.678 52.1895 151.689V152.141H47.8945C47.9102 152.766 48.0918 153.271 48.4395 153.658C48.791 154.041 49.2363 154.232 49.7754 154.232C50.1582 154.232 50.4824 154.154 50.748 153.998C51.0137 153.842 51.2461 153.635 51.4453 153.377L52.1074 153.893C51.5762 154.709 50.7793 155.117 49.7168 155.117ZM49.582 149.434C49.1445 149.434 48.7773 149.594 48.4805 149.914C48.1836 150.23 48 150.676 47.9297 151.25H51.1055V151.168C51.0742 150.617 50.9258 150.191 50.6602 149.891C50.3945 149.586 50.0352 149.434 49.582 149.434ZM56.5137 149.633C56.3496 149.605 56.1719 149.592 55.9805 149.592C55.2695 149.592 54.7871 149.895 54.5332 150.5V155H53.4492V148.66H54.5039L54.5215 149.393C54.877 148.826 55.3809 148.543 56.0332 148.543C56.2441 148.543 56.4043 148.57 56.5137 148.625V149.633Z" fill="#999999"/>
58
- <text x="262" y="42" font-size="${
59
- title.length > 5 ? 12 : 14
60
- }" fill="#222222" style='text-anchor:middle;font-weight:bold;font-family: Roboto;'>${title.toUpperCase()}</text>
61
- <text x="24" y="44" font-size="18" fill="#222222" style='width: 100'>${issuer}</text>
62
- <path d="M234 24.25H289V23.75H234V24.25ZM292.75 28V47H293.25V28H292.75ZM289 50.75H234V51.25H289V50.75ZM230.25 47V28H229.75V47H230.25ZM234 50.75C231.929 50.75 230.25 49.0711 230.25 47H229.75C229.75 49.3472 231.653 51.25 234 51.25V50.75ZM292.75 47C292.75 49.0711 291.071 50.75 289 50.75V51.25C291.347 51.25 293.25 49.3472 293.25 47H292.75ZM289 24.25C291.071 24.25 292.75 25.9289 292.75 28H293.25C293.25 25.6528 291.347 23.75 289 23.75V24.25ZM234 23.75C231.653 23.75 229.75 25.6528 229.75 28H230.25C230.25 25.9289 231.929 24.25 234 24.25V23.75Z" fill="#999999"/>
63
- <path fill-rule="evenodd" clip-rule="evenodd" d="M25.9318 163.527H41.0926C41.8394 163.527 42.4449 164.077 42.4449 164.756V173.244C42.4449 173.923 41.8394 174.473 41.0926 174.473H25.9318C25.185 174.473 24.5795 173.923 24.5795 173.244V173.172C24.5795 173.012 24.4498 172.882 24.2898 172.882C24.1297 172.882 24 173.012 24 173.172V173.244C24 174.214 24.8649 175 25.9318 175H41.0926C42.1595 175 43.0244 174.214 43.0244 173.244V164.756C43.0244 163.786 42.1595 163 41.0926 163H25.9318C24.8649 163 24 163.786 24 164.756V164.828C24 164.988 24.1297 165.118 24.2898 165.118C24.4498 165.118 24.5795 164.988 24.5795 164.828V164.756C24.5795 164.077 25.185 163.527 25.9318 163.527ZM24.0612 172.012C24.1079 172.053 24.1604 172.073 24.2187 172.073H26.5375C27.4008 172.073 28.057 171.877 28.5062 171.485C28.9612 171.087 29.2033 170.481 29.2325 169.667C29.2383 169.503 29.2412 169.281 29.2412 169C29.2412 168.719 29.2383 168.494 29.2325 168.324C29.2091 167.545 28.9612 166.951 28.4887 166.541C28.0162 166.132 27.3512 165.927 26.4937 165.927H24.2187C24.1604 165.927 24.1079 165.947 24.0612 165.988C24.0204 166.029 24 166.082 24 166.146V171.854C24 171.912 24.0204 171.965 24.0612 172.012ZM27.3425 170.537C27.1616 170.718 26.8787 170.809 26.4937 170.809H25.575V167.191H26.45C26.835 167.191 27.1237 167.285 27.3162 167.472C27.5146 167.66 27.6196 167.949 27.6312 168.341C27.6429 168.505 27.6487 168.722 27.6487 168.991C27.6487 169.26 27.6429 169.48 27.6312 169.65C27.6196 170.054 27.5233 170.349 27.3425 170.537ZM30.3246 172.012C30.3713 172.053 30.4238 172.073 30.4821 172.073H31.6546C31.7188 172.073 31.7713 172.053 31.8121 172.012C31.8588 171.971 31.8821 171.918 31.8821 171.854V166.146C31.8821 166.082 31.8588 166.029 31.8121 165.988C31.7713 165.947 31.7188 165.927 31.6546 165.927H30.4821C30.4238 165.927 30.3713 165.947 30.3246 165.988C30.2838 166.029 30.2634 166.082 30.2634 166.146V171.854C30.2634 171.912 30.2838 171.965 30.3246 172.012ZM33.319 172.073C33.2607 172.073 33.2082 172.053 33.1615 172.012C33.1207 171.965 33.1003 171.912 33.1003 171.854V166.146C33.1003 166.082 33.1207 166.029 33.1615 165.988C33.2082 165.947 33.2607 165.927 33.319 165.927H35.594C36.4515 165.927 37.1165 166.132 37.589 166.541C38.0615 166.951 38.3094 167.545 38.3327 168.324C38.3385 168.494 38.3415 168.719 38.3415 169C38.3415 169.281 38.3385 169.503 38.3327 169.667C38.3035 170.481 38.0615 171.087 37.6065 171.485C37.1573 171.877 36.5011 172.073 35.6377 172.073H33.319ZM35.594 170.809C35.979 170.809 36.2619 170.718 36.4427 170.537C36.6236 170.349 36.7198 170.054 36.7315 169.65C36.7431 169.48 36.749 169.26 36.749 168.991C36.749 168.722 36.7431 168.505 36.7315 168.341C36.7198 167.949 36.6148 167.66 36.4165 167.472C36.224 167.285 35.9352 167.191 35.5502 167.191H34.6752V170.809H35.594ZM40.1082 168.282C39.8193 168.282 39.5811 168.047 39.5811 167.759C39.5811 167.47 39.8193 167.235 40.1082 167.235C40.3972 167.235 40.6354 167.47 40.6354 167.759C40.6354 168.047 40.3972 168.282 40.1082 168.282ZM40.1082 171.017C39.8193 171.017 39.5811 170.782 39.5811 170.493C39.5811 170.205 39.8193 169.97 40.1082 169.97C40.3972 169.97 40.6354 170.205 40.6354 170.493C40.6354 170.782 40.3972 171.017 40.1082 171.017Z" fill="#999999"/>
64
- <text x="48" y="173" font-size="11" fill="#777777" font-family="Courier New">${issuerDid}</text>
65
- <rect x="0.5" y="0.5" width="316" height="199" rx="7.5" stroke="#E7ECF6"/>
66
- <defs>
67
- <linearGradient id="paint0_linear" x1="158.5" y1="0" x2="158.5" y2="200" gradientUnits="userSpaceOnUse">
68
- <stop stop-color="#F3F6FC"/>
69
- <stop offset="1" stop-color="#EEF1F7"/>
70
- </linearGradient>
71
- </defs>
72
- </svg>
73
- `;
74
-
75
- const createPassportVC = ({ issuerWallet, issuerName, ownerDid, passport, endpoint, types = [], tag } = {}) => {
52
+ const createPassportVC = ({
53
+ issuerWallet,
54
+ issuerName,
55
+ ownerDid,
56
+ passport,
57
+ endpoint,
58
+ types = [],
59
+ tag,
60
+ ownerProfile,
61
+ preferredColor,
62
+ } = {}) => {
76
63
  validatePassport(passport);
77
64
 
78
65
  return createVC({
@@ -86,7 +73,14 @@ const createPassportVC = ({ issuerWallet, issuerName, ownerDid, passport, endpoi
86
73
  passport,
87
74
  display: {
88
75
  type: 'svg',
89
- content: createPassportSvg({ issuer: issuerName, issuerDid: issuerWallet.address, title: passport.title }),
76
+ content: createPassportSvg({
77
+ issuer: issuerName,
78
+ issuerDid: issuerWallet.address,
79
+ title: passport.title,
80
+ ownerName: ownerProfile ? ownerProfile.fullName : '',
81
+ ownerAvatarUrl: ownerProfile ? ownerProfile.avatar : '',
82
+ preferredColor,
83
+ }),
90
84
  },
91
85
  },
92
86
  endpoint,
@@ -0,0 +1,93 @@
1
+ const { toHex } = require('@ocap/util');
2
+ const { Hasher } = require('@ocap/mcrypto');
3
+ const { getNftBGColor, DEFAULT_COLOR } = require('./passport-color');
4
+
5
+ /**
6
+ * Generate Passport SVG
7
+ *
8
+ * @param {string} title passport title
9
+ * @param {string} issuer issuer name
10
+ * @param {string} issuerDid
11
+ * @param {boolean} ownerName
12
+ * @param {string} preferredColor
13
+ * @param {boolean} ownerAvatarUrl
14
+ * @param {boolean} revoked 是否撤销
15
+ * @param {boolean} isDataUrl 返回生成 data url
16
+ * @returns {string} svg xml or image data url
17
+ */
18
+ const createPassportSvg = ({
19
+ issuer,
20
+ title,
21
+ issuerDid,
22
+ ownerName = '',
23
+ preferredColor = 'default',
24
+ ownerAvatarUrl = '',
25
+ revoked,
26
+ isDataUrl,
27
+ }) => {
28
+ let colors;
29
+ if (preferredColor === 'default') {
30
+ colors = DEFAULT_COLOR;
31
+ } else if (preferredColor === 'auto') {
32
+ colors = getNftBGColor(`#${toHex(Hasher.SHA3.hash224(issuerDid)).slice(-6)}`);
33
+ } else {
34
+ colors = getNftBGColor(preferredColor);
35
+ }
36
+
37
+ const svgXML = `<svg width="100%" height="100%" viewBox="0 0 424 564" fill="none" xmlns="http://www.w3.org/2000/svg">
38
+ <rect x="1" y="1" width="422" height="562" rx="21" fill="url(#paint0_linear_6_342)"/>
39
+ <text x="360" y="65" fill="white" xml:space="preserve" font-family="Roboto,Helvetica" font-size="${(() => {
40
+ if (title.length >= 6) {
41
+ return 24;
42
+ }
43
+
44
+ if (title.length >= 8) {
45
+ return 20;
46
+ }
47
+
48
+ return 28;
49
+ })()}" letter-spacing="0px" style="dominant-baseline:middle;text-anchor:middle;">${title.toUpperCase()}</text>
50
+ <text opacity="0.4" fill="white" xml:space="preserve" style="white-space: pre" font-family="Roboto,Helvetica" font-size="16" font-weight="500" letter-spacing="0px"><tspan x="319" y="40.9688">PASSPORT</tspan></text>
51
+ <text fill="white" xml:space="preserve" style="white-space: pre" font-family="Roboto,Helvetica" font-size="${(() => {
52
+ if (issuer.length > 15) {
53
+ return 20;
54
+ }
55
+
56
+ return 24;
57
+ })()}" font-weight="500" letter-spacing="0px"><tspan x="26" y="60.2031">${issuer}</tspan></text>
58
+ <rect x="2" y="102" width="420" height="168" fill="black" fill-opacity="0.3"/>
59
+ <circle cx="212" cy="250" r="60" fill="#FBFBFB"/>
60
+ <path fill-rule="evenodd" clip-rule="evenodd" d="M212.224 247.722C218.932 247.722 224.37 242.284 224.37 235.575C224.37 228.867 218.932 223.429 212.224 223.429C205.515 223.429 200.077 228.867 200.077 235.575C200.077 242.284 205.515 247.722 212.224 247.722ZM188.857 272.018C188.857 274.573 190.929 276.645 193.484 276.645H230.962C231.28 276.645 231.597 276.612 231.908 276.547C234.41 276.025 236.014 273.574 235.492 271.072L234.335 265.533C232.991 259.099 227.319 254.489 220.746 254.489H203.7C197.127 254.489 191.455 259.099 190.112 265.533L188.955 271.072C188.89 271.383 188.857 271.7 188.857 272.018Z" fill="#FBFBFB" />
61
+ <path d="M231.908 276.547L232.215 278.016L231.908 276.547ZM235.492 271.072L236.96 270.765H236.96L235.492 271.072ZM234.335 265.533L232.866 265.839L234.335 265.533ZM190.112 265.533L191.58 265.839L190.112 265.533ZM188.955 271.072L187.487 270.765H187.487L188.955 271.072ZM222.87 235.575C222.87 241.455 218.104 246.222 212.224 246.222V249.222C219.761 249.222 225.87 243.112 225.87 235.575H222.87ZM212.224 224.929C218.104 224.929 222.87 229.695 222.87 235.575H225.87C225.87 228.038 219.761 221.929 212.224 221.929V224.929ZM201.577 235.575C201.577 229.695 206.344 224.929 212.224 224.929V221.929C204.687 221.929 198.577 228.038 198.577 235.575H201.577ZM212.224 246.222C206.344 246.222 201.577 241.455 201.577 235.575H198.577C198.577 243.112 204.687 249.222 212.224 249.222V246.222ZM193.484 275.145C191.757 275.145 190.357 273.745 190.357 272.018H187.357C187.357 275.402 190.1 278.145 193.484 278.145V275.145ZM230.962 275.145H193.484V278.145H230.962V275.145ZM231.601 275.079C231.391 275.123 231.177 275.145 230.962 275.145V278.145C231.383 278.145 231.803 278.102 232.215 278.016L231.601 275.079ZM234.023 271.379C234.376 273.069 233.292 274.726 231.601 275.079L232.215 278.016C235.527 277.324 237.652 274.078 236.96 270.765L234.023 271.379ZM232.866 265.839L234.023 271.379L236.96 270.765L235.803 265.226L232.866 265.839ZM220.746 255.989C226.609 255.989 231.668 260.1 232.866 265.839L235.803 265.226C234.314 258.097 228.029 252.989 220.746 252.989V255.989ZM203.7 255.989H220.746V252.989H203.7V255.989ZM191.58 265.839C192.779 260.1 197.838 255.989 203.7 255.989V252.989C196.417 252.989 190.132 258.097 188.643 265.226L191.58 265.839ZM190.423 271.379L191.58 265.839L188.643 265.226L187.487 270.765L190.423 271.379ZM190.357 272.018C190.357 271.803 190.379 271.589 190.423 271.379L187.487 270.765C187.401 271.177 187.357 271.597 187.357 272.018H190.357Z" fill="#BFBFBF" />
62
+ <circle cx="212" cy="250" r="60" fill="url(#raduisImage)"/>
63
+ <defs>
64
+ <pattern id="raduisImage" patternUnits="userSpaceOnUse" width="424" height="564">
65
+ <image x="152" y="190" href="${ownerAvatarUrl}" width="120" height="120" />
66
+ </pattern>
67
+ </defs>
68
+ <text x="215" y="350" fill="white" xml:space="preserve" style="dominant-baseline:middle;text-anchor:middle;" font-family="Roboto,Helvetica" font-size="28" font-weight="500" letter-spacing="0px">${ownerName}</text>
69
+ <text opacity="0.4" fill="white" xml:space="preserve" style="white-space: pre" font-family="Roboto,Helvetica" font-size="18" font-weight="500" letter-spacing="0px"><tspan x="26" y="407.652">ISSUER DID</tspan></text>
70
+ <path fill-rule="evenodd" clip-rule="evenodd" d="M28.6722 423.313H45.3819C46.4294 423.313 47.2787 424.162 47.2787 425.21V433.79C47.2787 434.838 46.4294 435.687 45.3819 435.687H28.6722C27.9517 435.687 27.3249 435.285 27.0039 434.694H26.1167C26.4887 435.746 27.4924 436.5 28.6722 436.5H45.3819C46.8784 436.5 48.0916 435.287 48.0916 433.79V425.21C48.0916 423.713 46.8784 422.5 45.3819 422.5H28.6722C27.3296 422.5 26.215 423.477 26 424.758H26.8295C27.0321 423.929 27.7803 423.313 28.6722 423.313ZM27.8801 432.096C27.7831 432.089 27.6966 432.082 27.6204 432.075V427.526C27.7243 427.512 27.8351 427.505 27.9528 427.505C28.0705 427.498 28.1848 427.494 28.2955 427.494C29.1057 427.494 29.6978 427.688 30.0717 428.076C30.4457 428.464 30.6326 429.035 30.6326 429.79C30.6326 430.51 30.4353 431.074 30.0406 431.483C29.6528 431.892 29.0538 432.096 28.2436 432.096H27.8801ZM32.0141 431.421C32.208 430.957 32.305 430.413 32.305 429.79C32.305 429.139 32.208 428.582 32.0141 428.118C31.8202 427.654 31.5432 427.273 31.1832 426.975C30.8231 426.677 30.3903 426.459 29.8848 426.321C29.3862 426.182 28.8288 426.113 28.2125 426.113C27.9216 426.113 27.5719 426.127 27.1634 426.154C26.7617 426.175 26.3739 426.22 26 426.29V433.301C26.3739 433.377 26.7548 433.425 27.1426 433.446C27.5304 433.474 27.8697 433.488 28.1605 433.488C28.7976 433.488 29.3723 433.412 29.8848 433.259C30.3972 433.107 30.83 432.878 31.1832 432.574C31.5432 432.269 31.8202 431.885 32.0141 431.421ZM35.2788 426.196H33.6585V433.394H35.2788V426.196ZM38.5654 432.075C38.6416 432.082 38.7281 432.089 38.8251 432.096H39.1886C39.9988 432.096 40.5978 431.892 40.9856 431.483C41.3803 431.074 41.5776 430.51 41.5776 429.79C41.5776 429.035 41.3907 428.464 41.0167 428.076C40.6428 427.688 40.0507 427.494 39.2405 427.494C39.1298 427.494 39.0155 427.498 38.8978 427.505C38.7801 427.505 38.6693 427.512 38.5654 427.526V432.075ZM43.25 429.79C43.25 430.413 43.153 430.957 42.9591 431.421C42.7652 431.885 42.4882 432.269 42.1282 432.574C41.775 432.878 41.3422 433.107 40.8298 433.259C40.3173 433.412 39.7426 433.488 39.1055 433.488C38.8147 433.488 38.4754 433.474 38.0876 433.446C37.6998 433.425 37.3189 433.377 36.945 433.301V426.29C37.3189 426.22 37.7067 426.175 38.1084 426.154C38.5169 426.127 38.8666 426.113 39.1574 426.113C39.7738 426.113 40.3312 426.182 40.8298 426.321C41.3353 426.459 41.7681 426.677 42.1282 426.975C42.4882 427.273 42.7652 427.654 42.9591 428.118C43.153 428.582 43.25 429.139 43.25 429.79ZM45.1907 428.355C44.8209 428.355 44.5161 428.055 44.5161 427.686C44.5161 427.316 44.8209 427.016 45.1907 427.016C45.5604 427.016 45.8652 427.316 45.8652 427.686C45.8652 428.055 45.5604 428.355 45.1907 428.355ZM45.1907 431.855C44.8209 431.855 44.5161 431.555 44.5161 431.185C44.5161 430.815 44.8209 430.516 45.1907 430.516C45.5604 430.516 45.8652 430.815 45.8652 431.185C45.8652 431.555 45.5604 431.855 45.1907 431.855Z" fill="white"/>
71
+ <text fill="white" xml:space="preserve" style="white-space: pre" font-family="Roboto,Helvetica" font-size="15" font-weight="500" letter-spacing="0px"><tspan x="54.0916" y="434.969">${issuerDid}</tspan></text>
72
+ <rect x="1" y="1" width="422" height="562" rx="21" stroke="#F0F0F0" stroke-width="2"/>
73
+ <defs>
74
+ <linearGradient id="paint0_linear_6_342" x1="414.5" y1="562" x2="-116.166" y2="156.726" gradientUnits="userSpaceOnUse">
75
+ <stop stop-color="${colors.startColor}"/>
76
+ <stop offset="1" stop-color="${colors.endColor}"/>
77
+ </linearGradient>
78
+ </defs>
79
+ ${
80
+ revoked
81
+ ? '<circle cx="210" cy="250" r="120" stroke="rgba(255,0,0,0.5)" stroke-width="25" /><line x1="132" y1="168" x2="296" y2="323" stroke="rgba(255,0,0,0.5)" stroke-width="25" />'
82
+ : ''
83
+ }
84
+ </svg>`;
85
+
86
+ if (isDataUrl) {
87
+ return `data:image/svg+xml;utf8,${encodeURIComponent(svgXML)}`;
88
+ }
89
+
90
+ return svgXML;
91
+ };
92
+
93
+ module.exports = createPassportSvg;
@@ -0,0 +1,134 @@
1
+ const DEFAULT_COLOR = {
2
+ startColor: '#2B3845',
3
+ endColor: '#5A7A8A',
4
+ };
5
+
6
+ /**
7
+ * 将单个颜色转换为可用 nft 两个渐变背景色
8
+ * @param {String} rgbString 背景色
9
+ * @returns {Object}
10
+ */
11
+ const getNftBGColor = (rgbString) => {
12
+ if (rgbString) {
13
+ const hsldata = rgbToHsl(rgbString);
14
+
15
+ // 色相 h 随意
16
+ // 饱和度 s 不可超过 0.8
17
+ // 亮度 l 不可超过 0.6,0.3 以上
18
+ const newHsl = [hsldata[0]];
19
+ if (hsldata[1] > 0.7) {
20
+ newHsl.push(0.7);
21
+ } else {
22
+ newHsl.push(hsldata[1]);
23
+ }
24
+
25
+ if (hsldata[2] > 0.6) {
26
+ newHsl.push(0.6);
27
+ } else if (hsldata[2] < 0.3) {
28
+ newHsl.push(0.3);
29
+ } else {
30
+ newHsl.push(hsldata[2]);
31
+ }
32
+
33
+ const newRepeatHsl = [newHsl[0], newHsl[1], newHsl[2] - 0.1];
34
+
35
+ const newColor = hslToRgb(...newHsl);
36
+ const newRepeatColor = hslToRgb(...newRepeatHsl);
37
+
38
+ return {
39
+ startColor: newRepeatColor,
40
+ endColor: newColor,
41
+ };
42
+ }
43
+
44
+ return {
45
+ startColor: '#2B3845',
46
+ endColor: '#5A7A8A',
47
+ };
48
+ };
49
+
50
+ /**
51
+ * RGB 转换为 HSL 颜色
52
+ *
53
+ * @param {String} rgbString rgb色值
54
+ * @return {Array} HSL色值
55
+ */
56
+ function rgbToHsl(rgbString) {
57
+ const color = rgbString.replace('#', '');
58
+ const r = parseInt(color.slice(0, 2), 16) / 255;
59
+ const g = parseInt(color.slice(2, 4), 16) / 255;
60
+ const b = parseInt(color.slice(4, 6), 16) / 255;
61
+ const max = Math.max(r, g, b);
62
+ const min = Math.min(r, g, b);
63
+ let h;
64
+ let s;
65
+ const l = (max + min) / 2;
66
+
67
+ if (max === min) {
68
+ h = 0;
69
+ s = 0;
70
+ } else {
71
+ const d = max - min;
72
+ s = l > 0.5 ? d / (2 - max - min) : d / (max + min);
73
+ switch (max) {
74
+ case r:
75
+ h = (g - b) / d + (g < b ? 6 : 0);
76
+ break;
77
+ case g:
78
+ h = (b - r) / d + 2;
79
+ break;
80
+ case b:
81
+ h = (r - g) / d + 4;
82
+ break;
83
+ default:
84
+ }
85
+ h /= 6;
86
+ }
87
+
88
+ return [h, s, l];
89
+ }
90
+
91
+ /**
92
+ * HSL颜色值转换为RGB.
93
+ * h, s, 和 l 设定在 [0, 1] 之间
94
+ * @param {Number} h 色相
95
+ * @param {Number} s 饱和度
96
+ * @param {Number} l 亮度
97
+ * @returns rgb string
98
+ */
99
+ function hslToRgb(h, s, l) {
100
+ let r;
101
+ let g;
102
+ let b;
103
+
104
+ if (s === 0) {
105
+ r = l;
106
+ g = l;
107
+ b = l;
108
+ } else {
109
+ const hue2rgb = (p, q, t2) => {
110
+ let t = t2;
111
+ if (t < 0) t += 1;
112
+ if (t > 1) t -= 1;
113
+ if (t < 1 / 6) return p + (q - p) * 6 * t;
114
+ if (t < 1 / 2) return q;
115
+ if (t < 2 / 3) return p + (q - p) * (2 / 3 - t) * 6;
116
+ return p;
117
+ };
118
+
119
+ const q = l < 0.5 ? l * (1 + s) : l + s - l * s;
120
+ const p = 2 * l - q;
121
+ r = hue2rgb(p, q, h + 1 / 3);
122
+ g = hue2rgb(p, q, h);
123
+ b = hue2rgb(p, q, h - 1 / 3);
124
+ }
125
+
126
+ return `#${[Math.round(r * 255), Math.round(g * 255), Math.round(b * 255)]
127
+ .map((e) => e.toString(16).padStart(2, 0))
128
+ .join('')}`;
129
+ }
130
+
131
+ module.exports = {
132
+ getNftBGColor,
133
+ DEFAULT_COLOR,
134
+ };
package/package.json CHANGED
@@ -3,7 +3,7 @@
3
3
  "publishConfig": {
4
4
  "access": "public"
5
5
  },
6
- "version": "1.6.12",
6
+ "version": "1.6.16",
7
7
  "description": "Simple lib to manage auth in ABT Node",
8
8
  "main": "lib/index.js",
9
9
  "files": [
@@ -20,15 +20,15 @@
20
20
  "author": "linchen <linchen1987@foxmail.com> (http://github.com/linchen1987)",
21
21
  "license": "MIT",
22
22
  "dependencies": {
23
- "@abtnode/constant": "1.6.12",
24
- "@abtnode/logger": "1.6.12",
25
- "@abtnode/util": "1.6.12",
26
- "@arcblock/did": "^1.13.79",
27
- "@arcblock/vc": "^1.13.79",
28
- "@blocklet/meta": "1.6.12",
29
- "@ocap/mcrypto": "^1.13.79",
30
- "@ocap/util": "^1.13.79",
31
- "@ocap/wallet": "^1.13.79",
23
+ "@abtnode/constant": "1.6.16",
24
+ "@abtnode/logger": "1.6.16",
25
+ "@abtnode/util": "1.6.16",
26
+ "@arcblock/did": "^1.14.3",
27
+ "@arcblock/vc": "^1.14.3",
28
+ "@blocklet/meta": "1.6.16",
29
+ "@ocap/mcrypto": "^1.14.3",
30
+ "@ocap/util": "^1.14.3",
31
+ "@ocap/wallet": "^1.14.3",
32
32
  "axios": "^0.21.4",
33
33
  "joi": "^17.5.0",
34
34
  "jsonwebtoken": "^8.5.1",
@@ -39,5 +39,5 @@
39
39
  "devDependencies": {
40
40
  "jest": "^27.4.5"
41
41
  },
42
- "gitHead": "dd79037978dc12f72330227a553a80a504f03fa7"
42
+ "gitHead": "69b08db16aeb75ce23b0e6bb5b9fa396adba2d4b"
43
43
  }