Some checks failed
Checks on the Python tests / Call Ledger Python linters (push) Has been cancelled
Build and run functional tests using ragger through reusable workflow / Build application using the reusable workflow (push) Has been cancelled
Run coding style check / Check linting using the reusable workflow (push) Has been cancelled
Ensure compliance with Ledger guidelines / Call Ledger guidelines_enforcer (push) Has been cancelled
Misspellings checks / Check misspellings (push) Has been cancelled
Build and run functional tests using ragger through reusable workflow / Run standalone ragger tests using the reusable workflow (push) Has been cancelled
Hardware-wallet app for Handshake (HNS). Derives Handshake addresses and signs
Handshake transactions, including name-auction covenants, on Ledger Stax, Flex,
Nano S+, Nano X and Apex+.
The design premise is that the host computer is untrusted: every APDU byte is
attacker-controlled, and the device's job is to be correct when the host lies.
Two failures outrank all others, and the code is shaped around them.
Signing something other than what the user approved. Any field the signature
commits to and the host can vary has to appear on the review screen, and
anything the screen cannot represent honestly is refused rather than shown under
a label that does not describe it. So the device signs SIGHASH_ALL only,
checked per input rather than once for the session; it displays a TRANSFER
covenant's destination, which decides who ends up owning the name and appears
nowhere else in the output; it shows a covenant's name hash when the plaintext
name cannot be verified against it; and it refuses covenant kinds it cannot
name, outputs whose values exceed their inputs, and declared input totals that
would overflow the fee arithmetic.
Misusing key material. Derivation paths are constrained to
m/44'/{5353..5356}'/account'[/change/index] in the app itself, not only by the
install manifest, because BOLOS terminates the app on an out-of-whitelist path
instead of returning an error. One coin type per signing session, since the
review renders every address under a single HRP.
Panics count too: set_panic!(exiting_panic) means a panic kills the app and
strands the session, so nothing unwraps on host-derived data and the session
ceilings are sized to the configured heap.
Consensus follows shd, the Swift Handshake node, which is the reference for the
sighash preimage, covenant item layouts and address encoding.
SIGN_TX is a PSBT-style state machine driven by P1 (BEGIN / ADD_INPUT /
ADD_OUTPUT / REVIEW / SIGN_INPUT): the host streams the transaction as
structured records, the device accumulates its own view of the values, shows one
review, then signs each input on demand.
Tests run under speculos in a container (./scripts/test), 66 of them, green on
all five devices. test_sign_tx_policy.py covers each refusal above and needs no
golden snapshots because every case is rejected before anything is drawn; the
snapshots that do exist are the record of what the user sees before approving.
tests/application_client/handshake_sighash.py is a deliberately independent
reimplementation of the sighash, used to verify real signatures.
Status: v0.1, pre-audit. Not for mainnet keys. Signing has not yet been
exercised on physical hardware.
Forked from LedgerHQ/app-boilerplate-rust (Apache-2.0).
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
119 lines
3.8 KiB
Python
119 lines
3.8 KiB
Python
"""Builders for the structured records SIGN_TX streams to the device.
|
|
|
|
Wire layouts are documented at the top of `src/handlers/sign_tx.rs`; the
|
|
covenant item layouts mirror `shd/Sources/Covenants/CovenantData.swift`.
|
|
"""
|
|
|
|
from hashlib import sha3_256
|
|
from typing import List, Sequence, Tuple
|
|
|
|
from ragger.bip import pack_derivation_path
|
|
|
|
SCRIPT_KIND_P2WPKH: int = 0x00
|
|
SCRIPT_KIND_P2WSH: int = 0x01
|
|
|
|
SIGHASH_ALL: int = 0x01
|
|
SIGHASH_NONE: int = 0x02
|
|
SIGHASH_SINGLE: int = 0x03
|
|
SIGHASH_SINGLEREVERSE: int = 0x04
|
|
SIGHASH_NOINPUT: int = 0x40
|
|
SIGHASH_ANYONECANPAY: int = 0x80
|
|
|
|
# Covenant types, matching shd/Sources/Protocol/CovenantType.swift.
|
|
COVENANT_NONE: int = 0
|
|
COVENANT_CLAIM: int = 1
|
|
COVENANT_OPEN: int = 2
|
|
COVENANT_BID: int = 3
|
|
COVENANT_REVEAL: int = 4
|
|
COVENANT_REDEEM: int = 5
|
|
COVENANT_REGISTER: int = 6
|
|
COVENANT_UPDATE: int = 7
|
|
COVENANT_RENEW: int = 8
|
|
COVENANT_TRANSFER: int = 9
|
|
COVENANT_FINALIZE: int = 10
|
|
COVENANT_REVOKE: int = 11
|
|
|
|
|
|
def encode_varint(n: int) -> bytes:
|
|
if n < 0xFD:
|
|
return bytes([n])
|
|
if n <= 0xFFFF:
|
|
return b"\xfd" + n.to_bytes(2, "little")
|
|
if n <= 0xFFFFFFFF:
|
|
return b"\xfe" + n.to_bytes(4, "little")
|
|
return b"\xff" + n.to_bytes(8, "little")
|
|
|
|
|
|
def name_hash(name: bytes) -> bytes:
|
|
"""`shd` NameRules.hashName: SHA3-256, not Keccak."""
|
|
return sha3_256(name).digest()
|
|
|
|
|
|
def begin_payload(n_in: int, n_out: int, version: int = 0, locktime: int = 0) -> bytes:
|
|
return (version.to_bytes(4, "little")
|
|
+ encode_varint(n_in)
|
|
+ encode_varint(n_out)
|
|
+ locktime.to_bytes(4, "little"))
|
|
|
|
|
|
def input_payload(path: str,
|
|
value: int,
|
|
txid: bytes = b"\x11" * 32,
|
|
vout: int = 0,
|
|
sequence: int = 0xFFFFFFFF,
|
|
sighash: int = SIGHASH_ALL,
|
|
script_kind: int = SCRIPT_KIND_P2WPKH) -> bytes:
|
|
assert len(txid) == 32
|
|
return (txid
|
|
+ vout.to_bytes(4, "little")
|
|
+ sequence.to_bytes(4, "little")
|
|
+ value.to_bytes(8, "little")
|
|
+ bytes([sighash])
|
|
+ pack_derivation_path(path)
|
|
+ bytes([script_kind]))
|
|
|
|
|
|
def output_payload(value: int,
|
|
addr_hash: bytes = b"\x22" * 20,
|
|
addr_version: int = 0,
|
|
covenant_kind: int = COVENANT_NONE,
|
|
covenant_items: Sequence[bytes] = ()) -> bytes:
|
|
items = b"".join(encode_varint(len(i)) + i for i in covenant_items)
|
|
return (value.to_bytes(8, "little")
|
|
+ bytes([addr_version, len(addr_hash)])
|
|
+ addr_hash
|
|
+ bytes([covenant_kind])
|
|
+ encode_varint(len(covenant_items))
|
|
+ items)
|
|
|
|
|
|
def uint32_le(n: int) -> bytes:
|
|
return n.to_bytes(4, "little")
|
|
|
|
|
|
def open_items(name: bytes) -> List[bytes]:
|
|
"""OPEN: [nameHash, height, name] (CovenantData.makeOpen)."""
|
|
return [name_hash(name), uint32_le(0), name]
|
|
|
|
|
|
def bid_items(name: bytes, height: int = 0, blind: bytes = b"\x33" * 32) -> List[bytes]:
|
|
"""BID: [nameHash, startHeight, name, blind] (CovenantData.makeBid)."""
|
|
return [name_hash(name), uint32_le(height), name, blind]
|
|
|
|
|
|
def reveal_items(name: bytes, height: int = 0, nonce: bytes = b"\x44" * 32) -> List[bytes]:
|
|
"""REVEAL: [nameHash, startHeight, nonce]. Carries no plaintext name."""
|
|
return [name_hash(name), uint32_le(height), nonce]
|
|
|
|
|
|
def transfer_items(name: bytes,
|
|
dest_hash: bytes,
|
|
height: int = 0,
|
|
dest_version: int = 0) -> List[bytes]:
|
|
"""TRANSFER: [nameHash, startHeight, [version], addressHash].
|
|
|
|
`items[3]` is the address the name is handed to, which is the whole point
|
|
of the covenant and appears nowhere else in the output.
|
|
"""
|
|
return [name_hash(name), uint32_le(height), bytes([dest_version]), dest_hash]
|