feat: Waku v2 bridge

Issue #12610
This commit is contained in:
Michal Iskierko
2023-11-12 13:29:38 +01:00
parent 56e7bd01ca
commit 6d31343205
6716 changed files with 1982502 additions and 5891 deletions

View 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
}

View 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
}

View 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
}