Skip to content

Commit

Permalink
custom marshaller for token v4 to use hex strings
Browse files Browse the repository at this point in the history
  • Loading branch information
elnosh committed Nov 1, 2024
1 parent 8e030b3 commit 767dcee
Showing 1 changed file with 37 additions and 0 deletions.
37 changes: 37 additions & 0 deletions cashu/cashu.go
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,17 @@ type TokenV4Proof struct {
Proofs []ProofV4 `json:"p"`
}

func (tp *TokenV4Proof) MarshalJSON() ([]byte, error) {
tokenProof := struct {
Id string `json:"i"`
Proofs []ProofV4 `json:"p"`
}{
Id: hex.EncodeToString(tp.Id),
Proofs: tp.Proofs,
}
return json.Marshal(tokenProof)
}

type ProofV4 struct {
Amount uint64 `json:"a"`
Secret string `json:"s"`
Expand All @@ -253,12 +264,38 @@ type ProofV4 struct {
DLEQ *DLEQV4 `json:"d,omitempty"`
}

func (p *ProofV4) MarshalJSON() ([]byte, error) {
proof := struct {
Amount uint64 `json:"a"`
Secret string `json:"s"`
C string `json:"c"`
Witness string `json:"w,omitempty"`
DLEQ *DLEQV4 `json:"d,omitempty"`
}{
Amount: p.Amount,
Secret: p.Secret,
C: hex.EncodeToString(p.C),
Witness: p.Witness,
DLEQ: p.DLEQ,
}
return json.Marshal(proof)
}

type DLEQV4 struct {
E []byte `json:"e"`
S []byte `json:"s"`
R []byte `json:"r"`
}

func (d *DLEQV4) MarshalJSON() ([]byte, error) {
dleq := DLEQProof{
E: hex.EncodeToString(d.E),
S: hex.EncodeToString(d.S),
R: hex.EncodeToString(d.R),
}
return json.Marshal(dleq)
}

func NewTokenV4(proofs Proofs, mint string, unit Unit, includeDLEQ bool) (TokenV4, error) {
if unit != Sat {
return TokenV4{}, ErrInvalidUnit
Expand Down

0 comments on commit 767dcee

Please sign in to comment.