@lightprotocol/stateless.js 0.1.0-alpha.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.
Files changed (44) hide show
  1. package/README.md +58 -0
  2. package/dist/cjs/index.cjs +4031 -0
  3. package/dist/es/index.js +3988 -0
  4. package/dist/types/actions/common.d.ts +3 -0
  5. package/dist/types/actions/compress-lamports.d.ts +16 -0
  6. package/dist/types/actions/decompress-lamports.d.ts +16 -0
  7. package/dist/types/actions/index.d.ts +4 -0
  8. package/dist/types/actions/init-sol-omnibus-account.d.ts +13 -0
  9. package/dist/types/constants.d.ts +34 -0
  10. package/dist/types/errors.d.ts +74 -0
  11. package/dist/types/idls/account_compression.d.ts +932 -0
  12. package/dist/types/idls/index.d.ts +5 -0
  13. package/dist/types/idls/light.d.ts +192 -0
  14. package/dist/types/idls/psp_compressed_pda.d.ts +607 -0
  15. package/dist/types/idls/user_registry.d.ts +69 -0
  16. package/dist/types/index.d.ts +12 -0
  17. package/dist/types/instruction/index.d.ts +1 -0
  18. package/dist/types/instruction/pack-compressed-accounts.d.ts +30 -0
  19. package/dist/types/programs/compressed-pda.d.ts +148 -0
  20. package/dist/types/programs/index.d.ts +1 -0
  21. package/dist/types/rpc-interface.d.ts +430 -0
  22. package/dist/types/rpc.d.ts +26 -0
  23. package/dist/types/state/BN254.d.ts +20 -0
  24. package/dist/types/state/compressed-account.d.ts +35 -0
  25. package/dist/types/state/index.d.ts +3 -0
  26. package/dist/types/state/types.d.ts +108 -0
  27. package/dist/types/test-utils/common.d.ts +32 -0
  28. package/dist/types/test-utils/index.d.ts +5 -0
  29. package/dist/types/test-utils/merkle-tree.d.ts +92 -0
  30. package/dist/types/test-utils/parse-event.d.ts +7 -0
  31. package/dist/types/test-utils/parse-validity-proof.d.ts +20 -0
  32. package/dist/types/test-utils/test-rpc.d.ts +51 -0
  33. package/dist/types/utils/airdrop.d.ts +7 -0
  34. package/dist/types/utils/conversion.d.ts +10 -0
  35. package/dist/types/utils/index.d.ts +6 -0
  36. package/dist/types/utils/pipe.d.ts +2 -0
  37. package/dist/types/utils/send-and-confirm.d.ts +18 -0
  38. package/dist/types/utils/sleep.d.ts +1 -0
  39. package/dist/types/utils/validation.d.ts +4 -0
  40. package/dist/types/wallet/index.d.ts +1 -0
  41. package/dist/types/wallet/interface.d.ts +25 -0
  42. package/dist/types/wallet/use-wallet.d.ts +9 -0
  43. package/dist/umd/index.js +4027 -0
  44. package/package.json +81 -0
@@ -0,0 +1,108 @@
1
+ /// <reference types="node" />
2
+ import { BN } from '@coral-xyz/anchor';
3
+ import { PublicKey } from '@solana/web3.js';
4
+ export interface PackedCompressedAccountWithMerkleContext {
5
+ compressedAccount: CompressedAccount;
6
+ merkleTreePubkeyIndex: number;
7
+ nullifierQueuePubkeyIndex: number;
8
+ leafIndex: number;
9
+ }
10
+ /**
11
+ * Describe the generic compressed account details applicable to every
12
+ * compressed account.
13
+ * */
14
+ export interface CompressedAccount {
15
+ /** Public key of program or user that owns the account */
16
+ owner: PublicKey;
17
+ /** Lamports attached to the account */
18
+ lamports: BN;
19
+ /**
20
+ * TODO: Implement address functionality. Optional unique account ID that is
21
+ * persistent across transactions.
22
+ */
23
+ address: PublicKey | null;
24
+ /** Optional data attached to the account */
25
+ data: CompressedAccountData | null;
26
+ }
27
+ export interface CompressedAccountData {
28
+ discriminator: number[];
29
+ data: Buffer;
30
+ dataHash: number[];
31
+ }
32
+ export interface PublicTransactionEvent {
33
+ inputCompressedAccountHashes: number[][];
34
+ outputCompressedAccountHashes: number[][];
35
+ inputCompressedAccounts: PackedCompressedAccountWithMerkleContext[];
36
+ outputCompressedAccounts: CompressedAccount[];
37
+ outputStateMerkleTreeAccountIndices: Uint8Array;
38
+ outputLeafIndices: number[];
39
+ relayFee: BN | null;
40
+ isCompress: boolean;
41
+ deCompressLamports: BN | null;
42
+ pubkeyArray: PublicKey[];
43
+ message: Uint8Array | null;
44
+ }
45
+ export interface InstructionDataTransfer {
46
+ proof: CompressedProof | null;
47
+ inputRootIndices: number[];
48
+ inputCompressedAccountsWithMerkleContext: PackedCompressedAccountWithMerkleContext[];
49
+ outputCompressedAccounts: CompressedAccount[];
50
+ outputStateMerkleTreeAccountIndices: Buffer;
51
+ relayFee: BN | null;
52
+ deCompressLamports: BN | null;
53
+ isCompression: boolean;
54
+ newAddressParams: NewAddressParamsPacked[];
55
+ }
56
+ export interface NewAddressParamsPacked {
57
+ seed: number[];
58
+ addressQueueAccountIndex: number;
59
+ addressMerkleTreeAccountIndex: number;
60
+ addressMerkleTreeRootIndex: number;
61
+ }
62
+ export interface CompressedProof {
63
+ a: number[];
64
+ b: number[];
65
+ c: number[];
66
+ }
67
+ /**
68
+ * Compressed-token types
69
+ *
70
+ * TODO: Token-related code should ideally not have to go into stateless.js.
71
+ * Find a better altnerative way to extend the RPC.
72
+ *
73
+ */
74
+ export type TokenTransferOutputData = {
75
+ owner: PublicKey;
76
+ amount: BN;
77
+ lamports: BN | null;
78
+ };
79
+ export type CompressedTokenInstructionDataTransfer = {
80
+ proof: CompressedProof | null;
81
+ rootIndices: number[];
82
+ mint: PublicKey;
83
+ signerIsDelegate: boolean;
84
+ inputTokenDataWithContext: InputTokenDataWithContext[];
85
+ outputCompressedAccounts: TokenTransferOutputData[];
86
+ outputStateMerkleTreeAccountIndices: Buffer;
87
+ pubkeyArray: PublicKey[];
88
+ isCompress: boolean;
89
+ compressionAmount: BN | null;
90
+ };
91
+ export interface InputTokenDataWithContext {
92
+ amount: BN;
93
+ delegateIndex: number | null;
94
+ delegatedAmount: BN | null;
95
+ isNative: BN | null;
96
+ merkleTreePubkeyIndex: number;
97
+ nullifierQueuePubkeyIndex: number;
98
+ leafIndex: number;
99
+ }
100
+ export type TokenData = {
101
+ mint: PublicKey;
102
+ owner: PublicKey;
103
+ amount: BN;
104
+ delegate: PublicKey | null;
105
+ state: number;
106
+ isNative: BN | null;
107
+ delegatedAmount: BN;
108
+ };
@@ -0,0 +1,32 @@
1
+ import { Connection, Keypair, PublicKey, Signer } from '@solana/web3.js';
2
+ import { Rpc } from '../rpc';
3
+ import { LightWasm } from '@lightprotocol/hasher.rs';
4
+ import { TestRpc } from './test-rpc';
5
+ export declare const ALICE: Keypair;
6
+ export declare const BOB: Keypair;
7
+ export declare const CHARLIE: Keypair;
8
+ export declare const DAVE: Keypair;
9
+ export declare function newAccountWithLamports(rpc: Rpc, lamports?: number, counter?: number | undefined): Promise<Signer>;
10
+ export declare function getConnection(): Connection;
11
+ /**
12
+ * Returns a mock RPC instance for use in unit tests.
13
+ *
14
+ * @param endpoint RPC endpoint URL. Defaults to
15
+ * 'http://127.0.0.1:8899'.
16
+ * @param proverEndpoint Prover server endpoint URL. Defaults to
17
+ * 'http://localhost:3001'.
18
+ * @param lightWasm Wasm hasher instance.
19
+ * @param merkleTreeAddress Address of the merkle tree to index. Defaults
20
+ * to the public default test state tree.
21
+ * @param nullifierQueueAddress Optional address of the associated nullifier
22
+ * queue.
23
+ * @param depth Depth of the merkle tree.
24
+ * @param log Log proof generation time.
25
+ */
26
+ export declare function getTestRpc(endpoint?: string, proverEndpoint?: string, lightWasm?: LightWasm, merkleTreeAddress?: PublicKey, nullifierQueueAddress?: PublicKey, depth?: number, log?: boolean): Promise<TestRpc>;
27
+ /**
28
+ * For use in tests.
29
+ * Generate a unique keypair by passing in a counter <255. If no counter
30
+ * is supplied, it uses and increments a global counter.
31
+ */
32
+ export declare function getTestKeypair(counter?: number | undefined): Keypair;
@@ -0,0 +1,5 @@
1
+ export * from './common';
2
+ export * from './merkle-tree';
3
+ export * from './test-rpc';
4
+ export * from './parse-event';
5
+ export * from './parse-validity-proof';
@@ -0,0 +1,92 @@
1
+ import { LightWasm } from '@lightprotocol/hasher.rs';
2
+ export declare const DEFAULT_ZERO = "0";
3
+ /**
4
+ * @callback hashFunction
5
+ * @param left Left leaf
6
+ * @param right Right leaf
7
+ */
8
+ /**
9
+ * Merkle tree
10
+ */
11
+ export declare class MerkleTree {
12
+ /**
13
+ * Constructor
14
+ * @param {number} levels Number of levels in the tree
15
+ * @param {Array} [elements] Initial elements
16
+ * @param {Object} options
17
+ * @param {hashFunction} [options.hashFunction] Function used to hash 2 leaves
18
+ * @param [options.zeroElement] Value for non-existent leaves
19
+ */
20
+ levels: number;
21
+ capacity: number;
22
+ zeroElement: string;
23
+ _zeros: string[];
24
+ _layers: string[][];
25
+ _lightWasm: LightWasm;
26
+ constructor(levels: number, lightWasm: LightWasm, elements?: string[], { zeroElement }?: {
27
+ zeroElement?: string | undefined;
28
+ });
29
+ _rebuild(): void;
30
+ /**
31
+ * Get tree root
32
+ * @returns {*}
33
+ */
34
+ root(): string;
35
+ /**
36
+ * Insert new element into the tree
37
+ * @param element Element to insert
38
+ */
39
+ insert(element: string): void;
40
+ /**
41
+ * Insert multiple elements into the tree. Tree will be fully rebuilt during this operation.
42
+ * @param {Array} elements Elements to insert
43
+ */
44
+ bulkInsert(elements: string[]): void;
45
+ /**
46
+ * Change an element in the tree
47
+ * @param {number} index Index of element to change
48
+ * @param element Updated element value
49
+ */
50
+ update(index: number, element: string): void;
51
+ /**
52
+ * Get merkle path to a leaf
53
+ * @param {number} index Leaf index to generate path for
54
+ * @returns {{pathElements: number[], pathIndex: number[]}} An object containing adjacent elements and left-right index
55
+ */
56
+ path(index: number): {
57
+ pathElements: string[];
58
+ pathIndices: number[];
59
+ };
60
+ /**
61
+ * Find an element in the tree
62
+ * @param element An element to find
63
+ * @param comparator A function that checks leaf value equality
64
+ * @returns {number} Index if element is found, otherwise -1
65
+ */
66
+ indexOf(element: string, comparator?: ((element: string, el: string) => boolean) | null): number;
67
+ /**
68
+ * Returns a copy of non-zero tree elements
69
+ * @returns {Object[]}
70
+ */
71
+ elements(): string[];
72
+ /**
73
+ * Serialize entire tree state including intermediate layers into a plain object
74
+ * Deserializing it back will not require to recompute any hashes
75
+ * Elements are not converted to a plain type, this is responsibility of the caller
76
+ */
77
+ serialize(): {
78
+ levels: number;
79
+ _zeros: string[];
80
+ _layers: string[][];
81
+ };
82
+ /**
83
+ * Deserialize data into a MerkleTree instance
84
+ * Make sure to provide the same hashFunction as was used in the source tree,
85
+ * otherwise the tree state will be invalid
86
+ *
87
+ * @param data
88
+ * @param hashFunction
89
+ * @returns {MerkleTree}
90
+ */
91
+ static deserialize(data: any, hashFunction: (left: string, right: string) => string): any;
92
+ }
@@ -0,0 +1,7 @@
1
+ /// <reference types="node" />
2
+ import { ParsedTransactionWithMeta } from '@solana/web3.js';
3
+ import { PublicTransactionEvent } from '../state';
4
+ type Deserializer<T> = (data: Buffer, tx: ParsedTransactionWithMeta) => T;
5
+ export declare const parseEvents: <T>(indexerEventsTransactions: (ParsedTransactionWithMeta | null)[], deserializeFn: Deserializer<T>) => NonNullable<T>[];
6
+ export declare const parsePublicTransactionEventWithIdl: (data: Buffer) => PublicTransactionEvent | null;
7
+ export {};
@@ -0,0 +1,20 @@
1
+ import { CompressedProof } from '../state';
2
+ interface GnarkProofJson {
3
+ ar: string[];
4
+ bs: string[][];
5
+ krs: string[];
6
+ }
7
+ type ProofABC = {
8
+ a: Uint8Array;
9
+ b: Uint8Array;
10
+ c: Uint8Array;
11
+ };
12
+ export declare const placeholderValidityProof: () => {
13
+ a: number[];
14
+ b: number[];
15
+ c: number[];
16
+ };
17
+ export declare const checkValidityProofShape: (proof: CompressedProof) => void;
18
+ export declare function proofFromJsonStruct(json: GnarkProofJson): ProofABC;
19
+ export declare function negateAndCompressProof(proof: ProofABC): CompressedProof;
20
+ export {};
@@ -0,0 +1,51 @@
1
+ import { PublicKey } from '@solana/web3.js';
2
+ import { LightWasm } from '@lightprotocol/hasher.rs';
3
+ import { CompressedProofWithContext } from '../rpc-interface';
4
+ import { BN254, PublicTransactionEvent } from '../state';
5
+ import { Rpc } from '../rpc';
6
+ export interface TestRpcConfig {
7
+ /** Address of the state tree to index. Default: public default test state
8
+ * tree */
9
+ merkleTreeAddress?: PublicKey;
10
+ /** Nullifier queue associated with merkleTreeAddress */
11
+ nullifierQueueAddress?: PublicKey;
12
+ /** Depth of state tree. Defaults to the public default test state tree depth */
13
+ depth?: number;
14
+ /** Log proof generation time */
15
+ log?: boolean;
16
+ }
17
+ /**
18
+ * Simple mock rpc for unit tests that simulates the compression rpc interface.
19
+ * Fetches, parses events and builds merkletree on-demand, i.e. it does not persist state.
20
+ * Constraints:
21
+ * - Can only index 1 merkletree
22
+ * - Can only index up to 1000 transactions
23
+ *
24
+ * For advanced testing use photon: https://github.com/helius-labs/photon
25
+ */
26
+ export declare class TestRpc extends Rpc {
27
+ merkleTreeAddress: PublicKey;
28
+ nullifierQueueAddress: PublicKey;
29
+ lightWasm: LightWasm;
30
+ depth: number;
31
+ log: boolean;
32
+ /**
33
+ * Instantiate a mock RPC simulating the compression rpc interface.
34
+ *
35
+ * @param endpoint endpoint to the solana cluster (use for
36
+ * localnet only)
37
+ * @param hasher light wasm hasher instance
38
+ * @param testRpcConfig Config for the mock rpc
39
+ * @param proverEndpoint Optional endpoint to the prover server.
40
+ * defaults to endpoint
41
+ * @param connectionConfig Optional connection config
42
+ */
43
+ constructor(endpoint: string, hasher: LightWasm, proverEndpoint: string, testRpcConfig?: TestRpcConfig, connectionConfig?: string);
44
+ /**
45
+ * @internal
46
+ * Returns newest first
47
+ * */
48
+ getParsedEvents(): Promise<PublicTransactionEvent[]>;
49
+ /** Retrieve validity proof for compressed accounts */
50
+ getValidityProof(compressedAccountHashes: BN254[]): Promise<CompressedProofWithContext>;
51
+ }
@@ -0,0 +1,7 @@
1
+ import { Commitment, Connection, PublicKey } from '@solana/web3.js';
2
+ export declare function airdropSol({ connection, lamports, recipientPublicKey, }: {
3
+ connection: Connection;
4
+ lamports: number;
5
+ recipientPublicKey: PublicKey;
6
+ }): Promise<string>;
7
+ export declare function confirmTransaction(connection: Connection, signature: string, confirmation?: Commitment): Promise<import("@solana/web3.js").RpcResponseAndContext<import("@solana/web3.js").SignatureResult>>;
@@ -0,0 +1,10 @@
1
+ /// <reference types="node" />
2
+ import { Buffer } from 'buffer';
3
+ import { Keypair } from '@solana/web3.js';
4
+ export declare function byteArrayToKeypair(byteArray: number[]): Keypair;
5
+ export declare const toArray: <T>(value: T | T[]) => T[];
6
+ export declare const bufToDecStr: (buf: Buffer) => string;
7
+ export declare function hashToBn254FieldSizeLe(bytes: Buffer): Promise<[Buffer, number] | null>;
8
+ /** Mutates array in place */
9
+ export declare function pushUniqueItems<T>(items: T[], map: T[]): void;
10
+ export declare function toCamelCase(obj: Array<any> | unknown | any): Array<any> | unknown | any;
@@ -0,0 +1,6 @@
1
+ export * from './conversion';
2
+ export * from './pipe';
3
+ export * from './send-and-confirm';
4
+ export * from './sleep';
5
+ export * from './airdrop';
6
+ export * from './validation';
@@ -0,0 +1,2 @@
1
+ /** pipe function */
2
+ export declare function pipe<T, R>(initialFunction: (arg: T) => R, ...functions: ((arg: R) => R)[]): (initialValue: T) => R;
@@ -0,0 +1,18 @@
1
+ import { VersionedTransaction, SignatureResult, RpcResponseAndContext, Signer, TransactionInstruction, ConfirmOptions, TransactionSignature } from '@solana/web3.js';
2
+ import { Rpc } from '../rpc';
3
+ /** Sends a versioned transaction and confirms it. */
4
+ export declare function sendAndConfirmTx(rpc: Rpc, tx: VersionedTransaction, confirmOptions?: ConfirmOptions): Promise<TransactionSignature>;
5
+ /** @internal */
6
+ export declare function confirmTx(rpc: Rpc, txId: string, blockHashCtx?: {
7
+ blockhash: string;
8
+ lastValidBlockHeight: number;
9
+ }): Promise<RpcResponseAndContext<SignatureResult>>;
10
+ /**
11
+ * Builds a versioned Transaction from instructions and signs it.
12
+ *
13
+ * @param instructions instructions to include in the transaction
14
+ * @param payer payer of the transaction
15
+ * @param blockhash recent blockhash to use in the transaction
16
+ * @param additionalSigners non-feepayer signers to include in the transaction
17
+ */
18
+ export declare function buildAndSignTx(instructions: TransactionInstruction[], payer: Signer, blockhash: string, additionalSigners?: Signer[]): VersionedTransaction;
@@ -0,0 +1 @@
1
+ export declare function sleep(ms: number): Promise<void>;
@@ -0,0 +1,4 @@
1
+ import { BN } from '@coral-xyz/anchor';
2
+ import { CompressedAccount, CompressedAccountWithMerkleContext } from '../state';
3
+ export declare const validateSufficientBalance: (balance: BN) => void;
4
+ export declare const validateSameOwner: (compressedAccounts: CompressedAccount[] | CompressedAccountWithMerkleContext[]) => void;
@@ -0,0 +1 @@
1
+ export * from './use-wallet';
@@ -0,0 +1,25 @@
1
+ import { Commitment, Connection, Keypair, VersionedTransaction } from '@solana/web3.js';
2
+ import { PublicKey, Transaction } from '@solana/web3.js';
3
+ export type InclusionProofPublicInputs = {
4
+ root: string;
5
+ leaf: string;
6
+ };
7
+ export type InclusionProofPrivateInputs = {
8
+ merkleProof: string[];
9
+ leaf: string;
10
+ leafIndex: string;
11
+ };
12
+ export type InclusionProofInputs = (InclusionProofPublicInputs & InclusionProofPrivateInputs)[];
13
+ export declare class Wallet {
14
+ _publicKey: PublicKey;
15
+ _keypair: Keypair;
16
+ _connection: Connection;
17
+ _url: string;
18
+ _commitment: Commitment;
19
+ constructor(keypair: Keypair, url: string, commitment: Commitment);
20
+ signTransaction: (tx: any) => Promise<any>;
21
+ sendTransaction: (transaction: VersionedTransaction) => Promise<string>;
22
+ signAllTransactions: <T extends VersionedTransaction | Transaction>(transactions: T[]) => Promise<T[]>;
23
+ signMessage: (message: Uint8Array) => Promise<Uint8Array>;
24
+ sendAndConfirmTransaction: (transaction: Transaction, signers?: never[]) => Promise<any>;
25
+ }
@@ -0,0 +1,9 @@
1
+ import { Keypair, Commitment } from '@solana/web3.js';
2
+ export declare const useWallet: (keypair: Keypair, url?: string, commitment?: Commitment) => {
3
+ publicKey: import("@solana/web3.js").PublicKey;
4
+ sendAndConfirmTransaction: (transaction: import("@solana/web3.js").Transaction, signers?: never[]) => Promise<any>;
5
+ signMessage: (message: Uint8Array) => Promise<Uint8Array>;
6
+ signTransaction: (tx: any) => Promise<any>;
7
+ signAllTransactions: <T extends import("@solana/web3.js").VersionedTransaction | import("@solana/web3.js").Transaction>(transactions: T[]) => Promise<T[]>;
8
+ sendTransaction: (transaction: import("@solana/web3.js").VersionedTransaction) => Promise<string>;
9
+ };