<metaname="description"content="Learn how SIGHASH flags work in Bitcoin by building out a custom crowdfunding transaction that lets anyone add their own inputs to a transaction with a fixed output.">
<metaname="author"content="Buck Perley">
<title>bcoin | Extending Bitcoin into Enterprise & Production</title>
<title>Create a Crowdfunding Transaction</title>
<!-- Favicons -->
<!-- old
@ -318,11 +318,7 @@
<p>ALL|ANYONECANPAY<br>This construction can be used to make a "crowdfunding”-style transaction. Someone attempting to raise funds can construct a transaction with a single output. The single output pays the "goal" amount to the fundraiser. Such a transaction is obviously not valid, as it has no inputs. However, others can now amend it by adding an input of their own, as a donation. They sign their own input with ALL|ANYONECANPAY. Unless enough inputs are gathered to reach the value of the output, the transaction is invalid. Each donation is a "pledge," which cannot be collected by the fundraiser until the entire goal amount is raised.</p>
</blockquote>
<h2id="the-code">The Code</h2><p>We'll walk through the steps of creating the transaction first without any wallet database or node running. Then we'll do the same thing using bcoin's walletdb to manage the keys to see how it would work in a more realistic application (skip to <ahref="#version-2-using-the-bcoin-wallet-system">Version 2</a>) further in the guide to check it out). At the end, we'll put out some ideas of how these can be built upon for a more robust, production ready application. (If this is something you'd be interested in building, <ahref="http://bcoin.io/slack-signup.html">get in touch</a>!). If you want to see the code, checkout the <ahref="https://github.com/Bucko13/bitcoin-fundraise">repo on github</a>.</p>
<<<<<<<HEAD
<p>If you're not comfortable with key management, coin selection, and how transactions are constructed, checkout the tutorial on <ahref="http://bcoin.io/guides/working-with-txs.html">working with transactions</a> first.</p>
=======
<p>If you're not comfortable with key management, coin selection, and how transactions are constructed, checkout the tutorial on <ahref="https://github.com/bcoin-org/bcoin/blob/master/docs/Working-with-transactions.md">working with transactions</a> first.</p>
>>>>>>> staging
<h3id="version-1--manual-key-management">Version 1 - Manual Key Management</h3><h4id="step-1-setup">Step 1: Setup</h4><p>Let's first start by importing the right tools, setting up some constants, and creating our keychains. (make sure you've installed the latest version of bcoin into your project with <code>npm install bcoin</code>).</p>
<p>Note that we're setting the fundingTarget and amountToFund as constants for simplicity, but they could be set based on user input or some other variable circumstances.</p>
<spanclass="token punctuation">}</span></code><buttonclass="copy-button"><imgsrc="../assets/images/clippy.svg"alt="Copy to clipboard"></button></pre><h4id="step-3-prepare-your-coins">Step 3: Prepare your Coins</h4><p>Above, we just funded our funder accounts with a single 5BTC outpoint. This means that the next transaction funded from these accounts can only use that one outpoint (or <em>coin</em>) as an input and send the remainder back as change. Remember, in Bitcoin the way you send funds is you fund a transaction with a full UTXO (in this case we only have one worth 5BTC available to our keychains) and then send the change back to yourself as an additional output. Since ALL|ANYONECANPAY transactions mean a fixed output, you can't add new change outputs without other signatures becoming invalid which means we need a coin available equal to the amount we want to contribute to the crowdfund.</p>
=======
<spanclass="token punctuation">}</span></code><buttonclass="copy-button"><imgsrc="../assets/images/clippy.svg"alt="Copy to clipboard"></button></pre><h4id="step-4-prepare-your-coins">Step 4: Prepare your Coins</h4><p>Above, we just funded our funder accounts with a single 5BTC outpoint. This means that the next transaction funded from these accounts can only use that one outpoint (or <em>coin</em>) as an input and send the remainder back as change. Remember, in Bitcoin the way you send funds is you fund a transaction with a full UTXO (in this case we only have one worth 5BTC available to our keychains) and then send the change back to yourself as an additional output. Since ALL|ANYONECANPAY transactions mean a fixed output, you can't add new change outputs without other signatures becoming invalid which means we need a coin available equal to the amount we want to contribute to the crowdfund.</p>
>>>>>>> staging
<p>So what we want to do is have each funder create a coin (UTXO) with the value of what they want to donate.</p>
<p>The first thing we need to do make this work is calculate what the input will be. In our examples we are assuming that the funders cover the fee. Since different keyrings can be using different transaction types of different sizes (p2sh, multisig, etc.), we need a utility to calculate how much the fee should be for that input and add that to the amount to fund with.</p>
<h5id="utility-functions">Utility functions</h5><p>We'll need some utility functions to help us out. It's nice to split these out separate from our main operations since we'll actually be reusing some of the functionality.</p>
<spanclass="token punctuation">}</span></code><buttonclass="copy-button"><imgsrc="../assets/images/clippy.svg"alt="Copy to clipboard"></button></pre><h4id="step-4-construct-the-transaction">Step 4: Construct the Transaction</h4><p>Now that we've got our tools and coins ready, we can start to build the transaction!</p>
=======
<spanclass="token punctuation">}</span></code><buttonclass="copy-button"><imgsrc="../assets/images/clippy.svg"alt="Copy to clipboard"></button></pre><h4id="step-6-construct-the-transaction">Step 6: Construct the Transaction</h4><p>Now that we've got our tools and coins ready, we can start to build the transaction!</p>
<spanclass="token punctuation">.</span><spanclass="token keyword">catch</span><spanclass="token punctuation">(</span>e <spanclass="token operator">=</span><spanclass="token operator">></span> console<spanclass="token punctuation">.</span><spanclass="token function">log</span><spanclass="token punctuation">(</span><spanclass="token string">'There was a problem: '</span><spanclass="token punctuation">,</span> e<spanclass="token punctuation">)</span><spanclass="token punctuation">)</span><spanclass="token punctuation">;</span></code><buttonclass="copy-button"><imgsrc="../assets/images/clippy.svg"alt="Copy to clipboard"></button></pre><p>And there you have it! If you were doing this on testnet, your <code>fundeeWallet</code> should now be 1BTC richer. If you're on a simnet or regtest network, you'll have to mine a block with your transactions to get those funds confirmed. Also note that, unless you have exact change coins, there will be 3 transactions that need to be confirmed: one each for the wallets that are splitting coins, and one for the crowdfund transaction.</p>
<<<<<<<HEAD
<h2id="how-to-extend-and-improve">How to Extend and Improve</h2><p>These examples are obviously pretty basic, but they should give you an idea of how to use Bitcoin's scripting to build out the foundation for more complex applications. Here are some ideas on how you could build on top of these examples and get closer to a production ready application.</p>
=======
<h2id="how-to-build-it-out">How to Build it Out</h2><p>These examples are obviously pretty basic, but they should give you an idea of how to use Bitcoin's scripting to build out the foundation for more complex applications. Here are some ideas on how you could build on top of these examples and get closer to a production ready application.</p>
>>>>>>> staging
<ul>
<li>More flexible contribution scheme (currently it's just 2 funders that split the amount evenly). E.g. custom number of contributers, custom contribution amount, etc.</li>
<li>UX to let people interact with the transaction via a browser</li>