Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 16 additions & 6 deletions light-circuits/tests/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
VerifierTwo,
Verifier,
VerifierOne,
Action,
} from "light-sdk";
import * as anchor from "@coral-xyz/anchor";
import { assert, expect } from "chai";
Expand Down Expand Up @@ -69,20 +70,29 @@ async function functionalCircuitTest(verifier: Verifier, app: boolean = false) {
outputUtxos: [deposit_utxo1],
merkleTreePubkey: mockPubkey,
sender: mockPubkey,
senderFee: mockPubkey,
senderFee: lightProvider.nodeWallet.publicKey,
verifier: verifier,
lookUpTable: mockPubkey,
action: Action.DEPOSIT,
poseidon
});

let tx = new Transaction({
provider: lightProvider,
});
let tx

// successful proofgeneration
if (app) {
await tx.compile(txParams, { mock: "123" });
tx = new Transaction({
provider: lightProvider,
params: txParams,
appParams: { mock: "123" }
});
} else {
await tx.compile(txParams);
tx = new Transaction({
provider: lightProvider,
params: txParams,
});
}
await tx.compile();

await tx.getProof();
// unsuccessful proofgeneration
Expand Down
89 changes: 86 additions & 3 deletions light-sdk-ts/src/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,95 @@ export class UtxoError extends Error {
name = this.constructor.name;
code: string;
codeMessage: string;
codeStack: string | null;

constructor(code: string, codeMessage: string, codeStack?: string) {
constructor(code: string, codeMessage: string) {
super(`Utxo error ${code}: ${codeMessage}`);
this.code = code;
this.codeMessage = codeMessage;
this.codeStack = codeStack || null;
}
}

export enum ProviderErrorCode {
SOL_MERKLE_TREE_UNDEFINED = "SOL_MERKLE_TREE_UNDEFINED",
ANCHOR_PROVIDER_UNDEFINED = "ANCHOR_PROVIDER_UNDEFINED",
PROVIDER_UNDEFINED = "PROVIDER_UNDEFINED",
}

export enum SolMerkleTreeErrorCode {
MERKLE_TREE_UNDEFINED = "MERKLE_TREE_UNDEFINED",
}
export enum TransactionParametersErrorCode {
NO_VERIFIER_PROVIDED = "NO_VERIFIER_PROVIDED",
NO_POSEIDON_HASHER_PROVIDED = "NO_POSEIDON_HASHER_PROVIDED",
NO_ACTION_PROVIDED = "NO_ACTION_PROVIDED",
PUBLIC_AMOUNT_NEGATIVE = "PUBLIC_AMOUNT_NEGATIVE",
SOL_RECIPIENT_DEFINED = "SOL_RECIPIENT_DEFINED",
SPL_RECIPIENT_DEFINED = "SPL_RECIPIENT_DEFINED",
PUBLIC_AMOUNT_NOT_U64 = "PUBLIC_AMOUNT_NOT_U64",
RELAYER_DEFINED = "RELAYER_DEFINED",
INVALID_PUBLIC_AMOUNT = "INVALID_PUBLIC_AMOUNT",
SOL_SENDER_DEFINED = "SOL_SENDER_DEFINED",
SPL_SENDER_DEFINED = "SPL_SENDER_DEFINED",
PUBLIC_AMOUNT_SPL_NOT_ZERO = "PUBLIC_AMOUNT_SPL_NOT_ZERO",
PUBLIC_AMOUNT_SOL_NOT_ZERO = "PUBLIC_AMOUNT_SOL_NOT_ZERO",
LOOK_UP_TABLE_UNDEFINED = "LOOK_UP_TABLE_UNDEFINED",
}

export enum TransactionErrorCode {
ROOT_INDEX_NOT_FETCHED = "ROOT_INDEX_NOT_FETCHED",
REMAINING_ACCOUNTS_NOT_CREATED = "REMAINING_ACCOUNTS_NOT_CREATED",
TRANSACTION_INPUTS_UNDEFINED = "TRANSACTION_INPUTS_UNDEFINED",
WALLET_RELAYER_INCONSISTENT = "WALLET_RELAYER_INCONSISTENT",
TX_PARAMETERS_UNDEFINED = "TX_PARAMETERS_UNDEFINED",
APP_PARAMETERS_UNDEFINED = "APP_PARAMETERS_UNDEFINED",
RELAYER_UNDEFINED = "TransactionParameters.relayer is undefined",
WALLET_UNDEFINED = "WALLET_UNDEFINED",
NO_UTXOS_PROVIDED = "NO_UTXOS_PROVIDED",
EXCEEDED_MAX_ASSETS = "EXCEEDED_MAX_ASSETS",
VERIFIER_PROGRAM_UNDEFINED = "VERIFIER_PROGRAM_UNDEFINED",
SPL_RECIPIENT_UNDEFINED = "SPL_RECIPIENT_UNDEFINED",
SOL_RECIPIENT_UNDEFINED = "SOL_RECIPIENT_UNDEFINED",
SPL_SENDER_UNDEFINED = "SPL_SENDER_UNDEFINED",
SOL_SENDER_UNDEFINED = "SOL_SENDER_UNDEFINED",
ASSET_PUBKEYS_UNDEFINED = "ASSET_PUBKEYS_UNDEFINED",
ACTION_IS_NO_WITHDRAWAL = "ACTION_IS_NO_WITHDRAWAL",
ACTION_IS_NO_DEPOSIT = "ACTION_IS_NO_DEPOSIT",
INPUT_UTXOS_UNDEFINED = "INPUT_UTXOS_UNDEFINED",
OUTPUT_UTXOS_UNDEFINED = "OUTPUT_UTXOS_UNDEFINED",
GET_MINT_FAILED = "GET_MINT_FAILED",
VERIFIER_UNDEFINED = "VERIFIER_UNDEFINED",
PROOF_INPUT_UNDEFINED = "PROOF_INPUT_UNDEFINED",
NO_PARAMETERS_PROVIDED = "NO_PARAMETERS_PROVIDED",
ROOT_NOT_FOUND = "ROOT_NOT_FOUND",
VERIFIER_CONFIG_UNDEFINED = "VERIFIER_CONFIG_UNDEFINED",
RELAYER_FEE_UNDEFINED = "RELAYER_FEE_UNDEFINED",
ENCRYPTING_UTXOS_FAILED = "ENCRYPTING_UTXOS_FAILED",
GET_INSTRUCTIONS_FAILED = "GET_INSTRUCTIONS_FAILED",
SEND_TRANSACTION_FAILED = "SEND_TRANSACTION_FAILED",
PUBLIC_INPUTS_UNDEFINED = "PUBLIC_INPUTS_UNDEFINED",
MERKLE_TREE_PROGRAM_UNDEFINED = "MERKLE_TREE_PROGRAM_UNDEFINED",
INPUT_UTXO_NOT_INSERTED_IN_MERKLE_TREE = "INPUT_UTXO_NOT_INSERTED_IN_MERKLE_TREE",
INVALID_PROOF = "INVALID_PROOF",
POSEIDON_HASHER_UNDEFINED = "POSEIDON_HASHER_UNDEFINED",
}

export class MetaError extends Error {
code: string;
codeMessage?: string;
functionName: string;

constructor(code: string, functionName: string, codeMessage?: string) {
super(`${code}: ${codeMessage}`);

this.codeMessage = codeMessage;
this.code = code;
this.functionName = functionName;
}
}

/**
* @description Thrown when something fails in the Transaction class.
**/
export class TransactionError extends MetaError {}

export class TransactioParametersError extends MetaError {}
121 changes: 41 additions & 80 deletions light-sdk-ts/src/idls/merkle_tree_program.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,73 +54,53 @@ export type MerkleTreeProgram = {
},
{
name: "AUTHORITY_SEED";
type: {
defined: "&[u8]";
};
value: 'b"AUTHORITY_SEED"';
type: "bytes";
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see those changes being made back and forth (from array syntax to b"" strings and back). Any idea what causes that? Typescript/node version?

If you have no idea, I will try to find out. :)

It's quite annoying to have these all the time - it's quite distracting while reviewing or rebasing.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@vadorovsky seems like the differences in idl layout were due to my using anchor 0.25.0.

using the latest anchor v 0.27.0 resolves the issue

value: "[65, 85, 84, 72, 79, 82, 73, 84, 89, 95, 83, 69, 69, 68]";
},
{
name: "MERKLE_TREE_AUTHORITY_SEED";
type: {
defined: "&[u8]";
};
value: 'b"MERKLE_TREE_AUTHORITY"';
type: "bytes";
value: "[77, 69, 82, 75, 76, 69, 95, 84, 82, 69, 69, 95, 65, 85, 84, 72, 79, 82, 73, 84, 89]";
},
{
name: "TREE_ROOT_SEED";
type: {
defined: "&[u8]";
};
value: 'b"TREE_ROOT_SEED"';
type: "bytes";
value: "[84, 82, 69, 69, 95, 82, 79, 79, 84, 95, 83, 69, 69, 68]";
},
{
name: "STORAGE_SEED";
type: {
defined: "&[u8]";
};
value: 'b"storage"';
type: "bytes";
value: "[115, 116, 111, 114, 97, 103, 101]";
},
{
name: "LEAVES_SEED";
type: {
defined: "&[u8]";
};
value: 'b"leaves"';
type: "bytes";
value: "[108, 101, 97, 118, 101, 115]";
},
{
name: "NULLIFIER_SEED";
type: {
defined: "&[u8]";
};
value: 'b"nf"';
type: "bytes";
value: "[110, 102]";
},
{
name: "POOL_TYPE_SEED";
type: {
defined: "&[u8]";
};
value: 'b"pooltype"';
type: "bytes";
value: "[112, 111, 111, 108, 116, 121, 112, 101]";
},
{
name: "POOL_CONFIG_SEED";
type: {
defined: "&[u8]";
};
value: 'b"pool-config"';
type: "bytes";
value: "[112, 111, 111, 108, 45, 99, 111, 110, 102, 105, 103]";
},
{
name: "POOL_SEED";
type: {
defined: "&[u8]";
};
value: 'b"pool"';
type: "bytes";
value: "[112, 111, 111, 108]";
},
{
name: "TOKEN_AUTHORITY_SEED";
type: {
defined: "&[u8]";
};
value: 'b"spl"';
type: "bytes";
value: "[115, 112, 108]";
},
];
instructions: [
Expand Down Expand Up @@ -1248,73 +1228,54 @@ export const IDL: MerkleTreeProgram = {
},
{
name: "AUTHORITY_SEED",
type: {
defined: "&[u8]",
},
value: 'b"AUTHORITY_SEED"',
type: "bytes",
value: "[65, 85, 84, 72, 79, 82, 73, 84, 89, 95, 83, 69, 69, 68]",
},
{
name: "MERKLE_TREE_AUTHORITY_SEED",
type: {
defined: "&[u8]",
},
value: 'b"MERKLE_TREE_AUTHORITY"',
type: "bytes",
value:
"[77, 69, 82, 75, 76, 69, 95, 84, 82, 69, 69, 95, 65, 85, 84, 72, 79, 82, 73, 84, 89]",
},
{
name: "TREE_ROOT_SEED",
type: {
defined: "&[u8]",
},
value: 'b"TREE_ROOT_SEED"',
type: "bytes",
value: "[84, 82, 69, 69, 95, 82, 79, 79, 84, 95, 83, 69, 69, 68]",
},
{
name: "STORAGE_SEED",
type: {
defined: "&[u8]",
},
value: 'b"storage"',
type: "bytes",
value: "[115, 116, 111, 114, 97, 103, 101]",
},
{
name: "LEAVES_SEED",
type: {
defined: "&[u8]",
},
value: 'b"leaves"',
type: "bytes",
value: "[108, 101, 97, 118, 101, 115]",
},
{
name: "NULLIFIER_SEED",
type: {
defined: "&[u8]",
},
value: 'b"nf"',
type: "bytes",
value: "[110, 102]",
},
{
name: "POOL_TYPE_SEED",
type: {
defined: "&[u8]",
},
value: 'b"pooltype"',
type: "bytes",
value: "[112, 111, 111, 108, 116, 121, 112, 101]",
},
{
name: "POOL_CONFIG_SEED",
type: {
defined: "&[u8]",
},
value: 'b"pool-config"',
type: "bytes",
value: "[112, 111, 111, 108, 45, 99, 111, 110, 102, 105, 103]",
},
{
name: "POOL_SEED",
type: {
defined: "&[u8]",
},
value: 'b"pool"',
type: "bytes",
value: "[112, 111, 111, 108]",
},
{
name: "TOKEN_AUTHORITY_SEED",
type: {
defined: "&[u8]",
},
value: 'b"spl"',
type: "bytes",
value: "[115, 112, 108]",
},
],
instructions: [
Expand Down
9 changes: 4 additions & 5 deletions light-sdk-ts/src/merkleTree/solMerkleTree.ts
Original file line number Diff line number Diff line change
Expand Up @@ -189,19 +189,18 @@ export class SolMerkleTree {
poseidon,
leaves,
);
const tmpMtFetchedRoots: any = mtFetched.roots;

if (
Array.from(
leInt2Buff(unstringifyBigInts(fetchedMerkleTree.root()), 32),
).toString() !=
tmpMtFetchedRoots[mtFetched.currentRootIndex.toNumber()].toString()
// @ts-ignore: unknown type error
).toString() != mtFetched.roots[mtFetched.currentRootIndex].toString()
) {
throw new Error(
`building merkle tree from chain failed: root local ${Array.from(
leInt2Buff(unstringifyBigInts(fetchedMerkleTree.root()), 32),
).toString()} != root fetched ${
tmpMtFetchedRoots[mtFetched.currentRootIndex.toNumber()]
// @ts-ignore: unknown type error
mtFetched.roots[mtFetched.currentRootIndex]
}`,
);
}
Expand Down
11 changes: 11 additions & 0 deletions light-sdk-ts/src/test-utils/createAccounts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -330,6 +330,17 @@ export async function createTestAccounts(
} catch (error) {
console.log(error);
}
console.log("userSplAccount ", userSplAccount);

console.log(
"funded account",
await getAccount(
connection,
userSplAccount!, //userTokenAccount,
"confirmed",
TOKEN_PROGRAM_ID,
),
);

try {
if (balanceUserToken == null) {
Expand Down
Loading