[C#] 手把手來玩比特幣 (Bitcoin) - 製作高清錢包

這一篇算是番外篇,不用也是可以,主要就是產生高清錢包,其目的在於 你可以透過一個助記符(mnemonic),產生很多很多的錢包(uint 的 max ) 約莫 4,294,967,295,而且這 4,294,967,295 裡面還可以在生出小錢包,可以有助於你管理錢包,你只要知道他是編號幾,然後擁有註記符就可以還原

 

程式碼很簡單

            Mnemonic mnemonic = new Mnemonic(Wordlist.ChineseTraditional, WordCount.TwentyFour);

            var words = new string[] { "許", "當", "麻", "的", "密", "碼", "就", "是", "一", "組", "很", "奇", "怪", "的", "文", "字", "奇", "怪", "的", "文", "字", "許", "功", "蓋" };

            mnemonic = new Mnemonic(string.Join(" ", words));

            ExtKey fatherKey = mnemonic.DeriveExtKey();

            var wiff = "";
            for (int i = 0; i < 10; i++)
            {
                ExtKey key = fatherKey.Derive((uint)i);
                var c = ("Key " + i + " : " + key.PrivateKey.GetBitcoinSecret(Network.TestNet).GetAddress().ToString() + " , wif:" + key.GetWif(Network.TestNet));
                Response.Body.Write(System.Text.Encoding.UTF8.GetBytes(c + "\r\n"));
            }

結果:

 

Key 0 : moKkzru5Z3e3NnDSz17e3BqbiiBaFf45XE , wif:tprv8d3VbriRCuNkVGrNFhHPNQqQCN2H7zRbmSeWjAVQpstf6nCiqvKfZWe7VofemyqY5MfJdiMdqnAGFZ6gyufsLADJmAzTRGM4JoBrBKfoA7n
Key 1 : mokvmUXZoeAMDoscNs7DrUkXsQ1acKqsA9 , wif:tprv8d3VbriRCuNkYAPafRmcRGZEyTmHSqmTCC2UZgAncgQVpFmCs6JCu2AySC4GiJwbfhxfa7oYGAFAkowPuC8EwLNptNL73J1r4SExyThTVaX
Key 2 : n3zYsMStbxY2F5iJ7bERYDtGE1fmiCBahS , wif:tprv8d3VbriRCuNkaUAE1DfCoQxDc3USzZJ33tpzfcrdiURDy32rocZdwxJwaWQrgufedQr8AC3KJMtKFAHUCUNJJoNDfQm1vwqBD8t3bAJFKxe
Key 3 : mz3XtAYisQXro5r7YASbL85E4ukVEzAqsX , wif:tprv8d3VbriRCuNkeTwUX2HJpNU7PEBfiQE28mMCdhezqdaxgbgcvxYYRZdn8NZYUuotMFBz1TzG4iFhfCRq7rJDqMQF74gofssCL6eZpTtj1GK
Key 4 : mggC7iFvGCcXsuiQPq2jJd9sAtLiLLiCUf , wif:tprv8d3VbriRCuNkgUbPZ1ziuYwM41Z2xgaEWCvu4WMNHs25AfkZ42tArWRw52aKgrbyfjASzRyHnTGJQLbmfQoCfwMgrcEMBQFRS9oKsrFm29k
Key 5 : mw7BMvE9VDm9Mxs7e6ZvfsbXu2qq9fmcDf , wif:tprv8d3VbriRCuNkj4AXNPtG8Dbpx4qbYeZh3x6G2js4c51NHcqyHHJZqRDXup3sEJP9k8BNvqExpodHq5QfHytd9tfjDVuBGZfnah3Gn1XCv5s
Key 6 : mkqvHgXqDp1FatiC2YSKWdZLf7x5jcXr96 , wif:tprv8d3VbriRCuNkmXqZBoiAKpqx9uvbKW9y1jdCEs8fjoQnW1fSgM6KtqjHAtfD42K6m1yKYD6Mry3BmFZoYNt3NRhgbSJvPdxQvHxG129tAzX
Key 7 : mvzxL9zqKocRwSNGwmidPgocsKwnERY3No , wif:tprv8d3VbriRCuNko4AQDLF1p39uZeqozGVLZJyN3o5XveC9bSywxyD5JSnxQ6EMP1Zsf483wfddagR3fe88waXkjeLguSAFBe2fbZfvJnKpdJN
Key 8 : mjCBRT63NCGQcSg6PoBYNQk4GuATJg3Ty8 , wif:tprv8d3VbriRCuNkqQonai5s1F2Ez3BVqB2bZ3WDoVKHjsG3EobU3qHkM5dNwiqd1ojLyioctfMtovrLsMwxVAd52wXRVehFQ9QL5Ehpos1DCS1
Key 9 : n1Pqhq3mNNiPXKgiSuZK9psKNAKCAuwDet , wif:tprv8d3VbriRCuNktBz5b8yYHaNJvvSw5e6iPjftg4YTFAbKj51NnehPpHYXjHAJeJupkc7DkqxPgNkpUW3kEwW3ZAcPSgXGDDmZLz29UXrScRk

其中我還將 wif 印出來,您可以將此存起即可以還原該 Key ,當然,如果你有助記符,並且知道序號都可以進行還原,這樣會有助於管理其下的眾多 Key

這邊比較偏向實作,細節可以參考下面的文章參考,了解一下關於BIP39,會更了解高清錢包的好處。

文章參考 :
https://medium.com/bitcraft/hd-wallets-explained-from-high-level-to-nuts-and-bolts-9a41545f5b0
https://medium.com/bitcraft/so-you-want-to-build-a-bitcoin-hd-wallet-part-1-8b27aa001ac3
https://medium.com/taipei-ethereum-meetup/%E8%99%9B%E6%93%AC%E8%B2%A8%E5%B9%A3%E9%8C%A2%E5%8C%85-%E5%BE%9E-bip32-bip39-bip44-%E5%88%B0-ethereum-hd-%EF%BD%97allet-a40b1c87c1f7
https://docs.microsoft.com/zh-tw/dotnet/csharp/language-reference/keywords/uint

 

---

 

請你暫時把你的勇氣給我 在夢想快消失的時候 讓我的 Code 用力的穿過天空 為愛我的人做一秒英雄 如果這篇文章有幫助到您,簡單留個言,或是幫我按個讚,讓我有寫下去的動力...