def _tx_pay_btc(txhex, privk, burn_price, burn_addr=blockstack.lib.config.BLOCKSTACK_BURN_ADDRESS): tx = virtualchain.btc_tx_deserialize(txhex) # up the burn amount tx['outs'][3]['script'] = virtualchain.btc_make_payment_script(burn_addr) tx['outs'][3]['value'] = burn_price tx['outs'][4]['value'] -= burn_price # re-sign for i in tx['ins']: i['script'] = '' txhex = virtualchain.btc_tx_serialize(tx) _addr = virtualchain.address_reencode(virtualchain.get_privkey_address(privk)) txhex_signed = virtualchain.tx_sign_all_unsigned_inputs(privk, testlib.get_utxos(_addr), txhex) # re-sign the last output with the payment key tx_signed = virtualchain.btc_tx_deserialize(txhex_signed) tx_signed['ins'][-1]['script'] = '' txhex_signed = virtualchain.tx_sign_all_unsigned_inputs(testlib.get_default_payment_wallet().privkey, testlib.get_utxos(testlib.get_default_payment_wallet().addr), virtualchain.btc_tx_serialize(tx_signed)) print txhex_signed res = testlib.broadcast_transaction(txhex_signed) assert 'error' not in res return res
def mktx(satoshis, fee): outputs = None if satoshis is None: # send all satoshis = sum([u['value'] for u in utxos]) print 'WARN: sending all of {} ({}) to {}'.format(payment_addr, satoshis, recipient_addr) outputs = [ {'script': virtualchain.make_payment_script(payment_addr), 'value': virtualchain.calculate_change_amount(utxos, 0, fee)}, ] else: outputs = [ {"script": virtualchain.make_payment_script(payment_addr), "value": satoshis}, {"script": virtualchain.make_payment_script(recipient_addr), "value": virtualchain.calculate_change_amount(utxos, satoshis, fee)}, ] txobj = { 'ins': utxos, 'outs': outputs, 'locktime': 0, 'version': 1 } # log.debug("serialize tx: {}".format(json.dumps(txobj, indent=4, sort_keys=True))) txstr = virtualchain.btc_tx_serialize(txobj) signed_txstr = virtualchain.tx_sign_all_unsigned_inputs(privkey, utxos, txstr) return signed_txstr
def send_as_segwit_bech32(txhex, payment_key): print 'txhex: {}'.format(txhex) # get op-return data tx = virtualchain.btc_tx_deserialize(txhex) payload = tx['outs'][0]['script'] print json.dumps(tx, indent=4, sort_keys=True) # convert payment key to bech32 addr = virtualchain.address_reencode(virtualchain.get_privkey_address(payment_key)) pubk = virtualchain.lib.ecdsalib.ecdsa_private_key(payment_key, compressed=True).public_key().to_hex() addrhash = virtualchain.lib.hashing.bin_hash160(pubk.decode('hex')).encode('hex') segwit_addr = virtualchain.segwit_addr_encode(addrhash.decode('hex'), hrp='bcrt') print 'privk = {}'.format(payment_key) print 'pubk = {}'.format(pubk) print 'addr = {}'.format(addr) print 'segwit addr = {}'.format(segwit_addr) print 'script = 00{}'.format(addrhash) tx_fee = 5500 res = convert_funds_to_segwit(payment_key, tx_fee) fund_outputs = res['utxos'] fund_txid = res['tx_hash'] new_tx = { 'locktime': 0, 'version': 2, 'ins': [ {'outpoint': {'hash': fund_txid, 'index': 0}, 'script': '', 'witness_script': '', 'sequence': 4294967295}, ], 'outs': [ {'script': tx['outs'][0]['script'], 'value': tx['outs'][0]['value']}, {'script': '0014' + addrhash, 'value': fund_outputs[0]['value'] - tx_fee * 2}, {'script': tx['outs'][2]['script'], 'value': tx['outs'][2]['value']} ] } unsigned_txhex = virtualchain.btc_tx_serialize(new_tx) print 'unsigned: {}'.format(unsigned_txhex) pk_segwit = virtualchain.make_segwit_info(payment_key) print json.dumps(pk_segwit, indent=4, sort_keys=True) signed_txhex = virtualchain.tx_sign_input(unsigned_txhex, 0, fund_outputs[0]['script'], fund_outputs[0]['value'], pk_segwit, segwit=True, scriptsig_type='p2wpkh') print 'signed: {}'.format(signed_txhex) res = testlib.broadcast_transaction(signed_txhex) assert 'error' not in res return res
def scenario(wallets, **kw): global reveal_block global preorder_block res = testlib.blockstack_namespace_preorder("test", wallets[1].addr, wallets[0].privkey, tx_only=True, expect_fail=True) ns_preorder_txhex = res['transaction'] # change the burn address ns_preorder_tx = virtualchain.btc_tx_deserialize(ns_preorder_txhex) ns_preorder_tx['outs'][2]['script'] = virtualchain.btc_make_payment_script( wallets[2].addr) for i in ns_preorder_tx['ins']: i['script'] = '' utxos = testlib.get_utxos(wallets[0].addr) ns_preorder_txhex = virtualchain.btc_tx_serialize(ns_preorder_tx) ns_preorder_txhex_signed = virtualchain.tx_sign_all_unsigned_inputs( wallets[0].privkey, utxos, ns_preorder_txhex) print ns_preorder_txhex_signed res = testlib.broadcast_transaction(ns_preorder_txhex_signed) if 'error' in res: print res return False print res testlib.next_block(**kw) num_ops = virtualchain.lib.indexer.StateEngine.get_block_statistics( testlib.get_current_block(**kw)) if num_ops['num_parsed_ops'] != 1: print 'processed ops: {}'.format(num_ops) return False # try again, but use the right burn address testlib.blockstack_namespace_preorder("test", wallets[1].addr, wallets[0].privkey) preorder_block = testlib.get_current_block(**kw) + 1 testlib.next_block(**kw) testlib.blockstack_namespace_reveal( "test", wallets[1].addr, 52595, 250, 4, [6, 5, 4, 3, 2, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], 10, 10, wallets[0].privkey) reveal_block = testlib.get_current_block(**kw) + 1 testlib.next_block(**kw)
def serialize_tx(inputs, outputs): """ Given the inputs and outputs to a transaction, serialize them to the appropriate blockchain format. Return the hex-string containing the transaction """ # TODO: expand beyond bitcoin txobj = {'ins': inputs, 'outs': outputs, 'locktime': 0, 'version': 1} # log.debug("serialize tx: {}".format(json.dumps(txobj, indent=4, sort_keys=True))) txstr = virtualchain.btc_tx_serialize(txobj) return txstr
def db_parse( block_id, txid, vtxindex, op, data, senders, inputs, outputs, fee, db_state=None, **virtualchain_hints ): """ (required by virtualchain state engine) Parse a blockstack operation from a transaction. The transaction fields are as follows: * `block_id` is the blockchain height at which this transaction occurs * `txid` is the transaction ID * `data` is the scratch area of the transaction that contains the actual virtualchain operation (e.g. "id[opcode][payload]") * `senders` is a list in 1-to-1 correspondence with `inputs` that contains information about what funded the inputs * `inputs` are the list of inputs to the transaction. Some blockchains (like Bitcoin) support multiple inputs, whereas others (like Ethereum) support only 1. * `outputs` are the list of outputs of the transaction. Some blockchains (like Bitcoin) support multiple outputs, whereas others (like Ethereum) support only 1. * `fee` is the transaction fee. `db_state` is the StateEngine-derived class. This is a BlockstackDB instance. `**virtualchain_hints` is a dict with extra virtualchain hints that may be relevant. We require: * `raw_tx`: the hex-encoded string containing the raw transaction. Returns a dict with the parsed operation on success. Return None on error """ # basic sanity checks if len(senders) == 0: raise Exception("No senders given") if not check_tx_sender_types(senders, block_id): log.warning('Invalid senders for {}'.format(txid)) return None # this virtualchain instance must give the 'raw_tx' hint assert 'raw_tx' in virtualchain_hints, 'BUG: incompatible virtualchain: requires raw_tx support' # internal sanity check raw_tx = virtualchain_hints['raw_tx'] btc_tx_data = virtualchain.btc_tx_deserialize(raw_tx) test_btc_tx = virtualchain.btc_tx_serialize({'ins': inputs, 'outs': outputs, 'locktime': btc_tx_data['locktime'], 'version': btc_tx_data['version']}) assert raw_tx == test_btc_tx, 'TX mismatch: {} != {}'.format(raw_tx, test_btc_tx) # make sure each op has all the right fields defined try: opcode = op_get_opcode_name(op) assert opcode is not None, "Unrecognized opcode '%s'" % op except Exception, e: log.exception(e) log.error("Skipping unrecognized opcode") return None
def serialize_tx(inputs, outputs): """ Given the inputs and outputs to a transaction, serialize them to the appropriate blockchain format. Return the hex-string containing the transaction """ # TODO: expand beyond bitcoin txobj = { 'ins': inputs, 'outs': outputs, 'locktime': 0, 'version': 1 } # log.debug("serialize tx: {}".format(json.dumps(txobj, indent=4, sort_keys=True))) txstr = virtualchain.btc_tx_serialize(txobj) return txstr
def replace_output_with_bech32(txhex, output_index, payment_key, addrhash): print 'txhex: {}'.format(txhex) tx = virtualchain.btc_tx_deserialize(txhex) new_tx = { 'locktime': 0, 'version': 1, 'ins': tx['ins'], 'outs': tx['outs'], } for inp in new_tx['ins']: inp['script'] = '' inp['witness_script'] = '' new_tx['outs'][output_index] = { 'script': '0014' + addrhash, 'value': tx['outs'][output_index]['value'] } unsigned_txhex = virtualchain.btc_tx_serialize(new_tx) print 'unsigned: {}'.format(unsigned_txhex) addr = virtualchain.address_reencode( virtualchain.get_privkey_address(payment_key)) utxos = testlib.get_utxos(addr) prev_outputs = [{ 'out_script': inp['out_script'], 'value': inp['value'] } for inp in utxos] signed_txhex = virtualchain.tx_sign_all_unsigned_inputs( payment_key, prev_outputs, unsigned_txhex) print 'signed: {}'.format(signed_txhex) res = testlib.broadcast_transaction(signed_txhex) assert 'error' not in res return res
def _tx_pay_btc(txhex, privk, btc_paid, burn_addr): tx = virtualchain.btc_tx_deserialize(txhex) # up the burn amount btc_price = blockstack.lib.scripts.price_name('baz', namespace, testlib.get_current_block(**kw)) tx['outs'][2]['script'] = virtualchain.btc_make_payment_script(burn_addr) tx['outs'][2]['value'] = btc_paid tx['outs'][1]['value'] -= btc_paid # re-sign for i in tx['ins']: i['script'] = '' txhex = virtualchain.btc_tx_serialize(tx) _addr = virtualchain.address_reencode(virtualchain.get_privkey_address(privk)) txhex_signed = virtualchain.tx_sign_all_unsigned_inputs(privk, testlib.get_utxos(_addr), txhex) print txhex_signed res = testlib.broadcast_transaction(txhex_signed) assert 'error' not in res, res['error'] return res
def mktx(satoshis, fee): outputs = None if satoshis is None: # send all satoshis = sum([u['value'] for u in utxos]) print 'WARN: sending all of {} ({}) to {}'.format( payment_addr, satoshis, recipient_addr) outputs = [ { 'script': virtualchain.make_payment_script(payment_addr), 'value': virtualchain.calculate_change_amount(utxos, 0, fee) }, ] else: outputs = [ { "script": virtualchain.make_payment_script(payment_addr), "value": satoshis }, { "script": virtualchain.make_payment_script(recipient_addr), "value": virtualchain.calculate_change_amount(utxos, satoshis, fee) }, ] txobj = {'ins': utxos, 'outs': outputs, 'locktime': 0, 'version': 1} # log.debug("serialize tx: {}".format(json.dumps(txobj, indent=4, sort_keys=True))) txstr = virtualchain.btc_tx_serialize(txobj) signed_txstr = virtualchain.tx_sign_all_unsigned_inputs( privkey, utxos, txstr) return signed_txstr
def scenario(wallets, **kw): global pk, pk2 testlib.blockstack_namespace_preorder("test", wallets[1].addr, wallets[0].privkey) testlib.next_block(**kw) testlib.blockstack_namespace_reveal( "test", wallets[1].addr, 52595, 250, 4, [6, 6, 6, 6, 6, 6, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], 10, 10, wallets[0].privkey) testlib.next_block(**kw) testlib.blockstack_namespace_ready("test", wallets[1].privkey) testlib.next_block(**kw) # pay for a name in a v1 namespace with Stacks addr = virtualchain.address_reencode(virtualchain.get_privkey_address(pk)) addr2 = virtualchain.address_reencode( virtualchain.get_privkey_address(pk2)) # calculate the cost of doing so namespace = testlib.get_state_engine().get_namespace('test') stacks_price = blockstack.lib.scripts.price_name_stacks( 'baz', namespace, testlib.get_current_block(**kw)) btc_price = blockstack.lib.scripts.price_name( 'baz', namespace, testlib.get_current_block(**kw)) print '' print 'price of {} in Stacks is {}'.format('baz.test', stacks_price) print 'price of {} in BTC is {}'.format('baz.test', btc_price) print '' testlib.blockstack_send_tokens(addr, "STACKS", stacks_price, wallets[0].privkey) testlib.blockstack_send_tokens(addr2, "STACKS", stacks_price * 2, wallets[0].privkey) testlib.send_funds(wallets[0].privkey, 10 * btc_price, addr) testlib.send_funds(wallets[0].privkey, 10 * btc_price, addr2) testlib.next_block(**kw) # preorder/register using Stacks testlib.blockstack_name_preorder("baz.test", wallets[2].privkey, addr2, price={ 'units': 'STACKS', 'amount': stacks_price }) testlib.blockstack_name_preorder("goo.test", wallets[2].privkey, addr2, price={ 'units': 'STACKS', 'amount': stacks_price }) testlib.blockstack_name_preorder("nop.test", wallets[2].privkey, addr2, price={ 'units': 'STACKS', 'amount': stacks_price }) testlib.next_block(**kw) testlib.blockstack_name_register("baz.test", wallets[2].privkey, addr2) testlib.blockstack_name_register("goo.test", wallets[2].privkey, addr2) testlib.blockstack_name_register("nop.test", wallets[2].privkey, addr2) testlib.next_block(**kw) balance_before = testlib.get_addr_balances(addr2)[addr2]['STACKS'] # pay with both Stacks and Bitcoin # should favor Stacks payment over Bitcoin payment if we pay enough stacks. # Stacks should have been burned, as well as BTC. res = testlib.blockstack_name_renew('baz.test', pk2, price={ 'units': 'STACKS', 'amount': stacks_price }, tx_only=True, expect_success=True) txhex = res['transaction'] tx = virtualchain.btc_tx_deserialize(txhex) # up the burn amount btc_price = blockstack.lib.scripts.price_name( 'baz', namespace, testlib.get_current_block(**kw)) tx['outs'][3]['script'] = virtualchain.btc_make_payment_script( blockstack.lib.config.BLOCKSTACK_BURN_ADDRESS) tx['outs'][3]['value'] = btc_price tx['outs'][4]['value'] -= btc_price # re-sign for i in tx['ins']: i['script'] = '' txhex = virtualchain.btc_tx_serialize(tx) txhex_signed = virtualchain.tx_sign_all_unsigned_inputs( pk2, testlib.get_utxos(addr2), txhex) # re-sign the last output with the payment key tx_signed = virtualchain.btc_tx_deserialize(txhex_signed) tx_signed['ins'][-1]['script'] = '' txhex_signed = virtualchain.tx_sign_all_unsigned_inputs( testlib.get_default_payment_wallet().privkey, testlib.get_utxos(testlib.get_default_payment_wallet().addr), virtualchain.btc_tx_serialize(tx_signed)) print txhex_signed res = testlib.broadcast_transaction(txhex_signed) if 'error' in res: print res return False testlib.next_block(**kw) # should have paid in Stacks balance_after = testlib.get_addr_balances(addr2)[addr2]['STACKS'] if balance_after != balance_before - stacks_price: print 'baz.test cost {}'.format(balance_before - balance_after) return False balance_before = testlib.get_addr_balances(addr2)[addr2]['STACKS'] # try to renew a name where we pay not enough stacks, but enough bitcoin. # should be rejected. res = testlib.blockstack_name_renew('goo.test', pk2, price={ 'units': 'STACKS', 'amount': stacks_price - 1 }, tx_only=True) txhex = res['transaction'] tx = virtualchain.btc_tx_deserialize(txhex) # up the burn amount to the name price btc_price = blockstack.lib.scripts.price_name( 'goo', namespace, testlib.get_current_block(**kw)) tx['outs'][3]['script'] = virtualchain.btc_make_payment_script( blockstack.lib.config.BLOCKSTACK_BURN_ADDRESS) tx['outs'][3]['value'] = btc_price tx['outs'][4]['value'] -= btc_price # re-sign for i in tx['ins']: i['script'] = '' txhex = virtualchain.btc_tx_serialize(tx) txhex_signed = virtualchain.tx_sign_all_unsigned_inputs( pk2, testlib.get_utxos(addr2), txhex) # re-sign the last output with the payment key tx_signed = virtualchain.btc_tx_deserialize(txhex_signed) tx_signed['ins'][-1]['script'] = '' txhex_signed = virtualchain.tx_sign_all_unsigned_inputs( testlib.get_default_payment_wallet().privkey, testlib.get_utxos(testlib.get_default_payment_wallet().addr), virtualchain.btc_tx_serialize(tx_signed)) print txhex_signed res = testlib.broadcast_transaction(txhex_signed) if 'error' in res: print res return False testlib.next_block(**kw) # should NOT have paid in Stacks balance_after = testlib.get_addr_balances(addr2)[addr2]['STACKS'] if balance_after != balance_before: print 'goo.test paid {}'.format(balance_before - balance_after) return False balance_before = testlib.get_addr_balances(addr2)[addr2]['STACKS'] # underpay in both Stacks and Bitcoin. # only bitcoin will be burned; transaction will not be processed res = testlib.blockstack_name_renew('nop.test', pk2, price={ 'units': 'STACKS', 'amount': stacks_price - 1 }, tx_only=True) txhex = res['transaction'] tx = virtualchain.btc_tx_deserialize(txhex) # up the burn amount to the name price btc_price = blockstack.lib.scripts.price_name( 'goo', namespace, testlib.get_current_block(**kw)) tx['outs'][3]['script'] = virtualchain.btc_make_payment_script( blockstack.lib.config.BLOCKSTACK_BURN_ADDRESS) tx['outs'][3]['value'] = btc_price - 1 tx['outs'][4]['value'] -= btc_price + 1 # re-sign for i in tx['ins']: i['script'] = '' txhex = virtualchain.btc_tx_serialize(tx) txhex_signed = virtualchain.tx_sign_all_unsigned_inputs( pk2, testlib.get_utxos(addr2), txhex) # re-sign the last output with the payment key tx_signed = virtualchain.btc_tx_deserialize(txhex_signed) tx_signed['ins'][-1]['script'] = '' txhex_signed = virtualchain.tx_sign_all_unsigned_inputs( testlib.get_default_payment_wallet().privkey, testlib.get_utxos(testlib.get_default_payment_wallet().addr), virtualchain.btc_tx_serialize(tx_signed)) print txhex_signed res = testlib.broadcast_transaction(txhex_signed) if 'error' in res: print res return False testlib.next_block(**kw) testlib.expect_snv_fail_at('nop.test', testlib.get_current_block(**kw)) balance_after = testlib.get_addr_balances(addr2)[addr2]['STACKS'] if balance_after != balance_before: print 'paid {} for nop.test'.format(balance_before - balance_after) return False
def scenario(wallets, **kw): segwit_addr_1 = get_segwit_address(wallets[1].privkey) segwit_addr_1_tb = get_segwit_address(wallets[1].privkey, hrp='tb') segwit_addr_0_tb = get_segwit_address(wallets[0].privkey, hrp='tb') print segwit_addr_0_tb print segwit_addr_1_tb pubk = virtualchain.lib.ecdsalib.ecdsa_private_key( wallets[1].privkey, compressed=True).public_key().to_hex() addrhash = virtualchain.lib.hashing.bin_hash160( pubk.decode('hex')).encode('hex') a = 'tb1pzjpqjwmz5d5e9qkey6vphmtkvh5rsn9225xsgg79' namespace_preorder_name_hash = blockstack.lib.hashing.hash_name( 'test', virtualchain.make_payment_script(wallets[0].addr), a) print 'hash of {} + {} + {} = {}'.format( 'test', virtualchain.make_payment_script(wallets[0].addr), a, namespace_preorder_name_hash) resp = testlib.blockstack_namespace_preorder("test", wallets[1].addr, wallets[0].privkey, tx_only=True) tx = virtualchain.btc_tx_deserialize(resp['transaction']) new_tx = { 'locktime': 0, 'version': 1, 'ins': tx['ins'], 'outs': tx['outs'], } for inp in new_tx['ins']: inp['script'] = '' inp['witness_script'] = '' print 'script before: {}'.format(tx['outs'][0]['script']) patched_script = virtualchain.make_data_script( 'id*'.encode('hex') + namespace_preorder_name_hash + tx['outs'][0]['script'].decode('hex')[25:].encode('hex')) print 'script after : {}'.format(patched_script) new_tx['outs'][0] = {'script': patched_script, 'value': 0} unsigned_txhex = virtualchain.btc_tx_serialize(new_tx) print 'unsigned: {}'.format(unsigned_txhex) addr = virtualchain.address_reencode( virtualchain.get_privkey_address(wallets[0].privkey)) utxos = testlib.get_utxos(addr) prev_outputs = [{ 'out_script': inp['out_script'], 'value': inp['value'] } for inp in utxos] signed_txhex = virtualchain.tx_sign_all_unsigned_inputs( wallets[0].privkey, prev_outputs, unsigned_txhex) print 'signed: {}'.format(signed_txhex) res = testlib.broadcast_transaction(signed_txhex) assert 'error' not in res testlib.next_block(**kw) # should fail resp = testlib.blockstack_namespace_reveal( "test", wallets[1].addr, 52595, 250, 4, [6, 5, 4, 3, 2, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], 10, 10, wallets[0].privkey, tx_only=True) resp = replace_output_with_bech32(resp['transaction'], 1, wallets[0].privkey, addrhash) testlib.next_block(**kw)
def scenario(wallets, **kw): global pk testlib.blockstack_namespace_preorder("test", wallets[1].addr, wallets[0].privkey) testlib.next_block(**kw) testlib.blockstack_namespace_reveal( "test", wallets[1].addr, 52595, 250, 4, [6, 6, 6, 6, 6, 6, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], 10, 10, wallets[0].privkey) testlib.next_block(**kw) testlib.blockstack_namespace_ready("test", wallets[1].privkey) testlib.next_block(**kw) # pay for a name in a v1 namespace with Stacks addr = virtualchain.address_reencode(virtualchain.get_privkey_address(pk)) # calculate the cost of doing so namespace = testlib.get_state_engine().get_namespace('test') stacks_price = blockstack.lib.scripts.price_name_stacks( 'foo', namespace, testlib.get_current_block(**kw)) btc_price = blockstack.lib.scripts.price_name( 'foo', namespace, testlib.get_current_block(**kw)) print '' print 'price of {} in Stacks is {}'.format('foo.test', stacks_price) print '' testlib.blockstack_send_tokens(addr, "STACKS", stacks_price * 4, wallets[0].privkey) testlib.send_funds(wallets[0].privkey, btc_price * 10, addr) # fund with enough bitcoin testlib.next_block(**kw) # preorder/register using Stacks---Stacks should still be used since that's what the transaction indicates testlib.blockstack_name_preorder("foo.test", pk, wallets[3].addr, price={ 'units': 'STACKS', 'amount': stacks_price }) testlib.next_block(**kw) testlib.send_funds(wallets[0].privkey, btc_price * 10, addr) testlib.blockstack_name_register("foo.test", pk, wallets[3].addr) testlib.next_block(**kw) # preorder/register using Bitcoin--Stacks should NOT be used since that's what the transaction indicates testlib.blockstack_name_preorder("bar.test", pk, wallets[3].addr, price={ 'units': 'BTC', 'amount': btc_price }) testlib.next_block(**kw) testlib.blockstack_name_register('bar.test', pk, wallets[3].addr) testlib.next_block(**kw) balance_before = testlib.get_addr_balances(addr)[addr]['STACKS'] # pay with Stacks and Bitcoin. Preorder should succeed, and register should also succeed since we're paying enough stacks. Underpay bitcoin res = testlib.blockstack_name_preorder('baz.test', pk, wallets[3].addr, price={ 'units': 'STACKS', 'amount': stacks_price }, tx_only=True, expect_success=True) txhex = res['transaction'] tx = virtualchain.btc_tx_deserialize(txhex) # up the burn amount btc_price = blockstack.lib.scripts.price_name( 'baz', namespace, testlib.get_current_block(**kw)) tx['outs'][2]['script'] = virtualchain.btc_make_payment_script( blockstack.lib.config.BLOCKSTACK_BURN_ADDRESS) tx['outs'][2]['value'] = btc_price - 1 tx['outs'][1]['value'] -= btc_price - 1 # re-sign for i in tx['ins']: i['script'] = '' txhex = virtualchain.btc_tx_serialize(tx) txhex_signed = virtualchain.tx_sign_all_unsigned_inputs( pk, testlib.get_utxos(addr), txhex) print txhex_signed res = testlib.broadcast_transaction(txhex_signed) if 'error' in res: print res return False testlib.next_block(**kw) # should have paid in Stacks balance_after = testlib.get_addr_balances(addr)[addr]['STACKS'] if balance_after != balance_before - stacks_price: print 'baz.test cost {}'.format(balance_before - balance_after) return False # should succeed, since we paid enough stacks (Bitcoin is not considered) testlib.blockstack_name_register('baz.test', pk, wallets[3].addr) testlib.next_block(**kw) balance_before = testlib.get_addr_balances(addr)[addr]['STACKS'] # register a name where we pay not enough stacks, but enough bitcoin. preorder should succeed, but register should fail since we tried to use stacks res = testlib.blockstack_name_preorder('goo.test', pk, wallets[3].addr, price={ 'units': 'STACKS', 'amount': stacks_price - 1 }, tx_only=True, expect_success=True) txhex = res['transaction'] tx = virtualchain.btc_tx_deserialize(txhex) # up the burn amount to the name price btc_price = blockstack.lib.scripts.price_name( 'goo', namespace, testlib.get_current_block(**kw)) tx['outs'][2]['script'] = virtualchain.btc_make_payment_script( blockstack.lib.config.BLOCKSTACK_BURN_ADDRESS) tx['outs'][2]['value'] = btc_price tx['outs'][1]['value'] -= btc_price # re-sign for i in tx['ins']: i['script'] = '' txhex = virtualchain.btc_tx_serialize(tx) txhex_signed = virtualchain.tx_sign_all_unsigned_inputs( pk, testlib.get_utxos(addr), txhex) print txhex_signed res = testlib.broadcast_transaction(txhex_signed) if 'error' in res: print res return False testlib.next_block(**kw) # should have paid in Stacks balance_after = testlib.get_addr_balances(addr)[addr]['STACKS'] if balance_after != balance_before - stacks_price + 1: print 'goo.test paid {}'.format(balance_before - balance_after) return False # should fail, since we tried to pay in stacks and didn't pay enough testlib.blockstack_name_register('goo.test', pk, wallets[3].addr) testlib.next_block(**kw) testlib.expect_snv_fail_at('goo.test', testlib.get_current_block(**kw)) if testlib.get_state_engine().get_name('goo.test') is not None: print 'registered goo.test' return False balance_before = testlib.get_addr_balances(addr)[addr]['STACKS'] # underpay in both Stacks and Bitcoin. # both stacks and bitcoin will be burned. # preorder should succeed, but register should fail. res = testlib.blockstack_name_preorder('nop.test', pk, wallets[3].addr, price={ 'units': 'STACKS', 'amount': stacks_price - 1 }, safety_checks=False, tx_only=True, expect_success=True) txhex = res['transaction'] tx = virtualchain.btc_tx_deserialize(txhex) # up the burn amount to the name price, but just under btc_price = blockstack.lib.scripts.price_name( 'nop', namespace, testlib.get_current_block(**kw)) tx['outs'][2]['script'] = virtualchain.btc_make_payment_script( blockstack.lib.config.BLOCKSTACK_BURN_ADDRESS) tx['outs'][2]['value'] = btc_price - 1 tx['outs'][1]['value'] -= btc_price - 1 # re-sign for i in tx['ins']: i['script'] = '' txhex = virtualchain.btc_tx_serialize(tx) txhex_signed = virtualchain.tx_sign_all_unsigned_inputs( pk, testlib.get_utxos(addr), txhex) print txhex_signed res = testlib.broadcast_transaction(txhex_signed) if 'error' in res: print res return False testlib.next_block(**kw) # should fail, since we didn't pay enough stacks and tried to pay in stacks res = testlib.blockstack_name_register('nop.test', pk, wallets[3].addr) testlib.next_block(**kw) testlib.expect_snv_fail_at('nop.test', testlib.get_current_block(**kw)) # preorder should still have debited balance_after = testlib.get_addr_balances(addr)[addr]['STACKS'] if balance_after != balance_before - stacks_price + 1: print 'paid {} for nop.test'.format(balance_before - balance_after) return False