# Bearcoin protocol implementation

## The cryptocurrency that's upfront about how bearish you should be

Bearcoin was a cryptocurrency experiment created during weekend of Hack at Brown 2015. The goal was to be a simple explanatory tool for how basic blockchains work, as well as a prototype for a cryptocurrency that could be used at the Ratty (“That sounds silly” you say? Well we did name it BearCoin. I says right on the tin that you should be bearish about it).

(Like any cryptocurrency project, what’s important is that we have an impressive logo for our project to distract from the fact that it’s Bitcoin without Bitcoin’s original selling point)

The development of bearcoin followed a few main stages:

The first stage of development was managing transactions through a trusted authority, and later graduating to a UTXO model of transaction-tracking. An unspent transaction output (UTXO) is basically the output of a transaction that a user receives and is able to spend in the future. This is true because, as the name suggests, it is the unspent output of a transaction. But what does this mean, exactly? An analogy will be helpful in making UTXO clear.

Each and every UTXO is like a single fiat coin or a single fiat bill. If you have $\ 45$ in cash, you must have more than one bill because there’s no such thing as a forty-five dollar bill. So while you have $\ 45$ dollars in your wallet, you may have any number of combination of bills— UTXO— sitting in your wallet.

Bearcoin started out as a centralized transaction authority that handled UTXOs (much like any centralized e-cash). After this, data structures for the Blockchain blocks were created and a transaction verification process was added.

### How do we run it

Bearcoin has a lovely script test_bear.py, that you can run for similating transactions and getting a sense forthe network topology. You can also import directly from the files themselves. There are a few main high-level concepts to keep in mind when using it:

Bear

• The Bear is a trusted entity that creates coins, manages the blockchain and approves payments.

Coin creation

• Only the Bear can create coins.

Payment

• Whoever owns a coin can transfer it on to someone else.

• Payments must be signed by all the owners whose coins are consumed in the transaction.

• The Bear verifies signatures and checks double-spending before approving the transaction.

• When a coin is consumed it is deleted and other coin with the new owner is created.

• The amount of created coins in a payment must be equal to the amout of consumed coins.

Blockchain

• Transactions are inserted by the Bear into the blockchain.
• The Bear publishes the blockchain along with the signature of the last block. Wallet
• A wallet is identified by the sha256 hash of its public key.

#### Creating a Bear

A wallet is assigned to a Bear when it is created. The blockchain is created too, and the genesis block has a transaction that creates a coin assigned to the Bear’s wallet.

from bear import Bear
from bearcoin import Bearcoin

bear = Bear()
print(bear.wallet)
print(bear.blockchain)

#### Bear Creation Output

Wallet
------------------------------
------------------------------

Blockchain
------------------------------
Block: 0 Hash previous block: None
TransID: 0 Type: Coin creation

Created coins:
Num: 0, Value: 1, Wallet id: ae070ad51632631a3277dfb714be0685c7b23f2c44870e54cf01035174177b8e
------------------------------

#### Coin creation

Coins have an id, a value and the wallet id of the owner. The coin id is assigned by the Bear when the transaction that creates the coin is inserted in the blockchain. This id is composed by its transaction id and the correlative number of the coin into the transaction.

from wallet import Wallet
from bear import Bear
from bearcoin import Bearcoin

wallet_1 = Wallet()
coins = [
Bearcoin(value=200, wallet_id=wallet_1.id),
Bearcoin(value=500, wallet_id=wallet_1.id)
]
bear.create_coins(coins)
print(bear.blockchain)

#### Coin Creation Output

Blockchain
------------------------------
Block: 0 Hash previous block: None
TransID: 0 Type: Coin creation

Created coins:
Num: 0, Value: 1, Wallet id: ae070ad51632631a3277dfb714be0685c7b23f2c44870e54cf01035174177b8e
------------------------------
Block: 1 Hash previous block: 9d05ebd413c43272c6da3fb5045e3b795dd94016c7844ef23398bc5782fa1270
TransID: 1 Type: Coin creation

Created coins:
Num: 0, Value: 200, Wallet id: 78004f2a0206c7d7bb143e3ac27d74f420f35ecdaacd4dec767c430ccc83f01e
Num: 1, Value: 500, Wallet id: 78004f2a0206c7d7bb143e3ac27d74f420f35ecdaacd4dec767c430ccc83f01e
------------------------------

#### Payment

The following code executes a 600 bearcoins payment from wallet_1 to wallet_2. The payment is created and sent to the Bear along with the verifying key and the signature of the wallet_1.

wallet_2 = Wallet()

pay_coin = Bearcoin(value=600, wallet_id=wallet_2.id)

payment = Payment(created_coins=[pay_coin], consumed_coins=created_coins)

signature = wallet_1.sign(encoded_hash_object(payment))

payment_result = bear.process_payment(

payment, [(wallet_1.verifying_key, signature)]

)

print(bear.blockchain)

Output

The output shows that one coin had to be “devided” to pay the 600 bearcoins. The payment of the block 2 represents the coin division, in which the source wallet is equal to the destination wallet, and a coin with value 500 was devided into two coins with values 400 and 100. The block 3 contains the transfer between wallet1 and wallet2.

Blockchain
------------------------------
Block: 0 Hash previous block: None
TransID: 0 Type: Coin creation

Created coins:
Num: 0, Value: 1, Wallet id: 6d5a0eab2d03a8a7d033f02ddaf3688196d26c51cafebdba331910ca06f232dd
------------------------------
Block: 1 Hash previous block: 7c2d2f27a859b498315e58c4cffb026582be82006c0d39b37a4f26151c86db73
TransID: 1 Type: Coin creation

Created coins:
Num: 0, Value: 200, Wallet id: 26a9a4697e671a6ee06ae447536cb35ad1cf6ba39df80cf21f791bea175799c1
Num: 1, Value: 500, Wallet id: 26a9a4697e671a6ee06ae447536cb35ad1cf6ba39df80cf21f791bea175799c1
------------------------------
Block: 2 Hash previous block: 1a13477397e4572ecd6f60c34b681d46a5c1ddb4570ee21c9b3cd4bf983ec8ba
TransID: 2 Type: Payment

Consumed coins:
Num: 1, Value: 500, Wallet id: 26a9a4697e671a6ee06ae447536cb35ad1cf6ba39df80cf21f791bea175799c1

Created coins:
Num: 0, Value: 400, Wallet id: 26a9a4697e671a6ee06ae447536cb35ad1cf6ba39df80cf21f791bea175799c1
Num: 1, Value: 100, Wallet id: 26a9a4697e671a6ee06ae447536cb35ad1cf6ba39df80cf21f791bea175799c1
------------------------------
Block: 3 Hash previous block: d8b94a19925f9b59bf15ea6fbf63d10a867ae398f8e43738ec2ed3a73abbd0fb
TransID: 3 Type: Payment

Consumed coins:
Num: 0, Value: 200, Wallet id: 26a9a4697e671a6ee06ae447536cb35ad1cf6ba39df80cf21f791bea175799c1
Num: 0, Value: 400, Wallet id: 26a9a4697e671a6ee06ae447536cb35ad1cf6ba39df80cf21f791bea175799c1

Created coins:
------------------------------  

### Is that all it takes to bake a cryptocurrency?

Everything I showed right here is an incredibly simplistic overview of how cryptocurrencies work. Obviously I had to strip away many of the bells and whistles that Bitcoin has (like the SHA256 hashing), and that other coins like LiteCoin have

Ultimately, what’s needed for any decent cryptocurrency project (or for that matter even a fiat currency) is belief in the system itself. The reason Bitcoin can be used as a currency was because people like Lazlo Hanyecz bought two pizzas for 10000 Bitcoins, setting an actual value to it. People might poke fun at him for spending what today would be equivalent to \$2,950,000 on two pizzas, but the reason they have so much value in the first place is because he spent those bitcoins on the pizzas.

Meanwhile Bearcoin is still very far from being used for pizzas at the Ratty.

### So how did Bearcoin do at Hack@Brown?

You know those programming projects where you get so immersed in what you’re doing that you forget the original goal?

Yeah, that’s pretty much what happened with BearCoin. I just missed the submission deadline. Considering how it was also a very basic project, I doubt it would have gotten as far as the projects that actually did win.

Still, this was an exciting challenge, and this was a fantastic time to learn much more in-depth how cryptocurrencies work by building one myself.

Subscribe to know whenever I post new content. I don't spam!

#### At least this isn't a full screen popup

That would be more annoying. Anyways, if you like what you're reading, consider subscribing to my newsletter! I'll notify you when I publish new posts - no spam.