@fatsolutions/privacy-pools-core-starknet-sdk 0.0.1

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 (81) hide show
  1. package/dist/abis/ERC20.abi.d.ts +404 -0
  2. package/dist/abis/ERC20.abi.js +553 -0
  3. package/dist/abis/ERC20.abi.js.map +1 -0
  4. package/dist/abis/EntryPoint.abi.d.ts +667 -0
  5. package/dist/abis/EntryPoint.abi.js +898 -0
  6. package/dist/abis/EntryPoint.abi.js.map +1 -0
  7. package/dist/abis/PrivacyPool.abi.d.ts +388 -0
  8. package/dist/abis/PrivacyPool.abi.js +530 -0
  9. package/dist/abis/PrivacyPool.abi.js.map +1 -0
  10. package/dist/abis/index.d.ts +2 -0
  11. package/dist/abis/index.js +8 -0
  12. package/dist/abis/index.js.map +1 -0
  13. package/dist/constants.d.ts +2 -0
  14. package/dist/constants.js +6 -0
  15. package/dist/constants.js.map +1 -0
  16. package/dist/contracts/contracts.service.d.ts +286 -0
  17. package/dist/contracts/contracts.service.js +205 -0
  18. package/dist/contracts/contracts.service.js.map +1 -0
  19. package/dist/contracts/index.d.ts +2 -0
  20. package/dist/contracts/index.js +19 -0
  21. package/dist/contracts/index.js.map +1 -0
  22. package/dist/contracts/transactionHandler.d.ts +3 -0
  23. package/dist/contracts/transactionHandler.js +27 -0
  24. package/dist/contracts/transactionHandler.js.map +1 -0
  25. package/dist/data.service.d.ts +44 -0
  26. package/dist/data.service.js +166 -0
  27. package/dist/data.service.js.map +1 -0
  28. package/dist/errors/contracts.errors.d.ts +27 -0
  29. package/dist/errors/contracts.errors.js +39 -0
  30. package/dist/errors/contracts.errors.js.map +1 -0
  31. package/dist/errors/index.d.ts +31 -0
  32. package/dist/errors/index.js +61 -0
  33. package/dist/errors/index.js.map +1 -0
  34. package/dist/garaga.d.ts +2 -0
  35. package/dist/garaga.js +52 -0
  36. package/dist/garaga.js.map +1 -0
  37. package/dist/index.d.ts +14 -0
  38. package/dist/index.js +47 -0
  39. package/dist/index.js.map +1 -0
  40. package/dist/sdk.d.ts +17 -0
  41. package/dist/sdk.js +35 -0
  42. package/dist/sdk.js.map +1 -0
  43. package/dist/types/conversions.d.ts +6 -0
  44. package/dist/types/conversions.js +52 -0
  45. package/dist/types/conversions.js.map +1 -0
  46. package/dist/types/entrypoint.d.ts +12 -0
  47. package/dist/types/entrypoint.js +15 -0
  48. package/dist/types/entrypoint.js.map +1 -0
  49. package/dist/types/garaga.d.ts +26 -0
  50. package/dist/types/garaga.js +3 -0
  51. package/dist/types/garaga.js.map +1 -0
  52. package/dist/types/index.d.ts +4 -0
  53. package/dist/types/index.js +21 -0
  54. package/dist/types/index.js.map +1 -0
  55. package/dist/types/snarkjs.d.ts +51 -0
  56. package/dist/types/snarkjs.js +3 -0
  57. package/dist/types/snarkjs.js.map +1 -0
  58. package/dist/utils.d.ts +10 -0
  59. package/dist/utils.js +49 -0
  60. package/dist/utils.js.map +1 -0
  61. package/package.json +44 -0
  62. package/src/abis/ERC20.abi.ts +549 -0
  63. package/src/abis/EntryPoint.abi.ts +894 -0
  64. package/src/abis/PrivacyPool.abi.ts +526 -0
  65. package/src/abis/index.ts +2 -0
  66. package/src/constants.ts +2 -0
  67. package/src/contracts/contracts.service.ts +415 -0
  68. package/src/contracts/index.ts +2 -0
  69. package/src/contracts/transactionHandler.ts +32 -0
  70. package/src/data.service.ts +218 -0
  71. package/src/errors/contracts.errors.ts +35 -0
  72. package/src/errors/index.ts +69 -0
  73. package/src/garaga.ts +20 -0
  74. package/src/index.ts +46 -0
  75. package/src/sdk.ts +44 -0
  76. package/src/types/conversions.ts +59 -0
  77. package/src/types/entrypoint.ts +23 -0
  78. package/src/types/garaga.ts +29 -0
  79. package/src/types/index.ts +4 -0
  80. package/src/types/snarkjs.ts +26 -0
  81. package/src/utils.ts +49 -0
@@ -0,0 +1,35 @@
1
+ export const CONTRACT_ERROR_CODES = {
2
+ "0x4173736574204d6973736d61746368": "Asset Missmatch",
3
+ "0x43616c6c6572206973206e6f7420456e747279706f696e74": "Caller is not Entrypoint",
4
+ "0x436f6d6d69746d656e74206e6f7420666f756e64": "Commitment not found",
5
+ "0x436f6d6d69746d656e742070726573656e74": "Commitment present",
6
+ "0x496e636f72726563742041535020726f6f74": "Incorrect ASP root",
7
+ "0x496e76616c696420416d6f756e74": "Invalid Amount",
8
+ "0x496e76616c696420436f6e74657874": "Invalid Context",
9
+ "0x496e76616c696420456e747279706f696e74": "Invalid Entrypoint",
10
+ "0x496e76616c696420506f6f6c205374617465": "Invalid Pool State",
11
+ "0x496e76616c69642050726f636573736f72": "Invalid Processor",
12
+ "0x496e76616c69642050726f6f66": "Invalid Proof",
13
+ "0x496e76616c696420526f6f74": "Invalid Root",
14
+ "0x496e76616c69642054726565204465707468": "Invalid Tree Depth",
15
+ "0x4e6f2041535020726f6f742073746f726564": "No ASP root stored",
16
+ "0x4e6f74204f726967696e616c204465706f7369746f72": "Not Original Depositor",
17
+ "0x4e756c6c69666965722075736564": "Nullifier used",
18
+ "0x506f6f6c20616c72656164792072656769737465726564": "Pool already registered",
19
+ "0x506f6f6c20646f6573206e6f74206578697374": "Pool does not exist",
20
+ "0x506f6f6c2069732064656164": "Pool is dead",
21
+ "0x506f6f6c206e6f7420666f756e64": "Pool not found",
22
+ "0x506f6f6c206e6f742072656769737465726564": "Pool not registered",
23
+ "0x507265636f6d6d69746d656e742075736564": "Precommitment used",
24
+ "0x72656c617965722066656520544f2048494748": "relayer fee TO HIGH",
25
+ "0x56616c7565206c6573732074686174206d696e696d756d": "Value less that minimum"
26
+ } as const;
27
+
28
+ export function parseContractError(errorString: string): string | null {
29
+ for (const [hexCode, description] of Object.entries(CONTRACT_ERROR_CODES)) {
30
+ if (errorString.includes(hexCode)) {
31
+ return description;
32
+ }
33
+ }
34
+ return null;
35
+ }
@@ -0,0 +1,69 @@
1
+
2
+ export enum SNBaseErrorCode {
3
+ UNKNOWN,
4
+ UNREACHABLE,
5
+ INTERNAL
6
+ }
7
+
8
+ export enum SNContractErrorCode {
9
+ DEPOSIT_FAILED,
10
+ INVALID_IPFS_CID,
11
+ REVERT
12
+ }
13
+
14
+ export enum SNFormatErrorCode {
15
+ CONVERSION
16
+ }
17
+
18
+ export type SNErrorCode = SNBaseErrorCode | SNContractErrorCode | SNFormatErrorCode;
19
+
20
+ export class StarknetSDKError extends Error {
21
+
22
+ override readonly name: string;
23
+ override message: string;
24
+
25
+ constructor(message: string, readonly code: SNErrorCode) {
26
+ super();
27
+ this.message = message;
28
+ this.name = this.constructor.name;
29
+ this.code = code;
30
+ }
31
+
32
+ public static unknown() {
33
+ return new this("Unknown error", SNBaseErrorCode.UNKNOWN);
34
+ }
35
+
36
+ public static unreachable() {
37
+ return new this("Ureachable code", SNBaseErrorCode.UNREACHABLE);
38
+ }
39
+
40
+ public static internal(msg?: string) {
41
+ return new this(msg || "Internal error", SNBaseErrorCode.INTERNAL);
42
+ }
43
+
44
+ }
45
+
46
+ export class SNFormatError extends StarknetSDKError {
47
+
48
+ public static conversion(conetxt?: string) {
49
+ const _context = conetxt || "Conversion failed";
50
+ return new this(_context, SNFormatErrorCode.CONVERSION);
51
+ }
52
+
53
+ }
54
+
55
+ export class SNContractError extends StarknetSDKError {
56
+
57
+ public static depositFailed() {
58
+ return new this("Deposit failed", SNContractErrorCode.DEPOSIT_FAILED);
59
+ }
60
+
61
+ public static invalidIPFSsize(ipfsCID: string) {
62
+ return new this(`Invalid IPFS CID size: expected >32 & <64, got ${ipfsCID.length}`, SNContractErrorCode.INVALID_IPFS_CID);
63
+ }
64
+
65
+ public static revert(reason: string) {
66
+ return new this(reason, SNContractErrorCode.REVERT);
67
+ }
68
+
69
+ }
package/src/garaga.ts ADDED
@@ -0,0 +1,20 @@
1
+ import { Groth16VerifyingKey, Groth16Proof } from "./types/garaga.js";
2
+ import * as garaga from "garaga";
3
+
4
+ let _initialized = false;
5
+ garaga.init().then(() => _initialized = true);
6
+
7
+ function sleep(ms: number) {
8
+ return new Promise(res => setTimeout(res, ms));
9
+ }
10
+
11
+ async function _waitUntilReady() {
12
+ while (!_initialized) {
13
+ await sleep(100);
14
+ }
15
+ }
16
+
17
+ export async function getGroth16CallData(proof: Groth16Proof, verifyingKey: Groth16VerifyingKey) {
18
+ await _waitUntilReady();
19
+ return garaga.getGroth16CallData(proof, verifyingKey, garaga.CurveId.BN254);
20
+ }
package/src/index.ts ADDED
@@ -0,0 +1,46 @@
1
+ export {
2
+ generateMerkleProof,
3
+ getCommitment,
4
+ hashPrecommitment
5
+ } from "@0xbow/privacy-pools-core-sdk";
6
+
7
+ // Re-export core SDK
8
+ export * from "@0xbow/privacy-pools-core-sdk";
9
+
10
+ // Main SDK class
11
+ export { PrivacyPoolStarknetSDK } from "./sdk.js";
12
+
13
+ // Contract interactions
14
+ export * from "./contracts/index.js";
15
+
16
+ // Types and interfaces - rename conflicting exports
17
+ export type {
18
+ Withdrawal as StarknetWithdrawal,
19
+ RelayData
20
+ } from "./types/entrypoint.js";
21
+
22
+ // Functions from entrypoint types
23
+ export {
24
+ serializeRelayData,
25
+ serializeWithdrawal
26
+ } from "./types/entrypoint.js";
27
+ export * from "./types/conversions.js";
28
+ export * from "./types/garaga.js";
29
+ export * from "./types/snarkjs.js";
30
+
31
+ // Error classes
32
+ export * from "./errors/index.js";
33
+
34
+ // ABIs for contract interactions
35
+ export * from "./abis/index.js";
36
+
37
+ // Utility functions
38
+ export * from "./utils.js";
39
+
40
+ // Constants
41
+ export * from "./constants.js";
42
+
43
+ // Data service
44
+ export {
45
+ StarknetDataService,
46
+ } from "./data.service.js";
package/src/sdk.ts ADDED
@@ -0,0 +1,44 @@
1
+ import { Address } from "viem";
2
+ import { CircuitName, CircuitsInterface, Commitment, CommitmentProof, PrivacyPoolSDK, WithdrawalProof, WithdrawalProofInput } from "@0xbow/privacy-pools-core-sdk";
3
+ import { SNContractInteractionsService } from "./contracts/contracts.service";
4
+ import { withdrawalProofToGaragaCalldata } from "./utils";
5
+ import { RpcProvider, Account as SNAccount } from "starknet";
6
+
7
+ export class PrivacyPoolStarknetSDK extends PrivacyPoolSDK {
8
+ circuits: CircuitsInterface;
9
+
10
+ constructor(circuits: CircuitsInterface) {
11
+ super(circuits);
12
+ this.circuits = circuits;
13
+ }
14
+
15
+ public createSNContractInstance(
16
+ entrypointAddress: Address,
17
+ providerOrAccount: SNAccount | RpcProvider,
18
+ ): SNContractInteractionsService {
19
+ return new SNContractInteractionsService(
20
+ entrypointAddress,
21
+ providerOrAccount
22
+ );
23
+ }
24
+
25
+ async proveWithdrawalSN(commitment: Commitment, input: WithdrawalProofInput): Promise<{ withdrawalProof: WithdrawalProof, calldata: bigint[]; }> {
26
+ const withdrawalProof: WithdrawalProof = await super.proveWithdrawal(commitment, input);
27
+ const vkeyRaw = await this.circuits.getVerificationKey(CircuitName.Withdraw);
28
+ return {
29
+ withdrawalProof,
30
+ calldata: await withdrawalProofToGaragaCalldata(withdrawalProof, vkeyRaw)
31
+ };
32
+ }
33
+
34
+ async proveCommitmentSN(commitment: Commitment): Promise<{ commitmentProof: CommitmentProof, calldata: bigint[]; }> {
35
+ const { preimage: { label, value, precommitment: { nullifier, secret } } } = commitment;
36
+ const commitmentProof: WithdrawalProof = await super.proveCommitment(value, label, nullifier, secret);
37
+ const vkeyRaw = await this.circuits.getVerificationKey(CircuitName.Commitment);
38
+ return {
39
+ commitmentProof,
40
+ calldata: await withdrawalProofToGaragaCalldata(commitmentProof, vkeyRaw)
41
+ };
42
+ }
43
+
44
+ }
@@ -0,0 +1,59 @@
1
+ import { SNFormatError } from "../errors";
2
+ import { G1Point$1, G2Point$1, Groth16Proof, Groth16VerifyingKey } from "./garaga";
3
+ import { SnarkJsGroth16Proof, SnarkJsVkey } from "./snarkjs";
4
+
5
+
6
+ export function G1StoG1G(s: string[]): G1Point$1 {
7
+ const [x, y] = s;
8
+ if (x === undefined || y === undefined)
9
+ throw SNFormatError.conversion(`Invalid G1 point: missing x or y coordinate pair. Input (${x}, ${y})`);
10
+ return {
11
+ x: BigInt(x),
12
+ y: BigInt(y),
13
+ curveId: 0,
14
+ };
15
+ }
16
+
17
+ export function G2StoG2G(s: string[][]): G2Point$1 {
18
+ const [x, y] = s;
19
+
20
+ if (!x || !y) {
21
+ throw SNFormatError.conversion("Invalid G2 point: missing x or y coordinate pair");
22
+ }
23
+
24
+ const [x0, x1] = x;
25
+ const [y0, y1] = y;
26
+
27
+ if (x0 === undefined || x1 === undefined || y0 === undefined || y1 === undefined) {
28
+ throw SNFormatError.conversion(`Invalid G2 point: incomplete coordinate pair. Input ((${x0}, ${x1}), (${y0}, ${y1}))`);
29
+ }
30
+
31
+ return {
32
+ x: [BigInt(x0), BigInt(x1)],
33
+ y: [BigInt(y0), BigInt(y1)],
34
+ curveId: 0,
35
+ };
36
+ }
37
+
38
+ export function snarkJsKeyIntoGaraga(vkey: SnarkJsVkey): Groth16VerifyingKey {
39
+ return {
40
+ alpha: G1StoG1G(vkey.vk_alpha_1),
41
+ beta: G2StoG2G(vkey.vk_beta_2),
42
+ gamma: G2StoG2G(vkey.vk_gamma_2),
43
+ delta: G2StoG2G(vkey.vk_delta_2),
44
+ ic: vkey.IC.map(G1StoG1G),
45
+ };
46
+ }
47
+
48
+ export function snarkJsProofIntoGaraga(
49
+ proof: SnarkJsGroth16Proof,
50
+ publicInputs: string[]
51
+ ): Groth16Proof {
52
+ return {
53
+ a: G1StoG1G(proof.pi_a),
54
+ b: G2StoG2G(proof.pi_b),
55
+ c: G1StoG1G(proof.pi_c),
56
+ publicInputs: publicInputs.map(BigInt),
57
+ curveId: 0,
58
+ };
59
+ }
@@ -0,0 +1,23 @@
1
+ import { BigNumberish, cairo } from "starknet";
2
+
3
+ export interface Withdrawal {
4
+ processor: BigNumberish,
5
+ data: BigNumberish[];
6
+ }
7
+
8
+ export interface RelayData {
9
+ recipient: BigNumberish,
10
+ feeRecipient: BigNumberish,
11
+ relayFeeBPS: BigNumberish,
12
+ }
13
+
14
+ export function serializeRelayData(data: RelayData) {
15
+ const { recipient, feeRecipient, relayFeeBPS } = data;
16
+ const { low, high } = cairo.uint256(relayFeeBPS);
17
+ return [recipient.toString(), feeRecipient.toString(), low.toString(), high.toString()];
18
+ }
19
+
20
+ export function serializeWithdrawal(withdrawal: Withdrawal) {
21
+ const { processor, data } = withdrawal;
22
+ return [processor.toString(), data.length.toString(), ...data.map(x => x.toString())];
23
+ }
@@ -0,0 +1,29 @@
1
+ type CurveId = 0;
2
+
3
+ export interface G1Point$1 {
4
+ x: bigint;
5
+ y: bigint;
6
+ curveId: CurveId;
7
+ }
8
+
9
+ export interface G2Point$1 {
10
+ x: [bigint, bigint];
11
+ y: [bigint, bigint];
12
+ curveId: CurveId;
13
+ }
14
+
15
+ export interface Groth16Proof {
16
+ a: G1Point$1;
17
+ b: G2Point$1;
18
+ c: G1Point$1;
19
+ publicInputs: bigint[];
20
+ curveId?: CurveId;
21
+ }
22
+
23
+ export interface Groth16VerifyingKey {
24
+ alpha: G1Point$1;
25
+ beta: G2Point$1;
26
+ gamma: G2Point$1;
27
+ delta: G2Point$1;
28
+ ic: G1Point$1[];
29
+ }
@@ -0,0 +1,4 @@
1
+ export * from "./conversions.js";
2
+ export * from "./entrypoint.js";
3
+ export * from "./garaga.js";
4
+ export * from "./snarkjs.js";
@@ -0,0 +1,26 @@
1
+ import { type Groth16Proof } from "snarkjs";
2
+
3
+ export type SnarkJsGroth16Proof = Groth16Proof;
4
+
5
+ export interface SnarkJsVkey {
6
+ protocol: 'groth16',
7
+ curve: 'bn128',
8
+ nPublic: number,
9
+ vk_alpha_1: [string, string, '1'],
10
+ vk_beta_2: [
11
+ [string, string],
12
+ [string, string,],
13
+ ['1', '0']
14
+ ],
15
+ vk_gamma_2: [
16
+ [string, string],
17
+ [string, string],
18
+ ['1', '0']
19
+ ],
20
+ vk_delta_2: [
21
+ [string, string],
22
+ [string, string],
23
+ ['1', '0']
24
+ ],
25
+ IC: [string, string, '1'][];
26
+ }
package/src/utils.ts ADDED
@@ -0,0 +1,49 @@
1
+ import { WithdrawalProof } from "@0xbow/privacy-pools-core-sdk";
2
+ import { poseidonHashMany } from "@scure/starknet";
3
+ import { BigNumberish, cairo, num, uint256, Uint256 } from "starknet";
4
+
5
+ import { getGroth16CallData } from "./garaga";
6
+ import { snarkJsKeyIntoGaraga, snarkJsProofIntoGaraga } from "./types/conversions";
7
+ import { serializeWithdrawal, Withdrawal } from "./types/entrypoint";
8
+ import { SnarkJsVkey } from "./types/snarkjs";
9
+ const { toBigInt } = num;
10
+
11
+
12
+ export function computeScope(poolAddress: BigNumberish, chainId: BigNumberish, assetAddress: BigNumberish) {
13
+ return poseidonHashMany([toBigInt(poolAddress), toBigInt(chainId), toBigInt(assetAddress)]);
14
+ }
15
+
16
+ export function computeContext(withdrawal: Withdrawal, scope: BigNumberish) {
17
+ const numberArray = serializeWithdrawal(withdrawal).map(toBigInt);
18
+ const { low, high } = cairo.uint256(scope);
19
+ return poseidonHashMany([...numberArray, toBigInt(low), toBigInt(high)]);
20
+ }
21
+
22
+ export function rawKeyIntoSnarkJs(vkeyRaw: Uint8Array): SnarkJsVkey {
23
+ const vkeyString = (new TextDecoder("utf-8")).decode(vkeyRaw);
24
+ return JSON.parse(vkeyString);
25
+ }
26
+
27
+ export async function withdrawalProofToGaragaCalldata(withdrawalProof: WithdrawalProof, vkeyRaw: Uint8Array) {
28
+ const vkey = rawKeyIntoSnarkJs(vkeyRaw);
29
+ const vkeyGaraga = snarkJsKeyIntoGaraga(vkey);
30
+ const proofGaraga = snarkJsProofIntoGaraga(withdrawalProof.proof, withdrawalProof.publicSignals);
31
+ const calldata = await getGroth16CallData(proofGaraga, vkeyGaraga);
32
+ return calldata.slice(1);
33
+ }
34
+
35
+ export function castBigInt(x: number | bigint | Uint256) {
36
+ if (num.isBigNumberish(x)) {
37
+ return num.toBigInt(x);
38
+ } else {
39
+ return uint256.uint256ToBN(x);
40
+ }
41
+ }
42
+
43
+ export function anyToBigInt(value: BigNumberish | Uint256): bigint {
44
+ // If it's a Uint256 structure with low/high
45
+ if (value && typeof value === 'object' && 'low' in value && 'high' in value) {
46
+ return uint256.uint256ToBN(value);
47
+ }
48
+ return toBigInt(value);
49
+ }