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

Messed up Principal Ref #3

Closed aramakme closed 3 years ago

aramakme commented 3 years ago

I think I have a messed up principal reference somewhere. I'm using vite and am trying to integrate Stoic.

Error:

Uncaught (in promise) Error: Attempt to hash a value of unsupported type: q4eej-kyaaa-aaaaa-aaaha-cai
    at hashValue (vendor.90183b1f.js:formatted:13276)
    at vendor.90183b1f.js:formatted:13282
    at Array.map (<anonymous>)
    at requestIdOf (vendor.90183b1f.js:formatted:13282)
    at vendor.90183b1f.js:formatted:18634
    at new Promise (<anonymous>)
    at StoicIdentity.transformRequest (vendor.90183b1f.js:formatted:18629)
    at HttpAgent.call (vendor.90183b1f.js:formatted:8054)
    at async r (vendor.90183b1f.js:formatted:8632)
    at async te.i.loggedIn (index.e2d5eb40.js:1)

This is how I set things up: canisterID is a Text agent is let stoicIdentity = await StoicIdentity.load(); myCanister = Actor.createActor(canister_idl, { agent, canisterId: canisterID });

The following are the results from the console when I put a break point at

if (A instanceof Principal)
        return hash$3(A.toUint8Array());

A instanceof Principal - false A instanceof Principal$2 - true

I end up hitting:

throw Object.assign(new Error(`Attempt to hash a value of unsupported type: ${A}`), {
        value: A
    })

Principal$2

{ "_arr": { "0": 0, "1": 0, "2": 0, "3": 0, "4": 0, "5": 0, "6": 0, "7": 14, "8": 1, "9": 1 }, "_isPrincipal": true }

Do I need to wrap my canister id in something? Is there a ref inside the stoic Identity I can use?

aramakme commented 3 years ago

It looks like stoic-identity puts some node_modules underneath it with another copy of the agent libraries. I guess because of this it creates Principal and Principal$1 and Principal$2 as different object types. There fore the instanceof Principal doesn't work.

If I add a branch to hash_value in request_id.js I can get it to work:

else if (typeof value === 'object' &&
        value !== null &&
        value._isPrincipal == true &&
        typeof value.toUint8Array === 'function') {
        return hashValue(value.toUint8Array());

    }

I'll make an issue on the agent project to include this branch. It is possible this was caused because I originally had 0.9.7 for my project and npm installed stoic-identity referencing agent 0.10.0 and now npm can't unwind things.

stephenandrews commented 3 years ago

Thanks - yeah @jorgenbuilder came across the same sorts of things (as I have I in the past), definitely because of a mismatch I believe. Will try to see if we can make a fix from our end too incase they don't merge from the agent repo.

jorgenbuilder commented 3 years ago

Kyle is going to try to get the fix into next version of agent-js: https://forum.dfinity.org/t/mismatching-dfinity-agent-versions-can-cause-hashing-issues/7359/4

Not sure if @aramakme already found a temporary fix, but this works for me: https://github.com/aviate-labs/dots/commit/136575e21b10015932916f8e3f2b3d3c5dc2f7d5

tldr: instanceof operator doesn't work well across modules in npm.