Toniq-Labs / stoic-identity

Identity for DFINITY's IC HttpAgent (to connect to a Stoic wallet via Stoic Connect)
MIT License
27 stars 16 forks source link

TypeError: "list" argument must be an Array of Buffers #5

Closed lastmjs closed 3 years ago

lastmjs commented 3 years ago

I'm trying to run this code:

        const identity = await StoicIdentity.connect();

        const idlFactory = ({ IDL }) => {
            return IDL.Service({
                'test': IDL.Func([IDL.Text], [IDL.Text], ['query'])
            });
        };

        const agent = new HttpAgent({
            identity
        });

        const actor = Actor.createActor(idlFactory, {
            agent,
            canisterId: 'rrkah-fqaaa-aaaaa-aaaaq-cai'
        });

        console.log('result', await actor.test('hello'));

The test canister function looks like this in TypeScript:

export function test(message: string): Query<string> {
    return message;
}

and would have this candid:

    "test": (text) -> (text) query;

When I run the frontend code above, I keep getting this error:

Uncaught (in promise) TypeError: "list" argument must be an Array of Buffers
    at Function.concat (actor-1a9b5abd.js:5665)
    at ic-stoic-identity.js:11532

Everything executes just fine up to await actor.test('hello'), and somewhere during resolving actor.test the error is thrown. I've been trying a couple different combinations of canister methods and parameters and I'm not yet tracking down why this is happening.

For some more information, it seems to be here in the source code where the error is being thrown: https://github.com/Toniq-Labs/stoic-identity/blob/main/src/index.js#L86

Any pointers?

electrovir commented 3 years ago

I noticed some type errors like this when I was rewriting stoic-identity in TypeScript. You could try out my fork with the ic-stoic-identity-1634785846.tgz asset link here: https://github.com/electrovir/stoic-identity/releases

IIRC the type errors I found were centered around de-serializing the local storage data. (It's hard to know for certain where I changed actual logic though cause the git diff is useless since I basically rewrote the whole index.js file to add types.)

lastmjs commented 3 years ago

I just tried with your fork but I get the same issue

lastmjs commented 3 years ago

I think I figured it out, though it is very surprising that stoic-identity works at all if this is the case. Perhaps there has been a browser or dependency update that is causing this on my machine.

This line needs to be changed: https://github.com/Toniq-Labs/stoic-identity/blob/main/src/index.js#L86

Original:

const result = JSON.parse(await this.sign(Buffer.from(Buffer.concat([domainSeparator, new Uint8Array(requestId)]))));

Fixed:

const result = JSON.parse(await this.sign(Buffer.from(Buffer.concat([domainSeparator, Buffer.from(new Uint8Array(requestId))]))));

new Uint8Array(requestId) has been wrapped with Buffer.from.

stephenandrews commented 3 years ago

Nice find! I think it's because the type error is just ignored, but concat handles it OK anyway. Definitely worth the fix tho, thanks so much!

tommygames commented 1 year ago

We're also getting this error when integrating Stoic and trying to make a call with the identity:

TypeError: "list" argument must be an Array of Buffers

Our code looks like this:

        const identity = await StoicIdentity.connect();

        const authCanisterId = 'zxrmm-bqaaa-aaaai-abn6q-cai'

        const idlFactory = ({IDL}) => {
            return IDL.Service({
                'get_auth_token': IDL.Func([], [IDL.Text], [])
            });
        };

        const agent = new HttpAgent({
            identity: identity,
            host: "https://ic0.app/",
        });

        const actor = Actor.createActor(idlFactory, {
            agent,
            canisterId: authCanisterId,
        });

        const token = await actor.get_auth_token();

The code works with all other IC wallets, only having this issue with Stoic

tommygames commented 1 year ago

It turns out my issue was exactly the same as @lastmjs :)

I thought that the fix already existed in the npm package.

I was able to patch the code locally, but would be good to update the ic-stoic-identity npm package with the fix