@cofhe/sdk 0.0.0-alpha-20260409113701

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (132) hide show
  1. package/CHANGELOG.md +146 -0
  2. package/adapters/ethers5.test.ts +174 -0
  3. package/adapters/ethers5.ts +36 -0
  4. package/adapters/ethers6.test.ts +169 -0
  5. package/adapters/ethers6.ts +36 -0
  6. package/adapters/hardhat-node.ts +167 -0
  7. package/adapters/hardhat.hh2.test.ts +159 -0
  8. package/adapters/hardhat.ts +36 -0
  9. package/adapters/index.test.ts +20 -0
  10. package/adapters/index.ts +5 -0
  11. package/adapters/smartWallet.ts +99 -0
  12. package/adapters/test-utils.ts +53 -0
  13. package/adapters/types.ts +6 -0
  14. package/adapters/wagmi.test.ts +156 -0
  15. package/adapters/wagmi.ts +17 -0
  16. package/chains/chains/arbSepolia.ts +14 -0
  17. package/chains/chains/baseSepolia.ts +14 -0
  18. package/chains/chains/hardhat.ts +15 -0
  19. package/chains/chains/localcofhe.ts +14 -0
  20. package/chains/chains/sepolia.ts +14 -0
  21. package/chains/chains.test.ts +50 -0
  22. package/chains/defineChain.ts +18 -0
  23. package/chains/index.ts +35 -0
  24. package/chains/types.ts +32 -0
  25. package/core/baseBuilder.ts +119 -0
  26. package/core/client.test.ts +429 -0
  27. package/core/client.ts +341 -0
  28. package/core/clientTypes.ts +119 -0
  29. package/core/config.test.ts +242 -0
  30. package/core/config.ts +225 -0
  31. package/core/consts.ts +22 -0
  32. package/core/decrypt/MockThresholdNetworkAbi.ts +179 -0
  33. package/core/decrypt/cofheMocksDecryptForTx.ts +84 -0
  34. package/core/decrypt/cofheMocksDecryptForView.ts +48 -0
  35. package/core/decrypt/decryptForTxBuilder.ts +359 -0
  36. package/core/decrypt/decryptForViewBuilder.ts +332 -0
  37. package/core/decrypt/decryptUtils.ts +28 -0
  38. package/core/decrypt/pollCallbacks.test.ts +194 -0
  39. package/core/decrypt/polling.ts +14 -0
  40. package/core/decrypt/tnDecryptUtils.ts +65 -0
  41. package/core/decrypt/tnDecryptV1.ts +171 -0
  42. package/core/decrypt/tnDecryptV2.ts +365 -0
  43. package/core/decrypt/tnSealOutputV1.ts +59 -0
  44. package/core/decrypt/tnSealOutputV2.ts +324 -0
  45. package/core/decrypt/verifyDecryptResult.ts +52 -0
  46. package/core/encrypt/MockZkVerifierAbi.ts +106 -0
  47. package/core/encrypt/cofheMocksZkVerifySign.ts +281 -0
  48. package/core/encrypt/encryptInputsBuilder.test.ts +747 -0
  49. package/core/encrypt/encryptInputsBuilder.ts +583 -0
  50. package/core/encrypt/encryptUtils.ts +67 -0
  51. package/core/encrypt/zkPackProveVerify.ts +335 -0
  52. package/core/error.ts +168 -0
  53. package/core/fetchKeys.test.ts +195 -0
  54. package/core/fetchKeys.ts +144 -0
  55. package/core/index.ts +106 -0
  56. package/core/keyStore.test.ts +226 -0
  57. package/core/keyStore.ts +154 -0
  58. package/core/permits.test.ts +493 -0
  59. package/core/permits.ts +201 -0
  60. package/core/types.ts +419 -0
  61. package/core/utils.ts +130 -0
  62. package/dist/adapters.cjs +88 -0
  63. package/dist/adapters.d.cts +14576 -0
  64. package/dist/adapters.d.ts +14576 -0
  65. package/dist/adapters.js +83 -0
  66. package/dist/chains.cjs +111 -0
  67. package/dist/chains.d.cts +121 -0
  68. package/dist/chains.d.ts +121 -0
  69. package/dist/chains.js +1 -0
  70. package/dist/chunk-36FBWLUS.js +3310 -0
  71. package/dist/chunk-7HLGHV67.js +990 -0
  72. package/dist/chunk-TBLR7NNE.js +102 -0
  73. package/dist/clientTypes-AVSCBet7.d.cts +998 -0
  74. package/dist/clientTypes-flH1ju82.d.ts +998 -0
  75. package/dist/core.cjs +4362 -0
  76. package/dist/core.d.cts +138 -0
  77. package/dist/core.d.ts +138 -0
  78. package/dist/core.js +3 -0
  79. package/dist/node.cjs +4225 -0
  80. package/dist/node.d.cts +22 -0
  81. package/dist/node.d.ts +22 -0
  82. package/dist/node.js +91 -0
  83. package/dist/permit-jRirYqFt.d.cts +376 -0
  84. package/dist/permit-jRirYqFt.d.ts +376 -0
  85. package/dist/permits.cjs +1025 -0
  86. package/dist/permits.d.cts +353 -0
  87. package/dist/permits.d.ts +353 -0
  88. package/dist/permits.js +1 -0
  89. package/dist/types-YiAC4gig.d.cts +33 -0
  90. package/dist/types-YiAC4gig.d.ts +33 -0
  91. package/dist/web.cjs +4434 -0
  92. package/dist/web.d.cts +42 -0
  93. package/dist/web.d.ts +42 -0
  94. package/dist/web.js +256 -0
  95. package/dist/zkProve.worker.cjs +93 -0
  96. package/dist/zkProve.worker.d.cts +2 -0
  97. package/dist/zkProve.worker.d.ts +2 -0
  98. package/dist/zkProve.worker.js +91 -0
  99. package/node/client.test.ts +159 -0
  100. package/node/config.test.ts +68 -0
  101. package/node/encryptInputs.test.ts +155 -0
  102. package/node/index.ts +97 -0
  103. package/node/storage.ts +51 -0
  104. package/package.json +121 -0
  105. package/permits/index.ts +68 -0
  106. package/permits/localstorage.test.ts +113 -0
  107. package/permits/onchain-utils.ts +221 -0
  108. package/permits/permit.test.ts +534 -0
  109. package/permits/permit.ts +386 -0
  110. package/permits/sealing.test.ts +84 -0
  111. package/permits/sealing.ts +131 -0
  112. package/permits/signature.ts +79 -0
  113. package/permits/store.test.ts +88 -0
  114. package/permits/store.ts +156 -0
  115. package/permits/test-utils.ts +28 -0
  116. package/permits/types.ts +204 -0
  117. package/permits/utils.ts +58 -0
  118. package/permits/validation.test.ts +361 -0
  119. package/permits/validation.ts +327 -0
  120. package/web/client.web.test.ts +159 -0
  121. package/web/config.web.test.ts +69 -0
  122. package/web/const.ts +2 -0
  123. package/web/encryptInputs.web.test.ts +172 -0
  124. package/web/index.ts +166 -0
  125. package/web/storage.ts +49 -0
  126. package/web/worker.builder.web.test.ts +148 -0
  127. package/web/worker.config.web.test.ts +329 -0
  128. package/web/worker.output.web.test.ts +84 -0
  129. package/web/workerManager.test.ts +80 -0
  130. package/web/workerManager.ts +214 -0
  131. package/web/workerManager.web.test.ts +114 -0
  132. package/web/zkProve.worker.ts +133 -0
@@ -0,0 +1,88 @@
1
+ /**
2
+ * @vitest-environment node
3
+ */
4
+
5
+ import { describe, it, expect, beforeEach, afterEach } from 'vitest';
6
+ import {
7
+ permitStore,
8
+ getPermit,
9
+ getActivePermit,
10
+ getPermits,
11
+ setPermit,
12
+ removePermit,
13
+ getActivePermitHash,
14
+ setActivePermitHash,
15
+ PermitUtils,
16
+ } from './index.js';
17
+
18
+ import { createMockPermit } from './test-utils.js';
19
+
20
+ describe('Storage Tests', () => {
21
+ const chainId = 1;
22
+ const account = '0x1234567890123456789012345678901234567890';
23
+
24
+ beforeEach(() => {
25
+ permitStore.resetStore();
26
+ });
27
+
28
+ afterEach(() => {
29
+ permitStore.resetStore();
30
+ });
31
+
32
+ describe('Permit Storage', () => {
33
+ it('should store and retrieve permits', async () => {
34
+ const permit = await createMockPermit();
35
+
36
+ setPermit(chainId, account, permit);
37
+ const retrieved = getPermit(chainId, account, permit.hash);
38
+
39
+ expect(retrieved).toBeDefined();
40
+ expect(PermitUtils.serialize(retrieved!)).toEqual(PermitUtils.serialize(permit));
41
+ });
42
+
43
+ it('should handle multiple permits per account', async () => {
44
+ const permit1 = await createMockPermit();
45
+ const permit2 = await createMockPermit({
46
+ issuer: '0x0987654321098765432109876543210987654321' as `0x${string}`,
47
+ });
48
+
49
+ setPermit(chainId, account, permit1);
50
+ setPermit(chainId, account, permit2);
51
+
52
+ const permits = getPermits(chainId, account);
53
+ expect(Object.keys(permits)).toHaveLength(2);
54
+
55
+ expect(PermitUtils.serialize(permits[permit1.hash])).toEqual(PermitUtils.serialize(permit1));
56
+ expect(PermitUtils.serialize(permits[permit2.hash])).toEqual(PermitUtils.serialize(permit2));
57
+ });
58
+
59
+ it('should handle active permit hash', async () => {
60
+ const permit = await createMockPermit();
61
+
62
+ setPermit(chainId, account, permit);
63
+ setActivePermitHash(chainId, account, permit.hash);
64
+
65
+ const activeHash = getActivePermitHash(chainId, account);
66
+ expect(activeHash).toBe(permit.hash);
67
+
68
+ const activePermit = getActivePermit(chainId, account);
69
+ expect(activePermit).toBeDefined();
70
+ expect(PermitUtils.serialize(activePermit!)).toEqual(PermitUtils.serialize(permit));
71
+ });
72
+
73
+ it('should remove permits', async () => {
74
+ const permit = await createMockPermit();
75
+
76
+ setPermit(chainId, account, permit);
77
+ setActivePermitHash(chainId, account, permit.hash);
78
+
79
+ removePermit(chainId, account, permit.hash);
80
+
81
+ const retrieved = getPermit(chainId, account, permit.hash);
82
+ expect(retrieved).toBeUndefined();
83
+
84
+ const activeHash = getActivePermitHash(chainId, account);
85
+ expect(activeHash).toBeUndefined();
86
+ });
87
+ });
88
+ });
@@ -0,0 +1,156 @@
1
+ import { createStore } from 'zustand/vanilla';
2
+ import { persist } from 'zustand/middleware';
3
+ import { produce } from 'immer';
4
+ import { type Permit, type SerializedPermit } from './types.js';
5
+ import { PermitUtils } from './permit.js';
6
+
7
+ type ChainRecord<T> = Record<number, T>;
8
+ type AccountRecord<T> = Record<string, T>;
9
+ type HashRecord<T> = Record<string, T>;
10
+
11
+ type PermitsStore = {
12
+ permits: ChainRecord<AccountRecord<HashRecord<SerializedPermit | undefined>>>;
13
+ activePermitHash: ChainRecord<AccountRecord<string | undefined>>;
14
+ };
15
+
16
+ // Stores generated permits for each user, a hash indicating the active permit for each user
17
+ // Can be used to create reactive hooks
18
+ export const PERMIT_STORE_DEFAULTS: PermitsStore = {
19
+ permits: {},
20
+ activePermitHash: {},
21
+ };
22
+ export const _permitStore = createStore<PermitsStore>()(
23
+ persist(() => PERMIT_STORE_DEFAULTS, { name: 'cofhesdk-permits' })
24
+ );
25
+
26
+ export const clearStaleStore = () => {
27
+ // Any is used here because we do not have types of the previous store
28
+ const state = _permitStore.getState() as any;
29
+
30
+ // Check if the store has the expected structure
31
+ const hasExpectedStructure =
32
+ state &&
33
+ typeof state === 'object' &&
34
+ 'permits' in state &&
35
+ 'activePermitHash' in state &&
36
+ typeof state.permits === 'object' &&
37
+ typeof state.activePermitHash === 'object';
38
+
39
+ if (hasExpectedStructure) return;
40
+ // Invalid structure detected - clear the store
41
+ _permitStore.setState({ permits: {}, activePermitHash: {} });
42
+ };
43
+
44
+ export const getPermit = (
45
+ chainId: number | undefined,
46
+ account: string | undefined,
47
+ hash: string | undefined
48
+ ): Permit | undefined => {
49
+ clearStaleStore();
50
+ if (chainId == null || account == null || hash == null) return;
51
+
52
+ const savedPermit = _permitStore.getState().permits[chainId]?.[account]?.[hash];
53
+ if (savedPermit == null) return;
54
+
55
+ return PermitUtils.deserialize(savedPermit);
56
+ };
57
+
58
+ export const getActivePermit = (chainId: number | undefined, account: string | undefined): Permit | undefined => {
59
+ clearStaleStore();
60
+ if (chainId == null || account == null) return;
61
+
62
+ const activePermitHash = _permitStore.getState().activePermitHash[chainId]?.[account];
63
+ return getPermit(chainId, account, activePermitHash);
64
+ };
65
+
66
+ export const getPermits = (chainId: number | undefined, account: string | undefined): Record<string, Permit> => {
67
+ clearStaleStore();
68
+ if (chainId == null || account == null) return {};
69
+
70
+ return Object.entries(_permitStore.getState().permits[chainId]?.[account] ?? {}).reduce(
71
+ (acc, [hash, permit]) => {
72
+ if (permit == undefined) return acc;
73
+ return { ...acc, [hash]: PermitUtils.deserialize(permit) };
74
+ },
75
+ {} as Record<string, Permit>
76
+ );
77
+ };
78
+
79
+ export const setPermit = (chainId: number, account: string, permit: Permit) => {
80
+ clearStaleStore();
81
+ _permitStore.setState(
82
+ produce<PermitsStore>((state) => {
83
+ if (state.permits[chainId] == null) state.permits[chainId] = {};
84
+ if (state.permits[chainId][account] == null) state.permits[chainId][account] = {};
85
+ state.permits[chainId][account][permit.hash] = PermitUtils.serialize(permit);
86
+ })
87
+ );
88
+ };
89
+
90
+ export const removePermit = (chainId: number, account: string, hash: string) => {
91
+ clearStaleStore();
92
+ _permitStore.setState(
93
+ produce<PermitsStore>((state) => {
94
+ if (state.permits[chainId] == null) state.permits[chainId] = {};
95
+ if (state.activePermitHash[chainId] == null) state.activePermitHash[chainId] = {};
96
+
97
+ const accountPermits = state.permits[chainId][account];
98
+ if (accountPermits == null) return;
99
+
100
+ if (accountPermits[hash] == null) return;
101
+
102
+ if (state.activePermitHash[chainId][account] === hash) {
103
+ // if the active permit is the one to be removed - unset it
104
+ state.activePermitHash[chainId][account] = undefined;
105
+ }
106
+ // Remove the permit
107
+ accountPermits[hash] = undefined;
108
+ })
109
+ );
110
+ };
111
+
112
+ export const getActivePermitHash = (chainId: number | undefined, account: string | undefined): string | undefined => {
113
+ clearStaleStore();
114
+ if (chainId == null || account == null) return undefined;
115
+ return _permitStore.getState().activePermitHash[chainId]?.[account];
116
+ };
117
+
118
+ export const setActivePermitHash = (chainId: number, account: string, hash: string) => {
119
+ clearStaleStore();
120
+ _permitStore.setState(
121
+ produce<PermitsStore>((state) => {
122
+ if (state.activePermitHash[chainId] == null) state.activePermitHash[chainId] = {};
123
+ state.activePermitHash[chainId][account] = hash;
124
+ })
125
+ );
126
+ };
127
+
128
+ export const removeActivePermitHash = (chainId: number, account: string) => {
129
+ clearStaleStore();
130
+ _permitStore.setState(
131
+ produce<PermitsStore>((state) => {
132
+ if (state.activePermitHash[chainId]) state.activePermitHash[chainId][account] = undefined;
133
+ })
134
+ );
135
+ };
136
+
137
+ export const resetStore = () => {
138
+ clearStaleStore();
139
+ _permitStore.setState({ permits: {}, activePermitHash: {} });
140
+ };
141
+
142
+ export const permitStore = {
143
+ store: _permitStore,
144
+
145
+ getPermit,
146
+ getActivePermit,
147
+ getPermits,
148
+ setPermit,
149
+ removePermit,
150
+
151
+ getActivePermitHash,
152
+ setActivePermitHash,
153
+ removeActivePermitHash,
154
+
155
+ resetStore,
156
+ };
@@ -0,0 +1,28 @@
1
+ import { type Permit, type SerializedPermit, GenerateSealingKey, PermitUtils } from './index.js';
2
+
3
+ // Mock permit for testing - using Bob's address as issuer
4
+ export const createMockPermit = async (overrides: Partial<Permit> = {}): Promise<Permit> => {
5
+ const sealingPair = GenerateSealingKey();
6
+
7
+ const fields = {
8
+ name: 'Test Permit',
9
+ type: 'self',
10
+ issuer: '0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266', // Bob's address
11
+ expiration: 1000000000000,
12
+ recipient: '0x0000000000000000000000000000000000000000',
13
+ validatorId: 0,
14
+ validatorContract: '0x0000000000000000000000000000000000000000',
15
+ sealingPair: sealingPair.serialize(),
16
+ issuerSignature: '0x',
17
+ recipientSignature: '0x',
18
+ _signedDomain: undefined,
19
+ ...overrides,
20
+ } as const;
21
+
22
+ const serializedPermit: SerializedPermit = {
23
+ hash: PermitUtils.getHash(fields),
24
+ ...fields,
25
+ };
26
+
27
+ return PermitUtils.deserialize(serializedPermit);
28
+ };
@@ -0,0 +1,204 @@
1
+ import { SealingKey as SealingKeyClass, type EthEncryptedData } from './sealing.js';
2
+ import { type Hex } from 'viem';
3
+
4
+ /**
5
+ * EIP712 related types
6
+ */
7
+ export type EIP712Type = { name: string; type: string };
8
+ export type EIP712Types = Record<string, EIP712Type[]>;
9
+ export type EIP712Message = Record<string, string>;
10
+ export type EIP712Domain = {
11
+ chainId: number;
12
+ name: string;
13
+ verifyingContract: Hex;
14
+ version: string;
15
+ };
16
+
17
+ /**
18
+ * Sealing key type - using the actual SealingKey class
19
+ */
20
+ export type SealingKey = SealingKeyClass;
21
+
22
+ /**
23
+ * Re-export EthEncryptedData from sealing module
24
+ */
25
+ export type { EthEncryptedData };
26
+
27
+ // Viem client types will be imported from viem package
28
+
29
+ /**
30
+ * Core Permit interface - immutable design for React compatibility
31
+ */
32
+ export interface Permit {
33
+ /**
34
+ * Stable hash of relevant permit data, used as key in storage
35
+ */
36
+ hash: string;
37
+ /**
38
+ * Name for this permit, for organization and UI usage, not included in signature.
39
+ */
40
+ name: string;
41
+ /**
42
+ * The type of the Permit (self / sharing)
43
+ * (self) Permit that will be signed and used by the issuer
44
+ * (sharing) Permit that is signed by the issuer, but intended to be shared with recipient
45
+ * (recipient) Permit that has been received, and signed by the recipient
46
+ */
47
+ type: 'self' | 'sharing' | 'recipient';
48
+ /**
49
+ * (base) User that initially created the permission, target of data fetching
50
+ */
51
+ issuer: Hex;
52
+ /**
53
+ * (base) Expiration timestamp
54
+ */
55
+ expiration: number;
56
+ /**
57
+ * (sharing) The user that this permission will be shared with
58
+ * ** optional, use `address(0)` to disable **
59
+ */
60
+ recipient: Hex;
61
+ /**
62
+ * (issuer defined validation) An id used to query a contract to check this permissions validity
63
+ * ** optional, use `0` to disable **
64
+ */
65
+ validatorId: number;
66
+ /**
67
+ * (issuer defined validation) The contract to query to determine permission validity
68
+ * ** optional, user `address(0)` to disable **
69
+ */
70
+ validatorContract: Hex;
71
+ /**
72
+ * (base) The publicKey of a sealingPair used to re-encrypt `issuer`s confidential data
73
+ * (non-sharing) Populated by `issuer`
74
+ * (sharing) Populated by `recipient`
75
+ */
76
+ sealingPair: SealingKey;
77
+ /**
78
+ * (base) `signTypedData` signature created by `issuer`.
79
+ * (base) Shared- and Self- permissions differ in signature format: (`sealingKey` absent in shared signature)
80
+ * (non-sharing) < issuer, expiration, recipient, validatorId, validatorContract, sealingKey >
81
+ * (sharing) < issuer, expiration, recipient, validatorId, validatorContract >
82
+ */
83
+ issuerSignature: Hex;
84
+ /**
85
+ * (sharing) `signTypedData` signature created by `recipient` with format:
86
+ * (sharing) < sealingKey, issuerSignature>
87
+ * ** required for shared permits **
88
+ */
89
+ recipientSignature: Hex;
90
+ /**
91
+ * EIP712 domain used to sign this permit.
92
+ * Should not be set manually, included in metadata as part of serialization flows.
93
+ */
94
+ _signedDomain?: EIP712Domain;
95
+ }
96
+
97
+ /**
98
+ * Permit discriminant helpers
99
+ */
100
+ export type PermitType = Permit['type'];
101
+
102
+ /**
103
+ * Utility type to narrow a permit to a specific discriminant.
104
+ *
105
+ * Note: this only narrows the `type` field. Runtime/validation constraints
106
+ * (e.g. recipient == zeroAddress for self permits) are enforced elsewhere.
107
+ */
108
+ export type PermitOf<T extends PermitType> = Expand<Omit<Permit, 'type'> & { type: T }>;
109
+
110
+ export type SelfPermit = PermitOf<'self'>;
111
+ export type SharingPermit = PermitOf<'sharing'>;
112
+ export type RecipientPermit = PermitOf<'recipient'>;
113
+
114
+ /**
115
+ * Optional additional metadata of a Permit
116
+ * Can be passed into the constructor, but not necessary
117
+ * Useful for deserialization
118
+ */
119
+ export interface PermitMetadata {
120
+ /**
121
+ * EIP712 domain used to sign this permit.
122
+ * Should not be set manually, included in metadata as part of serialization flows.
123
+ */
124
+ _signedDomain?: EIP712Domain;
125
+ }
126
+
127
+ /**
128
+ * Utility types for permit creation
129
+ */
130
+
131
+ // Specific option types for each permit creation method
132
+ export type CreateSelfPermitOptions = {
133
+ type?: 'self';
134
+ issuer: string;
135
+ name?: string;
136
+ expiration?: number;
137
+ validatorId?: number;
138
+ validatorContract?: string;
139
+ };
140
+
141
+ export type CreateSharingPermitOptions = {
142
+ type?: 'sharing';
143
+ issuer: string;
144
+ recipient: string;
145
+ name?: string;
146
+ expiration?: number;
147
+ validatorId?: number;
148
+ validatorContract?: string;
149
+ };
150
+
151
+ export type ImportSharedPermitOptions = {
152
+ type?: 'sharing';
153
+ issuer: string;
154
+ recipient: string;
155
+ issuerSignature: string;
156
+ name?: string;
157
+ expiration: number;
158
+ validatorId?: number;
159
+ validatorContract?: string;
160
+ };
161
+
162
+ export type SerializedPermit = Omit<Permit, 'sealingPair'> & {
163
+ _signedDomain?: EIP712Domain;
164
+ sealingPair: {
165
+ privateKey: string;
166
+ publicKey: string;
167
+ };
168
+ };
169
+
170
+ /**
171
+ * A type representing the Permission struct that is passed to Permissioned.sol to grant encrypted data access.
172
+ */
173
+ export type Permission = Expand<
174
+ Omit<Permit, 'name' | 'type' | 'sealingPair' | 'hash'> & {
175
+ sealingKey: Hex;
176
+ }
177
+ >;
178
+
179
+ /**
180
+ * A type representing the permit fields that are used to generate the hash
181
+ */
182
+ export type PermitHashFields = Pick<
183
+ Permit,
184
+ 'type' | 'issuer' | 'expiration' | 'recipient' | 'validatorId' | 'validatorContract'
185
+ >;
186
+
187
+ /**
188
+ * Validation result type
189
+ */
190
+ export interface ValidationResult {
191
+ valid: boolean;
192
+ error: 'invalid-schema' | 'expired' | 'not-signed' | null;
193
+ }
194
+
195
+ /**
196
+ * Signature types for EIP712 signing
197
+ */
198
+ export type PermitSignaturePrimaryType =
199
+ | 'PermissionedV2IssuerSelf'
200
+ | 'PermissionedV2IssuerShared'
201
+ | 'PermissionedV2Recipient';
202
+
203
+ // Utils
204
+ export type Expand<T> = T extends infer O ? { [K in keyof O]: O[K] } : never;
@@ -0,0 +1,58 @@
1
+ // Utility functions for sealing key operations
2
+
3
+ declare const BigInt: (value: string | number | bigint) => bigint;
4
+
5
+ export const fromHexString = (hexString: string): Uint8Array => {
6
+ const cleanString = hexString.length % 2 === 1 ? `0${hexString}` : hexString;
7
+ const arr = cleanString.replace(/^0x/, '').match(/.{1,2}/g);
8
+ if (!arr) return new Uint8Array();
9
+ return new Uint8Array(arr.map((byte) => parseInt(byte, 16)));
10
+ };
11
+
12
+ export const toHexString = (bytes: Uint8Array) =>
13
+ bytes.reduce((str, byte) => str + byte.toString(16).padStart(2, '0'), '');
14
+
15
+ export function toBigInt(value: number | string | bigint | Uint8Array): bigint {
16
+ if (typeof value === 'string') {
17
+ return BigInt(value);
18
+ } else if (typeof value === 'number') {
19
+ return BigInt(value);
20
+ } else if (typeof value === 'object') {
21
+ // Uint8Array
22
+ return BigInt('0x' + toHexString(value));
23
+ } else {
24
+ return value as bigint;
25
+ }
26
+ }
27
+
28
+ export function toBeArray(value: bigint | number): Uint8Array {
29
+ const bigIntValue = typeof value === 'number' ? BigInt(value) : value;
30
+ const hex = bigIntValue.toString(16);
31
+ const paddedHex = hex.length % 2 === 0 ? hex : '0' + hex;
32
+ return fromHexString(paddedHex);
33
+ }
34
+
35
+ export function isString(value: unknown) {
36
+ if (typeof value !== 'string') {
37
+ throw new Error(`Expected value which is \`string\`, received value of type \`${typeof value}\`.`);
38
+ }
39
+ }
40
+
41
+ export function isNumber(value: unknown) {
42
+ const is = typeof value === 'number' && !Number.isNaN(value);
43
+ if (!is) {
44
+ throw new Error(`Expected value which is \`number\`, received value of type \`${typeof value}\`.`);
45
+ }
46
+ }
47
+
48
+ export function isBigIntOrNumber(value: unknown) {
49
+ const is = typeof value === 'bigint';
50
+
51
+ if (!is) {
52
+ try {
53
+ isNumber(value);
54
+ } catch (e) {
55
+ throw new Error(`Value ${value} is not a number or bigint: ${typeof value}`);
56
+ }
57
+ }
58
+ }