Bitcoin Forum
June 14, 2024, 10:27:34 AM *
News: Voting for pizza day contest
 
   Home   Help Search Login Register More  
Pages: « 1 [2]  All
  Print  
Author Topic: C# - From hexadecimal to mnemonic?  (Read 303 times)
Coding Enthusiast
Legendary
*
Offline Offline

Activity: 1039
Merit: 2783


Bitcoin and C♯ Enthusiast


View Profile WWW
December 01, 2020, 01:45:50 PM
Merited by BlackHatCoiner (5)
 #21

It's done: https://github.com/Autarkysoft/Denovo/commit/ddd3a6318a57aef2364f05a5a3e3bde18ada243c
You have to compile the library yourself or wait for the next release (this can take a while since I'm working on P2PNetwork namespace and it is very time consuming).

You encrypt messages using the PublicKey instance:
Code:
bool success = PublicKey.TryRead(Base16.Decode("replace_with_pubkey_hex"), out PublicKey pubkey);
// Make sure success is true
string encrypted = pubkey.Encrypt("replace_with_message");
and decrypt using the PrivateKey instance:
Code:
using PrivateKey key = new PrivateKey("replace_with_private_key_wif");
string decrypted = key.Decrypt(encrypted );
See the accompanying tests for more details.

Projects List+Suggestion box
Donate: 1Q9s or bc1q
|
|
|
FinderOuter(0.19.1)Ann-git
Denovo(0.7.0)Ann-git
Bitcoin.Net(0.26.0)Ann-git
|
|
|
BitcoinTransactionTool(0.11.0)Ann-git
WatchOnlyBitcoinWallet(3.2.1)Ann-git
SharpPusher(0.12.0)Ann-git
BlackHatCoiner (OP)
Legendary
*
Offline Offline

Activity: 1554
Merit: 7546


Protocols over bureaucrats


View Profile
December 01, 2020, 04:10:04 PM
 #22

Thank you very much. I guess I just import these csharp files on visual studio and then I perform the functions you gave me on my project's files. As for your library's namespace, I still get the same issue with the framework. I don't see any "core" on Target framework:


.
.BLACKJACK ♠ FUN.
█████████
██████████████
████████████
█████████████████
████████████████▄▄
░█████████████▀░▀▀
██████████████████
░██████████████
████████████████
░██████████████
████████████
███████████████░██
██████████
CRYPTO CASINO &
SPORTS BETTING
▄▄███████▄▄
▄███████████████▄
███████████████████
█████████████████████
███████████████████████
█████████████████████████
█████████████████████████
█████████████████████████
███████████████████████
█████████████████████
███████████████████
▀███████████████▀
█████████
.
Coding Enthusiast
Legendary
*
Offline Offline

Activity: 1039
Merit: 2783


Bitcoin and C♯ Enthusiast


View Profile WWW
December 01, 2020, 05:23:54 PM
 #23

That is because in the application window only the easy-to-convert target frameworks are listed, and since converting a project that is already targeting ".net framewoek" to ".net core" is not easy it can not be automated so the list excludes all .net core options.
You have to do it manually with some additional steps, I've already posted the migration link here, but the easier and better solution is to first decide what type of application* you want to make then create a new project of that type while selecting .net core as your target from the start. Then copy the code from this project over there, it doesn't seem to be that big.

* NO GUI -> console app with .net core as its target can run on any OS
* GUI -> Winforms or WPF both limited to Windows but can target .net core but still run on Windows only
* GUI -> other multi platform open source GUI such as Avalonia that I told you about, can target .net core and run on any OS

Projects List+Suggestion box
Donate: 1Q9s or bc1q
|
|
|
FinderOuter(0.19.1)Ann-git
Denovo(0.7.0)Ann-git
Bitcoin.Net(0.26.0)Ann-git
|
|
|
BitcoinTransactionTool(0.11.0)Ann-git
WatchOnlyBitcoinWallet(3.2.1)Ann-git
SharpPusher(0.12.0)Ann-git
BlackHatCoiner (OP)
Legendary
*
Offline Offline

Activity: 1554
Merit: 7546


Protocols over bureaucrats


View Profile
December 01, 2020, 05:35:43 PM
 #24

But even if I try to create a new project I still don't see any .net cores on my available frameworks:



I firstly want to end this project, with WinForms, and then I'll check the Avalonia thing you mentioned.

.
.BLACKJACK ♠ FUN.
█████████
██████████████
████████████
█████████████████
████████████████▄▄
░█████████████▀░▀▀
██████████████████
░██████████████
████████████████
░██████████████
████████████
███████████████░██
██████████
CRYPTO CASINO &
SPORTS BETTING
▄▄███████▄▄
▄███████████████▄
███████████████████
█████████████████████
███████████████████████
█████████████████████████
█████████████████████████
█████████████████████████
███████████████████████
█████████████████████
███████████████████
▀███████████████▀
█████████
.
BlackHatCoiner (OP)
Legendary
*
Offline Offline

Activity: 1554
Merit: 7546


Protocols over bureaucrats


View Profile
December 03, 2020, 10:32:18 AM
 #25

Fortunately, after a lot of searching, I successfully installed the Autarkysoft package on my project after following the instructions you sent me here. As I said, I want to convert mnemonic to hexadecimal and I have some queries about your BIP0039.cs. Do I simply import it on my project? What's next? What will I have to write on my main csharp file to run that backwards (mnemonic->hex) function?

.
.BLACKJACK ♠ FUN.
█████████
██████████████
████████████
█████████████████
████████████████▄▄
░█████████████▀░▀▀
██████████████████
░██████████████
████████████████
░██████████████
████████████
███████████████░██
██████████
CRYPTO CASINO &
SPORTS BETTING
▄▄███████▄▄
▄███████████████▄
███████████████████
█████████████████████
███████████████████████
█████████████████████████
█████████████████████████
█████████████████████████
███████████████████████
█████████████████████
███████████████████
▀███████████████▀
█████████
.
Coding Enthusiast
Legendary
*
Offline Offline

Activity: 1039
Merit: 2783


Bitcoin and C♯ Enthusiast


View Profile WWW
December 03, 2020, 04:18:03 PM
 #26

What's next? What will I have to write on my main csharp file to run that backwards (mnemonic->hex) function?
I don't really get what you're trying to make but based on your other topic (public key to mnemonic) I assume you don't want the checksum validation and you want to enter a mnemonic and get the 264 bit entropy out. With that assumption the code would be this:
Code:
using Autarkysoft.Bitcoin;
using Autarkysoft.Bitcoin.Encoders;
using Autarkysoft.Bitcoin.ImprovementProposals;
using System;
using System.Linq;
using System.Text;
Code:
public string MnToHex(string mnemonic, BIP0039.WordLists wl = BIP0039.WordLists.English)
{
    if (string.IsNullOrWhiteSpace(mnemonic))
        throw new ArgumentNullException(nameof(mnemonic), "Seed can not be null or empty!");
    string[] allWords = BIP0039.GetAllWords(wl);

    string[] words = mnemonic.Normalize(NormalizationForm.FormKD)
                                .Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
    if (!words.All(x => allWords.Contains(x)))
    {
        throw new ArgumentException(nameof(mnemonic), "Seed has invalid words.");
    }
    if (!new int[] { 12, 15, 18, 21, 24 }.Contains(words.Length))
    {
        throw new FormatException("Invalid seed length. It should be ∈{12, 15, 18, 21, 24}");
    }

    uint[] wordIndexes = new uint[words.Length];
    for (int i = 0; i < words.Length; i++)
    {
        wordIndexes[i] = (uint)Array.IndexOf(allWords, words[i]);
    }

    int ENTCS = words.Length * 11;
    int CS = ENTCS % 32;
    int ENT = ENTCS - CS;

    byte[] entropy = new byte[(ENT / 8) + 1];

    int itemIndex = 0;
    int bitIndex = 0;
    for (int i = 0; i < entropy.Length - 1; i++)
    {
        if (bitIndex + 8 <= 11)
        {
            entropy[i] = (byte)(wordIndexes[itemIndex] >> (3 - bitIndex));
        }
        else
        {
            entropy[i] = (byte)(((wordIndexes[itemIndex] << (bitIndex - 3)) & 0xff) |
                                    (wordIndexes[itemIndex + 1] >> (14 - bitIndex)));
        }

        bitIndex += 8;
        if (bitIndex >= 11)
        {
            bitIndex -= 11;
            itemIndex++;
        }
    }

    uint mask = (1U << CS) - 1;
    entropy[^1] = (byte)(wordIndexes[itemIndex] & mask);

    return entropy.ToBase16();
}
HexToMn works only with 33 byte entropy (66 char hex):
Code:
public string HexToMn(string hex, BIP0039.WordLists wl = BIP0039.WordLists.English)
{
    byte[] entropy = Base16.Decode(hex);
    if (entropy.Length != 33)
        throw new ArgumentOutOfRangeException();

    byte[] ba = entropy.ConcatFast(new byte[3]);

    uint[] bits = new uint[9];
    for (int i = 0, j = 0; i < ba.Length - 1; i += 4, j++)
    {
        bits[j] = (uint)(ba[i + 3] | (ba[i + 2] << 8) | (ba[i + 1] << 16) | (ba[i] << 24));
    }

    int itemIndex = 0;
    int bitIndex = 0;
    uint[] wordIndexes = new uint[24];
    for (int i = 0; i < 24; i++)
    {
        if (bitIndex + 11 <= 32)
        {
            wordIndexes[i] = (bits[itemIndex] << bitIndex) >> 21;
        }
        else
        {
            wordIndexes[i] = ((bits[itemIndex] << bitIndex) >> 21) |
                                (bits[itemIndex + 1] >> (53 - bitIndex));
        }

        bitIndex += 11;
        if (bitIndex >= 32)
        {
            bitIndex -= 32;
            itemIndex++;
        }
    }

    StringBuilder sb = new StringBuilder(wordIndexes.Length * 8);
    string[] allWords = BIP0039.GetAllWords(wl);
    for (int i = 0; i < wordIndexes.Length; i++)
    {
        sb.Append($"{allWords[wordIndexes[i]]} ");
    }

    sb.Length--;
    return sb.ToString();
}

Projects List+Suggestion box
Donate: 1Q9s or bc1q
|
|
|
FinderOuter(0.19.1)Ann-git
Denovo(0.7.0)Ann-git
Bitcoin.Net(0.26.0)Ann-git
|
|
|
BitcoinTransactionTool(0.11.0)Ann-git
WatchOnlyBitcoinWallet(3.2.1)Ann-git
SharpPusher(0.12.0)Ann-git
BlackHatCoiner (OP)
Legendary
*
Offline Offline

Activity: 1554
Merit: 7546


Protocols over bureaucrats


View Profile
December 04, 2020, 06:44:18 AM
Last edit: December 04, 2020, 07:30:35 PM by BlackHatCoiner
 #27

Once I try to convert Hex to mnemonic with HexToMn I get "Word list was not found.". I normally use 33 byte-string (66 chars).





I guess it has to do with:
Code:
string path = $"Autarkysoft.Bitcoin.ImprovementProposals.BIP0039WordLists.{wl}.txt";

Does it read it? Where is the wordlist located?

.
.BLACKJACK ♠ FUN.
█████████
██████████████
████████████
█████████████████
████████████████▄▄
░█████████████▀░▀▀
██████████████████
░██████████████
████████████████
░██████████████
████████████
███████████████░██
██████████
CRYPTO CASINO &
SPORTS BETTING
▄▄███████▄▄
▄███████████████▄
███████████████████
█████████████████████
███████████████████████
█████████████████████████
█████████████████████████
█████████████████████████
███████████████████████
█████████████████████
███████████████████
▀███████████████▀
█████████
.
Pages: « 1 [2]  All
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.19 | SMF © 2006-2009, Simple Machines Valid XHTML 1.0! Valid CSS!