Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/ApiKey.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ Name | Type | Description | Notes
**api_key_index** | **int** | |
**nonce** | **int** | |
**public_key** | **str** | |
**key_type** | **int** | | [optional]
**transaction_time** | **int** | |

## Example
Expand Down
2 changes: 1 addition & 1 deletion lighter/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -208,4 +208,4 @@

# manual additions
from lighter.ws_client import WsClient
from lighter.signer_client import SignerClient, create_api_key
from lighter.signer_client import SignerClient, create_api_key, create_schnorr_api_key
4 changes: 3 additions & 1 deletion lighter/models/api_key.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,10 @@ class ApiKey(BaseModel):
api_key_index: StrictInt
nonce: StrictInt
public_key: StrictStr
key_type: Optional[StrictInt] = None
transaction_time: StrictInt
additional_properties: Dict[str, Any] = {}
__properties: ClassVar[List[str]] = ["account_index", "api_key_index", "nonce", "public_key", "transaction_time"]
__properties: ClassVar[List[str]] = ["account_index", "api_key_index", "nonce", "public_key", "key_type", "transaction_time"]

model_config = ConfigDict(
populate_by_name=True,
Expand Down Expand Up @@ -96,6 +97,7 @@ def from_dict(cls, obj: Optional[Dict[str, Any]]) -> Optional[Self]:
"api_key_index": obj.get("api_key_index"),
"nonce": obj.get("nonce"),
"public_key": obj.get("public_key"),
"key_type": obj.get("key_type"),
"transaction_time": obj.get("transaction_time")
})
# store additional fields in additional_properties
Expand Down
26 changes: 25 additions & 1 deletion lighter/signer_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,9 @@ def __populate_shared_library_functions(signer):
signer.GenerateAPIKey.argtypes = []
signer.GenerateAPIKey.restype = ApiKeyResponse

signer.GenerateSchnorrAPIKey.argtypes = []
signer.GenerateSchnorrAPIKey.restype = ApiKeyResponse

signer.CreateClient.argtypes = [ctypes.c_char_p, ctypes.c_char_p, ctypes.c_int, ctypes.c_int, ctypes.c_longlong]
signer.CreateClient.restype = ctypes.c_void_p

Expand All @@ -162,6 +165,9 @@ def __populate_shared_library_functions(signer):
signer.SignChangePubKey.argtypes = [ctypes.c_char_p, ctypes.c_uint8, ctypes.c_longlong, ctypes.c_int, ctypes.c_longlong]
signer.SignChangePubKey.restype = SignedTxResponse

signer.SignChangeECDSAPubKey.argtypes = [ctypes.c_char_p, ctypes.c_uint8, ctypes.c_longlong, ctypes.c_int, ctypes.c_longlong]
signer.SignChangeECDSAPubKey.restype = SignedTxResponse

signer.SignCreateOrder.argtypes = [ctypes.c_longlong, ctypes.c_longlong, ctypes.c_longlong, ctypes.c_int, ctypes.c_int, ctypes.c_int, ctypes.c_int,
ctypes.c_int, ctypes.c_int, ctypes.c_longlong, ctypes.c_longlong, ctypes.c_int, ctypes.c_int, ctypes.c_uint8, ctypes.c_uint8, ctypes.c_uint8, ctypes.c_longlong, ctypes.c_int, ctypes.c_longlong]
signer.SignCreateOrder.restype = SignedTxResponse
Expand Down Expand Up @@ -252,6 +258,8 @@ def get_signer():
return __signer


# secp256k1 ECDSA api key: 32-byte private key, 33-byte compressed public key. Register it with
# change_api_key; the signer picks the scheme from the key length.
def create_api_key():
result = lighter.signer_client.get_signer().GenerateAPIKey()
private_key_str = decode_and_free(result.privateKey)
Expand All @@ -260,6 +268,15 @@ def create_api_key():
return private_key_str, public_key_str, error


# Legacy Schnorr api key: 40-byte private and public keys.
def create_schnorr_api_key():
result = lighter.signer_client.get_signer().GenerateSchnorrAPIKey()
private_key_str = decode_and_free(result.privateKey)
public_key_str = decode_and_free(result.publicKey)
error = decode_and_free(result.err)
return private_key_str, public_key_str, error


def trim_exc(exception_body: str):
return exception_body.strip().split("\n")[-1]

Expand Down Expand Up @@ -505,6 +522,10 @@ def check_client(self):
def create_api_key(self):
return create_api_key()

@staticmethod
def create_schnorr_api_key(self):
return create_schnorr_api_key()

def create_auth_token_with_expiry(self, deadline: int = DEFAULT_10_MIN_AUTH_EXPIRY, *, timestamp: int = None, api_key_index: int = DEFAULT_API_KEY_INDEX):
if deadline == SignerClient.DEFAULT_10_MIN_AUTH_EXPIRY:
deadline = 10 * SignerClient.MINUTE
Expand All @@ -517,8 +538,11 @@ def create_auth_token_with_expiry(self, deadline: int = DEFAULT_10_MIN_AUTH_EXPI
error = decode_and_free(result.err)
return auth, error

# 33-byte (66 hex) public keys are ECDSA and register via SignChangeECDSAPubKey (tx type 54); 40-byte ones are Schnorr
def sign_change_api_key(self, eth_private_key: str, new_pubkey: str, skip_nonce: int = SKIP_NONCE_OFF, nonce: int = DEFAULT_NONCE, api_key_index: int = DEFAULT_API_KEY_INDEX) -> Union[Tuple[str, str, str, None], Tuple[None, None, None, str]]:
return self.__decode_and_sign_tx_info(eth_private_key, self.signer.SignChangePubKey(
is_ecdsa = len(new_pubkey[2:] if new_pubkey.startswith("0x") else new_pubkey) == 66
sign = self.signer.SignChangeECDSAPubKey if is_ecdsa else self.signer.SignChangePubKey
return self.__decode_and_sign_tx_info(eth_private_key, sign(
ctypes.c_char_p(new_pubkey.encode("utf-8")),
skip_nonce,
nonce,
Expand Down
Binary file modified lighter/signers/lighter-signer-darwin-amd64.dylib
Binary file not shown.
2 changes: 2 additions & 0 deletions lighter/signers/lighter-signer-darwin-amd64.h
Original file line number Diff line number Diff line change
Expand Up @@ -160,9 +160,11 @@ extern "C" {
#endif

extern ApiKeyResponse GenerateAPIKey(void);
extern ApiKeyResponse GenerateSchnorrAPIKey(void);
extern char* CreateClient(char* cUrl, char* cPrivateKey, int cChainId, int cApiKeyIndex, long long cAccountIndex);
extern char* CheckClient(int cApiKeyIndex, long long cAccountIndex);
extern SignedTxResponse SignChangePubKey(char* cPubKey, uint8_t cSkipNonce, long long cNonce, int cApiKeyIndex, long long cAccountIndex);
extern SignedTxResponse SignChangeECDSAPubKey(char* cPubKey, uint8_t cSkipNonce, long long cNonce, int cApiKeyIndex, long long cAccountIndex);
extern SignedTxResponse SignCreateOrder(long long cMarketIndex, long long cClientOrderIndex, long long cBaseAmount, int cPrice, int cIsAsk, int cOrderType, int cTimeInForce, int cReduceOnly, int cTriggerPrice, long long cOrderExpiry, long long cIntegratorAccountIndex, int cIntegratorTakerFee, int cIntegratorMakerFee, uint8_t cSelfTradeBehaviorMode, uint8_t cSelfTradeEqualityMode, uint8_t cSkipNonce, long long cNonce, int cApiKeyIndex, long long cAccountIndex);
extern SignedTxResponse SignCreateGroupedOrders(uint8_t cGroupingType, CreateOrderTxReq* cOrders, int cLen, long long cIntegratorAccountIndex, int cIntegratorTakerFee, int cIntegratorMakerFee, uint8_t cSelfTradeBehaviorMode, uint8_t cSelfTradeEqualityMode, uint8_t cSkipNonce, long long cNonce, int cApiKeyIndex, long long cAccountIndex);
extern SignedTxResponse SignCancelOrder(long long cMarketIndex, long long cOrderIndex, uint8_t cSkipNonce, long long cNonce, int cApiKeyIndex, long long cAccountIndex);
Expand Down
Binary file modified lighter/signers/lighter-signer-darwin-arm64.dylib
Binary file not shown.
2 changes: 2 additions & 0 deletions lighter/signers/lighter-signer-darwin-arm64.h
Original file line number Diff line number Diff line change
Expand Up @@ -160,9 +160,11 @@ extern "C" {
#endif

extern ApiKeyResponse GenerateAPIKey(void);
extern ApiKeyResponse GenerateSchnorrAPIKey(void);
extern char* CreateClient(char* cUrl, char* cPrivateKey, int cChainId, int cApiKeyIndex, long long cAccountIndex);
extern char* CheckClient(int cApiKeyIndex, long long cAccountIndex);
extern SignedTxResponse SignChangePubKey(char* cPubKey, uint8_t cSkipNonce, long long cNonce, int cApiKeyIndex, long long cAccountIndex);
extern SignedTxResponse SignChangeECDSAPubKey(char* cPubKey, uint8_t cSkipNonce, long long cNonce, int cApiKeyIndex, long long cAccountIndex);
extern SignedTxResponse SignCreateOrder(long long cMarketIndex, long long cClientOrderIndex, long long cBaseAmount, int cPrice, int cIsAsk, int cOrderType, int cTimeInForce, int cReduceOnly, int cTriggerPrice, long long cOrderExpiry, long long cIntegratorAccountIndex, int cIntegratorTakerFee, int cIntegratorMakerFee, uint8_t cSelfTradeBehaviorMode, uint8_t cSelfTradeEqualityMode, uint8_t cSkipNonce, long long cNonce, int cApiKeyIndex, long long cAccountIndex);
extern SignedTxResponse SignCreateGroupedOrders(uint8_t cGroupingType, CreateOrderTxReq* cOrders, int cLen, long long cIntegratorAccountIndex, int cIntegratorTakerFee, int cIntegratorMakerFee, uint8_t cSelfTradeBehaviorMode, uint8_t cSelfTradeEqualityMode, uint8_t cSkipNonce, long long cNonce, int cApiKeyIndex, long long cAccountIndex);
extern SignedTxResponse SignCancelOrder(long long cMarketIndex, long long cOrderIndex, uint8_t cSkipNonce, long long cNonce, int cApiKeyIndex, long long cAccountIndex);
Expand Down
2 changes: 2 additions & 0 deletions lighter/signers/lighter-signer-linux-amd64.h
Original file line number Diff line number Diff line change
Expand Up @@ -160,9 +160,11 @@ extern "C" {
#endif

extern ApiKeyResponse GenerateAPIKey(void);
extern ApiKeyResponse GenerateSchnorrAPIKey(void);
extern char* CreateClient(char* cUrl, char* cPrivateKey, int cChainId, int cApiKeyIndex, long long int cAccountIndex);
extern char* CheckClient(int cApiKeyIndex, long long int cAccountIndex);
extern SignedTxResponse SignChangePubKey(char* cPubKey, uint8_t cSkipNonce, long long int cNonce, int cApiKeyIndex, long long int cAccountIndex);
extern SignedTxResponse SignChangeECDSAPubKey(char* cPubKey, uint8_t cSkipNonce, long long int cNonce, int cApiKeyIndex, long long int cAccountIndex);
extern SignedTxResponse SignCreateOrder(long long int cMarketIndex, long long int cClientOrderIndex, long long int cBaseAmount, int cPrice, int cIsAsk, int cOrderType, int cTimeInForce, int cReduceOnly, int cTriggerPrice, long long int cOrderExpiry, long long int cIntegratorAccountIndex, int cIntegratorTakerFee, int cIntegratorMakerFee, uint8_t cSelfTradeBehaviorMode, uint8_t cSelfTradeEqualityMode, uint8_t cSkipNonce, long long int cNonce, int cApiKeyIndex, long long int cAccountIndex);
extern SignedTxResponse SignCreateGroupedOrders(uint8_t cGroupingType, CreateOrderTxReq* cOrders, int cLen, long long int cIntegratorAccountIndex, int cIntegratorTakerFee, int cIntegratorMakerFee, uint8_t cSelfTradeBehaviorMode, uint8_t cSelfTradeEqualityMode, uint8_t cSkipNonce, long long int cNonce, int cApiKeyIndex, long long int cAccountIndex);
extern SignedTxResponse SignCancelOrder(long long int cMarketIndex, long long int cOrderIndex, uint8_t cSkipNonce, long long int cNonce, int cApiKeyIndex, long long int cAccountIndex);
Expand Down
Binary file modified lighter/signers/lighter-signer-linux-amd64.so
Binary file not shown.
2 changes: 2 additions & 0 deletions lighter/signers/lighter-signer-linux-arm64.h
Original file line number Diff line number Diff line change
Expand Up @@ -160,9 +160,11 @@ extern "C" {
#endif

extern ApiKeyResponse GenerateAPIKey(void);
extern ApiKeyResponse GenerateSchnorrAPIKey(void);
extern char* CreateClient(char* cUrl, char* cPrivateKey, int cChainId, int cApiKeyIndex, long long int cAccountIndex);
extern char* CheckClient(int cApiKeyIndex, long long int cAccountIndex);
extern SignedTxResponse SignChangePubKey(char* cPubKey, uint8_t cSkipNonce, long long int cNonce, int cApiKeyIndex, long long int cAccountIndex);
extern SignedTxResponse SignChangeECDSAPubKey(char* cPubKey, uint8_t cSkipNonce, long long int cNonce, int cApiKeyIndex, long long int cAccountIndex);
extern SignedTxResponse SignCreateOrder(long long int cMarketIndex, long long int cClientOrderIndex, long long int cBaseAmount, int cPrice, int cIsAsk, int cOrderType, int cTimeInForce, int cReduceOnly, int cTriggerPrice, long long int cOrderExpiry, long long int cIntegratorAccountIndex, int cIntegratorTakerFee, int cIntegratorMakerFee, uint8_t cSelfTradeBehaviorMode, uint8_t cSelfTradeEqualityMode, uint8_t cSkipNonce, long long int cNonce, int cApiKeyIndex, long long int cAccountIndex);
extern SignedTxResponse SignCreateGroupedOrders(uint8_t cGroupingType, CreateOrderTxReq* cOrders, int cLen, long long int cIntegratorAccountIndex, int cIntegratorTakerFee, int cIntegratorMakerFee, uint8_t cSelfTradeBehaviorMode, uint8_t cSelfTradeEqualityMode, uint8_t cSkipNonce, long long int cNonce, int cApiKeyIndex, long long int cAccountIndex);
extern SignedTxResponse SignCancelOrder(long long int cMarketIndex, long long int cOrderIndex, uint8_t cSkipNonce, long long int cNonce, int cApiKeyIndex, long long int cAccountIndex);
Expand Down
Binary file modified lighter/signers/lighter-signer-linux-arm64.so
Binary file not shown.
Binary file modified lighter/signers/lighter-signer-windows-amd64.dll
Binary file not shown.
Loading