28
vendor/github.com/status-im/status-go/services/wallet/bigint/big_int.go
generated
vendored
Normal file
28
vendor/github.com/status-im/status-go/services/wallet/bigint/big_int.go
generated
vendored
Normal file
@@ -0,0 +1,28 @@
|
||||
package bigint
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"math/big"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type BigInt struct {
|
||||
*big.Int
|
||||
}
|
||||
|
||||
func (b BigInt) MarshalJSON() ([]byte, error) {
|
||||
return []byte("\"" + b.String() + "\""), nil
|
||||
}
|
||||
|
||||
func (b *BigInt) UnmarshalJSON(p []byte) error {
|
||||
if string(p) == "null" {
|
||||
return nil
|
||||
}
|
||||
z := new(big.Int)
|
||||
_, ok := z.SetString(strings.Trim(string(p), "\""), 10)
|
||||
if !ok {
|
||||
return fmt.Errorf("not a valid big integer: %s", string(p))
|
||||
}
|
||||
b.Int = z
|
||||
return nil
|
||||
}
|
||||
33
vendor/github.com/status-im/status-go/services/wallet/bigint/hex_big_int.go
generated
vendored
Normal file
33
vendor/github.com/status-im/status-go/services/wallet/bigint/hex_big_int.go
generated
vendored
Normal file
@@ -0,0 +1,33 @@
|
||||
package bigint
|
||||
|
||||
import (
|
||||
"math/big"
|
||||
"reflect"
|
||||
|
||||
"github.com/ethereum/go-ethereum/common/hexutil"
|
||||
)
|
||||
|
||||
// Unmarshals a u256 as a fixed-length hex string with 0x prefix and leading zeros
|
||||
type HexBigInt struct {
|
||||
*big.Int
|
||||
}
|
||||
|
||||
const FixedLength = 32 // u256 -> 32 bytes
|
||||
|
||||
var (
|
||||
hexBigIntT = reflect.TypeOf(HexBigInt{})
|
||||
)
|
||||
|
||||
func (b *HexBigInt) UnmarshalJSON(input []byte) error {
|
||||
var buf [FixedLength]byte
|
||||
err := hexutil.UnmarshalFixedJSON(hexBigIntT, input, buf[:])
|
||||
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
z := new(big.Int)
|
||||
z.SetBytes(buf[:])
|
||||
b.Int = z
|
||||
return nil
|
||||
}
|
||||
60
vendor/github.com/status-im/status-go/services/wallet/bigint/sql_big_int.go
generated
vendored
Normal file
60
vendor/github.com/status-im/status-go/services/wallet/bigint/sql_big_int.go
generated
vendored
Normal file
@@ -0,0 +1,60 @@
|
||||
package bigint
|
||||
|
||||
import (
|
||||
"database/sql/driver"
|
||||
"errors"
|
||||
"math/big"
|
||||
)
|
||||
|
||||
// SQLBigInt type for storing uint256 in the databse.
|
||||
// FIXME(dshulyak) SQL big int is max 64 bits. Maybe store as bytes in big endian and hope
|
||||
// that lexographical sorting will work.
|
||||
type SQLBigInt big.Int
|
||||
|
||||
// Scan implements interface.
|
||||
func (i *SQLBigInt) Scan(value interface{}) error {
|
||||
if value == nil {
|
||||
return nil
|
||||
}
|
||||
val, ok := value.(int64)
|
||||
if !ok {
|
||||
return errors.New("not an integer")
|
||||
}
|
||||
(*big.Int)(i).SetInt64(val)
|
||||
return nil
|
||||
}
|
||||
|
||||
// Value implements interface.
|
||||
func (i *SQLBigInt) Value() (driver.Value, error) {
|
||||
val := (*big.Int)(i)
|
||||
if val == nil {
|
||||
return nil, nil
|
||||
}
|
||||
if !val.IsInt64() {
|
||||
return nil, errors.New("not an int64")
|
||||
}
|
||||
return (*big.Int)(i).Int64(), nil
|
||||
}
|
||||
|
||||
// SQLBigIntBytes type for storing big.Int as BLOB in the databse.
|
||||
type SQLBigIntBytes big.Int
|
||||
|
||||
func (i *SQLBigIntBytes) Scan(value interface{}) error {
|
||||
if value == nil {
|
||||
return nil
|
||||
}
|
||||
val, ok := value.([]byte)
|
||||
if !ok {
|
||||
return errors.New("not an integer")
|
||||
}
|
||||
(*big.Int)(i).SetBytes(val)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (i *SQLBigIntBytes) Value() (driver.Value, error) {
|
||||
val := (*big.Int)(i)
|
||||
if val == nil {
|
||||
return nil, nil
|
||||
}
|
||||
return (*big.Int)(i).Bytes(), nil
|
||||
}
|
||||
Reference in New Issue
Block a user