示例#1
0
    def test_transaction_invalid_accounts(self):
        keys = generate_keys(2)
        tx = Transaction.new(
            keys[0].public_key,
            [Instruction(
                keys[1].public_key,
                bytes([1, 2, 3]),
                [
                    AccountMeta.new(keys[0].public_key, True)
                ],
            )],
        )
        tx.message.instructions[0].program_index = 2  # invalid index
        with pytest.raises(ValueError):
            Transaction.unmarshal(tx.marshal())

        tx = Transaction.new(
            keys[0].public_key,
            [Instruction(
                keys[1].public_key,
                bytes([1, 2, 3]),
                [
                    AccountMeta.new(keys[0].public_key, True)
                ],
            )],
        )
        tx.message.instructions[0].accounts = bytes([2])  # invalid index
        with pytest.raises(ValueError):
            Transaction.unmarshal(tx.marshal())
示例#2
0
    def test_create_associated_account(self):
        keys = [key.public_key for key in generate_keys(3)]

        expected_addr = get_associated_account(keys[1], keys[2])

        instruction, addr = create_associated_token_account(
            keys[0], keys[1], keys[2])
        assert addr == expected_addr

        assert len(instruction.data) == 0
        assert len(instruction.accounts) == 7

        assert instruction.accounts[0].is_signer
        assert instruction.accounts[0].is_writable
        assert not instruction.accounts[1].is_signer
        assert instruction.accounts[1].is_writable

        for i in range(2, len(instruction.accounts)):
            assert not instruction.accounts[i].is_signer
            assert not instruction.accounts[i].is_writable

        assert instruction.accounts[4].public_key == system.PROGRAM_KEY
        assert instruction.accounts[5].public_key == PROGRAM_KEY
        assert instruction.accounts[6].public_key == system.RENT_SYS_VAR

        tx = Transaction.unmarshal(
            Transaction.new(keys[0], [instruction]).marshal())
        decompiled = decompile_create_associated_account(tx.message, 0)
        assert decompiled.subsidizer == keys[0]
        assert decompiled.owner == keys[1]
        assert decompiled.mint == keys[2]
示例#3
0
    def test_memo_progam(self):
        data = 'somedata'
        i = memo_instruction(data)

        assert i.data.decode('utf-8') == data

        tx = Transaction.unmarshal(Transaction.new(PublicKey(bytes(32)), [i]).marshal())
        memo = decompile_memo(tx.message, 0)
        assert memo.data.decode('utf-8') == data
示例#4
0
 def test_transaction_missing_blockhash(self):
     payer, program = generate_keys(2)
     tx = Transaction.new(
         payer.public_key,
         [Instruction(
             program.public_key,
             bytes([1, 2, 3]),
             [
                 AccountMeta.new(payer.public_key, True),
             ],
         )],
     )
     assert Transaction.unmarshal(tx.marshal()) == tx
示例#5
0
    def test_set_authority(self):
        public_keys = [key.public_key for key in generate_keys(3)]

        # With no new authority
        instruction = set_authority(public_keys[0], public_keys[1], AuthorityType.AccountHolder, _token_program)

        assert instruction.data[0] == Command.SET_AUTHORITY
        assert instruction.data[1] == AuthorityType.AccountHolder
        assert instruction.data[2] == 0

        tx = Transaction.unmarshal(Transaction.new(public_keys[0], [instruction]).marshal())
        decompiled = decompile_set_authority(tx.message, 0, _token_program)
        assert decompiled.account == public_keys[0]
        assert decompiled.current_authority == public_keys[1]
        assert decompiled.authority_type == AuthorityType.AccountHolder
        assert not decompiled.new_authority

        # With new authority
        instruction = set_authority(public_keys[0], public_keys[1], AuthorityType.CloseAccount, _token_program,
                                    new_authority=public_keys[2])

        assert instruction.data[0] == Command.SET_AUTHORITY
        assert instruction.data[1] == AuthorityType.CloseAccount
        assert instruction.data[2] == 1
        assert instruction.data[3:] == public_keys[2].raw

        assert not instruction.accounts[0].is_signer
        assert instruction.accounts[0].is_writable
        assert instruction.accounts[1].is_signer
        assert not instruction.accounts[1].is_writable

        tx = Transaction.unmarshal(Transaction.new(public_keys[0], [instruction]).marshal())
        decompiled = decompile_set_authority(tx.message, 0, _token_program)
        assert decompiled.account == public_keys[0]
        assert decompiled.current_authority == public_keys[1]
        assert decompiled.authority_type == AuthorityType.CloseAccount
        assert decompiled.new_authority == public_keys[2]
示例#6
0
    def test_initialize_account(self):
        public_keys = [key.public_key for key in generate_keys(3)]
        instruction = initialize_account(public_keys[0], public_keys[1], public_keys[2], _token_program)

        assert instruction.data == bytes([Command.INITIALIZE_ACCOUNT])
        assert instruction.accounts[0].is_signer
        assert instruction.accounts[0].is_writable
        for i in range(1, 4):
            assert not instruction.accounts[i].is_signer
            assert not instruction.accounts[i].is_writable

        tx = Transaction.unmarshal(Transaction.new(public_keys[0], [instruction]).marshal())
        decompiled = decompile_initialize_account(tx.message, 0, _token_program)
        assert decompiled.account == public_keys[0]
        assert decompiled.mint == public_keys[1]
        assert decompiled.owner == public_keys[2]
示例#7
0
    def test_create_account(self):
        public_keys = [key.public_key for key in generate_keys(3)]
        instruction = create_account(public_keys[0], public_keys[1],
                                     public_keys[2], 12345, 67890)

        assert len(instruction.data) == 52
        assert instruction.data[0:4] == Command.CREATE_ACCOUNT.to_bytes(
            4, 'little')
        assert instruction.data[4:12] == (12345).to_bytes(8, 'little')
        assert instruction.data[12:20] == (67890).to_bytes(8, 'little')
        assert instruction.data[20:] == public_keys[2].raw

        tx = Transaction.unmarshal(
            Transaction.new(public_keys[0], [instruction]).marshal())
        decompiled = decompile_create_account(tx.message, 0)
        assert decompiled.funder == public_keys[0]
        assert decompiled.address == public_keys[1]
        assert decompiled.owner == public_keys[2]
        assert decompiled.lamports == 12345
        assert decompiled.size == 67890
示例#8
0
    def test_transfer(self):
        public_keys = [key.public_key for key in generate_keys(3)]
        instruction = transfer(public_keys[0], public_keys[1], public_keys[2], 123456789, _token_program)

        assert instruction.data[0] == Command.TRANSFER
        assert instruction.data[1:] == (123456789).to_bytes(8, 'little')

        assert not instruction.accounts[0].is_signer
        assert instruction.accounts[0].is_writable
        assert not instruction.accounts[1].is_signer
        assert instruction.accounts[1].is_writable
        assert instruction.accounts[2].is_signer
        assert instruction.accounts[2].is_writable

        tx = Transaction.unmarshal(Transaction.new(public_keys[0], [instruction]).marshal())
        decompiled = decompile_transfer(tx.message, 0, _token_program)
        assert decompiled.source == public_keys[0]
        assert decompiled.dest == public_keys[1]
        assert decompiled.owner == public_keys[2]
        assert decompiled.amount == 123456789
示例#9
0
    def test_transaction_cross_impl(self):
        pk = PrivateKey(bytes([48, 83, 2, 1, 1, 48, 5, 6, 3, 43, 101, 112, 4, 34, 4, 32, 255, 101, 36, 24, 124, 23,
                               167, 21, 132, 204, 155, 5, 185, 58, 121, 75]))
        program_id = PublicKey(bytes([2, 2, 2, 4, 5, 6, 7, 8, 9, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 9, 8, 7, 6,
                                      5, 4, 2, 2, 2]))
        to = PublicKey(bytes([1, 1, 1, 4, 5, 6, 7, 8, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 8, 7, 6, 5, 4, 1,
                              1, 1]))

        tx = Transaction.new(
            pk.public_key,
            [
                Instruction(
                    program_id,
                    bytes([1, 2, 3]),
                    [AccountMeta.new(pk.public_key, True), AccountMeta.new(to, False)],
                ),
            ],
        )
        tx.sign([pk])

        generated = base64.b64decode(_RUST_GENERATED_ADJUSTED)
        assert tx.marshal() == generated
        assert Transaction.unmarshal(generated) == tx