LN-Zap / zap-android

Zap Wallet - Native android lightning wallet for node operators focused on user experience and ease of use ⚡️
MIT License
165 stars 49 forks source link

Add support for Near Field Communication (NFC) #158

Closed hawkmauk closed 4 years ago

hawkmauk commented 4 years ago

With services like Apple pay and Google pay NFC is going mainstream as a way to make payments with mobile devices, it would offer a familiar UX to new lightning users if payments via NFC were supported in mobile lightning wallets. I developed a PoC for this using a modified version of zap-android and would like some feedback and (hopefully) help and support in bringing this into the project which I believe would give it a good USP. Check out the [Https://GitHub.com/runcrypto/static-discharge](project repo) and [Https://GitHub.com/runcrypto/zap-android](modified zap repo) to see what I've managed to do so far.

michaelWuensch commented 4 years ago

Hi Hawkmauk, That is something I definetly want to have in Zap. I have no experience with NFC so far, but I will have a look at your implementation in the next days :) Thank you very much!

hawkmauk commented 4 years ago

There are a few details that still need to be worked on which is some of the additional help that would be needed .. ontop of this being my first foray into android development.

  1. There isn't a formal spec for the message format that needs to be sent. Ndef messages are sent over NFC with a URN header and URI that specifies the service the message is intended for . The URI would need to be agreed. (see https://developer.android.com/guide/topics/connectivity/nfc/)
  2. I believe from conversations over the last couple of days, that some other implementations already have NFC integration but using a LNURL encapsulated in a url Ndef message that might be worth considering.
  3. How should the NFC functionality be included and what should the workflow be through the Zap app?
michaelWuensch commented 4 years ago

Sorry, I was very busy the last two weeks with my job, so I didn't find the time to explore this. But I should be able to do this till the end of the year.

hawkmauk commented 4 years ago

Same here but hope to do some more over the next week when I have some time off

michaelWuensch commented 4 years ago

Hey, I just had a qick look at it and wanted to test it, but I do not know how to do so. If I use a second phone, both have NFC activated, then just the android Beam stuff starts. What do I need to test this? Furthermore I saw in my NFC settings that there is a "Tap and Pay" section where one can set the default Payment provider. Maybe we somehow have to get on that List with Zap.

hawkmauk commented 4 years ago

The setup I had was with a Raspberry Pi but two android phones would be simpler to setup... unfortunately I only have one NFC enabled android phone. There is an app called NFC Tools that is useful and should be able to provide the functionality to test:

  1. Navigate to the WRITE tab
  2. Click on Add a record
  3. Select Text
  4. Paste in a lightning invoice to the textfireld and click OK
  5. Click on the More options menu.
  6. Select Emulate an NFC tag (beta)
  7. Present 2nd Android phone to 1st phone to transfer invoice
michaelWuensch commented 4 years ago

Thanks for the info. I tried, but the emulation doesn't seem to work. I am not able to read the emulated NFC Tag even if I try to read it using the NFC Tools app on the other phone. I also switched phones and emulated on the other and even with another NFC Tag emulation app. But no luck so far.

I am buying some tags now and see if that works ;-)

michaelWuensch commented 4 years ago

Hi, I received my tags and with them it worked! So I did some tests and further reading.

The intent in the manifest should not be of type text in my opinion. Instead it should be:

<data android:scheme="bitcoin" />
<data android:scheme="lightning" />

And I think we should support both, normal lightning and LNURL request via nfc. But LNURL is completely independent from nfc. Nfc just transmits the data. When LNURL support is added, it should make no difference if the data comes from scanning a QR-Code or from reading a nfc tag. So all we need to do regarding nfc is read the data and then start the intent that handles transaction data.

In your current implementation the tag information gets "lost" if the app is either not open at all or on another activity than the home activity. In that case the app just opens / goes to the home screen and then the tag has to be scanned again. This is of course not ideal and will have to be changed.

Furthermore, I think in the current state we do not need this permission: android.permission.NFC_TRANSACTION_EVENT in my tests it worked without it.

hawkmauk commented 4 years ago

Great that you got it working! Did you use NFC Tools to emulate the tag? I was thinking that it might be easier to test if I tried to implement sending via NFC too but it might end up being implementation specific. Agree that both lightning and LNURL should be supported, although I'll need to do some reading up on LNURL but as you mention that shouldn't matter the transport of data which is in principal the same workflow as scanning a QR code. I realize that my current implementation is naive and need to do some more research on how Activities and Intents are used in Android.

michaelWuensch commented 4 years ago

The emulation did not work, so I bought some writeable tags. I have nearly finished the tag reading by now. I'll probably push a branch on my repo this Weekend, then you can have a look at it. I haven't included sending via NFC so far, but maybe we can try that next.

michaelWuensch commented 4 years ago

@hawkmauk I have a working solution now. Tags are readable directly from the home activity and the send activity. From other activities it behaves nearly similar to an app restart, which means it goes back to the home screen. When reading a tag while the app is closed, it now keeps the information and processes it as soon as the wallet is opened/unlocked. Furthermore I have moved most NFC logic into a separate util. You can check it out here: https://github.com/michaelWuensch/zap-android/tree/feature/Nfc If you find any problems, please let me know.

michaelWuensch commented 4 years ago

163 just got merged. NFC Support will be in the next release.