@thru/programs 0.2.37 → 0.2.39

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.
@@ -0,0 +1,63 @@
1
+ import { describe, expect, it } from 'vitest';
2
+ import { encodeAddress } from '@thru/sdk/helpers';
3
+ import {
4
+ assertWalletFeePayerCompatible,
5
+ buildAccountContext,
6
+ buildWalletAccountContext,
7
+ } from './context';
8
+
9
+ function account(fill: number): Uint8Array {
10
+ return new Uint8Array(32).fill(fill);
11
+ }
12
+
13
+ describe('buildWalletAccountContext', () => {
14
+ it('matches concrete account indices for any non-colliding fee payer', () => {
15
+ const wallet = account(10);
16
+ const writable = account(20);
17
+ const targetProgram = account(30);
18
+ const mint = account(40);
19
+ const wrapperProgram = encodeAddress(account(50));
20
+ const params = {
21
+ walletAddress: encodeAddress(wallet),
22
+ readWriteAccounts: [writable],
23
+ readOnlyAccounts: [targetProgram, mint],
24
+ programAddress: wrapperProgram,
25
+ };
26
+ const deferred = buildWalletAccountContext(params);
27
+
28
+ for (const feePayer of [account(1), account(2)]) {
29
+ const concrete = buildAccountContext({
30
+ ...params,
31
+ feePayerAddress: encodeAddress(feePayer),
32
+ });
33
+ expect(deferred.readWriteAddresses).toEqual(concrete.readWriteAddresses);
34
+ expect(deferred.readOnlyAddresses).toEqual(concrete.readOnlyAddresses);
35
+ expect(deferred.walletAccountIdx).toBe(concrete.walletAccountIdx);
36
+ for (const target of [wallet, writable, targetProgram, mint]) {
37
+ expect(deferred.getAccountIndex(target)).toBe(concrete.getAccountIndex(target));
38
+ }
39
+ }
40
+ });
41
+
42
+ it('rejects a fee payer that overlaps any encoded instruction account', () => {
43
+ const wallet = account(10);
44
+ const writable = account(20);
45
+ const targetProgram = account(30);
46
+ const mint = account(40);
47
+ const params = {
48
+ walletAddress: encodeAddress(wallet),
49
+ readWriteAccounts: [writable],
50
+ readOnlyAccounts: [targetProgram, mint],
51
+ programAddress: encodeAddress(account(50)),
52
+ };
53
+
54
+ for (const collision of [wallet, writable, targetProgram, mint, account(50)]) {
55
+ expect(() =>
56
+ assertWalletFeePayerCompatible({
57
+ ...params,
58
+ feePayerAddress: encodeAddress(collision),
59
+ })
60
+ ).toThrow('Wallet fee payer must not overlap instruction accounts');
61
+ }
62
+ });
63
+ });
@@ -1,7 +1,90 @@
1
1
  import { encodeAddress, decodeAddress } from '@thru/sdk/helpers';
2
2
  import { bytesEqual, compareBytes, uniqueAccounts } from './encoding';
3
3
  import { PASSKEY_MANAGER_PROGRAM_ADDRESS } from './constants';
4
- import type { AccountContext } from './types';
4
+ import type { AccountContext, WalletAccountContext } from './types';
5
+
6
+ interface WalletAccountContextParams {
7
+ walletAddress: string;
8
+ readWriteAccounts: Uint8Array[];
9
+ readOnlyAccounts: Uint8Array[];
10
+ programAddress?: string;
11
+ }
12
+
13
+ function buildDeferredAccountLists(params: WalletAccountContextParams) {
14
+ const programBytes = decodeAddress(params.programAddress ?? PASSKEY_MANAGER_PROGRAM_ADDRESS);
15
+ const walletBytes = decodeAddress(params.walletAddress);
16
+
17
+ const readWriteBytes = uniqueAccounts([walletBytes, ...params.readWriteAccounts])
18
+ .filter((addr) => !bytesEqual(addr, programBytes))
19
+ .sort(compareBytes);
20
+ const readOnlyBytes = uniqueAccounts(params.readOnlyAccounts)
21
+ .filter(
22
+ (addr) =>
23
+ !bytesEqual(addr, programBytes) &&
24
+ !readWriteBytes.some((candidate) => bytesEqual(candidate, addr))
25
+ )
26
+ .sort(compareBytes);
27
+
28
+ return { programBytes, walletBytes, readWriteBytes, readOnlyBytes };
29
+ }
30
+
31
+ /**
32
+ * Build account indices for an instruction that a wallet will wrap and pay for.
33
+ * The concrete fee payer is intentionally unknown here; index 0 is reserved for
34
+ * it and index 1 for the wrapper program.
35
+ */
36
+ export function buildWalletAccountContext(
37
+ params: WalletAccountContextParams
38
+ ): WalletAccountContext {
39
+ const { programBytes, walletBytes, readWriteBytes, readOnlyBytes } =
40
+ buildDeferredAccountLists(params);
41
+ const readWriteAddresses = readWriteBytes.map(encodeAddress);
42
+ const readOnlyAddresses = readOnlyBytes.map(encodeAddress);
43
+
44
+ const findIndex = (target: Uint8Array): number => {
45
+ if (bytesEqual(target, programBytes)) return 1;
46
+
47
+ const rwIndex = readWriteBytes.findIndex((candidate) => bytesEqual(candidate, target));
48
+ if (rwIndex >= 0) return rwIndex + 2;
49
+
50
+ const roIndex = readOnlyBytes.findIndex((candidate) => bytesEqual(candidate, target));
51
+ if (roIndex >= 0) return roIndex + 2 + readWriteBytes.length;
52
+
53
+ return -1;
54
+ };
55
+
56
+ const walletAccountIdx = findIndex(walletBytes);
57
+ if (walletAccountIdx < 2) {
58
+ throw new Error('Wallet account must be a non-program account');
59
+ }
60
+
61
+ return {
62
+ readWriteAddresses,
63
+ readOnlyAddresses,
64
+ walletAccountIdx,
65
+ getAccountIndex: (pubkey: Uint8Array) => {
66
+ const idx = findIndex(pubkey);
67
+ if (idx < 0) {
68
+ throw new Error('Account not found in transaction accounts');
69
+ }
70
+ return idx;
71
+ },
72
+ };
73
+ }
74
+
75
+ /** Ensure the wallet-selected fee payer does not invalidate indices that were
76
+ * encoded before that payer was known. */
77
+ export function assertWalletFeePayerCompatible(params: WalletAccountContextParams & {
78
+ feePayerAddress: string;
79
+ }): void {
80
+ const feePayerBytes = decodeAddress(params.feePayerAddress);
81
+ const { programBytes, walletBytes, readWriteBytes, readOnlyBytes } =
82
+ buildDeferredAccountLists(params);
83
+ const conflicts = [programBytes, walletBytes, ...readWriteBytes, ...readOnlyBytes];
84
+ if (conflicts.some((account) => bytesEqual(account, feePayerBytes))) {
85
+ throw new Error('Wallet fee payer must not overlap instruction accounts');
86
+ }
87
+ }
5
88
 
6
89
  /**
7
90
  * Build account context for passkey manager transactions.
@@ -26,6 +26,7 @@ export type {
26
26
  RemoveAuthorityInstructionParams,
27
27
  RegisterCredentialInstructionParams,
28
28
  AccountContext,
29
+ WalletAccountContext,
29
30
  WalletSigner,
30
31
  TransactionExecutionSummary,
31
32
  PasskeyMetadata,
@@ -61,7 +62,12 @@ export { createValidateChallenge, VALIDATE_CHALLENGE_DOMAIN } from './challenge'
61
62
  export { createWalletSeed, deriveWalletAddress, createCredentialLookupSeed, deriveCredentialLookupAddress } from './seeds';
62
63
 
63
64
  // Account context building
64
- export { buildAccountContext, buildPasskeyReadWriteAccounts } from './context';
65
+ export {
66
+ assertWalletFeePayerCompatible,
67
+ buildAccountContext,
68
+ buildPasskeyReadWriteAccounts,
69
+ buildWalletAccountContext,
70
+ } from './context';
65
71
  export { decodeAddress, encodeAddress } from '@thru/sdk/helpers';
66
72
 
67
73
  // Account parsing
@@ -115,6 +115,16 @@ export interface AccountContext {
115
115
  getAccountIndex: (pubkey: Uint8Array) => number;
116
116
  }
117
117
 
118
+ /** Account indices for a wallet-wrapped target instruction whose fee payer is
119
+ * selected later by the wallet. Index 0 is reserved for that fee payer and
120
+ * index 1 for the wrapper program. */
121
+ export interface WalletAccountContext {
122
+ readWriteAddresses: string[];
123
+ readOnlyAddresses: string[];
124
+ walletAccountIdx: number;
125
+ getAccountIndex: (pubkey: Uint8Array) => number;
126
+ }
127
+
118
128
  export interface RegisterCredentialInstructionParams {
119
129
  walletAccountIdx: number;
120
130
  lookupAccountIdx: number;
package/tsconfig.json CHANGED
@@ -2,7 +2,8 @@
2
2
  "extends": "../tsconfig.base.json",
3
3
  "compilerOptions": {
4
4
  "outDir": "./dist",
5
- "rootDir": "./src"
5
+ "rootDir": "./src",
6
+ "types": ["node"]
6
7
  },
7
8
  "include": ["src/**/*"],
8
9
  "exclude": ["node_modules", "dist"]
package/tsup.config.ts CHANGED
@@ -6,6 +6,7 @@ export default defineConfig({
6
6
  'passkey-manager/index': 'src/passkey-manager/index.ts',
7
7
  'multicall/index': 'src/multicall/index.ts',
8
8
  'amm/index': 'src/amm/index.ts',
9
+ 'clob/index': 'src/clob/index.ts',
9
10
  },
10
11
  format: ['cjs', 'esm'],
11
12
  dts: true,