Skip to content

Commit b1fd7ca

Browse files
authored
Remove the use of canonicaljson-go (#1611)
1 parent bb888fe commit b1fd7ca

4 files changed

Lines changed: 20 additions & 12 deletions

File tree

go.mod

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ require (
1212
github.com/dominikbraun/graph v0.23.0
1313
github.com/fxamacker/cbor/v2 v2.7.0
1414
github.com/gagliardetto/utilz v0.1.3
15-
github.com/gibson042/canonicaljson-go v1.0.3
1615
github.com/go-json-experiment/json v0.0.0-20250223041408-d3c622f1b874
1716
github.com/go-playground/validator/v10 v10.26.0
1817
github.com/go-viper/mapstructure/v2 v2.4.0

go.sum

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,6 @@ github.com/gabriel-vasile/mimetype v1.4.8/go.mod h1:ByKUIKGjh1ODkGM1asKUbQZOLGrP
6868
github.com/gagliardetto/hashsearch v0.0.0-20191005111333-09dd671e19f9/go.mod h1:513DXpQPzeRo7d4dsCP3xO3XI8hgvruMl9njxyQeraQ=
6969
github.com/gagliardetto/utilz v0.1.3 h1:A+asc+6/3a9qNBrgticApj3yW5F7y4TaJd8Ijg+o0zM=
7070
github.com/gagliardetto/utilz v0.1.3/go.mod h1:b+rGFkRHz3HWJD0RYMzat47JyvbTtpE0iEcYTRJTLLA=
71-
github.com/gibson042/canonicaljson-go v1.0.3 h1:EAyF8L74AWabkyUmrvEFHEt/AGFQeD6RfwbAuf0j1bI=
72-
github.com/gibson042/canonicaljson-go v1.0.3/go.mod h1:DsLpJTThXyGNO+KZlI85C1/KDcImpP67k/RKVjcaEqo=
7371
github.com/go-json-experiment/json v0.0.0-20250223041408-d3c622f1b874 h1:F8d1AJ6M9UQCavhwmO6ZsrYLfG8zVFWfEfMS2MXPkSY=
7472
github.com/go-json-experiment/json v0.0.0-20250223041408-d3c622f1b874/go.mod h1:TiCD2a1pcmjd7YnhGH0f/zKNcCD06B029pHhzV23c2M=
7573
github.com/go-kit/log v0.1.0/go.mod h1:zbhenjAZHb184qTLMA9ZjW7ThYL0H2mk7Q6pNt4vbaY=

pkg/jsonrpc2/types.go

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ import (
77
"fmt"
88
"strings"
99

10-
canonicaljson "github.com/gibson042/canonicaljson-go"
10+
jsonv2 "github.com/go-json-experiment/json"
11+
"github.com/go-json-experiment/json/jsontext"
1112
)
1213

1314
const (
@@ -63,22 +64,27 @@ func (r *Request[Params]) ServiceName() string {
6364
// Digest returns a digest of the request. This is used for signature verification.
6465
// The digest is a SHA256 hash of the canonical JSON string of the request excluding the auth field.
6566
func (r *Request[Params]) Digest() (string, error) {
66-
canonicalJSONBytes, err := canonicaljson.Marshal(Request[Params]{
67+
JSONBytes, err := jsonv2.Marshal(Request[Params]{
6768
Version: r.Version,
6869
ID: r.ID,
6970
Method: r.Method,
7071
Params: r.Params,
7172
// Auth is intentionally excluded from the digest
72-
})
73+
}, jsonv2.Deterministic(true))
7374
if err != nil {
7475
return "", fmt.Errorf("error marshaling JSON: %w", err)
7576
}
7677

78+
canonicalJSONBytes := jsontext.Value(JSONBytes)
79+
err = canonicalJSONBytes.Canonicalize()
80+
if err != nil {
81+
return "", fmt.Errorf("error canonicalizing JSON: %w", err)
82+
}
83+
7784
hasher := sha256.New()
7885
hasher.Write(canonicalJSONBytes)
79-
digestBytes := hasher.Sum(nil)
8086

81-
return hex.EncodeToString(digestBytes), nil
87+
return hex.EncodeToString(hasher.Sum(nil)), nil
8288
}
8389

8490
type Response[Result any] struct {
@@ -92,18 +98,23 @@ type Response[Result any] struct {
9298
// Digest returns a digest of the response. This is used for signature verification.
9399
// The digest is a SHA256 hash of the canonical JSON string of the response.
94100
func (r *Response[Result]) Digest() (string, error) {
95-
canonicalJSONBytes, err := canonicaljson.Marshal(r)
101+
JSONBytes, err := jsonv2.Marshal(r, jsonv2.Deterministic(true))
96102
if err != nil {
97103
return "", fmt.Errorf("error marshaling JSON: %w", err)
98104
}
99105

106+
canonicalJSONBytes := jsontext.Value(JSONBytes)
107+
err = canonicalJSONBytes.Canonicalize()
108+
if err != nil {
109+
return "", fmt.Errorf("error canonicalizing JSON: %w", err)
110+
}
111+
100112
hasher := sha256.New()
101113
if _, err := hasher.Write(canonicalJSONBytes); err != nil {
102114
return "", fmt.Errorf("error writing to hasher: %w", err)
103115
}
104-
digestBytes := hasher.Sum(nil)
105116

106-
return hex.EncodeToString(digestBytes), nil
117+
return hex.EncodeToString(hasher.Sum(nil)), nil
107118
}
108119

109120
// WireError represents a structured error in a Response.

pkg/jsonrpc2/types_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ func TestTypes(t *testing.T) {
4747

4848
digest, err := req.Digest()
4949
require.Error(t, err)
50-
require.Equal(t, "error marshaling JSON: canonicaljson: unsupported type: chan string", err.Error())
50+
require.Equal(t, "error marshaling JSON: json: cannot marshal from Go chan string within \"/params/channel\"", err.Error())
5151
require.Empty(t, digest)
5252
})
5353

0 commit comments

Comments
 (0)