#!/usr/bin/python # Public Domain # Original author: ArtForz # Twisted integration: slush import struct import socket import binascii import time import sys import random import cStringIO from Crypto.Hash import SHA256 from twisted.internet.protocol import Protocol from util import * MY_VERSION = 31402 MY_SUBVERSION = ".4" class CAddress(object): def __init__(self): self.nTime = 0 self.nServices = 1 self.pchReserved = "\x00" * 10 + "\xff" * 2 self.ip = "0.0.0.0" self.port = 0 def deserialize(self, f): #self.nTime = struct.unpack("H", f.read(2))[0] def serialize(self): r = "" #r += struct.pack("H", self.port) return r def __repr__(self): return "CAddress(nServices=%i ip=%s port=%i)" % (self.nServices, self.ip, self.port) class CInv(object): typemap = { 0: "Error", 1: "TX", 2: "Block"} def __init__(self): self.type = 0 self.hash = 0L def deserialize(self, f): self.type = struct.unpack(" 21000000L * 100000000L: return False return True def __repr__(self): return "CTransaction(nVersion=%i vin=%s vout=%s nLockTime=%i)" % (self.nVersion, repr(self.vin), repr(self.vout), self.nLockTime) class CBlock(object): def __init__(self): self.nVersion = 6 self.hashPrevBlock = 0 self.hashMerkleRoot = 0 self.nTime = 0 self.nBits = 0 self.nNonce = 0 self.vtx = [] self.sha256 = None self.signature = b"" def deserialize(self, f): self.nVersion = struct.unpack(" target: return False hashes = [] for tx in self.vtx: tx.sha256 = None if not tx.is_valid(): return False tx.calc_sha256() hashes.append(ser_uint256(tx.sha256)) while len(hashes) > 1: newhashes = [] for i in xrange(0, len(hashes), 2): i2 = min(i+1, len(hashes)-1) newhashes.append(SHA256.new(SHA256.new(hashes[i] + hashes[i2]).digest()).digest()) hashes = newhashes if uint256_from_str(hashes[0]) != self.hashMerkleRoot: return False return True def __repr__(self): return "CBlock(nVersion=%i hashPrevBlock=%064x hashMerkleRoot=%064x nTime=%s nBits=%08x nNonce=%08x vtx=%s)" % (self.nVersion, self.hashPrevBlock, self.hashMerkleRoot, time.ctime(self.nTime), self.nBits, self.nNonce, repr(self.vtx)) class msg_version(object): command = "version" def __init__(self): self.nVersion = MY_VERSION self.nServices = 0 self.nTime = time.time() self.addrTo = CAddress() self.addrFrom = CAddress() self.nNonce = random.getrandbits(64) self.strSubVer = MY_SUBVERSION self.nStartingHeight = 0 def deserialize(self, f): self.nVersion = struct.unpack("