Safe Private Transfer

Safe way to make a private transfer without risking losing your funds

To make sure that you will not lose your funds because of any instability on your machine, use this pattern:

import { TyphoonSDK } from 'typhoon-sdk'
import { RpcProvider, Account } from 'starknet';
import * as fs from 'fs';
import { readFile } from "fs/promises";

const provider = new RpcProvider({ nodeUrl: "https://starknet-mainnet.public.blastapi.io/rpc/v0_8" });
const sdk = new TyphoonSDK()
const STRK_ADDR = "0x04718f5a0fc34cc1af16a1cdee98ffb20c31f5cd61d6ab07201858f4287c938d"
const amount = 110000000000000000000n // 110 STRK

// use the wallet account if you run in your frontend
const account = new Account(provider, "0xyouraddress", "0xprivkey"); 

// this function generates the calls to make a deposit
let calls = await sdk.generate_approve_and_deposit_calls(amount, STRK_ADDR)

// execute the deposit calls
const multiCall = await account.execute(calls);
// get the data for withdraw as json
const jsonString = JSON.stringify(
    {   "secrets": sdk.get_secrets(), 
        "nullifiers": sdk.get_nullifiers(), 
        "pools": sdk.get_pools(), 
        "txHash": multiCall.transaction_hash 
    }, null, 2); // The third argument adds indentation for readability

// Write the JSON string to a file
fs.writeFile('deposit-data.json', jsonString, 'utf8', (err) => {
        if (err) {
            console.error('An error occurred while writing JSON Object to File:', err);
       } else {
            console.log('JSON file has been saved.');
       }
});

await account.waitForTransaction(multiCall.transaction_hash);

The code above allows you to save the data needed for the withdraw, it means that the withdraw could be executed latter, like in the code bellow:

import {TyphoonSDK} from 'typhoon-sdk'
import { readFile } from "fs/promises";

const jsonStrdata = await readFile('./deposit-data.json', "utf8");
let data = JSON.parse(data);
sdk.init(data.secrets, data.nullifiers, data.pools)
await sdk.withdraw(data.txHash, [recipient_addr]) 

Last updated