cashubtc / cashu-ts

A TypeScript library for building Cashu wallets
MIT License
57 stars 33 forks source link

Can't convert valid v3 token into v4 token. #177

Open Kelbie opened 1 month ago

Kelbie commented 1 month ago
getEncodedTokenV4(getDecodedToken("cashuAeyJ0b2tlbiI6W3sicHJvb2ZzIjpbeyJhbW91bnQiOjIsInNlY3JldCI6ImVhMWM2ZDM5NzY0ZGVmNWExNzc2ZDJmNzhiYTMzOTQ0NTY0NWFhZDhkYjkzZmIxZGFlZDU4ZDZiOTVlMWY0ZGQiLCJDIjoiMDIwN2I4ZWUzZmNmOWE3MmY2NGE2ODE5M2E1MDQ1YzQyZGRkN2M0ZGIwYjkxMmIxMmI1OGZlNmQ0NDMyYWVkNThjIiwiaWQiOiI5bWxmZDV2Q3pnR2wifV0sIm1pbnQiOiJodHRwczovL21pbnQubWluaWJpdHMuY2FzaC9CaXRjb2luIn1dLCJ1bml0Ijoic2F0In0"));

hex string expected, got non-hex character "9m" at index 0

This seems to fail because the id's aren't valid hex strings.

cstenglein commented 2 weeks ago

Decoding the token I get

{
  "token": [
    {
      "proofs": [
        {
          "amount": 2,
          "secret": "ea1c6d39764def5a1776d2f78ba339445645aad8db93fb1daed58d6b95e1f4dd",
          "C": "0207b8ee3fcf9a72f64a68193a5045c42ddd7c4db0b912b12b58fe6d4432aed58c",
          "id": "9mlfd5vCzgGl"
        }
      ],
      "mint": "https://mint.minibits.cash/Bitcoin"
    }
  ],
  "unit": "sat"
}

9m is not a valid hex char and id should be a hex string: https://github.com/cashubtc/nuts/blob/main/00.md#proof

It worked for the v3 token because a v3 cashu token was just converted to base64, while a v4 token needs their data encoded to binary: https://github.com/cashubtc/nuts/blob/main/00.md#token-format-1

So the error lies in the token, not the library IMHO.

gandlafbtc commented 2 weeks ago

It is true that there are still keysets out there that are not in hex format.

Egge21M commented 3 days ago

Yes, this is a expected. V4 token are incompatible with old keyset IDs. https://github.com/cashubtc/cashu-ts/pull/193 will change much of the token logic. I will make sure to include a proper error message for this case in there as well.