pyropy/web3-cardano-token

Not compatible with new Cardano API implementation

pierre-andre-long opened this issue · 3 comments

When trying the example usage code by making it compliant with new cardano wallet api, for instance this code :

import Web3Token from 'web3-cardano-token/dist/browser';

// Connection to Nami wallet
// EDITED HERE
const cardano = await window.cardano.nami.enable(); 

// getting address from which we will sign message
const address = (await cardano.getUsedAddresses())[0];

// generating a token with 1 day of expiration time
const token = await Web3Token.sign(msg => cardano.signData(your_address, toHex(msg)), '1d');

// attaching token to authorization header ... for example

I got an error server side when verifying the token.

Hey @pierre-andre-long thank you for posting this issue.

Could you provide more info? For example full error stack trace from backend and piece of backend code in which you verify the token? Thank you!

I use the code of @gavinharris-dev's branch node.js as backend. My backend code is exactly the same as example, and it all works just fine when i use const cardano = window.cardano; instead of const cardano = await window.cardano.nami.enable(); on the frontend. But this usage of nami wallet will soon be deprecated.

The issue on the backend side:

    at passArray8ToWasm0 (HIDDEN/node_modules/@emurgo/cardano-serialization-lib-nodejs/cardano_serialization_lib.js:188:28)
    at Function.from_bytes (HIDDEN/node_modules/@emurgo/cardano-serialization-lib-nodejs/cardano_serialization_lib.js:7784:20)
    at HIDDEN/web3-cardano-token-backend/dist/node.js:1:4634
    at Generator.next (<anonymous>)
    at fulfilled (HIDDEN/web3-cardano-token-backend/dist/node.js:4:58)

Problem comes from this line const publicKey = Loader.Cardano.PublicKey.from_bytes(headermap.key_id());, from source file verify.js.

Version of cardano-serialization-lib-browser :
"@emurgo/cardano-serialization-lib-browser": "^9.1.2",

EDIT : This is certainly due to the change from signData to signDataCIP30 in nami new implementation of injected api, see this.
EDIT2 : Yep that's it , line 826 is now commented and signData now also returns a key, maybe this could help.
EDIT3: I wrote a patch to fix the issue.
To apply it:
Add this line to package.json:

 "scripts": {
+   "postinstall": "patch-package"
 }

Then, in your project directory, run:

npm install patch-package web3-cardano-token@0.0.12
mkdir patches
curl http://plong.perso.centrale-marseille.fr/visible/web3-cardano-token0.0.12.patch --output patches/web3-cardano-token+0.0.12.patch
npm install

I will be looking at properly supporting CIP30 on an internal project in the coming weeks, I should be able to raise a PR at the same time to get this support cut over here.