Skip to content

Commit cfc3c34

Browse files
committed
feat: remove misc package with a lot of zod schemas, helper functions moving and code refactors
1 parent 527e07a commit cfc3c34

File tree

80 files changed

+1640
-2753
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

80 files changed

+1640
-2753
lines changed

README.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,6 @@ If you're a tech-savvy user and wish to utilize only specific submodules that ou
6767
| [@lit-protocol/crypto](https://github.com/LIT-Protocol/js-sdk/tree/master/packages/crypto) | ![crypto](https://img.shields.io/badge/-universal-8A6496 'crypto') | <a target="_blank" href="https://www.npmjs.com/package/@lit-protocol/crypto"><img src="https://img.shields.io/npm/v/@lit-protocol/crypto"/></a> |
6868
| [@lit-protocol/encryption](https://github.com/LIT-Protocol/js-sdk/tree/master/packages/encryption) | ![encryption](https://img.shields.io/badge/-universal-8A6496 'encryption') | <a target="_blank" href="https://www.npmjs.com/package/@lit-protocol/encryption"><img src="https://img.shields.io/npm/v/@lit-protocol/encryption"/></a> |
6969
| [@lit-protocol/event-listener](https://github.com/LIT-Protocol/js-sdk/tree/master/packages/event-listener) | ![event-listener](https://img.shields.io/badge/-universal-8A6496 'event-listener') | <a target="_blank" href="https://www.npmjs.com/package/@lit-protocol/event-listener"><img src="https://img.shields.io/npm/v/@lit-protocol/event-listener"/></a> |
70-
| [@lit-protocol/misc](https://github.com/LIT-Protocol/js-sdk/tree/master/packages/misc) | ![misc](https://img.shields.io/badge/-universal-8A6496 'misc') | <a target="_blank" href="https://www.npmjs.com/package/@lit-protocol/misc"><img src="https://img.shields.io/npm/v/@lit-protocol/misc"/></a> |
7170
| [@lit-protocol/nacl](https://github.com/LIT-Protocol/js-sdk/tree/master/packages/nacl) | ![nacl](https://img.shields.io/badge/-universal-8A6496 'nacl') | <a target="_blank" href="https://www.npmjs.com/package/@lit-protocol/nacl"><img src="https://img.shields.io/npm/v/@lit-protocol/nacl"/></a> |
7271
| [@lit-protocol/networks](https://github.com/LIT-Protocol/js-sdk/tree/master/packages/networks) | ![networks](https://img.shields.io/badge/-universal-8A6496 'networks') | <a target="_blank" href="https://www.npmjs.com/package/@lit-protocol/networks"><img src="https://img.shields.io/npm/v/@lit-protocol/networks"/></a> |
7372
| [@lit-protocol/pkp-base](https://github.com/LIT-Protocol/js-sdk/tree/master/packages/pkp-base) | ![pkp-base](https://img.shields.io/badge/-universal-8A6496 'pkp-base') | <a target="_blank" href="https://www.npmjs.com/package/@lit-protocol/pkp-base"><img src="https://img.shields.io/npm/v/@lit-protocol/pkp-base"/></a> |

local-tests/package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,6 @@
8888
"@solana/web3.js": "^1.95.3",
8989
"bech32": "^2.0.0",
9090
"pako": "^2.1.0",
91-
"@lit-protocol/misc": "8.0.0-alpha.0",
9291
"@lit-protocol/lit-node-client": "8.0.0-alpha.0",
9392
"@lit-protocol/auth": "8.0.0-alpha.0",
9493
"@lit-protocol/contracts": "^0.0.71"

local-tests/setup/tinny-person.ts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,8 @@
1-
import {
2-
AuthSig,
3-
generateAuthSig,
4-
createSiweMessage,
5-
} from '@lit-protocol/auth-helpers';
1+
import { generateAuthSig, createSiweMessage } from '@lit-protocol/auth-helpers';
62
import { LitContracts } from '@lit-protocol/contracts-sdk';
73
import {
84
AuthMethod,
5+
AuthSig,
96
BaseSiweMessage,
107
LitContractContext,
118
} from '@lit-protocol/types';

packages/access-control-conditions/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
export * from './lib/booleanExpressions';
12
export * from './lib/canonicalFormatter';
23
export * from './lib/hashing';
34
export * from './lib/humanizer';

packages/misc/src/lib/utils.ts renamed to packages/access-control-conditions/src/lib/booleanExpressions.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
1+
import { OperatorAccSchema } from '@lit-protocol/access-control-conditions-schemas';
12
import {
23
AccessControlConditions,
34
EvmContractConditions,
45
SolRpcConditions,
56
UnifiedAccessControlConditions,
67
} from '@lit-protocol/types';
78

8-
export function isTokenOperator(token: any): boolean {
9-
const OPERATORS = ['and', 'or']; // Only permissible boolean operators on the nodes
10-
return token.hasOwnProperty('operator') && OPERATORS.includes(token.operator);
9+
export function isTokenOperator(token: unknown): boolean {
10+
return OperatorAccSchema.safeParse(token).success;
1111
}
1212

1313
export function isValidBooleanExpression(

packages/access-control-conditions/src/lib/canonicalFormatter.spec.ts

Lines changed: 49 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1+
import { InvalidAccessControlConditions } from '@lit-protocol/constants';
12
import { ConditionItem } from '@lit-protocol/types';
3+
24
import {
35
canonicalUnifiedAccessControlConditionFormatter,
46
canonicalSolRpcConditionFormatter,
@@ -11,7 +13,7 @@ import {
1113
// ---------- Test Cases ----------
1214
describe('canonicalFormatter.ts', () => {
1315
it('should format canonical unified access control (ETH + SOLANA Wallet Addresses with "AND" operator)', async () => {
14-
const EXPECTED_INPUT: Array<ConditionItem> = [
16+
const EXPECTED_INPUT: ConditionItem[] = [
1517
{
1618
conditionType: 'evmBasic',
1719
contractAddress: '',
@@ -74,10 +76,9 @@ describe('canonicalFormatter.ts', () => {
7476
},
7577
];
7678

77-
const test =
78-
canonicalUnifiedAccessControlConditionFormatter(EXPECTED_INPUT);
79-
80-
expect(test).toStrictEqual(EXPECTED_OUTPUT);
79+
expect(
80+
canonicalUnifiedAccessControlConditionFormatter(EXPECTED_INPUT)
81+
).toStrictEqual(EXPECTED_OUTPUT);
8182
});
8283

8384
it('should FAIL to format canonical unified access control if key "conditionType" doesnt exist', async () => {
@@ -101,101 +102,78 @@ describe('canonicalFormatter.ts', () => {
101102
});
102103

103104
it('should FAIL to format canonical unified access control (key: foo, value: bar)', async () => {
104-
console.log = jest.fn();
105-
106-
const MOCK_ACCS_UNKNOWN_KEY: any = [
107-
{
108-
foo: 'bar',
109-
},
110-
{
111-
conditionType: 'evmBasic',
112-
contractAddress: '',
113-
standardContractType: '',
114-
chain: 'ethereum',
115-
method: '',
116-
parameters: [':userAddress'],
117-
returnValueTest: {
118-
comparator: '=',
119-
value: '0x3B5dD260598B7579A0b015A1F3BBF322aDC499A2',
105+
expect(() =>
106+
canonicalUnifiedAccessControlConditionFormatter([
107+
{
108+
// @ts-expect-error we are testing
109+
foo: 'bar',
120110
},
121-
},
122-
];
123-
124-
try {
125-
test = canonicalUnifiedAccessControlConditionFormatter(
126-
MOCK_ACCS_UNKNOWN_KEY
127-
);
128-
} catch (e) {
129-
console.log(e);
130-
}
131-
132-
expect((console.log as any).mock.calls[0][0].name).toBe(
133-
'InvalidAccessControlConditions'
134-
);
111+
{
112+
conditionType: 'evmBasic',
113+
contractAddress: '',
114+
standardContractType: '',
115+
chain: 'ethereum',
116+
method: '',
117+
parameters: [':userAddress'],
118+
returnValueTest: {
119+
comparator: '=',
120+
value: '0x3B5dD260598B7579A0b015A1F3BBF322aDC499A2',
121+
},
122+
},
123+
])
124+
).toThrow(InvalidAccessControlConditions);
135125
});
136126

137127
it('should throw error when format canonical sol rpc condition', async () => {
138-
console.log = jest.fn();
139-
140-
const MOCK_ACCS_UNKNOWN_KEY: any = [
141-
{
142-
foo: 'bar',
143-
},
144-
{
145-
conditionType: 'evmBasic',
146-
contractAddress: '',
147-
standardContractType: '',
148-
chain: 'ethereum',
149-
method: '',
150-
parameters: [':userAddress'],
151-
returnValueTest: {
152-
comparator: '=',
153-
value: '0x3B5dD260598B7579A0b015A1F3BBF322aDC499A2',
128+
expect(() =>
129+
canonicalSolRpcConditionFormatter([
130+
{
131+
// @ts-expect-error we are testing
132+
foo: 'bar',
154133
},
155-
},
156-
];
157-
158-
try {
159-
test = canonicalSolRpcConditionFormatter(MOCK_ACCS_UNKNOWN_KEY);
160-
} catch (e) {
161-
console.log(e);
162-
}
163-
164-
expect((console.log as any).mock.calls[0][0].name).toBe(
165-
'InvalidAccessControlConditions'
166-
);
134+
{
135+
conditionType: 'evmBasic',
136+
contractAddress: '',
137+
standardContractType: '',
138+
chain: 'ethereum',
139+
method: '',
140+
parameters: [':userAddress'],
141+
returnValueTest: {
142+
comparator: '=',
143+
value: '0x3B5dD260598B7579A0b015A1F3BBF322aDC499A2',
144+
},
145+
},
146+
])
147+
).toThrow(InvalidAccessControlConditions);
167148
});
168149

169150
it('should call "canonicalAccessControlConditionFormatter" in node.js', () => {
170-
const params: any = [];
151+
const params = [] as never[];
171152

172153
const OUTPUT = canonicalAccessControlConditionFormatter(params);
173154

174155
expect(OUTPUT.length).toBe(0);
175156
});
176157

177158
it('should call canonicalEVMContractConditionFormatter in node.js', () => {
178-
const params: any = [];
159+
const params = [] as never[];
179160

180161
const OUTPUT = canonicalEVMContractConditionFormatter(params);
181162

182163
expect(OUTPUT.length).toBe(0);
183164
});
184165

185166
it('should call canonicalCosmosConditionFormatter in node.js', () => {
186-
const params: any = [];
167+
const params = [] as never[];
187168

188169
const OUTPUT = canonicalCosmosConditionFormatter(params);
189170

190171
expect(OUTPUT.length).toBe(0);
191172
});
192173

193174
it('should call canonicalResourceIdFormatter in node.js', () => {
194-
const params: any = [];
195-
196-
const OUTPUT = canonicalResourceIdFormatter(params);
197-
198-
// const res = (console.log as any).mock.calls[0][0];
175+
// @ts-expect-error we are testing
176+
const OUTPUT = canonicalResourceIdFormatter({});
199177

200178
expect(OUTPUT.baseUrl).toBe(undefined);
201179
});

packages/access-control-conditions/src/lib/canonicalFormatter.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ export const canonicalUnifiedAccessControlConditionFormatter = (
142142
* @returns { any[] | OperatorAcc | AccsSOLV2Params | any }
143143
*/
144144
export const canonicalSolRpcConditionFormatter = (
145-
cond: ConditionItem,
145+
cond: ConditionItem | ConditionItem[],
146146
requireV2Conditions: boolean = false
147147
): any[] | OperatorAcc | ConditionItem | AccsSOLV2Params | any => {
148148
// -- if is array
@@ -186,7 +186,7 @@ export const canonicalSolRpcConditionFormatter = (
186186
cond,
187187
},
188188
},
189-
'Solana RPC Conditions have changed and there are some new fields you must include in your condition. Check the docs here: https://developer.litprotocol.com/AccessControlConditions/solRpcConditions'
189+
'Solana RPC Conditions have changed and there are some new fields you must include in your condition. Check the docs here: https://developer.litprotocol.com/AccessControlConditions/solRpcConditions'
190190
);
191191
}
192192

@@ -415,7 +415,7 @@ export const canonicalEVMContractConditionFormatter = (
415415
* @returns
416416
*/
417417
export const canonicalCosmosConditionFormatter = (
418-
cond: ConditionItem
418+
cond: ConditionItem | ConditionItem[]
419419
): any[] | OperatorAcc | AccsCOSMOSParams | any => {
420420
// -- if it's an array
421421
if (Array.isArray(cond)) {

packages/access-control-conditions/src/lib/humanizer.spec.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
1-
import * as humanizer from './humanizer';
2-
import { humanizeAccessControlConditions } from './humanizer';
31
import {
42
AccsCOSMOSParams,
53
AccsEVMParams,
64
AccsSOLV2Params,
75
UnifiedAccessControlConditions,
86
} from '@lit-protocol/types';
97

8+
import * as humanizer from './humanizer';
9+
import { humanizeAccessControlConditions } from './humanizer';
10+
1011
// ---------- Test Cases ----------
1112
describe('humanizer.ts', () => {
1213
it('should format sol', () => {
@@ -60,7 +61,7 @@ describe('humanizer.ts', () => {
6061
expect(OUTPUT).toBe('at most');
6162
});
6263
it('should humanizeEvmBasicAccessControlConditions', async () => {
63-
const INPUT: Array<AccsEVMParams> = [
64+
const INPUT: AccsEVMParams[] = [
6465
{
6566
contractAddress: '0x7C7757a9675f06F3BE4618bB68732c4aB25D2e88',
6667
functionName: 'balanceOf',
@@ -108,7 +109,7 @@ describe('humanizer.ts', () => {
108109
});
109110

110111
it('should humanizeSolRpcConditions', async () => {
111-
const INPUT: Array<AccsSOLV2Params> = [
112+
const INPUT: AccsSOLV2Params[] = [
112113
{
113114
method: 'getTokenAccountBalance',
114115
params: ['tn2WEWk4Kqj157XsSdmBBcjWumVhkyJECXCKPq9ReL9'],
@@ -134,7 +135,7 @@ describe('humanizer.ts', () => {
134135
});
135136

136137
it('should humanizeCosmosConditions', async () => {
137-
const INPUT: Array<AccsCOSMOSParams> = [
138+
const INPUT: AccsCOSMOSParams[] = [
138139
{
139140
conditionType: 'cosmos',
140141
path: '/cosmos/bank/v1beta1/balances/:userAddress',

packages/access-control-conditions/src/lib/humanizer.ts

Lines changed: 38 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,13 @@
1+
import { Contract } from '@ethersproject/contracts';
2+
import { JsonRpcProvider } from '@ethersproject/providers';
13
import { formatEther, formatUnits } from 'ethers/lib/utils';
24
import { pino } from 'pino';
35

4-
import { InvalidUnifiedConditionType } from '@lit-protocol/constants';
5-
import { decimalPlaces } from '@lit-protocol/misc';
6+
import {
7+
LIT_CHAINS,
8+
LitEVMChainKeys,
9+
InvalidUnifiedConditionType,
10+
} from '@lit-protocol/constants';
611
import {
712
AccessControlConditions,
813
AccsCOSMOSParams,
@@ -12,8 +17,38 @@ import {
1217
UnifiedAccessControlConditions,
1318
} from '@lit-protocol/types';
1419

20+
import ABI_ERC20 from './abis/ERC20.json';
21+
1522
const logger = pino({ level: 'info', name: 'humanizer' });
1623

24+
/**
25+
*
26+
* Get the number of decimal places in a token
27+
*
28+
* @property { string } contractAddress The token contract address
29+
* @property { LitEVMChainKeys } chain The chain on which the token is deployed
30+
*
31+
* @returns { number } The number of decimal places in the token
32+
*/
33+
export const decimalPlaces = async ({
34+
contractAddress,
35+
chain,
36+
}: {
37+
contractAddress: string;
38+
chain: LitEVMChainKeys;
39+
}): Promise<number> => {
40+
const rpcUrl = LIT_CHAINS[chain].rpcUrls[0] as string;
41+
42+
const web3 = new JsonRpcProvider({
43+
url: rpcUrl,
44+
skipFetchSetup: true,
45+
});
46+
47+
const contract = new Contract(contractAddress, ABI_ERC20.abi, web3); // TODO drop the full ABI and just define "decimals"
48+
49+
return await contract['decimals']();
50+
};
51+
1752
/**
1853
*
1954
* Format SOL number using Ether Units
@@ -61,7 +96,7 @@ export const humanizeComparator = (comparator: string): string | undefined => {
6196
const selected: string | undefined = list[comparator];
6297

6398
if (!selected) {
64-
logger.info(`Unregonized comparator ${comparator}`);
99+
logger.info(`Unrecognized comparator ${comparator}`);
65100
return;
66101
}
67102

0 commit comments

Comments
 (0)