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
1 change: 1 addition & 0 deletions light-sdk-ts/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
"test-transaction": "ts-mocha --resolveJsonModule ./tsconfig.json -t 100000000 tests/transaction.test.ts --exit",
"test-account": "ts-mocha --resolveJsonModule ./tsconfig.json -t 100000000 tests/account.test.ts --exit",
"test-relayer": "ts-mocha --resolveJsonModule ./tsconfig.json -t 100000000 tests/relayer.test.ts --exit",
"test-createOutUtxos": "ts-mocha --resolveJsonModule ./tsconfig.json -t 100000000 tests/createOutUtxos.test.ts --exit",
"build": "yarn tsc",
"format": "prettier --write \"src/**/*.{ts,js}\"",
"lint:fix": "prettier */*.js \"*/**/*{.js,.ts}\" -w",
Expand Down
2 changes: 1 addition & 1 deletion light-sdk-ts/src/account.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ const b2params = { dkLen: 32 };
const ffjavascript = require("ffjavascript");
// @ts-ignore:
import { buildEddsa } from "circomlibjs";

// TODO: add fromPubkeyString()
export class Account {
/**
* Initialize a new shielded account. Generates a random private key if not defined
Expand Down
15 changes: 15 additions & 0 deletions light-sdk-ts/src/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,19 @@ export enum RelayerErrorCode {
RELAYER_RECIPIENT_UNDEFINED = "RELAYER_RECIPIENT_UNDEFINED",
}

export enum CreateUtxoErrorCode {
INVALID_NUMER_OF_RECIPIENTS = "INVALID_NUMER_OF_RECIPIENTS",
INVALID_RECIPIENT_MINT = "INVALID_RECIPIENT_MINT",
RECIPIENTS_SUM_AMOUNT_MISSMATCH = "RECIPIENTS_SUM_AMOUNT_MISSMATCH",
NO_PUBLIC_AMOUNTS_PROVIDED = "NO_PUBLIC_AMOUNTS_PROVIDED_FOR_UNSHIELD",
NO_PUBLIC_MINT_PROVIDED = "NO_PUBLIC_MINT_PROVIDED_FOR_UNSHIELD",
MINT_UNDEFINED = "MINT_UNDEFINED",
SPL_AMOUNT_UNDEFINED = "SPL_AMOUNT_UNDEFINED",
ACCOUNT_UNDEFINED = "ACCOUNT_UNDEFINED",
INVALID_OUTPUT_UTXO_LENGTH = "INVALID_OUTPUT_UTXO_LENGTH",
RELAYER_FEE_DEFINED = "RELAYER_FEE_DEFINED",
PUBLIC_SOL_AMOUNT_UNDEFINED = "PUBLIC_SOL_AMOUNT_UNDEFINED",
}
export enum AccountErrorCode {
INVALID_SEED_SIZE = "INVALID_SEED_SIZE",
SEED_UNDEFINED = "SEED_UNDEFINED",
Expand Down Expand Up @@ -131,3 +144,5 @@ export class VerifierError extends MetaError {}
export class AccountError extends MetaError {}

export class RelayerError extends MetaError {}

export class CreateUtxoError extends MetaError {}
2 changes: 1 addition & 1 deletion light-sdk-ts/src/test-utils/functionalCircuit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ export async function functionalCircuitTest(
senderFee: lightProvider.nodeWallet!.publicKey,
verifier: verifier,
lookUpTable: mockPubkey,
action: Action.DEPOSIT,
action: Action.SHIELD,
poseidon,
});

Expand Down
63 changes: 29 additions & 34 deletions light-sdk-ts/src/transaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,9 @@ export type transactionParameters = {
};

export enum Action {
DEPOSIT = "DEPOSIT",
SHIELD = "SHIELD",
TRANSFER = "TRANSFER",
WITHDRAWAL = "WITHDRAWAL",
UNSHIELD = "UNSHIELD",
}

export type lightAccounts = {
Expand Down Expand Up @@ -176,7 +176,7 @@ export class TransactionParameters implements transactionParameters {
throw new TransactioParametersError(
TransactionParametersErrorCode.NO_ACTION_PROVIDED,
"constructor",
"Define an action either Action.TRANSFER, Action.DEPOSIT,Action.WITHDRAWAL",
"Define an action either Action.TRANSFER, Action.SHIELD,Action.UNSHIELD",
);
}

Expand All @@ -190,23 +190,23 @@ export class TransactionParameters implements transactionParameters {
this.verifier.config.out,
);

if (action === Action.DEPOSIT && senderFee && lookUpTable) {
if (action === Action.SHIELD && senderFee && lookUpTable) {
this.relayer = new Relayer(senderFee, lookUpTable);
} else if (action === Action.DEPOSIT && !senderFee) {
} else if (action === Action.SHIELD && !senderFee) {
throw new TransactioParametersError(
TransactionErrorCode.SOL_SENDER_UNDEFINED,
"constructor",
"Sender sol always needs to be defined because we use it as the signer to instantiate the relayer object.",
);
} else if (action === Action.DEPOSIT && !lookUpTable) {
} else if (action === Action.SHIELD && !lookUpTable) {
throw new TransactioParametersError(
TransactionParametersErrorCode.LOOK_UP_TABLE_UNDEFINED,
"constructor",
"At deposit lookup table needs to be defined to instantiate a relayer object with yourself as the relayer.",
);
}

if (action !== Action.DEPOSIT) {
if (action !== Action.SHIELD) {
if (relayer) {
this.relayer = relayer;
} else {
Expand Down Expand Up @@ -251,7 +251,7 @@ export class TransactionParameters implements transactionParameters {
);

// Checking plausibility of inputs
if (this.action === Action.DEPOSIT) {
if (this.action === Action.SHIELD) {
/**
* No relayer
* public amounts are u64s
Expand All @@ -270,7 +270,7 @@ export class TransactionParameters implements transactionParameters {
throw new TransactioParametersError(
TransactionParametersErrorCode.PUBLIC_AMOUNT_NOT_U64,
"constructor",
"Public amount needs to be a u64 at deposit. Check whether you defined input and output utxos correctly, for a deposit the amounts of output utxos need to be bigger than the amounts of input utxos",
`Public amount sol ${this.publicAmountSol} needs to be a u64 at deposit. Check whether you defined input and output utxos correctly, for a deposit the amounts of output utxos need to be bigger than the amounts of input utxos`,
);
}

Expand All @@ -280,7 +280,7 @@ export class TransactionParameters implements transactionParameters {
throw new TransactioParametersError(
TransactionParametersErrorCode.PUBLIC_AMOUNT_NOT_U64,
"constructor",
"Public amount needs to be a u64 at deposit. Check whether you defined input and output utxos correctly, for a deposit the amounts of output utxos need to be bigger than the amounts of input utxos",
`Public amount spl ${this.publicAmountSpl} needs to be a u64 at deposit. Check whether you defined input and output utxos correctly, for a deposit the amounts of output utxos need to be bigger than the amounts of input utxos`,
);
}
if (!this.publicAmountSol.eq(new BN(0)) && recipientFee) {
Expand Down Expand Up @@ -312,7 +312,7 @@ export class TransactionParameters implements transactionParameters {
"",
);
}
} else if (this.action === Action.WITHDRAWAL) {
} else if (this.action === Action.UNSHIELD) {
/**
* relayer is defined
* public amounts sub FieldSize are negative or 0
Expand Down Expand Up @@ -524,7 +524,7 @@ export class TransactionParameters implements transactionParameters {
);

if (
this.action.toString() === Action.WITHDRAWAL.toString() ||
this.action.toString() === Action.UNSHIELD.toString() ||
this.action.toString() === Action.TRANSFER.toString()
) {
this.accounts.sender = MerkleTreeConfig.getSplPoolPdaToken(
Expand Down Expand Up @@ -565,7 +565,7 @@ export class TransactionParameters implements transactionParameters {
}
}
} else {
if (this.action.toString() !== Action.DEPOSIT.toString()) {
if (this.action.toString() !== Action.SHIELD.toString()) {
throw new TransactioParametersError(
TransactionErrorCode.ACTION_IS_NO_DEPOSIT,
"assignAccounts",
Expand Down Expand Up @@ -615,19 +615,15 @@ export class TransactionParameters implements transactionParameters {
if (inputUtxos) {
inputUtxos.map((utxo) => {
let found = false;
if (
assetPubkeysCircuit.indexOf(utxo.assetsCircuit[1].toString()) !== -1
) {
found = true;
}

for (var i in assetPubkeysCircuit) {
if (
assetPubkeysCircuit[i].toString() ===
utxo.assetsCircuit[1].toString()
) {
found = true;
}

if (!found && utxo.assetsCircuit[1].toString() != "0") {
assetPubkeysCircuit.push(utxo.assetsCircuit[1].toString());
assetPubkeys.push(utxo.assets[1]);
}
if (!found && utxo.assetsCircuit[1].toString() != "0") {
assetPubkeysCircuit.push(utxo.assetsCircuit[1].toString());
assetPubkeys.push(utxo.assets[1]);
}
});
}
Expand All @@ -637,8 +633,7 @@ export class TransactionParameters implements transactionParameters {
let found = false;
for (var i in assetPubkeysCircuit) {
if (
assetPubkeysCircuit[i].toString() ===
utxo.assetsCircuit[1].toString()
assetPubkeysCircuit.indexOf(utxo.assetsCircuit[1].toString()) !== -1
) {
found = true;
}
Expand Down Expand Up @@ -832,7 +827,7 @@ export class Transaction {
this.appParams = appParams;

//TODO: change to check whether browser/node wallet are the same as signing address
if (params.action === Action.DEPOSIT) {
if (params.action === Action.SHIELD) {
let wallet =
this.provider.browserWallet !== undefined
? this.provider.browserWallet
Expand Down Expand Up @@ -1452,7 +1447,7 @@ export class Transaction {
this.params.accounts.recipientFee,
);
}
if (this.params.action === "DEPOSIT") {
if (this.params.action === "SHIELD") {
this.testValues.senderFeeBalancePriorTx = new BN(
await this.provider.provider.connection.getBalance(
this.params.relayer.accounts.relayerPubkey,
Expand Down Expand Up @@ -2080,7 +2075,7 @@ export class Transaction {
}
console.log("nrInstructions ", nrInstructions);

if (this.params.action == "DEPOSIT" && this.testValues.is_token == false) {
if (this.params.action == "SHIELD" && this.testValues.is_token == false) {
var recipientFeeAccountBalance =
await this.provider.provider.connection.getBalance(
this.params.accounts.recipientFee,
Expand Down Expand Up @@ -2112,10 +2107,10 @@ export class Transaction {
.toString() == senderFeeAccountBalance.toString(),
);
} else if (
this.params.action == "DEPOSIT" &&
this.params.action == "SHIELD" &&
this.testValues.is_token == true
) {
console.log("DEPOSIT and token");
console.log("SHIELD and token");

var recipientAccount = await getAccount(
this.provider.provider.connection,
Expand Down Expand Up @@ -2189,7 +2184,7 @@ export class Transaction {
.toString() == senderFeeAccountBalance.toString(),
);
} else if (
this.params.action == "WITHDRAWAL" &&
this.params.action == "UNSHIELD" &&
this.testValues.is_token == false
) {
var relayerAccount = await this.provider.provider.connection.getBalance(
Expand Down Expand Up @@ -2240,7 +2235,7 @@ export class Transaction {
this.testValues.relayerRecipientAccountBalancePriorLastTx?.toString(),
);
} else if (
this.params.action == "WITHDRAWAL" &&
this.params.action == "UNSHIELD" &&
this.testValues.is_token == true
) {
var senderAccount = await getAccount(
Expand Down
1 change: 1 addition & 0 deletions light-sdk-ts/src/utxo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ export const newNonce = () => nacl.randomBytes(nacl.box.nonceLength);
export const N_ASSETS = 2;
export const N_ASSET_PUBKEYS = 3;

// TODO: add static createSolUtxo()
export class Utxo {
/**
* @param {BN[]} amounts array of utxo amounts, amounts[0] is the sol amount amounts[1] is the spl amount
Expand Down
Loading