@nevermined-io/core-kit 0.1.4 → 0.1.6
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/dist/Instantiable.abstract.d.ts +33 -0
- package/dist/Instantiable.abstract.d.ts.map +1 -0
- package/dist/Instantiable.abstract.js +98 -0
- package/dist/artifacts/generated.d.ts +39371 -0
- package/dist/artifacts/generated.d.ts.map +1 -0
- package/dist/artifacts/generated.js +21448 -0
- package/dist/contracts/AccessManager.d.ts +17 -0
- package/dist/contracts/AccessManager.d.ts.map +1 -0
- package/dist/contracts/AccessManager.js +15 -0
- package/dist/contracts/AssetRegistry.d.ts +105 -0
- package/dist/contracts/AssetRegistry.d.ts.map +1 -0
- package/dist/contracts/AssetRegistry.js +155 -0
- package/dist/contracts/ContractBase.d.ts +45 -0
- package/dist/contracts/ContractBase.d.ts.map +1 -0
- package/dist/contracts/ContractBase.js +519 -0
- package/dist/contracts/ContractsApi.d.ts +69 -0
- package/dist/contracts/ContractsApi.d.ts.map +1 -0
- package/dist/contracts/ContractsApi.js +73 -0
- package/dist/contracts/CryptoTemplateBase.d.ts +11 -0
- package/dist/contracts/CryptoTemplateBase.d.ts.map +1 -0
- package/dist/contracts/CryptoTemplateBase.js +49 -0
- package/dist/contracts/FiatPaymentTemplate.d.ts +10 -0
- package/dist/contracts/FiatPaymentTemplate.d.ts.map +1 -0
- package/dist/contracts/FiatPaymentTemplate.js +14 -0
- package/dist/contracts/FiatSettlementCondition.d.ts +11 -0
- package/dist/contracts/FiatSettlementCondition.d.ts.map +1 -0
- package/dist/contracts/FiatSettlementCondition.js +18 -0
- package/dist/contracts/FixedPaymentTemplate.d.ts +12 -0
- package/dist/contracts/FixedPaymentTemplate.d.ts.map +1 -0
- package/dist/contracts/FixedPaymentTemplate.js +62 -0
- package/dist/contracts/NFT1155Base.d.ts +50 -0
- package/dist/contracts/NFT1155Base.d.ts.map +1 -0
- package/dist/contracts/NFT1155Base.js +24 -0
- package/dist/contracts/NFT1155Credits.d.ts +6 -0
- package/dist/contracts/NFT1155Credits.d.ts.map +1 -0
- package/dist/contracts/NFT1155Credits.js +9 -0
- package/dist/contracts/NFT1155ExpirableCredits.d.ts +34 -0
- package/dist/contracts/NFT1155ExpirableCredits.d.ts.map +1 -0
- package/dist/contracts/NFT1155ExpirableCredits.js +18 -0
- package/dist/contracts/NVMConfig.d.ts +10 -0
- package/dist/contracts/NVMConfig.d.ts.map +1 -0
- package/dist/contracts/NVMConfig.js +18 -0
- package/dist/contracts/PayAsYouGoTemplate.d.ts +12 -0
- package/dist/contracts/PayAsYouGoTemplate.d.ts.map +1 -0
- package/dist/contracts/PayAsYouGoTemplate.js +62 -0
- package/dist/contracts/ProtocolStandardFees.d.ts +9 -0
- package/dist/contracts/ProtocolStandardFees.d.ts.map +1 -0
- package/dist/contracts/ProtocolStandardFees.js +15 -0
- package/dist/contracts/Roles.d.ts +13 -0
- package/dist/contracts/Roles.d.ts.map +1 -0
- package/dist/contracts/Roles.js +12 -0
- package/dist/contracts/index.d.ts +3 -0
- package/dist/contracts/index.d.ts.map +1 -0
- package/dist/contracts/index.js +2 -0
- package/dist/errors/NeverminedErrors.d.ts +74 -0
- package/dist/errors/NeverminedErrors.d.ts.map +1 -0
- package/dist/errors/NeverminedErrors.js +128 -0
- package/dist/errors/index.d.ts +2 -0
- package/dist/errors/index.d.ts.map +1 -0
- package/dist/errors/index.js +1 -0
- package/dist/index.d.ts +8 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +8 -0
- package/dist/models/AgentAccessToken.d.ts +123 -0
- package/dist/models/AgentAccessToken.d.ts.map +1 -0
- package/dist/models/AgentAccessToken.js +164 -0
- package/dist/models/AgentX402AccessToken.d.ts +59 -0
- package/dist/models/AgentX402AccessToken.d.ts.map +1 -0
- package/dist/models/AgentX402AccessToken.js +78 -0
- package/dist/models/Logger.d.ts +9 -0
- package/dist/models/Logger.d.ts.map +1 -0
- package/dist/models/Logger.js +27 -0
- package/dist/models/NeverminedOptions.d.ts +30 -0
- package/dist/models/NeverminedOptions.d.ts.map +1 -0
- package/dist/models/NeverminedOptions.js +28 -0
- package/dist/models/NvmApiKey.d.ts +137 -0
- package/dist/models/NvmApiKey.d.ts.map +1 -0
- package/dist/models/NvmApiKey.js +235 -0
- package/dist/models/Transactions.d.ts +11 -0
- package/dist/models/Transactions.d.ts.map +1 -0
- package/dist/models/Transactions.js +1 -0
- package/dist/models/index.d.ts +7 -0
- package/dist/models/index.d.ts.map +1 -0
- package/dist/models/index.js +6 -0
- package/dist/nevermined/Nevermined.d.ts +48 -0
- package/dist/nevermined/Nevermined.d.ts.map +1 -0
- package/dist/nevermined/Nevermined.js +72 -0
- package/dist/nevermined/api/PaymentsApi.d.ts +24 -0
- package/dist/nevermined/api/PaymentsApi.d.ts.map +1 -0
- package/dist/nevermined/api/PaymentsApi.js +179 -0
- package/dist/nevermined/api/ServicesApi.d.ts +23 -0
- package/dist/nevermined/api/ServicesApi.d.ts.map +1 -0
- package/dist/nevermined/api/ServicesApi.js +27 -0
- package/dist/nevermined/api/UtilsApi.d.ts +28 -0
- package/dist/nevermined/api/UtilsApi.d.ts.map +1 -0
- package/dist/nevermined/api/UtilsApi.js +33 -0
- package/dist/nevermined/index.d.ts +3 -0
- package/dist/nevermined/index.d.ts.map +1 -0
- package/dist/nevermined/index.js +2 -0
- package/dist/nevermined/utils/AgentUtils.d.ts +8 -0
- package/dist/nevermined/utils/AgentUtils.d.ts.map +1 -0
- package/dist/nevermined/utils/AgentUtils.js +56 -0
- package/dist/nevermined/utils/BlockchainViemUtils.d.ts +280 -0
- package/dist/nevermined/utils/BlockchainViemUtils.d.ts.map +1 -0
- package/dist/nevermined/utils/BlockchainViemUtils.js +408 -0
- package/dist/nevermined/utils/JwtUtils.d.ts +27 -0
- package/dist/nevermined/utils/JwtUtils.d.ts.map +1 -0
- package/dist/nevermined/utils/JwtUtils.js +161 -0
- package/dist/nevermined/utils/SignatureUtils.d.ts +14 -0
- package/dist/nevermined/utils/SignatureUtils.d.ts.map +1 -0
- package/dist/nevermined/utils/SignatureUtils.js +99 -0
- package/dist/nevermined/utils/WebServiceConnector.d.ts +30 -0
- package/dist/nevermined/utils/WebServiceConnector.d.ts.map +1 -0
- package/dist/nevermined/utils/WebServiceConnector.js +170 -0
- package/dist/nevermined/utils/ZeroDevPolicies.d.ts +38 -0
- package/dist/nevermined/utils/ZeroDevPolicies.d.ts.map +1 -0
- package/dist/nevermined/utils/ZeroDevPolicies.js +196 -0
- package/dist/nevermined/utils/index.d.ts +7 -0
- package/dist/nevermined/utils/index.d.ts.map +1 -0
- package/dist/nevermined/utils/index.js +6 -0
- package/dist/services/Api.d.ts +7 -0
- package/dist/services/Api.d.ts.map +1 -0
- package/dist/services/Api.js +25 -0
- package/dist/services/Profiles.d.ts +16 -0
- package/dist/services/Profiles.d.ts.map +1 -0
- package/dist/services/Profiles.js +84 -0
- package/dist/services/index.d.ts +3 -0
- package/dist/services/index.d.ts.map +1 -0
- package/dist/services/index.js +2 -0
- package/dist/utils/ConversionTypeHelpers.d.ts +5 -0
- package/dist/utils/ConversionTypeHelpers.d.ts.map +1 -0
- package/dist/utils/ConversionTypeHelpers.js +30 -0
- package/dist/utils/DeploymentInfo.d.ts +15 -0
- package/dist/utils/DeploymentInfo.d.ts.map +1 -0
- package/dist/utils/DeploymentInfo.js +38 -0
- package/dist/utils/Network.d.ts +5 -0
- package/dist/utils/Network.d.ts.map +1 -0
- package/dist/utils/Network.js +224 -0
- package/dist/utils/helpers.d.ts +40 -0
- package/dist/utils/helpers.d.ts.map +1 -0
- package/dist/utils/helpers.js +189 -0
- package/dist/utils/index.d.ts +5 -0
- package/dist/utils/index.d.ts.map +1 -0
- package/dist/utils/index.js +4 -0
- package/package.json +3 -3
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"BlockchainViemUtils.d.ts","sourceRoot":"","sources":["../../../src/nevermined/utils/BlockchainViemUtils.ts"],"names":[],"mappings":"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"}
|
|
@@ -0,0 +1,408 @@
|
|
|
1
|
+
import { signerToEcdsaValidator } from '@zerodev/ecdsa-validator';
|
|
2
|
+
import { deserializePermissionAccount, serializePermissionAccount, toPermissionValidator, } from '@zerodev/permissions';
|
|
3
|
+
import { toECDSASigner } from '@zerodev/permissions/signers';
|
|
4
|
+
import { addressToEmptyAccount, createKernelAccount, createKernelAccountClient, createZeroDevPaymasterClient, getUserOperationGasPrice, } from '@zerodev/sdk';
|
|
5
|
+
import { KERNEL_V3_1, getEntryPoint } from '@zerodev/sdk/constants';
|
|
6
|
+
import { TransactionReceiptNotFoundError, encodeAbiParameters, getAbiItem, getAddress, getContract, http, isAddress, pad, stringToBytes, stringToHex, toBytes, keccak256 as viemKeccak256, } from 'viem';
|
|
7
|
+
import { english, generateMnemonic, generatePrivateKey, mnemonicToAccount, privateKeyToAccount, } from 'viem/accounts';
|
|
8
|
+
import { Instantiable } from '../../Instantiable.abstract.js';
|
|
9
|
+
import { ContractsError } from '../../errors/NeverminedErrors.js';
|
|
10
|
+
import { getChain } from '../../utils/Network.js';
|
|
11
|
+
import { _sleep } from '../../utils/helpers.js';
|
|
12
|
+
const ENTRY_POINT_VERSION = '0.7';
|
|
13
|
+
/**
|
|
14
|
+
* Utility class with methods that allow the interaction with the blockchain.
|
|
15
|
+
* This class uses Viem library to interact with the blockchain.
|
|
16
|
+
*/
|
|
17
|
+
export class BlockchainViemUtils extends Instantiable {
|
|
18
|
+
constructor(config) {
|
|
19
|
+
super();
|
|
20
|
+
this.setInstanceConfig(config);
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
//////////////////////////
|
|
24
|
+
///// UTILITIES //////////
|
|
25
|
+
//////////////////////////
|
|
26
|
+
///// CONTRACTS
|
|
27
|
+
/**
|
|
28
|
+
* Given a transaction hash, it returns the transaction receipt.
|
|
29
|
+
* If this function is called before the transaction is mined, it will iterate a few times in order to wait for the transaction to be mined.
|
|
30
|
+
* @param txHash - the transaction hash
|
|
31
|
+
* @param iteration - the iteration number
|
|
32
|
+
* @returns the transaction receipt
|
|
33
|
+
*/
|
|
34
|
+
export async function getTransactionReceipt({ txHash, iteration = 1, publicClient, }) {
|
|
35
|
+
if (iteration < 10) {
|
|
36
|
+
try {
|
|
37
|
+
return await publicClient.getTransactionReceipt({ hash: txHash });
|
|
38
|
+
}
|
|
39
|
+
catch (error) {
|
|
40
|
+
if (error instanceof TransactionReceiptNotFoundError) {
|
|
41
|
+
// this.logger.log(
|
|
42
|
+
// `Unable to get transaction receipt from hash ${txHash} on iteration ${iteration}. Sleeping and retrying.`,
|
|
43
|
+
// )
|
|
44
|
+
await _sleep(150);
|
|
45
|
+
return getTransactionReceipt({ txHash, iteration: iteration + 1, publicClient });
|
|
46
|
+
}
|
|
47
|
+
else {
|
|
48
|
+
const errorMessage = `Unknown error getting transaction receipt with hash: ${txHash}. Error: ${error}`;
|
|
49
|
+
// this.logger.error(errorMessage)
|
|
50
|
+
throw new ContractsError(errorMessage);
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
else {
|
|
55
|
+
const errorMessage = `Unable to get transaction receipt with hash: ${txHash} after ${iteration} iterations.`;
|
|
56
|
+
// this.logger.error(errorMessage)
|
|
57
|
+
throw new ContractsError(errorMessage);
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
/**
|
|
61
|
+
* Given an already deployed contract address and the ABI, it returns the contract instance.
|
|
62
|
+
*
|
|
63
|
+
* @param contractAddress - the contract address
|
|
64
|
+
* @param abi - the contract artifact
|
|
65
|
+
* @param client - the client to interact with the blockchain
|
|
66
|
+
* @returns a contract instance
|
|
67
|
+
*/
|
|
68
|
+
export async function getContractInstance(contractAddress, abi, client) {
|
|
69
|
+
return getContract({
|
|
70
|
+
abi,
|
|
71
|
+
address: contractAddress,
|
|
72
|
+
client: { wallet: client.wallet, public: client.public },
|
|
73
|
+
});
|
|
74
|
+
}
|
|
75
|
+
///// ABIs
|
|
76
|
+
/**
|
|
77
|
+
* It searchs an ABI function in the ABI.
|
|
78
|
+
* @param abi the ABI of the contract
|
|
79
|
+
* @param funcName the function name
|
|
80
|
+
* @param args the args of the function
|
|
81
|
+
* @returns the function found
|
|
82
|
+
*/
|
|
83
|
+
export function searchAbiFunction(abi, funcName, args = []) {
|
|
84
|
+
const func = getAbiItem({ abi, name: funcName, args });
|
|
85
|
+
if (!func || func.type !== 'function') {
|
|
86
|
+
throw new ContractsError(`Function "${funcName}" is not part of contract`);
|
|
87
|
+
}
|
|
88
|
+
return func;
|
|
89
|
+
}
|
|
90
|
+
/**
|
|
91
|
+
* It searchs an ABI event in the ABI.
|
|
92
|
+
* @param abi the ABI of the contract
|
|
93
|
+
* @param funcName the event name
|
|
94
|
+
* @returns the event found
|
|
95
|
+
*/
|
|
96
|
+
export function searchAbiEvent(abi, eventName) {
|
|
97
|
+
const event = getAbiItem({
|
|
98
|
+
abi,
|
|
99
|
+
name: eventName,
|
|
100
|
+
});
|
|
101
|
+
if (!event || event.type !== 'event') {
|
|
102
|
+
throw new ContractsError(`Event "${event}" is not part of contract`);
|
|
103
|
+
}
|
|
104
|
+
return event;
|
|
105
|
+
}
|
|
106
|
+
/**
|
|
107
|
+
* It searchs an ABI function in the ABI.
|
|
108
|
+
* @param abi the ABI of the contract
|
|
109
|
+
* @param funcName the function name
|
|
110
|
+
* @param args the args of the function
|
|
111
|
+
* @returns the function found
|
|
112
|
+
*/
|
|
113
|
+
export function getSignatureOfFunction(abi, funcName, args = []) {
|
|
114
|
+
return searchAbiFunction(abi, funcName, args);
|
|
115
|
+
}
|
|
116
|
+
/**
|
|
117
|
+
* It searchs an ABI function in the ABI and return the inputs.
|
|
118
|
+
* @param abi the ABI of the contract
|
|
119
|
+
* @param funcName the function name
|
|
120
|
+
* @param args the args of the function
|
|
121
|
+
* @returns the function found
|
|
122
|
+
*/
|
|
123
|
+
export function getInputsOfFunction(abi, funcName, args = []) {
|
|
124
|
+
return searchAbiFunction(abi, funcName, args).inputs;
|
|
125
|
+
}
|
|
126
|
+
/**
|
|
127
|
+
* It searchs an ABI function in the ABI and return the inputs formatted.
|
|
128
|
+
* @param abi the ABI of the contract
|
|
129
|
+
* @param funcName the function name
|
|
130
|
+
* @param args the args of the function
|
|
131
|
+
* @returns the function found
|
|
132
|
+
*/
|
|
133
|
+
export function getInputsOfFunctionFormatted(abi, funcName, args = []) {
|
|
134
|
+
return searchAbiFunction(abi, funcName, args).inputs.map((input, i) => {
|
|
135
|
+
return {
|
|
136
|
+
name: input.name,
|
|
137
|
+
value: args[i],
|
|
138
|
+
};
|
|
139
|
+
});
|
|
140
|
+
}
|
|
141
|
+
//////// UTILS
|
|
142
|
+
/**
|
|
143
|
+
* Given an address it returns that address in checksum format.
|
|
144
|
+
* @param address the address
|
|
145
|
+
* @returns the same address in checksum format
|
|
146
|
+
*/
|
|
147
|
+
export function getChecksumAddress(address) {
|
|
148
|
+
return getAddress(address);
|
|
149
|
+
}
|
|
150
|
+
/**
|
|
151
|
+
* It checks if the address is a valid address.
|
|
152
|
+
* @param address the address to check
|
|
153
|
+
* @returns true of the address is valid
|
|
154
|
+
*/
|
|
155
|
+
export function isValidAddress(address) {
|
|
156
|
+
return isAddress(address);
|
|
157
|
+
}
|
|
158
|
+
/**
|
|
159
|
+
* Encodes a UTF-8 string into a byte array.
|
|
160
|
+
|
|
161
|
+
* @param message the string to encode
|
|
162
|
+
* @returns the encoded byte array
|
|
163
|
+
*/
|
|
164
|
+
export function getBytes(message) {
|
|
165
|
+
return stringToBytes(message);
|
|
166
|
+
}
|
|
167
|
+
/**
|
|
168
|
+
* It pads a value with zeros.
|
|
169
|
+
* @param value the value to pad
|
|
170
|
+
* @param length the expected longitutde of the value
|
|
171
|
+
* @returns the padded value
|
|
172
|
+
*/
|
|
173
|
+
export function zeroPadValue(value, length) {
|
|
174
|
+
return pad(value, { size: length });
|
|
175
|
+
}
|
|
176
|
+
/**
|
|
177
|
+
* Encodes a UTF-8 string into a hex string
|
|
178
|
+
* @param message the string to encode
|
|
179
|
+
* @returns the hex string
|
|
180
|
+
*/
|
|
181
|
+
export function encodeBytes32String(message) {
|
|
182
|
+
return stringToHex(message, { size: 32 });
|
|
183
|
+
}
|
|
184
|
+
////// ACCOUNTS
|
|
185
|
+
/**
|
|
186
|
+
* Given a seedphrase, it returns an account.
|
|
187
|
+
* @param seedphrase - the seedphrase to be used to generate the account
|
|
188
|
+
* @param addressIndex - the address index
|
|
189
|
+
* @returns an account
|
|
190
|
+
*/
|
|
191
|
+
export function makeWallet(seedphrase, addressIndex = 0) {
|
|
192
|
+
return mnemonicToAccount(seedphrase, { addressIndex });
|
|
193
|
+
}
|
|
194
|
+
/**
|
|
195
|
+
* Given a seedphrase generates multiple accounts
|
|
196
|
+
* @param seedphrase - the seedphrase to be used to generate the account
|
|
197
|
+
* @param numAccounts - the number of accounts to create
|
|
198
|
+
* @returns the array of accounts
|
|
199
|
+
*/
|
|
200
|
+
export function makeWallets(seedphrase, numAccounts = 10) {
|
|
201
|
+
const accounts = [];
|
|
202
|
+
for (let i = 0; i < numAccounts; i++) {
|
|
203
|
+
accounts.push(makeWallet(seedphrase, i));
|
|
204
|
+
}
|
|
205
|
+
return accounts;
|
|
206
|
+
}
|
|
207
|
+
/**
|
|
208
|
+
* It generates a random account.
|
|
209
|
+
* @returns a new account
|
|
210
|
+
*/
|
|
211
|
+
export function makeRandomWallet() {
|
|
212
|
+
const mnemonic = generateMnemonic(english);
|
|
213
|
+
return makeWallet(mnemonic);
|
|
214
|
+
}
|
|
215
|
+
/**
|
|
216
|
+
* It generates a list of random accounts
|
|
217
|
+
* @param numAccounts - the number of accounts to create
|
|
218
|
+
* @returns the array of accounts
|
|
219
|
+
*/
|
|
220
|
+
export function makeRandomWallets(numAccounts = 10) {
|
|
221
|
+
const mnemonic = generateMnemonic(english);
|
|
222
|
+
return makeWallets(mnemonic, numAccounts);
|
|
223
|
+
}
|
|
224
|
+
/////// HASHES
|
|
225
|
+
/**
|
|
226
|
+
* It hashes a string using keccak256.
|
|
227
|
+
* @param seed the string to hash
|
|
228
|
+
* @returns the hash
|
|
229
|
+
*/
|
|
230
|
+
export function keccak256(seed) {
|
|
231
|
+
return viemKeccak256(toBytes(seed));
|
|
232
|
+
}
|
|
233
|
+
/**
|
|
234
|
+
* It encodes and hashes a list of primitive values into an ABI-encoded hex value.
|
|
235
|
+
* @param types the types of the values
|
|
236
|
+
* @param values the values to encode
|
|
237
|
+
* @returns the hash
|
|
238
|
+
*/
|
|
239
|
+
export function keccak256WithEncode(types, values) {
|
|
240
|
+
const encoded = encodeAbiParameters(types, values);
|
|
241
|
+
return keccak256(encoded);
|
|
242
|
+
}
|
|
243
|
+
/**
|
|
244
|
+
* It encodes and hashes a list of primitive values into an ABI-encoded hex value.
|
|
245
|
+
* @param types the types of the values
|
|
246
|
+
* @param values the values to encode
|
|
247
|
+
* @returns the hash
|
|
248
|
+
*/
|
|
249
|
+
export function keccak256Packed(types, values) {
|
|
250
|
+
return keccak256WithEncode(types, values);
|
|
251
|
+
}
|
|
252
|
+
/////// ZERO DEV
|
|
253
|
+
/**
|
|
254
|
+
* It creates a ZeroDev Kernel client.
|
|
255
|
+
* @param signer the signer account
|
|
256
|
+
* @param chainId the chain id
|
|
257
|
+
* @param zeroDevProjectId the zero dev project id, you can get it from the ZeroDev dashboard
|
|
258
|
+
* @returns the kernel client
|
|
259
|
+
*/
|
|
260
|
+
export async function createKernelClient(kernelAccount, chainId, zeroDevProjectId, publicClient) {
|
|
261
|
+
const ZERODEV_RPC = `https://rpc.zerodev.app/api/v3/${zeroDevProjectId}/chain/${chainId}`;
|
|
262
|
+
return createKernelAccountClient({
|
|
263
|
+
account: kernelAccount,
|
|
264
|
+
chain: getChain(chainId),
|
|
265
|
+
bundlerTransport: http(ZERODEV_RPC),
|
|
266
|
+
client: publicClient,
|
|
267
|
+
paymaster: {
|
|
268
|
+
getPaymasterData: (userOperation) => {
|
|
269
|
+
const zerodevPaymaster = createZeroDevPaymasterClient({
|
|
270
|
+
chain: getChain(chainId),
|
|
271
|
+
transport: http(ZERODEV_RPC),
|
|
272
|
+
});
|
|
273
|
+
return zerodevPaymaster.sponsorUserOperation({
|
|
274
|
+
userOperation,
|
|
275
|
+
});
|
|
276
|
+
},
|
|
277
|
+
},
|
|
278
|
+
userOperation: {
|
|
279
|
+
estimateFeesPerGas: async ({ bundlerClient }) => {
|
|
280
|
+
return getUserOperationGasPrice(bundlerClient);
|
|
281
|
+
},
|
|
282
|
+
},
|
|
283
|
+
});
|
|
284
|
+
}
|
|
285
|
+
export async function createKernelSmartAccount(signer, publicClient) {
|
|
286
|
+
const ecdsaValidator = await signerToEcdsaValidator(publicClient, {
|
|
287
|
+
signer: signer,
|
|
288
|
+
entryPoint: getEntryPoint(ENTRY_POINT_VERSION),
|
|
289
|
+
kernelVersion: KERNEL_V3_1,
|
|
290
|
+
});
|
|
291
|
+
return await createKernelAccount(publicClient, {
|
|
292
|
+
plugins: {
|
|
293
|
+
sudo: ecdsaValidator,
|
|
294
|
+
},
|
|
295
|
+
entryPoint: getEntryPoint(ENTRY_POINT_VERSION),
|
|
296
|
+
kernelVersion: KERNEL_V3_1,
|
|
297
|
+
});
|
|
298
|
+
}
|
|
299
|
+
export async function getApproval(signer, publicClient, policies) {
|
|
300
|
+
const ecdsaValidator = await signerToEcdsaValidator(publicClient, {
|
|
301
|
+
entryPoint: getEntryPoint(ENTRY_POINT_VERSION),
|
|
302
|
+
signer,
|
|
303
|
+
kernelVersion: KERNEL_V3_1,
|
|
304
|
+
});
|
|
305
|
+
const emptyAccount = addressToEmptyAccount(signer.address);
|
|
306
|
+
const emptySessionKeySigner = await toECDSASigner({ signer: emptyAccount });
|
|
307
|
+
const permissionPlugin = await toPermissionValidator(publicClient, {
|
|
308
|
+
entryPoint: getEntryPoint(ENTRY_POINT_VERSION),
|
|
309
|
+
signer: emptySessionKeySigner,
|
|
310
|
+
policies: policies,
|
|
311
|
+
kernelVersion: KERNEL_V3_1,
|
|
312
|
+
});
|
|
313
|
+
const sessionKeyAccount = await createKernelAccount(publicClient, {
|
|
314
|
+
entryPoint: getEntryPoint(ENTRY_POINT_VERSION),
|
|
315
|
+
plugins: {
|
|
316
|
+
sudo: ecdsaValidator,
|
|
317
|
+
regular: permissionPlugin,
|
|
318
|
+
},
|
|
319
|
+
kernelVersion: KERNEL_V3_1,
|
|
320
|
+
});
|
|
321
|
+
return await serializePermissionAccount(sessionKeyAccount);
|
|
322
|
+
}
|
|
323
|
+
export async function useSessionKey(approval, signer, publicClient) {
|
|
324
|
+
const sessionKeySigner = await toECDSASigner({
|
|
325
|
+
signer,
|
|
326
|
+
});
|
|
327
|
+
const sessionKeyAccount = await deserializePermissionAccount(publicClient, getEntryPoint(ENTRY_POINT_VERSION), KERNEL_V3_1, approval, sessionKeySigner);
|
|
328
|
+
return sessionKeyAccount;
|
|
329
|
+
}
|
|
330
|
+
export async function createSessionKey(signer, publicClient, policies) {
|
|
331
|
+
const ecdsaValidator = await signerToEcdsaValidator(publicClient, {
|
|
332
|
+
entryPoint: getEntryPoint(ENTRY_POINT_VERSION),
|
|
333
|
+
signer,
|
|
334
|
+
kernelVersion: KERNEL_V3_1,
|
|
335
|
+
});
|
|
336
|
+
const sessionPrivateKey = generatePrivateKey();
|
|
337
|
+
const masterAccount = privateKeyToAccount(sessionPrivateKey);
|
|
338
|
+
const sessionKeySigner = await toECDSASigner({
|
|
339
|
+
signer: masterAccount,
|
|
340
|
+
});
|
|
341
|
+
const permissionPlugin = await toPermissionValidator(publicClient, {
|
|
342
|
+
entryPoint: getEntryPoint(ENTRY_POINT_VERSION),
|
|
343
|
+
signer: sessionKeySigner,
|
|
344
|
+
policies: policies,
|
|
345
|
+
kernelVersion: KERNEL_V3_1,
|
|
346
|
+
});
|
|
347
|
+
const sessionKeyAccount = await createKernelAccount(publicClient, {
|
|
348
|
+
entryPoint: getEntryPoint(ENTRY_POINT_VERSION),
|
|
349
|
+
plugins: {
|
|
350
|
+
sudo: ecdsaValidator,
|
|
351
|
+
regular: permissionPlugin,
|
|
352
|
+
},
|
|
353
|
+
kernelVersion: KERNEL_V3_1,
|
|
354
|
+
});
|
|
355
|
+
return serializePermissionAccount(sessionKeyAccount, sessionPrivateKey);
|
|
356
|
+
}
|
|
357
|
+
export async function getSessionKey(serializedSessionKey, publicClient) {
|
|
358
|
+
const sessionKeyAccount = await deserializePermissionAccount(publicClient, getEntryPoint(ENTRY_POINT_VERSION), KERNEL_V3_1, serializedSessionKey);
|
|
359
|
+
return sessionKeyAccount;
|
|
360
|
+
}
|
|
361
|
+
/**
|
|
362
|
+
* Creates a delegated session key for an existing kernel account.
|
|
363
|
+
* The resulting serialized session key can be deserialized into a SmartAccount
|
|
364
|
+
* that shares the same address as the kernel account but enforces the given policies.
|
|
365
|
+
*
|
|
366
|
+
* @param kernelAccount The existing SmartAccount (kernel) to delegate from
|
|
367
|
+
* @param publicClient Viem PublicClient instance
|
|
368
|
+
* @param policies Array of policies to enforce for this session key
|
|
369
|
+
* @returns Serialized session key string
|
|
370
|
+
*/
|
|
371
|
+
export async function createDelegatedSessionKeyFromKernel(kernelAccount, publicClient, policies) {
|
|
372
|
+
// 1) Generate ephemeral session private key (used only for the permission plugin)
|
|
373
|
+
const sessionPrivateKey = generatePrivateKey();
|
|
374
|
+
const sessionAccount = privateKeyToAccount(sessionPrivateKey);
|
|
375
|
+
// 2) Wrap ephemeral key as ECDSA signer for permissions
|
|
376
|
+
const sessionKeySigner = await toECDSASigner({ signer: sessionAccount });
|
|
377
|
+
// 3) Create the permission validator plugin
|
|
378
|
+
const permissionValidator = await toPermissionValidator(publicClient, {
|
|
379
|
+
entryPoint: getEntryPoint(ENTRY_POINT_VERSION),
|
|
380
|
+
signer: sessionKeySigner,
|
|
381
|
+
policies,
|
|
382
|
+
kernelVersion: KERNEL_V3_1,
|
|
383
|
+
});
|
|
384
|
+
// 4) Extract the sudo validator from the existing kernel account
|
|
385
|
+
const kernelAccountTyped = kernelAccount;
|
|
386
|
+
const sudoValidator = kernelAccountTyped.kernelPluginManager.sudoValidator;
|
|
387
|
+
if (!sudoValidator) {
|
|
388
|
+
throw new Error('Kernel account does not have a sudo validator');
|
|
389
|
+
}
|
|
390
|
+
// 5) Create a new kernel account with both sudo (from original) and permission validator
|
|
391
|
+
const sessionKeyAccount = await createKernelAccount(publicClient, {
|
|
392
|
+
entryPoint: getEntryPoint(ENTRY_POINT_VERSION),
|
|
393
|
+
plugins: {
|
|
394
|
+
sudo: sudoValidator, // Use existing sudo validator
|
|
395
|
+
regular: permissionValidator,
|
|
396
|
+
},
|
|
397
|
+
kernelVersion: KERNEL_V3_1,
|
|
398
|
+
});
|
|
399
|
+
// 6) Serialize the session key account with the ephemeral private key
|
|
400
|
+
const serialized = await serializePermissionAccount(sessionKeyAccount, sessionPrivateKey);
|
|
401
|
+
return serialized;
|
|
402
|
+
}
|
|
403
|
+
export const WalletUtils = {
|
|
404
|
+
makeWallet,
|
|
405
|
+
makeWallets,
|
|
406
|
+
makeRandomWallet,
|
|
407
|
+
makeRandomWallets,
|
|
408
|
+
};
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import type { Eip712Data } from '@nvm-monorepo/commons';
|
|
2
|
+
import { type JWSHeaderParameters, SignJWT } from 'jose';
|
|
3
|
+
import { type Account } from 'viem';
|
|
4
|
+
import { SmartAccount } from 'viem/account-abstraction';
|
|
5
|
+
import { Instantiable, InstantiableConfig } from '../../Instantiable.abstract.js';
|
|
6
|
+
import { SignatureUtils } from './SignatureUtils.js';
|
|
7
|
+
export declare class EthSignJWT extends SignJWT {
|
|
8
|
+
protectedHeader: JWSHeaderParameters | undefined;
|
|
9
|
+
setProtectedHeader(protectedHeader: JWSHeaderParameters): this;
|
|
10
|
+
ethSign(signatureUtils: SignatureUtils, account: Account | SmartAccount, eip712Data?: Eip712Data): Promise<string>;
|
|
11
|
+
static signText(text: string | Uint8Array, account: Account): Promise<string | undefined>;
|
|
12
|
+
private base64url;
|
|
13
|
+
private concat;
|
|
14
|
+
}
|
|
15
|
+
export declare class JwtUtils extends Instantiable {
|
|
16
|
+
static CLIENT_ASSERTION_TYPE: string;
|
|
17
|
+
tokenCache: Map<string, string>;
|
|
18
|
+
signatureUtils: SignatureUtils;
|
|
19
|
+
constructor(config: InstantiableConfig);
|
|
20
|
+
generateCacheKey(...args: string[]): string;
|
|
21
|
+
accountToJwk(account: Account | SmartAccount): Promise<any>;
|
|
22
|
+
generateClientAssertion(account: Account | SmartAccount, message?: string): Promise<string>;
|
|
23
|
+
isTokenValid(token: string): boolean;
|
|
24
|
+
static createCompressedJwt(token: string): string;
|
|
25
|
+
static decompressJwt(compressedJwt: string): string;
|
|
26
|
+
}
|
|
27
|
+
//# sourceMappingURL=JwtUtils.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"JwtUtils.d.ts","sourceRoot":"","sources":["../../../src/nevermined/utils/JwtUtils.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,uBAAuB,CAAA;AAEvD,OAAO,EAAE,KAAK,mBAAmB,EAAE,OAAO,EAAwB,MAAM,MAAM,CAAA;AAC9E,OAAO,EAAE,KAAK,OAAO,EAAmD,MAAM,MAAM,CAAA;AACpF,OAAO,EAAE,YAAY,EAAE,MAAM,0BAA0B,CAAA;AACvD,OAAO,EAAE,YAAY,EAAE,kBAAkB,EAAE,MAAM,gCAAgC,CAAA;AAGjF,OAAO,EAAE,cAAc,EAAE,MAAM,qBAAqB,CAAA;AAEpD,qBAAa,UAAW,SAAQ,OAAO;IACrC,eAAe,EAAE,mBAAmB,GAAG,SAAS,CAAA;IAEvC,kBAAkB,CAAC,eAAe,EAAE,mBAAmB;IAKnD,OAAO,CAClB,cAAc,EAAE,cAAc,EAC9B,OAAO,EAAE,OAAO,GAAG,YAAY,EAC/B,UAAU,CAAC,EAAE,UAAU,GACtB,OAAO,CAAC,MAAM,CAAC;WAoDE,QAAQ,CAC1B,IAAI,EAAE,MAAM,GAAG,UAAU,EACzB,OAAO,EAAE,OAAO,GACf,OAAO,CAAC,MAAM,GAAG,SAAS,CAAC;IAY9B,OAAO,CAAC,SAAS;IAKjB,OAAO,CAAC,MAAM;CAUf;AAED,qBAAa,QAAS,SAAQ,YAAY;IACxC,MAAM,CAAC,qBAAqB,SAA2D;IAGvF,UAAU,EAAE,GAAG,CAAC,MAAM,EAAE,MAAM,CAAC,CAAA;IAC/B,cAAc,EAAE,cAAc,CAAA;gBAElB,MAAM,EAAE,kBAAkB;IAO/B,gBAAgB,CAAC,GAAG,IAAI,EAAE,MAAM,EAAE,GAAG,MAAM;IAIrC,YAAY,CAAC,OAAO,EAAE,OAAO,GAAG,YAAY,GAAG,OAAO,CAAC,GAAG,CAAC;IAqB3D,uBAAuB,CAAC,OAAO,EAAE,OAAO,GAAG,YAAY,EAAE,OAAO,CAAC,EAAE,MAAM;IA2B/E,YAAY,CAAC,KAAK,EAAE,MAAM,GAAG,OAAO;IAc3C,MAAM,CAAC,mBAAmB,CAAC,KAAK,EAAE,MAAM;IAKxC,MAAM,CAAC,aAAa,CAAC,aAAa,EAAE,MAAM;CAK3C"}
|
|
@@ -0,0 +1,161 @@
|
|
|
1
|
+
import { deflateSync, inflateSync } from 'fflate';
|
|
2
|
+
import { SignJWT, decodeJwt, importJWK } from 'jose';
|
|
3
|
+
import { hexToBytes, toHex } from 'viem';
|
|
4
|
+
import { Instantiable } from '../../Instantiable.abstract.js';
|
|
5
|
+
import { urlSafeBase64Decode, urlSafeBase64Encode } from '../../utils/helpers.js';
|
|
6
|
+
import { getChecksumAddress } from './BlockchainViemUtils.js';
|
|
7
|
+
import { SignatureUtils } from './SignatureUtils.js';
|
|
8
|
+
export class EthSignJWT extends SignJWT {
|
|
9
|
+
protectedHeader;
|
|
10
|
+
setProtectedHeader(protectedHeader) {
|
|
11
|
+
this.protectedHeader = protectedHeader;
|
|
12
|
+
return this;
|
|
13
|
+
}
|
|
14
|
+
async ethSign(signatureUtils, account, eip712Data) {
|
|
15
|
+
const encoder = new TextEncoder();
|
|
16
|
+
const decoder = new TextDecoder();
|
|
17
|
+
let payload = this._payload;
|
|
18
|
+
if (eip712Data) {
|
|
19
|
+
payload = {
|
|
20
|
+
...payload,
|
|
21
|
+
eip712Data,
|
|
22
|
+
};
|
|
23
|
+
}
|
|
24
|
+
const encodedPayload = encoder.encode(this.base64url(JSON.stringify(payload)));
|
|
25
|
+
const encodedHeader = encoder.encode(this.base64url(JSON.stringify(this.protectedHeader)));
|
|
26
|
+
const data = this.concat(encodedHeader, encoder.encode('.'), encodedPayload);
|
|
27
|
+
// EIP-712 signature
|
|
28
|
+
let sign;
|
|
29
|
+
if (eip712Data) {
|
|
30
|
+
const domain = {
|
|
31
|
+
name: 'Nevermined',
|
|
32
|
+
version: '1',
|
|
33
|
+
chainId: eip712Data.chainId,
|
|
34
|
+
};
|
|
35
|
+
const types = {
|
|
36
|
+
Nevermined: [
|
|
37
|
+
{ name: 'from', type: 'address' },
|
|
38
|
+
{ name: 'message', type: 'string' },
|
|
39
|
+
{ name: 'token', type: 'string' },
|
|
40
|
+
],
|
|
41
|
+
};
|
|
42
|
+
const value = {
|
|
43
|
+
from: account.address,
|
|
44
|
+
message: eip712Data.message,
|
|
45
|
+
token: decoder.decode(data),
|
|
46
|
+
};
|
|
47
|
+
sign = await signatureUtils.signTypedData(domain, types, value, account);
|
|
48
|
+
}
|
|
49
|
+
else {
|
|
50
|
+
sign = await signatureUtils.signText(decoder.decode(data), account);
|
|
51
|
+
}
|
|
52
|
+
const input = hexToBytes(sign);
|
|
53
|
+
const signed = this.base64url(input);
|
|
54
|
+
const grantToken = `${decoder.decode(encodedHeader)}.${decoder.decode(encodedPayload)}.${signed}`;
|
|
55
|
+
return grantToken;
|
|
56
|
+
}
|
|
57
|
+
static async signText(text, account) {
|
|
58
|
+
try {
|
|
59
|
+
const message = typeof text === 'string' ? text : toHex(text);
|
|
60
|
+
return account.signMessage({
|
|
61
|
+
message: message,
|
|
62
|
+
});
|
|
63
|
+
}
|
|
64
|
+
catch (e) {
|
|
65
|
+
console.error('Error signing message:', e);
|
|
66
|
+
return undefined;
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
base64url(input) {
|
|
70
|
+
const buffer = typeof input === 'string' ? Buffer.from(input, 'utf-8') : Buffer.from(input);
|
|
71
|
+
return buffer.toString('base64').replace(/=/g, '').replace(/\+/g, '-').replace(/\//g, '_');
|
|
72
|
+
}
|
|
73
|
+
concat(...buffers) {
|
|
74
|
+
const size = buffers.reduce((acc, { length }) => acc + length, 0);
|
|
75
|
+
const buf = new Uint8Array(size);
|
|
76
|
+
let i = 0;
|
|
77
|
+
buffers.forEach((buffer) => {
|
|
78
|
+
buf.set(buffer, i);
|
|
79
|
+
i += buffer.length;
|
|
80
|
+
});
|
|
81
|
+
return buf;
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
export class JwtUtils extends Instantiable {
|
|
85
|
+
static CLIENT_ASSERTION_TYPE = 'urn:ietf:params:oauth:client-assertion-type:jwt-bearer';
|
|
86
|
+
// BASE_AUD = '/api/v1/node/services'
|
|
87
|
+
tokenCache;
|
|
88
|
+
signatureUtils;
|
|
89
|
+
constructor(config) {
|
|
90
|
+
super();
|
|
91
|
+
this.setInstanceConfig(config);
|
|
92
|
+
this.tokenCache = new Map();
|
|
93
|
+
this.signatureUtils = new SignatureUtils(config);
|
|
94
|
+
}
|
|
95
|
+
generateCacheKey(...args) {
|
|
96
|
+
return args.join();
|
|
97
|
+
}
|
|
98
|
+
async accountToJwk(account) {
|
|
99
|
+
const address = account.address.toLowerCase();
|
|
100
|
+
// Currently only works with HDWalletProvider
|
|
101
|
+
// eslint-disable-next-line
|
|
102
|
+
// @ts-ignore
|
|
103
|
+
const publicKey = this.web3.currentProvider.wallets[address].getPublicKey();
|
|
104
|
+
// eslint-disable-next-line
|
|
105
|
+
// @ts-ignore
|
|
106
|
+
const privateKey = this.web3.currentProvider.wallets[address].getPrivateKey();
|
|
107
|
+
return importJWK({
|
|
108
|
+
alg: 'ES256K',
|
|
109
|
+
crv: 'secp256k1',
|
|
110
|
+
kty: 'EC',
|
|
111
|
+
d: privateKey.toString('base64'),
|
|
112
|
+
x: publicKey.slice(0, 32).toString('base64'),
|
|
113
|
+
y: publicKey.slice(32, 64).toString('base64'),
|
|
114
|
+
});
|
|
115
|
+
}
|
|
116
|
+
async generateClientAssertion(account, message) {
|
|
117
|
+
let eip712Data;
|
|
118
|
+
if (message) {
|
|
119
|
+
eip712Data = {
|
|
120
|
+
message,
|
|
121
|
+
chainId: await this.publicClient.getChainId(),
|
|
122
|
+
};
|
|
123
|
+
}
|
|
124
|
+
const address = getChecksumAddress(account.address);
|
|
125
|
+
// Not need it, but stay commented. If we need to bring back roles to update endpoints based on wallet with roles.
|
|
126
|
+
// const isAdmin = await this.nevermined.contracts.accessManager.hasRole(
|
|
127
|
+
// NVM_INFRA_ADMIN_ROLE,
|
|
128
|
+
// address as Address,
|
|
129
|
+
// )
|
|
130
|
+
return new EthSignJWT({
|
|
131
|
+
iss: address,
|
|
132
|
+
// roles: isAdmin[0] ? ['admin'] : [],
|
|
133
|
+
roles: [],
|
|
134
|
+
})
|
|
135
|
+
.setProtectedHeader({ alg: 'ES256K' })
|
|
136
|
+
.setIssuedAt()
|
|
137
|
+
.setExpirationTime('1h')
|
|
138
|
+
.ethSign(this.signatureUtils, account, eip712Data);
|
|
139
|
+
}
|
|
140
|
+
isTokenValid(token) {
|
|
141
|
+
const decodedToken = decodeJwt(token);
|
|
142
|
+
if (!decodedToken) {
|
|
143
|
+
return false;
|
|
144
|
+
}
|
|
145
|
+
const expiry = decodedToken.exp;
|
|
146
|
+
if (expiry) {
|
|
147
|
+
const now = new Date();
|
|
148
|
+
return now.getTime() < Number(expiry) * 1000;
|
|
149
|
+
}
|
|
150
|
+
return false;
|
|
151
|
+
}
|
|
152
|
+
static createCompressedJwt(token) {
|
|
153
|
+
const compressed = deflateSync(new TextEncoder().encode(token));
|
|
154
|
+
return urlSafeBase64Encode(compressed);
|
|
155
|
+
}
|
|
156
|
+
static decompressJwt(compressedJwt) {
|
|
157
|
+
const decoded = urlSafeBase64Decode(compressedJwt);
|
|
158
|
+
const decompressed = inflateSync(decoded);
|
|
159
|
+
return new TextDecoder().decode(decompressed);
|
|
160
|
+
}
|
|
161
|
+
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { Account, type Hash } from 'viem';
|
|
2
|
+
import { Instantiable, InstantiableConfig } from '../../Instantiable.abstract.js';
|
|
3
|
+
import type { TypedDataDomain, TypedDataTypes } from '@nvm-monorepo/commons';
|
|
4
|
+
import { SmartAccount } from 'viem/account-abstraction';
|
|
5
|
+
export declare class SignatureUtils extends Instantiable {
|
|
6
|
+
constructor(config: InstantiableConfig);
|
|
7
|
+
signText(text: string | Uint8Array, account: Account | SmartAccount): Promise<Hash>;
|
|
8
|
+
signTypedData(domain: TypedDataDomain, types: TypedDataTypes, value: Record<string, any>, account: Account | SmartAccount): Promise<Hash>;
|
|
9
|
+
signTransaction(tx: `0x${string}`, account: Account | SmartAccount): Promise<string>;
|
|
10
|
+
verifyIsSigner(text: string, signature: string, signerAddress: string): Promise<boolean>;
|
|
11
|
+
static recoverSignerAddress(message: string, signature: string): Promise<string>;
|
|
12
|
+
static hash(seed: string): string;
|
|
13
|
+
}
|
|
14
|
+
//# sourceMappingURL=SignatureUtils.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"SignatureUtils.d.ts","sourceRoot":"","sources":["../../../src/nevermined/utils/SignatureUtils.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,KAAK,IAAI,EAAmD,MAAM,MAAM,CAAA;AAC1F,OAAO,EAAE,YAAY,EAAE,kBAAkB,EAAE,MAAM,gCAAgC,CAAA;AAEjF,OAAO,KAAK,EAAE,eAAe,EAAE,cAAc,EAAE,MAAM,uBAAuB,CAAA;AAC5E,OAAO,EAAE,YAAY,EAAE,MAAM,0BAA0B,CAAA;AAGvD,qBAAa,cAAe,SAAQ,YAAY;gBAClC,MAAM,EAAE,kBAAkB;IAKzB,QAAQ,CAAC,IAAI,EAAE,MAAM,GAAG,UAAU,EAAE,OAAO,EAAE,OAAO,GAAG,YAAY,GAAG,OAAO,CAAC,IAAI,CAAC;IAqBnF,aAAa,CACxB,MAAM,EAAE,eAAe,EACvB,KAAK,EAAE,cAAc,EACrB,KAAK,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,EAC1B,OAAO,EAAE,OAAO,GAAG,YAAY,GAC9B,OAAO,CAAC,IAAI,CAAC;IA6BH,eAAe,CAC1B,EAAE,EAAE,KAAK,MAAM,EAAE,EACjB,OAAO,EAAE,OAAO,GAAG,YAAY,GAC9B,OAAO,CAAC,MAAM,CAAC;IAmBL,cAAc,CACzB,IAAI,EAAE,MAAM,EACZ,SAAS,EAAE,MAAM,EACjB,aAAa,EAAE,MAAM,GACpB,OAAO,CAAC,OAAO,CAAC;WAQN,oBAAoB,CAAC,OAAO,EAAE,MAAM,EAAE,SAAS,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,CAAC;IAOtF,MAAM,CAAC,IAAI,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM;CAGlC"}
|