InvalidWitnessesUTXO after submitting

Hey guys!

Some transactions I’ve been creating are throwing an InvalidWitnessesUTXO error. After some investigation, it seems to happen when they have multiple inputs that contain multiassets, because in this case I try to extract the coin from the output and merge the multiassets into a single output.

I’m not sure what’s wrong, my suspicion is that I ask for the user to sign a transaction but when it gets in the ledger it gets mutated somehow (is there some kind of multiasset sorting or anything?) and the ledger generates a different hash for the signature.

By the way, those transactions are not being created by cardano-cli, their bytes are manually generated, so probably there’s where the error is. Here are some examples of transactions that are throwing this error:

84A300828258206E653F99BEA970245BF3D9D789506FD5E63F2B2A4477553BC092DB9C6F49B17A00825820AB1F82271CE3BF378967F9990F32D92B15DF619CA7A9DFD31BB23C4653D9B73701018282583901CC2D0DD2B430C73E49CA10BCB69256E2078E09AAA0F25A60EF3505A6F7C259A5CF29BB0439A084B77C918ABA70140943EF5017B68D47597F821A00ABA9C7A9581C3C2CFD4F1AD33678039CFD0347CCA8DF363C710067D739624218ABC0A751576F726C647357697468696E30303038330151576F726C647357697468696E30303039320151576F726C647357697468696E30303236320151576F726C647357697468696E30303637320151576F726C647357697468696E30323939340151576F726C647357697468696E30333332360151576F726C647357697468696E303334353001581C83C0AB67AFC9148BD1571B7A14DE1DF03CD5624F5992D3B8EC84D6FBA44D4164614E696E6A617A30303936014D4164614E696E6A617A32393635014D4164614E696E6A617A36343637014D4164614E696E6A617A3637323201581C83CB87B69639E20D7C99755FCFC310FB47882C3591778A3C869EA34CA34A417473756B6F30363137014A417473756B6F34333231014A417473756B6F3737373701581CAA4F61AF3A968B979B53C5F2CBD9EBC656C172CEF6A49626DB24482DA44F507572706C65466F756E64657233330150507572706C65466F756E6465723239320150507572706C65466F756E6465723933390151507572706C65466F756E6465723130383701581CAF2E27F580F7F08E93190A81F72462F153026D06450924726645891BA144445249501AEE6B1860581CC16777D742784B7ACD3940E0356596D313E141200FE8E48C8DF16BA7A2581942697446696E734861776B66696E556E636F6D6D6F6E32313901581942697446696E7350656E67757068696E45786F74696331343501581C5A46271A9B32A4517CED20BE4BA1F184C2F91B1A5DD480AB639EEE57A14D42726967687450616C3231313301581C7DDC646D0E15B44DB03F0F904E0AB6B72E57AA48D2E579CE66366A3DA4544265617273436C75624D656D626572303137363701544265617273436C75624D656D626572303538323901544265617273436C75624D656D626572303635353801544265617273436C75624D656D626572303639313601581CA4ACDB40F495B61BC98F10C4DF10CA7204F2EFCADDC9E0A2AF9C8E04A153436861726C7A20466F756E64657220233536340183581D7115EEA1EEAB457FB4545F260BFC84506DFF8B5026E82DD1E516A859A3821A001A4EC4A1581C83CB87B69639E20D7C99755FCFC310FB47882C3591778A3C869EA34CA14A417473756B6F333637390158205C1AFDA1752274151ED7EA1E816BD7206076CE0813A23DAF7501E0AD2528DB3E021A0003E785A10081825820FC9FADC1DCDCA0816E59815FE85A0D990FB7F8CF1BD98F3A52D191DA11882BEB58403B11BE2D64A87BB4876677CA7E2E709B9C1699C8038A267E94E514CE68E0ED2D95DC4B69BD1B6A822391EF197C7D9836BFDB218F080EA3D73F171EF1168B9E04F5F6
84A30082825820353AC8FABB2963D9C5FD2160B706973D70333A87F4B9421BF7404A7EB5AC7A420182582039C885AD9B66757B9E2C1B7F2C816FF67B8D56AF857DC451752926C241F9C46F000182825839015E914486736E665A966C010AE54017593F6D69E6E00ACD0DC6531E8D337C746991A49CEE49ED190F2FDEA52887D33F84F1E7E71F3B415515821A1796433FA4581C1CDA8886F4CBAB2217346187A923B3BE670E42EF5778A0AD7D25358CB44F4D7574616E74546F61643032313030014F4D7574616E74546F61643033303330014F4D7574616E74546F61643033323732014F4D7574616E74546F61643033333535014F4D7574616E74546F61643033383038014F4D7574616E74546F61643033383239014F4D7574616E74546F61643033383639014F4D7574616E74546F61643033393434014F4D7574616E74546F61643034303932014F4D7574616E74546F61643034313139014F4D7574616E74546F61643034323236014F4D7574616E74546F61643034343232014F4D7574616E74546F61643034373431014F4D7574616E74546F61643034393439014F4D7574616E74546F61643033303336014F4D7574616E74546F61643033333239014F4D7574616E74546F61643033343031014F4D7574616E74546F61643034343636014F4D7574616E74546F61643034363732014F4D7574616E74546F6164303439343701581CD517F38DD2C5ACC3347C8E933E2C0185FAFE3BA838F8830000A3C95FA14F4D7574616E7443726F63303439323801581C7DDC646D0E15B44DB03F0F904E0AB6B72E57AA48D2E579CE66366A3DA4544265617273436C75624D656D626572303133373501544265617273436C75624D656D626572303137353601544265617273436C75624D656D626572303238303701544265617273436C75624D656D626572303336333301581CF4988F549728DC76B58D7677849443CAF6E5385DC67E6C25F6AA901EA54A506978656C54696C6534014B506978656C54696C653238014B506978656C54696C653532014B506978656C54696C653535014B506978656C54696C6535390183581D7115EEA1EEAB457FB4545F260BFC84506DFF8B5026E82DD1E516A859A3821A001A4EC4A1581C1CDA8886F4CBAB2217346187A923B3BE670E42EF5778A0AD7D25358CA14F4D7574616E74546F61643033393139015820BBE0865A7AF0500E70474074674E8D45E6FDBB2FC37CEE2F12023F2A15BFEA36021A0003BA25A100818258206395B3BA1098D9855AB88FA56D51EF9CFCA792CDD4701561CCE2FE7E00748C0858401CB6F5B2EBF2D8D44FDA986F479163C4E47586ABDDE411CC8E093EB1AF5281F8561E8219DB9C47F3F842E5151DB65C5AA9DC476CF60A08A40E37D478D1054C04F5F6

Thank you in advance for the help!