2019-09-26 15:36:23 +00:00
|
|
|
package utils
|
|
|
|
|
|
|
|
import "math/big"
|
|
|
|
|
|
|
|
// MarshalBigInt marshalls big int into text string for consistent encoding
|
2020-04-23 15:49:25 +00:00
|
|
|
func MarshalBigInt(i *big.Int) (string, error) {
|
2019-09-26 15:36:23 +00:00
|
|
|
bz, err := i.MarshalText()
|
2020-04-23 15:49:25 +00:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
2020-11-24 15:43:08 +00:00
|
|
|
|
2020-04-23 15:49:25 +00:00
|
|
|
return string(bz), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// MustMarshalBigInt marshalls big int into text string for consistent encoding.
|
|
|
|
// It panics if an error is encountered.
|
|
|
|
func MustMarshalBigInt(i *big.Int) string {
|
|
|
|
str, err := MarshalBigInt(i)
|
2019-09-26 15:36:23 +00:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2020-11-24 15:43:08 +00:00
|
|
|
|
2020-04-23 15:49:25 +00:00
|
|
|
return str
|
2019-09-26 15:36:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// UnmarshalBigInt unmarshalls string from *big.Int
|
|
|
|
func UnmarshalBigInt(s string) (*big.Int, error) {
|
|
|
|
ret := new(big.Int)
|
2020-11-24 15:43:08 +00:00
|
|
|
|
2019-09-26 15:36:23 +00:00
|
|
|
err := ret.UnmarshalText([]byte(s))
|
2020-04-23 15:49:25 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-11-24 15:43:08 +00:00
|
|
|
|
2020-04-23 15:49:25 +00:00
|
|
|
return ret, nil
|
2019-09-26 15:36:23 +00:00
|
|
|
}
|
2019-10-16 00:46:50 +00:00
|
|
|
|
2020-04-23 15:49:25 +00:00
|
|
|
// MustUnmarshalBigInt unmarshalls string from *big.Int.
|
|
|
|
// It panics if an error is encountered.
|
2019-10-16 00:46:50 +00:00
|
|
|
func MustUnmarshalBigInt(s string) *big.Int {
|
2020-04-23 15:49:25 +00:00
|
|
|
ret, err := UnmarshalBigInt(s)
|
2019-10-16 00:46:50 +00:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2020-11-24 15:43:08 +00:00
|
|
|
|
2019-10-16 00:46:50 +00:00
|
|
|
return ret
|
|
|
|
}
|