0

If i add this line to my code then it refuses to compile!(This happens with All scripts in unity, even empty ones)

string publicKey = Nethereum.Signer.EthECKey.GetPublicAddress(privatekey);

Code:

using System.Collections;
using System;
using System.Collections.Generic;
using Nethereum.Util;
using UnityEngine;
using System.Threading.Tasks;
using Nethereum.Hex.HexConvertors.Extensions;
using Org.BouncyCastle.Crypto.Digests;
using System.Linq;
using System.Text;
using Nethereum.Util.Keccak;
using System.Numerics;
using System.Runtime.CompilerServices;
using Nethereum.RLP;
using Nethereum.Signer.Crypto;
using Org.BouncyCastle.Crypto;
using Org.BouncyCastle.Crypto.Agreement;
using Org.BouncyCastle.Crypto.Generators;
using Org.BouncyCastle.Crypto.Parameters;
using Org.BouncyCastle.Security;
using Org.BouncyCastle.Utilities;
public class MianScript : MonoBehaviour
{
    string chain = "ethereum";
    string network = "rinkeby";
    [SerializeField]
    public string password;
    public void OnClickBTN()
    {

        string privatekey = CreateAccount();
        string publicKey = Nethereum.Signer.EthECKey.GetPublicAddress(privatekey);
        Debug.Log("\nPrivate: " + privatekey + "\nPublicKey: " + publicKey);
    }


    public byte[] CalculateHash(byte[] value)
    {
        var digest = new KeccakDigest(256);
        var output = new byte[digest.GetDigestSize()];
        digest.BlockUpdate(value, 0, value.Length);
        digest.DoFinal(output, 0);
        return output;
    }
    public async Task<String> GetBalance(string chain1, string network1, string address1)
    {
        string balance = await EVM.BalanceOf(chain1, network1, address1);
        return balance;
    }
    public static string CreateAccount()
    {
        System.Random random = new System.Random();
        var bytes = new Byte[32];
        random.NextBytes(bytes);

        var hexArray = Array.ConvertAll(bytes, x => x.ToString("X2"));
        var hexStr = String.Concat(hexArray);
        return Convert.ToString(hexStr.ToLower());
    }
}

Help pls!

seantsang
  • 1,123
  • 2
  • 7
  • 20

1 Answers1

0

I'm not using unity, C# either but it seems that GetPublicAddress() is not a static function but a class method.

So I think you need to do something like that:

// Creating an EthECKey instance
var key = new Nethereum.Signer.EthECKey(privateKey.HexToByteArray(), true);

// Getting the public Address
string publicKey = key.GetPublicAddress();

I simply checked examples from here.

Ben Souchet
  • 1,450
  • 6
  • 20