perone / protocoin

A pure Python Bitcoin protocol implementation
http://protocoin.readthedocs.org/
BSD 4-Clause "Original" or "Old" License
98 stars 34 forks source link

Support for altcoins #7

Closed snare closed 10 years ago

snare commented 10 years ago

This small patch makes it easier to build clients for other altcoins based on protocoin. Here is a sample Litecoin client based on the provided examples:

import socket
from protocoin.clients import *

class LitecoinClient(BitcoinClient):
    coin = "litecoin"

    def handle_message_header(self, message_header, payload):
        print "Received message:", message_header.command

    def handle_send_message(self, message_header, message):
        print "Message sent:", message_header.command

def run_main():
    sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    sock.connect(("82.27.193.66", 9333))
    client = LitecoinClient(sock)
    client.handshake()
    client.loop()

run_main()

To add other altcoins we'd just need to add to the MAGIC_VALUES dict.

perone commented 10 years ago

Hello @snarez, great thanks for the pull request. I haven't found any documentation about the Litecoin protocol, the protocol (messages and field formats) are the same of the Bitcoin protocol ? I ask this because if they are different there will be more changes to support it and this merge can cause a problem when a different message format arrives.=

snare commented 10 years ago

Yeah the protocols for altcoins are generally a direct clone of the original Bitcoin protocol. I haven't looked at anything apart from Litecoin and Dogecoin, but they both seem to work OK without modification apart from the magic number. If there are any protocol differences I think it will be fairly easy for people to subclass the existing packet classes and monkey patch things to make it work. I'm working on something at the moment that will talk to a lot of altcoin networks so I will see how it goes and submit further pull requests if there are other areas that require generalisation in order to support other coins.

snare commented 10 years ago

I'm currently adding other coins by this hackery:

ALTMAGIC = {
    "dogecoin":     0xc0c0c0c0
}
protocoin.fields.MAGIC_VALUES.update(ALTMAGIC)

So this is probably an area that could be generalised (or at least wrapped in some configuration functions), but I'll tackle it down the track if it seems worth it. This method will work for now. Great work on protocoin :)

perone commented 10 years ago

Thanks for the reply @snarez, I'll merge your changes and then later I'll generalize this. Thanks for the contribution !