|
| 1 | +import { z } from 'zod'; |
| 2 | + |
| 3 | +import { |
| 4 | + ILitResourceSchema, |
| 5 | + ISessionCapabilityObjectSchema, |
| 6 | + LitNodeClientConfigSchema, |
| 7 | +} from './models'; |
| 8 | +import { |
| 9 | + DecryptRequestSchema, |
| 10 | + DecryptResponseSchema, |
| 11 | + EncryptResponseSchema, |
| 12 | + EncryptSdkParamsSchema, |
| 13 | + ExecuteJsResponseSchema, |
| 14 | + FormattedMultipleAccsSchema, |
| 15 | + GetSignedTokenRequestSchema, |
| 16 | + HandshakeWithNodeSchema, |
| 17 | + JsonExecutionSdkParamsSchema, |
| 18 | + JsonHandshakeResponseSchema, |
| 19 | + MultipleAccessControlConditionsSchema, |
| 20 | + NodeBlsSigningShareSchema, |
| 21 | + NodeCommandServerKeysResponseSchema, |
| 22 | + RejectedNodePromisesSchema, |
| 23 | + SendNodeCommandSchema, |
| 24 | + SuccessNodePromisesSchema, |
| 25 | + SupportedJsonRequestsSchema, |
| 26 | +} from './schemas'; |
| 27 | + |
| 28 | +export const ILitNodeClientSchema = z.object({ |
| 29 | + config: LitNodeClientConfigSchema, |
| 30 | + connectedNodes: z.set(z.string()), |
| 31 | + serverKeys: z.record(z.string(), JsonHandshakeResponseSchema), |
| 32 | + ready: z.boolean(), |
| 33 | + subnetPubKey: z.string().nullable(), |
| 34 | + networkPubKey: z.string().nullable(), |
| 35 | + networkPubKeySet: z.string().nullable(), |
| 36 | + latestBlockhash: z.string().nullable(), |
| 37 | + |
| 38 | + // ========== Constructor ========== |
| 39 | + // ** IMPORTANT !! You have to create your constructor when implementing this class ** |
| 40 | + // constructor: z.function().args(LitNodeClientConfigSchema), |
| 41 | + |
| 42 | + // ========== Scoped Class Helpers ========== |
| 43 | + |
| 44 | + /** |
| 45 | + * |
| 46 | + * Set bootstrapUrls to match the network litNetwork unless it's set to custom |
| 47 | + * |
| 48 | + * @returns { void } |
| 49 | + * |
| 50 | + */ |
| 51 | + setCustomBootstrapUrls: z.function().returns(z.void()), |
| 52 | + /** |
| 53 | + * |
| 54 | + * we need to send jwt params iat (issued at) and exp (expiration) because the nodes may have different wall clock times, the nodes will verify that these params are withing a grace period |
| 55 | + * |
| 56 | + */ |
| 57 | + getJWTParams: z.function().returns( |
| 58 | + z.object({ |
| 59 | + iat: z.number(), |
| 60 | + exp: z.number(), |
| 61 | + }) |
| 62 | + ), |
| 63 | + /** |
| 64 | + * |
| 65 | + * Combine Shares from signature shares |
| 66 | + * @returns { string } final JWT (convert the sig to base64 and append to the jwt) |
| 67 | + * |
| 68 | + */ |
| 69 | + combineSharesAndGetJWT: z |
| 70 | + .function() |
| 71 | + .args(z.array(NodeBlsSigningShareSchema), z.string().optional()) |
| 72 | + .returns(z.promise(z.string())), |
| 73 | + /** |
| 74 | + * |
| 75 | + * Get different formats of access control conditions, eg. evm, sol, unified etc. |
| 76 | + * |
| 77 | + * @param { SupportedJsonRequests } params |
| 78 | + * |
| 79 | + * @returns { FormattedMultipleAccs } |
| 80 | + * |
| 81 | + */ |
| 82 | + getFormattedAccessControlConditions: z |
| 83 | + .function() |
| 84 | + .args(SupportedJsonRequestsSchema) |
| 85 | + .returns(FormattedMultipleAccsSchema), |
| 86 | + /** |
| 87 | + * |
| 88 | + * Get hash of access control conditions |
| 89 | + * |
| 90 | + * @param { MultipleAccessControlConditions } params |
| 91 | + * |
| 92 | + * @returns { Promise<ArrayBuffer | undefined> } |
| 93 | + * |
| 94 | + */ |
| 95 | + getHashedAccessControlConditions: z |
| 96 | + .function() |
| 97 | + .args(MultipleAccessControlConditionsSchema) |
| 98 | + .returns(z.promise(z.union([z.instanceof(ArrayBuffer), z.undefined()]))), |
| 99 | + |
| 100 | + // ========== Promise Handlers ========== |
| 101 | + |
| 102 | + /** |
| 103 | + * |
| 104 | + * Get and gather node promises |
| 105 | + * |
| 106 | + * @param { any } callback |
| 107 | + * |
| 108 | + * @returns { Array<Promise<any>> } |
| 109 | + * |
| 110 | + */ |
| 111 | + getNodePromises: z |
| 112 | + .function() |
| 113 | + .args(z.function().args(z.string()).returns(z.promise(z.any()))) // TODO improve |
| 114 | + .returns(z.array(z.promise(z.any()))), // TODO |
| 115 | + /** |
| 116 | + * Handle node promises |
| 117 | + * |
| 118 | + * @param { Array<Promise<T>> } nodePromises |
| 119 | + * |
| 120 | + * @param {string} requestId request Id used for logging |
| 121 | + * @param {number} minNodeCount The minimum number of nodes we need a successful response from to continue |
| 122 | + * @returns { Promise<SuccessNodePromises<T> | RejectedNodePromisesSchema> } |
| 123 | + * |
| 124 | + */ |
| 125 | + handleNodePromises: z |
| 126 | + .function() |
| 127 | + .args(z.array(z.promise(z.any())), z.string(), z.number()) |
| 128 | + .returns( |
| 129 | + z.promise( |
| 130 | + z.discriminatedUnion('success', [ |
| 131 | + SuccessNodePromisesSchema, |
| 132 | + RejectedNodePromisesSchema, |
| 133 | + ]) |
| 134 | + ) |
| 135 | + ), |
| 136 | + /** |
| 137 | + * |
| 138 | + * Throw node error |
| 139 | + * |
| 140 | + * @param { RejectedNodePromisesSchema } res |
| 141 | + * @param { string } requestId |
| 142 | + * |
| 143 | + * @returns { void } |
| 144 | + * |
| 145 | + */ |
| 146 | + _throwNodeError: z |
| 147 | + .function() |
| 148 | + .args(RejectedNodePromisesSchema, z.string()) |
| 149 | + .returns(z.never()), |
| 150 | + |
| 151 | + // ========== API Calls to Nodes ========== |
| 152 | + sendCommandToNode: z |
| 153 | + .function() |
| 154 | + .args(SendNodeCommandSchema) |
| 155 | + .returns(z.promise(z.any())), // TODO |
| 156 | + /** |
| 157 | + * |
| 158 | + * Handshake with SGX |
| 159 | + * |
| 160 | + * @param { HandshakeWithNode } params |
| 161 | + * |
| 162 | + * @returns { Promise<NodeCommandServerKeysResponse> } |
| 163 | + * |
| 164 | + */ |
| 165 | + handshakeWithNode: z |
| 166 | + .function() |
| 167 | + .args(HandshakeWithNodeSchema, z.string()) |
| 168 | + .returns(z.promise(NodeCommandServerKeysResponseSchema)), |
| 169 | + |
| 170 | + // ========== Scoped Business Logics ========== |
| 171 | + /** |
| 172 | + * |
| 173 | + * Execute JS on the nodes and combine and return any resulting signatures |
| 174 | + * |
| 175 | + * @param { ExecuteJsRequest } params |
| 176 | + * |
| 177 | + * @returns { ExecuteJsResponse } |
| 178 | + * |
| 179 | + */ |
| 180 | + executeJs: z |
| 181 | + .function() |
| 182 | + .args(JsonExecutionSdkParamsSchema) |
| 183 | + .returns(z.promise(z.union([ExecuteJsResponseSchema, z.undefined()]))), |
| 184 | + /** |
| 185 | + * |
| 186 | + * Request a signed JWT from the LIT network. Before calling this function, you must know the access control conditions for the item you wish to gain authorization for. |
| 187 | + * |
| 188 | + * @param { GetSignedTokenRequest } params |
| 189 | + * |
| 190 | + * @returns { Promise<string> } final JWT |
| 191 | + * |
| 192 | + */ |
| 193 | + getSignedToken: z |
| 194 | + .function() |
| 195 | + .args(GetSignedTokenRequestSchema) |
| 196 | + .returns(z.promise(z.union([z.string(), z.undefined()]))), |
| 197 | + /** |
| 198 | + * Encrypt data with Lit identity-based Timelock Encryption. |
| 199 | + * |
| 200 | + * @param params |
| 201 | + */ |
| 202 | + encrypt: z |
| 203 | + .function() |
| 204 | + .args(EncryptSdkParamsSchema) |
| 205 | + .returns(z.promise(EncryptResponseSchema)), |
| 206 | + /** |
| 207 | + * Decrypt data with Lit identity-based Timelock Encryption. |
| 208 | + * |
| 209 | + * @param params |
| 210 | + */ |
| 211 | + decrypt: z |
| 212 | + .function() |
| 213 | + .args(DecryptRequestSchema) |
| 214 | + .returns(z.promise(DecryptResponseSchema)), |
| 215 | + /** |
| 216 | + * |
| 217 | + * Connect to the LIT nodes |
| 218 | + * |
| 219 | + * @returns { Promise } A promise that resolves when the nodes are connected. |
| 220 | + * |
| 221 | + */ |
| 222 | + connect: z.function().returns(z.promise(z.any())), // TODO |
| 223 | + /** |
| 224 | + * Generates a session capability object |
| 225 | + * |
| 226 | + * @param litResources An array of ILitResource to be processed. |
| 227 | + * @returns A Promise resolving to an ISessionCapabilityObject. |
| 228 | + */ |
| 229 | + generateSessionCapabilityObjectWithWildcards: z |
| 230 | + .function() |
| 231 | + .args(z.array(ILitResourceSchema)) |
| 232 | + .returns(z.promise(ISessionCapabilityObjectSchema)), |
| 233 | +}); |
0 commit comments