0

I want to take a private key as input from the user and convert it to a wallet address using C#.

I have seen this in a tutorial to generate a random key:

Key privateKey = new Key();
PubKey publicKey = privateKey.PubKey;
Console.WriteLine(publicKey.GetAddress(ScriptPubKeyType.Legacy, Network.Main));

How do I convert a private key string from the user to a wallet address for main net?

3 Answers 3

0

Assuming the private key string in this example is pk

Console.WriteLine("Enter private key:");
string pk = Console.ReadLine();
var bitcoinPrivateKey = new BitcoinSecret(pk, Network.Main);

var legacy_address = bitcoinPrivateKey.GetAddress(ScriptPubKeyType.Legacy);

Console.WriteLine("Legacy Address :" + legacy_address);

And if you want to get p2sh-segwit and bech32 address, add below lines:

var p2shsegwit_address = bitcoinPrivateKey.GetAddress(ScriptPubKeyType.SegwitP2SH);
var nativesegwit_address = bitcoinPrivateKey.GetAddress(ScriptPubKeyType.Segwit);

Console.WriteLine("P2SH-Segwit Address :" + p2shsegwit_address);
Console.WriteLine("Bech32 Address :" + nativesegwit_address);
2
  • The code provided by Prayank didn't work. This code give an Exception Unhandled "System.FormatException: 'Invalid hash of the base 58 string" If anyone have an idea to how generate a private key from an user string input like (brainwallet) I will be grateful. Thanks Commented Aug 26, 2021 at 1:07
  • It works for private key in WIF format. Check details: bitcoin.stackexchange.com/a/108320
    – user103136
    Commented Aug 26, 2021 at 6:15
0

I have implemented it by javascript, maybe it can help you.

//usage:
//$ yarn add bip32 bip39
// node --version is 16.1.0

const bs58check = require('bs58check');
const bip39 = require("bip39");
const bip32 = require("bip32");
const createHash = require('create-hash');


function addr(node) {
    var hash = node.identifier; // hash160 of the publicKey

    const payload = Buffer.allocUnsafe(21);

    var version = 0x0;
    payload.writeUInt8(version, 0);

    hash.copy(payload, 1);

    var address = bs58check.encode(payload);
    return address;
}

var puzzle = "some puzzle you inputting";

var entropy = createHash("sha256").update(puzzle).digest().toString("hex");
console.log("entropy: " + entropy);

var mnemonic = bip39.entropyToMnemonic(entropy);
console.log("mnemonic: " + mnemonic);

var passphrase = "";

var seed = bip39.mnemonicToSeedSync(mnemonic, passphrase);
var master = bip32.fromSeed(seed); // m

// generate 10 btc addresses...
for (var i = 0; i < 3; i++) {
    var node = master.derivePath("m/44'/0'/0'/1/" + i);
    var address = addr(node);
    console.log("BTC #" + i + ": " + address);
}

0

You can find information how to calculate Public key from Private here https://bitcoin-math.000webhostapp.com Also, you can find Doubling, Addition and Scalar multiply functions on C# without NBitcoin class Key, BigInteger is used only.

Your Answer

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

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