tatumio/tatum-csharp

How to use the C# in the repo to generate private key for Ethereum address?

Closed this issue · 1 comments

Hello:
I am new to this repo, I want to do some test.
I have done the following:
I created one C# WinForms app target .NET 6, and install the following NUGET packages:
PM> Install-Package Microsoft.AspNetCore.Mvc -Version 2.2.0
PM> Install-Package Tatum.CSharp.Core -Version 2.4.0
PM> Install-Package Tatum.CSharp.Ethereum -Version 2.4.0

I created a normal Form1.cs and add one class: GeneratePrivateKeyExampleService.cs
The following is the code in GeneratePrivateKeyExampleService.cs:
using Microsoft.AspNetCore.Mvc;
using Tatum.CSharp.Core.Model;
using Tatum.CSharp.Ethereum.Clients;

namespace GenPrivateKeysForm
{
public class GeneratePrivateKeyExampleService
{
private const string ERC20_Mnemonic =
@"My_24_Words_Parameter_For_Ethereum_Wallet";

    private readonly IEthereumClient _ethereumClient;

    public GeneratePrivateKeyExampleService(IEthereumClient ethereumClient)
    {
        _ethereumClient = ethereumClient;
    }

    public PrivKey GeneratePrivateKey([FromBody] PrivKeyRequest request)
    {
        request.Mnemonic = ERC20_Mnemonic;
        request.Index = 1;

        PrivKey privateKey = _ethereumClient.Local.GenerateAddressPrivateKey(request);
        return privateKey;
    }
}

}

I can compile my code, but I don’t know how to call this and get the private. How can I pass the request parameter in GeneratePrivateKey function?

By the way, I also tried to use Rest-Clien (Insomnia) to generate private key for the address I created before. But I got the following error:
{
"statusCode": 400,
"errorCode": "validation.failed",
"message": "Request validation failed. Please see data for additional information.",
"data": [
"Mnemonics must consist of 12 or 24 words, and every word should be at least 2 letters long.",
"mnemonic must be shorter than or equal to 500 characters",
"mnemonic should not be empty",
"index must not be greater than 2147483647",
"index must not be less than 0",
"index must be an integer number",
"index should not be empty"
]
}

I used the same mnemonic when I generated an Ethereum wallet. But I got the error.
Finally, I tried to write some Node Javascript to generate a private key for the Ethereum address I created.
The following is the Node JavaScript (Node version: 19.1.0)

const Tatum = require('@tatumio/tatum');
const {generatePrivateKeyFromMnemonic, Currency} = require("@tatumio/tatum");

const CreatePrivateKey = async () =>
{
try
{
const erc20PrivateKey =
await generatePrivateKeyFromMnemonic(Currency.ETHEREUM, false, "My_24_Words_Parameter_For_Ethereum_Wallet" 1);
console.log({key: erc20PrivateKeyPrivateKey});}
catch (err)
{
console.log(err);
}
};
CreatePrivateKey();

When I run this, I got no output at all.
D:\nodejs\Tatum>npm list
Tatum@ D:\nodejs\Tatum
`-- @tatumio/tatum@1.37.17

D:\nodejs\Tatum>node PrivateKey_ERC20Address1.js
^C
D:\nodejs\Tatum>

How can I debug or show more verbose output to find the issue?
I am using Windows 10 (Version 22H2), Visual Studio 2022 (Version 17.4.1)
Please advise,
Thanks,

Hello @zydjohnHotmail,
Can we please move the discussion to https://discord.com/channels/847940290903932939/914832388431958036

I'll be able to answer all of your questions related to use cases there. Github issues it more for technical issues with the SDKs and not so well suited for support discussions.

As for the question - request body parameters are passed to the method straight from the json body of the request, it depends on the caller how that is defined. You can also just pass mnemonic and index as argument and instanciate PrivateKeyRequest object inside the mothod.

For other requests seems like the mnemonic string is malformed in some way, please check if api is called with expected values.