zkp/bigint.go
2022-11-14 17:46:23 +08:00

27 lines
483 B
Go

package zkp
import (
"fmt"
"math/big"
)
type big_Int big.Int
func (b big_Int) MarshalJSON() ([]byte, error) {
i := big.Int(b)
v := fmt.Sprintf("\"%x\"", i.Bytes())
return []byte(v), nil
}
func (b *big_Int) UnmarshalJSON(p []byte) error {
if string(p) == "null" {
return nil
}
var v = []byte{}
if _, err := fmt.Sscanf(string(p), "\"%x\"", &v); err != nil {
return fmt.Errorf("not a valid big integer: %s", p)
}
*b = big_Int(*new(big.Int).SetBytes(v))
return nil
}