Ejemplo n.º 1
0
 async def deposit(self, ctx, block_id: int = -1):
     """Used to deposit Nyzo on you account"""
     if block_id != -1:
         count = 0
         successed = 0
         result = await async_get("{}/block/{}".format(
             CONFIG["api_url"], block_id),
                                  is_json=True)
         for block in result:
             for transaction in block["value"]["transactions"]:
                 if transaction["value"]["receiver_identifier"] == MAIN_ADDRESS and \
                         transaction["value"]["sender_data"]:
                     count += 1
                     if self.deposit_transaction(
                             transaction["value"]["amount"] -
                             ceil(transaction["value"]["amount"] * 0.0025),
                             bytes.fromhex(transaction["value"]
                                           ["sender_data"]).decode("utf-8"),
                             transaction["value"]["signature"],
                             transaction["value"]["sender_identifier"]):
                         successed += 1
         await ctx.send(
             "{} deposit transactions found in block {}\nSuccessfuly processed {}"
             .format(count, block_id, successed))
     else:
         nyzostring = NyzoStringEncoder.encode(
             NyzoStringPrefilledData.from_hex(
                 MAIN_ADDRESS,
                 str(ctx.author.id).encode().hex()))
         await ctx.send(
             "To deposit nyzo on your account, send a transaction to `{}` with `{}` in the data field\n"
             "Or use this nyzostring: `{}`\nYou will get a message once the deposit is validated."
             .format(MAIN_ID, ctx.author.id, nyzostring))
def test_vector44000():
    nyzo_string = NyzoStringPrefilledData.from_hex(
        "b0c5aef7f03d6c95-e6643f98e12196cb-41fff5e71d4903f4-55419cf80e28781e",
        "")
    encoded = NyzoStringEncoder.encode(nyzo_string)
    assert encoded == "pre_8s35IMwNfnQmXDg_De4yCJK1__oE7kB3.5m1Efxea7xv0c64GT24"
    decoded = NyzoStringEncoder.decode(encoded)
    assert decoded.receiver_identifier == nyzo_string.receiver_identifier
    assert decoded.sender_data == nyzo_string.sender_data
    assert decoded.get_bytes() == nyzo_string.get_bytes()
def test_vector0():
    nyzo_string = NyzoStringPrefilledData.from_hex(
        "1a74dff9c1bb47f7-01eabcfd217f2d6e-ab58a6889efaa44b-5bbe06d396596560",
        "82fb2a31c2fdedd9",
    )
    encoded = NyzoStringEncoder.encode(nyzo_string)
    assert (
        encoded ==
        "pre_ahGSV_E1LSwV0vH-_i5_bnYInar8EMHBiTL~1Kennnmx28bZaA72_vVqroRgFqRe")
    decoded = NyzoStringEncoder.decode(encoded)
    assert decoded.receiver_identifier == nyzo_string.receiver_identifier
    assert decoded.sender_data == nyzo_string.sender_data
    assert decoded.get_bytes() == nyzo_string.get_bytes()
def test_vector75000():
    nyzo_string = NyzoStringPrefilledData.from_hex(
        "7182d875367fbb32-eef8831d47079a78-fd5daf7aabaa145e-1bdb4914676ae579",
        "8044e813bd9acf83149a31d819e22c820edc76f3cd00cd87e67958495e562cd3",
    )
    encoded = NyzoStringEncoder.encode(nyzo_string)
    assert (
        encoded ==
        "pre_go62U7kUwZJQZMz37kt7DEA.or.YHYFkoyMsihhErLmX8814Y1e.DJ~359FPU1Ezb88eV7sRRg3dy~qXn4CvmzRjFuo.qVfn"
    )
    decoded = NyzoStringEncoder.decode(encoded)
    assert decoded.receiver_identifier == nyzo_string.receiver_identifier
    assert decoded.sender_data == nyzo_string.sender_data
    assert decoded.get_bytes() == nyzo_string.get_bytes()
def test_vector17000():
    nyzo_string = NyzoStringPrefilledData.from_hex(
        "53daaf0f7243222a-89324a82a2f2da6d-e9a3c025c3156b9d-fbd1bdab6af1409c",
        "536142cd5127b085fd48d3512cc3300abdd7bbca774d691e76437f7db0",
    )
    encoded = NyzoStringEncoder.encode(nyzo_string)
    assert (
        encoded ==
        "pre_fCfrIN.QgQ8Hzj9axHbQUDVGF-0CNPmIEwMhMrKH-k2t7mdygJTh9Z25_kAjkiR3c0H.TZMauSTG7Eq3wVUNIh0cVwG-"
    )
    decoded = NyzoStringEncoder.decode(encoded)
    assert decoded.receiver_identifier == nyzo_string.receiver_identifier
    assert decoded.sender_data == nyzo_string.sender_data
    assert decoded.get_bytes() == nyzo_string.get_bytes()
Ejemplo n.º 6
0
def test_vector0new():
    nyzo_string = NyzoStringPrefilledData.from_hex(
        "848db2de31cbe4c4-28dbb9e6bdda3aba-98581356ab0e6e02-37b37fd370ac3c7b",
        # id__88idJKWPQ~j4adLXXIVreIHpn1dnHNXL0AvRw.dNI3PZXtxdHx7u
        "",
    )
    encoded = NyzoStringEncoder.encode(nyzo_string)
    assert (
        encoded == "pre_8pidJKWPQ~j4adLXXIVreIHpn1dnHNXL0AvRw.dNI3PZ0fp93qvz")
    decoded = NyzoStringEncoder.decode(encoded)
    assert decoded.receiver_identifier == nyzo_string.receiver_identifier
    assert decoded.sender_data == nyzo_string.sender_data
    assert decoded.amount == nyzo_string.amount
    assert decoded.get_bytes() == nyzo_string.get_bytes()
Ejemplo n.º 7
0
def test_vector0new2():
    nyzo_string = NyzoStringPrefilledData.from_hex(
        "848db2de31cbe4c4-28dbb9e6bdda3aba-98581356ab0e6e02-37b37fd370ac3c7b",
        # id__88idJKWPQ~j4adLXXIVreIHpn1dnHNXL0AvRw.dNI3PZXtxdHx7u
        "",
        10 *
        1000000  # Amount to be provided as int, micronyzos. no amount = 0.
    )
    encoded = NyzoStringEncoder.encode(nyzo_string)
    assert (
        encoded ==
        "pre_apidJKWPQ~j4adLXXIVreIHpn1dnHNXL0AvRw.dNI3PZx0000000D9r0fJmDjEFZ")
    decoded = NyzoStringEncoder.decode(encoded)
    assert decoded.receiver_identifier == nyzo_string.receiver_identifier
    assert decoded.sender_data == nyzo_string.sender_data
    assert decoded.amount == nyzo_string.amount
    assert decoded.get_bytes() == nyzo_string.get_bytes()
Ejemplo n.º 8
0
    def decode(cls, encoded_string: str) -> NyzoString:
        result = None
        try:
            # Map characters from the old encoding to the new encoding. A few characters were changed to make Nyzo
            # strings more URL-friendly.
            encoded_string = encoded_string.replace('*', '-').replace(
                '+', '.').replace('=', '~')
            # Map characters that may be mistyped. Nyzo strings contain neither 'l' nor 'O'.
            encoded_string = encoded_string.replace('l', '1').replace('O', '0')
            # Get the type from the prefix. Here, type is the 4 char prefix as string.
            string_type = encoded_string[:4]
            # If the type is valid, continue.
            if string_type in NYZO_PREFIXES:
                # Get the array representation of the encoded string.
                expanded_array = cls.bytes_for_encoded_string(encoded_string)
                # Get the content length from the next byte and calculate the checksum length.
                content_length = expanded_array[3] & 0xff
                # print("content_length", content_length)
                checksum_length = len(expanded_array) - content_length - 4
                # print("checksum_length", checksum_length)
                # Only continue if the checksum length is valid.
                if 4 <= checksum_length <= 6:
                    # Calculate the checksum and compare it to the provided checksum.
                    # Only create the result array if the checksums match.
                    content_buffer = memoryview(
                        expanded_array)[0:HEADER_LENGTH + content_length]
                    calculated_checksum = sha256(
                        sha256(content_buffer).digest()).digest(
                        )[:checksum_length]
                    provided_checksum = memoryview(
                        expanded_array)[-checksum_length:]
                    if provided_checksum.tobytes() == calculated_checksum:
                        # Get the content array. This is the encoded object with the prefix, length byte, and checksum
                        # removed.
                        content_bytes = memoryview(
                            expanded_array)[HEADER_LENGTH:content_length +
                                            HEADER_LENGTH]
                        # print("Content", content_bytes.tobytes())
                        # Make the object from the content array.
                        if string_type == 'pre_':
                            result = NyzoStringPrefilledData.from_bytes(
                                content_bytes)
                        elif string_type == 'key_':
                            result = NyzoStringPrivateSeed(content_bytes)
                        elif string_type == 'id__':
                            result = NyzoStringPublicIdentifier(content_bytes)
                        elif string_type == 'pay_':
                            result = NyzoStringMicropay.from_bytes(
                                content_bytes)
                        elif string_type == 'tx__':
                            result = NyzoStringTransaction.from_bytes(
                                content_bytes)
                    else:
                        print("Invalid checksum: <{}> vs calc <{}>".format(
                            provided_checksum.tobytes(), provided_checksum))
                else:
                    print("Invalid checksum len: <{}>".format(checksum_length))
            else:
                print("Unknown String type: <{}>".format(string_type))
        except Exception as e:
            print("Exception decode: {}".format(e))  # debug

        return result