def createFirstCFTransaction(target_amount, pubkey_addr, end_time, pre_out_ids_for_fee=[], cert=''): if len(pre_out_ids_for_fee) == 0: tx_in = TransactionIn.coinbase_tx_in() tx_ins = [] tx_ins.append(tx_in) else: tx_ins = __get_tx_ins(pre_out_ids_for_fee) cert = SecretKeyDao.searchCertByPubAddr(pubkey_addr) cfscript = standard_tx_out_script(pubkey_addr) cfout = TransactionOut(0, cfscript, 0, 0, end_time) tx_outs = [] tx_outs.append(cfout) original_hash = Constants.ZERO_HASH pre_hash = Constants.ZERO_HASH lack_amount = target_amount cf_header = CFHeader(original_hash, target_amount, pubkey_addr, end_time, pre_hash, lack_amount, cert) cf = TransactionCF(cf_header, Constants.VERSION, tx_ins, tx_outs, Constants.LOCK_TIME) if len(tx_ins) != 0: cf = signTx(cf, pre_out_ids_for_fee) cf.check() if verify(cf): insert(cf) return cf
def createNormalCFTransaction(pre_out_ids, pre_cf_hash, spendValue, otherPublicAddrToValueDict, refund_addr): tx_ins = __get_tx_ins(pre_out_ids) pre_cf = TransactionDao.searchByHash(pre_cf_hash) cf_header = pre_cf.cf_header cf_header.lack_amount = cf_header.lack_amount - spendValue cf_header.pre_hash = pre_cf_hash if cf_header.original_hash == '' or cf_header.original_hash == Constants.ZERO_HASH: cf_header.original_hash = pre_cf_hash # cfscript = b'' if cf_header.lack_amount <= 0: #CF suceess outValue = cf_header.target_amount cfscript = standard_tx_out_script(cf_header.pubkey) # elif cf_header.lack_amount == cf_header.target_amount:#CF start # outValue = 0 # cfscript = ScriptPayToAddress(refund_addr).script() else: #CF ing outValue = spendValue cfscript = standard_tx_out_script(refund_addr) #outValue = cf_header.target_amount if cf_header.lack_amount == 0 else spendValue cfout = TransactionOut(outValue, cfscript, 0, 0, cf_header.end_time) tx_outs = __get_tx_outs(otherPublicAddrToValueDict) tx_outs.insert(0, cfout) cf = TransactionCF(cf_header, Constants.VERSION, tx_ins, tx_outs, Constants.LOCK_TIME, None, 0) if len(tx_ins) != 0: cf = signTx(cf, pre_out_ids) cf.check() if verify(cf): insert(cf) return cf
def parse(f): tx_type, = parse_struct("L", f) if 0x01 == tx_type: tx = Transaction.parse(f) elif 0x02 == tx_type: tx = TransactionCF.parse(f) return tx
def __getSearchResultSingle(c): tmp = c.fetchone() if tmp == None: return None parentBlockId = tmp[4] parentTxId = tmp[1] txs_in = TransactionInDao.search(parentBlockId, parentTxId) txs_out = TransactionOutDao.search(parentBlockId, parentTxId) unspents = unspents_from_db(txs_in) if tmp[7] == 1: tx = Transaction(tmp[2], txs_in, txs_out, tmp[3], unspents, tmp[6], tmp[0]) else: tx = TransactionCF(CFHeader(tmp[8], tmp[9], tmp[10], tmp[11], tmp[12], tmp[13], tmp[14]), tmp[2], txs_in, txs_out, tmp[3], unspents, tmp[6], tmp[0]) return tx
def __getSearchResult(c): txs = [] for tmp in c.fetchall(): parentBlockId = tmp[4] parentTxId = tmp[1] txs_in = TransactionInDao.search(parentBlockId, parentTxId) txs_out = TransactionOutDao.search(parentBlockId, parentTxId) unspents = unspents_from_db(txs_in) if tmp[7] == 1: tx = Transaction(tmp[2], txs_in, txs_out, tmp[3], unspents, tmp[6], tmp[0]) else: tx = TransactionCF(CFHeader(tmp[8], tmp[9], tmp[10], tmp[11], tmp[12], tmp[13], tmp[14]), tmp[2], txs_in, txs_out, tmp[3], unspents, tmp[6], tmp[0]) txs.append(tx) return txs