NFC experts: can a terminal tell a Java card to "select 1337 sats worth of ecash from your memory using a simple coin selection algorithm and send it to me via NFC"?
Discussion
I'm not an expert but I find NFC technology very interesting so, I asked GPT about it...
In theory, yes, a terminal could interact with a Java Card via NFC and instruct it to perform certain operations like selecting "1337 sats worth of ecash." However, the exact implementation would depend on several factors related to both the Java Card's applet (software) and the NFC terminal's capabilities. Here's how this interaction could conceptually work:
1. Java Card with a Custom Applet:
Java Cards run applets (Java Card applications) that handle specific tasks, such as managing digital currencies (e.g., ecash or Bitcoin sats). In your case, the Java Card would need to have a pre-programmed applet that knows how to store and manage ecash, including sats.
The applet would also need to have a coin selection algorithm that can calculate how to pick "1337 sats" from the available balance. This is similar to how Bitcoin wallets handle UTXO (Unspent Transaction Output) selection.
2. NFC Terminal Commands:
The NFC terminal (like a payment terminal or smartphone) would communicate with the Java Card over NFC using a set of APDU (Application Protocol Data Units) commands, which are the standard command/response format used to communicate with Java Cards.
The terminal would issue a SELECT command to the card, specifying the amount (e.g., 1337 sats). This command would be interpreted by the applet, which then runs the coin selection algorithm to choose a set of ecash coins that sum up to 1337 sats.
3. Coin Selection Algorithm:
The applet would implement a coin selection algorithm similar to those used in Bitcoin wallets, which would scan the stored coins (UTXOs) in the card’s memory. It would then select the best combination of UTXOs to form 1337 sats. This could involve selecting one or more UTXOs that total at least 1337 sats.
Any change (leftover sats from a larger UTXO) would need to be handled, potentially by generating a new UTXO to be stored back in the card or sent elsewhere.
4. Transaction Data and Sending:
Once the correct amount has been selected, the Java Card would prepare the transaction data (likely a signature or hash) and send it back to the terminal over NFC.
The terminal would then broadcast the transaction to the Bitcoin network or handle it in whatever way is required to finalize the transfer of ecash (e.g., uploading the transaction data to a blockchain or private ledger).
Technical Challenges:
Applet Development: The Java Card would need a specialized applet capable of storing Bitcoin UTXOs and implementing a coin selection algorithm.
Security: The transaction needs to be securely signed, ensuring that the Java Card's private keys are never exposed during the NFC exchange.
Communication Protocol: The terminal and Java Card need to agree on a specific protocol and set of APDU commands for selecting and transferring ecash.
In conclusion, while the concept is feasible, the implementation requires sophisticated programming on both the Java Card (for storing and managing ecash and coin selection) and the terminal (for securely requesting and processing the transaction).
I'm not at all an expert in this area, I have never coded a Java card, but I looked into this a bit; javacard.framework.Applet defines a method called process(javacard.framework.APDU) which gets called whenever there is an incoming packet of information, and allows it to receive any kind of arbitrary data. This means that it's absolutely possible to code any instruction you would want, in fact from what I'm reading from the documentation, the byte at position ISO7816.OFFSET_INS, is designed specifically for that. So you'd check if it's whatever constant you set to getting sats and then read the following n bytes as the amount, the card can then respond with an ecash token of that amount without any issue.
The #satochip guys have a number of javacard products, they should have some practical insights.
Check out Elerium https://beechat.network/shop/
They did a talk at my local meetup and it looks like they focus on hardware but their small nfc chip can seem to do almost anything.
Afaik, the most powerful small factor NFC solution available.