0

We are designing a blockchain game, currently we have encountered some challenges in the wallet and interaction with the smart contract and token transfer that I would like to consult you about.

First part: making and managing the wallet

The game has an internal wallet, in fact, if I want to explain the whole boter, when the user registers in the game, a request to create a wallet is sent to the API of the project, which plays the role of the interface between the user and Web 3, like this The CreateWallet function is called in the API and randomly generates a GUID called recovery ID and uses this ID to create a wallet, then returns the recovery ID along with the private key and wallet address to the user. The recovery ID is shown to the user only once and the user is told to save it in a safe place and be diligent in protecting it, from this ID to recover the wallet when the user deletes the history of his device or the game from It is used to delete on his mobile phone

Our first challenge in this section is whether this method is optimal for making an in-game wallet or not? Our second challenge is how to store the private key, should we store the private key in the user's phone or in the database? Our third challenge in this section is what encryption algorithm to use for private key encryption and what parameters to use for encryption key and IV values?

In addition, I would like to point out that because the game users are generally not familiar with the blockchain world, that is why we want to use the internal wallet so that the users do not get involved in creating an external wallet and connecting it to the game in order to avoid the complexities of this task. to give a better experience to the game user.

Second part: Transaction fees

Due to the fact that almost any interaction with the blockchain in a way that has an impact on the blockchain requires payment of a fee, because the game and its smart contracts are based on the BNB smart chain, users must charge their wallets with BNB currency to be able to spend pay their fees,

The first challenge of this section is whether it makes sense for blockchain games that users pay their own fees or should the company pay the fees?

What if the user does not have enough BNB to pay the fee? Should we show an error to the user to increase his BNB balance or in this case, the company should pay the fee?

And the last challenge of this section is whether to embed a button in the game through which the user can withdraw all his BNB balance and transfer it to another wallet. There is no a balance to pay the fee, is it reasonable to reduce the estimated amount of gas from the balance amount and transfer the remaining amount so that the fee can be paid? In this case, some BNB currency will be blocked forever in the wallet, is this method reasonable?

Third part: How to pay for gas by the company

If the best decision for all transactions, or in certain cases such as when the user does not have enough balance to pay the transaction fee, is for the company to bear the cost of gas, how should this be done?

The solution that comes to my mind is to ask the user to allow the company's smart contract or wallet to execute some transactions and transfers on behalf of the user after the user is registered and after the wallet is created.

In this method, the user must transfer some BNB currency to his own wallet, then the Approve function in the smart contract is called to allow the smart contract or company wallet to perform transactions on behalf of the user and use BNB currency for payment. Approve transaction gas is used.

In this case, for all transactions or in some cases, the company's smart contract or wallet will mediate the execution of transactions, and the cost of gas will be deducted from the company's BNB balance. Is this method reasonable?

The challenge of this method is that the cost of gas may be more than the balance of the company's wallet or smart contract, what should be done in this case?

Or if we want to use the smart contract as an intermediary for gas payment, it is not possible to deposit BNB currency into the smart contract, what should be done in this case?

I would like to thank my dear friends who guide me, to be honest, I already had experience in DApp development, but I have no experience in making wallets and GameFi development, that's why I ask so many questions about this.

Thank you very much for your help ...

2 Answers 2

1

DISCLAIMER : This is just a piece of my mind, as you probably know there are many different approaches to solve all of those problems and the ones im going here with are just the ones i thought about when reading your questions, ask the same questions to someone else and you'll get different answers.

Our first challenge in this section is whether this method is optimal for making an in-game wallet or not? Our second challenge is how to store the private key, should we store the private key in the user's phone or in the database? Our third challenge in this section is what encryption algorithm to use for private key encryption and what parameters to use for encryption key and IV values?

I think it's reasonable. Now, I like self custody, i think it's good, and i don't think any server of any kind should play any role in generating/managing/storing your users credentials here. What I would do (i wanna insist that this is personal opinion, and that your method is perfectly valid) is generate the users private key on their device (be very careful with your seeds choice, do NOT use a single entropy source, take multiple of them (like for example, idk, global timer + coordinates of the last 5 clicks + some device-specific info, hash all that, and use it as your seed), store them on the device, and have the user set a password for encryption/decryption). The wallet is still embedded in the game, the user doesn't have to manage it externally, but it is custodial, which in my opinion is better.

The first challenge of this section is whether it makes sense for blockchain games that users pay their own fees or should the company pay the fees?

It does, but again it depends on your economic model and your target audience. If your target audience is not familiar with blockchain, i don't think it's reasonable to expect them to refill their wallets to pay for gas. (i think that answers the next questions as well)

In this case, some BNB currency will be blocked forever in the wallet, is this method reasonable?

Yes, that's basically what happens when you empty a wallet, no matter what client you use.

The solution that comes to my mind is to ask the user to allow the company's smart contract or wallet to execute some transactions and transfers on behalf of the user after the user is registered and after the wallet is created.The solution that comes to my mind is to ask the user to allow the company's smart contract or wallet to execute some transactions and transfers on behalf of the user after the user is registered and after the wallet is created.

There is no such thing as complete delegation over a wallet (In ethereum-like chains at least) You can get allowance to spend a users ERC20 token, but you can't interact with smart contracts on their behalf if the smart contract hasnt been designed with that in mind, and you can't spend your users BNB on their behalf (assuming you're on the binance smart chain). If you want to let your users have their own wallets within the app, while paying the gas yourself, something you could do is make an API request to your games servers to fund the users wallet with just enough BNB to pay the gas, and then have them make the tx. Now, that's probably quite slow, you'd have to test how it feels UX-wise (you can make it kinda seamless by making them pre-sign the transaction, optimistically assume it went well, actually broadcast it once the wallet has been funded, and handle any error afterwards). Alternatively you can give up on self custody, have a smart contract or a company wallet hold users funds, and keep tabs yourself.

The challenge of this method is that the cost of gas may be more than the balance of the company's wallet or smart contract, what should be done in this case?

Just top up whenever the wallets balance reaches a certain threshold, shouldn't be too hard.

1
  • My friend, thank you for answering my questions, I had another question, the explanation of which was long and I couldn't write it in the comment, that's why I had to record an answer to my question so that I can explain all my explanations. Please pay attention to this and guide me thank you
    – Ali
    Sep 22 at 9:10
0

Thank you very much for your help my friend

The only other question that arises is if I use the AES 256 algorithm to encrypt the private key, is it optimal from a security point of view to use the user's password hash for the encryption key and the user's email hash or the user's username hash for the IV? (Note that the hash of the user's password is stored in the game database)

The way I'm currently considering is to cache the encrypted private key along with the IV on the user's device and during each transaction the password hash is read from the database and used along with the IV to decrypt the private key. will be The main private key (decrypted) is sent to the API via http request to complete and sign the transaction along with other required information, is this a reasonable and secure method?

Thanks for your help ...

4
  • 1
    It's against the stack's rules to post another question as an answer, your answer might get deleted. Next time just edit your original question or create another one. To answer you tho, 1) that sounds reasonable, but a bad actor with access to the games database and a users machine will be able to decrypt their private keys without knowing their password, im not an expert in web security, and i don't know if this is a reasonable threat to consider. Maybe you could store the users address and check if the decrypted private key matches the stored address instead?
    – Foxxxey
    Sep 22 at 10:20
  • And 2) you do NOT want the users unencrypted private key to ever leave their machine, for the same reason. I'd do it the other way around, so client makes an API request => server returns transaction data => client signs the transaction locally => client sends the signed tx to server through http(s!!)
    – Foxxxey
    Sep 22 at 10:23
  • 1
    Thank you very much for your guidance, you helped me a lot
    – Ali
    Sep 22 at 11:07
  • You're very welcome :) Always happy to help
    – Foxxxey
    Sep 22 at 13:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.