How to check deposit from users

Hi guys!
I have a system app.
I want to receive zcash from users and move them to the zcash master address.
So, I have a master zcash address and this is the main flow:

  1. If the user request a deposit, the system will gen a new temp address (use z_getnewaddress function and sapling format).
  2. Then the user send some zcash to the temp address.
  3. Then the system (cron job) will check the balance of the temp address (use z_getbalance function).
  4. The system moves all balances of the temp address to the master address.
  5. Bla bla…
  6. Transfer: send to the user: use the method: sendtoaddress or z_sendmany?

So everything is ok for me?
And how do I move all balances from temp address to master address?
Is there a way to move them quickly? (do not use the transfer function) .
Thank you so much!

hello! Can you help!

To move ZEC to a new address you will need to send like any other transaction.

z_sendmany

1 Like

This is going to quickly cause a big slowdown in your node, because z_getnewaddress gets a new spending key, and the speed of node scanning is linear in the number of spending keys.

For your given workflow, I would instead recommend generating diversified addresses. This allows you to generate multiple payment addresses backed by the same spending key. You would generate a diversified address for each user (could be one per user, or a fresh one every time), and then when a user sends you funds, you can detect which address the funds came from (to determine which user sent the funds).

With diversified addresses, there is no need to move funds to the master address, because the funds are already there :slight_smile:

zcashd doesn’t support generating diversified addresses, but it does support receiving funds from them. So what I’d recommend is that you export your Sapling full viewing key from zcashd, and use the Rust libraries to generate diversified addresses from it to give to users.

2 Likes

GOOOOOOD!
You are very best. Thank you so so so much!

Do you know the libs write by GOLANG? or the full code to gen diversified address by rust language (I don’t know rust language)

I’ve put together an example Rust binary showing how to do this:

2 Likes

I have 3 cmd, but can’t gen a diversified address from the viewing key.
the error: Invalid mainnet Sapling viewing key
But I want to gen a testnet diversified address
Can you check for me? Many many thanks!

1. list address:

./src/zcash-cli z_listaddresses
[
  "ztQYRe97Xu3o86VDKjPQRYNL6XwU5EChvUNkNSS4dXo6GwRm8FtBs1BL2PcPwxrGFd8cSjHbJdwW3nabRd4YDKyy3Vn3MZ4",
  "ztestsapling1xjqv349c4hk0625j975g5fcyy6eyezxucxyztaga0eqznww9jesd8a9nzwjgy8mqwa9g5u4dpht",
  "ztestsapling1t4cemg84hzjyh466qru7gfdusd3f53639g5g4t4xxz9gpdza2z9mv6n39gw83kkdzhcw7s44xpv",
  "ztestsapling13w57wcx9446pef2t2l292hje8yw8vhy2pc4rz23wk3qt7pxl0a8p4tfc7scqdr00ay5kvkvlef4",
  "ztestsapling14f874qp8a0fu74rwjgfwwt57tv89fu6345zdatp9k9re3adz3jhptaxqwyxdzjxqp72uyr8m54m"
]

2. export viewing key:

./src/zcash-cli z_exportviewingkey ztestsapling14f874qp8a0fu74rwjgfwwt57tv89fu6345zdatp9k9re3adz3jhptaxqwyxdzjxqp72uyr8m54m

zxviewtestsapling1qv9ufts9qqqqpq94gjtgvss3065thw4e8sftpa4q20r9zjw8rafr0cyqnm6cxvu5l907cq9mgxdcvqvlcpjfmzkp46870yeu4w2e2fv66fs3udzasy6utye99a50ndskpks65y6mugcyq3n3ee5sedazejn00dx8yev8lqaw6cq2hr7m3wwmazfak3czl0eehjpdp0y2urrs7fj7zha4ahhz202qrlmzh0h2sep7v24f7v0l8pnrlf9thn5sc6dyvj75zl3quh792nc4jjcka

3. gen diversified:

cargo run --example diversify-address -- zxviewtestsapling1qv9ufts9qqqqpq94gjtgvss3065thw4e8sftpa4q20r9zjw8rafr0cyqnm6cxvu5l907cq9mgxdcvqvlcpjfmzkp46870yeu4w2e2fv66fs3udzasy6utye99a50ndskpks65y6mugcyq3n3ee5sedazejn00dx8yev8lqaw6cq2hr7m3wwmazfak3czl0eehjpdp0y2urrs7fj7zha4ahhz202qrlmzh0h2sep7v24f7v0l8pnrlf9thn5sc6dyvj75zl3quh792nc4jjcka 0

**Compiling** zcash_client_backend v0.4.0 (/Users/Downloads/librustzcash-master/zcash_client_backend)

**Finished** dev [unoptimized + debuginfo] target(s) in 3.12s

**Running** `/Users/Downloads/librustzcash-master/target/debug/examples/diversify-address zxviewtestsapling1qv9ufts9qqqqpq94gjtgvss3065thw4e8sftpa4q20r9zjw8rafr0cyqnm6cxvu5l907cq9mgxdcvqvlcpjfmzkp46870yeu4w2e2fv66fs3udzasy6utye99a50ndskpks65y6mugcyq3n3ee5sedazejn00dx8yev8lqaw6cq2hr7m3wwmazfak3czl0eehjpdp0y2urrs7fj7zha4ahhz202qrlmzh0h2sep7v24f7v0l8pnrlf9thn5sc6dyvj75zl3quh792nc4jjcka 0`

/Users//Downloads/librustzcash-master/target/debug/examples/diversify-address: invalid argument to option `viewing_key`: Invalid mainnet Sapling viewing key

There was a bug in my example that meant it only worked for mainnet viewing keys :sweat_smile:

Fixed in zcash_client_backend: Fix testnet bug in diversify-address example by str4d · Pull Request #327 · zcash/librustzcash · GitHub

3 Likes

woa, it works for me. Thank you I love you.
ah how to build this to an execute file.
ex:
cargo run --example diversify-address --<viewing_key> <index>
to:
./gen_diversify_address --<viewing_key> <index>

cargo run just calls cargo build and then runs the resulting binary. To get the binary yourself:

cargo build --example diversify-address --release

and then the binary should be at target/release/examples/diversify-address.

2 Likes

love you so much!

hi :slight_smile:
I builded and run it for the testnet.
But the index of address I set different with the result, you can see some ex:

./diversify-address – zxviewtestsapling1qv9ufts9qqqqpq94gjtgvss3065thw4e8sftpa4q20r9zjw8rafr0cyqnm6cxvu5l907cq9mgxdcvqvlcpjfmzkp46870yeu4w2e2fv66fs3udzasy6utye99a50ndskpks65y6mugcyq3n3ee5sedazejn00dx8yev8lqaw6cq2hr7m3wwmazfak3czl0eehjpdp0y2urrs7fj7zha4ahhz202qrlmzh0h2sep7v24f7v0l8pnrlf9thn5sc6dyvj75zl3quh792nc4jjcka 0
result:

Diversifier index: 3

ztestsapling14f874qp8a0fu74rwjgfwwt57tv89fu6345zdatp9k9re3adz3jhptaxqwyxdzjxqp72uyr8m54m

./diversify-address – zxviewtestsapling1qv9ufts9qqqqpq94gjtgvss3065thw4e8sftpa4q20r9zjw8rafr0cyqnm6cxvu5l907cq9mgxdcvqvlcpjfmzkp46870yeu4w2e2fv66fs3udzasy6utye99a50ndskpks65y6mugcyq3n3ee5sedazejn00dx8yev8lqaw6cq2hr7m3wwmazfak3czl0eehjpdp0y2urrs7fj7zha4ahhz202qrlmzh0h2sep7v24f7v0l8pnrlf9thn5sc6dyvj75zl3quh792nc4jjcka 1
result:

Diversifier index: 3

ztestsapling14f874qp8a0fu74rwjgfwwt57tv89fu6345zdatp9k9re3adz3jhptaxqwyxdzjxqp72uyr8m54m

./diversify-address – zxviewtestsapling1qv9ufts9qqqqpq94gjtgvss3065thw4e8sftpa4q20r9zjw8rafr0cyqnm6cxvu5l907cq9mgxdcvqvlcpjfmzkp46870yeu4w2e2fv66fs3udzasy6utye99a50ndskpks65y6mugcyq3n3ee5sedazejn00dx8yev8lqaw6cq2hr7m3wwmazfak3czl0eehjpdp0y2urrs7fj7zha4ahhz202qrlmzh0h2sep7v24f7v0l8pnrlf9thn5sc6dyvj75zl3quh792nc4jjcka 2

result:

Diversifier index: 3

ztestsapling14f874qp8a0fu74rwjgfwwt57tv89fu6345zdatp9k9re3adz3jhptaxqwyxdzjxqp72uyr8m54m

hoangphuong@PhuongMacBookPro examples % ./diversify-address – zxviewtestsapling1qv9ufts9qqqqpq94gjtgvss3065thw4e8sftpa4q20r9zjw8rafr0cyqnm6cxvu5l907cq9mgxdcvqvlcpjfmzkp46870yeu4w2e2fv66fs3udzasy6utye99a50ndskpks65y6mugcyq3n3ee5sedazejn00dx8yev8lqaw6cq2hr7m3wwmazfak3czl0eehjpdp0y2urrs7fj7zha4ahhz202qrlmzh0h2sep7v24f7v0l8pnrlf9thn5sc6dyvj75zl3quh792nc4jjcka 3

Diversifier index: 3

ztestsapling14f874qp8a0fu74rwjgfwwt57tv89fu6345zdatp9k9re3adz3jhptaxqwyxdzjxqp72uyr8m54m


./diversify-address – zxviewtestsapling1qv9ufts9qqqqpq94gjtgvss3065thw4e8sftpa4q20r9zjw8rafr0cyqnm6cxvu5l907cq9mgxdcvqvlcpjfmzkp46870yeu4w2e2fv66fs3udzasy6utye99a50ndskpks65y6mugcyq3n3ee5sedazejn00dx8yev8lqaw6cq2hr7m3wwmazfak3czl0eehjpdp0y2urrs7fj7zha4ahhz202qrlmzh0h2sep7v24f7v0l8pnrlf9thn5sc6dyvj75zl3quh792nc4jjcka 100

Diversifier index: 102

ztestsapling1qct4jge6tq89cvgahgu4knypgas6x5kqlhswq48l2y4k09nst5fxn5m82c46exr25f5csjw5chj

Yes, this is expected: not every diversifier index results in a valid Sapling address (on average half of the 288 diversifiers will be invalid).

The example tool starts searching for the next Sapling address starting from the index you provide, and it returns the closest index for which it could find a valid address. You will need to account for this in your code.

2 Likes

Ok I see, thank you!

hi, so I user want to withdraw, I will send some zec to the user (one user not many user).
what’s the method I should use?

sendtoaddress

or

z_sendmany

thank you!

./src/zcash-cli sendtoaddress ztestsapling1e7ncn23xnh5av9rsag5zyvn8gv6akzgl0nx9m6p5gxe27vdlx7mcquc6dd6djsyhqz4ucmkn35j 0.01 “xxx” “yyy” true

I have an error:
Invalid Zcash address

hi @str4d
I have some code to transfer, but happen error, can you help me to check?

   ./src/zcash-cli getaccountaddress ""
tmFzUb6c8cP9kBR5w2L9vSZ2uMN2sgy5m5P

-------

./src/zcash-cli getbalance ""
0.12687863

------------------

./src/zcash-cli z_sendmany "tmFzUb6c8cP9kBR5w2L9vSZ2uMN2sgy5m5P" '[{"address": "ztestsapling1e7ncn23xnh5av9rsag5zyvn8gv6akzgl0nx9m6p5gxe27vdlx7mcquc6dd6djsyhqz4u
cmkn35j", "amount": 0.0001}]'
opid-8a715bef-ddf4-4a17-bdf3-b5dab15dc5ab

-------
./src/zcash-cli z_getoperationstatus '["opid-8a715bef-ddf4-4a17-bdf3-b5dab15dc5ab"]'
[
  {
    "id": "opid-8a715bef-ddf4-4a17-bdf3-b5dab15dc5ab",
    "status": "failed",
    "creation_time": 1611126515,
    "error": {
      "code": -6,
      "message": "Insufficient transparent funds, no UTXOs found for taddr from address."
    },