-
Notifications
You must be signed in to change notification settings - Fork 55
/
lit-core.ts
912 lines (829 loc) · 27.6 KB
/
lit-core.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
import { computeHDPubKey, checkSevSnpAttestation } from '@lit-protocol/crypto';
import { keccak256 } from '@ethersproject/keccak256';
import { toUtf8Bytes } from '@ethersproject/strings';
import {
canonicalAccessControlConditionFormatter,
canonicalEVMContractConditionFormatter,
canonicalSolRpcConditionFormatter,
canonicalUnifiedAccessControlConditionFormatter,
hashAccessControlConditions,
hashEVMContractConditions,
hashSolRpcConditions,
hashUnifiedAccessControlConditions,
} from '@lit-protocol/access-control-conditions';
import {
INTERNAL_DEFAULT_CONFIG,
LitNetwork,
LIT_ERROR,
LIT_ERROR_CODE,
LIT_NETWORKS,
version,
TELEM_API_URL,
SIGTYPE,
StakingStates,
} from '@lit-protocol/constants';
import {
bootstrapLogManager,
isBrowser,
isNode,
log,
logError,
logErrorWithRequestId,
logWithRequestId,
mostCommonString,
throwError,
} from '@lit-protocol/misc';
import {
AuthMethod,
AuthSig,
CustomNetwork,
FormattedMultipleAccs,
HandshakeWithNode,
JsonExecutionRequest,
JsonHandshakeResponse,
JsonPkpSignRequest,
KV,
LitNodeClientConfig,
MultipleAccessControlConditions,
NodeAttestation,
NodeClientErrorV0,
NodeClientErrorV1,
NodeCommandServerKeysResponse,
NodeErrorV3,
NodePromiseResponse,
RejectedNodePromises,
SendNodeCommand,
SessionSig,
SessionSigsMap,
SuccessNodePromises,
SupportedJsonRequests,
} from '@lit-protocol/types';
import { ethers } from 'ethers';
import { uint8arrayFromString } from '@lit-protocol/uint8arrays';
import { LitContracts } from '@lit-protocol/contracts-sdk';
import { LogLevel, LogManager } from '@lit-protocol/logger';
export class LitCore {
config: LitNodeClientConfig;
connectedNodes: SetConstructor | Set<any> | any;
serverKeys: KV | any;
ready: boolean;
subnetPubKey: string | null;
networkPubKey: string | null;
networkPubKeySet: string | null;
hdRootPubkeys: string[] | null;
latestBlockhash: string | null;
// ========== Constructor ==========
constructor(args: any[LitNodeClientConfig | CustomNetwork | any]) {
const customConfig = args;
let _defaultConfig = {
alertWhenUnauthorized: false,
debug: true,
connectTimeout: 20000,
litNetwork: 'cayenne', // Default to cayenne network. will be replaced by custom config.
minNodeCount: 2, // Default value, should be replaced
bootstrapUrls: [], // Default value, should be replaced
};
// Initialize default config based on litNetwork
if (args && 'litNetwork' in args) {
switch (args.litNetwork) {
case LitNetwork.Cayenne:
this.config = {
..._defaultConfig,
litNetwork: LitNetwork.Cayenne,
} as unknown as LitNodeClientConfig;
break;
default:
this.config = {
..._defaultConfig,
...customConfig,
} as LitNodeClientConfig;
}
} else {
this.config = { ..._defaultConfig, ...customConfig };
}
// -- initialize default auth callback
// this.defaultAuthCallback = args?.defaultAuthCallback;
// -- if config params are specified, replace it
if (customConfig) {
this.config = { ...this.config, ...customConfig };
// this.config = override(this.config, customConfig);
}
// -- init default properties
this.connectedNodes = new Set();
this.serverKeys = {};
this.ready = false;
this.subnetPubKey = null;
this.networkPubKey = null;
this.networkPubKeySet = null;
this.hdRootPubkeys = null;
this.latestBlockhash = null;
// -- set bootstrapUrls to match the network litNetwork unless it's set to custom
this.setCustomBootstrapUrls();
// -- set global variables
globalThis.litConfig = this.config;
bootstrapLogManager('core');
// -- configure local storage if not present
// LitNodeClientNodejs is a base for LitNodeClient
// First check for if our runtime is node
// If the user sets a new storage provider we respect it over our default storage
// If the user sets a new file path, we respect it over the default path.
if (this.config.storageProvider?.provider) {
log(
'localstorage api not found, injecting persistance instance found in config'
);
// using Object definProperty in order to set a property previously defined as readonly.
// if the user wants to override the storage option explicitly we override.
Object.defineProperty(globalThis, 'localStorage', {
value: this.config.storageProvider?.provider,
});
} else if (
isNode() &&
!globalThis.localStorage &&
!this.config.storageProvider?.provider
) {
log(
'Looks like you are running in NodeJS and did not provide a storage provider, youre sessions will not be cached'
);
}
}
// ========== Logger utilities ==========
getLogsForRequestId = (id: string): string[] => {
return globalThis.logManager.getLogsForId(id);
};
// ========== Scoped Class Helpers ==========
/**
* Asynchronously updates the configuration settings for the LitNodeClient.
* This function fetches the minimum node count and bootstrap URLs for the
* specified Lit network. It validates these values and updates the client's
* configuration accordingly. If the network is set to 'InternalDev', it
* dynamically updates the bootstrap URLs in the configuration.
*
* @throws Will throw an error if the minimum node count is invalid or if
* the bootstrap URLs array is empty.
* @returns {Promise<void>} A promise that resolves when the configuration is updated.
*/
setNewConfig = async (): Promise<void> => {
if (
this.config.litNetwork !== LitNetwork.Cayenne &&
this.config.litNetwork !== LitNetwork.Custom
) {
const minNodeCount = await LitContracts.getMinNodeCount(
this.config.litNetwork as LitNetwork
);
const bootstrapUrls = await LitContracts.getValidators(
this.config.litNetwork as LitNetwork
);
log('Bootstrap urls: ', bootstrapUrls);
if (minNodeCount <= 0) {
throwError({
message: `minNodeCount is ${minNodeCount}, which is invalid. Please check your network connection and try again.`,
errorKind: LIT_ERROR.INVALID_ARGUMENT_EXCEPTION.kind,
errorCode: LIT_ERROR.INVALID_ARGUMENT_EXCEPTION.name,
});
}
if (bootstrapUrls.length <= 0) {
throwError({
message: `bootstrapUrls is empty, which is invalid. Please check your network connection and try again.`,
errorKind: LIT_ERROR.INVALID_ARGUMENT_EXCEPTION.kind,
errorCode: LIT_ERROR.INVALID_ARGUMENT_EXCEPTION.name,
});
}
this.config.minNodeCount = parseInt(minNodeCount, 10);
} else if (this.config.litNetwork === LitNetwork.Cayenne) {
// If the network is cayenne it is a centralized testnet so we use a static config
// This is due to staking contracts holding local ip / port contexts which are innacurate to the ip / port exposed to the world
this.config.bootstrapUrls = LIT_NETWORKS.cayenne;
this.config.minNodeCount =
LIT_NETWORKS.cayenne.length == 2
? 2
: (LIT_NETWORKS.cayenne.length * 2) / 3;
}
};
/**
* Sets up a listener to detect state changes (new epochs) in the staking contract.
* When a new epoch is detected, it triggers the `setNewConfig` function to update
* the client's configuration based on the new state of the network. This ensures
* that the client's configuration is always in sync with the current state of the
* staking contract.
*
* @returns {Promise<void>} A promise that resolves when the listener is successfully set up.
*/
listenForNewEpoch = async (): Promise<void> => {
if (
this.config.litNetwork !== LitNetwork.Cayenne &&
this.config.litNetwork !== LitNetwork.Custom
) {
const stakingContract = await LitContracts.getStakingContract(
this.config.litNetwork as any
);
log(
'listening for state change on staking contract: ',
stakingContract.address
);
stakingContract.on('StateChanged', async (state: StakingStates) => {
log(`New state detected: "${state}"`);
if (state === StakingStates.NextValidatorSetLocked) {
await this.setNewConfig();
}
});
}
};
/**
*
* Set bootstrapUrls to match the network litNetwork unless it's set to custom
*
* @returns { void }
*
*/
setCustomBootstrapUrls = (): void => {
// -- validate
if (this.config.litNetwork === 'custom') return;
// -- execute
const hasNetwork: boolean = this.config.litNetwork in LIT_NETWORKS;
if (!hasNetwork) {
// network not found, report error
throwError({
message:
'the litNetwork specified in the LitNodeClient config not found in LIT_NETWORKS',
errorKind: LIT_ERROR.LIT_NODE_CLIENT_BAD_CONFIG_ERROR.kind,
errorCode: LIT_ERROR.LIT_NODE_CLIENT_BAD_CONFIG_ERROR.name,
});
return;
}
this.config.bootstrapUrls = LIT_NETWORKS[this.config.litNetwork];
};
/**
*
* Connect to the LIT nodes
*
* @returns { Promise } A promise that resolves when the nodes are connected.
*
*/
connect = async (): Promise<any> => {
// -- handshake with each node
await this.setNewConfig();
await this.listenForNewEpoch();
// -- handshake with each node
const requestId = this.getRequestId();
for (const url of this.config.bootstrapUrls) {
const challenge = this.getRandomHexString(64);
this.handshakeWithNode({ url, challenge }, requestId)
.then((resp: any) => {
this.connectedNodes.add(url);
let keys: JsonHandshakeResponse = {
serverPubKey: resp.serverPublicKey,
subnetPubKey: resp.subnetPublicKey,
networkPubKey: resp.networkPublicKey,
networkPubKeySet: resp.networkPublicKeySet,
hdRootPubkeys: resp.hdRootPubkeys,
latestBlockhash: resp.latestBlockhash,
};
// -- validate returned keys
if (
keys.serverPubKey === 'ERR' ||
keys.subnetPubKey === 'ERR' ||
keys.networkPubKey === 'ERR' ||
keys.networkPubKeySet === 'ERR'
) {
logErrorWithRequestId(
requestId,
'Error connecting to node. Detected "ERR" in keys',
url,
keys
);
}
if (!keys.latestBlockhash) {
logErrorWithRequestId(
requestId,
'Error getting latest blockhash from the node.'
);
}
if (this.config.checkNodeAttestation) {
// check attestation
if (!resp.attestation) {
logErrorWithRequestId(
requestId,
`Missing attestation in handshake response from ${url}`
);
throwError({
message: `Missing attestation in handshake response from ${url}`,
errorKind: LIT_ERROR.INVALID_NODE_ATTESTATION.kind,
errorCode: LIT_ERROR.INVALID_NODE_ATTESTATION.name,
});
} else {
// actually verify the attestation by checking the signature against AMD certs
log('Checking attestation against amd certs...');
const attestation = resp.attestation;
try {
checkSevSnpAttestation(attestation, challenge, url).then(() => {
log(`Lit Node Attestation verified for ${url}`);
// only set server keys if attestation is valid
// so that we don't use this node if it's not valid
this.serverKeys[url] = keys;
});
} catch (e) {
logErrorWithRequestId(
requestId,
`Lit Node Attestation failed verification for ${url}`
);
throwError({
message: `Lit Node Attestation failed verification for ${url}`,
errorKind: LIT_ERROR.INVALID_NODE_ATTESTATION.kind,
errorCode: LIT_ERROR.INVALID_NODE_ATTESTATION.name,
});
}
}
} else {
// don't check attestation, just set server keys
this.serverKeys[url] = keys;
}
})
.catch((e: any) => {
log('Error connecting to node ', url, e);
});
}
// -- get promise
const promise = new Promise((resolve: any, reject: any) => {
const startTime = Date.now();
const interval = setInterval(() => {
if (
Object.keys(this.serverKeys).length ==
this.config.bootstrapUrls.length
) {
clearInterval(interval);
// pick the most common public keys for the subnet and network from the bunch, in case some evil node returned a bad key
this.subnetPubKey = mostCommonString(
Object.values(this.serverKeys).map(
(keysFromSingleNode: any) => keysFromSingleNode.subnetPubKey
)
);
this.networkPubKey = mostCommonString(
Object.values(this.serverKeys).map(
(keysFromSingleNode: any) => keysFromSingleNode.networkPubKey
)
);
this.networkPubKeySet = mostCommonString(
Object.values(this.serverKeys).map(
(keysFromSingleNode: any) => keysFromSingleNode.networkPubKeySet
)
);
this.hdRootPubkeys = mostCommonString(
Object.values(this.serverKeys).map(
(keysFromSingleNode: any) => keysFromSingleNode.hdRootPubkeys
)
);
this.latestBlockhash = mostCommonString(
Object.values(this.serverKeys).map(
(keysFromSingleNode: any) => keysFromSingleNode.latestBlockhash
)
);
if (!this.latestBlockhash) {
logErrorWithRequestId(
requestId,
'Error getting latest blockhash from the nodes.'
);
throwError({
message: 'Error getting latest blockhash from the nodes.',
errorKind: LIT_ERROR.INVALID_ETH_BLOCKHASH.kind,
errorCode: LIT_ERROR.INVALID_ETH_BLOCKHASH.name,
});
}
this.ready = true;
log(
`🔥 lit is ready. "litNodeClient" variable is ready to use globally.`
);
log('current network config', {
networkPubkey: this.networkPubKey,
networkPubKeySet: this.networkPubKeySet,
hdRootPubkeys: this.hdRootPubkeys,
subnetPubkey: this.subnetPubKey,
latesBlockhash: this.latestBlockhash,
});
// @ts-ignore
globalThis.litNodeClient = this;
// browser only
if (isBrowser()) {
document.dispatchEvent(new Event('lit-ready'));
}
// @ts-ignore: Expected 1 arguments, but got 0. Did you forget to include 'void' in your type argument to 'Promise'?ts(2794)
resolve();
} else {
const now = Date.now();
if (now - startTime > this.config.connectTimeout) {
clearInterval(interval);
const msg = `Error: Could not connect to enough nodes after timeout of ${
this.config.connectTimeout
}ms. Could only connect to ${
Object.keys(this.serverKeys).length
} of ${
this.config.minNodeCount
} required nodes. Please check your network connection and try again. Note that you can control this timeout with the connectTimeout config option which takes milliseconds.`;
logErrorWithRequestId(requestId, msg);
reject(msg);
}
}
}, 500);
});
return promise;
};
/**
*
* Get a random request ID
*
* @returns { string }
*
*/
getRequestId() {
return Math.random().toString(16).slice(2);
}
/**
*
* Get a random hex string for use as an attestation challenge
*
* @returns { string }
*/
getRandomHexString(size: number) {
return [...Array(size)]
.map(() => Math.floor(Math.random() * 16).toString(16))
.join('');
}
/**
*
* Handshake with Node
*
* @param { HandshakeWithNode } params
*
* @returns { Promise<NodeCommandServerKeysResponse> }
*
*/
handshakeWithNode = async (
params: HandshakeWithNode,
requestId: string
): Promise<NodeCommandServerKeysResponse> => {
// -- get properties from params
const { url } = params;
// -- create url with path
const urlWithPath = `${url}/web/handshake`;
log(`handshakeWithNode ${urlWithPath}`);
const data = {
clientPublicKey: 'test',
challenge: params.challenge,
};
return this.sendCommandToNode({
url: urlWithPath,
data,
requestId,
});
};
// ==================== SENDING COMMAND ====================
/**
*
* Send a command to nodes
*
* @param { SendNodeCommand }
*
* @returns { Promise<any> }
*
*/
sendCommandToNode = async ({
url,
data,
requestId,
}: SendNodeCommand): Promise<any> => {
logWithRequestId(
requestId,
`sendCommandToNode with url ${url} and data`,
data
);
const req: RequestInit = {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'X-Lit-SDK-Version': version,
'X-Lit-SDK-Type': 'Typescript',
'X-Request-Id': 'lit_' + requestId,
},
body: JSON.stringify(data),
};
return fetch(url, req)
.then(async (response) => {
const isJson = response.headers
.get('content-type')
?.includes('application/json');
const data = isJson ? await response.json() : null;
if (!response.ok) {
// get error message from body or default to response status
const error = data || response.status;
return Promise.reject(error);
}
return data;
})
.catch((error: NodeErrorV3) => {
logErrorWithRequestId(
requestId,
`Something went wrong, internal id for request: lit_${requestId}. Please provide this identifier with any support requests. ${
error?.message || error?.details
? `Error is ${error.message} - ${error.details}`
: ''
}`
);
return Promise.reject(error);
});
};
/**
*
* Get and gather node promises
*
* @param { any } callback
*
* @returns { Array<Promise<any>> }
*
*/
getNodePromises = (callback: Function): Array<Promise<any>> => {
const nodePromises = [];
for (const url of this.connectedNodes) {
nodePromises.push(callback(url));
}
return nodePromises;
};
/**
*
* Get either auth sig or session auth sig
*
*/
getSessionOrAuthSig = ({
authSig,
sessionSigs,
url,
mustHave = true,
}: {
authSig?: AuthSig;
sessionSigs?: SessionSigsMap;
url: string;
mustHave?: boolean;
}): AuthSig | SessionSig => {
if (!authSig && !sessionSigs) {
if (mustHave) {
throwError({
message: `You must pass either authSig, or sessionSigs`,
errorKind: LIT_ERROR.INVALID_ARGUMENT_EXCEPTION.kind,
errorCode: LIT_ERROR.INVALID_ARGUMENT_EXCEPTION.name,
});
} else {
log(`authSig or sessionSigs not found. This may be using authMethod`);
}
}
if (sessionSigs) {
const sigToPassToNode = sessionSigs[url];
if (!sigToPassToNode) {
throwError({
message: `You passed sessionSigs but we could not find session sig for node ${url}`,
errorKind: LIT_ERROR.INVALID_ARGUMENT_EXCEPTION.kind,
errorCode: LIT_ERROR.INVALID_ARGUMENT_EXCEPTION.name,
});
}
return sigToPassToNode;
}
return authSig!;
};
/**
*
* Get hash of access control conditions
*
* @param { MultipleAccessControlConditions } params
*
* @returns { Promise<ArrayBuffer | undefined> }
*
*/
getHashedAccessControlConditions = async (
params: MultipleAccessControlConditions
): Promise<ArrayBuffer | undefined> => {
let hashOfConditions: ArrayBuffer;
// ========== Prepare Params ==========
const {
accessControlConditions,
evmContractConditions,
solRpcConditions,
unifiedAccessControlConditions,
} = params;
// ========== Hash ==========
if (accessControlConditions) {
hashOfConditions = await hashAccessControlConditions(
accessControlConditions
);
} else if (evmContractConditions) {
hashOfConditions = await hashEVMContractConditions(evmContractConditions);
} else if (solRpcConditions) {
hashOfConditions = await hashSolRpcConditions(solRpcConditions);
} else if (unifiedAccessControlConditions) {
hashOfConditions = await hashUnifiedAccessControlConditions(
unifiedAccessControlConditions
);
} else {
return;
}
// ========== Result ==========
return hashOfConditions;
};
/**
* Handle node promises
*
* @param { Array<Promise<any>> } nodePromises
*
* @returns { Promise<SuccessNodePromises<T> | RejectedNodePromises> }
*
*/
handleNodePromises = async <T>(
nodePromises: Array<Promise<T>>,
requestId?: string,
minNodeCount?: number
): Promise<SuccessNodePromises<T> | RejectedNodePromises> => {
// -- prepare
const responses = await Promise.allSettled(nodePromises);
const minNodes = minNodeCount ?? this.config.minNodeCount;
// -- get fulfilled responses
const successes: Array<NodePromiseResponse> = responses.filter(
(r: any) => r.status === 'fulfilled'
);
// -- case: success (when success responses are more than minNodeCount)
if (successes.length >= minNodes) {
const successPromises: SuccessNodePromises<T> = {
success: true,
values: successes.map((r: any) => r.value),
};
return successPromises;
}
// -- case: if we're here, then we did not succeed. time to handle and report errors.
// -- get "rejected" responses
const rejected = responses.filter((r: any) => r.status === 'rejected');
const mostCommonError = JSON.parse(
mostCommonString(
rejected.map((r: NodePromiseResponse) => JSON.stringify(r.reason))
)
);
logErrorWithRequestId(
requestId || '',
`most common error: ${JSON.stringify(mostCommonError)}`
);
const rejectedPromises: RejectedNodePromises = {
success: false,
error: mostCommonError,
};
return rejectedPromises;
};
/**
*
* Throw node error
*
* @param { RejectedNodePromises } res
*
* @returns { void }
*
*/
_throwNodeError = (res: RejectedNodePromises): void => {
if (res.error && res.error.errorCode) {
if (
(res.error.errorCode === LIT_ERROR_CODE.NODE_NOT_AUTHORIZED ||
res.error.errorCode === 'not_authorized') &&
this.config.alertWhenUnauthorized
) {
log(
'[Alert originally] You are not authorized to access to this content'
);
}
throwError({
...res.error,
message:
res.error.message ||
'You are not authorized to access to this content',
errorCode: res.error.errorCode!,
} as NodeClientErrorV0 | NodeClientErrorV1);
} else {
throwError({
message: `There was an error getting the signing shares from the nodes`,
error: LIT_ERROR.UNKNOWN_ERROR,
});
}
};
/**
*
* Get different formats of access control conditions, eg. evm, sol, unified etc.
*
* @param { SupportedJsonRequests } params
*
* @returns { FormattedMultipleAccs }
*
*/
getFormattedAccessControlConditions = (
params: SupportedJsonRequests
): FormattedMultipleAccs => {
// -- prepare params
const {
accessControlConditions,
evmContractConditions,
solRpcConditions,
unifiedAccessControlConditions,
} = params;
// -- execute
let formattedAccessControlConditions;
let formattedEVMContractConditions;
let formattedSolRpcConditions;
let formattedUnifiedAccessControlConditions;
let error = false;
if (accessControlConditions) {
formattedAccessControlConditions = accessControlConditions.map((c: any) =>
canonicalAccessControlConditionFormatter(c)
);
log(
'formattedAccessControlConditions',
JSON.stringify(formattedAccessControlConditions)
);
} else if (evmContractConditions) {
formattedEVMContractConditions = evmContractConditions.map((c: any) =>
canonicalEVMContractConditionFormatter(c)
);
log(
'formattedEVMContractConditions',
JSON.stringify(formattedEVMContractConditions)
);
} else if (solRpcConditions) {
formattedSolRpcConditions = solRpcConditions.map((c: any) =>
canonicalSolRpcConditionFormatter(c)
);
log(
'formattedSolRpcConditions',
JSON.stringify(formattedSolRpcConditions)
);
} else if (unifiedAccessControlConditions) {
formattedUnifiedAccessControlConditions =
unifiedAccessControlConditions.map((c: any) =>
canonicalUnifiedAccessControlConditionFormatter(c)
);
log(
'formattedUnifiedAccessControlConditions',
JSON.stringify(formattedUnifiedAccessControlConditions)
);
} else {
error = true;
}
return {
error,
formattedAccessControlConditions,
formattedEVMContractConditions,
formattedSolRpcConditions,
formattedUnifiedAccessControlConditions,
};
};
/**
* Calculates an HD public key from a given {@link keyId} the curve type or signature type will assumed to be k256 unless given
* @param keyId
* @param sigType
* @returns {string} public key
*/
computeHDPubKey = (
keyId: string,
sigType: SIGTYPE = SIGTYPE.EcdsaCaitSith
): string => {
if (!this.hdRootPubkeys) {
logError('root public keys not found, have you connected to the nodes?');
throwError({
message: `root public keys not found, have you connected to the nodes?`,
errorKind: LIT_ERROR.LIT_NODE_CLIENT_NOT_READY_ERROR.kind,
errorCode: LIT_ERROR.LIT_NODE_CLIENT_NOT_READY_ERROR.code,
});
}
return computeHDPubKey(this.hdRootPubkeys as string[], keyId, sigType);
};
/**
* Calculates a Key Id for claiming a pkp based on a user identifier and an app identifier.
* The key Identifier is an Auth Method Id which scopes the key uniquely to a specific application context.
* These identifiers are specific to each auth method and will derive the public key protion of a pkp which will be persited
* when a key is claimed.
* | Auth Method | User ID | App ID |
* |:------------|:--------|:-------|
* | Google OAuth | token `sub` | token `aud` |
* | Discord OAuth | user id | client app identifier |
* | Stytch OTP |token `sub` | token `aud`|
* | Lit Actions | user defined | ipfs cid |
* *Note* Lit Action claiming uses a different schema than oter auth methods
* isForActionContext should be set for true if using claiming through actions
* @param userId {string} user identifier for the Key Identifier
* @param appId {string} app identifier for the Key Identifier
* @returns {String} public key of pkp when claimed
*/
computeHDKeyId(
userId: string,
appId: string,
isForActionContext: boolean = false
): string {
if (!isForActionContext) {
return ethers.utils.keccak256(
ethers.utils.toUtf8Bytes(`${userId}:${appId}`)
);
} else {
return ethers.utils.keccak256(
ethers.utils.toUtf8Bytes(`${appId}:${userId}`)
);
}
}
}