@@ -3,11 +3,13 @@ import {
33 NoWalletException ,
44 ParamsMissingError ,
55} from '@lit-protocol/constants' ;
6- import { TokenInfo } from '@lit-protocol/types' ;
6+ import { DerivedAddresses } from '@lit-protocol/types' ;
77import { bech32 } from 'bech32' ;
88import { createHash } from 'crypto' ;
99import { Contract , ethers } from 'ethers' ;
1010import { computeAddress } from 'ethers/lib/utils' ;
11+ import { z } from 'zod' ;
12+ import { fromError , isZodErrorLike } from 'zod-validation-error' ;
1113
1214/**
1315 * Converts a public key between compressed and uncompressed formats.
@@ -142,23 +144,30 @@ function deriveCosmosAddress(
142144 return bech32 . encode ( prefix , bech32 . toWords ( ripemd160Hash ) ) ;
143145}
144146
145- type DerivedAddressesParams =
146- | {
147- publicKey : string ;
148- pkpTokenId ?: never ;
149- pkpContractAddress ?: never ;
150- defaultRPCUrl ?: never ;
151- options ?: never ;
152- }
153- | {
154- publicKey ?: never ;
155- pkpTokenId : string ;
156- pkpContractAddress : string ;
157- defaultRPCUrl : string ;
158- options ?: {
159- cacheContractCall ?: boolean ;
160- } ;
161- } ;
147+ const PublicKeyParamsSchema = z . object ( {
148+ publicKey : z . string ( ) ,
149+ pkpTokenId : z . undefined ( ) ,
150+ pkpContractAddress : z . undefined ( ) ,
151+ defaultRPCUrl : z . undefined ( ) ,
152+ options : z . undefined ( ) ,
153+ } ) ;
154+ const PKPTokenParamsSchema = z . object ( {
155+ publicKey : z . undefined ( ) ,
156+ pkpTokenId : z . string ( ) ,
157+ pkpContractAddress : z . string ( ) ,
158+ defaultRPCUrl : z . string ( ) ,
159+ options : z
160+ . object ( {
161+ cacheContractCall : z . boolean ( ) . optional ( ) ,
162+ } )
163+ . optional ( ) ,
164+ } ) ;
165+ const DerivedAddressesParamsSchema = z . union ( [
166+ PublicKeyParamsSchema ,
167+ PKPTokenParamsSchema ,
168+ ] ) ;
169+
170+ type DerivedAddressesParams = z . infer < typeof DerivedAddressesParamsSchema > ;
162171
163172/**
164173 * Derives multiple blockchain addresses (Ethereum, Bitcoin, and Cosmos) from a given uncompressed eth public key
@@ -173,7 +182,6 @@ type DerivedAddressesParams =
173182 * @param params.options.cacheContractCall - Whether to cache the contract call result in local storage (default: false).
174183 *
175184 * @returns A Promise that resolves to an object containing token information:
176- * @property {string } tokenId - The PKP token ID.
177185 * @property {string } publicKey - The Ethereum public key as a hex string.
178186 * @property {Buffer } publicKeyBuffer - The buffer representation of the public key.
179187 * @property {string } ethAddress - The derived Ethereum address.
@@ -185,28 +193,35 @@ type DerivedAddressesParams =
185193 * @throws {ParamsMissingError } If neither publicKey nor pkpTokenId is provided.
186194 * @throws {MultiError } If any of the derived addresses (btcAddress, ethAddress, cosmosAddress) are undefined.
187195 */
188- export const derivedAddresses = async ( {
189- publicKey,
190- pkpTokenId,
191- pkpContractAddress,
192- defaultRPCUrl,
193- options = {
194- cacheContractCall : false ,
195- } ,
196- } : DerivedAddressesParams ) : Promise < TokenInfo | any > => {
197- // one of the two must be provided
198- if ( ! publicKey && ! pkpTokenId ) {
196+ export const derivedAddresses = async (
197+ params : DerivedAddressesParams
198+ ) : Promise < DerivedAddresses > => {
199+ let _params : DerivedAddressesParams ;
200+ try {
201+ _params = DerivedAddressesParamsSchema . parse ( params ) ;
202+ } catch ( e ) {
199203 throw new ParamsMissingError (
200204 {
201205 info : {
202- publicKey,
203- pkpTokenId,
206+ publicKey : params . publicKey ,
207+ pkpTokenId : params . pkpTokenId ,
204208 } ,
209+ cause : isZodErrorLike ( e ) ? fromError ( e ) : e ,
205210 } ,
206211 'publicKey or pkpTokenId must be provided'
207212 ) ;
208213 }
209214
215+ let {
216+ publicKey,
217+ pkpTokenId,
218+ pkpContractAddress,
219+ defaultRPCUrl,
220+ options = {
221+ cacheContractCall : false ,
222+ } ,
223+ } = _params ;
224+
210225 // if pkpTokenId is provided, we must get the public key from it (in cache or from the contract)
211226 let isNewPKP = false ;
212227 if ( pkpTokenId ) {
@@ -347,7 +362,6 @@ export const derivedAddresses = async ({
347362 }
348363
349364 return {
350- tokenId : pkpTokenId ,
351365 publicKey : `0x${ publicKey } ` ,
352366 publicKeyBuffer : pubkeyBuffer ,
353367 ethAddress,
0 commit comments