Documentation ¶
Overview ¶
starkex provides signing and other algorithms for starkex (https://starkware.co/starkex/).
Index ¶
- Constants
- Variables
- func FactToCondition(factRegistryAddress string, fact string) *big.Int
- func GenerateKRfc6979(msgHash, priKey *big.Int, seed int) *big.Int
- func GetTransferErc20Fact(recipient string, tokenDecimals int, humanAmount, tokenAddress, salt string) (string, error)
- func IntToHex32(x *big.Int) string
- func NonceByClientId(clientId string) *big.Int
- func OrderSign(starkPrivateKey string, param OrderSignParam) (string, error)
- func PedersenHash(str ...string) string
- func PrivateKeyToEcPointOnStarkCurv(priv_key *big.Int) (*big.Int, *big.Int, error)
- func SerializeSignature(r, s *big.Int) string
- func ToJsonString(input interface{}) string
- func TransferSign(starkPrivateKey string, param TransferSignParam) (string, error)
- func WithdrawSign(starkPrivateKey string, param WithdrawSignParam) (string, error)
- type Hash
- type OrderSignParam
- type OrderSigner
- type PedersenCfg
- type Signable
- type Signer
- func (s *Signer) SetNetworkId(networkId int) *Signer
- func (s *Signer) SetSigner(signer Signable) *Signer
- func (s *Signer) Sign() (string, error)
- func (s *Signer) SignOrder(param OrderSignParam) (string, error)
- func (s *Signer) SignTransfer(param TransferSignParam) (string, error)
- func (s *Signer) SignWithdraw(param WithdrawSignParam) (string, error)
- type TransferSignParam
- type TransferSigner
- type WithdrawSignParam
- type WithdrawSigner
Constants ¶
const ( MARKET_BTC_USD string = "BTC-USD" MARKET_ETH_USD string = "ETH-USD" MARKET_LINK_USD string = "LINK-USD" MARKET_AAVE_USD string = "AAVE-USD" MARKET_UNI_USD string = "UNI-USD" MARKET_SUSHI_USD string = "SUSHI-USD" MARKET_SOL_USD string = "SOL-USD" MARKET_YFI_USD string = "YFI-USD" MARKET_ONEINCH_USD string = "1INCH-USD" MARKET_AVAX_USD string = "AVAX-USD" MARKET_SNX_USD string = "SNX-USD" MARKET_CRV_USD string = "CRV-USD" MARKET_UMA_USD string = "UMA-USD" MARKET_DOT_USD string = "DOT-USD" MARKET_DOGE_USD string = "DOGE-USD" MARKET_MATIC_USD string = "MATIC-USD" MARKET_MKR_USD string = "MKR-USD" MARKET_FIL_USD string = "FIL-USD" MARKET_ADA_USD string = "ADA-USD" MARKET_ATOM_USD string = "ATOM-USD" MARKET_COMP_USD string = "COMP-USD" MARKET_BCH_USD string = "BCH-USD" MARKET_LTC_USD string = "LTC-USD" MARKET_EOS_USD string = "EOS-USD" MARKET_ALGO_USD string = "ALGO-USD" MARKET_ZRX_USD string = "ZRX-USD" MARKET_XMR_USD string = "XMR-USD" MARKET_ZEC_USD string = "ZEC-USD" MARKET_ENJ_USD string = "ENJ-USD" MARKET_ETC_USD string = "ETC-USD" MARKET_XLM_USD string = "XLM-USD" MARKET_TRX_USD string = "TRX-USD" MARKET_XTZ_USD string = "XTZ-USD" MARKET_HNT_USD string = "HNT-USD" MARKET_ICP_USD string = "ICP-USD" MARKET_RUNE_USD string = "RUNE-USD" MARKET_LUNA_USD string = "LUNA-USD" MARKET_NEAR_USD string = "NEAR-USD" MARKET_AR_USD string = "AR-USD" MARKET_FLOW_USD string = "FLOW-USD" MARKET_PERP_USD string = "PERP-USD" MARKET_REN_USD string = "REN-USD" MARKET_CELO_USD string = "CELO-USD" MARKET_KSM_USD string = "KSM-USD" MARKET_BAL_USD string = "BAL-USD" MARKET_BNT_USD string = "BNT-USD" MARKET_MIR_USD string = "MIR-USD" MARKET_SRM_USD string = "SRM-USD" MARKET_LON_USD string = "LON-USD" MARKET_DODO_USD string = "DODO-USD" MARKET_ALPHA_USD string = "ALPHA-USD" MARKET_WNXM_USD string = "WNXM-USD" MARKET_XCH_USD string = "XCH-USD" )
const ( ASSET_USDC string = "USDC" ASSET_BTC string = "BTC" ASSET_ETH string = "ETH" ASSET_LINK string = "LINK" ASSET_AAVE string = "AAVE" ASSET_UNI string = "UNI" ASSET_SUSHI string = "SUSHI" ASSET_SOL string = "SOL" ASSET_YFI string = "YFI" ASSET_ONEINCH string = "1INCH" ASSET_AVAX string = "AVAX" ASSET_SNX string = "SNX" ASSET_CRV string = "CRV" ASSET_UMA string = "UMA" ASSET_DOT string = "DOT" ASSET_DOGE string = "DOGE" ASSET_MATIC string = "MATIC" ASSET_MKR string = "MKR" ASSET_FIL string = "FIL" ASSET_ADA string = "ADA" ASSET_ATOM string = "ATOM" ASSET_COMP string = "COMP" ASSET_BCH string = "BCH" ASSET_LTC string = "LTC" ASSET_EOS string = "EOS" ASSET_ALGO string = "ALGO" ASSET_ZRX string = "ZRX" ASSET_XMR string = "XMR" ASSET_ZEC string = "ZEC" ASSET_ENJ string = "ENJ" ASSET_ETC string = "ETC" ASSET_XLM string = "XLM" ASSET_TRX string = "TRX" ASSET_XTZ string = "XTZ" ASSET_HNT string = "HNT" ASSET_ICP string = "ICP" ASSET_RUNE string = "RUNE" ASSET_LUNA string = "LUNA" ASSET_NEAR string = "NEAR" ASSET_AR string = "AR" ASSET_FLOW string = "FLOW" ASSET_PERP string = "PERP" ASSET_REN string = "REN" ASSET_CELO string = "CELO" ASSET_KSM string = "KSM" ASSET_BAL string = "BAL" ASSET_BNT string = "BNT" ASSET_MIR string = "MIR" ASSET_SRM string = "SRM" ASSET_LON string = "LON" ASSET_DODO string = "DODO" ASSET_ALPHA string = "ALPHA" ASSET_WNXM string = "WNXM" ASSET_XCH string = "XCH" COLLATERAL_ASSET string = ASSET_USDC )
const ( ORDER_PREFIX = 3 CONDITIONAL_TRANSFER_PREFIX = 4 WITHDRAWAL_PREFIX = 6 )
const ( ORDER_PADDING_BITS = 17 WITHDRAWAL_PADDING_BITS = 49 CONDITIONAL_TRANSFER_PADDING_BITS = 81 )
const ( CONDITIONAL_TRANSFER_FEE_ASSET_ID = 0 CONDITIONAL_TRANSFER_MAX_AMOUNT_FEE = 0 )
const ( NETWORK_ID_MAINNET = 1 NETWORK_ID_ROPSTEN = 3 )
const ( ONE_HOUR_IN_SECONDS = 60 * 60 ORDER_SIGNATURE_EXPIRATION_BUFFER_HOURS = 24 * 7 // Seven days. )
const ( ASSET_ID_MAINNET = "0x02893294412a4c8f915f75892b395ebbf6859ec246ec365c3b1f56f47c3a0a5d" ASSET_ID_ROPSTEN = "0x02c04d8b650f44092278a7cb1e1028c82025dff622db96c934b611b84cc8de5a" )
const COLLATERAL_TOKEN_DECIMALS = 6
const (
// HashLength is the expected length of the hash
HashLength = 32
)
Lengths of hashes and addresses in bytes.
const NONCE_UPPER_BOUND_EXCLUSIVE = 1 << 32 // 1 << ORDER_FIELD_BIT_LENGTHS['nonce']
Variables ¶
var ASSET_RESOLUTION = map[string]int64{ ASSET_USDC: 1e6, ASSET_BTC: 1e10, ASSET_ETH: 1e9, ASSET_LINK: 1e7, ASSET_AAVE: 1e8, ASSET_UNI: 1e7, ASSET_SUSHI: 1e7, ASSET_SOL: 1e7, ASSET_YFI: 1e10, ASSET_ONEINCH: 1e7, ASSET_AVAX: 1e7, ASSET_SNX: 1e7, ASSET_CRV: 1e6, ASSET_UMA: 1e7, ASSET_DOT: 1e7, ASSET_DOGE: 1e5, ASSET_MATIC: 1e6, ASSET_MKR: 1e9, ASSET_FIL: 1e7, ASSET_ADA: 1e6, ASSET_ATOM: 1e7, ASSET_COMP: 1e8, ASSET_BCH: 1e8, ASSET_LTC: 1e8, ASSET_EOS: 1e6, ASSET_ALGO: 1e6, ASSET_ZRX: 1e6, ASSET_XMR: 1e8, ASSET_ZEC: 1e8, ASSET_ENJ: 1e6, ASSET_ETC: 1e7, ASSET_XLM: 1e5, ASSET_TRX: 1e4, ASSET_XTZ: 1e6, ASSET_HNT: 1e7, ASSET_ICP: 1e7, ASSET_RUNE: 1e6, ASSET_LUNA: 1e6, ASSET_NEAR: 1e6, ASSET_AR: 1e7, ASSET_FLOW: 1e7, ASSET_PERP: 1e6, ASSET_REN: 1e5, ASSET_CELO: 1e6, ASSET_KSM: 1e8, ASSET_BAL: 1e7, ASSET_BNT: 1e6, ASSET_MIR: 1e6, ASSET_SRM: 1e6, ASSET_LON: 1e6, ASSET_DODO: 1e6, ASSET_ALPHA: 1e5, ASSET_WNXM: 1e7, ASSET_XCH: 1e8, }
------------ Asset Resolution (Quantum Size) ------------
The asset resolution is the number of quantums (Starkware units) that fit within one "human-readable" unit of the asset. For example, if the asset resolution for BTC is 1e10, then the smallest unit representable within Starkware is 1e-10 BTC, i.e. 1/100th of a satoshi.
For the collateral asset (USDC), the chosen resolution corresponds to the base units of the ERC-20 token. For the other, synthetic, assets, the resolutions are chosen such that prices relative to USDC are close to one.
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
var COLLATERAL_ASSET_ID_BY_NETWORK_ID = map[int]*big.Int{ NETWORK_ID_MAINNET: mainNet, NETWORK_ID_ROPSTEN: ropstenNet, }
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,
}
var FACT_REGISTRY_CONTRACT = map[int]string{ NETWORK_ID_MAINNET: "0xBE9a129909EbCb954bC065536D2bfAfBd170d27A", NETWORK_ID_ROPSTEN: "0x8Fb814935f7E63DEB304B500180e19dF5167B50e", }
var N_ELEMENT_BITS_ECDSA = big.NewInt(251)
N_ELEMENT_BITS_ECDSA math.floor(math.log(FIELD_PRIME, 2))
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,
}
var SYNTHETIC_ASSET_ID_MAP = map[string]string{ ASSET_BTC: "0x4254432d3130000000000000000000", ASSET_ETH: "0x4554482d3900000000000000000000", ASSET_LINK: "0x4c494e4b2d37000000000000000000", ASSET_AAVE: "0x414156452d38000000000000000000", ASSET_UNI: "0x554e492d3700000000000000000000", ASSET_SUSHI: "0x53555348492d370000000000000000", ASSET_SOL: "0x534f4c2d3700000000000000000000", ASSET_YFI: "0x5946492d3130000000000000000000", ASSET_ONEINCH: "0x31494e43482d370000000000000000", ASSET_AVAX: "0x415641582d37000000000000000000", ASSET_SNX: "0x534e582d3700000000000000000000", ASSET_CRV: "0x4352562d3600000000000000000000", ASSET_UMA: "0x554d412d3700000000000000000000", ASSET_DOT: "0x444f542d3700000000000000000000", ASSET_DOGE: "0x444f47452d35000000000000000000", ASSET_MATIC: "0x4d415449432d360000000000000000", ASSET_MKR: "0x4d4b522d3900000000000000000000", ASSET_FIL: "0x46494c2d3700000000000000000000", ASSET_ADA: "0x4144412d3600000000000000000000", ASSET_ATOM: "0x41544f4d2d37000000000000000000", ASSET_COMP: "0x434f4d502d38000000000000000000", ASSET_BCH: "0x4243482d3800000000000000000000", ASSET_LTC: "0x4c54432d3800000000000000000000", ASSET_EOS: "0x454f532d3600000000000000000000", ASSET_ALGO: "0x414c474f2d36000000000000000000", ASSET_ZRX: "0x5a52582d3600000000000000000000", ASSET_XMR: "0x584d522d3800000000000000000000", ASSET_ZEC: "0x5a45432d3800000000000000000000", ASSET_ENJ: "0x454e4a2d3600000000000000000000", ASSET_ETC: "0x4554432d3700000000000000000000", ASSET_XLM: "0x584c4d2d3500000000000000000000", ASSET_TRX: "0x5452582d3400000000000000000000", ASSET_XTZ: "0x58545a2d3600000000000000000000", ASSET_HNT: "0x484e542d3700000000000000000000", ASSET_ICP: "0x4943502d3700000000000000000000", ASSET_RUNE: "0x52554e452d36000000000000000000", ASSET_LUNA: "0x4c554e412d36000000000000000000", ASSET_NEAR: "0x4e4541522d36000000000000000000", ASSET_AR: "0x41522d370000000000000000000000", ASSET_FLOW: "0x464c4f572d37000000000000000000", ASSET_PERP: "0x504552502d36000000000000000000", ASSET_REN: "0x52454e2d3500000000000000000000", ASSET_CELO: "0x43454c4f2d36000000000000000000", ASSET_KSM: "0x4b534d2d3800000000000000000000", ASSET_BAL: "0x42414c2d3700000000000000000000", ASSET_BNT: "0x424e542d3600000000000000000000", ASSET_MIR: "0x4d49522d3600000000000000000000", ASSET_SRM: "0x53524d2d3600000000000000000000", ASSET_LON: "0x4c4f4e2d3600000000000000000000", ASSET_DODO: "0x444f444f2d36000000000000000000", ASSET_ALPHA: "0x414c5048412d350000000000000000", ASSET_WNXM: "0x574e584d2d37000000000000000000", ASSET_XCH: "0x5843482d3800000000000000000000", }
var SYNTHETIC_ASSET_MAP = map[string]string{ MARKET_BTC_USD: ASSET_BTC, MARKET_ETH_USD: ASSET_ETH, MARKET_LINK_USD: ASSET_LINK, MARKET_AAVE_USD: ASSET_AAVE, MARKET_UNI_USD: ASSET_UNI, MARKET_SUSHI_USD: ASSET_SUSHI, MARKET_SOL_USD: ASSET_SOL, MARKET_YFI_USD: ASSET_YFI, MARKET_ONEINCH_USD: ASSET_ONEINCH, MARKET_AVAX_USD: ASSET_AVAX, MARKET_SNX_USD: ASSET_SNX, MARKET_CRV_USD: ASSET_CRV, MARKET_UMA_USD: ASSET_UMA, MARKET_DOT_USD: ASSET_DOT, MARKET_DOGE_USD: ASSET_DOGE, MARKET_MATIC_USD: ASSET_MATIC, MARKET_MKR_USD: ASSET_MKR, MARKET_FIL_USD: ASSET_FIL, MARKET_ADA_USD: ASSET_ADA, MARKET_ATOM_USD: ASSET_ATOM, MARKET_COMP_USD: ASSET_COMP, MARKET_BCH_USD: ASSET_BCH, MARKET_LTC_USD: ASSET_LTC, MARKET_EOS_USD: ASSET_EOS, MARKET_ALGO_USD: ASSET_ALGO, MARKET_ZRX_USD: ASSET_ZRX, MARKET_XMR_USD: ASSET_XMR, MARKET_ZEC_USD: ASSET_ZEC, MARKET_ENJ_USD: ASSET_ENJ, MARKET_ETC_USD: ASSET_ETC, MARKET_XLM_USD: ASSET_XLM, MARKET_TRX_USD: ASSET_TRX, MARKET_XTZ_USD: ASSET_XTZ, MARKET_HNT_USD: ASSET_HNT, MARKET_ICP_USD: ASSET_ICP, MARKET_RUNE_USD: ASSET_RUNE, MARKET_LUNA_USD: ASSET_LUNA, MARKET_NEAR_USD: ASSET_NEAR, MARKET_AR_USD: ASSET_AR, MARKET_FLOW_USD: ASSET_FLOW, MARKET_PERP_USD: ASSET_PERP, MARKET_REN_USD: ASSET_REN, MARKET_CELO_USD: ASSET_CELO, MARKET_KSM_USD: ASSET_KSM, MARKET_BAL_USD: ASSET_BAL, MARKET_BNT_USD: ASSET_BNT, MARKET_MIR_USD: ASSET_MIR, MARKET_SRM_USD: ASSET_SRM, MARKET_LON_USD: ASSET_LON, MARKET_DODO_USD: ASSET_DODO, MARKET_ALPHA_USD: ASSET_ALPHA, MARKET_WNXM_USD: ASSET_WNXM, MARKET_XCH_USD: ASSET_XCH, }
var SYNTHETIC_ID_MAP = SYNTHETIC_ASSET_ID_MAP
var TOKEN_CONTRACTS = map[string]map[int]string{ COLLATERAL_ASSET: { NETWORK_ID_MAINNET: "0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48", NETWORK_ID_ROPSTEN: "0x8707A5bf4C2842d46B31A405Ba41b858C0F876c4", }, }
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 ¶
FactToCondition Generate the condition, signed as part of a conditional transfer.
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 ¶
IntToHex32 Normalize to a 32-byte hex string without 0x prefix.
func NonceByClientId ¶
NonceByClientId generate nonce by clientId
func PedersenHash ¶
func SerializeSignature ¶
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 Hash ¶
type Hash [HashLength]byte
Hash represents the 32 byte Keccak256 hash of arbitrary data.
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 Signer ¶
type Signer struct { NetworkId int // contains filtered or unexported fields }
func (*Signer) SetNetworkId ¶
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 WithdrawSigner ¶
type WithdrawSigner struct {
// contains filtered or unexported fields
}