Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Bug] Sometimes the signature does not correspond to the public key #441

Open
libvirgo opened this issue Dec 29, 2023 · 0 comments
Open
Assignees
Labels
bug Something isn't working

Comments

@libvirgo
Copy link

libvirgo commented Dec 29, 2023

Describe the bug
Sometimes a "Transaction verify failured" error occurs when a transaction is requested
debug shows that the signature does not correspond to the public key.

To Reproduce

    var rpcClient = ClientFactory.GetClient("http://localhost:8899");

    var alice = new Account([], new PublicKey("EypZqXgKUMP5RAMQCUpcwmJx8jknxPLJcD8PzwRPXBUE").KeyBytes);
    var aliceXTokenAccount = new PublicKey("74Ldi1XCcS4JuYnYmKxFo5ZPkX6ZWF3M1YV4RMEnyBAm");
    var aliceYTokenAccount = new PublicKey("CegyVoHotKcqGAeU5krSUjqKX2Nd7aKoJ1WKjTpVLdRQ");
    var escrowProgramId = new PublicKey("9qtJTdFfmKcuxNDD99qn87yS9Y7Axq4QaAFEFTT3z6in");
    var tempTokenAccount = new Account();
    Console.WriteLine(
        $"Alice temp token account: {tempTokenAccount.PublicKey.Key}");
    var escrowAccount = new Account();
    Console.WriteLine($"Alice Escrow account: {escrowAccount.PublicKey.Key}");

    var xMintAccount = await rpcClient.GetTokenAccountInfoAsync(aliceXTokenAccount);
    Console.WriteLine($"Alice X mint account: {xMintAccount.Result.Value.Data.Parsed.Info.Mint}");
    var yMintAccount = await rpcClient.GetTokenAccountInfoAsync(aliceYTokenAccount);
    Console.WriteLine($"Alice Y mint account: {yMintAccount.Result.Value.Data.Parsed.Info.Mint}");

    var minBalanceForExemptionAcc =
        await rpcClient.GetMinimumBalanceForRentExemptionAsync(TokenProgram.TokenAccountDataSize);
    var minBalanceForExemptionEscrow =
        await rpcClient.GetMinimumBalanceForRentExemptionAsync(105);

    var createTempTokenAccountIx = SystemProgram.CreateAccount(
        alice.PublicKey,
        tempTokenAccount.PublicKey,
        minBalanceForExemptionAcc.Result,
        TokenProgram.TokenAccountDataSize,
        TokenProgram.ProgramIdKey);
    var initTempAccountIx = TokenProgram.InitializeAccount(
        tempTokenAccount.PublicKey,
        new PublicKey(xMintAccount.Result.Value.Data.Parsed.Info.Mint),
        alice.PublicKey
    );
    var createTransferIx = TokenProgram.Transfer(
        aliceXTokenAccount,
        tempTokenAccount.PublicKey,
        10,
        alice.PublicKey);
    var createEscrowIx = SystemProgram.CreateAccount(
        alice.PublicKey,
        escrowAccount.PublicKey,
        minBalanceForExemptionEscrow.Result,
        105,
        escrowProgramId);
    var initEscrowIx = TransactionInstructionFactory.Create(
        escrowProgramId,
        new List<AccountMeta>()
        {
            AccountMeta.ReadOnly(alice.PublicKey, true),
            AccountMeta.Writable(tempTokenAccount.PublicKey, false),
            AccountMeta.ReadOnly(aliceYTokenAccount, false),
            AccountMeta.Writable(escrowAccount.PublicKey, false),
            AccountMeta.ReadOnly(SysVars.RentKey, false),
            AccountMeta.ReadOnly(TokenProgram.ProgramIdKey, false),
        },
        [0, 10, 0, 0, 0, 0, 0, 0, 0]
    );

    var block = await rpcClient.GetLatestBlockHashAsync();
    var message = new TransactionBuilder()
        .SetRecentBlockHash(block.Result.Value.Blockhash)
        .SetFeePayer(alice)
        .AddInstruction(createTempTokenAccountIx)
        .AddInstruction(initTempAccountIx)
        .AddInstruction(createTransferIx)
        .AddInstruction(createEscrowIx)
        .AddInstruction(initEscrowIx);
    var msg = message.CompileMessage();
    var transaction = message.Build(new List<Account>() { alice, tempTokenAccount, escrowAccount });
    // .Build(new List<Account> { alice, tempTokenAccount, escrowAccount });
    // verify signature
    var tx = Transaction.Deserialize(transaction);
    Console.WriteLine(
        $"key1:{tx.Signatures[0].PublicKey} signature1:{tx.Signatures[0].PublicKey.Verify(msg, tx.Signatures[0].Signature)}\n" +
        $"key2:{tx.Signatures[1].PublicKey} signature2:{tx.Signatures[1].PublicKey.Verify(msg, tx.Signatures[1].Signature)} signature3:{tx.Signatures[1].PublicKey.Verify(msg, tx.Signatures[2].Signature)}\n" +
        $"key3:{tx.Signatures[2].PublicKey} signature3:{tx.Signatures[2].PublicKey.Verify(msg, tx.Signatures[2].Signature)} signature2:{tx.Signatures[2].PublicKey.Verify(msg, tx.Signatures[1].Signature)}");
    // var sig = await rpcClient.SendTransactionAsync(transaction);
    // Console.WriteLine($"sig result:{sig.Result}, raw:{sig.RawRpcResponse}");

Expected behavior
"transaction verify failured" never happens

Screenshots

Alice temp token account: DF5rPH7wD8957bUco1Hdk7LakZwnrg8E1PZFuhxUhWuK
Alice Escrow account: BBGurECrghixLFw8LgA9xKragzb2SBQ2bXuuwjv4CdNL
Alice X mint account: C4xqgBWFMyX7D8atVKYrezWBQjCxr5iD4EcxoENFKDJ4
Alice Y mint account: 5QX23zQRgfow7SDPLrDmh41oK8ucwYFiq94k7ViWgYaK
key1:EypZqXgKUMP5RAMQCUpcwmJx8jknxPLJcD8PzwRPXBUE signature1:True
key2:BBGurECrghixLFw8LgA9xKragzb2SBQ2bXuuwjv4CdNL signature2:False signature3:True
key3:DF5rPH7wD8957bUco1Hdk7LakZwnrg8E1PZFuhxUhWuK signature3:False signature2:True
@libvirgo libvirgo added the bug Something isn't working label Dec 29, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants