starkex

package module
v0.0.0-...-aee2624 Latest Latest
Warning

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

Go to latest
Published: Jun 3, 2023 License: MIT Imports: 18 Imported by: 0

README

stark key authentication library, signature generator for dydx exchange

for the following operations:

  • Place an order
  • Withdraw funds

link : https://docs.dydx.exchange/#authentication

Installation
go get github.com/yanue/starkex
demos
order sign demo
    const MOCK_PRIVATE_KEY = "58c7d5a90b1776bde86ebac077e053ed85b0f7164f53b080304a531947f46e3"
    param := OrderSignParam{
		NetworkId:  NETWORK_ID_ROPSTEN,
		Market:     "ETH-USD",
		Side:       "BUY",
		PositionId: 12345,
		HumanSize:  "145.0005",
		HumanPrice: "350.00067",
		LimitFee:   "0.125",
		ClientId:   "This is an ID that the client came up with to describe this order",
		Expiration: "2020-09-17T04:15:55.028Z",
	}
	sign, err := OrderSign(MOCK_PRIVATE_KEY, param)
	// 00cecbe513ecdbf782cd02b2a5efb03e58d5f63d15f2b840e9bc0029af04e8dd0090b822b16f50b2120e4ea9852b340f7936ff6069d02acca02f2ed03029ace5
	fmt.Println("sign,err", sign, err)
withdraw sign demo
    const MOCK_PRIVATE_KEY = "58c7d5a90b1776bde86ebac077e053ed85b0f7164f53b080304a531947f46e3"
    param := WithdrawSignParam{
		NetworkId:   NETWORK_ID_ROPSTEN,
		PositionId:  12345,
		HumanAmount: "49.478023",
		ClientId:    "This is an ID that the client came up with to describe this withdrawal",
		Expiration:  "2020-09-17T04:15:55.028Z",
	}
	sign, err := WithdrawSign(MOCK_PRIVATE_KEY, param)
	// 05e48c33f8205a5359c95f1bd7385c1c1f587e338a514298c07634c0b6c952ba0687d6980502a5d7fa84ef6fdc00104db22c43c7fb83e88ca84f19faa9ee3de1
	fmt.Println("sign,err", sign, err)
transfer sign demo (fast_withdraw)
    const MOCK_PRIVATE_KEY = "58c7d5a90b1776bde86ebac077e053ed85b0f7164f53b080304a531947f46e3"
    param := TransferSignParam{
		NetworkId:          NETWORK_ID_MAINNET,
		CreditAmount:       "1",
		DebitAmount:        "2",
		SenderPositionId:   12345,
		ReceiverPositionId: 67890,
		ReceiverPublicKey:  "04a9ecd28a67407c3cff8937f329ca24fd631b1d9ca2b9f2df47c7ebf72bf0b0",
		ReceiverAddress:    "0x1234567890123456789012345678901234567890",
		Expiration:         "2020-09-17T04:15:55.028Z",
		ClientId:           "This is an ID that the client came up with to describe this transfer",
	}
	sign, err := TransferSign(MOCK_PRIVATE_KEY, param)
	// 0278aeb361938d4c377950487bb770fc9464bf5352e19117c03243efad4e10a302bb3983e05676c7952caa4acdc1a83426d5c8cb8c56d7f6c477cfdafd37718a
	fmt.Println("sign,err", sign, err)
inspired by

https://github.com/dydxprotocol/dydx-v3-python

https://github.com/starkware-libs/starkex-resources

Documentation

Index

Constants

View Source
const ASSET_ID_GOERLI = "0x03bda2b4764039f2df44a00a9cf1d1569a83f95406a983ce4beb95791c376008"
View Source
const ASSET_ID_MAINNET = "0x02893294412a4c8f915f75892b395ebbf6859ec246ec365c3b1f56f47c3a0a5d"
View Source
const ASSET_ID_ROPSTEN = "0x02c04d8b650f44092278a7cb1e1028c82025dff622db96c934b611b84cc8de5a"
View Source
const COLLATERAL_ASSET = "USDC"
View Source
const COLLATERAL_TOKEN_DECIMALS = 6
View Source
const CONDITIONAL_TRANSFER_FEE_ASSET_ID = 0
View Source
const CONDITIONAL_TRANSFER_MAX_AMOUNT_FEE = 0
View Source
const CONDITIONAL_TRANSFER_PADDING_BITS = 81
View Source
const CONDITIONAL_TRANSFER_PREFIX = 5
View Source
const NETWORK_ID_GOERLI = 5
View Source
const NETWORK_ID_MAINNET = 1
View Source
const NETWORK_ID_ROPSTEN = 3
View Source
const NONCE_UPPER_BOUND_EXCLUSIVE = 1 << 32 // 1 << ORDER_FIELD_BIT_LENGTHS['nonce']
View Source
const ONE_HOUR_IN_SECONDS = 60 * 60
View Source
const ORDER_PADDING_BITS = 17
View Source
const ORDER_PREFIX = 3
View Source
const ORDER_SIGNATURE_EXPIRATION_BUFFER_HOURS = 24 * 7 // Seven days.
View Source
const WITHDRAWAL_PADDING_BITS = 49
View Source
const WITHDRAWAL_PREFIX = 6

Variables

View Source
var ASSET_RESOLUTION = map[string]int64{
	"USDC":  1e6,
	"BTC":   1e10,
	"ETH":   1e9,
	"LINK":  1e7,
	"AAVE":  1e8,
	"UNI":   1e7,
	"SUSHI": 1e7,
	"SOL":   1e7,
	"YFI":   1e10,
	"1INCH": 1e7,
	"AVAX":  1e7,
	"SNX":   1e7,
	"CRV":   1e6,
	"UMA":   1e7,
	"DOT":   1e7,
	"DOGE":  1e5,
	"MATIC": 1e6,
	"MKR":   1e9,
	"FIL":   1e7,
	"ADA":   1e6,
	"ATOM":  1e7,
	"COMP":  1e8,
	"BCH":   1e8,
	"LTC":   1e8,
	"EOS":   1e6,
	"ALGO":  1e6,
	"ZRX":   1e6,
	"XMR":   1e8,
	"ZEC":   1e8,
	"ENJ":   1e6,
	"ETC":   1e7,
	"XLM":   1e5,
	"TRX":   1e4,
	"XTZ":   1e6,
	"ICP":   1e7,
	"RUNE":  1e6,
	"LUNA":  1e6,
	"NEAR":  1e6,
	"CELO":  1e6,
}
View Source
var BIT_MASK_250 = big.NewInt(0).Sub(big.NewInt(0).Exp(big.NewInt(2), big.NewInt(250), nil), one)

BIT_MASK_250 (2 ** 250) - 1

View Source
var COLLATERAL_ASSET_ID_BY_NETWORK_ID = map[int]*big.Int{
	NETWORK_ID_MAINNET: mainNet,
	NETWORK_ID_ROPSTEN: ropstenNet,
	NETWORK_ID_GOERLI:  goerliNet,
}
View Source
var CONDITIONAL_TRANSFER_FIELD_BIT_LENGTHS = map[string]uint{
	"asset_id":               250,
	"receiver_public_key":    251,
	"position_id":            64,
	"condition":              251,
	"quantums_amount":        64,
	"nonce":                  32,
	"expiration_epoch_hours": 32,
}
View Source
var EC_ORDER = new(big.Int)
View Source
var FACT_REGISTRY_CONTRACT = map[int]string{
	NETWORK_ID_MAINNET: "0xBE9a129909EbCb954bC065536D2bfAfBd170d27A",
	NETWORK_ID_ROPSTEN: "0x8Fb814935f7E63DEB304B500180e19dF5167B50e",
	NETWORK_ID_GOERLI:  "0xc5061C08cF892C79DDB106B777138982433C8865",
}
View Source
var FIELD_PRIME = new(big.Int)
View Source
var N_ELEMENT_BITS_ECDSA = big.NewInt(251)

N_ELEMENT_BITS_ECDSA math.floor(math.log(FIELD_PRIME, 2))

View Source
var ORDER_FIELD_BIT_LENGTHS = map[string]uint{
	"asset_id_synthetic":     128,
	"asset_id_collateral":    250,
	"asset_id_fee":           250,
	"quantums_amount":        64,
	"nonce":                  32,
	"position_id":            64,
	"expiration_epoch_hours": 32,
}
View Source
var SYNTHETIC_ID_MAP = map[string]string{
	"BTC":   "344400637343183300222065759427231744",
	"ETH":   "359977924069045087395084506562035712",
	"LINK":  "396101380212426712967694464499843072",
	"AAVE":  "338824487460109173243580632516263936",
	"UNI":   "442933058573725082054137099516379136",
	"SUSHI": "432691242383575582125453041830199296",
	"SOL":   "432568984950746621260512834741075968",
	"YFI":   "463539986724008297878357474744991744",
	"1INCH": "255909362588701076462300859102920704",
	"AVAX":  "339248760150583523010467496495939584",
	"SNX":   "432549653279093140762140010017193984",
	"CRV":   "349553874723416551039058041492209664",
	"UMA":   "442912142338821316269012403913490432",
	"DOT":   "354685165898024320947256138154377216",
	"DOGE":  "354684143347712898450594870927032320",
	"MATIC": "401131892570687533667041932340101120",
	"MKR":   "401334549532515986032888887373725696",
	"FIL":   "364947431332171952067072698953498624",
	"ADA":   "338883663480482972844972322892808192",
	"ATOM":  "339209301121316311335550155636080640",
	"COMP":  "349492325262502431542334300769746944",
	"BCH":   "344056232528969600292671285127806976",
	"LTC":   "396323605936767383653495383972118528",
	"EOS":   "359877383527187708491834411764940800",
	"ALGO":  "339046408638152281133665097078538240",
	"ZRX":   "468976860926042615023934644152172544",
	"XMR":   "458490379794397467467957612323012608",
	"ZEC":   "468711525812200195396563123811909632",
	"ENJ":   "359856388064121428442372122618101760",
	"ETC":   "359977527925814664434167280492871680",
	"XLM":   "458469701240354467017001809827856384",
	"TRX":   "437823079772415797613522407827439616",
	"XTZ":   "458632990484505291636396733084205056",
	"ICP":   "380402944362804581987470878609244160",
	"RUNE":  "427498548421964717489411527496695808",
	"LUNA":  "396344766032815712432848274622251008",
	"NEAR":  "406403816491359422489603563510038528",
	"CELO":  "349289421628808946019446426697728000",
}
View Source
var TOKEN_CONTRACTS = map[string]map[int]string{
	COLLATERAL_ASSET: {
		NETWORK_ID_MAINNET: "0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48",
		NETWORK_ID_ROPSTEN: "0x8707A5bf4C2842d46B31A405Ba41b858C0F876c4",
		NETWORK_ID_GOERLI:  "0xF7a2fa2c2025fFe64427dd40Dc190d47ecC8B36e",
	},
}
View Source
var WITHDRAWAL_FIELD_BIT_LENGTHS = map[string]uint{
	"asset_id":               250,
	"position_id":            64,
	"nonce":                  32,
	"quantums_amount":        64,
	"expiration_epoch_hours": 32,
}

Functions

func FactToCondition

func FactToCondition(factRegistryAddress string, fact string) *big.Int

FactToCondition Generate the condition, signed as part of a conditional transfer.

func GenerateKRfc6979

func GenerateKRfc6979(msgHash, priKey *big.Int, seed int) *big.Int

func GetTransferErc20Fact

func GetTransferErc20Fact(recipient string, tokenDecimals int, humanAmount, tokenAddress, salt string) (string, error)

GetTransferErc20Fact get erc20 fact tokenDecimals is COLLATERAL_TOKEN_DECIMALS

func IntToHex32

func IntToHex32(x *big.Int) string

IntToHex32 Normalize to a 32-byte hex string without 0x prefix.

func NonceByClientId

func NonceByClientId(clientId string) *big.Int

NonceByClientId generate nonce by clientId

func OrderSign

func OrderSign(starkPrivateKey string, param OrderSignParam) (string, error)

func PedersenHash

func PedersenHash(str ...string) string

func SerializeSignature

func SerializeSignature(r, s *big.Int) string

SerializeSignature Convert a Sign from an r, s pair to a 32-byte hex string.

func ToJsonString

func ToJsonString(input interface{}) string

func TransferSign

func TransferSign(starkPrivateKey string, param TransferSignParam) (string, error)

func WithdrawSign

func WithdrawSign(starkPrivateKey string, param WithdrawSignParam) (string, error)

Types

type OrderSignParam

type OrderSignParam struct {
	NetworkId  int    `json:"network_id"` // 1 MAINNET 3 ROPSTEN
	PositionId int64  `json:"position_id"`
	Market     string `json:"market"`
	Side       string `json:"side"`
	HumanSize  string `json:"human_size"`
	HumanPrice string `json:"human_price"`
	LimitFee   string `json:"limit_fee"`
	ClientId   string `json:"clientId"`
	Expiration string `json:"expiration"` // 2006-01-02T15:04:05.000Z
}

type OrderSigner

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

type PedersenCfg

type PedersenCfg struct {
	Comment        string        `json:"_comment"`
	FieldPrime     *big.Int      `json:"FIELD_PRIME"`
	FieldGen       int           `json:"FIELD_GEN"`
	EcOrder        *big.Int      `json:"EC_ORDER"`
	ALPHA          int           `json:"ALPHA"`
	BETA           *big.Int      `json:"BETA"`
	ConstantPoints [][2]*big.Int `json:"CONSTANT_POINTS"`
}

type Signable

type Signable interface {
	// contains filtered or unexported methods
}

type Signer

type Signer struct {
	NetworkId int
	// contains filtered or unexported fields
}

func NewSigner

func NewSigner(starkPrivateKey string) *Signer

func (*Signer) SetNetworkId

func (s *Signer) SetNetworkId(networkId int) *Signer

func (*Signer) SetSigner

func (s *Signer) SetSigner(signer Signable) *Signer

func (*Signer) Sign

func (s *Signer) Sign() (string, error)

func (*Signer) SignOrder

func (s *Signer) SignOrder(param OrderSignParam) (string, error)

func (*Signer) SignTransfer

func (s *Signer) SignTransfer(param TransferSignParam) (string, error)

func (*Signer) SignWithdraw

func (s *Signer) SignWithdraw(param WithdrawSignParam) (string, error)

type TransferSignParam

type TransferSignParam struct {
	NetworkId          int    `json:"network_id"` // 1 MAINNET 3 ROPSTEN
	SenderPositionId   int64  `json:"sender_position_id"`
	ReceiverPositionId int64  `json:"receiver_position_id"`
	ReceiverPublicKey  string `json:"receiver_public_key"`
	ReceiverAddress    string `json:"receiver_address"`
	CreditAmount       string `json:"credit_amount"`
	DebitAmount        string `json:"debit_amount"`
	Expiration         string `json:"expiration"`
	ClientId           string `json:"client_id"`
}

type TransferSigner

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

type WithdrawSignParam

type WithdrawSignParam struct {
	NetworkId   int    `json:"network_id"` // 1 MAINNET 3 ROPSTEN
	PositionId  int64  `json:"position_id"`
	HumanAmount string `json:"human_amount"`
	ClientId    string `json:"clientId"`
	Expiration  string `json:"expiration"` // 2006-01-02T15:04:05.000Z
}

type WithdrawSigner

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

Jump to

Keyboard shortcuts

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