def create_buyer_message(self, price, memo): wallet = Wallet.objects.get_for_user(self.buyer) inputs = [{ 'output': u'6317c147efc54f1c0291cd5cc403db289c5228d054f0cf2ff91265c480efd385:0', 'value': price, 'address': wallet.address, 'privkey': wallet.privkey }] nego_buyer = pickle.loads(self.nego_buyer) last_msg = nego_buyer.get_last_message_received() refund_to = [{'script': address_to_script(wallet.address)}] details = BargainProposalDetails( inputs, last_msg.details.outputs, price, refund_to, [], memo.encode(), '', '' ) msg = nego_buyer.build_bargain_proposal( details, SIGN_ECDSA_SHA256, wallet.pubkey.encode(), wallet.privkey ) nego_seller = pickle.loads(self.nego_seller) nego_seller.check_bargain_proposal(msg) self.nego_buyer = pickle.dumps(nego_buyer) self.nego_seller = pickle.dumps(nego_seller) self.save()
def init_negotiation(self): wallet = Wallet.objects.get_for_user(self.product.owner) nego_buyer = Negotiation(role=Negotiation.ROLE_BUYER) nego_seller = Negotiation(role=Negotiation.ROLE_SELLER) details = BargainInitDetails('', '') init_msg = nego_buyer.build_bargain_init(details) nego_seller.check_bargain_init(init_msg) output = [{ 'amount': self.product.initial_price, 'script': address_to_script(wallet.address) }] details = BargainRequestDetails( int(time.time()), output, expires=int(time.time()) + 3600, memo='Hello! Do you want to buy my wonderful product?', ) request_msg = nego_seller.build_bargain_request(details) nego_buyer.check_bargain_request(request_msg) self.nego_buyer = pickle.dumps(nego_buyer) self.nego_seller = pickle.dumps(nego_seller)
def create_seller_message(self, price, memo): wallet = Wallet.objects.get_for_user(self.product.owner) output = [{ 'amount': price, 'script': address_to_script(wallet.address) }] details = BargainProposalAckDetails( output, memo, '', '' ) nego_seller = pickle.loads(self.nego_seller) msg = nego_seller.build_bargain_proposal_ack( details, SIGN_ECDSA_SHA256, wallet.pubkey, wallet.privkey ) nego_buyer = pickle.loads(self.nego_buyer) nego_buyer.check_bargain_proposal_ack(msg) self.nego_buyer = pickle.dumps(nego_buyer) self.nego_seller = pickle.dumps(nego_seller) self.save()
def query_utxo_set(self, txout, includeconf=False): self.current_height = self.get_from_electrum( "blockchain.numblocks.subscribe")['result'] if not isinstance(txout, list): txout = [txout] utxos = [[t[:64], int(t[65:])] for t in txout] result = [] for ut in utxos: address = self.get_from_electrum("blockchain.utxo.get_address", ut)['result'] utxo_info = self.get_from_electrum( "blockchain.address.listunspent", address)['result'] utxo = None for u in utxo_info: if u['tx_hash'] == ut[0] and u['tx_pos'] == ut[1]: utxo = u if utxo is None: raise Exception("UTXO Not Found") r = { 'value': utxo['value'], 'address': address, 'script': btc.address_to_script(address) } if includeconf: if int(utxo['height']) in [0, -1]: #-1 means unconfirmed inputs r['confirms'] = 0 else: #+1 because if current height = tx height, that's 1 conf r['confirms'] = int(self.current_height) - int( utxo['height']) + 1 result.append(r) return result
def pay_script(self, output_type, addr): if output_type == TYPE_SCRIPT: return addr elif output_type == TYPE_ADDRESS: return bitcoin.address_to_script(addr) elif output_type == TYPE_PUBKEY: return bitcoin.public_key_to_p2pk_script(addr) else: raise TypeError('Unknown output type')
def get_preimage_script(self, txin): preimage_script = txin.get('preimage_script', None) if preimage_script is not None: return preimage_script pubkeys, x_pubkeys = self.get_sorted_pubkeys(txin) if txin['type'] == 'p2pkh': return bitcoin.address_to_script(txin['address']) elif txin['type'] in ['p2sh']: return multisig_script(pubkeys, txin['num_sig']) elif txin['type'] == 'p2pk': pubkey = pubkeys[0] return bitcoin.public_key_to_p2pk_script(pubkey) else: raise TypeError('Unknown txin type', txin['type'])
def get_preimage_script(self, txin): preimage_script = txin.get('preimage_script', None) if preimage_script is not None: return preimage_script pubkeys, x_pubkeys = self.get_sorted_pubkeys(txin) if txin['type'] == 'p2pkh': return bitcoin.address_to_script(txin['address']) elif txin['type'] in ['p2sh', 'p2wsh', 'p2wsh-p2sh']: return multisig_script(pubkeys, txin['num_sig']) elif txin['type'] in ['p2wpkh', 'p2wpkh-p2sh']: pubkey = pubkeys[0] pkh = bh2u(bitcoin.hash_160(bfh(pubkey))) return '76a9' + push_script(pkh) + '88ac' elif txin['type'] == 'p2pk': pubkey = pubkeys[0] return bitcoin.public_key_to_p2pk_script(pubkey) else: raise TypeError('Unknown txin type', txin['type'])
def address_to_scripthash(addr): script = btc.address_to_script(addr) return script_to_scripthash(script)
def estimated_output_size(cls, address): """Return an estimate of serialized output size in bytes.""" script = bitcoin.address_to_script(address) # 8 byte value + 1 byte script len + script return 9 + len(script) // 2
VALID_TIME3 = 1400300000L VALID_TIME4 = 1400400000L VALID_TIME5 = 1400500000L VALID_TIME6 = 1400600000L VALID_EXPIRES1 = 1401100000L VALID_EXPIRES2 = 1401200000L VALID_MEMO = 'Ceci est le contenu d\'un memo.' VALID_SELLER_DATA = json.dumps({'product_id': 11521, 'user_id': 1, 'nego_id': 1}) VALID_BUYER_DATA = json.dumps({'product_id': 11521, 'user_id': 2, 'nego_id': 4}) VALID_BARGAIN_URI1 = 'http://www.myhost1.com/bargain' VALID_BARGAIN_URI2 = 'http://www.myhost2.com/bargain' VALID_BUYER_AMOUNT = 50000000 VALID_FEES = 1000 VALID_REFUND_TO = [{'script': address_to_script(TEST_ADDR1)}] VALID_OUTPUTS1 = [{'amount': 100000000, 'script': address_to_script(TEST_ADDR2)}, {'amount': 150000000, 'script': address_to_script(TEST_ADDR2)}] VALID_AMOUNT1 = VALID_OUTPUTS1[0]['amount'] + VALID_OUTPUTS1[1]['amount'] VALID_OUTPUTS2 = [{'amount': 100000000, 'script': address_to_script(TEST_ADDR2)}, {'amount': 50000000, 'script': address_to_script(TEST_ADDR2)}] VALID_AMOUNT2 = VALID_OUTPUTS2[0]['amount'] + VALID_OUTPUTS2[1]['amount'] INVALID_MEMO = VALID_MEMO.encode("utf-16") INVALID_OUTPUTS = [{'amount': 100000000}] INVALID_EXPIRES = 1300100000L INVALID_NETWORK = 'myfakenet'