C区块链编程入门教程.pdf

返回 相似 举报
C区块链编程入门教程.pdf_第1页
第1页 / 共83页
C区块链编程入门教程.pdf_第2页
第2页 / 共83页
C区块链编程入门教程.pdf_第3页
第3页 / 共83页
C区块链编程入门教程.pdf_第4页
第4页 / 共83页
C区块链编程入门教程.pdf_第5页
第5页 / 共83页
点击查看更多>>
资源描述:
Blockchain Programming in C Authored by Nicolas Dorier Contributor for NBitcoin, The .NET Bitcoin Framework Co-authored With Bill Strait Founder of Billd Labs 2 Nicolas Dorier Bill Strait CC ASA 3U TableofContentsI. Introduction ..................................................................................................................................... 4 1. Foreword ..................................................................................................................................... 4 2. Why Blockchain Programming and not Bitcoin Programming .................................................. 5 3. Why C ....................................................................................................................................... 5 4. Pre-requisites .............................................................................................................................. 5 a. Skills ......................................................................................................................................... 5 b. Tools ........................................................................................................................................ 6 5. Crowdfunding this book .............................................................................................................. 6 6. Complementary Reading ............................................................................................................. 6 7. Diagrams ...................................................................................................................................... 7 8. License CC ASA 3U .................................................................................................................... 8 9. Project Setup ............................................................................................................................... 9 II. Bitcoin transfer .............................................................................................................................. 10 10. Bitcoin Address ...................................................................................................................... 10 11. Transaction ............................................................................................................................ 15 12. Blockchain.............................................................................................................................. 19 13. “The Blockchain is more than just Bitcoin” ........................................................................... 19 14. Spend your coin ..................................................................................................................... 20 15. Proof of ownership as an authentication ................................................................ 24 III. Key Storage and Generation...................................................................................................... 25 1. Is it random enough................................................................................................................. 25 c. Key Derivation Function ........................................................................................................ 26 2. Key Encryption ........................................................................................................................... 27 3. Key Generation .......................................................................................................................... 28 a. Like the good ol’ days ............................................................................................................ 28 a. BIP38 part 2 ......................................................................................................................... 28 b. HD Wallet BIP 32 ................................................................................................................. 30 c. Mnemonic Code for HD Keys BIP39 .................................................................................... 36 d. Dark Wallet ............................................................................................................................ 38 IV. Other types of ownership .......................................................................................................... 42 1. P2PK[H] Pay to Public Key [Hash]............................................................................................ 42 2. Multi Sig ..................................................................................................................................... 44 3. P2SH Pay To Script Hash ......................................................................................................... 48 4. Arbitrary .................................................................................................................................... 50 5. Using the TransactionBuilder .................................................................................................... 52 3 Nicolas Dorier Bill Strait CC ASA 3U V. Other types of asset ...................................................................................................................... 57 1. Colored Coins............................................................................................................................. 57 2. Issuing an Asset ......................................................................................................................... 58 a. Objective................................................................................................................................ 58 b. Issuance Coin ......................................................................................................................... 58 3. Transfer an Asset ....................................................................................................................... 61 4. Unit tests ................................................................................................................................... 64 5. Ricardian contracts .................................................................................................................... 73 a. What is a Ricardian Contract ................................................................................................. 73 b. Ricardian Contract inside Open Asset ................................................................................... 73 c. Check list ................................................................................................................................ 74 d. What is it for ........................................................................................................................ 75 6. Liquid Democracy ...................................................................................................................... 75 a. Overview ................................................................................................................................ 75 b. Issuing voting power ............................................................................................................. 75 c. Running a vote ....................................................................................................................... 77 d. Vote delegation ..................................................................................................................... 78 e. Voting .................................................................................................................................... 79 f. Alternative Use of Ricardian Contract .................................................................................. 79 7. Proof of Burn and Reputation ................................................................................................... 80 VI. Security ...................................................................................................................................... 83 1. The challenge of Bitcoin Development ..................................................................................... 83 2. How to prove a Coin exists in the Blockchain ........................................................................... 83 3. How to prove a Colored Coin exists in the Blockchain .............................................................. 83 4. Breaking trust relationship with a third party API ..................................................................... 83 5. Preventing Malleability attacks ................................................................................................. 83 6. Protecting your private keys ..................................................................................................... 83 4 Nicolas Dorier Bill Strait CC ASA 3U I. Introduction 1. Foreword A passage in Fountain Head by Ayn Rand resonated with me. GAIL WYNAND, THE POWERFUL PUPPET MASTER OF THE WORLD, AND HOARK HOWARD, THE PROTAGONIST BUILDING ARCHITECT DISCUSSED TOGETHER. GAIL FINDS A STRANGE RELIEF WHEN HE IS WITH HOARK, NOT KNOWING WHERE IT COMES FROM, HE QUESTIONED HIM. WYNAND ASKED HOWARD, HAVE YOU EVER BEEN IN LOVE ROARK TURNED TO LOOK STRAIGHT AT HIM AND ANSWER QUICKLY I STILL AM. BUT WHEN YOU WALK THROUGH A BUILDING, WHAT YOU FEEL IS GREATER THAN THAT MUCH GREATER, GAIL I WAS THINKING OF PEOPLE WHO SAY THAT HAPPINESS IS IMPOSSIBLE ON EARTH. LOOK HOW HARD THEY ALL TRY TO FIND SOMEONE JOY IN LIFE. LOOK HOW THEY STRUGGLE FOR IT. WHY SHOULD ANY LIVING CREATURE EXIST IN PAIN BY WHAT CONCEIVABLE RIGHT CAN ANYONE DEMAND THAT A HUMAN BEING EXIST FOR ANYTHING BUT HIS OWN JOY EVERY ONE OF THEM WANTS IT. EVERY PART OF HIM WANTS IT. BUT THEY NEVER FIND IT. I WONDER WHY. THEY WHINE AND SAY THEY DONT UNDERSTAND THE MEANING OF LIFE. THERES A PARTICULAR KIND OF PEOPLE THAT I DESPISE. THOSE WHO SEEK SOME SORT OF A HIGHER PURPOSE OR ‘UNIVERSAL GOAL, WHO DONT KNOW WHAT TO LIVE FOR, WHO MOAN THAT THEY MUST ‘FIND THEMSELVES.’ YOU HEAR IT ALL AROUND US. THAT SEEMS TO BE THE OFFICIAL BROMIDE OF OUR CENTURY. EVERY BOOK YOU OPEN. EVERY DROOLING SELF-CONFESSION. IT SEEMS TO BE THE NOBLE THING TO CONFESS. ID THINK IT WOULD BE THE MOST SHAMEFUL ONE. LOOK, GAIL. ROARK GOT UP, REACHED OUT, TORE A THICK BRANCH OFF A TREE, HELD IT IN BOTH HANDS, ONE FIST CLOSED AT EACH END; THEN, HIS WRISTS AND KNUCKLES TENSED AGAINST THE RESISTANCE, HE BENT THE BRANCH SLOWLY INTO AN ARC. NOW I CAN MAKE WHAT I WANT OF IT A BOW, A SPEAR, A CANE, A RAILING. THATS THE MEANING OF LIFE. YOUR STRENGTH” YOUR WORK. HE TOSSED THE BRANCH ASIDE. THE MATERIAL THE EARTH OFFERS YOU AND WHAT YOU MAKE OF IT... I think the Blockchain is like the tree branch. For outsiders, it feels like a boring and useless collection of bits. For programmers and entrepreneurs, it is a marvelous raw material that can be shaped with our imagination. We give it meaning and purpose. Just as you need to know about wood to make a bow, spear or cane from a branch, you need to learn about programming to shape the Blockchain. My hope is that you will discover how much your skill and intelligence can shape that useless collection of bits. Let me warn you learning about the Blockchain is like taking the red pill from The Matrix. You may find yourself ready to quit your job to work on it full time. This book will take you from basic to advanced use of the Blockchain. It will not teach you how to use an API such as the RPC API provided with Bitcoin Core, but it will teach you how to make such an API. FACT Satoshi Nakamoto once described Bitcoin as “boring grey in colour.” 5 Nicolas Dorier Bill Strait CC ASA 3U While programming to an API can assist in getting an application up quickly, the developer is limited to innovations that can take place against the API. By fully understanding the Blockchain, the developer is empowered to unleash its full potential. 2. Why Blockchain Programming and not Bitcoin Programming The Blockchain is to gold what Bitcoin is to jewelry. We did not compare Bitcoin to a gold coin, but rather with a jewelry. That’s because gold’s first killer app was jewelry. Coins came later. Do not be fooled into thinking that Bitcoin is flawed while the Blockchain is valuable. If gold is valuable, would you throw away a gold necklace The Blockchain is built on and thrives because of bitcoin. Any increase in value of the Blockchain will increase the amount of Bitcoin that is spent to use it, which will increase its demand. Whether or not your app will use the “Bitcoin as a currency” feature is your own decision. Blockchain is the raw material. Bitcoin is the fuel. Bitcoin as a currency is a feature that emerges every time someone thinks this fuel is also a good medium of exchange. You can do a lot more with the Blockchain than exchange value. You don’t even have to believe in the currency. We will show you how to use Bitcoin as a currency in this book, but that’s not all 3. Why C The .NET framework is popular in corporate environments. We also believe this is the perfect tool for startups and hobbyists. .NET can create portable code that functions across IOS, Android, Windows tablets/phone, desktops, servers and embedded devices. Everything from the compiler to the core runtime is open source. The BizSpark program allows any startup to get all Microsoft tools, including 150/month of Azure service, for free. Visual Studio Community 2013 is a professional grade IDE that you can use freely as hobbyist. C is closely related to Java and C. As such, it can be easily read by developers who already know C syntax. Nicolas Dorier, one of the authors of this book, created the most popular Bitcoin Framework for .NET, called NBitcoin. You can find it here https// The authors of this book have over 15 years combined experience with C. It is our go-to language for any project for fun or profit. Fact We have not been paid by Microsoft. It’s not too late to change that. 4. Pre-requisites a. Skills You need to be comfortable with object oriented as well as functional programming. 6 Nicolas Dorier Bill Strait CC ASA 3U A basic grasp of C is helpful, but we feel the code will be legible to Java and other C-based languages. No mathematic knowledge is required. We will not cover cryptography beyond the bare minimum that you need to know to make a secure service. You don’t need to have deep knowledge of Bitcoin. We do recommend reading Mastering Bitcoin by Andreas Antonopoulos for extra credit. b. Tools Visual Studio 2013 - You can get it for free by searching for “Visual studio 2013 community” on Google Bing. Bitcoin Core - You should have this synchronized before beginning. Fact You can ask Microsoft’s Cortan
展开阅读全文

最新标签

网站客服QQ:123120571
环境100文库手机站版权所有
经营许可证编号:京ICP备16041442号-6