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

how add Signer to Transaction Solnet C# #5

Closed
Mehrdadgame opened this issue Feb 19, 2023 · 4 comments
Closed

how add Signer to Transaction Solnet C# #5

Mehrdadgame opened this issue Feb 19, 2023 · 4 comments

Comments

@Mehrdadgame
Copy link

Hi
How Are You?
I have tow Transaction in Solnet .
I wanna add Signer first Transaction to second Transaction ?
`
Wallet wallet = new Wallet(MnemonicWords);

        Account fromAccount = wallet.GetAccount(10);
        Account toAccount = wallet.GetAccount(8);

        var blockHash = rpcClient.GetLatestBlockHash();
        Console.WriteLine($"BlockHash >> {blockHash.Result.Value.Blockhash}");

        TransactionBuilder txBuilder = new TransactionBuilder()
            .SetRecentBlockHash(blockHash.Result.Value.Blockhash)
            .SetFeePayer(fromAccount)
            .AddInstruction(SystemProgram.Transfer(fromAccount.PublicKey, toAccount.PublicKey, 10000000))
            .AddInstruction(MemoProgram.NewMemo(fromAccount.PublicKey, "Hello from Sol.Net :)"));

        byte[] msgBytes = txBuilder.CompileMessage();
        byte[] signature = fromAccount.Sign(msgBytes);

        byte[] tx = txBuilder.AddSignature(signature)
            .Serialize();

        /////Part2
        ///
        ///
        var decode = Transaction.Deserialize(tx);
        var base58 = new Base58Encoder();
        var convertToStrig = base58.EncodeData(decode.Signatures[0].Signature);

        Wallet walletUser2 = new(MnemonicWordsUser2);

        Account fromAccountUser2 = walletUser2.GetAccount(10);
        Account toAccountUser2 = walletUser2.GetAccount(8);

        var blockHashUser2 = rpcClient.GetLatestBlockHash();
        Console.WriteLine($"BlockHash >> {blockHashUser2.Result.Value.Blockhash}");

        TransactionBuilder txBuilderUser2 = new TransactionBuilder()
            .SetRecentBlockHash(blockHashUser2.Result.Value.Blockhash)
            .SetFeePayer(fromAccountUser2)
            .AddInstruction(SystemProgram.Transfer(fromAccountUser2.PublicKey, toAccountUser2.PublicKey, 10000000))
            .AddInstruction(MemoProgram.NewMemo(fromAccountUser2.PublicKey, "Hello Mehrdad :)"));

        byte[] msgBytesUser2 = txBuilderUser2.CompileMessage();
        byte[] signatureUser2 = fromAccountUser2.Sign(msgBytesUser2);


        var rr = decode.Sign(fromAccountUser2);
        var msgPopulate = Message.Deserialize(txBuilderUser2.CompileMessage());
        var tXPu = Transaction.Populate(msgPopulate, new List<byte[]>
        {
            signatureUser2,
            decode.Signatures[0].Signature
        });


        var txMsgDes = Transaction.Deserialize(tXPu.Serialize());

        var s1 = txMsgDes.Signatures[0].PublicKey.Verify(msgBytesUser2, txMsgDes.Signatures[0].Signature); // true
        var s2 = txMsgDes.Signatures[1].PublicKey.Verify(msgBytesUser2, txMsgDes.Signatures[1].Signature); // true

        var badKeyOrdering = txMsgDes.VerifySignatures(); // returns false


        Console.WriteLine($"Tx base64: {Convert.ToBase64String(txMsgDes.CompileMessage())}");
        RequestResult<ResponseValue<SimulationLogs>> txSimUser2 = rpcClient.SimulateTransaction(tx);
        string logsUser2 = Examples.PrettyPrintTransactionSimulationLogs(txSimUser2.Result.Value.Logs);
        Console.WriteLine($"Transaction Simulation:\n\tError: {txSimUser2.Result.Value.Error}\n\tLogs: \n" + logsUser2);
        RequestResult<string> SecendtSig = rpcClient.SendTransaction(txMsgDes.CompileMessage());
        Console.WriteLine($"Secend Tx Signature: {SecendtSig.Result}");`
@Woody4618
Copy link
Owner

Hey, which version of the UnitySDK are you using?

@Mehrdadgame
Copy link
Author

Hi
No i used Solnet in Vs console

@Woody4618
Copy link
Owner

Then maybe its better to ask there. https://github.com/bmresearch/Solnet/issues Are you using the newest version? I think there were some fixes about signature ordering there a few months back.
Looks like you cant sign tx1 with walletUser2 because the signer needs to be first fromAccount. But its a bit hard to know what you are trying to do.

@Mehrdadgame
Copy link
Author

Thank you man .

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants