Skip to main content
Version: 19.0.0

Sapling Toolkit

Sapling is a protocol allowing private transactions in a decentralized environment. Sapling was introduced in Tezos in the Edo protocol. Refer to the Tezos documentation for more information on Sapling: https://tezos.gitlab.io/active/sapling.html

Keys

Spending key

The spending key is used to spend tokens. It must be handled securely to prevent funds from being lost or stolen.

Taquito offers support for encrypted/unencrypted spending keys and mnemonics. Refer to the following link for more information: InMemorySpendingKey

Proving key

The proving key can be used to generate proof without allowing tokens to be spent. Zero-knowledge proofs can be created with either the spending or the proving key. In cases where the device holding the spending key is computationally or memory-limited, such as a hardware wallet, proofs can be produced on a separate device using the proving key.

Viewing key

The viewing key is derived from the spending key. This key can be used to view all incoming and outgoing transactions. It must be handled securely to prevent a loss of privacy, as anyone accessing it can see the transaction history and the balance.

Refer to the following link for more information: InMemoryViewingKey

Sapling address

Sapling addresses are used to receive tokens. They are derived from the viewing key.

Here is an example on how to retrieve addresses: InMemoryViewingKey

Sapling toolkit

The @mavrykdynamics/taquito-sapling package provides a SaplingToolkit class that surfaces all of the Sapling capabilities, allowing it to read from a Sapling state and prepare transactions.

The constructor of the SaplingToolkit takes the following properties:

  • the first parameter is an object containing:
    • a saplingSigner property, an instance of InMemorySpendingKey as the spending key is needed to prepare and sign transactions that spend tokens.
    • an optional saplingProver property which can be an instance of InMemoryProvingKey if you want to generate the proofs from a proving key rather than the spending key.
  • the second parameter is an object containing:
    • the address of the Sapling contract (string)
    • the size of the memo of the corresponding Sapling contract (number)
    • an optional Sapling id that must be specified if the contract contains more than one Sapling state.
  • an instance of a class implementing the TzReadProvider interface, which allows getting data from the blockchain
  • it is possible to specify a different packer than the MichelCodecPacker, which is used by default

Here is an example of how to instantiate a SaplingToolkit:

import { TezosToolkit, RpcReadAdapter } from '@mavrykdynamics/taquito';
import { SaplingToolkit } from '@mavrykdynamics/taquito-sapling';
import { RpcClient } from '@mavrykdynamics/taquito-rpc';
const tezos = new TezosToolkit('https://rpc.mavryk,network/basenet/');
const readProvider = new RpcReadAdapter(new RpcClient('https://YOUR_PREFERRED_RPC_URL'));
const saplingContract = await tezos.contract.at('KT1ToBD7bovonshNrxs3i4KMFuZ8PE2LUmQf');
const inMemorySpendingKey = await InMemorySpendingKey.fromMnemonic('YOUR_MNEMONIC');
const saplingToolkit = new SaplingToolkit(
{ saplingSigner: inMemorySpendingKey },
{ contractAddress: saplingContract.address, memoSize: 8 },
readProvider
)

How to retrieve my balance in the Sapling shielded pool?

When calling the getSaplingTransactionViewer method of the SaplingToolkit class, an instance of the SaplingTransactionViewer class is returned. The SaplingTransactionViewer class allows retrieving and decrypting Sapling transactions for the specified viewing key and calculating the unspent balance.

For each entry in the shielded pool, the SaplingTransactionViewer class will try to decrypt them using the viewing key as if it were the receiver. If a ciphertext is successfully decrypted, the configured account was the receiver of the output. The SaplingTransactionViewer will find which inputs were not spent by computing their nullifier. If an input is spent, its nullifier will be in the Sapling state. If the nullifier is not present, the input has not been spent, and its value will be considered in the calculated balance.

Note that the balance is represented in mumav.

The balance can be retrieved as follows:

Live Editor
Result

How to retrieve my transaction history?

The SaplingTransactionViewer class exposes a method called getIncomingAndOutgoingTransactions which allows decrypting the transactions received and sent based on the viewing key. Information like the value in mumav, the memo or the payment address can be retrieved as well as if the input is spent for the incoming ones.

Example:

Live Editor
Result

How to prepare a shielded transaction?

A shielded transaction allows sending tokens from a Tezos account (mv1, mv2, mv3) to a Sapling address (zet). The prepareShieldedTransaction method of the SaplingToolkit takes an array of ParametersSaplingTransaction, making it possible to send tez to multiple addresses at once if needed.

The ParametersSaplingTransaction is an object made of:

  • a to property, which is the destination address (zet)
  • an amount property, which is the amount to shield in tez by default
  • an optional memo that cannot be longer than the specified memo size
  • an optional mumav property that must be set to true if the specified amount is in mumav rather than tez

The prepareShieldedTransaction method returns the crafted Sapling transaction parameter but does not perform any change on the shielded pool. A subsequent step where the Sapling transaction parameter is submitted to the smart contract must be done. Note that in a case of a shielded transaction, the shielded amount must be sent along when calling the smart contract to transfer the tez to the shielded pool, or it will result in an error.

Here is an example of how to prepare and inject a shielded transaction using Taquito:

Live Editor
Result

How to prepare a Sapling transaction?

A Sapling transaction allows sending tokens from an address (zet) to an address (zet). The prepareSaplingTransaction method of the SaplingToolkit takes an array of ParametersSaplingTransaction, making it possible to send tez to multiple addresses at once if needed.

The ParametersSaplingTransaction is an object made of:

  • a to property, which is the destination address (zet)
  • an amount property, which is the amount to shield in tez by default
  • an optional memo that cannot be longer than the specified memo size
  • an optional mumav property that must be set to true if the specified amount is in mumav rather than tez

The prepareSaplingTransaction method returns the crafted Sapling transaction parameter but does not perform any change on the shielded pool. A subsequent step where the Sapling transaction parameter is submitted to the smart contract must be done.

note

A user should not use their own implicit account (mv1, mv2, mv3) to submit a Sapling transaction but rather have a third party inject it.

Here is an example of how to prepare and inject a Sapling transaction using Taquito:

Live Editor
Result

How to prepare an unshielded transaction?

An unshielded transaction allows sending tokens from an address (zet) to a Tezos address (mv1, mv2, mv3). The prepareUnshieldedTransaction method of the SaplingToolkit takes a single ParametersUnshieldedTransaction.

The ParametersUnshieldedTransaction is an object made of:

  • a to property, which is the destination account (mv1, mv2, mv3)
  • an amount property, which is the amount to shield in tez by default
  • an optional mumav property that must be set to true if the specified amount is in mumav rather than tez

The prepareUnshieldedTransaction method returns the crafted Sapling transaction parameter but does not perform any change on the shielded pool. A subsequent step where the Sapling transaction parameter is submitted to the smart contract must be done to retrieve the tokens from the pool.

Here is an example of how to prepare and inject an unshielded transaction using Taquito:

Live Editor
Result

SaplingTransactionViewer

We don't require the spending key to retrieve the balance and transaction history. It can be done using the viewing key and the SaplingTransactionViewer class.

The constructor of the SaplingTransactionViewer takes the following properties:

  • an instance of InMemoryViewingKey
  • the second parameter is an object containing:
    • the address of the Sapling contract or a Sapling id if the contract contains more than one Sapling state.
  • an instance of a class implementing the TzReadProvider interface, which allows getting data from the blockchain

Here is an example of how to instantiate a SaplingTransactionViewer:

import { TezosToolkit, RpcReadAdapter } from '@mavrykdynamics/taquito';
import { InMemoryViewingKey } from '@mavrykdynamics/taquito-sapling';
import { RpcClient } from '@mavrykdynamics/taquito-rpc';
const readProvider = new RpcReadAdapter(new RpcClient('https://YOUR_PREFERRED_RPC_URL'));
const tezos = new TezosToolkit('https://rpc.mavryk,network/basenet/');
const saplingContract = await tezos.contract.at('KT1ToBD7bovonshNrxs3i4KMFuZ8PE2LUmQf');
const inMemoryViewingKey = new InMemoryViewingKey(
'000000000000000000977d725fc96387e8ec1e603e7ae60c6e63529fb84e36e126770e9db9899d7f2344259fd700dc80120d3c9ca65d698f6064043b048b079caa4f198aed96271740b1d6fd523d71b15cd0b3d75644afbe9abfedb6883e299165665ab692c14ca5c835c61a0e53de553a751c78fbc42d5e7eca807fd441206651c84bf88de803efba837583145a5f338b1a7af8a5f9bec4783054f9d063d365f2352f72cbced95e0a'
);
const saplingTransactionViewer = new SaplingTransactionViewer(
inMemoryViewingKey,
{ contractAddress: saplingContract.address },
readProvider
)

Refer to these sections to retrieve the balance and view the transaction history.

Provide detailed feedback