Woody4618 / SolPlay_Unity_SDK

An SDK that lets you connect to phantom wallet, list NFTs, Swap Tokens, Unlock content with NFTs and interact with any anchor program. Build on top of the Garbles Unity SDK.
MIT License
47 stars 20 forks source link

how add Signer to Transaction Solnet C# #5

Closed Mehrdadgame closed 1 year ago

Mehrdadgame commented 1 year ago

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 commented 1 year ago

Hey, which version of the UnitySDK are you using?

Mehrdadgame commented 1 year ago

Hi No i used Solnet in Vs console

Woody4618 commented 1 year ago

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 commented 1 year ago

Thank you man .