BlackHatCoiner (OP)
Legendary
Offline
Activity: 1694
Merit: 8328
Fiatheist
|
|
November 29, 2020, 11:40:04 AM |
|
I've read how an mnemonic works. Basically the program takes parts of the hex that represent a number between 0 and 2047 and it prints words from a txt file that correspond to the analogous numbers of their file line. I've also seen that there are many libraries in which you can create mnemonics with just a command. On C#'s NBitcoin for example you can with Mnemonic mnemo = new Mnemonic(Wordlist.English, WordCount.Twelve); What I want to do is converting a hex (128-bit or 256-bit) into mnemonic. I have found ways to do that, but only from other programming languages' libraries. I want to do it on C# and because I'm a little afraid of implementing this into code for the first time, I would like to know if there is already a simple way to do that.
|
|
|
|
Coding Enthusiast
Legendary
Offline
Activity: 1042
Merit: 2805
Bitcoin and C♯ Enthusiast
|
|
November 29, 2020, 01:53:59 PM |
|
The conversion is pretty simple and straight forward. As you already know you treat the entropy (your hex) as an octet string and have to select 11 bits at a time. It appears that NBitcoin uses .net's BitArray class which I find very inefficient way of working with bits so I simply put the bits with the padding in a uint[] and then select the bits from there. You can see it here with sufficient comments. What you want can be achieved from the following single line method: using Autarkysoft.Bitcoin.Encoders; using Autarkysoft.Bitcoin.ImprovementProposals;
public BIP0039 GetBip39FromHex(string hex) => new BIP0039(Base16.Decode(hex));
|
|
|
|
BlackHatCoiner (OP)
Legendary
Offline
Activity: 1694
Merit: 8328
Fiatheist
|
|
November 29, 2020, 01:59:13 PM Last edit: November 30, 2020, 10:24:00 AM by BlackHatCoiner |
|
I guess that Autarkysoft.Bitcoin is one of your projects. Unfortunately, it seems that I can't install it. (Visual studio 2017 on windows 10) Attempting to gather dependency information for package 'Autarkysoft.Bitcoin.0.6.1' with respect to project 'Mnemonic keys', targeting '.NETFramework,Version=v4.6.1' Gathering dependency information took 20.43 ms Attempting to resolve dependencies for package 'Autarkysoft.Bitcoin.0.6.1' with DependencyBehavior 'Lowest' Resolving dependency information took 0 ms Resolving actions to install package 'Autarkysoft.Bitcoin.0.6.1' Resolved actions to install package 'Autarkysoft.Bitcoin.0.6.1' Retrieving package 'Autarkysoft.Bitcoin 0.6.1' from 'nuget.org'. Install failed. Rolling back... Package 'Autarkysoft.Bitcoin.0.6.1' does not exist in project 'Mnemonic keys' Package 'Autarkysoft.Bitcoin.0.6.1' does not exist in folder 'C:\Users\bymet\source\repos\Mnemonic keys\packages' Executing nuget actions took 707.17 ms Could not install package 'Autarkysoft.Bitcoin 0.6.1'. You are trying to install this package into a project that targets '.NETFramework,Version=v4.6.1', but the package does not contain any assembly references or content files that are compatible with that framework. For more information, contact the package author. Time Elapsed: 00:00:00.8753458 ========== Finished ==========
|
|
|
|
Coding Enthusiast
Legendary
Offline
Activity: 1042
Merit: 2805
Bitcoin and C♯ Enthusiast
|
|
November 29, 2020, 02:25:54 PM |
|
I guess that Autarkysoft.Bitcoin is one of your projects.
Yes it is, you can find it in my signature too. Keep in mind that the library is in beta and you can find my versioning convention hereUnfortunately, it seems that I can't install it. (Visual studio 2017 on windows 10)
This is a known bug with old visual studio, it will be fixed if you upgrade to VS2019. Until then check out this SE reply which should help.
|
|
|
|
BlackHatCoiner (OP)
Legendary
Offline
Activity: 1694
Merit: 8328
Fiatheist
|
|
November 29, 2020, 03:14:56 PM |
|
I just installed VS2019 and got a similar error: Could not install package 'Autarkysoft.Bitcoin 0.6.1'. You are trying to install this package into a project that targets '.NETFramework,Version=v4.7.2', but the package does not contain any assembly references or content files that are compatible with that framework. For more information, contact the package author.
|
|
|
|
Coding Enthusiast
Legendary
Offline
Activity: 1042
Merit: 2805
Bitcoin and C♯ Enthusiast
|
|
November 29, 2020, 03:39:40 PM |
|
That error is because .net standard 2.1 (the library's target framework) is not compatible with the very old .net framework 4.7.2, you should upgrade to .net core 3.0+ (3.1 is LTS). See this table on MSDN.
|
|
|
|
BlackHatCoiner (OP)
Legendary
Offline
Activity: 1694
Merit: 8328
Fiatheist
|
|
November 29, 2020, 04:07:54 PM |
|
That error is because .net standard 2.1 (the library's target framework) is not compatible with the very old .net framework 4.7.2, you should upgrade to .net core 3.0+ (3.1 is LTS). See this table on MSDN. I upgraded to 3, then I tried to install your library, same error. I upgraded to 5, then I tried to install your library, same error again. I cleared the nuget cache as this guy says here, still nothing. I wonder what's the fault. Should I download .net framework 4.8?
|
|
|
|
Coding Enthusiast
Legendary
Offline
Activity: 1042
Merit: 2805
Bitcoin and C♯ Enthusiast
|
|
November 29, 2020, 04:40:22 PM |
|
I upgraded to 3, then I tried to install your library, same error.
If you get the "same error" saying "...into a project that targets '.NETFramework,Version=v4.7.2',..." it means you have not changed your target framework. Go to your project properties (select the project from solution explorer then from menu Project > {project name} properties) then in Application tab you should see a Target framework combobox where you can select the available frameworks. You can also manually modify your .csproj file if you are familiar with it, I'm already using the package in another project of mine targeting netcoreapp3.1 like this: https://github.com/Coding-Enthusiast/FinderOuter/blob/b35b843987541dac24f8577342d287fd49f9dad7/Src/FinderOuter/FinderOuter.csproj#L58No version of .net framework would work, they are kind of obsolete. There are certain improvements that were added to .net core and are present only in .net standard 2.1 which I'm taking advantage of in the library, so you have to target .net core 3.0+.
|
|
|
|
BlackHatCoiner (OP)
Legendary
Offline
Activity: 1694
Merit: 8328
Fiatheist
|
|
November 29, 2020, 05:53:29 PM |
|
If you get the "same error" saying "...into a project that targets '.NETFramework,Version=v4.7.2',..." it means you have not changed your target framework. Go to your project properties (select the project from solution explorer then from menu Project > {project name} properties) then in Application tab you should see a Target framework combobox where you can select the available frameworks. And choose what framework? I was on 4.7.2, it has 2.0, 3.0 etc. I changed it to 3.0, then tried to install, same error.
|
|
|
|
Coding Enthusiast
Legendary
Offline
Activity: 1042
Merit: 2805
Bitcoin and C♯ Enthusiast
|
|
November 29, 2020, 06:53:45 PM |
|
And choose what framework? I was on 4.7.2, it has 2.0, 3.0 etc. I changed it to 3.0, then tried to install, same error.
Are you sure you set it to .net core 3.0 and not .net framework 3.0? Try creating a new project like this:
|
|
|
|
BlackHatCoiner (OP)
Legendary
Offline
Activity: 1694
Merit: 8328
Fiatheist
|
|
November 29, 2020, 07:02:18 PM |
|
There is no core 3.0: Edit: It's a windows forms app, not a console app.
|
|
|
|
Coding Enthusiast
Legendary
Offline
Activity: 1042
Merit: 2805
Bitcoin and C♯ Enthusiast
|
|
November 29, 2020, 07:40:54 PM Merited by Vod (1), ABCbits (1) |
|
Edit: It's a windows forms app, not a console app.
You should be able to migrate the app to .net 5: https://docs.microsoft.com/en-us/dotnet/desktop/winforms/migration/?view=netdesktop-5.0If this is a new project and if you are new to c# and .net I strongly suggest getting more familiar with XAML instead of winforms. The later is windows only and is old and limited while the former is a lot better and there are UI projects such as Avalonia that run on any OS. If you want to try things out then using a console app is the easiest way to go, otherwise if you want UI install Avalonia and start from there. There is also WPF which is windows only but is ported to .net core and .net too.
|
|
|
|
BlackHatCoiner (OP)
Legendary
Offline
Activity: 1694
Merit: 8328
Fiatheist
|
|
November 29, 2020, 08:03:50 PM |
|
If this is a new project and if you are new to c# I am a beginner on C# and excited about that. I can imagine that any expert on C# can create brilliant programs on Visual Studio. Unfortunately, as far as I've seen in the internet, everything looks complicated. I try to learn, but only amateurly on my free time. I've looked on your projects and you've done some great work, especially FinderOuter. Do you have any e-books to suggest me that you believe are perfect for a beginner? To clear up, once I say amateurly I mean that I don't have an important knowledge. If I ever have a query like how to insert a button that opens Windows Explorer onclick to choose a file, I'll have to google it. Because I simply don't know how to do it. I strongly suggest getting more familiar with XAML instead of winforms. Too bad that winforms are not a good choice. I had tided over with them. Buttons and other stuff look the same like on every single program on winforms. On WPF it looks more professional, something like photoshop. If you want to try things out then using a console app is the easiest way to go To prevent any misunderstandings, console app means that it is only available on cmd? It means that it has no UI?
|
|
|
|
A-Bolt
Legendary
Offline
Activity: 2334
Merit: 2374
|
What I want to do is converting a hex (128-bit or 256-bit) into mnemonic.
NBitcoin again: Byte[] entropy = { 0xfe, 0x5e, 0xa5, 0x18, 0xfb, 0x51, 0x53, 0xd7, 0x34, 0x75, 0x54, 0x52, 0x5c, 0xba, 0xc7, 0x15 }; // 128-bit entropy Mnemonic mnemo = new Mnemonic(Wordlist.English, entropy); Console.WriteLine(mnemo.ToString());
|
|
|
|
Coding Enthusiast
Legendary
Offline
Activity: 1042
Merit: 2805
Bitcoin and C♯ Enthusiast
|
Do you have any e-books to suggest me that you believe are perfect for a beginner?
I prefer videos myself, there is a free course by Microsoft here: https://channel9.msdn.com/Series/CSharp-Fundamentals-for-Absolute-BeginnersThere are paid ones on Udemy which I hear are good. For XAML: https://docs.microsoft.com/en-us/dotnet/desktop/wpf/fundamentals/xaml?view=netdesktop-5.0For MVVM (an architectural pattern that goes well with XAML and C# following the IoC principle): https://www.tutorialspoint.com/mvvm/index.htmFor everything else just Google or feel free to ask me, I'll answer to the best of my abilities. To clear up, once I say amateurly I mean that I don't have an important knowledge. If I ever have a query like how to insert a button that opens Windows Explorer onclick to choose a file, I'll have to google it. Because I simply don't know how to do it.
If you want to become familiar with XAML (the language used in UI) you could start a WPF project and then use the designer to simply draw any item such as a button (like you'd do in MS paint) then look at the XAML code that it generates. It may seem overwhelming at first but eventually you'll just type all of that in a much cleaner and more flexible way. To prevent any misunderstandings, console app means that it is only available on cmd? It means that it has no UI?
Yes, it is a text-only interface similar to command line in windows. Technically this is a UI (user interface), but is not a GUI (graphical user interface).
|
|
|
|
BlackHatCoiner (OP)
Legendary
Offline
Activity: 1694
Merit: 8328
Fiatheist
|
|
December 01, 2020, 07:35:09 AM |
|
I would like to ask one last thing related to the thread's title. As @A-Bolt posted, NBitcoin has a simple way on converting entropy to mnemonic. Is there any simple way to go backwards? From mnemonic to hexadecimal? I think that this, along with finding a way to encrypt messages with ECC on c#, are the only things that have remained to finish my work.
|
|
|
|
|
BlackHatCoiner (OP)
Legendary
Offline
Activity: 1694
Merit: 8328
Fiatheist
|
|
December 01, 2020, 08:56:41 AM |
|
I didn't fully understand this. Do you have a function that does that in your library? Because it seems that the link you gave me doesn't do that. (Tbh, I don't understand what exactly public BIP0039 does) I could implement ECIES if you are stuck. I'm really stuck on that! I don't know how to do it and yes I would like you to give me some code or a link to your library. I had started this thread asking how to implement it, but didn't understand. Fortunately, I achieved the signing/verifying part, which I was searching too.
|
|
|
|
Coding Enthusiast
Legendary
Offline
Activity: 1042
Merit: 2805
Bitcoin and C♯ Enthusiast
|
|
December 01, 2020, 09:28:58 AM |
|
I didn't fully understand this. Do you have a function that does that in your library? Because it seems that the link you gave me doesn't do that. (Tbh, I don't understand what exactly public BIP0039 does)
This constructor instantiates BIP-39 (and 32) from a given mnemonic string, it first validates the string which requires converting the words to the entropy, computing its checksum and comparing it with the given checksum. The part of the code I posted is what you want (from mnemonic to entropy which can be converted to hex). There is no public method for this since this not needed in any real world scenario, however you can copy the code and add a final line at the end using the ToBase16() extension on the entropy like string hex = entropy.ToBase16();I'm really stuck on that! I don't know how to do it and yes I would like you to give me some code or a link to your library. I had started this thread asking how to implement it, but didn't understand. Fortunately, I achieved the signing/verifying part, which I was searching too. There is no code for it yet, I'll have to read how it works and then implement it.
|
|
|
|
BlackHatCoiner (OP)
Legendary
Offline
Activity: 1694
Merit: 8328
Fiatheist
|
|
December 01, 2020, 11:56:47 AM |
|
This constructor instantiates BIP-39 (and 32) from a given mnemonic string, it first validates the string which requires converting the words to the entropy, computing its checksum and comparing it with the given checksum. The part of the code I posted is what you want (from mnemonic to entropy which can be converted to hex). Yes but as far as I can see, within that part of the code there are variables already set outside of it. (for example ENT or wordIndexes). What am I supposed to put there? Copy the entire code? There is no code for it yet, I'll have to read how it works and then implement it. The whole encryption/decryption thing is based on pretty complicated maths, so if it's difficult to understand it imagine how difficult it would be for me to implement it on C#. A big thank you for giving a guiding hand to a C# beginner and cryptography enthusiast here. Your merits talk.
|
|
|
|
|