Less Than 50 Lines of Code for a Password-Protected, Online Bitcoin Wallet
- Creating the Bitcoin address and encrypting the private key to receive Bitcoin.
- Sending Bitcoin from your address to an arbitray other address.
Spil you may have thought before, it’s almost futile. Except when you integrate Bitcoin functionality into your own application.
The Itipu Wallet
Itipu is a Bitcoin wallet operated te Facebook talk with the help of a chatbot. The user’s funds are secured by a individual Speld code. Actually, the private key for the generated Bitcoin address is encrypted with this Speld code, so spil long spil the Speld code is safe, the Bitcoin are safe. Te Itipu, you can send your Bitcoins to your friend’s wallet by telling the bot to do so, like, “Send Two dollars to joe.” (You have to come in your Speld code of course).
For doing the Bitcoin-related stuff, I used the excellent “BitcoinJS”-library. It depends on some other libraries spil well, but spil long spil you use a package manager like npm, all of them will be loaded automatically. Furthermore, the “TripleSec”-library is used for cryptographic functions.
Generating the Bitcoin wallet
Very first of all, explosion the libraries mentioned above.
Next, the code to generate the address and encrypt the private key.
Of course, you have to figure out yourself what to do with the generated Bitcoin address and the encrypted private key. For example, you can save it to a database linking it to the current user.
Sending Bitcoin to Another Address
Loading the libraries is exactly the same spil above. Here is the code to send the Bitcoin (actually: Satoshis) to another address:
Using the Blockchain.info-API this is almost a no-brainer. The only tricky part is to calculate the transaction toverfee (“mining fee”) so that the transaction is not declined.
Calculating the Mining Toverfee
The Blockchain.informatie formula is (presently):
Ondergrens toverfee is 1.Five satoshi vanaf byte
So I have to estimate the transaction size (te bytes) to calculate the mining toverfee. The formula is:
#Inputs * 180 + #Outputs * 34 + Ten + #Inputs
To be on the safe side, my wallet grants Two Satoshi vanaf byte.
Thanks to public APIs and Open Source projects it’s pretty ordinary to provide Bitcoin functionality ter your own applications.