def __init__(self, blockchain, prev_hash, miner_address): ''' Class for the current work block. Functions such as "output_used" search both this AND the passed blockchain object. ''' self.BLOCKCHAIN = blockchain.blockchain self.hash = "" self.block = {} #TODO get from IRC self.block['nonce'] = "" self.block['prev'] = prev_hash self.block['start_time'] = str(int(time())) self.block['solve_time'] = "" self.block['transactions'] = { "0000": { # Temp value "time": str(int(time())), 'ins': [], 'outs': [{ "address": miner_address, "amount": util.padint(REWARD_AMOUNT) }] } } # Set reward transaction hash self.block['transactions'][util.gen_tx_hash( self.block['transactions'] ["0000"])] = self.block['transactions']["0000"] del self.block['transactions']["0000"]
def sign_input(tx,input_i,privkey): assert len(privkey) == 3 input = tx['ins'][int(input_i)] msg = tx['time'] + input['tx'] + input['tx_i'] + input['out_i'] _output = tx['outs'][int(input['out_i'])] msg += _output['address'] + util.padint(_output['amount']) p = privkey[0] q = privkey[1] d = privkey[2] n = p * q e = crypto.PUBLIC_EXPONENT privkey = rsa.PrivateKey(n,e,d,p,q) return str(hexlify(rsa.sign(unhexlify(msg), privkey, crypto.SIG_HASH_METHOD)))[2:crypto.SIG_HASH_METHOD_LEN+2]
def tx_inputs_signed(self,tx): for input in tx['ins']: msg = tx['time'] + input['tx'] + input['tx_i'] + input['out_i'] _output = tx['outs'][int(input['out_i'])] msg += _output['address'] + util.padint(_output['amount']) try: input_trans = self.get_tx_h(input['tx']) pubkey = input_trans['outs'][int(input['tx_i'])]['address'] except TypeError: raise NoSuchInputException('Input nonexistant: ' + input['tx'] + "-" + input['tx_i']) try: rsa.verify(unhexlify(msg), unhexlify(input['sig']), rsa.PublicKey(int(pubkey,16), crypto.PUBLIC_EXPONENT)) except rsa.pkcs1.VerificationError: raise SignatureVerificationException("False signature: " + input['sig']) return True
def sign_input(tx, input_i, privkey): assert len(privkey) == 3 input = tx['ins'][int(input_i)] msg = tx['time'] + input['tx'] + input['tx_i'] + input['out_i'] _output = tx['outs'][int(input['out_i'])] msg += _output['address'] + util.padint(_output['amount']) p = privkey[0] q = privkey[1] d = privkey[2] n = p * q e = crypto.PUBLIC_EXPONENT privkey = rsa.PrivateKey(n, e, d, p, q) return str( hexlify(rsa.sign( unhexlify(msg), privkey, crypto.SIG_HASH_METHOD)))[2:crypto.SIG_HASH_METHOD_LEN + 2]
def tx_inputs_signed(self, tx): for input in tx['ins']: msg = tx['time'] + input['tx'] + input['tx_i'] + input['out_i'] _output = tx['outs'][int(input['out_i'])] msg += _output['address'] + util.padint(_output['amount']) try: input_trans = self.get_tx_h(input['tx']) pubkey = input_trans['outs'][int(input['tx_i'])]['address'] except TypeError: raise NoSuchInputException('Input nonexistant: ' + input['tx'] + "-" + input['tx_i']) try: rsa.verify( unhexlify(msg), unhexlify(input['sig']), rsa.PublicKey(int(pubkey, 16), crypto.PUBLIC_EXPONENT)) except rsa.pkcs1.VerificationError: raise SignatureVerificationException("False signature: " + input['sig']) return True
def __init__(self,blockchain,prev_hash,miner_address): ''' Class for the current work block. Functions such as "output_used" search both this AND the passed blockchain object. ''' self.BLOCKCHAIN = blockchain.blockchain self.hash = "" self.block = {} #TODO get from IRC self.block['nonce'] = "" self.block['prev'] = prev_hash self.block['start_time'] = str(int(time())) self.block['solve_time'] = "" self.block['transactions'] = {"0000":{ # Temp value "time":str(int(time())), 'ins':[], 'outs':[{ "address":miner_address, "amount":util.padint(REWARD_AMOUNT) }] } } # Set reward transaction hash self.block['transactions'][util.gen_tx_hash(self.block['transactions']["0000"])] = self.block['transactions']["0000"] del self.block['transactions']["0000"]