Issues with Signing and Validating the Signature in StarkEX Perpetual "StarkErrorCode.INVALID_SIGNATURE"

155 views Asked by At

I'm currently working with the StarkEX Perpetual Playground and trying to send a transfer transaction. I've followed the provided documentation, but when I send a curl request to the gateway, I'm consistently getting the following error:

curl -k -d '{
    "tx_id": 39,
    "tx": {
        "amount": "3",
        "asset_id": "0xa21edc9d9997b1b1956f542fe95922518a9e28ace11b7b2972a1974bf5971f",
        "expiration_timestamp": "20000000",
        "nonce": "0",
        "receiver_position_id": "4",
        "receiver_public_key": "0x63f62982fa598ad7c4e469870b3a1c23316ba8fb717aa2b1ee3114283fb1b82",
        "sender_position_id": "997",
        "sender_public_key": "0x63e08af5f1a5a70c118b5a47c4ae89ddad32f9d36626ad0f0cc296fd570fec4",
        "signature": {
             "r": "0x4f3106aa42f35b94a290349387fe1d7366e201a65a86314ccf80025375d9ed5",
            "s": "0x37b4705ac61f712d13196a260320433122016afc8c5a79c48ac7847db0cd68"
        },
        "type": "TRANSFER"
    }
}' https://perpetual-playground-v2.starkex.co/gateway/add_transaction; echo
{"code": "StarkErrorCode.INVALID_SIGNATURE", "message": "Transfer has an invalid signature."}

Here's how I'm signing and verifying the message in Python:

from services.perpetual.public.perpetual_messages import get_transfer_msg
from starkware.crypto.signature.signature import private_to_stark_key, sign
from starkware.crypto.signature.signature import *

# Define the transfer message data.

type = "Transfer"
asset_id = 286442224669982855773917167725901379555005478797788066723536016706544965407
asset_id_fee = 0
receiver_public_key = 2825868930652315540133093693348064822157481518754303749560050236804923333506
sender_position_id = 997
receiver_position_id = 4
src_fee_position_id = 7
nonce = 0
amount = 3
max_amount_fee = 0
expiration_timestamp = 20000000

# Calculate the message hash.
message_hash = get_transfer_msg(
    asset_id,
    asset_id_fee,
    receiver_public_key,
    sender_position_id,
    receiver_position_id,
    src_fee_position_id,
    nonce,
    amount,
    max_amount_fee,
    expiration_timestamp,
)

private_key=<private key>

# Sign the message hash.
r, s = sign(message_hash, private_key)

# verify the signature

verification_result = verify(
    msg_hash=message_hash,
    r=r,
    s=s,
    public_key=<public key>
)

if verification_result:
    print("Signature is valid.")
else:
    print("Signature is not valid.")

# Print the signature.
r_hex = hex(r)
s_hex = hex(s)

# Print the signature in hexadecimal.
print("Signature  (r, s):", r_hex, s_hex)

I've followed the documentation and guidelines provided by StarkWare but still can't figure out what's causing this error. I've tried multiple approaches, changing the Stark key and checking the message formatting. Still no luck.

Can someone provide insights into what might be causing this "StarkErrorCode.INVALID_SIGNATURE" error when interacting with the StarkEX Playground? Any help or suggestions would be greatly appreciated.

  • I tried signing message in python, then sent transfer transaction request to gateway.
  • I expected gateway to return transaction_received.
  • However, I'm encountering the "StarkErrorCode.INVALID_SIGNATURE" error when submitting the message. The gateway is not validating the signature, and I'm not sure why.
1

There are 1 answers

0
arpish R On

happy to report that issue was resolved. It turned out that the problem was related to a mismatch in the position ID.

If you encounter a similar problem, here are steps I took to resolve it:

  • Verify Ownership of the position ID, make sure your key pairs own that position id
  • Maybe in some cases, the src_fee_position_id may need to be the same as the position_id
  • Pay close attention to the order in which arguments are passed to the message hash generation function. Even a small change in the order or value will result in different hash values.