A beginner web developer can create a simple key pair by just using:
bitcore = require("bitcore-lib"); privateKey = new bitcore.PrivateKey // Generate a random private key privateKey.toString() >> "23bc740f87bd68729e794eb48b5137150f17109f855a34512c3c5f93d7498291"
The privateKey comes with ample of build in methods, most of which are more than enough for basic Bitcoin implementation in many web applications.
privateKey.toAddress().toString() >> "1LY75mCpuD3xPfLLFwbWccBNoQQY4VBeQj" privateKey.toPublicKey().toString() >> "0379d42509499082436c89a4e1637ed14a27e56174843bddb980ab6f155f82431c"
This codes can be implemented in a manner of minutes. Bitcoin can be incorporated into many web applications in less than a day. However, those of you with a keen eye to Bitcoin protocol probably noticed that the public key comes in its compressed format. The uncompressed public key will be twice as long (64 bytes + 1 byte) and will have 0x04 as their leading byte.
This is, of course, a non-issue for most use cases, but it might be somewhat counterproductive when learning the relations between private keys, public keys, and address.
To truly understand something, there’s no better way than to get your hands dirty. And when we’re referring to something as crucial to Bitcoin (and blockchains in general) as key pairs, no shortcuts should be made.
During my first experimentation with this library, I received the impression that, as good as this library is, it might not be sufficient to teach and learn the underlying mechanism of the Bitcoin blockchain itself.
However, after more careful examination I came to realize that this library provides much more granular tools than what I expected. In fact, it seems that I can use it in conjunction to the developer documentation on Bitcoin addresses.
This is done thanks to one specific method that returns the numeric value of each point in the public key (don’t forget, the uncompressed public key is just a set of x-y coordinates).
This is highly useful method for a number of reasons:
- Each point is given independently; as a result, the students are more aware of the real meaning of what a public key is really is (in terms of x-y coordinates). It’s also an excellent opportunity to show the students how one coordinate can be dropped to achieve the compressed address.
Another two very useful methods are:
bitcore.crypto.Hash.sha256ripemd160(pubKey); bitcore.crypto.Hash.sha256sha256(hashedPublicKey); The first two methods makes the hashing process really easy to execute. The Bitcoin protocol requires double hashing to be executed at two different locations in order to turn the public key to Bitcoin address. In many other languages (such as Python, which I've been using extensively when teaching this subject), there's a need to import and define each hash function. The process is tedious, and at the end of the day, somewhat messy.
By having these two methods, we save almost 40% in code space, and the students can focus on what’s really important in this lesson.
And here’s the final result:
Now compare to what we might see in Python:
Private_key = bytes.fromhex("BF9795D3FCB4E2181B7B536C2247EA0001397C99BA94D4D4DD62801BB151B091") import ecdsa signing_key = ecdsa.SigningKey.from_string(Private_key, curve = ecdsa.SECP256k1) verifying_key = signing_key.get_verifying_key() public_key = bytes.fromhex("04") + verifying_key.to_string() import hashlib sha256_1 = hashlib.sha256(public_key) ripemd160 = hashlib.new("ripemd160") ripemd160.update(sha256_1.digest()) hashed_public_key = bytes.fromhex("00") + ripemd160.digest() checksum_full = hashlib.sha256(hashlib.sha256(hashed_public_key).digest()).digest() checksum = checksum_full[:4] bin_addr = hashed_public_key + checksum import base58 FINALE_BTC_ADDRESS = base58.b58encode(bin_addr)
In this instance, we can see that the code created with bitcore js is much more straightforward and readable than even the simplest Python code.
The libraries can be used when teaching both web developers as well as more protocol oriented students. The code can be presented in highly simplified manner for beginners while still alluding
to its origin and the logic behind it.