Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CLI decode-token: use CBOR diagnostic format instead of pure JSON #334

Merged
merged 1 commit into from
Sep 8, 2024

Conversation

ok300
Copy link
Contributor

@ok300 ok300 commented Sep 7, 2024

Before this PR, decode-token printed the pure JSON representation of a token. For V3 that is straightforward, but for V4 tokens the byte array fields were printed as arrays of numbers (decimal representation of each byte value).

Example output before PR
cargo run -- decode-token cashuBo2F0gqJhaUgA_9SLj17PgGFwgaNhYQFhc3hAYWNjMTI0MzVlN2I4NDg0YzNjZjE4NTAxNDkyMThhZjkwZjcxNmE1MmJmNGE1ZWQzNDdlNDhlY2MxM2Y3NzM4OGFjWCECRFODGd5IXVW-07KaZCvuWHk3WrnnpiDhHki6SCQh88-iYWlIAK0mjE0fWCZhcIKjYWECYXN4QDEzMjNkM2Q0NzA3YTU4YWQyZTIzYWRhNGU5ZjFmNDlmNWE1YjRhYzdiNzA4ZWIwZDYxZjczOGY0ODMwN2U4ZWVhY1ghAjRWqhENhLSsdHrr2Cw7AFrKUL9Ffr1XN6RBT6w659lNo2FhAWFzeEA1NmJjYmNiYjdjYzY0MDZiM2ZhNWQ1N2QyMTc0ZjRlZmY4YjQ0MDJiMTc2OTI2ZDNhNTdkM2MzZGNiYjU5ZDU3YWNYIQJzEpxXGeWZN5qXSmJjY8MzxWyvwObQGr5G1YCCgHicY2FtdWh0dHA6Ly9sb2NhbGhvc3Q6MzMzOGF1Y3NhdA


{
  "m": "http://localhost:3338",
  "u": "sat",
  "t": [
    {
      "i": [
        0,
        255,
        212,
        139,
        143,
        94,
        207,
        128
      ],
      "p": [
        {
          "a": 1,
          "s": "acc12435e7b8484c3cf1850149218af90f716a52bf4a5ed347e48ecc13f77388",
          "c": [
            2,
            68,
            83,
            131,
            25,
            222,
            72,
            93,
            85,
            190,
            211,
            178,
            154,
            100,
            43,
            238,
            88,
            121,
            55,
            90,
            185,
            231,
            166,
            32,
            225,
            30,
            72,
            186,
            72,
            36,
            33,
            243,
            207
          ],
          "d": null
        }
      ]
    },
    {
      "i": [
        0,
        173,
        38,
        140,
        77,
        31,
        88,
        38
      ],
      "p": [
        {
          "a": 2,
          "s": "1323d3d4707a58ad2e23ada4e9f1f49f5a5b4ac7b708eb0d61f738f48307e8ee",
          "c": [
            2,
            52,
            86,
            170,
            17,
            13,
            132,
            180,
            172,
            116,
            122,
            235,
            216,
            44,
            59,
            0,
            90,
            202,
            80,
            191,
            69,
            126,
            189,
            87,
            55,
            164,
            65,
            79,
            172,
            58,
            231,
            217,
            77
          ],
          "d": null
        },
        {
          "a": 1,
          "s": "56bcbcbb7cc6406b3fa5d57d2174f4eff8b4402b176926d3a57d3c3dcbb59d57",
          "c": [
            2,
            115,
            18,
            156,
            87,
            25,
            229,
            153,
            55,
            154,
            151,
            74,
            98,
            99,
            99,
            195,
            51,
            197,
            108,
            175,
            192,
            230,
            208,
            26,
            190,
            70,
            213,
            128,
            130,
            128,
            120,
            156,
            99
          ],
          "d": null
        }
      ]
    }
  ]
}

NUT-00 defines1 how a TokenV4 JSON should look like. That is essentially the Diagnostic Notation described2 in the CBOR spec. There, byte array fields are represented like h'00ad268c4d1f5826', which is an h followed by the hex representation wrapped in single quotes.

This PR updates decode-token in cdk-cli to print the diagnostic notation instead of the pure JSON representation of a token. This brings decoded TokenV4s in line with the format described in NUT-00, especially around how byte array fields should be handled.

Example output after PR
cargo run -- decode-token cashuBo2F0gqJhaUgA_9SLj17PgGFwgaNhYQFhc3hAYWNjMTI0MzVlN2I4NDg0YzNjZjE4NTAxNDkyMThhZjkwZjcxNmE1MmJmNGE1ZWQzNDdlNDhlY2MxM2Y3NzM4OGFjWCECRFODGd5IXVW-07KaZCvuWHk3WrnnpiDhHki6SCQh88-iYWlIAK0mjE0fWCZhcIKjYWECYXN4QDEzMjNkM2Q0NzA3YTU4YWQyZTIzYWRhNGU5ZjFmNDlmNWE1YjRhYzdiNzA4ZWIwZDYxZjczOGY0ODMwN2U4ZWVhY1ghAjRWqhENhLSsdHrr2Cw7AFrKUL9Ffr1XN6RBT6w659lNo2FhAWFzeEA1NmJjYmNiYjdjYzY0MDZiM2ZhNWQ1N2QyMTc0ZjRlZmY4YjQ0MDJiMTc2OTI2ZDNhNTdkM2MzZGNiYjU5ZDU3YWNYIQJzEpxXGeWZN5qXSmJjY8MzxWyvwObQGr5G1YCCgHicY2FtdWh0dHA6Ly9sb2NhbGhvc3Q6MzMzOGF1Y3NhdA


{
    "m": "http://localhost:3338",
    "u": "sat",
    "t": [
        {
            "i": h'00ffd48b8f5ecf80',
            "p": [
                {
                    "a": 1,
                    "s": "acc12435e7b8484c3cf1850149218af90f716a52bf4a5ed347e48ecc13f77388",
                    "c": h'0244538319de485d55bed3b29a642bee5879375ab9e7a620e11e48ba482421f3cf',
                    "d": null,
                },
            ],
        },
        {
            "i": h'00ad268c4d1f5826',
            "p": [
                {
                    "a": 2,
                    "s": "1323d3d4707a58ad2e23ada4e9f1f49f5a5b4ac7b708eb0d61f738f48307e8ee",
                    "c": h'023456aa110d84b4ac747aebd82c3b005aca50bf457ebd5737a4414fac3ae7d94d',
                    "d": null,
                },
                {
                    "a": 1,
                    "s": "56bcbcbb7cc6406b3fa5d57d2174f4eff8b4402b176926d3a57d3c3dcbb59d57",
                    "c": h'0273129c5719e599379a974a626363c333c56cafc0e6d01abe46d5808280789c63',
                    "d": null,
                },
            ],
        },
    ],
}

Footnotes

  1. https://github.com/cashubtc/nuts/blob/main/00.md#example-1

  2. https://www.rfc-editor.org/rfc/rfc8949.html#name-diagnostic-notation

This brings decoded TokenV4s in line with the format specified in NUT-00, especially around how byte array fields are handled.
Copy link
Collaborator

@thesimplekid thesimplekid left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice, thank you been meaning to do something about this for awhile.

Also appreciate the detailed PR notes.

ACK 5cf0403

@thesimplekid thesimplekid merged commit 65c1aea into cashubtc:main Sep 8, 2024
34 checks passed
@ok300 ok300 deleted the ok300-v4-serialize-cbor-diag branch September 8, 2024 20:24
@ok300
Copy link
Contributor Author

ok300 commented Sep 8, 2024

🙏

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants