|
| 1 | +import { getChildLogger } from '@lit-protocol/logger'; |
| 2 | +import { |
| 3 | + PricingContextSchema, |
| 4 | + issueSessionFromContext, |
| 5 | +} from '@lit-protocol/networks'; |
| 6 | +import { HexPrefixedSchema } from '@lit-protocol/schemas'; |
| 7 | +import { AuthSig, SessionKeyPair, SessionSigsMap } from '@lit-protocol/types'; |
| 8 | +import { z } from 'zod'; |
| 9 | +import type { LitClient } from '@lit-protocol/lit-client'; |
| 10 | +import type { AuthManagerParams } from '../auth-manager'; |
| 11 | +import { getPkpAuthContextAdapter } from './getPkpAuthContextAdapter'; |
| 12 | +import { getPkpAuthContextFromPreGeneratedAdapter } from './getPkpAuthContextFromPreGeneratedAdapter'; |
| 13 | + |
| 14 | +const _logger = getChildLogger({ |
| 15 | + module: 'getPkpSessionSigsAdapter', |
| 16 | +}); |
| 17 | + |
| 18 | +type SupportedProduct = |
| 19 | + | 'DECRYPTION' |
| 20 | + | 'SIGN' |
| 21 | + | 'LIT_ACTION' |
| 22 | + | 'SIGN_SESSION_KEY'; |
| 23 | + |
| 24 | +export type PkpSessionSigsProduct = SupportedProduct; |
| 25 | + |
| 26 | +const BigIntLikeSchema = z |
| 27 | + .union([z.bigint(), z.number().finite(), z.string()]) |
| 28 | + .transform((value) => { |
| 29 | + if (typeof value === 'bigint') { |
| 30 | + return value; |
| 31 | + } |
| 32 | + |
| 33 | + if (typeof value === 'number') { |
| 34 | + return BigInt(Math.trunc(value)); |
| 35 | + } |
| 36 | + |
| 37 | + const normalized = value.trim(); |
| 38 | + |
| 39 | + if (normalized.length === 0) { |
| 40 | + throw new Error('Cannot convert empty string to bigint'); |
| 41 | + } |
| 42 | + |
| 43 | + return BigInt(normalized); |
| 44 | + }); |
| 45 | + |
| 46 | +const RespondingNodePricesSchema = z.array( |
| 47 | + z.object({ |
| 48 | + url: z.string(), |
| 49 | + prices: z.array(BigIntLikeSchema).transform((prices) => { |
| 50 | + return prices.map((price) => { |
| 51 | + if (price < 0n) { |
| 52 | + throw new Error('Node price must be non-negative'); |
| 53 | + } |
| 54 | + return price; |
| 55 | + }); |
| 56 | + }), |
| 57 | + }) |
| 58 | +); |
| 59 | + |
| 60 | +const HandshakeThresholdSchema = z |
| 61 | + .union([z.number(), z.bigint(), z.string()]) |
| 62 | + .transform((value) => { |
| 63 | + if (typeof value === 'number') { |
| 64 | + return Math.trunc(value); |
| 65 | + } |
| 66 | + |
| 67 | + if (typeof value === 'bigint') { |
| 68 | + return Number(value); |
| 69 | + } |
| 70 | + |
| 71 | + const parsed = Number(value); |
| 72 | + |
| 73 | + if (!Number.isFinite(parsed)) { |
| 74 | + throw new Error('Invalid threshold value provided by handshake result'); |
| 75 | + } |
| 76 | + |
| 77 | + return Math.trunc(parsed); |
| 78 | + }) |
| 79 | + .refine((value) => value > 0, { |
| 80 | + message: 'Threshold must be a positive integer', |
| 81 | + }); |
| 82 | + |
| 83 | +export const getPkpSessionSigsAdapter = async ( |
| 84 | + _: AuthManagerParams, |
| 85 | + params: { |
| 86 | + pkpPublicKey: z.infer<typeof HexPrefixedSchema>; |
| 87 | + litClient: LitClient; |
| 88 | + sessionKeyPair: SessionKeyPair; |
| 89 | + delegationAuthSig: AuthSig; |
| 90 | + product?: SupportedProduct; |
| 91 | + } |
| 92 | +): Promise<SessionSigsMap> => { |
| 93 | + const { |
| 94 | + pkpPublicKey, |
| 95 | + sessionKeyPair, |
| 96 | + delegationAuthSig, |
| 97 | + litClient, |
| 98 | + product = 'LIT_ACTION', |
| 99 | + } = params; |
| 100 | + |
| 101 | + _logger.info( |
| 102 | + { |
| 103 | + pkpPublicKey, |
| 104 | + hasSessionKeyPair: !!sessionKeyPair, |
| 105 | + hasDelegationAuthSig: !!delegationAuthSig, |
| 106 | + product, |
| 107 | + }, |
| 108 | + 'getPkpSessionSigsAdapter: Preparing to generate session signatures' |
| 109 | + ); |
| 110 | + |
| 111 | + const litClientCtx = await litClient.getContext(); |
| 112 | + |
| 113 | + const latestConnectionInfo = litClientCtx?.latestConnectionInfo; |
| 114 | + const handshakeResult = litClientCtx?.handshakeResult; |
| 115 | + |
| 116 | + if (!latestConnectionInfo || !handshakeResult) { |
| 117 | + throw new Error( |
| 118 | + 'Missing latest connection info or handshake result from Lit client context' |
| 119 | + ); |
| 120 | + } |
| 121 | + |
| 122 | + const nodePrices = latestConnectionInfo.priceFeedInfo?.networkPrices; |
| 123 | + |
| 124 | + if (!nodePrices?.length) { |
| 125 | + throw new Error( |
| 126 | + 'No node pricing information available from Lit client context' |
| 127 | + ); |
| 128 | + } |
| 129 | + |
| 130 | + const serverKeys = handshakeResult.serverKeys ?? {}; |
| 131 | + const respondingUrlSet = new Set(Object.keys(serverKeys)); |
| 132 | + |
| 133 | + const respondingNodePrices = nodePrices.filter((item) => |
| 134 | + respondingUrlSet.has(item.url) |
| 135 | + ); |
| 136 | + |
| 137 | + const threshold = HandshakeThresholdSchema.parse(handshakeResult.threshold); |
| 138 | + |
| 139 | + if (respondingNodePrices.length < threshold) { |
| 140 | + throw new Error( |
| 141 | + `Not enough handshake nodes to satisfy threshold. Threshold: ${threshold}, responding nodes: ${respondingNodePrices.length}` |
| 142 | + ); |
| 143 | + } |
| 144 | + |
| 145 | + const pricingNodePrices = |
| 146 | + RespondingNodePricesSchema.parse(respondingNodePrices); |
| 147 | + |
| 148 | + const userMaxPriceValue = |
| 149 | + typeof litClientCtx.getUserMaxPrice === 'function' |
| 150 | + ? litClientCtx.getUserMaxPrice({ product }) |
| 151 | + : undefined; |
| 152 | + |
| 153 | + const userMaxPrice = |
| 154 | + userMaxPriceValue !== undefined |
| 155 | + ? BigIntLikeSchema.parse(userMaxPriceValue) |
| 156 | + : undefined; |
| 157 | + |
| 158 | + const pricingContextInput: Parameters<typeof PricingContextSchema.parse>[0] = |
| 159 | + { |
| 160 | + product, |
| 161 | + nodePrices: pricingNodePrices, |
| 162 | + threshold, |
| 163 | + ...(userMaxPrice !== undefined ? { userMaxPrice } : {}), |
| 164 | + }; |
| 165 | + |
| 166 | + const pricingContext = PricingContextSchema.parse(pricingContextInput); |
| 167 | + |
| 168 | + let authContext: |
| 169 | + | Awaited<ReturnType<typeof getPkpAuthContextAdapter>> |
| 170 | + | Awaited<ReturnType<typeof getPkpAuthContextFromPreGeneratedAdapter>>; |
| 171 | + |
| 172 | + // if (authConfig && authData) { |
| 173 | + // authContext = await getPkpAuthContextAdapter(upstreamParams, { |
| 174 | + // authData, |
| 175 | + // pkpPublicKey, |
| 176 | + // authConfig, |
| 177 | + // cache, |
| 178 | + // sessionKeyPair, |
| 179 | + // delegationAuthSig, |
| 180 | + // litClient: { |
| 181 | + // getContext: async () => litClientCtx, |
| 182 | + // }, |
| 183 | + // }); |
| 184 | + // } else { |
| 185 | + if (!sessionKeyPair || !delegationAuthSig) { |
| 186 | + throw new Error( |
| 187 | + 'sessionKeyPair and delegationAuthSig are required when authConfig or authData are not provided' |
| 188 | + ); |
| 189 | + } |
| 190 | + |
| 191 | + _logger.info( |
| 192 | + { |
| 193 | + pkpPublicKey, |
| 194 | + }, |
| 195 | + 'getPkpSessionSigsAdapter: Falling back to pre-generated auth context helper' |
| 196 | + ); |
| 197 | + |
| 198 | + authContext = await getPkpAuthContextFromPreGeneratedAdapter({ |
| 199 | + pkpPublicKey, |
| 200 | + sessionKeyPair, |
| 201 | + delegationAuthSig, |
| 202 | + // ...(authData ? { authData } : {}), |
| 203 | + }); |
| 204 | + // } |
| 205 | + |
| 206 | + const sessionSigs = await issueSessionFromContext({ |
| 207 | + authContext, |
| 208 | + pricingContext, |
| 209 | + delegationAuthSig: delegationAuthSig, |
| 210 | + }); |
| 211 | + |
| 212 | + _logger.info( |
| 213 | + { |
| 214 | + pkpPublicKey, |
| 215 | + product, |
| 216 | + respondingNodeCount: respondingNodePrices.length, |
| 217 | + }, |
| 218 | + 'getPkpSessionSigsAdapter: Session signatures generated successfully' |
| 219 | + ); |
| 220 | + |
| 221 | + return sessionSigs; |
| 222 | +}; |
0 commit comments