예제 #1
0
 def __encode_message(
         self) -> bytes:  # TODO: Replace this with a construct struct.
     MessageFormat = NamedTuple(
         "MessageFormat",
         [
             ("num_required_signatures", bytes),
             ("num_readonly_signed_accounts", bytes),
             ("num_readonly_unsigned_accounts", bytes),
             ("pubkeys_length", bytes),
             ("pubkeys", bytes),
             ("recent_blockhash", bytes),
         ],
     )
     return b"".join(
         MessageFormat(
             num_required_signatures=helpers.to_uint8_bytes(
                 self.header.num_required_signatures),
             num_readonly_signed_accounts=helpers.to_uint8_bytes(
                 self.header.num_readonly_signed_accounts),
             num_readonly_unsigned_accounts=helpers.to_uint8_bytes(
                 self.header.num_readonly_unsigned_accounts),
             pubkeys_length=shortvec.encode_length(len(self.account_keys)),
             pubkeys=b"".join(
                 [bytes(pubkey) for pubkey in self.account_keys]),
             recent_blockhash=b58decode(
                 self.recent_blockhash.encode("ascii")),
         ))
예제 #2
0
def test_find_program_address():
    """Test create associated_token_address."""
    program_id = PublicKey("BPFLoader1111111111111111111111111111111111")
    program_address, nonce = PublicKey.find_program_address([bytes()],
                                                            program_id)
    assert program_address == PublicKey.create_program_address(
        [bytes(), helpers.to_uint8_bytes(nonce)], program_id)
예제 #3
0
 def __encode_instruction(
     instruction: "CompiledInstruction",
 ) -> bytes:  # TODO: Replace this with a construct struct.
     InstructionFormat = NamedTuple(
         "InstructionFormat",
         [
             ("program_idx", bytes),
             ("accounts_length", bytes),
             ("accounts", bytes),
             ("data_length", bytes),
             ("data", bytes),
         ],
     )
     data = b58decode(instruction.data)
     data_length = shortvec.encode_length(len(data))
     return b"".join(
         InstructionFormat(
             program_idx=helpers.to_uint8_bytes(
                 instruction.program_id_index),
             accounts_length=shortvec.encode_length(
                 len(instruction.accounts)),
             accounts=bytes(instruction.accounts),
             data_length=data_length,
             data=data,
         ))
예제 #4
0
    def find_program_address(seeds: List[bytes],
                             program_id: PublicKey) -> Tuple[PublicKey, int]:
        """Find a valid program address.

        Valid program addresses must fall off the ed25519 curve.  This function
        iterates a nonce until it finds one that when combined with the seeds
        results in a valid program address.
        """
        nonce = 255
        while nonce != 0:
            try:
                buffer = seeds + [helpers.to_uint8_bytes(nonce)]
                address = PublicKey.create_program_address(buffer, program_id)
            except Exception:
                nonce -= 1
                continue
            return address, nonce
        raise KeyError("Unable to find a viable program address nonce")
예제 #5
0
def test_to_uint8_bytes():
    """Test int to uint8 bytes."""
    assert helpers.to_uint8_bytes(255) == b"\xff"
    with pytest.raises(OverflowError):
        helpers.to_uint8_bytes(256)
예제 #6
0
def test_from_uint8():
    """Test uint8 bytes to int."""
    num = randint(0, 255)
    assert helpers.from_uint8_bytes(helpers.to_uint8_bytes(num)) == num