def equal(stream, machine): x1 = machine.pop() x2 = machine.pop() if x1 == x2: machine.push(bytestream.fromunsigned(1)) else: machine.push(bytestream.fromunsigned(0))
def encode(self): stream = bytestream.bytestream("") stream += bytestream.bytestream(self.prev_hash).reverse() stream += bytestream.fromunsigned(self.index, 4) stream += bytestream.fromvarlen(self.script_length) stream += self.script.stream() stream += bytestream.fromunsigned(self.sequence, 4) return stream
def opnot(stream, machine): x = machine.pop() if x.signed() == 0: machine.push(bytestream.fromunsigned(1,len(x))) elif x.signed() == 1: machine.push(bytestream.fromunsigned(0,len(x))) else: machine.push(bytestream.fromunsigned(0,1))
def checksig(stream, machine, transaction, index, subscript): """ For details, please see https://en.bitcoin.it/wiki/OP_CHECKSIG. """ # How it works # Firstly always this (the default) procedure is # applied: Signature verification process of the default procedure # the public key and the signature are popped from the stack, in # that order. If the hash-type value is 0, then it is replaced by # the last_byte of the signature. Then the last byte of the # signature is always deleted. pubkey = machine.pop() sig = machine.pop() #sig.stream = sig.stream[:-2] # this is actually done later # A new subscript is created from the instruction from the most # recently parsed OP_CODESEPARATOR (last one in script) to the end # of the script. If there is no OP_CODESEPARATOR the entire script # becomes the subscript (hereby referred to as subScript) # not implemented yet (james) # The sig is deleted from subScript. # note: this is nonstandard so I am ignoring it (james) # All OP_CODESEPARATORS are removed from subScript # not implemented yet (james) # The hashtype is removed from the last byte of the sig and stored (as 4 bytes) hashtype = bytestream.fromunsigned(bytestream.bytestream(sig.stream[-2:]).unsigned(),4) sig.stream = sig.string()[:-2] # A copy is made of the current transaction (hereby referred to txCopy) txCopy = copy.deepcopy(transaction) # The scripts for all transaction inputs in txCopy are set to empty scripts (exactly 1 byte 0x00) for i in xrange(txCopy.tx_in_count): txCopy.tx_in[i].script_length = 0 txCopy.tx_in[i].script = script.script(bytestream.fromunsigned(0,1)) # The script for the current transaction input in txCopy is set to subScript (lead in by its length as a var-integer encoded!) txCopy.tx_in[index-1].script_length = len(subscript) txCopy.tx_in[index-1].script = subscript # Serialize txCopy and append hashtype serial = txCopy.encode() + hashtype # hash twice with sha256 msg = ((hashlib.sha256(hashlib.sha256(serial.decode()).digest()).digest()))# [::-1]).encode('hex_codec') # verify via ecdsa key = btct.decompress(pubkey.stream) vk = ecdsa.VerifyingKey.from_string(key[2:].decode('hex'), curve=ecdsa.SECP256k1) try: vk.verify_digest(sig.decode(), msg, sigdecode=ecdsa.util.sigdecode_der) machine.push(bytestream.fromunsigned(1,1)) except ecdsa.BadSignatureError: machine.push(bytestream.fromunsigned(0,1))
def encode(self): stream = bytestream.bytestream("") stream += bytestream.fromunsigned(self.version, 4) stream += bytestream.fromvarlen(self.tx_in_count) for tx_in in self.tx_in: stream += tx_in.encode() stream += bytestream.fromvarlen(self.tx_out_count) for tx_out in self.tx_out: stream += tx_out.encode() stream += bytestream.fromunsigned(self.lock_time, 4) return stream
def encode(self): stream = bytestream.bytestream('') stream += bytestream.fromunsigned(self.version, 4) stream += bytestream.fromvarlen(self.tx_in_count) for tx_in in self.tx_in: stream += tx_in.encode() stream += bytestream.fromvarlen(self.tx_out_count) for tx_out in self.tx_out: stream += tx_out.encode() stream += bytestream.fromunsigned(self.lock_time, 4) return stream
def __init__(self, payload): self.magic = bytestream.bytestream("F9BEB4D9") #self.command = bytestream.bytestream("000000000000000000000000") self.command = bytestream.bytestream("747800000000000000000000") #self.command = bytestream.bytestream("000000000000000000000000") self.length = bytestream.fromunsigned(len(payload)) first_hasher = hashlib.new('sha256') first_hasher.update(payload.decode()) second_hasher = hashlib.new('sha256') second_hasher.update(first_hasher.digest()) hashed = bytestream.bytestream(second_hasher.hexdigest()) self.checksum = hashed.peek(4) self.payload = payload
def __init__(self, *args): if len(args) == 0: self.prev_hash = str(bytestream.fromunsigned(0, 32)) self.index = 1 self.script_length = 0 self.script = None self.sequence = 0 self.is_coinbase = True elif len(args) == 1: stream = args[0] self.prev_hash = str(stream.read(32).reverse().stream) self.index = stream.read(4).unsigned() self.script_length = stream.readvarlensize() self.script = script.script(stream.read(self.script_length)) self.sequence = stream.read(4).unsigned() if int(self.prev_hash, 16) == 0: self.is_coinbase = True else: self.is_coinbase = False else: raise ValueError("Zero or one args")
def __init__(self, *args): if len(args) == 0: self.prev_hash = str(bytestream.fromunsigned(0,32)) self.index = 1 self.script_length = 0 self.script = None self.sequence = 0 self.is_coinbase = True elif len(args) == 1: stream = args[0] self.prev_hash = str(stream.read(32).reverse().stream) self.index = stream.read(4).unsigned() self.script_length = stream.readvarlensize() self.script = script.script(stream.read(self.script_length)) self.sequence = stream.read(4).unsigned() if int(self.prev_hash,16) == 0: self.is_coinbase = True else: self.is_coinbase = False else: raise ValueError("Zero or one args")
def encode(self): stream = bytestream.bytestream("") stream += bytestream.fromunsigned(self.value, 8) stream += bytestream.fromvarlen(self.script_length) stream += self.script.stream() return stream
def numpusher(stream, machine): data = bytestream.fromunsigned(x) machine.push(data) return data