@lethenymous/sdk 0.0.0-stage → 0.1.0

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,203 @@
1
+ import type { Connection, PublicKey, Signer, Transaction, TransactionInstruction, VersionedTransaction } from "@solana/web3.js";
2
+ export type Asset = "A" | "B";
3
+ export type Direction = "AToB" | "BToA";
4
+ export type Hash32 = Uint8Array;
5
+ export interface WalletAdapter {
6
+ publicKey: PublicKey;
7
+ signTransaction<T extends Transaction>(tx: T): Promise<T>;
8
+ signVersionedTransaction?(tx: VersionedTransaction): Promise<VersionedTransaction>;
9
+ }
10
+ export interface LookupTableConfig {
11
+ address: PublicKey;
12
+ expectedAddresses?: PublicKey[];
13
+ expectedAuthority?: PublicKey | null;
14
+ }
15
+ export interface ClientConfig {
16
+ connection: Connection;
17
+ wallet: WalletAdapter;
18
+ programId?: PublicKey;
19
+ witnessProvider?: MerkleWitnessProvider;
20
+ lookupTables?: Array<PublicKey | LookupTableConfig>;
21
+ }
22
+ export interface PoolState {
23
+ address: PublicKey;
24
+ authority: PublicKey;
25
+ tokenAMint: PublicKey;
26
+ tokenBMint: PublicKey;
27
+ tokenAVault: PublicKey;
28
+ tokenBVault: PublicKey;
29
+ protocolFeeVaultA: PublicKey;
30
+ protocolFeeVaultB: PublicKey;
31
+ creatorFeeVaultA: PublicKey;
32
+ creatorFeeVaultB: PublicKey;
33
+ creatorFeeRecipient: PublicKey;
34
+ lpMint: PublicKey;
35
+ feeBps: number;
36
+ bump: number;
37
+ version: number;
38
+ swapNonce: bigint;
39
+ }
40
+ export interface ShieldedState {
41
+ pool: PublicKey;
42
+ tokenAMint: PublicKey;
43
+ tokenBMint: PublicKey;
44
+ custodyA: PublicKey;
45
+ custodyB: PublicKey;
46
+ tree: PublicKey;
47
+ bump: number;
48
+ version: number;
49
+ }
50
+ export interface TreeState {
51
+ pool: PublicKey;
52
+ generation: bigint;
53
+ nextIndex: bigint;
54
+ sequence: bigint;
55
+ frontier: Uint8Array[];
56
+ frontierPresent: number[];
57
+ emptySubtrees: Uint8Array[];
58
+ roots: Uint8Array[];
59
+ rootSequences: bigint[];
60
+ rootGenerations: bigint[];
61
+ }
62
+ export interface ProtocolConfig {
63
+ authority: PublicKey;
64
+ feeRecipient: PublicKey;
65
+ bump: number;
66
+ version: number;
67
+ }
68
+ export interface BuiltTransaction {
69
+ instructions: TransactionInstruction[];
70
+ signers?: Signer[];
71
+ }
72
+ export interface MerkleWitness {
73
+ index: bigint;
74
+ siblings: Uint8Array[];
75
+ root: Uint8Array;
76
+ rootSequence: bigint;
77
+ generation: bigint;
78
+ }
79
+ export interface MerkleWitnessProvider {
80
+ getWitness(pool: PublicKey, commitment: Uint8Array): Promise<MerkleWitness>;
81
+ }
82
+ /** Implementations must authenticate checkpoint bytes and make replacement writes crash-safe. */
83
+ export interface MerkleCheckpointStore {
84
+ loadMerkleCheckpoint(identity: string): Promise<Uint8Array | undefined>;
85
+ saveMerkleCheckpoint(identity: string, checkpoint: Uint8Array): Promise<void>;
86
+ }
87
+ export type NoteState = "available" | "reserved" | "submitted" | "spent";
88
+ export interface Note {
89
+ pool: PublicKey;
90
+ asset: PublicKey;
91
+ amount: bigint;
92
+ ownerCommitment: Uint8Array;
93
+ randomness: Uint8Array;
94
+ commitment: Uint8Array;
95
+ encryptedPayload?: Uint8Array;
96
+ leafIndex?: bigint;
97
+ state?: NoteState;
98
+ operationId?: Uint8Array;
99
+ transactionSignature?: string;
100
+ spent?: boolean;
101
+ }
102
+ export interface NoteStore {
103
+ getNotes(ownerCommitment?: Uint8Array): Promise<Note[]>;
104
+ saveNote(note: Note): Promise<void>;
105
+ reserveNote(commitment: Uint8Array, operationId: Uint8Array, ownerCommitment: Uint8Array): Promise<Note>;
106
+ markSubmitted(commitment: Uint8Array, operationId: Uint8Array, signature: string): Promise<void>;
107
+ markSpent(commitment: Uint8Array, operationId?: Uint8Array): Promise<void>;
108
+ releaseReservation(commitment: Uint8Array, operationId?: Uint8Array): Promise<void>;
109
+ }
110
+ export type OperationKind = "shield" | "unshield" | "private_swap";
111
+ export type OperationState = "intent" | "prepared" | "submitted" | "finalized" | "failed" | "unknown";
112
+ export interface OperationRecord {
113
+ id: Uint8Array;
114
+ kind: OperationKind;
115
+ state: OperationState;
116
+ pool: PublicKey;
117
+ inputCommitment?: Uint8Array;
118
+ signature?: string;
119
+ lastValidBlockHeight?: bigint;
120
+ signedTransaction?: Uint8Array;
121
+ metadata: Record<string, string>;
122
+ outputNotes: Note[];
123
+ }
124
+ export interface JournaledNoteStore extends NoteStore {
125
+ reserveNoteAndBegin(commitment: Uint8Array, operationId: Uint8Array, ownerCommitment: Uint8Array, operation: OperationRecord): Promise<Note>;
126
+ beginOperation(operation: OperationRecord): Promise<void>;
127
+ updateOperation(id: Uint8Array, patch: Partial<Pick<OperationRecord, "metadata" | "outputNotes">>): Promise<void>;
128
+ markPrepared(id: Uint8Array, signedTransaction: Uint8Array, lastValidBlockHeight: bigint): Promise<void>;
129
+ markOperationSubmitted(id: Uint8Array, signature: string): Promise<void>;
130
+ markOperationFinalized(id: Uint8Array): Promise<void>;
131
+ markOperationFailed(id: Uint8Array, reason: string): Promise<void>;
132
+ markOperationUnknown(id: Uint8Array, reason: string): Promise<void>;
133
+ getPendingOperations(): Promise<OperationRecord[]>;
134
+ }
135
+ export interface Prover {
136
+ proveUnshield(input: UnshieldProverInput): Promise<{
137
+ proof: Uint8Array;
138
+ publicInputs: Uint8Array;
139
+ }>;
140
+ provePrivateSwap(input: PrivateSwapProverInput): Promise<{
141
+ proof: Uint8Array;
142
+ publicInputs: Uint8Array;
143
+ }>;
144
+ }
145
+ export interface UnshieldProverInput {
146
+ pool: PublicKey;
147
+ asset: PublicKey;
148
+ root: Uint8Array;
149
+ rootSequence: bigint;
150
+ generation: bigint;
151
+ nullifier: Uint8Array;
152
+ amount: bigint;
153
+ recipient: PublicKey;
154
+ spendSecret: Uint8Array;
155
+ randomness: Uint8Array;
156
+ witness: MerkleWitness;
157
+ }
158
+ export interface PrivateSwapProverInput {
159
+ pool: PublicKey;
160
+ assetIn: PublicKey;
161
+ assetOut: PublicKey;
162
+ root: Uint8Array;
163
+ rootSequence: bigint;
164
+ generation: bigint;
165
+ nullifier: Uint8Array;
166
+ reserveIn: bigint;
167
+ reserveOut: bigint;
168
+ feeBps: number;
169
+ amountIn: bigint;
170
+ amountOut: bigint;
171
+ changeAmount: bigint;
172
+ changeCommitment: Uint8Array;
173
+ outputCommitment: Uint8Array;
174
+ direction: number;
175
+ swapNonce: bigint;
176
+ inputSpendSecret: Uint8Array;
177
+ inputRandomness: Uint8Array;
178
+ witness: MerkleWitness;
179
+ changeSpendSecret: Uint8Array;
180
+ changeRandomness: Uint8Array;
181
+ outputSpendSecret: Uint8Array;
182
+ outputRandomness: Uint8Array;
183
+ }
184
+ export interface ShieldParams {
185
+ pool: PublicKey;
186
+ mint: PublicKey;
187
+ amount: bigint;
188
+ }
189
+ export interface RecipientParams extends ShieldParams {
190
+ recipient: PublicKey;
191
+ }
192
+ export type TransactionOutcome = {
193
+ status: "finalized-success";
194
+ signature: string;
195
+ } | {
196
+ status: "finalized-failed";
197
+ signature: string;
198
+ error: unknown;
199
+ } | {
200
+ status: "unknown";
201
+ signature: string;
202
+ reason: string;
203
+ };
package/dist/types.js ADDED
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,73 @@
1
+ import { PublicKey } from "@solana/web3.js";
2
+ import type { JournaledNoteStore, MerkleWitnessProvider, Note, NoteStore, OperationRecord, Prover, ShieldParams } from "./types.js";
3
+ import { type Lethenymous } from "./client.js";
4
+ export declare class InMemoryNoteStore implements JournaledNoteStore {
5
+ private readonly notes;
6
+ private readonly operations;
7
+ getNotes(ownerCommitment?: Uint8Array): Promise<Note[]>;
8
+ saveNote(input: Note): Promise<void>;
9
+ reserveNote(commitment: Uint8Array, operationId: Uint8Array, owner: Uint8Array): Promise<Note>;
10
+ reserveNoteAndBegin(commitment: Uint8Array, operationId: Uint8Array, owner: Uint8Array, operation: OperationRecord): Promise<Note>;
11
+ markSubmitted(commitment: Uint8Array, operationId: Uint8Array, signature: string): Promise<void>;
12
+ markSpent(commitment: Uint8Array, operationId?: Uint8Array): Promise<void>;
13
+ releaseReservation(commitment: Uint8Array, operationId?: Uint8Array): Promise<void>;
14
+ beginOperation(operation: OperationRecord): Promise<void>;
15
+ updateOperation(id: Uint8Array, patch: Partial<Pick<OperationRecord, "metadata" | "outputNotes">>): Promise<void>;
16
+ markPrepared(id: Uint8Array, signedTransaction: Uint8Array, lastValidBlockHeight: bigint): Promise<void>;
17
+ markOperationSubmitted(id: Uint8Array, signature: string): Promise<void>;
18
+ markOperationFinalized(id: Uint8Array): Promise<void>;
19
+ markOperationFailed(id: Uint8Array, reason: string): Promise<void>;
20
+ markOperationUnknown(id: Uint8Array, reason: string): Promise<void>;
21
+ getPendingOperations(): Promise<OperationRecord[]>;
22
+ }
23
+ export declare class ShieldedWallet {
24
+ private readonly sdk;
25
+ private readonly prover;
26
+ private readonly witnessProvider;
27
+ readonly spendSecret: Uint8Array;
28
+ readonly viewKey: Uint8Array;
29
+ readonly ownerCommitment: Uint8Array;
30
+ private readonly store;
31
+ constructor(sdk: Lethenymous, seed: Uint8Array, prover: Prover | undefined, witnessProvider: MerkleWitnessProvider | undefined, store: NoteStore);
32
+ private journal;
33
+ addNote(note: Note): Promise<void>;
34
+ getNotes(): Promise<Note[]>;
35
+ getPrivateBalance(input: {
36
+ pool: PublicKey;
37
+ mint: PublicKey;
38
+ }): Promise<bigint>;
39
+ private requireBackend;
40
+ private select;
41
+ private begin;
42
+ private reserveAndBegin;
43
+ private fail;
44
+ private unknown;
45
+ private submitted;
46
+ private finalized;
47
+ shield(input: ShieldParams): Promise<{
48
+ signature: string;
49
+ note: Note;
50
+ }>;
51
+ unshield(input: {
52
+ pool: PublicKey;
53
+ mint: PublicKey;
54
+ amount: bigint;
55
+ recipient: PublicKey;
56
+ }): Promise<string>;
57
+ privateSend(input: {
58
+ pool: PublicKey;
59
+ mint: PublicKey;
60
+ amount: bigint;
61
+ recipient: PublicKey;
62
+ }): Promise<string>;
63
+ privateSwap(input: {
64
+ pool: PublicKey;
65
+ inputMint: PublicKey;
66
+ outputMint: PublicKey;
67
+ amountIn: bigint;
68
+ minAmountOut: bigint;
69
+ }): Promise<string>;
70
+ private localNote;
71
+ recoverShieldedNotes(pool: PublicKey): Promise<Note[]>;
72
+ reconcilePending(): Promise<OperationRecord[]>;
73
+ }