Ejemplo n.º 1
0
def get_blocks2(n):
    height = yield blockchain('q/getblockcount')
    
    res = []
    
    for i in xrange(n):
        x = yield blockchain('block-height/%i?format=json' % (height - i,))
        for block in x['blocks']:
            #print block
            header = dict(
                version=block['ver'],
                previous_block=int(block['prev_block'], 16),
                merkle_root=int(block['mrkl_root'], 16),
                timestamp=block['time'],
                bits=bitcoin_data.FloatingInteger(block['bits']),
                nonce=block['nonce'],
            )
            assert bitcoin_data.hash256(bitcoin_data.block_header_type.pack(header)) == int(block['hash'], 16)
            
            
            # there seems to be no way to get the raw transaction
            # from blockchain.info (?format=hex doesn't work for
            # coinbase transctions ): so instead fake it
            
            
            txs = [dict(
                version=tx['ver'],
                tx_ins=[dict(
                    previous_output=None,
                    script='',
                    sequence=0,
                ) for tx_in in tx['inputs']],
                tx_outs=[dict(
                    value=tx_out['value'],
                    script='\x6a' + 'blah'*100 if tx_out['type'] == -1 else
                        p2pool_data.DONATION_SCRIPT if tx_out['addr'] == bitcoin_data.script2_to_address(p2pool_data.DONATION_SCRIPT, net) else
                        bitcoin_data.pubkey_hash_to_script2(bitcoin_data.address_to_pubkey_hash(tx_out['addr'], net)),
                ) for tx_out in tx['out']],
                lock_time=0,
            ) for tx in block['tx']]
            
            #print txs[0]
            
            # fails because we don't have coinbase script ):
            #assert bitcoin_data.hash256(bitcoin_data.tx_type.pack(txs[0])) == block['tx'][0]['hash']
            
            block2 = dict(header=header, txs=txs)
            
            res.append(dict(
                block=block2,
                height=block['height'],
                gentx_hash=int(block['tx'][0]['hash'], 16),
            ))
    
    defer.returnValue(res)
Ejemplo n.º 2
0
def getwork(bitcoind, net, use_getblocktemplate=False):
    def go():
        if use_getblocktemplate:
            return bitcoind.rpc_getblocktemplate(dict(mode='template'))
        else:
            return bitcoind.rpc_getmemorypool()
    try:
        start = time.time()
        work = yield go()
        end = time.time()
    except jsonrpc.Error_for_code(-32601): # Method not found
        use_getblocktemplate = not use_getblocktemplate
        try:
            start = time.time()
            work = yield go()
            end = time.time()
        except jsonrpc.Error_for_code(-32601): # Method not found
            print >>sys.stderr, 'Error: Bitcoin version too old! Upgrade to v0.5 or newer!'
            raise deferral.RetrySilentlyException()
    packed_transactions = [(x['data'] if isinstance(x, dict) else x).decode('hex') for x in work['transactions']]
    packed_votes = [(x['data'] if isinstance(x, dict) else x).decode('hex') for x in work['votes']]
    if 'height' not in work:
        work['height'] = (yield bitcoind.rpc_getblock(work['previousblockhash']))['height'] + 1
    elif p2pool.DEBUG:
        assert work['height'] == (yield bitcoind.rpc_getblock(work['previousblockhash']))['height'] + 1
    defer.returnValue(dict(
        version=work['version'],
        previous_block=int(work['previousblockhash'], 16),
        transactions=map(bitcoin_data.tx_type.unpack, packed_transactions),
        transaction_hashes=map(bitcoin_data.hash256, packed_transactions),
        transaction_fees=[x.get('fee', None) if isinstance(x, dict) else None for x in work['transactions']],
        subsidy=work['coinbasevalue'],
        time=work['time'] if 'time' in work else work['curtime'],
        bits=bitcoin_data.FloatingIntegerType().unpack(work['bits'].decode('hex')[::-1]) if isinstance(work['bits'], (str, unicode)) else bitcoin_data.FloatingInteger(work['bits']),
        coinbaseflags=work['coinbaseflags'].decode('hex') if 'coinbaseflags' in work else ''.join(x.decode('hex') for x in work['coinbaseaux'].itervalues()) if 'coinbaseaux' in work else '',
        height=work['height'],
        last_update=time.time(),
        use_getblocktemplate=use_getblocktemplate,
        latency=end - start,
        votes=map(bitcoin_data.vote_type.unpack, packed_votes),
        payee=bitcoin_data.address_to_pubkey_hash(work['payee'], net.PARENT) if (work['payee'] != '') else None,
        masternode_payments=work['masternode_payments'],
        payee_amount=work['payee_amount'] if (work['payee_amount'] != '') else work['coinbasevalue'] / 5,
    ))
Ejemplo n.º 3
0
share_type = pack.ComposedType([
    ('type', pack.VarIntType()),
    ('contents', pack.VarStrType()),
])

def load_share(share, net, peer_addr):
    assert peer_addr is None or isinstance(peer_addr, tuple)
    if share['type'] < Share.VERSION:
        from p2pool import p2p
        raise p2p.PeerMisbehavingError('sent an obsolete share')
    elif share['type'] == Share.VERSION:
        return Share(net, peer_addr, Share.share_type.unpack(share['contents']))
    else:
        raise ValueError('unknown share type: %r' % (share['type'],))

DONATION_SCRIPT = bitcoin_data.pubkey_hash_to_script2(bitcoin_data.address_to_pubkey_hash('BJG379oBv9cn94cdz85E9K5ejRhrHcvKgs',networks.nets['buffalocoin']))

class Share(object):
    VERSION = 13
    VOTING_VERSION = 13
    SUCCESSOR = None
    
    small_block_header_type = pack.ComposedType([
        ('version', pack.VarIntType()),
        ('previous_block', pack.PossiblyNoneType(0, pack.IntType(256))),
        ('timestamp', pack.IntType(32)),
        ('bits', bitcoin_data.FloatingIntegerType()),
        ('nonce', pack.IntType(32)),
    ])
    
    share_info_type = pack.ComposedType([
Ejemplo n.º 4
0
 def test_address_to_pubkey_hash(self):
     assert data.address_to_pubkey_hash('1KUCp7YP5FP8ViRxhfszSUJCTAajK6viGy', networks.nets['bitcoin']) == pack.IntType(160).unpack('ca975b00a8c203b8692f5a18d92dc5c2d2ebc57b'.decode('hex'))
Ejemplo n.º 5
0
block_header = dict(
version=3,
previous_block=0xf260c39629d99355c5476d710d46ca0d35b3d962b44054b9ff943fe622,
merkle_root=0x916fb05aed6373d6e5c49626efc0dea5d103038135bffbc86014f5204df2ebe3,
timestamp=1367995782,
bits=data.FloatingInteger(0x1e0fffff),
nonce=58004,
)

DONATION_SCRIPT = '01210241b8aba0994f320a8b438c627dbf31fbdd7dc722dd8418d829d67a9c6e4fd69021036fbd9d0a34a569f10b0431c8aeecf74ad796b99838b7272ef35ded130a794f9b02ae'.decode('hex')
print DONATION_SCRIPT[2:35].encode('hex')
print data.pubkey_to_address(DONATION_SCRIPT[2:35], networks.nets['yacoin'])
print networks.nets['yacoin'].POW_FUNC(data.block_header_type.pack(block_header)) 

print data.pubkey_hash_to_script2(data.address_to_pubkey_hash('YJL3vTFn7m82zQRs7XAXcJXnBNNmZdb1Ty', networks.nets['yacoin'])).encode('hex')

donate = '4104ffd03de44a6e11b9917f3a29f9443283d9871c9d743ef30d5eddcd37094b64d1b3d8090496b53256786bf5c82932ec23c3b74d9f05a6f95a8b5529352656664bac'.decode('hex')
#print data.script2_to_address(donate, networks.nets['bitcoin'])
#print len("c8c6a3c0957d53698da14d7a2c176a133e92fc53".decode('hex'))
#print donate[1:-1].encode('hex')
#print data.pubkey_to_script2(donate[1:-1]).encode('hex')
#print donate[3:-2].encode('hex')
#print data.pubkey_hash_to_script2(donate[3:-2]).encode('hex')
#print len('0241b8aba0994f320a8b438c627dbf31fbdd7dc722dd8418d829d67a9c6e4fd690'.decode('hex'))
#print data.pubkey_to_script2('0241b8aba0994f320a8b438c627dbf31fbdd7dc722dd8418d829d67a9c6e4fd690'
#036fbd9d0a34a569f10b0431c8aeecf74ad796b99838b7272ef35ded130a794f9b
print data.base58_encode('cac8509b8f959d14253e934585c52d04bf4ae21d28e62414a0c71c9fb80a5713'.decode('hex'))
text = sha256.sha256('cac8509f4ae21d28e62414a0c71c9fb80a5713sdfsdfsdfsdfsfsdssssssssss')
print text.state
print text.buf
Ejemplo n.º 6
0
block_header = dict(
version=3,
previous_block=0xf260c39629d99355c5476d710d46ca0d35b3d962b44054b9ff943fe622,
merkle_root=0x916fb05aed6373d6e5c49626efc0dea5d103038135bffbc86014f5204df2ebe3,
timestamp=1367995782,
bits=data.FloatingInteger(0x1e0fffff),
nonce=58004,
)

DONATION_SCRIPT = '01210241b8aba0994f320a8b438c627dbf31fbdd7dc722dd8418d829d67a9c6e4fd69021036fbd9d0a34a569f10b0431c8aeecf74ad796b99838b7272ef35ded130a794f9b02ae'.decode('hex')
print DONATION_SCRIPT[2:35].encode('hex')
print data.pubkey_to_address(DONATION_SCRIPT[2:35], networks.nets['cachecoin'])
print networks.nets['cachecoin'].POW_FUNC(data.block_header_type.pack(block_header)) 

print data.pubkey_hash_to_script2(data.address_to_pubkey_hash('CP8qRQAESbU6CqJvsjh2tCSZWLQnhQd3n3', networks.nets['cachecoin'])).encode('hex')

donate = '4104ffd03de44a6e11b9917f3a29f9443283d9871c9d743ef30d5eddcd37094b64d1b3d8090496b53256786bf5c82932ec23c3b74d9f05a6f95a8b5529352656664bac'.decode('hex')
#print data.script2_to_address(donate, networks.nets['bitcoin'])
#print len("c8c6a3c0957d53698da14d7a2c176a133e92fc53".decode('hex'))
#print donate[1:-1].encode('hex')
#print data.pubkey_to_script2(donate[1:-1]).encode('hex')
#print donate[3:-2].encode('hex')
#print data.pubkey_hash_to_script2(donate[3:-2]).encode('hex')
#print len('0241b8aba0994f320a8b438c627dbf31fbdd7dc722dd8418d829d67a9c6e4fd690'.decode('hex'))
#print data.pubkey_to_script2('0241b8aba0994f320a8b438c627dbf31fbdd7dc722dd8418d829d67a9c6e4fd690'
#036fbd9d0a34a569f10b0431c8aeecf74ad796b99838b7272ef35ded130a794f9b
print data.base58_encode('cac8509b8f959d14253e934585c52d04bf4ae21d28e62414a0c71c9fb80a5713'.decode('hex'))
text = sha256.sha256('cac8509f4ae21d28e62414a0c71c9fb80a5713sdfsdfsdfsdfsfsdssssssssss')
print text.state
print text.buf
Ejemplo n.º 7
0
def get_payout_script(request, net):
    try:
        user = base64.b64decode(request.getHeader('Authorization').split(' ', 1)[1]).split(':')[0]
        return bitcoin_data.pubkey_hash_to_script2(bitcoin_data.address_to_pubkey_hash(user, net))
    except: # XXX blah
        return None