Mind reviewing my x402 core library?

Could you please review my x402 core library? x402 provides possibility to request (small) payments for accessing your web-server.

I’ve been writing it for the last few weeks. Code style-wise I follow examples from Zig std.http and std.json.

Are there any best practices I’ve missed?

Thank you!

2 Likes

I’m not the greatest reviewer… anyways, below my two cents (and yes, those are really only TWO cents, haha)

This is really minor, but the byte checker in Network.zig:36-42 could be more comfortable to read.

    for (reference) |byte| {
        if (!((byte >= 'a' and byte <= 'z') or
            (byte >= 'A' and byte <= 'Z') or
            (byte >= '0' and byte <= '9') or
            byte == '-' or byte == '_'))
            return error.InvalidNetwork;
    }

It’s not that great to read, in my opinion. I’d write the following instead:

    for (reference) |byte| {
        switch (byte) {
            'a'...'z', 'A'...'Z', '0'...'9', '-', '_' => {},
            else => return error.InvalidNetwork,
        }
    }

Another improvement would be more explicit error unions. What I mean by that is that you sometimes (e.g. PaymentPayload.zig:64) have a fn name(args) !Ret {} instead of fn name(args) Error!Ret {}.

6 Likes

Thank you, it’s the greatest review in this thread!

2 Likes

Agree, that’s clearer to me too.

Good point, I could define ValidationError and ParseError and use them explicitly.
Parse error would be std.json.ParseError(std.json.Reader).