Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
fbeutin-ledger committed Aug 5, 2024
1 parent 88730bd commit 265f9ef
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 28 deletions.
53 changes: 30 additions & 23 deletions test/python/apps/polkadot.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ class SignP2Last:


class PolkadotClient:
CLA = 0x90
CLA = 0xF9
def __init__(self, client):
self._client = client

Expand All @@ -92,19 +92,14 @@ def client(self):
return self._client

def get_pubkey(self):
msg = self.client.exchange(self.CLA, ins=Command.GET_ADDRESS, p1=0, p2=0, data=DOT_PACKED_DERIVATION_PATH_SIGN_INIT)
data = DOT_PACKED_DERIVATION_PATH_SIGN_INIT + bytes([0x00, 0x00])
msg = self.client.exchange(self.CLA, ins=Command.GET_ADDRESS, p1=0, p2=0, data=data)
return msg.data[:32].hex().encode()

def sign_init(self):
return self.client.exchange(self.CLA, ins=Command.SIGN_TX, p1=SignP1.INIT, data=DOT_PACKED_DERIVATION_PATH_SIGN_INIT)
def sign_add(self, tx_chunk):
return self.client.exchange(self.CLA, ins=Command.SIGN_TX, p1=SignP1.ADD, data=tx_chunk)

def sign_add(self,tx_chunk):
return self.client.exchange(self.CLA, ins=Command.SIGN_TX, p1=SignP1.ADD,data=tx_chunk)

def sign_last(self,tx_chunk):
return self.client.exchange(self.CLA, ins=Command.SIGN_TX, p1=SignP1.LAST, p2=SignP2Last.ED25519, data=tx_chunk)

def verify_signature(self,hex_key:bytes,signature:bytes,message:bytes) -> bool :
def verify_signature(self, hex_key: bytes, signature: bytes, message: bytes) -> bool :
# Create a VerifyKey object from a hex serialized public key
verify_key = VerifyKey(hex_key, encoder=HexEncoder)
# Check the validity of a message's signature
Expand All @@ -122,18 +117,7 @@ def verify_signature(self,hex_key:bytes,signature:bytes,message:bytes) -> bool :
print("Signature is ok.")
return True

def craft_valid_polkadot_transaction(address, send_amount, fees, memo) -> bytes:
return Method.BALANCE_TRANSFER_ALLOW_DEATH.to_bytes(2, "big") \
+ AccountIdLookupType.ID.to_bytes(1, "big") \
+ _polkadot_address_to_pk(address) \
+ _format_amount(send_amount) \
+ UNKNOWN \
+ SPEC_VERSION.to_bytes(4, "little") \
+ TX_VERSION.to_bytes(4, "little") \
+ GENESIS_HASH \
+ BLOCK_HASH

def craft_invalid_polkadot_transaction(address, send_amount, fees, memo) -> bytes:
def craft_invalid_polkadot_transaction(self, address, send_amount) -> bytes:
force_transfer = Method.BALANCE_FORCE_TRANSFER.to_bytes(2, "big") \
+ bytes([0x00, 0xdc, 0x5a, 0xda, 0x10, 0xee, 0xdd, 0x89, 0x81, 0x92,
0x78, 0xb0, 0x92, 0x35, 0x87, 0x80, 0x3d, 0x7d, 0xb2, 0x07,
Expand All @@ -149,3 +133,26 @@ def craft_invalid_polkadot_transaction(address, send_amount, fees, memo) -> byte
+ TX_VERSION.to_bytes(4, "little") \
+ GENESIS_HASH \
+ BLOCK_HASH

def perform_polkadot_transaction(self, address, send_amount) -> bytes:
# Init signature process and assert response APDU code is 0x9000 (OK).
self.client.exchange(self.CLA, ins=Command.SIGN_TX, p1=SignP1.INIT, data=DOT_PACKED_DERIVATION_PATH_SIGN_INIT + bytes([0x00, 0x00]))

# craft tx
tx_blob = Method.BALANCE_TRANSFER_ALLOW_DEATH.to_bytes(2, "big") \
+ AccountIdLookupType.ID.to_bytes(1, "big") \
+ _polkadot_address_to_pk(address) \
+ _format_amount(send_amount) \
+ UNKNOWN \
+ SPEC_VERSION.to_bytes(4, "little") \
+ TX_VERSION.to_bytes(4, "little") \
+ GENESIS_HASH \
+ BLOCK_HASH

message = DOT_PACKED_DERIVATION_PATH_SIGN_INIT
message += len(tx_blob).to_bytes(2, "little")
message += tx_blob

# Send message to be signed
return self.client.exchange(self.CLA, ins=Command.SIGN_TX, p1=SignP1.LAST, p2=SignP2Last.ED25519, data=message)

7 changes: 2 additions & 5 deletions test/python/test_polkadot.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,9 @@ def perform_final_tx(self, destination, send_amount, fees, memo):
dot = PolkadotClient(self.backend)
# Get public key.
key = dot.get_pubkey()
# Init signature process and assert response APDU code is 0x9000 (OK).
dot.sign_init().status
# craft tx
message = PolkadotClient.craft_valid_polkadot_transaction(destination, send_amount, None, None)

# Send message to be signed
sign_response = dot.sign_last(message)
sign_response = dot.perform_polkadot_transaction(destination, send_amount)

# Assert signature is verified properly with key and message
assert dot.verify_signature(hex_key=key,signature=sign_response.data[1:],message=message.hex().encode()) == True
Expand Down

0 comments on commit 265f9ef

Please sign in to comment.