NFC access for Dapps (#670)

This commit is contained in:
alwx 2017-01-26 10:16:07 +03:00 committed by Roman Volosovskyi
parent 30fb48523e
commit 0944df715f
9 changed files with 63 additions and 4 deletions

View File

@ -1,7 +1,7 @@
{
"name": "StatusIm",
"interface": "reagent",
"androidHost": "localhost",
"androidHost": "10.0.3.2",
"modules": [
"react-native-contacts",
"react-native-invertible-scroll-view",
@ -37,7 +37,8 @@
"react-native-share",
"react-native-emoji-picker",
"react-native-autolink",
"instabug-reactnative"
"instabug-reactnative",
"nfc-react-native"
],
"imageDirs": [
"images"

View File

@ -158,6 +158,7 @@ android {
}
dependencies {
compile project(':nfc-react-native')
compile project(':instabug-reactnative')
compile project(':react-native-splash-screen')
compile project(':react-native-image-resizer')

View File

@ -3,6 +3,7 @@ package im.status.ethereum;
import android.app.Application;
import com.facebook.react.ReactApplication;
import es.tiarg.nfcreactnative.NfcReactNativePackage;
import com.instabug.reactlibrary.RNInstabugReactnativePackage;
import com.cboy.rn.splashscreen.SplashScreenReactPackage;
import com.facebook.react.ReactNativeHost;
@ -41,6 +42,7 @@ public class MainApplication extends Application implements ReactApplication {
protected List<ReactPackage> getPackages() {
return Arrays.asList(
new MainReactPackage(),
new NfcReactNativePackage(),
new RNInstabugReactnativePackage("b239f82a9cb00464e4c72cc703e6821e",MainApplication.this,"shake"),
new SplashScreenReactPackage(),
new StatusPackage(),

View File

@ -1,6 +1,8 @@
rootProject.name = 'StatusIm'
include ':app'
include ':nfc-react-native'
project(':nfc-react-native').projectDir = new File(rootProject.projectDir, '../node_modules/nfc-react-native/android')
include ':instabug-reactnative'
project(':instabug-reactnative').projectDir = new File(rootProject.projectDir, '../node_modules/instabug-reactnative/android')
include ':react-native-splash-screen'

View File

@ -198,5 +198,8 @@ var TopLevel = {
"_value" : function () {},
"ListView": function() {},
"DataSource": function() {},
"IBGLog": function() {}
"IBGLog": function() {},
"getCardId": function() {},
"readTag": function() {},
"writeTag": function() {}
};

View File

@ -34,6 +34,7 @@
"https-browserify": "0.0.1",
"identicon.js": "github:status-im/identicon.js",
"instabug-reactnative": "git+https://github.com/status-im/instabug-reactnative.git",
"nfc-react-native": "^0.3.9",
"os-browserify": "^0.1.2",
"path-browserify": "0.0.0",
"process": "^0.11.5",

View File

@ -8,6 +8,7 @@
[status-im.models.commands :as commands]
[status-im.commands.utils :as cu]
[status-im.components.status :as s]
[status-im.components.nfc :as nfc]
[status-im.constants :as c]
[cljs.reader :refer [read-string]]
[status-im.navigation.handlers :as nav]))
@ -92,6 +93,7 @@
:params params}])
:webview-scan-qr (dispatch [:show-scan-qr :webview-address-from-qr])
:webview-send-eth (dispatch [:webview-send-eth! params])
:nfc (dispatch [:webview-nfc params])
(log/error (str "Unknown event: " event')))))))
(defmethod nav/preload-data! :contact-list-modal
@ -127,3 +129,21 @@
parameters
(fn [data]
(log/debug :webview-send-eth-callback data)))))))
(register-handler :webview-nfc
(u/side-effect!
(fn [_ [_ {:keys [event params]}]]
(let [callback #(dispatch [:send-to-webview-bridge {:params % :event "nfc"}])]
(case (keyword event)
:get-card-id (nfc/get-card-id #(callback {:event :get-card-id :card %})
#(callback {:event :get-card-id :error %}))
:read-tag (let [{:keys [sectors]} params]
(nfc/read-tag sectors
#(callback {:event :read-tag :card %})
#(callback {:event :read-tag :error %})))
:write-tag (let [{:keys [sectors id]} params]
(nfc/write-tag sectors
id
#(callback {:event :write-tag :card %})
#(callback {:event :write-tag :error %})))
:default)))))

View File

@ -20,7 +20,6 @@
[status-im.i18n :refer [label]]
[status-im.components.react :refer [dismiss-keyboard!]]
[clojure.string :as str]
[cljs.spec :as s]
[status-im.components.chat-icon.screen :as ci]))
(defonce drawer-atom (atom))

View File

@ -0,0 +1,30 @@
(ns status-im.components.nfc
(:require [cljs.spec :as s]
[status-im.utils.platform :as platform]))
(def class
(when platform/android?
(js/require "nfc-react-native")))
(def android-only-error "NFC API is available only on Android")
(defn get-card-id [on-success on-error]
(if platform/android?
(-> (.getCardId class)
(.then on-success)
(.catch on-error))
(on-error android-only-error)))
(defn read-tag [sectors on-success on-error]
(if platform/android?
(-> (.readTag class (clj->js sectors))
(.then on-success)
(.catch on-error))
(on-error android-only-error)))
(defn write-tag [sectors card-id on-success on-error]
(if platform/android?
(-> (.writeTag class (clj->js sectors) card-id)
(.then on-success)
(.catch on-error))
(on-error android-only-error)))