wallet

package
v0.1.5 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 30, 2025 License: MIT Imports: 12 Imported by: 0

Documentation

Overview

Package wallet provides a wallet for the Renegade system

Index

Constants

View Source
const (

	// MaxBalances is the maximum number of balances in a wallet
	MaxBalances = 10
	// MaxOrders is the maximum number of orders in a wallet
	MaxOrders = 4
)
View Source
const OrderSide_BUY = 0 //nolint:revive

OrderSide_BUY is a buy side order

View Source
const OrderSide_SELL = 1 //nolint:revive

OrderSide_SELL is a sell side order

Variables

This section is empty.

Functions

func DeriveWalletID

func DeriveWalletID(privateKey *ecdsa.PrivateKey, chainID uint64) (uuid.UUID, error)

DeriveWalletID derives the wallet ID from the private key

func FromScalarsRecursive

func FromScalarsRecursive(s interface{}, scalars *ScalarIterator) error

FromScalarsRecursive is a helper function to deserialize a struct from a slice of scalars using reflection

Types

type Balance

type Balance struct {
	// Mint is the erc20 address of the balance's asset
	Mint Scalar
	// Amount is the amount of the balance
	Amount Scalar
	// RelayerFeeBalance is the balance due to the relayer in fees
	RelayerFeeBalance Scalar
	// ProtocolFeeBalance is the balance due to the protocol in fees
	ProtocolFeeBalance Scalar
}

Balance is a balance in the Renegade system

func NewBalance

func NewBalance(mint Scalar, amount Scalar) Balance

NewBalance creates a new balance with the given mint and amount

func NewEmptyBalance

func NewEmptyBalance() Balance

NewEmptyBalance creates a new balance with all zero values

func (*Balance) IsZero

func (b *Balance) IsZero() bool

IsZero returns true if the balance amount and fees are zero

type BalanceBuilder

type BalanceBuilder struct {
	// contains filtered or unexported fields
}

BalanceBuilder is a builder for Balance

func NewBalanceBuilder

func NewBalanceBuilder() *BalanceBuilder

NewBalanceBuilder creates a new BalanceBuilder

func (*BalanceBuilder) Build

func (bb *BalanceBuilder) Build() Balance

Build returns the constructed Balance

func (*BalanceBuilder) WithAmount

func (bb *BalanceBuilder) WithAmount(amount Scalar) *BalanceBuilder

WithAmount sets the Amount

func (*BalanceBuilder) WithAmountBigInt

func (bb *BalanceBuilder) WithAmountBigInt(amount *big.Int) *BalanceBuilder

WithAmountBigInt sets the Amount from a big.Int

func (*BalanceBuilder) WithMint

func (bb *BalanceBuilder) WithMint(mint Scalar) *BalanceBuilder

WithMint sets the Mint

func (*BalanceBuilder) WithMintHex

func (bb *BalanceBuilder) WithMintHex(hexMint string) *BalanceBuilder

WithMintHex sets the Mint from a hex string

func (*BalanceBuilder) WithProtocolFeeBalance

func (bb *BalanceBuilder) WithProtocolFeeBalance(fee Scalar) *BalanceBuilder

WithProtocolFeeBalance sets the ProtocolFeeBalance

func (*BalanceBuilder) WithRelayerFeeBalance

func (bb *BalanceBuilder) WithRelayerFeeBalance(fee Scalar) *BalanceBuilder

WithRelayerFeeBalance sets the RelayerFeeBalance

type FeeEncryptionKey

type FeeEncryptionKey struct {
	X Scalar
	Y Scalar
}

FeeEncryptionKey is a public encryption key on the Baby Jubjub curve We represent the key in coordinate form with scalar values

func (*FeeEncryptionKey) FromBytes

func (pk *FeeEncryptionKey) FromBytes(bytes []byte) error

FromBytes converts a byte slice to a fee encryption key

func (*FeeEncryptionKey) FromHexString

func (pk *FeeEncryptionKey) FromHexString(hexString string) error

FromHexString converts a hex string to a fee encryption key

func (*FeeEncryptionKey) ToBytes

func (pk *FeeEncryptionKey) ToBytes() []byte

ToBytes converts the fee encryption key to a byte slice

func (*FeeEncryptionKey) ToHexString

func (pk *FeeEncryptionKey) ToHexString() string

ToHexString converts the fee encryption key to a hex string

type FixedPoint

type FixedPoint struct {
	// Repr is the integral representation of the fixed point number
	Repr Scalar
}

FixedPoint is a fixed point number with a scalar representation The scalar represents the value `floor(repr >> 2^PRECISION)` For our purposes, the precision is 63 bits

func FixedPointFromFloat

func FixedPointFromFloat(f float64) FixedPoint

FixedPointFromFloat creates a new fixed point number from a float

func NewFixedPoint

func NewFixedPoint(repr Scalar) FixedPoint

NewFixedPoint creates a new fixed point number from a scalar representation

func ZeroFixedPoint

func ZeroFixedPoint() FixedPoint

ZeroFixedPoint is the fixed point number 0

func (*FixedPoint) FromReprDecimalString

func (fp *FixedPoint) FromReprDecimalString(s string) (FixedPoint, error)

FromReprDecimalString creates a new fixed point number from a decimal string

func (FixedPoint) ToFloat

func (fp FixedPoint) ToFloat() float64

ToFloat converts a fixed point number to a float

func (FixedPoint) ToReprDecimalString

func (fp FixedPoint) ToReprDecimalString() string

ToReprDecimalString converts a fixed point number to the base10 string representation of its `repr`

type HmacKey

type HmacKey [32]byte

HmacKey is a symmetric key for HMAC-SHA256

func (*HmacKey) FromBase64String

func (k *HmacKey) FromBase64String(b64String string) (HmacKey, error)

FromBase64String converts a base64 string to an HMAC key

func (*HmacKey) FromHexString

func (k *HmacKey) FromHexString(hexString string) (HmacKey, error)

FromHexString converts a hex string to an HMAC key

func (*HmacKey) ToBase64String

func (k *HmacKey) ToBase64String() string

ToBase64String converts the HMAC key to a base64 string

func (*HmacKey) ToHexString

func (k *HmacKey) ToHexString() string

ToHexString converts the HMAC key to a hex string

type Keychain

type Keychain struct {
	PublicKeys  PublicKeychain
	PrivateKeys PrivateKeychain
}

Keychain is a keychain for the API wallet

func DeriveKeychain

func DeriveKeychain(pkey *ecdsa.PrivateKey, chainID uint64) (*Keychain, error)

DeriveKeychain derives the keychain from the private key

func (*Keychain) SkRoot

func (k *Keychain) SkRoot() *PrivateSigningKey

SkRoot returns the private root key

type Order

type Order struct {
	// ID is the id of the order
	Id uuid.UUID `scalar_serialize:"skip"` //nolint:revive
	// QuoteMint is the erc20 address of the quote asset
	QuoteMint Scalar
	// BaseMint is the erc20 address of the base asset
	BaseMint Scalar
	// Side is the side of the order
	// 0 for buy, 1 for sell
	Side Scalar
	// Amount is the amount of the order
	Amount Scalar
	// WorstCasePrice is the worst case price of the order
	WorstCasePrice FixedPoint
}

Order is an order in the Renegade system

func NewEmptyOrder

func NewEmptyOrder() Order

NewEmptyOrder creates a new empty order

func NewOrder

func NewOrder(
	quoteMint Scalar,
	baseMint Scalar,
	side OrderSide,
	amount Scalar,
	worstCasePrice FixedPoint,
) Order

NewOrder creates a new order

func (*Order) IsZero

func (o *Order) IsZero() bool

IsZero returns whether the volume of the order is zero

type OrderBuilder

type OrderBuilder struct {
	// contains filtered or unexported fields
}

OrderBuilder is a builder for Order

func NewOrderBuilder

func NewOrderBuilder() *OrderBuilder

NewOrderBuilder creates a new OrderBuilder

func (*OrderBuilder) Build

func (ob *OrderBuilder) Build() Order

Build returns the constructed Order

func (*OrderBuilder) WithAmount

func (ob *OrderBuilder) WithAmount(amount Scalar) *OrderBuilder

WithAmount sets the Amount

func (*OrderBuilder) WithAmountBigInt

func (ob *OrderBuilder) WithAmountBigInt(amount *big.Int) *OrderBuilder

WithAmountBigInt sets the Amount from a big.Int

func (*OrderBuilder) WithBaseMint

func (ob *OrderBuilder) WithBaseMint(baseMint Scalar) *OrderBuilder

WithBaseMint sets the BaseMint

func (*OrderBuilder) WithBaseMintHex

func (ob *OrderBuilder) WithBaseMintHex(hexBaseMint string) *OrderBuilder

WithBaseMintHex sets the BaseMint from a hex string

func (*OrderBuilder) WithId

func (ob *OrderBuilder) WithId(id uuid.UUID) *OrderBuilder

WithId sets the Id

func (*OrderBuilder) WithQuoteMint

func (ob *OrderBuilder) WithQuoteMint(quoteMint Scalar) *OrderBuilder

WithQuoteMint sets the QuoteMint

func (*OrderBuilder) WithQuoteMintHex

func (ob *OrderBuilder) WithQuoteMintHex(hexQuoteMint string) *OrderBuilder

WithQuoteMintHex sets the QuoteMint from a hex string

func (*OrderBuilder) WithSide

func (ob *OrderBuilder) WithSide(side OrderSide) *OrderBuilder

WithSide sets the Side

func (*OrderBuilder) WithWorstCasePrice

func (ob *OrderBuilder) WithWorstCasePrice(price FixedPoint) *OrderBuilder

WithWorstCasePrice sets the WorstCasePrice

type OrderSide

type OrderSide int

OrderSide is an enum for the side of an order

const (
	// Buy is the buy side of an order
	Buy OrderSide = iota
	// Sell is the sell side of an order
	Sell
)

func (*OrderSide) FromScalars

func (s *OrderSide) FromScalars(scalars *ScalarIterator) error

FromScalars converts a slice of scalars to an OrderSide

func (*OrderSide) NumScalars

func (s *OrderSide) NumScalars() int

NumScalars returns the number of scalars in the OrderSide

func (*OrderSide) ToScalars

func (s *OrderSide) ToScalars() ([]Scalar, error)

ToScalars converts an OrderSide to a slice of scalars

type PrivateKeychain

type PrivateKeychain struct {
	SkRoot       *PrivateSigningKey
	SkMatch      Scalar
	SymmetricKey HmacKey
}

PrivateKeychain is a private keychain for the API wallet

type PrivateSigningKey

type PrivateSigningKey ecdsa.PrivateKey

PrivateSigningKey is a private key over the secp256k1 curve

func (*PrivateSigningKey) FromHexString

func (pk *PrivateSigningKey) FromHexString(hexString string) (PrivateSigningKey, error)

FromHexString converts a hex string to a private key

func (*PrivateSigningKey) FromScalars

func (pk *PrivateSigningKey) FromScalars(scalars *ScalarIterator) error

FromScalars converts a slice of scalars to a private key

func (*PrivateSigningKey) NumScalars

func (pk *PrivateSigningKey) NumScalars() int

NumScalars returns the number of scalars in the private key

func (*PrivateSigningKey) ToHexString

func (pk *PrivateSigningKey) ToHexString() string

ToHexString converts the private key to a hex string

func (*PrivateSigningKey) ToScalars

func (pk *PrivateSigningKey) ToScalars() ([]Scalar, error)

ToScalars converts the private key to a slice of scalars

type PublicKeychain

type PublicKeychain struct {
	PkRoot  PublicSigningKey
	PkMatch Scalar
	Nonce   Scalar
}

PublicKeychain is a public keychain for the API wallet

type PublicSigningKey

type PublicSigningKey ecdsa.PublicKey

PublicSigningKey is a verification key over the secp256k1 curve

func (*PublicSigningKey) FromHexString

func (pk *PublicSigningKey) FromHexString(hexString string) (PublicSigningKey, error)

FromHexString converts a hex string to a public key

func (*PublicSigningKey) FromScalars

func (pk *PublicSigningKey) FromScalars(scalars *ScalarIterator) error

FromScalars converts a slice of scalars to a public key

func (*PublicSigningKey) NumScalars

func (pk *PublicSigningKey) NumScalars() int

NumScalars returns the number of scalars in the public key

func (*PublicSigningKey) ToHexString

func (pk *PublicSigningKey) ToHexString() string

ToHexString converts the public key to a hex string

func (*PublicSigningKey) ToScalars

func (pk *PublicSigningKey) ToScalars() ([]Scalar, error)

ToScalars converts the public key to a slice of scalars

type Scalar

type Scalar fr.Element

Scalar is a scalar field element from the bn254 curve

func DeriveWalletSeeds

func DeriveWalletSeeds(privateKey *ecdsa.PrivateKey, chainID uint64) (
	blinderSeed,
	shareSeed Scalar,
	err error,
)

DeriveWalletSeeds derives the blinder and secret share seeds from the derivation key

func HashScalars

func HashScalars(scalars []Scalar) Scalar

HashScalars hashes a slice of scalars using Poseidon2

func RandomScalar

func RandomScalar() (Scalar, error)

RandomScalar generates a random scalar

func ToScalarsRecursive

func ToScalarsRecursive(s interface{}) ([]Scalar, error)

ToScalarsRecursive is a helper function to serialize a value to a slice of scalars using reflection

func (*Scalar) Add

func (s *Scalar) Add(other Scalar) Scalar

Add adds two scalars

func (*Scalar) Bytes

func (s *Scalar) Bytes() [fr.Bytes]byte

Bytes returns the bytes representation of the scalar in big-endian order

func (*Scalar) FromBigInt

func (s *Scalar) FromBigInt(i *big.Int) Scalar

FromBigInt sets the scalar from a big.Int

func (*Scalar) FromBytes

func (s *Scalar) FromBytes(bytes [fr.Bytes]byte)

FromBytes sets the scalar from a big-endian byte slice

func (*Scalar) FromHexString

func (s *Scalar) FromHexString(hexString string) (Scalar, error)

FromHexString sets the scalar from a hex string

func (*Scalar) FromLittleEndianBytes

func (s *Scalar) FromLittleEndianBytes(bytes [fr.Bytes]byte) (*Scalar, error)

FromLittleEndianBytes sets the scalar from a little-endian byte slice

func (*Scalar) FromScalars

func (s *Scalar) FromScalars(scalars *ScalarIterator) error

FromScalars converts a `ScalarIterator` to

func (*Scalar) IsOne

func (s *Scalar) IsOne() bool

IsOne returns whether the scalar is one

func (*Scalar) IsZero

func (s *Scalar) IsZero() bool

IsZero returns whether the scalar is zero

func (*Scalar) LittleEndianBytes

func (s *Scalar) LittleEndianBytes() [fr.Bytes]byte

LittleEndianBytes returns the bytes representation of the scalar in little-endian order

func (*Scalar) NumScalars

func (s *Scalar) NumScalars() int

NumScalars returns the number of `Scalar`s in the `Scalar`

func (*Scalar) SetUint64

func (s *Scalar) SetUint64(val uint64) *Scalar

SetUint64 sets the scalar from a uint64

func (*Scalar) Sub

func (s *Scalar) Sub(other Scalar) Scalar

Sub subtracts two scalars

func (*Scalar) ToBigInt

func (s *Scalar) ToBigInt() *big.Int

ToBigInt converts the scalar to a big.Int

func (*Scalar) ToHexString

func (s *Scalar) ToHexString() string

ToHexString returns the hex string representation of the scalar

func (*Scalar) ToScalars

func (s *Scalar) ToScalars() ([]Scalar, error)

ToScalars converts a `Scalar` to a slice fo `Scalar`s

func (*Scalar) Uint64

func (s *Scalar) Uint64() uint64

Uint64 returns the scalar as a uint64

type ScalarIterator

type ScalarIterator struct {
	// contains filtered or unexported fields
}

ScalarIterator is a helper type that iterates over a slice of scalars

func NewScalarIterator

func NewScalarIterator(scalars []Scalar) *ScalarIterator

NewScalarIterator creates a new ScalarIterator

func (*ScalarIterator) Next

func (s *ScalarIterator) Next() (Scalar, error)

Next returns the next scalar in the iterator

func (*ScalarIterator) NumRemaining

func (s *ScalarIterator) NumRemaining() int

NumRemaining returns the remaining scalars in the iterator

type ScalarSerialize

type ScalarSerialize interface {
	// FromScalars deserializes a value from a slice of Scalars
	FromScalars(scalars *ScalarIterator) error
	// ToScalars serializes a value to a slice of Scalars
	ToScalars() ([]Scalar, error)
	// NumScalars returns the number of Scalars that will be serialized
	NumScalars() int
}

ScalarSerialize is an interface that can be implemented by any type that can be serialized to a slice of Scalars

type Uint64

type Uint64 uint64

Uint64 is a type that can be serialized to a slice of `Scalar`s

func (*Uint64) FromScalars

func (s *Uint64) FromScalars(scalars *ScalarIterator) error

FromScalars converts a `ScalarIterator` to a `Uint64`

func (*Uint64) NumScalars

func (s *Uint64) NumScalars() int

NumScalars returns the number of `Scalar`s in the `Uint64`

func (*Uint64) ToScalars

func (s *Uint64) ToScalars() ([]Scalar, error)

ToScalars converts a `Uint64` to a slice of `Scalar`s

type Wallet

type Wallet struct {
	Id                  uuid.UUID //nolint:revive
	Orders              []Order
	Balances            []Balance
	Keychain            *Keychain
	ManagingCluster     FeeEncryptionKey
	MatchFee            FixedPoint
	BlindedPublicShares WalletShare
	PrivateShares       WalletShare
	Blinder             Scalar
}

Wallet is a wallet in the Renegade system

func NewEmptyWallet

func NewEmptyWallet(privateKey *ecdsa.PrivateKey, chainID uint64) (*Wallet, error)

NewEmptyWallet creates a new empty wallet

func NewEmptyWalletFromSecrets

func NewEmptyWalletFromSecrets(secrets *WalletSecrets) (*Wallet, error)

NewEmptyWalletFromSecrets creates a new wallet from the given wallet secrets

func (*Wallet) AddBalance

func (w *Wallet) AddBalance(balance Balance) error

AddBalance appends a balance to the wallet

func (*Wallet) CancelOrder

func (w *Wallet) CancelOrder(orderID uuid.UUID) error

CancelOrder cancels an order by ID

func (*Wallet) GetBalance

func (w *Wallet) GetBalance(mint string) (*big.Int, error)

GetBalance gets the balance for a given mint

func (*Wallet) GetNonzeroBalances

func (w *Wallet) GetNonzeroBalances() []Balance

GetNonzeroBalances gets all non-zero balances in a wallet

func (*Wallet) GetNonzeroOrders

func (w *Wallet) GetNonzeroOrders() []Order

GetNonzeroOrders gets all non-empty orders

func (*Wallet) GetPrivateShareCommitment

func (w *Wallet) GetPrivateShareCommitment() (Scalar, error)

GetPrivateShareCommitment returns a Poseidon hash commitment of the wallet's private share

func (*Wallet) GetShareCommitment

func (w *Wallet) GetShareCommitment() (Scalar, error)

GetShareCommitment returns a Poseidon hash commitment of the wallet's shares

func (*Wallet) NewOrder

func (w *Wallet) NewOrder(order Order) error

NewOrder appends an order to the wallet

func (*Wallet) Reblind

func (w *Wallet) Reblind() error

Reblind reblinds the wallet, sampling new secret shares and blinders from the CSPRNGs

func (*Wallet) RemoveBalance

func (w *Wallet) RemoveBalance(balance Balance) error

RemoveBalance removes a balance from the wallet

func (*Wallet) SignCommitment

func (w *Wallet) SignCommitment(commitment Scalar) ([]byte, error)

SignCommitment signs the given commitment using the private root key

type WalletSecrets

type WalletSecrets struct {
	// Id is the UUID of the wallet
	Id uuid.UUID //nolint:revive
	// Address is the Ethereum address of the wallet
	Address string
	// Keychain is the keychain used to manage the wallet
	Keychain *Keychain
	// BlinderSeed is the seed of the CSPRNG used to generate blinders and blinder shares
	BlinderSeed Scalar
	// ShareSeed is the seed of the CSPRNG used to generate wallet secret shares
	ShareSeed Scalar
}

WalletSecrets contains the information about a wallet necessary to recover it

func DeriveWalletSecrets

func DeriveWalletSecrets(ethKey *ecdsa.PrivateKey, chainId uint64) (*WalletSecrets, error)

DeriveWalletSecrets derives the wallet secrets from the given Ethereum private key

type WalletShare

type WalletShare struct {
	// Balances are the balances of the wallet
	Balances [MaxBalances]Balance
	// Orders are the orders of the wallet
	Orders [MaxOrders]Order
	// Keys are the public keys of the wallet
	Keys PublicKeychain
	// MatchFee is the fee that the wallet pays to the cluster that matches its orders
	MatchFee FixedPoint
	// ManagingCluster is the public encryption key of the cluster that
	// receives fees for matching orders in the wallet
	ManagingCluster FeeEncryptionKey
	// Blinder is the additive blinder applied to all secret shares to make an adequately determined
	// algebraic system on the shares impossible, even when one knows the underlying value
	Blinder Scalar
}

WalletShare represents a secret share of a wallet, containing only the elements of a wallet that are stored on-chain

func CombineShares

func CombineShares(
	publicShare WalletShare,
	privateShare WalletShare,
	blinder Scalar,
) (WalletShare, error)

CombineShares combines two wallet shares into a single wallet share

func EmptyWalletShare

func EmptyWalletShare(publicKeys PublicKeychain) (WalletShare, error)

EmptyWalletShare creates a new wallet share with all zero values

func (*WalletShare) SplitPublicPrivate

func (ws *WalletShare) SplitPublicPrivate(
	privateShares []Scalar,
	blinder Scalar,
) (WalletShare, WalletShare, error)

SplitPublicPrivate splits a wallet share into two shares using the given private shares and blinder

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL