solana-labs / solana-program-library

A collection of Solana programs maintained by Solana Labs
https://solanalabs.com
Apache License 2.0
3.44k stars 2.02k forks source link

Confidential Transfer Support in @solana/spl-token #6338

Open ixmorrow opened 6 months ago

ixmorrow commented 6 months ago

I have been messing with confidential transfers on the client side recently and ran into some issues with the @solana/spl-token API that I'd like to highlight here.

  1. getMintLen() does not return the correct byte size for a mint with the confidential transfer extension.

    • This method seems to be the suggested way for determining how much space to allocate when creating an account with extensions from the client side. Using this function with the ExtensionType.ConfidentialTransferMint type as input returns a size of 267 bytes.
    • 267 bytes does not seem to be the correct size for a mint with the ConfidentialTransfer extension. Attempting to initialize a mint account with 267 bytes allocated will result in an error when you eventually call the InitializeMint instruction on the Token22 program.
    • An InvalidAccountData error is thrown because the program checks that the size of the given account matches exactly what is expected for the given extension.
    • The correct amount of bytes is 235. Allocating a buffer of exactly 235 bytes when creating the account will pass this check.
    • You can even test this by running the commandline example in the Confidential Transfer quick start guide and inspecting the transaction in an explorer.
      spl-token --program-id TokenzQdBNbLqP5VEhdkAS6EPFLC1PHnBqCXEpPxuEb create-token --enable-confidential-transfers auto
    • Here is an example transaction on Devnet. You can see the account is 235 bytes long.
  2. There is virtually no support for any of the Confidential Transfer instructions or any of the relevant types in the @solana/spl-token API. I assume this is intentional, but I have begun implementing the API following the same format as the other Token Extension methods for my own use cases. Which is a bit of a pain so far and I'm not sure if this is completely viable. I am curious why they are not supported yet and if there are plans to implement them?

  3. It seems like the "Configure a token account for confidential transfers" commandline example does not even work in the Confidential Transfers quick start guide.. I get a "Transaction simulation failed: Attempt to load a program that does not exist" RPC error. I'm at least able to invoke the ConfidentialTransfer::ConfigureAccount instruction via typescript on devnet, so I thought this error response was odd. But that might be unrelated.

Commandline RPC Error:

Error: Client(Error { request: Some(SendTransaction), kind: RpcError(RpcResponseError { code: -32002, message: "Transaction simulation failed: Attempt to load a program that does not exist", data: SendTransactionPreflightFailure(RpcSimulateTransactionResult { err: Some(ProgramAccountNotFound), logs: Some([]), accounts: None, units_consumed: Some(0), return_data: None }) }) })

Repo with my code.

samkim-crypto commented 5 months ago

I apologize for the late response on this. The wrong size for the confidential transfer mint and accounts in token-js will be fixed once https://github.com/solana-labs/solana-program-library/pull/6487 is merged.

There has been some delay with supporting confidential transfers in token-js as we plan to compile the zk logic with wasm. We hope to have them all supported within a couple of weeks.

Also, unfortunately, it is currently not possible to run the confidential transfers outside of tests since the zk proof verification program is behind a feature-gate (https://github.com/solana-labs/solana/issues/32613). We are working hard to get the program activated, so please stay tuned.

huckym commented 4 months ago

I am building the token-2022 program from the master branch, locally and running the local validator like this:

solana-test-validator -r \ --bpf-program TokenzQdBNbLqP5VEhdkAS6EPFLC1PHnBqCXEpPxuEb \

/target/deploy/spl_token_2022.so I can create the mint with the confidential transfer extension enabled and can deposit into confidential account, apply pending balance and can verify the encrypted available/pending balances on the explorer fine. transfer and withdraw fail, however, with zkproof errors. Am I missing something in the build process or is something broken? **transfer error:** `+ spl-token transfer --confidential 5WTfhiKQpEoisZmVca29tSKyPFqkDzRxuhoXujm34qQA 10 34DYPAVxjs4trEyHnbBhtKiNttTXzkyeqYusKsCLWTrY Confidential Transfer 10 tokens Sender: H4Ngfi9uMWzdKXd3WS6TbxCGQxhvnrK3pmFo84HrcMPN Recipient: 34DYPAVxjs4trEyHnbBhtKiNttTXzkyeqYusKsCLWTrY Error: Client(Error { request: Some(SendTransaction), kind: RpcError(RpcResponseError { code: -32002, message: "Transaction simulation failed: Error processing Instruction 0: invalid instruction data", data: SendTransactionPreflightFailure(RpcSimulateTransactionResult { err: Some(InstructionError(0, InvalidInstructionData)), logs: Some(["Program ZkTokenProof1111111111111111111111111111111 invoke [1]", "VerifyBatchedRangeProof128", "proof_verification failed: RangeProof(AlgebraicRelation)", "Program ZkTokenProof1111111111111111111111111111111 failed: invalid instruction data"]), accounts: None, units_consumed: Some(200000), return_data: None }) }) })` **withdraw error:** `+ spl-token withdraw-confidential-tokens 5WTfhiKQpEoisZmVca29tSKyPFqkDzRxuhoXujm34qQA 20 Withdrawing 20 confidential tokens Error: Client(Error { request: Some(SendTransaction), kind: RpcError(RpcResponseError { code: -32002, message: "Transaction simulation failed: Error processing Instruction 0: invalid instruction data", data: SendTransactionPreflightFailure(RpcSimulateTransactionResult { err: Some(InstructionError(0, InvalidInstructionData)), logs: Some(["Program ZkTokenProof1111111111111111111111111111111 invoke [1]", "VerifyWithdraw", "proof_verification failed: RangeProof(AlgebraicRelation)", "Program ZkTokenProof1111111111111111111111111111111 failed: invalid instruction data"]), accounts: None, units_consumed: Some(110000), return_data: None }) }) })` @joncinque @samkim-crypto
samkim-crypto commented 4 months ago

Can you verify if the same error happens with the latest spl-token-cli build, which is 3.4.0? These type of errors were reported with 3.3.0, but should be fixed with the latest cli release.

huckym commented 4 months ago

yes, that was indeed the error with 3.3.0. Just built and tried with 3.4.0 and both transfer/withdraw work fine too. Thanks for your quick pointer. Are audit keys supported yet?

samkim-crypto commented 4 months ago

Audit keys are supported in the program, but we have not added the functionality to decrypt yet. That will be coming very soon.

doutv commented 3 weeks ago

spl-token-cli 3.4.1 still cannot work

I run the script here: https://github.com/solana-labs/solana-program-library/blob/master/token/cli/examples/confidential-transfer.sh

+ spl-token configure-confidential-transfer-account --address 29HvjNB2v1iHYQm3na9i3KPDpFsLcFYnhARVp71qwZJC
Error: Client(Error { request: Some(SendTransaction), kind: RpcError(RpcResponseError { code: -32002, message: "Transaction simulation failed: Attempt to load a program that does not exist", data: SendTransactionPreflightFailure(RpcSimulateTransactionResult { err: Some(ProgramAccountNotFound), logs: Some([]), accounts: None, units_consumed: Some(0), return_data: None, inner_instructions: None }) }) })

@huckym Could you take a look at it? Thanks a lot!

joncinque commented 3 weeks ago

@doutv The error says: "Transaction simulation failed: Attempt to load a program that does not exist" -- the zk proof program has not been enabled on any network, so this error is expected. Once the zk proof program is enabled, then it'll work

doutv commented 3 weeks ago

@doutv The error says: "Transaction simulation failed: Attempt to load a program that does not exist" -- the zk proof program has not been enabled on any network, so this error is expected. Once the zk proof program is enabled, then it'll work

Thanks for your reply.

I thought it was enabled since Solana version is 1.18 now.

The status here should be updated: https://spl.solana.com/token-2022/status

image
joncinque commented 1 week ago

My apologies, the info is being updated with https://github.com/solana-labs/solana-program-library/pull/7222