@canton-network/wallet-gateway-remote 1.12.0 → 1.13.0
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/README.md +105 -29
- package/dist/auth/jwt-auth-service.d.ts.map +1 -1
- package/dist/auth/jwt-auth-service.js +150 -77
- package/dist/config/Config.d.ts +146 -22
- package/dist/config/Config.d.ts.map +1 -1
- package/dist/config/Config.js +120 -6
- package/dist/config/ConfigUtils.d.ts.map +1 -1
- package/dist/config/ConfigUtils.js +3 -1
- package/dist/dapp-api/controller.d.ts.map +1 -1
- package/dist/dapp-api/controller.js +14 -19
- package/dist/dapp-api/rpc-gen/index.d.ts.map +1 -1
- package/dist/dapp-api/rpc-gen/typings.d.ts +3 -3
- package/dist/dapp-api/rpc-gen/typings.d.ts.map +1 -1
- package/dist/dapp-api/server.d.ts.map +1 -1
- package/dist/dapp-api/sse-sink.d.ts.map +1 -1
- package/dist/env.d.ts +1 -24
- package/dist/env.d.ts.map +1 -1
- package/dist/env.js +0 -23
- package/dist/example-config.d.ts +40 -4
- package/dist/example-config.d.ts.map +1 -1
- package/dist/example-config.js +44 -8
- package/dist/index.d.ts +3 -3
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +75 -28
- package/dist/init.d.ts.map +1 -1
- package/dist/init.js +24 -110
- package/dist/ledger/party-allocation-service.d.ts +1 -4
- package/dist/ledger/party-allocation-service.d.ts.map +1 -1
- package/dist/ledger/party-allocation-service.js +1 -3
- package/dist/ledger/self-issued-auth-service.d.ts +15 -15
- package/dist/ledger/self-issued-auth-service.d.ts.map +1 -1
- package/dist/ledger/self-issued-auth-service.js +44 -43
- package/dist/ledger/wallet-allocation/signing-providers/bitgo-wallet-allocator.d.ts.map +1 -1
- package/dist/ledger/wallet-allocation/signing-providers/bitgo-wallet-allocator.js +1 -1
- package/dist/ledger/wallet-allocation/signing-providers/blockdaemon-wallet-allocator.d.ts.map +1 -1
- package/dist/ledger/wallet-allocation/signing-providers/dfns-wallet-allocator.d.ts.map +1 -1
- package/dist/ledger/wallet-allocation/signing-providers/fireblocks-wallet-allocator.d.ts.map +1 -1
- package/dist/ledger/wallet-allocation/signing-providers/kernel-wallet-allocator.d.ts.map +1 -1
- package/dist/ledger/wallet-allocation/signing-providers/participant-wallet-allocator.d.ts.map +1 -1
- package/dist/ledger/wallet-allocation/signing-providers/securosys-wallet-allocator.d.ts.map +1 -1
- package/dist/ledger/wallet-allocation/wallet-allocation-service.d.ts +2 -2
- package/dist/ledger/wallet-allocation/wallet-allocation-service.d.ts.map +1 -1
- package/dist/ledger/wallet-allocation/wallet-allocation-service.js +2 -2
- package/dist/ledger/wallet-sync-service.d.ts +3 -0
- package/dist/ledger/wallet-sync-service.d.ts.map +1 -1
- package/dist/ledger/wallet-sync-service.js +143 -28
- package/dist/middleware/apiKeyAuth.d.ts.map +1 -1
- package/dist/middleware/apiKeyAuth.js +4 -8
- package/dist/middleware/errorHandler.d.ts +1 -1
- package/dist/middleware/errorHandler.d.ts.map +1 -1
- package/dist/middleware/jsonRpcHandler.d.ts.map +1 -1
- package/dist/middleware/jsonRpcHandler.js +3 -3
- package/dist/middleware/jwtAuth.d.ts +1 -1
- package/dist/middleware/jwtAuth.d.ts.map +1 -1
- package/dist/middleware/jwtAuth.js +17 -2
- package/dist/middleware/securityHeaders.d.ts.map +1 -1
- package/dist/middleware/sessionHandler.d.ts +3 -3
- package/dist/middleware/sessionHandler.d.ts.map +1 -1
- package/dist/middleware/sessionHandler.js +8 -8
- package/dist/signing/service-account-session.d.ts.map +1 -1
- package/dist/signing/service-account-session.js +1 -1
- package/dist/signing/signing-worker.d.ts +1 -1
- package/dist/signing/signing-worker.d.ts.map +1 -1
- package/dist/signing/signing-worker.js +2 -5
- package/dist/signing-providers-registration.d.ts +6 -0
- package/dist/signing-providers-registration.d.ts.map +1 -0
- package/dist/signing-providers-registration.js +216 -0
- package/dist/user-api/controller.d.ts +6 -3
- package/dist/user-api/controller.d.ts.map +1 -1
- package/dist/user-api/controller.js +218 -94
- package/dist/user-api/rpc-gen/index.d.ts +18 -9
- package/dist/user-api/rpc-gen/index.d.ts.map +1 -1
- package/dist/user-api/rpc-gen/index.js +6 -3
- package/dist/user-api/rpc-gen/schemas.d.ts.map +1 -1
- package/dist/user-api/rpc-gen/schemas.js +42 -10
- package/dist/user-api/rpc-gen/typings.d.ts +67 -45
- package/dist/user-api/rpc-gen/typings.d.ts.map +1 -1
- package/dist/user-api/server.d.ts.map +1 -1
- package/dist/web/frontend/404/index.html +2 -2
- package/dist/web/frontend/activities/index.html +3 -3
- package/dist/web/frontend/api-keys/add/index.html +2 -2
- package/dist/web/frontend/api-keys/index.html +2 -2
- package/dist/web/frontend/approve/index.html +4 -3
- package/dist/web/frontend/assets/404-D6hNG1H8.js +5 -0
- package/dist/web/frontend/assets/{activities-HZnPjlMp.js → activities-CSaNJ4Bw.js} +2 -2
- package/dist/web/frontend/assets/{addApiKey-BvkCtUF3.js → addApiKey-IKOYP6lC.js} +3 -3
- package/dist/web/frontend/assets/addIdentityProvider-CnRS1C3U.js +28 -0
- package/dist/web/frontend/assets/addNetwork-Wq0K3t5Z.js +38 -0
- package/dist/web/frontend/assets/{addParty-Cdoed_Yx.js → addParty-B0GaqKvG.js} +3 -2
- package/dist/web/frontend/assets/{apiKeys-BwV6ylWL.js → apiKeys-BgJyxg5r.js} +2 -2
- package/dist/web/frontend/assets/approve-kKlT_54l.js +27 -0
- package/dist/web/frontend/assets/callback-83Lr1QFW.js +7 -0
- package/dist/web/frontend/assets/common-BkYw_tO_.js +31 -0
- package/dist/web/frontend/assets/{editParty-OOjTHFay.js → editParty-DU9eMclk.js} +3 -2
- package/dist/web/frontend/assets/{identityProviders-BxJAq1Ne.js → identityProviders-sWmbKlSu.js} +2 -2
- package/dist/web/frontend/assets/{index-D1Brmwbg.js → index-AvzB2hln.js} +3 -3
- package/dist/web/frontend/assets/index-BF3EIXoM.js +2 -0
- package/dist/web/frontend/assets/{index-CG_aFABR.js → index-CGK8ht7_.js} +1 -1
- package/dist/web/frontend/assets/index-DRQ0oWml.js +4814 -0
- package/dist/web/frontend/assets/index-NgSpx7q_.js +3 -0
- package/dist/web/frontend/assets/login-BqK1nbQm.js +11 -0
- package/dist/web/frontend/assets/{networks-CBprjUF5.js → networks-C62lWsP4.js} +3 -3
- package/dist/web/frontend/assets/{reviewIdentityProvider-CCmKgN8N.js → reviewIdentityProvider-pILqo6EV.js} +4 -4
- package/dist/web/frontend/assets/{reviewNetwork-CX8Bmuf3.js → reviewNetwork-DZOWqkuG.js} +3 -3
- package/dist/web/frontend/assets/selfIssuedLogin-DarXIpvS.js +81 -0
- package/dist/web/frontend/assets/signMessage-k9b_lLxh.js +23 -0
- package/dist/web/frontend/callback/index.html +3 -2
- package/dist/web/frontend/identity-providers/add/index.html +2 -2
- package/dist/web/frontend/identity-providers/index.html +2 -2
- package/dist/web/frontend/identity-providers/review/index.html +2 -2
- package/dist/web/frontend/index.html +1 -1
- package/dist/web/frontend/login/index.html +3 -2
- package/dist/web/frontend/networks/add/index.html +2 -2
- package/dist/web/frontend/networks/index.html +2 -2
- package/dist/web/frontend/networks/review/index.html +2 -2
- package/dist/web/frontend/parties/add/index.html +5 -5
- package/dist/web/frontend/parties/edit/index.html +5 -5
- package/dist/web/frontend/parties/index.html +3 -3
- package/dist/web/frontend/{onboarding → self-issued-login}/index.html +6 -5
- package/dist/web/frontend/sign-message/index.html +2 -2
- package/dist/web/route-inputs.js +1 -1
- package/dist/web/server.d.ts.map +1 -1
- package/dist/web/server.js +1 -1
- package/package.json +49 -51
- package/dist/auth/jwt-auth-service.test.d.ts +0 -2
- package/dist/auth/jwt-auth-service.test.d.ts.map +0 -1
- package/dist/auth/jwt-auth-service.test.js +0 -659
- package/dist/config/Config.test.d.ts +0 -2
- package/dist/config/Config.test.d.ts.map +0 -1
- package/dist/config/Config.test.js +0 -26
- package/dist/dapp-api/controller.test.d.ts +0 -2
- package/dist/dapp-api/controller.test.d.ts.map +0 -1
- package/dist/dapp-api/controller.test.js +0 -531
- package/dist/dapp-api/server.test.d.ts +0 -2
- package/dist/dapp-api/server.test.d.ts.map +0 -1
- package/dist/dapp-api/server.test.js +0 -79
- package/dist/dapp-api/sse-sink.test.d.ts +0 -2
- package/dist/dapp-api/sse-sink.test.d.ts.map +0 -1
- package/dist/dapp-api/sse-sink.test.js +0 -30
- package/dist/ledger/party-allocation-service.test.d.ts +0 -2
- package/dist/ledger/party-allocation-service.test.d.ts.map +0 -1
- package/dist/ledger/party-allocation-service.test.js +0 -105
- package/dist/ledger/self-issued-auth-service.test.d.ts +0 -2
- package/dist/ledger/self-issued-auth-service.test.d.ts.map +0 -1
- package/dist/ledger/self-issued-auth-service.test.js +0 -454
- package/dist/ledger/wallet-allocation/wallet-allocation-service.test.d.ts +0 -2
- package/dist/ledger/wallet-allocation/wallet-allocation-service.test.d.ts.map +0 -1
- package/dist/ledger/wallet-allocation/wallet-allocation-service.test.js +0 -1005
- package/dist/ledger/wallet-sync-service.test.d.ts +0 -2
- package/dist/ledger/wallet-sync-service.test.d.ts.map +0 -1
- package/dist/ledger/wallet-sync-service.test.js +0 -1017
- package/dist/middleware/apiKeyAuth.test.d.ts +0 -2
- package/dist/middleware/apiKeyAuth.test.d.ts.map +0 -1
- package/dist/middleware/apiKeyAuth.test.js +0 -126
- package/dist/middleware/errorHandler.test.d.ts +0 -2
- package/dist/middleware/errorHandler.test.d.ts.map +0 -1
- package/dist/middleware/errorHandler.test.js +0 -138
- package/dist/middleware/jsonRpcHandler.test.d.ts +0 -2
- package/dist/middleware/jsonRpcHandler.test.d.ts.map +0 -1
- package/dist/middleware/jsonRpcHandler.test.js +0 -377
- package/dist/middleware/jwtAuth.test.d.ts +0 -2
- package/dist/middleware/jwtAuth.test.d.ts.map +0 -1
- package/dist/middleware/jwtAuth.test.js +0 -100
- package/dist/middleware/rateLimit.test.d.ts +0 -2
- package/dist/middleware/rateLimit.test.d.ts.map +0 -1
- package/dist/middleware/rateLimit.test.js +0 -56
- package/dist/middleware/securityHeaders.test.d.ts +0 -2
- package/dist/middleware/securityHeaders.test.d.ts.map +0 -1
- package/dist/middleware/securityHeaders.test.js +0 -18
- package/dist/middleware/sessionHandler.test.d.ts +0 -2
- package/dist/middleware/sessionHandler.test.d.ts.map +0 -1
- package/dist/middleware/sessionHandler.test.js +0 -215
- package/dist/signing/service-account-session.test.d.ts +0 -2
- package/dist/signing/service-account-session.test.d.ts.map +0 -1
- package/dist/signing/service-account-session.test.js +0 -88
- package/dist/signing/signing-worker.test.d.ts +0 -2
- package/dist/signing/signing-worker.test.d.ts.map +0 -1
- package/dist/signing/signing-worker.test.js +0 -158
- package/dist/user-api/controller.test.d.ts +0 -2
- package/dist/user-api/controller.test.d.ts.map +0 -1
- package/dist/user-api/controller.test.js +0 -1228
- package/dist/user-api/server.test.d.ts +0 -2
- package/dist/user-api/server.test.d.ts.map +0 -1
- package/dist/user-api/server.test.js +0 -117
- package/dist/user-api/token-network-matching.test.d.ts +0 -2
- package/dist/user-api/token-network-matching.test.d.ts.map +0 -1
- package/dist/user-api/token-network-matching.test.js +0 -66
- package/dist/utils.test.d.ts +0 -2
- package/dist/utils.test.d.ts.map +0 -1
- package/dist/utils.test.js +0 -24
- package/dist/web/frontend/assets/404-B1XXXMXN.js +0 -5
- package/dist/web/frontend/assets/addIdentityProvider-Dvjma837.js +0 -28
- package/dist/web/frontend/assets/addNetwork-Cahac0uo.js +0 -38
- package/dist/web/frontend/assets/approve-B0nIfe2y.js +0 -26
- package/dist/web/frontend/assets/callback-BieW5Ux2.js +0 -7
- package/dist/web/frontend/assets/common-Bv9twGrA.js +0 -31
- package/dist/web/frontend/assets/index-Bo6MnlYI.js +0 -2
- package/dist/web/frontend/assets/index-pMOoiNFq.js +0 -4887
- package/dist/web/frontend/assets/login-BHxgJEz_.js +0 -11
- package/dist/web/frontend/assets/onboarding-ChmWs-TC.js +0 -84
- package/dist/web/frontend/assets/signMessage-BN_6S5FV.js +0 -79
- package/dist/web/route-inputs.test.d.ts +0 -2
- package/dist/web/route-inputs.test.d.ts.map +0 -1
- package/dist/web/route-inputs.test.js +0 -30
|
@@ -1,1228 +0,0 @@
|
|
|
1
|
-
// Copyright (c) 2025-2026 Digital Asset (Switzerland) GmbH and/or its affiliates. All rights reserved.
|
|
2
|
-
// SPDX-License-Identifier: Apache-2.0
|
|
3
|
-
import { beforeEach, describe, expect, it, vi } from 'vitest';
|
|
4
|
-
import { pino } from 'pino';
|
|
5
|
-
import { sink } from 'pino-test';
|
|
6
|
-
import { PartyLevelRight, } from '@canton-network/core-wallet-store';
|
|
7
|
-
import { StoreInternal } from '@canton-network/core-wallet-store-inmemory';
|
|
8
|
-
import { SigningProvider } from '@canton-network/core-signing-lib';
|
|
9
|
-
import { NotificationService } from '@canton-network/core-wallet-services';
|
|
10
|
-
import { userController } from './controller.js';
|
|
11
|
-
import { getLogger } from '@logtape/logtape';
|
|
12
|
-
const ledgerMocks = vi.hoisted(() => ({
|
|
13
|
-
get: vi.fn(),
|
|
14
|
-
getWithRetry: vi.fn(),
|
|
15
|
-
postWithRetry: vi.fn(),
|
|
16
|
-
getSynchronizerId: vi.fn(),
|
|
17
|
-
}));
|
|
18
|
-
const mockNetworkStatus = vi.hoisted(() => vi.fn().mockResolvedValue({
|
|
19
|
-
isConnected: true,
|
|
20
|
-
reason: undefined,
|
|
21
|
-
cantonVersion: '3.4',
|
|
22
|
-
}));
|
|
23
|
-
const walletAllocationMocks = vi.hoisted(() => ({
|
|
24
|
-
createWallet: vi.fn(),
|
|
25
|
-
allocateParty: vi.fn(),
|
|
26
|
-
}));
|
|
27
|
-
const walletSyncMocks = vi.hoisted(() => ({
|
|
28
|
-
syncWallets: vi.fn().mockResolvedValue({
|
|
29
|
-
added: [],
|
|
30
|
-
updated: [],
|
|
31
|
-
disabled: [],
|
|
32
|
-
}),
|
|
33
|
-
syncRights: vi.fn().mockResolvedValue([]),
|
|
34
|
-
isWalletSyncNeeded: vi.fn().mockResolvedValue(false),
|
|
35
|
-
}));
|
|
36
|
-
const transactionServiceMocks = vi.hoisted(() => ({
|
|
37
|
-
sign: vi.fn(),
|
|
38
|
-
execute: vi.fn(),
|
|
39
|
-
}));
|
|
40
|
-
const mockV4 = vi.hoisted(() => vi.fn(() => crypto.randomUUID()));
|
|
41
|
-
vi.mock('uuid', async (importOriginal) => {
|
|
42
|
-
const actual = await importOriginal();
|
|
43
|
-
return { ...actual, v4: mockV4 };
|
|
44
|
-
});
|
|
45
|
-
vi.mock('@canton-network/core-ledger-client', async (importOriginal) => {
|
|
46
|
-
const actual = await importOriginal();
|
|
47
|
-
return {
|
|
48
|
-
...actual,
|
|
49
|
-
LedgerClient: vi.fn(function LedgerClientMock() {
|
|
50
|
-
return {
|
|
51
|
-
get: ledgerMocks.get,
|
|
52
|
-
getWithRetry: ledgerMocks.getWithRetry,
|
|
53
|
-
postWithRetry: ledgerMocks.postWithRetry,
|
|
54
|
-
getSynchronizerId: ledgerMocks.getSynchronizerId,
|
|
55
|
-
};
|
|
56
|
-
}),
|
|
57
|
-
};
|
|
58
|
-
});
|
|
59
|
-
vi.mock('@canton-network/core-wallet-services', async (importOriginal) => {
|
|
60
|
-
const actual = await importOriginal();
|
|
61
|
-
return {
|
|
62
|
-
...actual,
|
|
63
|
-
networkStatus: mockNetworkStatus,
|
|
64
|
-
TransactionService: vi.fn(function TransactionServiceMock() {
|
|
65
|
-
return transactionServiceMocks;
|
|
66
|
-
}),
|
|
67
|
-
};
|
|
68
|
-
});
|
|
69
|
-
vi.mock('../ledger/wallet-allocation/wallet-allocation-service.js', () => ({
|
|
70
|
-
WalletAllocationService: vi.fn(function WalletAllocationServiceMock() {
|
|
71
|
-
return walletAllocationMocks;
|
|
72
|
-
}),
|
|
73
|
-
}));
|
|
74
|
-
vi.mock('../ledger/wallet-sync-service.js', () => ({
|
|
75
|
-
WalletSyncService: vi.fn(function WalletSyncServiceMock() {
|
|
76
|
-
return walletSyncMocks;
|
|
77
|
-
}),
|
|
78
|
-
}));
|
|
79
|
-
vi.mock('../ledger/party-allocation-service.js', () => ({
|
|
80
|
-
PartyAllocationService: vi.fn(),
|
|
81
|
-
}));
|
|
82
|
-
const kernelInfo = {
|
|
83
|
-
id: 'kernel-test',
|
|
84
|
-
clientType: 'browser',
|
|
85
|
-
};
|
|
86
|
-
const userUrl = 'https://user.example';
|
|
87
|
-
const regularUserId = 'user-1';
|
|
88
|
-
const adminUserId = 'admin-user';
|
|
89
|
-
const idp = {
|
|
90
|
-
id: 'idp1',
|
|
91
|
-
type: 'oauth',
|
|
92
|
-
issuer: 'http://auth',
|
|
93
|
-
configUrl: 'http://auth/.well-known/openid-configuration',
|
|
94
|
-
};
|
|
95
|
-
const storeNetwork = {
|
|
96
|
-
id: 'network1',
|
|
97
|
-
name: 'testnet',
|
|
98
|
-
synchronizerId: 'sync1::fingerprint',
|
|
99
|
-
description: 'Test',
|
|
100
|
-
identityProviderId: 'idp1',
|
|
101
|
-
ledgerApi: { baseUrl: 'http://ledger.test' },
|
|
102
|
-
auth: {
|
|
103
|
-
method: 'authorization_code',
|
|
104
|
-
clientId: 'cid',
|
|
105
|
-
scope: 'scope1 scope2',
|
|
106
|
-
audience: 'aud',
|
|
107
|
-
},
|
|
108
|
-
adminAuth: {
|
|
109
|
-
method: 'client_credentials',
|
|
110
|
-
clientId: 'admin-cid',
|
|
111
|
-
clientSecret: 'admin-secret',
|
|
112
|
-
audience: 'admin-aud',
|
|
113
|
-
scope: 'admin-scope',
|
|
114
|
-
},
|
|
115
|
-
};
|
|
116
|
-
const auth = {
|
|
117
|
-
userId: regularUserId,
|
|
118
|
-
accessToken: 'access-token-1',
|
|
119
|
-
};
|
|
120
|
-
const adminAuth = {
|
|
121
|
-
userId: adminUserId,
|
|
122
|
-
accessToken: 'admin-access-token',
|
|
123
|
-
};
|
|
124
|
-
const session = {
|
|
125
|
-
id: 'session-1',
|
|
126
|
-
origin: 'dapp-1',
|
|
127
|
-
network: 'network1',
|
|
128
|
-
accessToken: 'access-token-1',
|
|
129
|
-
};
|
|
130
|
-
const pendingTransaction = {
|
|
131
|
-
id: 'tx-1',
|
|
132
|
-
commandId: 'cmd-1',
|
|
133
|
-
status: 'pending',
|
|
134
|
-
preparedTransaction: 'blob',
|
|
135
|
-
preparedTransactionHash: 'hash',
|
|
136
|
-
origin: null,
|
|
137
|
-
createdAt: new Date('2026-01-01T00:00:00.000Z'),
|
|
138
|
-
};
|
|
139
|
-
const primaryWallet = {
|
|
140
|
-
primary: true,
|
|
141
|
-
partyId: 'party::namespace',
|
|
142
|
-
status: 'allocated',
|
|
143
|
-
hint: 'party',
|
|
144
|
-
signingProviderId: SigningProvider.WALLET_KERNEL,
|
|
145
|
-
publicKey: 'wallet-public-key',
|
|
146
|
-
namespace: 'namespace',
|
|
147
|
-
networkId: 'network1',
|
|
148
|
-
userId: 'user-1',
|
|
149
|
-
rights: [PartyLevelRight.CanActAs],
|
|
150
|
-
};
|
|
151
|
-
const participantWallet = {
|
|
152
|
-
...primaryWallet,
|
|
153
|
-
partyId: 'party::participant',
|
|
154
|
-
signingProviderId: SigningProvider.PARTICIPANT,
|
|
155
|
-
};
|
|
156
|
-
async function createStore(logger, context, options = {}) {
|
|
157
|
-
const { withSession = true, withWallet = true } = options;
|
|
158
|
-
const store = new StoreInternal({ idps: [idp], networks: [storeNetwork] }, getLogger('mock'), context);
|
|
159
|
-
if (context && withSession) {
|
|
160
|
-
await store.setSession(session);
|
|
161
|
-
}
|
|
162
|
-
if (context && withWallet) {
|
|
163
|
-
await store.addWallet(primaryWallet);
|
|
164
|
-
}
|
|
165
|
-
return store;
|
|
166
|
-
}
|
|
167
|
-
function createController(store, notificationService, logger, context, drivers = {}, adminId) {
|
|
168
|
-
return userController(kernelInfo, userUrl, store, notificationService, context, drivers, logger, 'HASHING_SCHEME_VERSION_V3', adminId);
|
|
169
|
-
}
|
|
170
|
-
describe('userController', () => {
|
|
171
|
-
let logger;
|
|
172
|
-
let notificationService;
|
|
173
|
-
beforeEach(() => {
|
|
174
|
-
logger = pino({ level: 'silent' }, sink());
|
|
175
|
-
notificationService = new NotificationService(logger);
|
|
176
|
-
ledgerMocks.get.mockReset();
|
|
177
|
-
ledgerMocks.get.mockRejectedValue({
|
|
178
|
-
code: 'USER_NOT_FOUND',
|
|
179
|
-
cause: 'missing',
|
|
180
|
-
errorCategory: 11,
|
|
181
|
-
});
|
|
182
|
-
ledgerMocks.getWithRetry.mockReset();
|
|
183
|
-
ledgerMocks.getWithRetry.mockResolvedValue({ rights: [] });
|
|
184
|
-
ledgerMocks.postWithRetry.mockReset();
|
|
185
|
-
ledgerMocks.getSynchronizerId.mockReset();
|
|
186
|
-
mockNetworkStatus.mockReset();
|
|
187
|
-
mockNetworkStatus.mockResolvedValue({
|
|
188
|
-
isConnected: true,
|
|
189
|
-
reason: undefined,
|
|
190
|
-
cantonVersion: '3.4',
|
|
191
|
-
});
|
|
192
|
-
walletAllocationMocks.createWallet.mockReset();
|
|
193
|
-
walletAllocationMocks.allocateParty.mockReset();
|
|
194
|
-
walletSyncMocks.syncWallets.mockReset();
|
|
195
|
-
walletSyncMocks.syncWallets.mockResolvedValue({
|
|
196
|
-
added: [],
|
|
197
|
-
updated: [],
|
|
198
|
-
disabled: [],
|
|
199
|
-
});
|
|
200
|
-
walletSyncMocks.syncRights.mockReset();
|
|
201
|
-
walletSyncMocks.syncRights.mockResolvedValue([]);
|
|
202
|
-
walletSyncMocks.isWalletSyncNeeded.mockReset();
|
|
203
|
-
walletSyncMocks.isWalletSyncNeeded.mockResolvedValue(false);
|
|
204
|
-
transactionServiceMocks.sign.mockReset();
|
|
205
|
-
transactionServiceMocks.execute.mockReset();
|
|
206
|
-
});
|
|
207
|
-
describe('addSelfIssuedSession', () => {
|
|
208
|
-
it('creates a tokenless session for the username and network', async () => {
|
|
209
|
-
const selfIssuedNetwork = {
|
|
210
|
-
...storeNetwork,
|
|
211
|
-
id: 'self-issued-network',
|
|
212
|
-
auth: { method: 'self_issued' },
|
|
213
|
-
};
|
|
214
|
-
const store = new StoreInternal({ idps: [idp], networks: [selfIssuedNetwork] }, getLogger('mock'));
|
|
215
|
-
const controller = createController(store, notificationService, logger, undefined);
|
|
216
|
-
const { sessionId } = await controller.addSelfIssuedSession({
|
|
217
|
-
username: 'alice',
|
|
218
|
-
networkId: selfIssuedNetwork.id,
|
|
219
|
-
origin: 'https://example.com',
|
|
220
|
-
});
|
|
221
|
-
await expect(store.getOnboardingSession(sessionId)).resolves.toEqual(expect.objectContaining({
|
|
222
|
-
id: sessionId,
|
|
223
|
-
origin: 'https://example.com',
|
|
224
|
-
network: selfIssuedNetwork.id,
|
|
225
|
-
userId: 'alice',
|
|
226
|
-
}));
|
|
227
|
-
const onboardingStore = store.withAuthContext({
|
|
228
|
-
userId: 'alice',
|
|
229
|
-
accessToken: '',
|
|
230
|
-
sessionId,
|
|
231
|
-
});
|
|
232
|
-
await expect(onboardingStore.getCurrentNetwork()).resolves.toEqual(selfIssuedNetwork);
|
|
233
|
-
await expect(store
|
|
234
|
-
.withAuthContext({ userId: 'alice', accessToken: '' })
|
|
235
|
-
.getCurrentNetwork()).rejects.toThrow('No session found');
|
|
236
|
-
});
|
|
237
|
-
it('does not create a session for an existing user without an authentication party', async () => {
|
|
238
|
-
const selfIssuedNetwork = {
|
|
239
|
-
...storeNetwork,
|
|
240
|
-
id: 'self-issued-network',
|
|
241
|
-
auth: { method: 'self_issued' },
|
|
242
|
-
};
|
|
243
|
-
const store = new StoreInternal({ idps: [idp], networks: [selfIssuedNetwork] }, getLogger('mock'));
|
|
244
|
-
ledgerMocks.get.mockResolvedValue({
|
|
245
|
-
user: { id: 'alice', primaryParty: 'alice::ns' },
|
|
246
|
-
});
|
|
247
|
-
await expect(createController(store, notificationService, logger, undefined).addSelfIssuedSession({
|
|
248
|
-
username: 'alice',
|
|
249
|
-
networkId: selfIssuedNetwork.id,
|
|
250
|
-
origin: 'https://example.com',
|
|
251
|
-
})).rejects.toThrow('Self-issued login is not available for this user.');
|
|
252
|
-
});
|
|
253
|
-
});
|
|
254
|
-
describe('getUser', () => {
|
|
255
|
-
it('returns user id and isAdmin false for a regular user', async () => {
|
|
256
|
-
const store = await createStore(logger, auth);
|
|
257
|
-
const controller = createController(store, notificationService, logger, auth, {}, adminUserId);
|
|
258
|
-
await expect(controller.getUser()).resolves.toEqual({
|
|
259
|
-
userId: regularUserId,
|
|
260
|
-
isAdmin: false,
|
|
261
|
-
});
|
|
262
|
-
});
|
|
263
|
-
it('returns isAdmin true when the user matches adminUserId', async () => {
|
|
264
|
-
const store = await createStore(logger, adminAuth);
|
|
265
|
-
const controller = createController(store, notificationService, logger, adminAuth, {}, adminUserId);
|
|
266
|
-
await expect(controller.getUser()).resolves.toEqual({
|
|
267
|
-
userId: adminUserId,
|
|
268
|
-
isAdmin: true,
|
|
269
|
-
});
|
|
270
|
-
});
|
|
271
|
-
it('throws when auth context is missing', async () => {
|
|
272
|
-
const store = await createStore(logger, auth, {
|
|
273
|
-
withSession: false,
|
|
274
|
-
});
|
|
275
|
-
const controller = createController(store, notificationService, logger, undefined);
|
|
276
|
-
await expect(controller.getUser()).rejects.toThrow();
|
|
277
|
-
});
|
|
278
|
-
});
|
|
279
|
-
describe('networks', () => {
|
|
280
|
-
it('rejects addNetwork for non-admin users', async () => {
|
|
281
|
-
const store = await createStore(logger, auth);
|
|
282
|
-
const controller = createController(store, notificationService, logger, auth, {}, adminUserId);
|
|
283
|
-
await expect(controller.addNetwork({
|
|
284
|
-
network: {
|
|
285
|
-
id: 'net-new',
|
|
286
|
-
name: 'New Net',
|
|
287
|
-
synchronizerId: 'sync::fingerprint',
|
|
288
|
-
identityProviderId: 'idp1',
|
|
289
|
-
ledgerApi: 'http://ledger.new',
|
|
290
|
-
auth: storeNetwork.auth,
|
|
291
|
-
description: 'description',
|
|
292
|
-
},
|
|
293
|
-
})).rejects.toThrow('Unauthorized');
|
|
294
|
-
});
|
|
295
|
-
it('adds a new network for the admin user', async () => {
|
|
296
|
-
const store = await createStore(logger, adminAuth);
|
|
297
|
-
const addSpy = vi.spyOn(store, 'addNetwork');
|
|
298
|
-
const controller = createController(store, notificationService, logger, adminAuth, {}, adminUserId);
|
|
299
|
-
await controller.addNetwork({
|
|
300
|
-
network: {
|
|
301
|
-
id: 'net-new',
|
|
302
|
-
name: 'New Net',
|
|
303
|
-
synchronizerId: 'sync::fingerprint',
|
|
304
|
-
identityProviderId: 'idp1',
|
|
305
|
-
ledgerApi: 'http://ledger.new',
|
|
306
|
-
auth: storeNetwork.auth,
|
|
307
|
-
description: 'description',
|
|
308
|
-
},
|
|
309
|
-
});
|
|
310
|
-
expect(addSpy).toHaveBeenCalledWith(expect.objectContaining({
|
|
311
|
-
id: 'net-new',
|
|
312
|
-
ledgerApi: { baseUrl: 'http://ledger.new' },
|
|
313
|
-
}));
|
|
314
|
-
});
|
|
315
|
-
it('rejects a new network referencing an unknown identity provider', async () => {
|
|
316
|
-
const store = await createStore(logger, adminAuth);
|
|
317
|
-
const addSpy = vi.spyOn(store, 'addNetwork');
|
|
318
|
-
const controller = createController(store, notificationService, logger, adminAuth, {}, adminUserId);
|
|
319
|
-
await expect(controller.addNetwork({
|
|
320
|
-
network: {
|
|
321
|
-
id: 'net-new',
|
|
322
|
-
name: 'New Net',
|
|
323
|
-
synchronizerId: 'sync::fingerprint',
|
|
324
|
-
identityProviderId: 'missing-idp',
|
|
325
|
-
ledgerApi: 'http://ledger.new',
|
|
326
|
-
auth: storeNetwork.auth,
|
|
327
|
-
description: 'description',
|
|
328
|
-
},
|
|
329
|
-
})).rejects.toThrow('Identity provider "missing-idp" not found');
|
|
330
|
-
expect(addSpy).not.toHaveBeenCalled();
|
|
331
|
-
});
|
|
332
|
-
it('updates an existing network for the admin user', async () => {
|
|
333
|
-
const store = await createStore(logger, adminAuth);
|
|
334
|
-
const updateSpy = vi.spyOn(store, 'updateNetwork');
|
|
335
|
-
const controller = createController(store, notificationService, logger, adminAuth, {}, adminUserId);
|
|
336
|
-
await controller.addNetwork({
|
|
337
|
-
network: {
|
|
338
|
-
id: 'network1',
|
|
339
|
-
name: 'Renamed Net',
|
|
340
|
-
synchronizerId: storeNetwork.synchronizerId,
|
|
341
|
-
identityProviderId: 'idp1',
|
|
342
|
-
ledgerApi: 'http://ledger.updated',
|
|
343
|
-
auth: storeNetwork.auth,
|
|
344
|
-
description: 'description',
|
|
345
|
-
},
|
|
346
|
-
});
|
|
347
|
-
expect(updateSpy).toHaveBeenCalledWith(expect.objectContaining({
|
|
348
|
-
id: 'network1',
|
|
349
|
-
name: 'Renamed Net',
|
|
350
|
-
}));
|
|
351
|
-
});
|
|
352
|
-
it('rejects an update with an unknown auth identity provider override', async () => {
|
|
353
|
-
const store = await createStore(logger, adminAuth);
|
|
354
|
-
const updateSpy = vi.spyOn(store, 'updateNetwork');
|
|
355
|
-
const controller = createController(store, notificationService, logger, adminAuth, {}, adminUserId);
|
|
356
|
-
await expect(controller.addNetwork({
|
|
357
|
-
network: {
|
|
358
|
-
id: 'network1',
|
|
359
|
-
name: 'Renamed Net',
|
|
360
|
-
synchronizerId: storeNetwork.synchronizerId,
|
|
361
|
-
identityProviderId: 'idp1',
|
|
362
|
-
ledgerApi: 'http://ledger.updated',
|
|
363
|
-
auth: storeNetwork.auth,
|
|
364
|
-
adminAuth: {
|
|
365
|
-
method: 'client_credentials',
|
|
366
|
-
identityProviderId: 'missing-admin-idp',
|
|
367
|
-
clientId: 'admin-cid',
|
|
368
|
-
clientSecret: 'admin-secret',
|
|
369
|
-
audience: 'admin-aud',
|
|
370
|
-
scope: 'admin-scope',
|
|
371
|
-
},
|
|
372
|
-
description: 'description',
|
|
373
|
-
},
|
|
374
|
-
})).rejects.toThrow('Identity provider "missing-admin-idp" not found');
|
|
375
|
-
expect(updateSpy).not.toHaveBeenCalled();
|
|
376
|
-
});
|
|
377
|
-
it('removes a network for the admin user', async () => {
|
|
378
|
-
const store = await createStore(logger, adminAuth);
|
|
379
|
-
const removeSpy = vi.spyOn(store, 'removeNetwork');
|
|
380
|
-
const controller = createController(store, notificationService, logger, adminAuth, {}, adminUserId);
|
|
381
|
-
await controller.removeNetwork({ networkName: 'testnet' });
|
|
382
|
-
expect(removeSpy).toHaveBeenCalledWith('testnet');
|
|
383
|
-
});
|
|
384
|
-
it('lists networks with ledgerApi as a string', async () => {
|
|
385
|
-
const store = await createStore(logger, auth);
|
|
386
|
-
const controller = createController(store, notificationService, logger, auth);
|
|
387
|
-
const result = await controller.listNetworks();
|
|
388
|
-
expect(result.networks).toHaveLength(1);
|
|
389
|
-
expect(result.networks[0]).toMatchObject({
|
|
390
|
-
id: 'network1',
|
|
391
|
-
ledgerApi: 'http://ledger.test',
|
|
392
|
-
});
|
|
393
|
-
});
|
|
394
|
-
});
|
|
395
|
-
describe('selfSignedAccessToken', () => {
|
|
396
|
-
const selfSignedIdp = {
|
|
397
|
-
id: 'idp-self-signed',
|
|
398
|
-
type: 'self_signed',
|
|
399
|
-
issuer: 'unsafe-auth',
|
|
400
|
-
};
|
|
401
|
-
const selfSignedNetwork = {
|
|
402
|
-
id: 'network-self-signed',
|
|
403
|
-
name: 'Self Signed',
|
|
404
|
-
description: 'Test',
|
|
405
|
-
identityProviderId: 'idp-self-signed',
|
|
406
|
-
ledgerApi: { baseUrl: 'http://ledger.test' },
|
|
407
|
-
auth: {
|
|
408
|
-
method: 'self_signed',
|
|
409
|
-
issuer: 'self-signed',
|
|
410
|
-
audience: 'aud',
|
|
411
|
-
scope: 'scope',
|
|
412
|
-
clientId: 'operator',
|
|
413
|
-
clientSecret: 'network-secret',
|
|
414
|
-
},
|
|
415
|
-
};
|
|
416
|
-
it('mints a token when the client secret matches the network', async () => {
|
|
417
|
-
const store = new StoreInternal({ idps: [selfSignedIdp], networks: [selfSignedNetwork] }, getLogger('mock'));
|
|
418
|
-
const controller = createController(store, notificationService, logger, undefined);
|
|
419
|
-
const result = await controller.selfSignedAccessToken({
|
|
420
|
-
networkId: 'network-self-signed',
|
|
421
|
-
clientId: 'test-user',
|
|
422
|
-
clientSecret: 'network-secret',
|
|
423
|
-
});
|
|
424
|
-
expect(typeof result.accessToken).toBe('string');
|
|
425
|
-
const header = JSON.parse(Buffer.from(result.accessToken.split('.')[0], 'base64url').toString());
|
|
426
|
-
const payload = JSON.parse(Buffer.from(result.accessToken.split('.')[1], 'base64url').toString());
|
|
427
|
-
expect(header.kid).toBe('network-self-signed');
|
|
428
|
-
expect(payload.sub).toBe('test-user');
|
|
429
|
-
});
|
|
430
|
-
it('rejects when the client secret does not match', async () => {
|
|
431
|
-
const store = new StoreInternal({ idps: [selfSignedIdp], networks: [selfSignedNetwork] }, getLogger('mock'));
|
|
432
|
-
const controller = createController(store, notificationService, logger, undefined);
|
|
433
|
-
await expect(controller.selfSignedAccessToken({
|
|
434
|
-
networkId: 'network-self-signed',
|
|
435
|
-
clientId: 'test-user',
|
|
436
|
-
clientSecret: 'wrong-secret',
|
|
437
|
-
})).rejects.toThrow('Invalid client secret');
|
|
438
|
-
});
|
|
439
|
-
});
|
|
440
|
-
describe('idps', () => {
|
|
441
|
-
it('adds and removes an idp for the admin user', async () => {
|
|
442
|
-
const store = await createStore(logger, adminAuth);
|
|
443
|
-
const addIdpSpy = vi.spyOn(store, 'addIdp');
|
|
444
|
-
const removeIdpSpy = vi.spyOn(store, 'removeIdp');
|
|
445
|
-
const controller = createController(store, notificationService, logger, adminAuth, {}, adminUserId);
|
|
446
|
-
const newIdp = {
|
|
447
|
-
id: 'idp-new',
|
|
448
|
-
type: 'self_signed',
|
|
449
|
-
issuer: 'self',
|
|
450
|
-
};
|
|
451
|
-
await controller.addIdp({ idp: newIdp });
|
|
452
|
-
expect(addIdpSpy).toHaveBeenCalledWith(newIdp);
|
|
453
|
-
await controller.removeIdp({ identityProviderId: 'idp-new' });
|
|
454
|
-
expect(removeIdpSpy).toHaveBeenCalledWith('idp-new');
|
|
455
|
-
});
|
|
456
|
-
it('updates an existing idp for the admin user', async () => {
|
|
457
|
-
const store = await createStore(logger, adminAuth);
|
|
458
|
-
const updateIdpSpy = vi.spyOn(store, 'updateIdp');
|
|
459
|
-
const controller = createController(store, notificationService, logger, adminAuth, {}, adminUserId);
|
|
460
|
-
const updatedIdp = {
|
|
461
|
-
...idp,
|
|
462
|
-
issuer: 'http://auth-updated',
|
|
463
|
-
};
|
|
464
|
-
await controller.addIdp({ idp: updatedIdp });
|
|
465
|
-
expect(updateIdpSpy).toHaveBeenCalledWith(updatedIdp);
|
|
466
|
-
});
|
|
467
|
-
it('lists idps from the store', async () => {
|
|
468
|
-
const store = await createStore(logger, auth);
|
|
469
|
-
const controller = createController(store, notificationService, logger, auth);
|
|
470
|
-
await expect(controller.listIdps()).resolves.toEqual({
|
|
471
|
-
idps: [idp],
|
|
472
|
-
});
|
|
473
|
-
});
|
|
474
|
-
});
|
|
475
|
-
describe('setPrimaryWallet', () => {
|
|
476
|
-
it('sets the primary wallet and emits accountsChanged', async () => {
|
|
477
|
-
const store = await createStore(logger, auth);
|
|
478
|
-
const setPrimarySpy = vi.spyOn(store, 'setPrimaryWallet');
|
|
479
|
-
const notifier = notificationService.getNotifier('user-1');
|
|
480
|
-
const emitSpy = vi.spyOn(notifier, 'emit');
|
|
481
|
-
const controller = createController(store, notificationService, logger, auth);
|
|
482
|
-
await controller.setPrimaryWallet({ partyId: 'party::namespace' });
|
|
483
|
-
expect(setPrimarySpy).toHaveBeenCalledWith('party::namespace');
|
|
484
|
-
expect(emitSpy).toHaveBeenCalledWith('accountsChanged', expect.arrayContaining([
|
|
485
|
-
expect.objectContaining({ partyId: 'party::namespace' }),
|
|
486
|
-
]));
|
|
487
|
-
});
|
|
488
|
-
});
|
|
489
|
-
describe('listWallets', () => {
|
|
490
|
-
it('returns wallets from the store', async () => {
|
|
491
|
-
const store = await createStore(logger, auth);
|
|
492
|
-
const controller = createController(store, notificationService, logger, auth);
|
|
493
|
-
const wallets = await controller.listWallets({});
|
|
494
|
-
expect(wallets).toHaveLength(1);
|
|
495
|
-
expect(wallets[0]?.partyId).toBe('party::namespace');
|
|
496
|
-
});
|
|
497
|
-
});
|
|
498
|
-
describe('message signing', () => {
|
|
499
|
-
const pendingMessage = {
|
|
500
|
-
id: 'msg-1',
|
|
501
|
-
status: 'pending',
|
|
502
|
-
userId: auth.userId,
|
|
503
|
-
partyId: primaryWallet.partyId,
|
|
504
|
-
publicKey: primaryWallet.publicKey,
|
|
505
|
-
message: 'Sign this',
|
|
506
|
-
origin: 'https://dapp.example',
|
|
507
|
-
createdAt: new Date('2026-01-01T00:00:00.000Z'),
|
|
508
|
-
};
|
|
509
|
-
async function storeWithMessage(message = pendingMessage) {
|
|
510
|
-
const store = await createStore(logger, auth);
|
|
511
|
-
await store.setMessageRaw(message);
|
|
512
|
-
return store;
|
|
513
|
-
}
|
|
514
|
-
it('returns message details via getMessageToSign', async () => {
|
|
515
|
-
const store = await storeWithMessage();
|
|
516
|
-
const controller = createController(store, notificationService, logger, auth);
|
|
517
|
-
const result = await controller.getMessageToSign({
|
|
518
|
-
messageId: 'msg-1',
|
|
519
|
-
});
|
|
520
|
-
expect(result.message).toMatchObject({
|
|
521
|
-
id: 'msg-1',
|
|
522
|
-
status: 'pending',
|
|
523
|
-
message: 'Sign this',
|
|
524
|
-
origin: 'https://dapp.example',
|
|
525
|
-
createdAt: pendingMessage.createdAt.toISOString(),
|
|
526
|
-
});
|
|
527
|
-
});
|
|
528
|
-
it('lists all messages to sign', async () => {
|
|
529
|
-
const store = await storeWithMessage();
|
|
530
|
-
const controller = createController(store, notificationService, logger, auth);
|
|
531
|
-
const result = await controller.listMessagesToSign();
|
|
532
|
-
expect(result.messages).toHaveLength(1);
|
|
533
|
-
expect(result.messages[0]?.id).toBe('msg-1');
|
|
534
|
-
});
|
|
535
|
-
it('deletes a pending message owned by the user and emits a failed event', async () => {
|
|
536
|
-
const store = await storeWithMessage();
|
|
537
|
-
const removeSpy = vi.spyOn(store, 'removeMessageRaw');
|
|
538
|
-
const emitSpy = vi.spyOn(notificationService.getNotifier(session.id), 'emit');
|
|
539
|
-
const controller = createController(store, notificationService, logger, auth);
|
|
540
|
-
await controller.deleteMessageToSign({ messageId: 'msg-1' });
|
|
541
|
-
expect(removeSpy).toHaveBeenCalledWith('msg-1');
|
|
542
|
-
expect(emitSpy).toHaveBeenCalledExactlyOnceWith('messageSignature', {
|
|
543
|
-
status: 'failed',
|
|
544
|
-
messageId: 'msg-1',
|
|
545
|
-
});
|
|
546
|
-
expect(removeSpy.mock.invocationCallOrder[0]).toBeLessThan(emitSpy.mock.invocationCallOrder[0]);
|
|
547
|
-
});
|
|
548
|
-
it('rejects delete when the message is not pending', async () => {
|
|
549
|
-
const store = await storeWithMessage({
|
|
550
|
-
...pendingMessage,
|
|
551
|
-
status: 'signed',
|
|
552
|
-
});
|
|
553
|
-
const controller = createController(store, notificationService, logger, auth);
|
|
554
|
-
await expect(controller.deleteMessageToSign({ messageId: 'msg-1' })).rejects.toThrow("Cannot delete message with status 'signed'");
|
|
555
|
-
});
|
|
556
|
-
it('rejects delete when the message belongs to another user', async () => {
|
|
557
|
-
const store = await storeWithMessage();
|
|
558
|
-
vi.spyOn(store, 'getMessageRaw').mockResolvedValue({
|
|
559
|
-
...pendingMessage,
|
|
560
|
-
userId: 'other-user',
|
|
561
|
-
});
|
|
562
|
-
const controller = createController(store, notificationService, logger, auth);
|
|
563
|
-
await expect(controller.deleteMessageToSign({ messageId: 'msg-1' })).rejects.toThrow('not owned by user');
|
|
564
|
-
});
|
|
565
|
-
it('signs a pending WALLET_KERNEL message and emits messageSignature', async () => {
|
|
566
|
-
const store = await storeWithMessage();
|
|
567
|
-
const mockSignMessage = vi.fn().mockResolvedValue({
|
|
568
|
-
signature: 'signature',
|
|
569
|
-
});
|
|
570
|
-
const drivers = {
|
|
571
|
-
[SigningProvider.WALLET_KERNEL]: {
|
|
572
|
-
controller: vi.fn(() => ({ signMessage: mockSignMessage })),
|
|
573
|
-
},
|
|
574
|
-
};
|
|
575
|
-
const notifier = notificationService.getNotifier('session-1');
|
|
576
|
-
const emitSpy = vi.spyOn(notifier, 'emit');
|
|
577
|
-
const controller = createController(store, notificationService, logger, auth, drivers);
|
|
578
|
-
const result = await controller.signMessage({ messageId: 'msg-1' });
|
|
579
|
-
expect(mockSignMessage).toHaveBeenCalledWith({
|
|
580
|
-
message: 'Sign this',
|
|
581
|
-
keyIdentifier: { publicKey: primaryWallet.publicKey },
|
|
582
|
-
});
|
|
583
|
-
expect(result).toEqual({
|
|
584
|
-
signature: 'signature',
|
|
585
|
-
publicKey: primaryWallet.publicKey,
|
|
586
|
-
});
|
|
587
|
-
expect(emitSpy).toHaveBeenCalledWith('messageSignature', {
|
|
588
|
-
status: 'signed',
|
|
589
|
-
messageId: 'msg-1',
|
|
590
|
-
signature: 'signature',
|
|
591
|
-
});
|
|
592
|
-
const updated = await store.getMessageRaw('msg-1');
|
|
593
|
-
expect(updated?.status).toBe('signed');
|
|
594
|
-
expect(updated?.signature).toBe('signature');
|
|
595
|
-
});
|
|
596
|
-
it('rejects signMessage for non-WALLET_KERNEL wallets', async () => {
|
|
597
|
-
const store = await createStore(logger, auth);
|
|
598
|
-
await store.removeWallet(primaryWallet.partyId);
|
|
599
|
-
await store.addWallet({
|
|
600
|
-
...primaryWallet,
|
|
601
|
-
signingProviderId: SigningProvider.FIREBLOCKS,
|
|
602
|
-
});
|
|
603
|
-
await store.setMessageRaw(pendingMessage);
|
|
604
|
-
const notifier = notificationService.getNotifier('session-1');
|
|
605
|
-
const emitSpy = vi.spyOn(notifier, 'emit');
|
|
606
|
-
const controller = createController(store, notificationService, logger, auth, {
|
|
607
|
-
[SigningProvider.WALLET_KERNEL]: {
|
|
608
|
-
controller: vi.fn(() => ({
|
|
609
|
-
signMessage: vi.fn(),
|
|
610
|
-
})),
|
|
611
|
-
},
|
|
612
|
-
});
|
|
613
|
-
await expect(controller.signMessage({ messageId: 'msg-1' })).rejects.toThrow('only supported for wallet-kernel');
|
|
614
|
-
expect(emitSpy).toHaveBeenCalledWith('messageSignature', {
|
|
615
|
-
status: 'failed',
|
|
616
|
-
messageId: 'msg-1',
|
|
617
|
-
});
|
|
618
|
-
});
|
|
619
|
-
});
|
|
620
|
-
describe('transactions', () => {
|
|
621
|
-
const transaction = pendingTransaction;
|
|
622
|
-
it('returns transaction details via getTransaction', async () => {
|
|
623
|
-
const store = await createStore(logger, auth);
|
|
624
|
-
await store.setTransaction(transaction);
|
|
625
|
-
const controller = createController(store, notificationService, logger, auth);
|
|
626
|
-
const result = await controller.getTransaction({
|
|
627
|
-
transactionId: 'tx-1',
|
|
628
|
-
});
|
|
629
|
-
expect(result).toMatchObject({
|
|
630
|
-
id: 'tx-1',
|
|
631
|
-
commandId: 'cmd-1',
|
|
632
|
-
status: 'pending',
|
|
633
|
-
createdAt: transaction.createdAt.toISOString(),
|
|
634
|
-
});
|
|
635
|
-
});
|
|
636
|
-
it('lists transactions', async () => {
|
|
637
|
-
const store = await createStore(logger, auth);
|
|
638
|
-
await store.setTransaction(transaction);
|
|
639
|
-
const controller = createController(store, notificationService, logger, auth);
|
|
640
|
-
const result = await controller.listTransactions({});
|
|
641
|
-
expect(result.transactions).toHaveLength(1);
|
|
642
|
-
expect(result.transactions[0]?.id).toBe('tx-1');
|
|
643
|
-
});
|
|
644
|
-
it('deletes a pending transaction and emits a failed event', async () => {
|
|
645
|
-
const store = await createStore(logger, auth);
|
|
646
|
-
await store.setTransaction(transaction);
|
|
647
|
-
const removeSpy = vi.spyOn(store, 'removeTransaction');
|
|
648
|
-
const emitSpy = vi.spyOn(notificationService.getNotifier(session.id), 'emit');
|
|
649
|
-
const controller = createController(store, notificationService, logger, auth);
|
|
650
|
-
await controller.deleteTransaction({ transactionId: 'tx-1' });
|
|
651
|
-
expect(removeSpy).toHaveBeenCalledWith('tx-1');
|
|
652
|
-
expect(emitSpy).toHaveBeenCalledOnce();
|
|
653
|
-
expect(emitSpy).toHaveBeenCalledWith('txChanged', {
|
|
654
|
-
status: 'failed',
|
|
655
|
-
commandId: transaction.commandId,
|
|
656
|
-
});
|
|
657
|
-
expect(removeSpy.mock.invocationCallOrder[0]).toBeLessThan(emitSpy.mock.invocationCallOrder[0]);
|
|
658
|
-
});
|
|
659
|
-
it('rejects delete when the transaction is not pending', async () => {
|
|
660
|
-
const store = await createStore(logger, auth);
|
|
661
|
-
await store.setTransaction({ ...transaction, status: 'signed' });
|
|
662
|
-
const controller = createController(store, notificationService, logger, auth);
|
|
663
|
-
await expect(controller.deleteTransaction({ transactionId: 'tx-1' })).rejects.toThrow("Cannot delete transaction with status 'signed'");
|
|
664
|
-
});
|
|
665
|
-
});
|
|
666
|
-
describe('sign', () => {
|
|
667
|
-
it('throws when there is no network session', async () => {
|
|
668
|
-
const store = await createStore(logger, auth, {
|
|
669
|
-
withSession: false,
|
|
670
|
-
});
|
|
671
|
-
const controller = createController(store, notificationService, logger, auth);
|
|
672
|
-
await expect(controller.sign({
|
|
673
|
-
transactionId: 'tx-1',
|
|
674
|
-
partyId: primaryWallet.partyId,
|
|
675
|
-
})).rejects.toThrow('No session found');
|
|
676
|
-
});
|
|
677
|
-
it('delegates to TransactionService.sign', async () => {
|
|
678
|
-
const store = await createStore(logger, auth);
|
|
679
|
-
await store.removeWallet(primaryWallet.partyId);
|
|
680
|
-
await store.addWallet(participantWallet);
|
|
681
|
-
transactionServiceMocks.sign.mockReturnValue({
|
|
682
|
-
status: 'signed',
|
|
683
|
-
signature: 'none',
|
|
684
|
-
signedBy: 'namespace',
|
|
685
|
-
partyId: participantWallet.partyId,
|
|
686
|
-
});
|
|
687
|
-
const controller = createController(store, notificationService, logger, auth, {
|
|
688
|
-
[SigningProvider.PARTICIPANT]: {
|
|
689
|
-
controller: vi.fn(() => ({})),
|
|
690
|
-
},
|
|
691
|
-
});
|
|
692
|
-
const signParams = {
|
|
693
|
-
transactionId: 'tx-1',
|
|
694
|
-
partyId: participantWallet.partyId,
|
|
695
|
-
};
|
|
696
|
-
const result = await controller.sign(signParams);
|
|
697
|
-
expect(transactionServiceMocks.sign).toHaveBeenCalledWith(auth, participantWallet, signParams);
|
|
698
|
-
expect(result).toMatchObject({ status: 'signed' });
|
|
699
|
-
});
|
|
700
|
-
});
|
|
701
|
-
describe('execute', () => {
|
|
702
|
-
const executeParams = {
|
|
703
|
-
signature: 'sig',
|
|
704
|
-
partyId: primaryWallet.partyId,
|
|
705
|
-
transactionId: pendingTransaction.id,
|
|
706
|
-
signedBy: primaryWallet.namespace,
|
|
707
|
-
};
|
|
708
|
-
it('delegates to TransactionService.execute', async () => {
|
|
709
|
-
const store = await createStore(logger, auth);
|
|
710
|
-
await store.removeWallet(primaryWallet.partyId);
|
|
711
|
-
await store.addWallet(participantWallet);
|
|
712
|
-
await store.setTransaction(pendingTransaction);
|
|
713
|
-
transactionServiceMocks.execute.mockResolvedValue({
|
|
714
|
-
commandId: pendingTransaction.commandId,
|
|
715
|
-
});
|
|
716
|
-
const controller = createController(store, notificationService, logger, auth, {
|
|
717
|
-
[SigningProvider.PARTICIPANT]: {
|
|
718
|
-
controller: vi.fn(() => ({})),
|
|
719
|
-
},
|
|
720
|
-
});
|
|
721
|
-
const params = {
|
|
722
|
-
...executeParams,
|
|
723
|
-
partyId: participantWallet.partyId,
|
|
724
|
-
};
|
|
725
|
-
const result = await controller.execute(params);
|
|
726
|
-
expect(transactionServiceMocks.execute).toHaveBeenCalledWith(auth.userId, participantWallet, expect.objectContaining({ id: pendingTransaction.id }), params, expect.objectContaining({
|
|
727
|
-
getWithRetry: ledgerMocks.getWithRetry,
|
|
728
|
-
}), auth, expect.objectContaining({ id: storeNetwork.id }));
|
|
729
|
-
expect(result).toEqual({ commandId: pendingTransaction.commandId });
|
|
730
|
-
});
|
|
731
|
-
it('delegates execute for external signing providers', async () => {
|
|
732
|
-
const store = await createStore(logger, auth);
|
|
733
|
-
await store.setTransaction(pendingTransaction);
|
|
734
|
-
transactionServiceMocks.execute.mockResolvedValue({
|
|
735
|
-
commandId: pendingTransaction.commandId,
|
|
736
|
-
});
|
|
737
|
-
const controller = createController(store, notificationService, logger, auth, {
|
|
738
|
-
[SigningProvider.WALLET_KERNEL]: {
|
|
739
|
-
controller: vi.fn(() => ({})),
|
|
740
|
-
},
|
|
741
|
-
});
|
|
742
|
-
const result = await controller.execute(executeParams);
|
|
743
|
-
expect(transactionServiceMocks.execute).toHaveBeenCalledWith(auth.userId, primaryWallet, expect.objectContaining({ id: pendingTransaction.id }), executeParams, expect.objectContaining({
|
|
744
|
-
getWithRetry: ledgerMocks.getWithRetry,
|
|
745
|
-
}), auth, expect.objectContaining({ id: storeNetwork.id }));
|
|
746
|
-
expect(result).toEqual({ commandId: pendingTransaction.commandId });
|
|
747
|
-
});
|
|
748
|
-
});
|
|
749
|
-
describe('sessions', () => {
|
|
750
|
-
const validAddSessionClaims = {
|
|
751
|
-
iss: idp.issuer,
|
|
752
|
-
aud: storeNetwork.auth.audience,
|
|
753
|
-
sub: 'sub',
|
|
754
|
-
scope: 'scope1 scope2',
|
|
755
|
-
scp: ['scope1', 'scope2'],
|
|
756
|
-
exp: 1_900_000_000,
|
|
757
|
-
iat: 1_800_000_000,
|
|
758
|
-
};
|
|
759
|
-
const createAuthWithAddSessionClaims = (claimsOverride = {}) => ({
|
|
760
|
-
...auth,
|
|
761
|
-
accessToken: createJwt({
|
|
762
|
-
...validAddSessionClaims,
|
|
763
|
-
...claimsOverride,
|
|
764
|
-
}),
|
|
765
|
-
});
|
|
766
|
-
const createJwt = (payload) => {
|
|
767
|
-
const encode = (value) => Buffer.from(JSON.stringify(value)).toString('base64url');
|
|
768
|
-
return `${encode({ alg: 'none', typ: 'JWT' })}.${encode(payload)}.signature`;
|
|
769
|
-
};
|
|
770
|
-
it('returns an empty list when there is no session', async () => {
|
|
771
|
-
const store = await createStore(logger, auth, {
|
|
772
|
-
withSession: false,
|
|
773
|
-
});
|
|
774
|
-
const controller = createController(store, notificationService, logger, auth);
|
|
775
|
-
await expect(controller.listSessions()).resolves.toEqual({
|
|
776
|
-
sessions: [],
|
|
777
|
-
});
|
|
778
|
-
});
|
|
779
|
-
it('lists the current session with network status', async () => {
|
|
780
|
-
const store = await createStore(logger, auth);
|
|
781
|
-
const controller = createController(store, notificationService, logger, auth, {}, adminUserId);
|
|
782
|
-
const result = await controller.listSessions();
|
|
783
|
-
expect(result.sessions).toHaveLength(1);
|
|
784
|
-
expect(result.sessions[0]).toMatchObject({
|
|
785
|
-
id: 'session-1',
|
|
786
|
-
status: 'connected',
|
|
787
|
-
accessToken: 'access-token-1',
|
|
788
|
-
network: expect.objectContaining({
|
|
789
|
-
id: 'network1',
|
|
790
|
-
ledgerApi: 'http://ledger.test',
|
|
791
|
-
auth: expect.objectContaining({
|
|
792
|
-
method: 'authorization_code',
|
|
793
|
-
clientId: 'cid',
|
|
794
|
-
}),
|
|
795
|
-
}),
|
|
796
|
-
idp,
|
|
797
|
-
});
|
|
798
|
-
expect(result.sessions[0].network).not.toHaveProperty('adminAuth');
|
|
799
|
-
expect(result.sessions[0].network).not.toHaveProperty('serviceAccountAuth');
|
|
800
|
-
expect(mockNetworkStatus).toHaveBeenCalled();
|
|
801
|
-
});
|
|
802
|
-
it('includes adminAuth in listSessions for the admin user', async () => {
|
|
803
|
-
const store = await createStore(logger, adminAuth, {
|
|
804
|
-
withSession: false,
|
|
805
|
-
});
|
|
806
|
-
await store.setSession({
|
|
807
|
-
...session,
|
|
808
|
-
accessToken: adminAuth.accessToken,
|
|
809
|
-
});
|
|
810
|
-
const controller = createController(store, notificationService, logger, adminAuth, {}, adminUserId);
|
|
811
|
-
const result = await controller.listSessions();
|
|
812
|
-
expect(result.sessions).toHaveLength(1);
|
|
813
|
-
expect(result.sessions[0].network).toMatchObject({
|
|
814
|
-
id: 'network1',
|
|
815
|
-
adminAuth: expect.objectContaining({
|
|
816
|
-
method: 'client_credentials',
|
|
817
|
-
clientId: 'admin-cid',
|
|
818
|
-
clientSecret: 'admin-secret',
|
|
819
|
-
}),
|
|
820
|
-
});
|
|
821
|
-
});
|
|
822
|
-
it('removeSession clears the session and emits statusChanged', async () => {
|
|
823
|
-
const store = await createStore(logger, auth);
|
|
824
|
-
const notifier = notificationService.getNotifier('session-1');
|
|
825
|
-
const emitSpy = vi.spyOn(notifier, 'emit');
|
|
826
|
-
const controller = createController(store, notificationService, logger, auth);
|
|
827
|
-
await controller.removeSession();
|
|
828
|
-
await expect(store.listSessions()).resolves.toHaveLength(0);
|
|
829
|
-
expect(emitSpy).toHaveBeenCalledWith('statusChanged', expect.objectContaining({
|
|
830
|
-
connection: expect.objectContaining({
|
|
831
|
-
isConnected: false,
|
|
832
|
-
reason: 'disconnect',
|
|
833
|
-
}),
|
|
834
|
-
userUrl: `${userUrl}/login/`,
|
|
835
|
-
}));
|
|
836
|
-
});
|
|
837
|
-
it('addSession creates a session and emits connected', async () => {
|
|
838
|
-
const newSessionId = '00000000-0000-0000-0000-000000000001';
|
|
839
|
-
mockV4.mockReturnValueOnce(newSessionId);
|
|
840
|
-
const authWithValidClaims = createAuthWithAddSessionClaims();
|
|
841
|
-
const store = await createStore(logger, authWithValidClaims, {
|
|
842
|
-
withWallet: false,
|
|
843
|
-
});
|
|
844
|
-
const controller = createController(store, notificationService, logger, authWithValidClaims);
|
|
845
|
-
const notifier = notificationService.getNotifier(newSessionId);
|
|
846
|
-
const emitSpy = vi.spyOn(notifier, 'emit');
|
|
847
|
-
const result = await controller.addSession({
|
|
848
|
-
origin: 'dapp-1',
|
|
849
|
-
networkId: 'network1',
|
|
850
|
-
});
|
|
851
|
-
expect(result.network.id).toBe('network1');
|
|
852
|
-
expect(result.status).toBe('connected');
|
|
853
|
-
expect(emitSpy).toHaveBeenCalledWith('connected', expect.objectContaining({
|
|
854
|
-
session: {
|
|
855
|
-
accessToken: authWithValidClaims.accessToken,
|
|
856
|
-
userId: authWithValidClaims.userId,
|
|
857
|
-
},
|
|
858
|
-
}));
|
|
859
|
-
await expect(store.getSession(authWithValidClaims.accessToken)).resolves.toMatchObject({
|
|
860
|
-
network: 'network1',
|
|
861
|
-
});
|
|
862
|
-
});
|
|
863
|
-
it('addSession keeps the session when the ledger is disconnected', async () => {
|
|
864
|
-
mockNetworkStatus.mockResolvedValueOnce({
|
|
865
|
-
isConnected: false,
|
|
866
|
-
reason: 'Ledger unreachable: fetch failed',
|
|
867
|
-
});
|
|
868
|
-
const authWithValidClaims = createAuthWithAddSessionClaims();
|
|
869
|
-
const store = await createStore(logger, authWithValidClaims, {
|
|
870
|
-
withWallet: false,
|
|
871
|
-
});
|
|
872
|
-
const controller = createController(store, notificationService, logger, authWithValidClaims);
|
|
873
|
-
const result = await controller.addSession({
|
|
874
|
-
networkId: 'network1',
|
|
875
|
-
origin: 'dapp-1',
|
|
876
|
-
});
|
|
877
|
-
expect(result.status).toBe('disconnected');
|
|
878
|
-
expect(result.reason).toBe('Ledger unreachable: fetch failed');
|
|
879
|
-
expect(walletSyncMocks.syncWallets).not.toHaveBeenCalled();
|
|
880
|
-
await expect(store.getSession(authWithValidClaims.accessToken)).resolves.toMatchObject({
|
|
881
|
-
network: 'network1',
|
|
882
|
-
});
|
|
883
|
-
});
|
|
884
|
-
it('addSession keeps the session when initial wallet sync fails', async () => {
|
|
885
|
-
walletSyncMocks.syncWallets.mockRejectedValueOnce(new Error('Wallet sync failed'));
|
|
886
|
-
const authWithValidClaims = createAuthWithAddSessionClaims();
|
|
887
|
-
const store = await createStore(logger, authWithValidClaims, {
|
|
888
|
-
withWallet: false,
|
|
889
|
-
});
|
|
890
|
-
const controller = createController(store, notificationService, logger, authWithValidClaims);
|
|
891
|
-
const result = await controller.addSession({
|
|
892
|
-
networkId: 'network1',
|
|
893
|
-
origin: 'dapp-1',
|
|
894
|
-
});
|
|
895
|
-
expect(result.status).toBe('connected');
|
|
896
|
-
expect(walletSyncMocks.syncWallets).toHaveBeenCalledOnce();
|
|
897
|
-
await expect(store.getSession(authWithValidClaims.accessToken)).resolves.toMatchObject({
|
|
898
|
-
network: 'network1',
|
|
899
|
-
});
|
|
900
|
-
});
|
|
901
|
-
it('addSession does not validate scope or scp against network.auth.scope', async () => {
|
|
902
|
-
const authWithUnrelatedScopes = createAuthWithAddSessionClaims({
|
|
903
|
-
scope: 'openid email',
|
|
904
|
-
scp: ['profile'],
|
|
905
|
-
});
|
|
906
|
-
const store = await createStore(logger, authWithUnrelatedScopes, {
|
|
907
|
-
withWallet: false,
|
|
908
|
-
});
|
|
909
|
-
const controller = createController(store, notificationService, logger, authWithUnrelatedScopes);
|
|
910
|
-
const result = await controller.addSession({
|
|
911
|
-
origin: 'dapp-1',
|
|
912
|
-
networkId: 'network1',
|
|
913
|
-
});
|
|
914
|
-
expect(result.status).toBe('connected');
|
|
915
|
-
});
|
|
916
|
-
it('addSession rejects token with issuer mismatch', async () => {
|
|
917
|
-
const authWithInvalidIssuer = createAuthWithAddSessionClaims({
|
|
918
|
-
iss: 'http://wrong-issuer',
|
|
919
|
-
});
|
|
920
|
-
const store = await createStore(logger, authWithInvalidIssuer, {
|
|
921
|
-
withWallet: false,
|
|
922
|
-
});
|
|
923
|
-
const controller = createController(store, notificationService, logger, authWithInvalidIssuer);
|
|
924
|
-
await expect(controller.addSession({
|
|
925
|
-
origin: 'dapp-1',
|
|
926
|
-
networkId: 'network1',
|
|
927
|
-
})).rejects.toThrow('Failed to add session');
|
|
928
|
-
});
|
|
929
|
-
it('addSession rejects token with audience mismatch', async () => {
|
|
930
|
-
const authWithInvalidAudience = createAuthWithAddSessionClaims({
|
|
931
|
-
aud: 'wrong-audience',
|
|
932
|
-
});
|
|
933
|
-
const store = await createStore(logger, authWithInvalidAudience, {
|
|
934
|
-
withWallet: false,
|
|
935
|
-
});
|
|
936
|
-
const controller = createController(store, notificationService, logger, authWithInvalidAudience);
|
|
937
|
-
await expect(controller.addSession({
|
|
938
|
-
origin: 'dapp-1',
|
|
939
|
-
networkId: 'network1',
|
|
940
|
-
})).rejects.toThrow('Failed to add session');
|
|
941
|
-
});
|
|
942
|
-
it('addSession rejects token with client_id claim and auth.clientId mismatch', async () => {
|
|
943
|
-
const authWithInvalidSubject = createAuthWithAddSessionClaims({
|
|
944
|
-
client_id: 'wrong-client-id',
|
|
945
|
-
});
|
|
946
|
-
const store = await createStore(logger, authWithInvalidSubject, {
|
|
947
|
-
withWallet: false,
|
|
948
|
-
});
|
|
949
|
-
const controller = createController(store, notificationService, logger, authWithInvalidSubject);
|
|
950
|
-
await expect(controller.addSession({
|
|
951
|
-
origin: 'dapp-1',
|
|
952
|
-
networkId: 'network1',
|
|
953
|
-
})).rejects.toThrow('Failed to add session');
|
|
954
|
-
});
|
|
955
|
-
it('addSession rejects token with azp claim and auth.clientId mismatch', async () => {
|
|
956
|
-
const authWithInvalidSubject = createAuthWithAddSessionClaims({
|
|
957
|
-
azp: 'wrong-client-id',
|
|
958
|
-
});
|
|
959
|
-
const store = await createStore(logger, authWithInvalidSubject, {
|
|
960
|
-
withWallet: false,
|
|
961
|
-
});
|
|
962
|
-
const controller = createController(store, notificationService, logger, authWithInvalidSubject);
|
|
963
|
-
await expect(controller.addSession({
|
|
964
|
-
origin: 'dapp-1',
|
|
965
|
-
networkId: 'network1',
|
|
966
|
-
})).rejects.toThrow('Failed to add session');
|
|
967
|
-
});
|
|
968
|
-
it("addSession passes when token doesn't token have azp and client-id claims", async () => {
|
|
969
|
-
const authWithInvalidSubject = createAuthWithAddSessionClaims();
|
|
970
|
-
const store = await createStore(logger, authWithInvalidSubject, {
|
|
971
|
-
withWallet: false,
|
|
972
|
-
});
|
|
973
|
-
const controller = createController(store, notificationService, logger, authWithInvalidSubject);
|
|
974
|
-
const result = await controller.addSession({
|
|
975
|
-
origin: 'dapp-1',
|
|
976
|
-
networkId: 'network1',
|
|
977
|
-
});
|
|
978
|
-
expect(result).toMatchObject({
|
|
979
|
-
network: expect.objectContaining({
|
|
980
|
-
id: 'network1',
|
|
981
|
-
auth: expect.objectContaining({
|
|
982
|
-
method: 'authorization_code',
|
|
983
|
-
}),
|
|
984
|
-
}),
|
|
985
|
-
status: 'connected',
|
|
986
|
-
});
|
|
987
|
-
});
|
|
988
|
-
});
|
|
989
|
-
describe('createWallet', () => {
|
|
990
|
-
const walletKernelDriver = {
|
|
991
|
-
[SigningProvider.WALLET_KERNEL]: { controller: vi.fn() },
|
|
992
|
-
};
|
|
993
|
-
it('throws when no network session exists', async () => {
|
|
994
|
-
const store = await createStore(logger, auth, {
|
|
995
|
-
withSession: false,
|
|
996
|
-
});
|
|
997
|
-
const controller = createController(store, notificationService, logger, auth, { [SigningProvider.WALLET_KERNEL]: { controller: vi.fn() } });
|
|
998
|
-
await expect(controller.createWallet({
|
|
999
|
-
partyHint: 'alice',
|
|
1000
|
-
signingProviderId: SigningProvider.WALLET_KERNEL,
|
|
1001
|
-
})).rejects.toThrow('No session found');
|
|
1002
|
-
});
|
|
1003
|
-
it('throws when the signing provider is not configured', async () => {
|
|
1004
|
-
const store = await createStore(logger, auth);
|
|
1005
|
-
const controller = createController(store, notificationService, logger, auth);
|
|
1006
|
-
await expect(controller.createWallet({
|
|
1007
|
-
partyHint: 'alice',
|
|
1008
|
-
signingProviderId: SigningProvider.WALLET_KERNEL,
|
|
1009
|
-
})).rejects.toThrow('Signing provider wallet-kernel not supported');
|
|
1010
|
-
});
|
|
1011
|
-
it('creates a wallet when the signing provider is configured', async () => {
|
|
1012
|
-
const authWithEmail = { ...auth, email: 'user@example.com' };
|
|
1013
|
-
const store = await createStore(logger, authWithEmail);
|
|
1014
|
-
const newWallet = {
|
|
1015
|
-
...primaryWallet,
|
|
1016
|
-
partyId: 'party::new',
|
|
1017
|
-
primary: false,
|
|
1018
|
-
rights: [],
|
|
1019
|
-
};
|
|
1020
|
-
walletAllocationMocks.createWallet.mockImplementation(async () => {
|
|
1021
|
-
await store.addWallet(newWallet);
|
|
1022
|
-
return newWallet;
|
|
1023
|
-
});
|
|
1024
|
-
walletSyncMocks.syncRights.mockImplementation(async () => {
|
|
1025
|
-
await store.updateWallet({
|
|
1026
|
-
partyId: newWallet.partyId,
|
|
1027
|
-
rights: [PartyLevelRight.CanActAs],
|
|
1028
|
-
});
|
|
1029
|
-
return [];
|
|
1030
|
-
});
|
|
1031
|
-
const notifier = notificationService.getNotifier('user-1');
|
|
1032
|
-
const emitSpy = vi.spyOn(notifier, 'emit');
|
|
1033
|
-
const controller = createController(store, notificationService, logger, authWithEmail, walletKernelDriver);
|
|
1034
|
-
const result = await controller.createWallet({
|
|
1035
|
-
partyHint: 'alice',
|
|
1036
|
-
signingProviderId: SigningProvider.WALLET_KERNEL,
|
|
1037
|
-
primary: false,
|
|
1038
|
-
});
|
|
1039
|
-
expect(walletAllocationMocks.createWallet).toHaveBeenCalledWith(authWithEmail, 'alice', false, SigningProvider.WALLET_KERNEL, undefined);
|
|
1040
|
-
expect(walletSyncMocks.syncRights).toHaveBeenCalledOnce();
|
|
1041
|
-
expect(walletSyncMocks.syncWallets).not.toHaveBeenCalled();
|
|
1042
|
-
expect(emitSpy).toHaveBeenCalledWith('accountsChanged', expect.any(Array));
|
|
1043
|
-
expect(result.wallet).toMatchObject({
|
|
1044
|
-
partyId: 'party::new',
|
|
1045
|
-
rights: [PartyLevelRight.CanActAs],
|
|
1046
|
-
});
|
|
1047
|
-
});
|
|
1048
|
-
it('passes auth context to wallet allocation service', async () => {
|
|
1049
|
-
const authWithEmail = { ...auth, email: 'direct@example.com' };
|
|
1050
|
-
const store = await createStore(logger, authWithEmail);
|
|
1051
|
-
walletAllocationMocks.createWallet.mockResolvedValue(primaryWallet);
|
|
1052
|
-
const controller = createController(store, notificationService, logger, authWithEmail, walletKernelDriver);
|
|
1053
|
-
await controller.createWallet({
|
|
1054
|
-
partyHint: 'bob',
|
|
1055
|
-
signingProviderId: SigningProvider.WALLET_KERNEL,
|
|
1056
|
-
});
|
|
1057
|
-
expect(walletAllocationMocks.createWallet).toHaveBeenCalledWith(authWithEmail, 'bob', false, SigningProvider.WALLET_KERNEL, undefined);
|
|
1058
|
-
});
|
|
1059
|
-
it('throws when no current network is configured', async () => {
|
|
1060
|
-
const store = await createStore(logger, auth);
|
|
1061
|
-
// @ts-expect-error type doesn't allow empty return. override to validate guard clause
|
|
1062
|
-
vi.spyOn(store, 'getCurrentNetwork').mockResolvedValue(undefined);
|
|
1063
|
-
const controller = createController(store, notificationService, logger, auth, walletKernelDriver);
|
|
1064
|
-
await expect(controller.createWallet({
|
|
1065
|
-
partyHint: 'alice',
|
|
1066
|
-
signingProviderId: SigningProvider.WALLET_KERNEL,
|
|
1067
|
-
})).rejects.toThrow('No network session found');
|
|
1068
|
-
expect(walletAllocationMocks.createWallet).not.toHaveBeenCalled();
|
|
1069
|
-
});
|
|
1070
|
-
});
|
|
1071
|
-
describe('allocatePartyForWallet', () => {
|
|
1072
|
-
const walletKernelDriver = {
|
|
1073
|
-
[SigningProvider.WALLET_KERNEL]: { controller: vi.fn() },
|
|
1074
|
-
};
|
|
1075
|
-
it('allocates a party for an existing wallet', async () => {
|
|
1076
|
-
const authWithEmail = { ...auth, email: 'user@example.com' };
|
|
1077
|
-
const store = await createStore(logger, authWithEmail);
|
|
1078
|
-
const notifier = notificationService.getNotifier('user-1');
|
|
1079
|
-
const emitSpy = vi.spyOn(notifier, 'emit');
|
|
1080
|
-
const controller = createController(store, notificationService, logger, authWithEmail, walletKernelDriver);
|
|
1081
|
-
walletSyncMocks.syncRights.mockImplementation(async () => {
|
|
1082
|
-
await store.updateWallet({
|
|
1083
|
-
partyId: primaryWallet.partyId,
|
|
1084
|
-
rights: [
|
|
1085
|
-
PartyLevelRight.CanActAs,
|
|
1086
|
-
PartyLevelRight.CanReadAs,
|
|
1087
|
-
],
|
|
1088
|
-
});
|
|
1089
|
-
return [];
|
|
1090
|
-
});
|
|
1091
|
-
const result = await controller.allocatePartyForWallet({
|
|
1092
|
-
partyId: primaryWallet.partyId,
|
|
1093
|
-
});
|
|
1094
|
-
expect(walletAllocationMocks.allocateParty).toHaveBeenCalledWith(authWithEmail, primaryWallet, SigningProvider.WALLET_KERNEL);
|
|
1095
|
-
expect(walletSyncMocks.syncRights).toHaveBeenCalledOnce();
|
|
1096
|
-
expect(walletSyncMocks.syncWallets).not.toHaveBeenCalled();
|
|
1097
|
-
expect(emitSpy).toHaveBeenCalledWith('accountsChanged', expect.any(Array));
|
|
1098
|
-
expect(result.wallet).toMatchObject({
|
|
1099
|
-
partyId: primaryWallet.partyId,
|
|
1100
|
-
networkId: storeNetwork.id,
|
|
1101
|
-
rights: [PartyLevelRight.CanActAs, PartyLevelRight.CanReadAs],
|
|
1102
|
-
});
|
|
1103
|
-
});
|
|
1104
|
-
it('throws when the wallet is not found', async () => {
|
|
1105
|
-
const store = await createStore(logger, auth);
|
|
1106
|
-
const controller = createController(store, notificationService, logger, auth, walletKernelDriver);
|
|
1107
|
-
await expect(controller.allocatePartyForWallet({
|
|
1108
|
-
partyId: 'party::missing',
|
|
1109
|
-
})).rejects.toThrow('Wallet not found for party party::missing');
|
|
1110
|
-
});
|
|
1111
|
-
it('throws when the signing provider is not configured', async () => {
|
|
1112
|
-
const store = await createStore(logger, auth);
|
|
1113
|
-
const controller = createController(store, notificationService, logger, auth);
|
|
1114
|
-
await expect(controller.allocatePartyForWallet({
|
|
1115
|
-
partyId: primaryWallet.partyId,
|
|
1116
|
-
})).rejects.toThrow('Signing provider wallet-kernel not supported');
|
|
1117
|
-
});
|
|
1118
|
-
});
|
|
1119
|
-
describe('syncWallets', () => {
|
|
1120
|
-
it('syncs wallets and returns the result without emitting when nothing changed', async () => {
|
|
1121
|
-
const store = await createStore(logger, auth);
|
|
1122
|
-
const notifier = notificationService.getNotifier('session-1');
|
|
1123
|
-
const emitSpy = vi.spyOn(notifier, 'emit');
|
|
1124
|
-
const emptyResult = { added: [], updated: [], disabled: [] };
|
|
1125
|
-
walletSyncMocks.syncWallets.mockResolvedValue(emptyResult);
|
|
1126
|
-
const controller = createController(store, notificationService, logger, auth);
|
|
1127
|
-
const result = await controller.syncWallets();
|
|
1128
|
-
expect(walletSyncMocks.syncWallets).toHaveBeenCalledOnce();
|
|
1129
|
-
expect(result).toEqual(emptyResult);
|
|
1130
|
-
expect(emitSpy).not.toHaveBeenCalledWith('accountsChanged', expect.anything());
|
|
1131
|
-
});
|
|
1132
|
-
it('throws when admin auth is not configured', async () => {
|
|
1133
|
-
const networkWithoutAdmin = {
|
|
1134
|
-
...storeNetwork,
|
|
1135
|
-
adminAuth: undefined,
|
|
1136
|
-
};
|
|
1137
|
-
const store = new StoreInternal({ idps: [idp], networks: [networkWithoutAdmin] }, getLogger('mock'), auth);
|
|
1138
|
-
await store.setSession(session);
|
|
1139
|
-
const controller = createController(store, notificationService, logger, auth);
|
|
1140
|
-
await expect(controller.syncWallets()).rejects.toThrow('No admin auth configured');
|
|
1141
|
-
expect(walletSyncMocks.syncWallets).not.toHaveBeenCalled();
|
|
1142
|
-
});
|
|
1143
|
-
it('throws when auth context is missing', async () => {
|
|
1144
|
-
const store = await createStore(logger, auth, {
|
|
1145
|
-
withSession: false,
|
|
1146
|
-
});
|
|
1147
|
-
const controller = createController(store, notificationService, logger, undefined);
|
|
1148
|
-
await expect(controller.syncWallets()).rejects.toThrow();
|
|
1149
|
-
expect(walletSyncMocks.syncWallets).not.toHaveBeenCalled();
|
|
1150
|
-
});
|
|
1151
|
-
it('emits accountsChanged when wallets are added and disabled during sync', async () => {
|
|
1152
|
-
const store = await createStore(logger, auth);
|
|
1153
|
-
const notifier = notificationService.getNotifier('user-1');
|
|
1154
|
-
const emitSpy = vi.spyOn(notifier, 'emit');
|
|
1155
|
-
const addedWallet = {
|
|
1156
|
-
...primaryWallet,
|
|
1157
|
-
partyId: 'party::added',
|
|
1158
|
-
};
|
|
1159
|
-
const disabledWallet = {
|
|
1160
|
-
...primaryWallet,
|
|
1161
|
-
partyId: 'party::disabled',
|
|
1162
|
-
status: 'removed',
|
|
1163
|
-
};
|
|
1164
|
-
const syncResult = {
|
|
1165
|
-
added: [addedWallet],
|
|
1166
|
-
updated: [],
|
|
1167
|
-
disabled: [disabledWallet],
|
|
1168
|
-
};
|
|
1169
|
-
walletSyncMocks.syncWallets.mockResolvedValue(syncResult);
|
|
1170
|
-
const controller = createController(store, notificationService, logger, auth);
|
|
1171
|
-
const result = await controller.syncWallets();
|
|
1172
|
-
expect(result).toEqual(syncResult);
|
|
1173
|
-
expect(emitSpy).toHaveBeenCalledWith('accountsChanged', expect.arrayContaining([
|
|
1174
|
-
expect.objectContaining({ partyId: primaryWallet.partyId }),
|
|
1175
|
-
]));
|
|
1176
|
-
});
|
|
1177
|
-
it('does not emit accountsChanged when only wallets are added', async () => {
|
|
1178
|
-
const store = await createStore(logger, auth);
|
|
1179
|
-
const notifier = notificationService.getNotifier('session-1');
|
|
1180
|
-
const emitSpy = vi.spyOn(notifier, 'emit');
|
|
1181
|
-
walletSyncMocks.syncWallets.mockResolvedValue({
|
|
1182
|
-
added: [{ ...primaryWallet, partyId: 'party::added' }],
|
|
1183
|
-
updated: [],
|
|
1184
|
-
disabled: [],
|
|
1185
|
-
});
|
|
1186
|
-
const controller = createController(store, notificationService, logger, auth);
|
|
1187
|
-
await controller.syncWallets();
|
|
1188
|
-
expect(emitSpy).not.toHaveBeenCalledWith('accountsChanged', expect.anything());
|
|
1189
|
-
});
|
|
1190
|
-
});
|
|
1191
|
-
describe('isWalletSyncNeeded', () => {
|
|
1192
|
-
it('returns false when wallet sync is not needed', async () => {
|
|
1193
|
-
const store = await createStore(logger, auth);
|
|
1194
|
-
walletSyncMocks.isWalletSyncNeeded.mockResolvedValue(false);
|
|
1195
|
-
const controller = createController(store, notificationService, logger, auth);
|
|
1196
|
-
const result = await controller.isWalletSyncNeeded();
|
|
1197
|
-
expect(walletSyncMocks.isWalletSyncNeeded).toHaveBeenCalledOnce();
|
|
1198
|
-
expect(result).toEqual({ walletSyncNeeded: false });
|
|
1199
|
-
});
|
|
1200
|
-
it('returns true when wallet sync is needed', async () => {
|
|
1201
|
-
const store = await createStore(logger, auth);
|
|
1202
|
-
walletSyncMocks.isWalletSyncNeeded.mockResolvedValue(true);
|
|
1203
|
-
const controller = createController(store, notificationService, logger, auth);
|
|
1204
|
-
const result = await controller.isWalletSyncNeeded();
|
|
1205
|
-
expect(walletSyncMocks.isWalletSyncNeeded).toHaveBeenCalledOnce();
|
|
1206
|
-
expect(result).toEqual({ walletSyncNeeded: true });
|
|
1207
|
-
});
|
|
1208
|
-
it('throws when admin auth is not configured', async () => {
|
|
1209
|
-
const networkWithoutAdmin = {
|
|
1210
|
-
...storeNetwork,
|
|
1211
|
-
adminAuth: undefined,
|
|
1212
|
-
};
|
|
1213
|
-
const store = new StoreInternal({ idps: [idp], networks: [networkWithoutAdmin] }, getLogger('mock'), auth);
|
|
1214
|
-
await store.setSession(session);
|
|
1215
|
-
const controller = createController(store, notificationService, logger, auth);
|
|
1216
|
-
await expect(controller.isWalletSyncNeeded()).rejects.toThrow('No admin auth configured');
|
|
1217
|
-
expect(walletSyncMocks.isWalletSyncNeeded).not.toHaveBeenCalled();
|
|
1218
|
-
});
|
|
1219
|
-
it('throws when auth context is missing', async () => {
|
|
1220
|
-
const store = await createStore(logger, auth, {
|
|
1221
|
-
withSession: false,
|
|
1222
|
-
});
|
|
1223
|
-
const controller = createController(store, notificationService, logger, undefined);
|
|
1224
|
-
await expect(controller.isWalletSyncNeeded()).rejects.toThrow();
|
|
1225
|
-
expect(walletSyncMocks.isWalletSyncNeeded).not.toHaveBeenCalled();
|
|
1226
|
-
});
|
|
1227
|
-
});
|
|
1228
|
-
});
|