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

Provide UnmarshalJSON() for common.uint160 #944

Merged
merged 1 commit into from
Aug 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion common/uint160.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,11 +89,23 @@ func IsValidHexAddr(s []byte) bool {
return false
}

func (f Uint160) MarshalJSON() ([]byte, error) {
func (f *Uint160) MarshalJSON() ([]byte, error) {
str, err := f.ToAddress()
return []byte("\"" + str + "\""), err
}

func (f *Uint160) UnmarshalJSON(in []byte) (err error) {
if len(in) < 2 {
return errors.New("[Common]: Uint160UnmarshalJSON err, len < 2")
}
temp, err := ToScriptHash(string(in[1 : len(in)-1]))
if err != nil {
return err
}
f.SetBytes(temp.ToArray())
return nil
}

func (f *Uint160) ToAddress() (string, error) {
data := append(big.NewInt(FOOLPROOFPREFIX).Bytes(), f.ToArray()...)
temp := sha256.Sum256(data)
Expand Down
72 changes: 72 additions & 0 deletions common/uint160_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package common

import (
"encoding/hex"
"testing"
)

// NKN address: NKNPkGps7i6yye6W2qSVsQUgiyqxWHEoTRby
// ToHexString(): 867ff0ca31905faec269949de37d27773ba7394e
const NKNADDRESS = "\"NKNPkGps7i6yye6W2qSVsQUgiyqxWHEoTRby\""
const NKNADDRESS_HEX = "867ff0ca31905faec269949de37d27773ba7394e"

func TestMarshalJSON(t *testing.T) {
// Create instance of Uint160
f := new(Uint160)

dat, err := hex.DecodeString(NKNADDRESS_HEX)
if err != nil {
t.Fatal(err)
}
f.SetBytes(dat)

// Expected value after Marshalling
expected := []byte(NKNADDRESS)

bytes, err := f.MarshalJSON()
if err != nil {
t.Fatalf("Unexpected error: %v", err)
}

if !bytesEqual(bytes, expected) {
t.Fatalf("Expected %v, got %v", expected, bytes)
}

// TODO: If possible, create a scenario where ToAddress returns an error
// and verify that MarshalJSON also returns this error.
}

func TestUnmarshalJSON(t *testing.T) {
// Input for unmarshalling
input := []byte(NKNADDRESS)

f := new(Uint160)
err := f.UnmarshalJSON(input)
if err != nil {
t.Fatalf("Unexpected error: %v", err)
}

// Verify that f has been updated correctly
expected, err := hex.DecodeString(NKNADDRESS_HEX)
if err != nil {
t.Fatal(err)
}
if !bytesEqual(f.ToArray(), expected) {
t.Fatalf("Expected %v, got %v", expected, f.ToArray())
}

// TODO: If possible, create a scenario where ToScriptHash returns an error
// and verify that UnmarshalJSON also returns this error.
}

func bytesEqual(a, b []byte) bool {
if len(a) != len(b) {
return false
}
for i, v := range a {
if v != b[i] {
return false
}
}
return true
}