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 serialize(self): return { 'address': Address.toAddressStr(self.address), 'public': DataType.toHex(self.public), 'private': DataType.toHex(self.private), 'type': self.type }
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 handleLocalScript(self, fromAccount, output, deploy): gasLimit = Config.getIntValue('TRANSACTION_LOCAL_GAS_LIMIT') gasPrice = Config.getIntValue('TRANSACTION_LOCAL_GAS_PRICE') localTx = Transaction(gasLimit, gasPrice) localTx.gasRemaining = Config.getIntValue( 'TRANSACTION_LOCAL_GAS_REMAINING') _input = None if deploy: pass else: scriptData = [] scriptData.append(DataType.zeroFillArray(0, 32)) scriptData.append(DataType.zeroFillArray(0, 32)) scriptData.append(fromAccount.address) scriptData.extend(self.parameters) _input = Input(Config.getValue('SCRIPT_TRANSACTION_ID'), Config.getIntValue('SCRIPT_OUTPUT_INDEX')) _input.witness = scriptData if RVM.run(localTx, _input, output, True, deploy, True): if len(localTx.internalOutputs) > 0: internalOutput = localTx.internalOutputs[-1] if deploy: PersistentStorage.add(internalOutput.address, internalOutput, True) result = 'Script Deployed Locally' else: result = internalOutput.script try: result = DataType.toHex(result) result = DataType.asInt(result, 16) except ValueError: pass return result return None
def call(self, isLocal, callback): result = None fromAccount = Accounts.getAccountByAddress(self.fromAddress) if fromAccount != None: if isLocal: _output = PersistentStorage.get(self.script, True) if _output == None: unspentTransactionScript = UXTO.getUnspentTransactionScript( self.script) if unspentTransactionScript != None: _output = unspentTransactionScript.output if _output != None: result = self.handleLocalScript(fromAccount, _output, False) else: result = MemoryPool.addUnconfirmedTransaction( fromAccount, self.script, self.value, self.gasLimit, self.gasPrice, None, self.parameters) try: result = DataType.toHex(result) except: pass if result != None: callback(JSONRPC.createResultObject(result, self.id)) else: self.onFailure(callback)
def addUnconfirmedMultiSigTransaction(self, callback=None): fromAccount = Accounts.getAccountByAddress(self.fromAddress) txId = None if fromAccount != None: txId = MemoryPool.addUnconfirmedMultiSigTransaction( fromAccount, self.publicKeys, self.signatures, self.toAddress, self.value, self.gasLimit, self.gasPrice, self.threshold) if txId != None: callback(JSONRPC.createResultObject(DataType.toHex(txId), self.id)) else: self.onFailure(callback)
def signAtomicSwapTx(self, callback): transaction = Transaction() transaction.deserialize(self.unsignedTx) transaction.sign() if MemoryPool.addSignedTransaction(transaction): txId = transaction.hash() callback( JSONRPC.createResultObject(DataType.toHex(txId), self.id) ) else: self.onFailure(callback)
def signMultiSigOutput(self, callback=None): signatures = [] multiSigAccount = Accounts.getAccountByAddress(self.fromAddress) if multiSigAccount != None: transaction = MemoryPool.createUnconfirmedMultiSigTransaction( multiSigAccount, self.toAddress, self.value, self.gasLimit, self.gasPrice, self.threshold) if transaction != None: for txIn in transaction.inputs: multiSigAddress = DataType.toHex(multiSigAccount.address) _publicKeys = txIn.witness[::2] _signatures = txIn.witness[1::2] for publicKey, signature in zip(_publicKeys, _signatures): publicKey = DataType.toHex(publicKey) signature = DataType.toHex(signature) signatures.append({ 'address': multiSigAddress, 'public': publicKey, 'signature': signature }) callback(JSONRPC.createResultObject(signatures, self.id))