def createAtomicSwapAccount(self, callback = None): _address, _public, _private = Crypto.generateKeys() privateBytes = bytearray() privateBytes.extend(_address) privateBytes.extend(_public) privateBytes.extend(_private) privateKey = Crypto.generateHash(privateBytes) self.createNewMultiSigAccountType(AccountType.ATOMIC_SWAP, privateKey, callback)
def checkmultisig(self): outputHash = self.transaction.hashOutput(self.input, self.output) threshold = DataType.bytesToInt(self.output.extraData) match = 0 publicKeys = self.input.witness[::2] multiSigAddressBytes = bytearray() for publicKey in publicKeys: multiSigAddressBytes.extend(publicKey) multiSigAddress = Crypto.generateAddress(multiSigAddressBytes) if multiSigAddress == self.output.address: signatures = self.input.witness[1::2] for public, signature in zip(publicKeys, signatures): if Crypto.verify(public, signature, outputHash): match += 1 self.stack.append(match >= threshold)
def deploy(self, isLocal, callback): result = None fromAccount = Accounts.getAccountByAddress(self.fromAddress) if fromAccount != None: if isLocal: toAddress = Crypto.generateAddress(fromAccount.address) _script = bytearray() _script.append(Config.getIntValue("EXTENSION_SCRIPT_VERSION")) _script.extend(self.script) _script = DataType.serialize(_script) extraData = None if len(self.parameters) > 0: extraData = self.parameters extraData.append(fromAccount.address) output = Output(toAddress, _script, self.value, extraData) result = self.handleLocalScript(fromAccount, output, True) if result != None: Contracts.addContract(Contract(toAddress), False) else: result = MemoryPool.addUnconfirmedTransaction( fromAccount, None, self.value, self.gasLimit, self.gasPrice, self.script, self.parameters) try: result = DataType.toHex(result) except: pass if result != None: callback(JSONRPC.createResultObject(result, self.id)) else: self.onFailure(callback)
def __init__(self, address = None, public = None, private = None, type=AccountType.STANDARD): if address == None and public == None and private == None: address, public, private = Crypto.generateKeys() self.address = address self.public = public self.private = private self.type = type
def createAtomicSwapTx(self, callback): fromAccount = Accounts.getAccountByAddress(self.fromAddress) toMultiSigAccount = Accounts.getAccountByAddress(self.toAddress) ''' A picks a random number x ''' hashPrivKey = Crypto.generateHash(toMultiSigAccount.private) ''' A creates TX1: "Pay w BTC to <B's public key> if (x for H(x) known and signed by B) or (signed by A & B)" ''' transactionA1 = MemoryPool.createUnconfirmedAtomicSwapTransaction(fromAccount, toMultiSigAccount.address, self.value, self.gasLimit, self.gasPrice, Script.verifyAtomicSwapSignature(), self.threshold) if transactionA1 != None: ''' A creates TX2: "Pay w BTC from TX1 to <A's public key>, locked 48 hours in the future, signed by A" ''' transactionA2 = MemoryPool.createUnconfirmedAtomicSwapTransaction(fromAccount, toMultiSigAccount.address, self.value, self.gasLimit, self.gasPrice, Script.verifyAtomicSwapLock()) if transactionA2 != None: tx = transactionA1.serialize() unsignedTx = transactionA2.serialize() result = { 'tx': DataType.toHex(tx), 'unsignedTx': DataType.toHex(unsignedTx) } callback( JSONRPC.createResultObject(result, self.id) ) else: self.onFailure(callback) else: self.onFailure(callback)
def validateBlockBits(self, blockHeader, bits): currTarget = Bits.getTargetFromBits(bits) blockHeaderHash = Crypto.proofOfWorkHash(blockHeader) blockHeaderHashLong = DataType.bytesToInt(blockHeaderHash) if blockHeaderHashLong < currTarget: return True else: return False
def sha3(self): offset = self.popAsInt() length = self.popAsInt() data = self.memory[offset:offset + length] print('memory', 'sha3', 'data', data, len(self.memory)) _hash = Crypto.generateHash(data, Config.getValue('SHA3_HASHING_ALGORITHM')) print('memory', 'sha3', 'hash', _hash) self.stack.append(_hash)
def extcodehash(self): address = self.pop() unspentTransactionScript = UXTO.getUnspentTransactionScript(address) txOut = unspentTransactionScript.output script = txOut.script _hash = 0 if script == None else Crypto.generateHash(script, Config.getValue('SHA3_HASHING_ALGORITHM')) self.stack.append(_hash)
def init(self): merkleNodes = [] for transaction in self.transactions: merkleNode = MerkleNode() txHash = transaction.hashTxWitness( ) if self.includeWitness else transaction.hash() merkleNode.hash = Crypto.generateHash(txHash) merkleNodes.append(merkleNode) self.generateMerkleTree(merkleNodes) self.merkleNodes.extend(merkleNodes)
def createNewMultiSigAccountType(self, accountType, privateKey, callback = None): multiSigAddressBytes = bytearray() for publicKey in self.publicKeys: multiSigAddressBytes.extend(publicKey) multiSigAddress = Crypto.generateAddress(multiSigAddressBytes) account = CoreAccount(multiSigAddress, self.publicKeys, privateKey, accountType) Accounts.addAccount(account) multiSigAddress = Address.to0xAddress(multiSigAddress) callback( JSONRPC.createResultObject(multiSigAddress, self.id) )
def generateMerkleTree(self, prevMerkleNodes): if len(prevMerkleNodes) > 1: merkleNodes = [] merkleNode = None for prevMerkleNode in prevMerkleNodes: if merkleNode == None: merkleNode = MerkleNode() if merkleNode.left == None: merkleNode.left = prevMerkleNode elif merkleNode.right == None: merkleNode.right = prevMerkleNode if merkleNode.left != None and merkleNode.right != None: data = merkleNode.left.hash + merkleNode.right.hash merkleNode.hash = Crypto.generateHash(data) merkleNodes.append(merkleNode) merkleNode = None if merkleNode != None: merkleNode.hash = Crypto.generateHash(merkleNode.left.hash) merkleNodes.append(merkleNode) self.generateMerkleTree(merkleNodes) self.merkleNodes.extend(merkleNodes)
def verifyAddress(self, transaction): for txOut in transaction.outputs: address = txOut.address if address == None or len(address) != Config.getIntValue( 'ADDRESS_LEN'): return False if txOut.hasExtScript(): if UXTO.getUnspentTransactionScript(address) == None: outpointBytes = bytearray() for inputs in transaction.inputs: outpoint = inputs.outpoint outpointBytes.extend(outpoint.serialize()) if address != Crypto.generateAddress(outpointBytes): return False else: #contract exists and address is not available return False return True
def hash(self): return Crypto.proofOfWorkHash(self.serializeHeader())
def createUnconfirmedTransaction(fromAccount, toAddress, value, gasLimit, gasPrice, script=None, parameters=None): transaction, outpoints = createNewUnconfirmedTransaction( fromAccount, value, gasLimit, gasPrice) if transaction != None: if script != None or parameters != None: #smart contract if script == None: #call witness = [] witness.append(fromAccount.address) witness.extend(parameters) unspentTransactionOutpoint = UXTO.getUnspentTransactionOutpointByAddress( toAddress) transaction.addInput(unspentTransactionOutpoint.txId, unspentTransactionOutpoint.outputIndex, witness) if value > 0: #contract value transfer _script = Script.merge() transaction.addOutput(toAddress, _script, value) else: #deploy outpointBytes = bytearray() for outpoint in outpoints: outpointBytes.extend(outpoint.serialize()) extensionAddress = Crypto.generateAddress(outpointBytes) _script = bytearray() _script.append(Config.getIntValue("EXTENSION_SCRIPT_VERSION")) _script.extend(script) _script = DataType.serialize(_script) extraData = None if len(parameters) > 0: extraData = parameters extraData.append(fromAccount.address) transaction.addOutput(extensionAddress, _script, value, extraData) else: _script = None unspentTransactionScript = UXTO.getUnspentTransactionScript( toAddress) if unspentTransactionScript != None: #contract value transfer _script = Script.merge() if _script == None: #value _script = Script.verifySignature() transaction.addOutput(toAddress, _script, value) transaction.sign() return transaction
def pubaddr(self): data = self.pop() _address = Crypto.generateAddress(data) self.stack.append(_address)
def sign(self, message): return Crypto.sign(self.private, message)
def checksig(self): public = self.pop() signature = self.pop() outputHash = self.transaction.hashOutput(self.input, self.output) self.stack.append(Crypto.verify(public, signature, outputHash))
def sha256(self): data = self.pop() _hash = Crypto.generateHash(data, Config.getValue('SHA2_HASHING_ALGORITHM')) self.stack.append(_hash)
def verify(self, signature, message): return Crypto.verify(self.public, signature, message)
def hash(self): includeWitness = self.isCoinbase() return Crypto.generateHash(self.serialize(includeWitness))
def hashTxWitness(self): return Crypto.generateHash(self.serialize())