near / near-api-js

JavaScript library to interact with NEAR Protocol via RPC API
https://near.github.io/near-api-js
MIT License
387 stars 239 forks source link

Array deserialization #1063

Open mohitxskull opened 1 year ago

mohitxskull commented 1 year ago

Prerequisites

Description

i have an function on a smart contract which accepts an argument Vec<TokenMetadata> from my node.js backend i am sending an array, and i am getting an errorsaying

ExecutionError: Smart contract panicked: panicked at 'Failed to deserialize input from JSON.: Error("Invalid last symbol 51, offset 29.", line: 1, column: 185)', nft/src/lib.rs:91:1

Reproducible demo

https://github.com/servedbyskull/near-api-js-error

Steps to reproduce

clone the repo yarn yarn build yarn start

Expected behavior

Should run perfectly

Actual behavior

Throwed an error

Your environment

Self-service

andy-haynes commented 1 year ago

Thank you for the detailed description and examples @servedbyskull !

The transaction data appears to be serialized properly in near-api-js since I can deserialize the transaction before it's sent to get the original input. I think the next step would be to try this in the JS and Rust CLIs to see if the behavior is any different there - I'd expect the JS CLI to throw the same error but the Rust CLI uses a different serialization wrapper AFAIK.

mohitxskull commented 1 year ago

Thank you for the detailed description and examples @servedbyskull !

The transaction data appears to be serialized properly in near-api-js since I can deserialize the transaction before it's sent to get the original input. I think the next step would be to try this in the JS and Rust CLIs to see if the behavior is any different there - I'd expect the JS CLI to throw the same error but the Rust CLI uses a different serialization wrapper AFAIK.

err

Tried with JS Cli, same error

err

Tried with Rust Cli, same error

andy-haynes commented 1 year ago

Looking up that Invalid last symbol text I see various Rust libraries with the same code around this error message (disclaimer: I don't speak Rust or know whether transaction serialization relies on this same logic):

    /// The last non-padding input symbol's encoded 6 bits have nonzero bits that will be discarded.
    /// This is indicative of corrupted or truncated Base64.
    /// Unlike InvalidByte, which reports symbols that aren't in the alphabet, this error is for
    /// symbols that are in the alphabet but represent nonsensical encodings.
    InvalidLastSymbol(usize, u8),

...

            DecodeError::InvalidLastSymbol(index, byte) => {
                write!(f, "Invalid last symbol {}, offset {}.", byte, index)
            }

It's interesting that the "last symbol" byte mentioned in the error in your example code is 51 but in the CLI examples it's 117. I can't seem to get a value other than 51 by changing the parameters in the example repo so I'm not sure what could account for the disparity there.

This isn't an issue specific to near-api-js but I'm not sure where it would be best transferred. I can keep the issue open until I have a better place to put it but in the meantime I would try DevRel Office Hours or Discord - they should know who to escalate your issue to.

mohitxskull commented 1 year ago

Any update?

andy-haynes commented 1 year ago

Did you follow up with DevRel or on Discord?