Skip to content

Commit 42dd84f

Browse files
committed
feat: a lot of new schemas and types derivation from them
1 parent 5fdd978 commit 42dd84f

File tree

11 files changed

+863
-680
lines changed

11 files changed

+863
-680
lines changed

packages/constants/src/lib/constants/constants.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -954,7 +954,7 @@ export const LIT_COSMOS_CHAINS: { [key: string]: any } = {
954954
chainId: 'cosmoshub-4',
955955
rpcUrls: ['https://lcd-cosmoshub.keplr.app'] as const,
956956
blockExplorerUrls: ['https://atomscan.com/'] as const,
957-
vmType: 'CVM',
957+
vmType: 'CVM', // TODO replace all these with the constant
958958
},
959959
kyve: {
960960
name: 'Kyve',

packages/contracts-sdk/src/lib/contracts-sdk.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1917,7 +1917,10 @@ https://developer.litprotocol.com/v3/sdk/wallets/auth-methods/#auth-method-scope
19171917
publicKey: pubKey,
19181918
});
19191919

1920-
arr.push(addrs);
1920+
arr.push({
1921+
tokenId,
1922+
...addrs,
1923+
});
19211924
}
19221925

19231926
return arr;

packages/core/src/lib/lit-core.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1190,7 +1190,7 @@ export class LitCore {
11901190
nodePromises: Promise<T>[],
11911191
requestId: string,
11921192
minNodeCount: number
1193-
): Promise<SuccessNodePromises<T> | RejectedNodePromises> => {
1193+
): Promise<SuccessNodePromises | RejectedNodePromises> => {
11941194
async function waitForNSuccessesWithErrors<T>(
11951195
promises: Promise<T>[],
11961196
n: number

packages/misc/src/lib/addresses.spec.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ describe('adddresses', () => {
99
publicKey: COMPRESSED_PUBLIC_KEY,
1010
});
1111
expect(derivedAddress).toEqual({
12-
tokenId: undefined,
1312
publicKey: `0x${COMPRESSED_PUBLIC_KEY}`,
1413
publicKeyBuffer: Buffer.from(COMPRESSED_PUBLIC_KEY, 'hex'),
1514
ethAddress: '0x7206cB69380ee83c4Ef13f05713e814F3e4dee0f',

packages/misc/src/lib/addresses.ts

Lines changed: 47 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -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';
77
import { bech32 } from 'bech32';
88
import { createHash } from 'crypto';
99
import { Contract, ethers } from 'ethers';
1010
import { 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

Comments
 (0)