def global_freezing(self):
        self.log.info("Testing global freezing...")
        n0, n1 = self.nodes[0], self.nodes[1]

        base_asset_name = "FROZEN_GLOBAL"
        asset_name = f"${base_asset_name}"
        qty = 10000
        verifier = "true"
        address = n0.getnewaddress()
        spl_change_address = n0.getnewaddress()

        n0.issue(base_asset_name)
        n0.generate(1)

        n0.issuerestrictedasset(asset_name, qty, verifier, address)
        n0.generate(1)

        # pre-freeze validation
        assert_does_not_contain(asset_name, n0.listglobalrestrictions())
        assert not n0.checkglobalrestriction(asset_name)
        assert_equal(10000, n0.listassetbalancesbyaddress(address)[asset_name])
        change_address = n0.getnewaddress()
        n0.transferfromaddress(asset_name, address, 1000, n1.getnewaddress(), "", "", "", change_address)
        n0.generate(1)
        self.sync_all()
        assert_equal(9000, n0.listassetbalancesbyaddress(change_address)[asset_name])
        assert_equal(1000, n1.listmyassets()[asset_name])
        address = change_address  # assets have moved

        assert_raises_rpc_error(None, "Invalid Splendid change address", n0.freezerestrictedasset, asset_name, "garbagechangeaddress")

        n0.freezerestrictedasset(asset_name, spl_change_address)  # Can only freeze once!
        assert_raises_rpc_error(-26, "Freezing transaction already in mempool", n0.freezerestrictedasset, asset_name, spl_change_address)
        n0.generate(1)
        assert_raises_rpc_error(None, "global-freeze-when-already-frozen", n0.freezerestrictedasset, asset_name, spl_change_address)

        # post-freeze validation
        assert_contains(asset_name, n0.listglobalrestrictions())
        assert n0.checkglobalrestriction(asset_name)
        assert_raises_rpc_error(-8, "restricted asset has been globally frozen", n0.transferfromaddress, asset_name, address, 1000, n1.getnewaddress())
        assert_raises_rpc_error(None, "Invalid Splendid change address", n0.unfreezerestrictedasset, asset_name, "garbagechangeaddress")

        n0.unfreezerestrictedasset(asset_name, spl_change_address)  # Can only un-freeze once!
        assert_raises_rpc_error(-26, "Unfreezing transaction already in mempool", n0.unfreezerestrictedasset, asset_name, spl_change_address)
        n0.generate(1)
        assert_raises_rpc_error(None, "global-unfreeze-when-not-frozen", n0.unfreezerestrictedasset, asset_name, spl_change_address)

        # post-unfreeze validation
        assert_does_not_contain(asset_name, n0.listglobalrestrictions())
        assert not n0.checkglobalrestriction(asset_name)
        assert_equal(9000, n0.listassetbalancesbyaddress(address)[asset_name])
        change_address = n0.getnewaddress()
        n0.transferfromaddress(asset_name, address, 1000, n1.getnewaddress(), "", "", "", change_address)
        n0.generate(1)
        self.sync_all()
        assert_equal(8000, n0.listassetbalancesbyaddress(change_address)[asset_name])
        assert_equal(2000, n1.listmyassets()[asset_name])
    def freezing(self):
        self.log.info("Testing freezing...")
        n0, n1 = self.nodes[0], self.nodes[1]

        base_asset_name = "FROZEN_TM"
        asset_name = f"${base_asset_name}"
        qty = 11000
        verifier = "true"
        address = n0.getnewaddress()
        safe_address = n0.getnewaddress()
        spl_change_address = n0.getnewaddress()

        n0.issue(base_asset_name)
        n0.generate(1)

        n0.issuerestrictedasset(asset_name, qty, verifier, address)
        n0.generate(1)

        # squirrel some away
        change_address = n0.getnewaddress()
        n0.transferfromaddress(asset_name, address, 1000, safe_address, "", "", "", change_address)
        n0.generate(1)
        address = change_address

        # pre-freezing verification
        assert_does_not_contain(asset_name, n0.listaddressrestrictions(address))
        assert not n0.checkaddressrestriction(address, address)
        assert_equal(10000, n0.listassetbalancesbyaddress(address)[asset_name])

        # Isolate this test from other freezing on n0...
        def viewmyrestrictedaddresses():
            return list(filter(lambda x: asset_name == x['Asset Name'], n0.viewmyrestrictedaddresses()))

        # viewmyrestrictedaddresses
        assert_equal(0, len(viewmyrestrictedaddresses()))

        change_address = n0.getnewaddress()
        n0.transferfromaddress(asset_name, address, 1000, n1.getnewaddress(), "", "", "", change_address)
        n0.generate(1)
        self.sync_all()
        assert_equal(9000, n0.listassetbalancesbyaddress(change_address)[asset_name])
        assert_equal(1000, n1.listmyassets()[asset_name])
        address = change_address  # assets have moved

        assert_raises_rpc_error(None, "Invalid Splendid address", n0.freezeaddress, asset_name, "garbageaddress")
        assert_raises_rpc_error(None, "Invalid Splendid change address", n0.freezeaddress, asset_name, address, "garbagechangeaddress")

        n0.freezeaddress(asset_name, address, spl_change_address)
        n0.generate(1)

        assert_raises_rpc_error(-32600, "freeze-address-when-already-frozen", n0.freezeaddress, asset_name, address, spl_change_address)

        # post-freezing verification
        assert_contains(asset_name, n0.listaddressrestrictions(address))
        assert n0.checkaddressrestriction(address, asset_name)

        # viewmyrestrictedaddresses
        restrictions = viewmyrestrictedaddresses()
        assert_equal(1, len(restrictions))
        r = restrictions[0]
        assert_equal(address, r['Address'])
        assert_equal(asset_name, r['Asset Name'])
        assert_happening(r['Restricted'])

        assert_raises_rpc_error(-8, "No asset outpoints are selected from the given address", n0.transferfromaddress, asset_name, address, 1000, n1.getnewaddress())

        assert_raises_rpc_error(None, "Invalid Splendid address", n0.unfreezeaddress, asset_name, "garbageaddress")
        assert_raises_rpc_error(None, "Invalid Splendid change address", n0.unfreezeaddress, asset_name, address, "garbagechangeaddress")

        n0.unfreezeaddress(asset_name, address, spl_change_address)
        n0.generate(1)

        assert_raises_rpc_error(-32600, "unfreeze-address-when-not-frozen", n0.unfreezeaddress, asset_name, address, spl_change_address)

        # post-unfreezing verification
        assert_does_not_contain(asset_name, n0.listaddressrestrictions(address))
        assert not n0.checkaddressrestriction(address, asset_name)
        assert_equal(9000, n0.listassetbalancesbyaddress(address)[asset_name])

        # viewmyrestrictedaddresses
        restrictions = viewmyrestrictedaddresses()
        assert_equal(1, len(restrictions))
        r = restrictions[0]
        assert_equal(address, r['Address'])
        assert_equal(asset_name, r['Asset Name'])
        assert_happening(r['Derestricted'])

        change_address = n0.getnewaddress()
        n0.transferfromaddress(asset_name, address, 1000, n1.getnewaddress(), "", "", "", change_address)
        n0.generate(1)
        self.sync_all()
        assert_equal(8000, n0.listassetbalancesbyaddress(change_address)[asset_name])
        assert_equal(2000, n1.listmyassets()[asset_name])
예제 #3
0
    def run_test(self):
        nodes = self.nodes

        # BIP39 list should contain 2048 words
        assert_equal(len(bip39_english), 2048)

        # Node 0 has 12 words and a passphrase
        self.log.info("Testing BIP-44 Word-Lists and passphrases")
        assert_equal(len(nodes[0].getmywords()['word_list'].split(' ')), 12) # Contains 12 words
        assert_equal(nodes[0].getmywords()['word_list'], MNEMONIC_0) # Word list matches
        assert_equal(nodes[0].getmywords()['passphrase'], MNEMONIC_PASS_0) # Passphrase matches

        # Node 1 has 12 words but no passphrase
        assert_equal(len(nodes[1].getmywords()['word_list'].split(' ')), 12) # Contains 12 words
        assert_equal(nodes[1].getmywords()['word_list'], MNEMONIC_1) # Word list matches
        assert_does_not_contain(str(nodes[1].getmywords()), 'passphrase') # Passphrase does not exist

        # Node 2 was not created with specific 12 words (using random-auto-generated), and a passphrase
        assert_equal(len(nodes[2].getmywords()['word_list'].split(' ')), 12) # Contains 12 words
        assert(nodes[2].getmywords()['word_list'] != nodes[3].getmywords()['word_list']) # auto-generated word-lists should not match
        assert_equal(nodes[2].getmywords()['passphrase'], MNEMONIC_PASS_2)

        # Node 3 was not created with specific 12 words (using random-auto-generated), and no passphrase
        assert_equal(len(nodes[3].getmywords()['word_list'].split(' ')), 12) # Contains 12 words
        assert_does_not_contain(str(nodes[3].getmywords()), 'passphrase') # Passphrase does not exist

        # Nodes 4 & 5 are not BIP44 and should not have 12-words or passphrase
        assert_raises_rpc_error(-4, "Wallet doesn't have 12 words.", nodes[4].getmywords)
        assert_raises_rpc_error(-4, "Wallet doesn't have 12 words.", nodes[5].getmywords)

        # Cannot change from a non-BIP44 to a BIP44 wallet
        self.log.info("Testing that BIP-44 wallets are intransigent")
        self.stop_node(4)
        self.stop_node(5)
        self.start_node(4, extra_args=['-bip44=1', '-mnemonicpassphrase=test4'])
        self.start_node(5, extra_args=['-bip44=1', '-mnemonic=' + MNEMONIC_0, '-mnemonicpassphrase=' + MNEMONIC_PASS_0])
        assert_raises_rpc_error(-4, "Wallet doesn't have 12 words.", nodes[4].getmywords)
        assert_raises_rpc_error(-4, "Wallet doesn't have 12 words.", nodes[5].getmywords)

        # Try to add a passphrase to an existing bip44 wallet (should not add passphrase)
        self.stop_node(3)
        self.start_node(3, extra_args=['-mnemonicpassphrase=test3'])
        assert_does_not_contain(str(nodes[3].getmywords()), 'passphrase') # Passphrase does not exist

        # Cannot change an already created bip44 wallet to a non-bip44 wallet
        word_list_3 = nodes[3].getmywords()['word_list']
        self.stop_node(3)
        self.start_node(3, extra_args=['-bip44=0'])
        assert_equal(nodes[3].getmywords()['word_list'], word_list_3) # Word list matches

        # All 4 bip44 enabled wallets word-lists are in the bip39 word-list
        self.log.info("Testing that BIP-44 wallets words are valid")
        word_list_0 = MNEMONIC_0.split(' ')
        word_list_1 = MNEMONIC_1.split(' ')
        word_list_2 = nodes[2].getmywords()['word_list'].split(' ')
        word_list_3 = word_list_3.split(' ')
        for i in range(0, 12):
            assert_contains(word_list_0[i], bip39_english)
            assert_contains(word_list_1[i], bip39_english)
            assert_contains(word_list_2[i], bip39_english)
            assert_contains(word_list_3[i], bip39_english)

        # None of the words should be text-readable in the log files
        self.log.info("Testing that BIP-44 words aren't text readable")
        mnemonic_2 = nodes[2].getmywords()['word_list']
        mnemonic_3 = nodes[3].getmywords()['word_list']
        self.stop_nodes()
        with open(os.path.join(self.options.tmpdir+"/node0/regtest/", "debug.log"), 'r', encoding='utf8') as f:
            assert_does_not_contain(MNEMONIC_0, f.read())
        with open(os.path.join(self.options.tmpdir+"/node1/regtest/", "debug.log"), 'r', encoding='utf8') as f:
            assert_does_not_contain(MNEMONIC_1, f.read())
        with open(os.path.join(self.options.tmpdir+"/node2/regtest/", "debug.log"), 'r', encoding='utf8') as f:
            assert_does_not_contain(mnemonic_2, f.read())
        with open(os.path.join(self.options.tmpdir+"/node3/regtest/", "debug.log"), 'r', encoding='utf8') as f:
            assert_does_not_contain(mnemonic_3, f.read())

        # But words are readable in a non-encrypted wallet
        with open(os.path.join(self.options.tmpdir+"/node0/regtest/", "wallet.dat"), 'rb') as f:
            assert_contains(MNEMONIC_0, str(f.read()))
        with open(os.path.join(self.options.tmpdir+"/node1/regtest/", "wallet.dat"), 'rb') as f:
            assert_contains(MNEMONIC_1, str(f.read()))
        with open(os.path.join(self.options.tmpdir+"/node2/regtest/", "wallet.dat"), 'rb') as f:
            assert_contains(mnemonic_2, str(f.read()))
        with open(os.path.join(self.options.tmpdir+"/node3/regtest/", "wallet.dat"), 'rb') as f:
            assert_contains(mnemonic_3, str(f.read()))

        # After encryption the words are no longer readable
        self.log.info("Testing that BIP-44 wallet encryption")
        self.start_nodes()
        nodes[0].node_encrypt_wallet("password0")
        nodes[1].node_encrypt_wallet("password1")
        nodes[2].node_encrypt_wallet("password2")
        nodes[3].node_encrypt_wallet("password3")
        self.stop_nodes()
        # But words are not readable in an encrypted wallet
        with open(os.path.join(self.options.tmpdir+"/node0/regtest/", "wallet.dat"), 'rb') as f:
            assert_does_not_contain(MNEMONIC_0, str(f.read()))
        with open(os.path.join(self.options.tmpdir+"/node1/regtest/", "wallet.dat"), 'rb') as f:
            assert_does_not_contain(MNEMONIC_1, str(f.read()))
        with open(os.path.join(self.options.tmpdir+"/node2/regtest/", "wallet.dat"), 'rb') as f:
            assert_does_not_contain(mnemonic_2, str(f.read()))
        with open(os.path.join(self.options.tmpdir+"/node3/regtest/", "wallet.dat"), 'rb') as f:
            assert_does_not_contain(mnemonic_3, str(f.read()))

        # But the words are still available using getmywords after entering the passphrase
        self.start_nodes()
        assert_raises_rpc_error(-13, "Please enter the wallet passphrase with walletpassphrase first.", nodes[0].getmywords)
        nodes[0].walletpassphrase("password0", 48)
        assert_equal(nodes[0].getmywords()['word_list'], MNEMONIC_0) # Word list matches

        # Words can also be retrieved from the dumpwallet command
        nodes[0].dumpwallet(os.path.join(self.options.tmpdir+"/node0/regtest/", "dump_wallet_0.txt"))
        with open(os.path.join(self.options.tmpdir+"/node0/regtest/", "dump_wallet_0.txt"), 'r') as f:
            assert_contains(MNEMONIC_0, str(f.read()))
    def tagging(self):
        self.log.info("Testing tagging...")
        n0 = self.nodes[0]

        tag = "#TAG"
        address = n0.getnewaddress()
        change_address = n0.getnewaddress()

        n0.issuequalifierasset(tag)
        n0.generate(1)

        base_asset_name = "TAG_RESTRICTED"
        asset_name = f"${base_asset_name}"
        qty = 1000
        verifier = tag
        issue_address = n0.getnewaddress()
        n0.issue(base_asset_name)
        assert_raises_rpc_error(-8, "bad-txns-null-verifier-address-failed-verification", n0.issuerestrictedasset, asset_name, qty, verifier, issue_address)

        # Isolate this test from other tagging on n0...
        def viewmytaggedaddresses():
            return list(filter(lambda x: tag == x['Tag Name'], n0.viewmytaggedaddresses()))

        assert_equal(0, len(viewmytaggedaddresses()))

        n0.addtagtoaddress(tag, issue_address, change_address)
        n0.generate(1)
        n0.issuerestrictedasset(asset_name, qty, verifier, issue_address)
        n0.generate(1)

        # pre-tagging verification
        assert_does_not_contain(address, n0.listaddressesfortag(tag))
        assert_does_not_contain(tag, n0.listtagsforaddress(address))
        assert not n0.checkaddresstag(address, tag)

        # viewmytaggedaddresses
        tagged = viewmytaggedaddresses()
        assert_equal(1, len(tagged))
        t1 = tagged[0]
        assert_equal(issue_address, t1['Address'])
        assert_equal(tag, t1['Tag Name'])
        assert_contains_key('Assigned', t1)
        assert_happening(t1['Assigned'])

        assert_raises_rpc_error(-8, "bad-txns-null-verifier-address-failed-verification", n0.transfer, asset_name, 100, address)

        # special case: make sure transfer fails if change address(es) are verified even though to address isn't
        spl_change_address = n0.getnewaddress()
        asset_change_address = n0.getnewaddress()
        n0.addtagtoaddress(tag, spl_change_address)
        n0.addtagtoaddress(tag, asset_change_address)
        n0.generate(1)
        assert_raises_rpc_error(-8, "bad-txns-null-verifier-address-failed-verification", n0.transfer, asset_name, 100, address, "", 0, spl_change_address, asset_change_address)
        n0.removetagfromaddress(tag, spl_change_address)
        n0.removetagfromaddress(tag, asset_change_address)
        n0.generate(1)
        ##

        assert_raises_rpc_error(None, "Invalid Splendid address", n0.addtagtoaddress, tag, "garbageaddress")
        assert_raises_rpc_error(None, "Invalid Splendid change address", n0.addtagtoaddress, tag, address, "garbagechangeaddress")

        n0.addtagtoaddress(tag, address, change_address)
        n0.generate(1)

        assert_raises_rpc_error(-32600, "add-qualifier-when-already-assigned", n0.addtagtoaddress, tag, address, change_address)

        # post-tagging verification
        assert_contains(address, n0.listaddressesfortag(tag))
        assert_contains(tag, n0.listtagsforaddress(address))
        assert n0.checkaddresstag(address, tag)

        # viewmytaggedaddresses
        tagged = viewmytaggedaddresses()
        assert_equal(4, len(tagged))
        assert_contains(issue_address, list(map(lambda x: x['Address'], tagged)))
        assert_contains(address, list(map(lambda x: x['Address'], tagged)))
        for t in tagged:
            assert_equal(tag, t['Tag Name'])
            if 'Assigned' in t:
                assert_happening(t['Assigned'])
            else:
                assert_happening(t['Removed'])

        # special case: make sure transfer fails if the asset change address isn't verified (even if the spl change address is)
        spl_change_address = n0.getnewaddress()
        asset_change_address = n0.getnewaddress()
        assert_raises_rpc_error(-20, "bad-txns-null-verifier-address-failed-verification", n0.transfer, asset_name, 100, address, "", 0, spl_change_address, asset_change_address)
        n0.addtagtoaddress(tag, spl_change_address)
        n0.generate(1)
        assert_raises_rpc_error(-20, "bad-txns-null-verifier-address-failed-verification", n0.transfer, asset_name, 100, address, "", 0, spl_change_address, asset_change_address)
        n0.removetagfromaddress(tag, spl_change_address)
        n0.generate(1)

        # do the transfer already!
        txid = n0.transfer(asset_name, 100, address)
        n0.generate(1)
        assert_equal(64, len(txid[0]))
        assert_equal(100, n0.listassetbalancesbyaddress(address)[asset_name])

        # do another transfer with specified, tagged asset change address
        asset_change_address = n0.getnewaddress()
        n0.addtagtoaddress(tag, asset_change_address)
        n0.generate(1)
        txid = n0.transfer(asset_name, 1, issue_address, "", 0, "", asset_change_address)
        n0.generate(1)
        assert_equal(64, len(txid[0]))
        assert (n0.listassetbalancesbyaddress(asset_change_address)[asset_name] > 0)

        assert_raises_rpc_error(None, "Invalid Splendid address", n0.removetagfromaddress, tag, "garbageaddress")
        assert_raises_rpc_error(None, "Invalid Splendid change address", n0.removetagfromaddress, tag, address, "garbagechangeaddress")

        n0.removetagfromaddress(tag, address, change_address)
        n0.generate(1)

        assert_raises_rpc_error(-32600, "removing-qualifier-when-not-assigned", n0.removetagfromaddress, tag, address, change_address)

        # TODO: test without specifying change address when there are no valid change addresses (all untagged)

        # post-untagging verification
        assert_does_not_contain(address, n0.listaddressesfortag(tag))
        assert_does_not_contain(tag, n0.listtagsforaddress(address))
        assert not n0.checkaddresstag(address, tag)

        # viewmytaggedaddresses
        tagged = viewmytaggedaddresses()
        assert_equal(6, len(tagged))  # includes removed
        assert_contains(issue_address, list(map(lambda x: x['Address'], tagged)))
        assert_contains(address, list(map(lambda x: x['Address'], tagged)))
        for t in tagged:
            assert_equal(tag, t['Tag Name'])
            if issue_address == t['Address']:
                assert_happening(t['Assigned'])
            if address == t['Address']:
                assert_happening(t['Removed'])

        assert_raises_rpc_error(-8, "bad-txns-null-verifier-address-failed-verification", n0.transfer, asset_name, 100, address)
예제 #5
0
    def test_messaging(self):
        self.log.info("Testing messaging!")
        n0, n1 = self.nodes[0], self.nodes[1]

        spam_name = "SPAM"
        asset_name = "MESSAGING"
        owner_name = "MESSAGING!"
        channel_one = "MESSAGING~ONE"
        channel_two = "MESSAGING~TWO"
        ipfs_hash = "QmZPGfJojdTzaqCWJu2m3krark38X1rqEHBo4SjeqHKB26"

        # need ownership before channels can be created
        assert_raises_rpc_error(-32600,
                                "Wallet doesn't have asset: " + owner_name,
                                n0.issue, channel_one)

        n0.issue(asset_name, 100)
        n0.issue(channel_one)
        n0.issue(channel_two)
        n0.issue(spam_name, 100)

        n0.generate(1)
        self.sync_all()

        # you're auto-subscribed to your own channels
        n0_channels = n0.viewallmessagechannels()
        assert_contains(owner_name, n0_channels)
        assert_contains(channel_one, n0_channels)
        assert_contains(channel_two, n0_channels)

        # n1 subscribes to owner and channel one
        assert_equal([], n1.viewallmessagechannels())
        n1.subscribetochannel(owner_name)
        n1.subscribetochannel(channel_one)
        n1_channels = n1.viewallmessagechannels()
        assert_contains(owner_name, n1_channels)
        assert_contains(channel_one, n1_channels)
        assert_does_not_contain(channel_two, n1_channels)

        # n0 sends a message on owner
        n0.sendmessage(owner_name, ipfs_hash)
        n0.generate(1)
        self.sync_all()

        # n1 views then clears messages
        n1_messages = n1.viewallmessages()
        assert_equal(1, len(n1_messages))
        message = n1_messages[0]
        assert_contains_pair("Asset Name", owner_name, message)
        assert_contains_pair("Message", ipfs_hash, message)
        n1.clearmessages()
        n1_messages = n1.viewallmessages()
        assert_equal(0, len(n1_messages))

        # n0 sends more messages on channels one and two
        n0.sendmessage(channel_one, ipfs_hash)
        n0.sendmessage(channel_two, ipfs_hash)
        n0.generate(1)
        self.sync_all()

        # n1 views then clears messages
        n1_messages = n1.viewallmessages()
        assert_equal(1, len(n1_messages))
        message = n1_messages[0]
        assert_contains_pair("Asset Name", channel_one, message)
        assert_contains_pair("Message", ipfs_hash, message)
        n1.clearmessages()
        n1_messages = n1.viewallmessages()
        assert_equal(0, len(n1_messages))

        # n1 unsubscribes
        n1.unsubscribefromchannel(owner_name)
        n1.unsubscribefromchannel(channel_one)
        assert_equal(0, len(n1.viewallmessagechannels()))

        # auto-subscribe / spam protection (first address use only)
        addr1 = n1.getnewaddress()
        n0.transfer(asset_name, 10, addr1)
        n0.generate(1)
        self.sync_all()
        n0.transfer(spam_name, 10, addr1)
        n1_channels = n1.viewallmessagechannels()
        assert_equal(1, len(n1_channels))
        assert_contains(owner_name, n1_channels)
        assert_does_not_contain(spam_name, n1_channels)
        n1.unsubscribefromchannel(owner_name)

        # pre-existing messages (don't see w/o rescan)
        assert_equal(0, len(n1.viewallmessages()))
        n0.sendmessage(channel_two, ipfs_hash)
        n0.generate(1)
        self.sync_all()
        assert_equal(0, len(n1.viewallmessages()))
        n1.subscribetochannel(channel_two)
        assert_equal(0, len(n1.viewallmessages()))
        n0.sendmessage(channel_two, ipfs_hash)
        n0.generate(1)
        self.sync_all()
        assert_equal(1, len(n1.viewallmessages()))
        assert_contains_pair("Asset Name", channel_two,
                             n1.viewallmessages()[0])
        n1.clearmessages()
        n1.unsubscribefromchannel(channel_two)