@unchainedshop/api 3.1.0 → 3.1.2

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.
@@ -1,6 +1,6 @@
1
1
  import { Context } from '../../../context.js';
2
2
  export default function verifyWeb3Address(root: never, { address, hash }: {
3
3
  address: string;
4
- hash: string;
4
+ hash: `0x${string}`;
5
5
  }, { modules, userId, user }: Context): Promise<import("@unchainedshop/core-users").User>;
6
6
  //# sourceMappingURL=verifyWeb3Address.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"verifyWeb3Address.d.ts","sourceRoot":"","sources":["../../../../src/resolvers/mutations/accounts/verifyWeb3Address.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,OAAO,EAAE,MAAM,qBAAqB,CAAC;AAE9C,wBAA8B,iBAAiB,CAC7C,IAAI,EAAE,KAAK,EACX,EAAE,OAAO,EAAE,IAAI,EAAE,EAAE;IAAE,OAAO,EAAE,MAAM,CAAC;IAAC,IAAI,EAAE,MAAM,CAAA;CAAE,EACpD,EAAE,OAAO,EAAE,MAAM,EAAE,IAAI,EAAE,EAAE,OAAO,qDAqDnC"}
1
+ {"version":3,"file":"verifyWeb3Address.d.ts","sourceRoot":"","sources":["../../../../src/resolvers/mutations/accounts/verifyWeb3Address.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,OAAO,EAAE,MAAM,qBAAqB,CAAC;AAE9C,wBAA8B,iBAAiB,CAC7C,IAAI,EAAE,KAAK,EACX,EAAE,OAAO,EAAE,IAAI,EAAE,EAAE;IAAE,OAAO,EAAE,MAAM,CAAC;IAAC,IAAI,EAAE,KAAK,MAAM,EAAE,CAAA;CAAE,EAC3D,EAAE,OAAO,EAAE,MAAM,EAAE,IAAI,EAAE,EAAE,OAAO,qDAwDnC"}
@@ -8,16 +8,17 @@ export default async function verifyWeb3Address(root, { address, hash }, { modul
8
8
  if (!foundCredentials) {
9
9
  throw new UserWeb3AddressNotFoundError({ userId, address });
10
10
  }
11
- const msg = `0x${Buffer.from(foundCredentials.nonce, 'utf8').toString('hex')}`;
12
11
  // eslint-disable-next-line
13
12
  // @ts-ignore
14
- const { recoverPersonalSignature } = await import('@metamask/eth-sig-util');
15
- const recoveredAddr = recoverPersonalSignature({
16
- data: msg,
17
- signature: hash,
18
- });
19
- const signatureCorrectForAddress = recoveredAddr.toLowerCase() === foundCredentials.address.toLowerCase();
20
- if (!signatureCorrectForAddress) {
13
+ const { bufferToHex, fromRpcSig, ecrecover, toBuffer, hashPersonalMessage, publicToAddress } = await import('@ethereumjs/util');
14
+ const message = `0x${Buffer.from(foundCredentials.nonce, 'utf8').toString('hex')}`;
15
+ const messageHash = hashPersonalMessage(toBuffer(message));
16
+ const sigParams = fromRpcSig(hash);
17
+ const publicKey = ecrecover(messageHash, sigParams.v, sigParams.r, sigParams.s);
18
+ const sender = publicToAddress(publicKey);
19
+ const recoveredAddr = bufferToHex(sender);
20
+ const isSignatureCorrectForAddress = recoveredAddr.toLowerCase() === foundCredentials.address.toLowerCase();
21
+ if (!isSignatureCorrectForAddress) {
21
22
  throw new UserWeb3AddressSignatureError({ userId, address: foundCredentials.address });
22
23
  }
23
24
  const web3Services = user.services.web3.map((service) => {
@@ -1 +1 @@
1
- {"version":3,"file":"verifyWeb3Address.js","sourceRoot":"","sources":["../../../../src/resolvers/mutations/accounts/verifyWeb3Address.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,GAAG,EAAE,MAAM,uBAAuB,CAAC;AAC5C,OAAO,EAAE,4BAA4B,EAAE,6BAA6B,EAAE,MAAM,oBAAoB,CAAC;AAGjG,MAAM,CAAC,OAAO,CAAC,KAAK,UAAU,iBAAiB,CAC7C,IAAW,EACX,EAAE,OAAO,EAAE,IAAI,EAAqC,EACpD,EAAE,OAAO,EAAE,MAAM,EAAE,IAAI,EAAW;IAElC,GAAG,CAAC,8BAA8B,OAAO,IAAI,IAAI,CAAC,QAAQ,EAAE,EAAE;QAC5D,MAAM;KACP,CAAC,CAAC;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"@unchainedshop/api",
3
- "version": "3.1.0",
3
+ "version": "3.1.2",
4
4
  "main": "lib/api-index.js",
5
5
  "types": "lib/api-index.d.ts",
6
6
  "type": "module",
@@ -9,8 +9,8 @@
9
9
  "build": "tsc -b",
10
10
  "prepublishOnly": "npm run clean && npm run build",
11
11
  "watch": "tsc -w",
12
- "test": "NODE_OPTIONS=--experimental-vm-modules jest --detectOpenHandles --forceExit",
13
- "test:watch": "NODE_OPTIONS=--experimental-vm-modules jest --watch --detectOpenHandles --forceExit"
12
+ "test": "tsx --test",
13
+ "test:watch": "tsx --test --watch"
14
14
  },
15
15
  "repository": {
16
16
  "type": "git",
@@ -31,14 +31,14 @@
31
31
  },
32
32
  "homepage": "https://github.com/unchainedshop/unchained#readme",
33
33
  "peerDependencies": {
34
+ "@ethereumjs/util": ">= 8 < 10",
34
35
  "@fastify/cookie": ">= 11.0 < 12",
35
36
  "@fastify/session": ">= 11.0 < 12",
36
- "@metamask/eth-sig-util": ">= 8.1 < 9",
37
37
  "connect-mongo": ">= 5.1.0 < 6",
38
38
  "express": ">= 5.0 < 6",
39
39
  "express-session": ">= 1.18 < 2",
40
- "graphql-yoga": ">= 5.9 < 6.0",
41
40
  "fastify": ">= 5.2 < 6",
41
+ "graphql-yoga": ">= 5.9 < 6.0",
42
42
  "passport": ">= 0.7 < 1"
43
43
  },
44
44
  "peerDependenciesMeta": {
@@ -48,7 +48,7 @@
48
48
  "connect-mongo": {
49
49
  "optional": false
50
50
  },
51
- "@metamask/eth-sig-util": {
51
+ "@ethereumjs/util": {
52
52
  "optional": true
53
53
  },
54
54
  "@fastify/cookie": {
@@ -84,17 +84,16 @@
84
84
  "p-memoize": "^7.1.1"
85
85
  },
86
86
  "devDependencies": {
87
+ "@ethereumjs/util": "^9.1.0",
87
88
  "@fastify/cookie": "^11.0.2",
88
89
  "@fastify/session": "^11.1.0",
89
- "@metamask/eth-sig-util": "^8.2.0",
90
90
  "@types/express": "^5.0.0",
91
- "@types/node": "^22.13.1",
91
+ "@types/node": "^22.13.4",
92
92
  "express": "^5.0.1",
93
93
  "express-session": "^1.18.1",
94
94
  "fastify": "^5.2.1",
95
- "jest": "^29.7.0",
96
95
  "passport": "^0.7.0",
97
- "ts-jest": "^29.2.5",
96
+ "tsx": "^4.19.2",
98
97
  "typescript": "^5.7.3"
99
98
  }
100
99
  }
@@ -4,7 +4,7 @@ import { Context } from '../../../context.js';
4
4
 
5
5
  export default async function verifyWeb3Address(
6
6
  root: never,
7
- { address, hash }: { address: string; hash: string },
7
+ { address, hash }: { address: string; hash: `0x${string}` },
8
8
  { modules, userId, user }: Context,
9
9
  ) {
10
10
  log(`mutation verifyWeb3Address ${address} ${user.username}`, {
@@ -18,21 +18,24 @@ export default async function verifyWeb3Address(
18
18
  throw new UserWeb3AddressNotFoundError({ userId, address });
19
19
  }
20
20
 
21
- const msg = `0x${Buffer.from(foundCredentials.nonce, 'utf8').toString('hex')}`;
22
-
23
21
  // eslint-disable-next-line
24
22
  // @ts-ignore
25
- const { recoverPersonalSignature } = await import('@metamask/eth-sig-util');
23
+ const { bufferToHex, fromRpcSig, ecrecover, toBuffer, hashPersonalMessage, publicToAddress } =
24
+ await import('@ethereumjs/util');
26
25
 
27
- const recoveredAddr = recoverPersonalSignature({
28
- data: msg,
29
- signature: hash,
30
- });
26
+ const message = `0x${Buffer.from(foundCredentials.nonce, 'utf8').toString('hex')}`;
27
+ const messageHash = hashPersonalMessage(toBuffer(message));
28
+
29
+ const sigParams = fromRpcSig(hash);
30
+ const publicKey = ecrecover(messageHash, sigParams.v, sigParams.r, sigParams.s);
31
+
32
+ const sender = publicToAddress(publicKey);
33
+ const recoveredAddr = bufferToHex(sender);
31
34
 
32
- const signatureCorrectForAddress =
35
+ const isSignatureCorrectForAddress =
33
36
  recoveredAddr.toLowerCase() === foundCredentials.address.toLowerCase();
34
37
 
35
- if (!signatureCorrectForAddress) {
38
+ if (!isSignatureCorrectForAddress) {
36
39
  throw new UserWeb3AddressSignatureError({ userId, address: foundCredentials.address });
37
40
  }
38
41
 
@@ -1,4 +1,6 @@
1
1
  // Import the function to be tested.
2
+ import { describe, it, mock } from 'node:test';
3
+ import assert from 'node:assert';
2
4
  import { admin } from '../src/roles/admin.js';
3
5
  import { actions } from '../src/roles/index.js';
4
6
  import { checkAction, ensureActionExists, ensureIsFunction } from '../src/acl.js';
@@ -8,24 +10,28 @@ import { Roles } from '@unchainedshop/roles';
8
10
  describe('API', () => {
9
11
  describe('roles', () => {
10
12
  const role = {
11
- allow: import.meta.jest.fn(),
13
+ allow: mock.fn(),
12
14
  };
13
15
 
14
16
  it('creates the admin role and grants permissions to all actions', () => {
15
17
  admin(role, actions);
16
18
  for (const actionName of Object.keys(actions)) {
17
- expect(role.allow).toHaveBeenCalledWith(actions[actionName], expect.any(Function));
19
+ assert.equal(
20
+ role.allow.mock.calls.some((call) => call.arguments[0] === actions[actionName]),
21
+ true,
22
+ "action wasn't granted",
23
+ );
18
24
  }
19
25
  });
20
26
  });
21
27
 
22
28
  describe('ensureActionExists', () => {
23
29
  it('should throw a PermissionSystemError if the action is undefined', () => {
24
- expect(() => ensureActionExists(undefined, {})).toThrow(PermissionSystemError);
30
+ assert.throws(() => ensureActionExists(undefined, {}), PermissionSystemError);
25
31
  });
26
32
 
27
33
  it('should not throw an error if the action is defined', () => {
28
- expect(() => ensureActionExists('some action', {})).not.toThrow();
34
+ assert.doesNotThrow(() => ensureActionExists('some action', {}));
29
35
  });
30
36
  });
31
37
 
@@ -34,14 +40,14 @@ describe('API', () => {
34
40
  const action = 'some action';
35
41
  const options = { showKey: true };
36
42
  const key = 'some key';
37
- expect(() => ensureIsFunction(null, action, options, key)).toThrow(PermissionSystemError);
43
+ assert.throws(() => ensureIsFunction(null, action, options, key), PermissionSystemError);
38
44
  });
39
45
 
40
46
  it('should not throw an error if the provided value is a function', () => {
41
47
  const action = 'some action';
42
48
  const options = { showKey: true };
43
49
  const key = 'some key';
44
- expect(() =>
50
+ assert.doesNotThrow(() =>
45
51
  ensureIsFunction(
46
52
  () => {
47
53
  /**/
@@ -50,30 +56,31 @@ describe('API', () => {
50
56
  options,
51
57
  key,
52
58
  ),
53
- ).not.toThrow();
59
+ );
54
60
  });
55
61
  });
56
62
 
57
63
  describe('checkAction', () => {
58
64
  it('should throw a NoPermissionError if the user does not have permission to perform the action', async () => {
59
- Roles.userHasPermission = import.meta.jest.fn(async () => false);
65
+ Roles.userHasPermission = mock.fn(async () => false);
60
66
 
61
67
  const context = { userId: '123' };
62
68
  const action = 'some action';
63
69
  const args: any = [];
64
70
  const options = { key: 'some key' };
65
71
 
66
- return expect(checkAction(context, action, args, options)).rejects.toThrow(NoPermissionError);
72
+ return assert.rejects(checkAction(context, action, args, options), NoPermissionError);
67
73
  });
68
74
 
69
75
  it('should not throw an error if the user has permission to perform the action', async () => {
70
- Roles.userHasPermission = import.meta.jest.fn(async () => true);
76
+ Roles.userHasPermission = mock.fn(async () => true);
77
+
71
78
  const context = { userId: '123' };
72
79
  const action = 'some action';
73
80
  const args: any = {};
74
81
  const options = { key: 'some key' };
75
82
 
76
- return expect(checkAction(context, action, args, options)).resolves.not.toThrow();
83
+ return assert.doesNotReject(checkAction(context, action, args, options));
77
84
  });
78
85
  });
79
86
  });
package/jest.config.js DELETED
@@ -1,17 +0,0 @@
1
- /** @type {import('ts-jest/dist/types').InitialOptionsTsJest} */
2
- export default {
3
- preset: 'ts-jest/presets/default-esm', // or other ESM presets
4
- testEnvironment: 'node',
5
- extensionsToTreatAsEsm: ['.ts'],
6
- transform: {
7
- '^.+\\.tsx?$': [
8
- 'ts-jest',
9
- {
10
- useESM: true,
11
- },
12
- ],
13
- },
14
- moduleNameMapper: {
15
- '^(\\.{1,2}/.*)\\.js$': '$1',
16
- },
17
- };