Skip to content

Commit ad4db6f

Browse files
author
cawfree
committed
chore: config delegation to native runtime
1 parent c30a6cd commit ad4db6f

File tree

12 files changed

+4942
-78
lines changed

12 files changed

+4942
-78
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
`$ react-native link web3-react-native`
1010

1111
## Usage
12+
1213
```javascript
1314
import Web3 from 'web3-react-native';
1415

android/src/main/java/io/github/cawfree/web3/Web3Module.java

Lines changed: 82 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -15,101 +15,118 @@
1515
import org.web3j.protocol.Web3j;
1616
import org.web3j.protocol.core.methods.response.TransactionReceipt;
1717
import org.web3j.protocol.http.HttpService;
18-
import org.web3j.sample.contracts.generated.Greeter;
1918
import org.web3j.tx.Transfer;
2019
import org.web3j.tx.gas.ContractGasProvider;
2120
import org.web3j.tx.gas.DefaultGasProvider;
2221
import org.web3j.utils.Convert;
2322
import org.web3j.utils.Numeric;
2423

24+
// TODO: Remove this!
25+
import android.util.Log;
26+
2527
// Heavily influenced by (thanks guys!):
2628
// https://github.com/web3j/sample-project-gradle/blob/master/src/main/java/org/web3j/sample/Application.java
2729
public final class Web3Module extends ReactContextBaseJavaModule {
2830

31+
/* Static Declarations */
32+
public static final String TAG = "Web3Module";
33+
34+
/** A simple utility method to print via ADB. */
35+
private static final void debug(final String pString) {
36+
Log.d(TAG, pString);
37+
return;
38+
}
39+
2940
/* Member Variables. */
3041
private final ReactApplicationContext mReactApplicationContext;
31-
private Web3j mWeb3j;
32-
private Credentials mCredentials;
42+
//private Web3j mWeb3j;
43+
//private Credentials mCredentials;
3344

3445
/** Constructor */
3546
public Web3Module(final ReactApplicationContext pReactApplicationContext) {
3647
super(pReactApplicationContext);
3748
// Initialize Member Variables.
3849
this.mReactApplicationContext = pReactApplicationContext;
39-
this.mWeb3j = null;
40-
this.mCredentials = null;
50+
//this.mWeb3j = null;
51+
//this.mCredentials = null;
4152
}
4253

43-
@Override
44-
public String getName() {
45-
return "Web3";
46-
}
54+
/** Module name when imported via NativeModules. **/
55+
@Override public String getName() { return "Web3"; }
4756

48-
// XXX: Where to get wallet? Where to get password?
4957
@ReactMethod
50-
public final void create(final String pService, final String pPassword, final Promise pPromise) {
51-
// Ensure we're being passed a valid service, such as "https://rinkeby.infura.io/<your token>"
52-
if (pService != null && pService.length > 0) {
53-
this.setWeb3j(Web3j.build(new HttpService(pService)));
54-
// TODO: How to pass a wallet buffer? Where to get a wallet from? Anyone?!
55-
this.setCredentials(WalletUtils.loadCredentials(pPassword, "/path/to/<walletfile>"));
56-
// TODO: Can we return interesting properties, like the address of the wallet?
57-
pPromise.resolve(Arguments.createMap());
58-
} else {
59-
pPromise.reject(new Exception("Calls to create() expect a non-null, non empty service name."));
60-
}
58+
public final void init(final ReadableMap pKeystore, final String pPassword, final Promise pPromise) {
59+
debug("got to this bit");
60+
debug(pKeystore.toString());
61+
debug(pPassword);
62+
pPromise.resolve(Arguments.createMap());
6163
}
6264

63-
// TODO: Validate parameters.
64-
@ReactMethod
65-
public final void sendFunds(final String pAddress, final String pAmount, final String pUnits, final Promise pPromise) {
66-
try {
67-
this.throwIfMissingDependencies();
68-
// TODO: Verify this transaction value propagates through.
69-
final Transaction t = Transfer.sendFunds(this.getWeb3j(), this.getCredentials(), pAddress, new BigDecimal(pAmount), Convert.Unit.valueOf(pUnits));
70-
// TODO: Propagate useful information.
71-
// final String transactionHash = t.getTransactionHash();
72-
pPromise.resolve(Arguments.createMap());
73-
} catch (final Exception pException) {
74-
pPromise.reject(pException);
75-
}
76-
}
65+
//// XXX: Where to get wallet? Where to get password?
66+
//@ReactMethod
67+
//public final void create(final String pService, final String pPassword, final Promise pPromise) {
68+
// // Ensure we're being passed a valid service, such as "https://rinkeby.infura.io/<your token>"
69+
// if (pService != null && pService.length > 0) {
70+
// this.setWeb3j(Web3j.build(new HttpService(pService)));
71+
// // TODO: How to pass a wallet buffer? Where to get a wallet from? Anyone?!
72+
// this.setCredentials(WalletUtils.loadCredentials(pPassword, "/path/to/<walletfile>"));
73+
// // TODO: Can we return interesting properties, like the address of the wallet?
74+
// pPromise.resolve(Arguments.createMap());
75+
// } else {
76+
// pPromise.reject(new Exception("Calls to create() expect a non-null, non empty service name."));
77+
// }
78+
//}
7779

78-
@ReactMethod
79-
public final void destroy(final Promise pPromise) {
80-
try {
81-
this.throwIfMissingDependencies();
82-
this.setWeb3j(null);
83-
this.setCredentials(null);
84-
pPromise.resolve(Arguments.createMap());
85-
} catch (final Exception pException) {
86-
pPromise.reject(pException);
87-
}
88-
}
80+
//// TODO: Validate parameters.
81+
//@ReactMethod
82+
//public final void sendFunds(final String pAddress, final String pAmount, final String pUnits, final Promise pPromise) {
83+
// try {
84+
// this.throwIfMissingDependencies();
85+
// // TODO: Verify this transaction value propagates through.
86+
// final Transaction t = Transfer.sendFunds(this.getWeb3j(), this.getCredentials(), pAddress, new BigDecimal(pAmount), Convert.Unit.valueOf(pUnits));
87+
// // TODO: Propagate useful information.
88+
// // final String transactionHash = t.getTransactionHash();
89+
// pPromise.resolve(Arguments.createMap());
90+
// } catch (final Exception pException) {
91+
// pPromise.reject(pException);
92+
// }
93+
//}
8994

90-
/** Throws an Exception if the Service has not been configured. */
91-
private final void throwIfMissingDependencies() {
92-
if (this.getWeb3j() == null || this.getCredentials() == null) {
93-
throw new IllegalStateException("You must call create() before attempting to interact with a service.");
94-
}
95-
}
95+
//@ReactMethod
96+
//public final void destroy(final Promise pPromise) {
97+
// try {
98+
// this.throwIfMissingDependencies();
99+
// this.setWeb3j(null);
100+
// this.setCredentials(null);
101+
// pPromise.resolve(Arguments.createMap());
102+
// } catch (final Exception pException) {
103+
// pPromise.reject(pException);
104+
// }
105+
//}
96106

97-
/* Getters and Setters */
98-
private final void setWeb3j(final Web3j pWeb3j) {
99-
this.mWeb3j = pWeb3j;
100-
}
107+
///** Throws an Exception if the Service has not been configured. */
108+
//private final void throwIfMissingDependencies() {
109+
// if (this.getWeb3j() == null || this.getCredentials() == null) {
110+
// throw new IllegalStateException("You must call create() before attempting to interact with a service.");
111+
// }
112+
//}
101113

102-
private final Web3j getWeb3j() {
103-
return this.mWeb3j;
104-
}
114+
///* Getters and Setters */
115+
//private final void setWeb3j(final Web3j pWeb3j) {
116+
// this.mWeb3j = pWeb3j;
117+
//}
105118

106-
private final void setCredentials(final Credentials pCredentials) {
107-
this.mCredentials = pCredentials;
108-
}
119+
//private final Web3j getWeb3j() {
120+
// return this.mWeb3j;
121+
//}
109122

110-
private final Credentials getCredentials() {
111-
return this.mCredentials;
112-
}
123+
//private final void setCredentials(final Credentials pCredentials) {
124+
// this.mCredentials = pCredentials;
125+
//}
126+
127+
//private final Credentials getCredentials() {
128+
// return this.mCredentials;
129+
//}
113130

114131
//@ReactMethod
115132
//public void sampleMethod(String stringArgument, int numberArgument, Callback callback) {

example/.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,3 +57,6 @@ buck-out/
5757

5858
# CocoaPods
5959
/ios/Pods/
60+
61+
# Config files
62+
.env

example/App.js

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,41 @@
1-
import React from 'react';
2-
import { StyleSheet, Text, View } from 'react-native';
1+
import React, { useEffect } from 'react';
2+
import { Alert, StyleSheet, Text, View } from 'react-native';
33
import Web3 from 'web3-react-native';
44

5-
const { create, sendFunds, destroy } = Web3;
5+
import { URL, KEYSTORE, PASSWORD } from 'react-native-dotenv';
66

77
export default ({ ...unusedProps }) => {
88
useEffect(
99
async () => {
10+
const { ...result } = await Web3(
11+
KEYSTORE,
12+
PASSWORD,
13+
);
14+
Alert.alert('ici');
15+
//console.log('got result');
16+
1017
// TODO: Get this working, permit dynamic credentials specification, then allow multiple instances
1118
/* await configuration of the local environment */
12-
await create("https://rinkeby.infura.io/<your-token>", "<your-password>");
13-
/* send funds to a designated address */
14-
await sendFunds("0x19e03255f667bdfd50a32722df860b1eeaf4d635", "1", "WEI");
15-
/* teardown created environment */
16-
await destroy();
19+
//const { sendFunds, destroy } = await Web3(
20+
// url,
21+
// keystore,
22+
// password,
23+
//);
24+
///* send funds to a designated address */
25+
//await sendFunds("0x19e03255f667bdfd50a32722df860b1eeaf4d635", "1", "WEI");
26+
///* teardown created environment */
27+
//await destroy();
1728
},
1829
[],
1930
);
2031
return (
2132
<View
22-
style={StyleSheet.absoluteFill}
33+
style={[
34+
StyleSheet.absoluteFill,
35+
{
36+
backgroundColor: 'green',
37+
},
38+
]}
2339
>
2440
<Text
2541
children="web3-react-native"

example/android/app/build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,7 @@ android {
132132
applicationId "com.example"
133133
minSdkVersion rootProject.ext.minSdkVersion
134134
targetSdkVersion rootProject.ext.targetSdkVersion
135+
multiDexEnabled true
135136
versionCode 1
136137
versionName "1.0"
137138
}

example/app.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
{
22
"name": "example",
33
"displayName": "example"
4-
}
4+
}

example/babel.config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
module.exports = {
2-
presets: ['module:metro-react-native-babel-preset'],
2+
presets: ['module:metro-react-native-babel-preset', 'module:react-native-dotenv'],
33
};

example/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
"dependencies": {
1313
"react": "16.11.0",
1414
"react-native": "0.62.2",
15+
"react-native-dotenv": "^0.2.0",
1516
"web3-react-native": "link:../"
1617
},
1718
"devDependencies": {

example/yarn.lock

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1567,6 +1567,13 @@ babel-jest@^25.4.0:
15671567
chalk "^3.0.0"
15681568
slash "^3.0.0"
15691569

1570+
babel-plugin-dotenv@0.1.1:
1571+
version "0.1.1"
1572+
resolved "https://registry.yarnpkg.com/babel-plugin-dotenv/-/babel-plugin-dotenv-0.1.1.tgz#9c8faea67a7c034fe7e94099187ab2e7573400bc"
1573+
integrity sha1-nI+upnp8A0/n6UCZGHqy51c0ALw=
1574+
dependencies:
1575+
dotenv "^2.0.0"
1576+
15701577
babel-plugin-dynamic-import-node@^2.3.0:
15711578
version "2.3.3"
15721579
resolved "https://registry.yarnpkg.com/babel-plugin-dynamic-import-node/-/babel-plugin-dynamic-import-node-2.3.3.tgz#84fda19c976ec5c6defef57f9427b3def66e17a3"
@@ -2307,6 +2314,11 @@ domexception@^1.0.1:
23072314
dependencies:
23082315
webidl-conversions "^4.0.2"
23092316

2317+
dotenv@^2.0.0:
2318+
version "2.0.0"
2319+
resolved "https://registry.yarnpkg.com/dotenv/-/dotenv-2.0.0.tgz#bd759c357aaa70365e01c96b7b0bec08a6e0d949"
2320+
integrity sha1-vXWcNXqqcDZeAclrewvsCKbg2Uk=
2321+
23102322
ecc-jsbn@~0.1.1:
23112323
version "0.1.2"
23122324
resolved "https://registry.yarnpkg.com/ecc-jsbn/-/ecc-jsbn-0.1.2.tgz#3a83a904e54353287874c564b7549386849a98c9"
@@ -5289,6 +5301,11 @@ posix-character-classes@^0.1.0:
52895301
resolved "https://registry.yarnpkg.com/posix-character-classes/-/posix-character-classes-0.1.1.tgz#01eac0fe3b5af71a2a6c02feabb8c1fef7e00eab"
52905302
integrity sha1-AerA/jta9xoqbAL+q7jB/vfgDqs=
52915303

5304+
prelude-ls@^1.2.1:
5305+
version "1.2.1"
5306+
resolved "https://registry.yarnpkg.com/prelude-ls/-/prelude-ls-1.2.1.tgz#debc6489d7a6e6b0e7611888cec880337d316396"
5307+
integrity sha512-vkcDPrRZo1QZLbn5RLGPpg/WmIQ65qoWWhcGKf/b5eplkkarX0m9z8ppCat4mlOqUsWpyNuYgO3VRyrYHSzX5g==
5308+
52925309
prelude-ls@~1.1.2:
52935310
version "1.1.2"
52945311
resolved "https://registry.yarnpkg.com/prelude-ls/-/prelude-ls-1.1.2.tgz#21932a549f5e52ffd9a827f570e04be62a97da54"
@@ -5411,6 +5428,13 @@ react-is@^16.12.0, react-is@^16.8.1, react-is@^16.8.4, react-is@^16.8.6:
54115428
resolved "https://registry.yarnpkg.com/react-is/-/react-is-16.13.1.tgz#789729a4dc36de2999dc156dd6c1d9c18cea56a4"
54125429
integrity sha512-24e6ynE2H+OKt4kqsOvNd8kBpV65zoxbA4BVsEOB3ARVWQki/DHzaUoC5KuON/BiccDaCCTZBuOcfZs70kR8bQ==
54135430

5431+
react-native-dotenv@^0.2.0:
5432+
version "0.2.0"
5433+
resolved "https://registry.yarnpkg.com/react-native-dotenv/-/react-native-dotenv-0.2.0.tgz#311551cb6a35a3dcfede648bded55c0e3ece579d"
5434+
integrity sha1-MRVRy2o1o9z+3mSL3tVcDj7OV50=
5435+
dependencies:
5436+
babel-plugin-dotenv "0.1.1"
5437+
54145438
react-native@0.62.2:
54155439
version "0.62.2"
54165440
resolved "https://registry.yarnpkg.com/react-native/-/react-native-0.62.2.tgz#d831e11a3178705449142df19a70ac2ca16bad10"
@@ -6516,6 +6540,13 @@ tweetnacl@^0.14.3, tweetnacl@~0.14.0:
65166540
resolved "https://registry.yarnpkg.com/tweetnacl/-/tweetnacl-0.14.5.tgz#5ae68177f192d4456269d108afa93ff8743f4f64"
65176541
integrity sha1-WuaBd/GS1EViadEIr6k/+HQ/T2Q=
65186542

6543+
type-check@^0.4.0:
6544+
version "0.4.0"
6545+
resolved "https://registry.yarnpkg.com/type-check/-/type-check-0.4.0.tgz#07b8203bfa7056c0657050e3ccd2c37730bab8f1"
6546+
integrity sha512-XleUoc9uwGXqjWwXaUTZAmzMcFZ5858QA2vvx1Ur5xIcixXIP+8LnFDgRplU30us6teqdlskFfu+ae4K79Ooew==
6547+
dependencies:
6548+
prelude-ls "^1.2.1"
6549+
65196550
type-check@~0.3.2:
65206551
version "0.3.2"
65216552
resolved "https://registry.yarnpkg.com/type-check/-/type-check-0.3.2.tgz#5884cab512cf1d355e3fb784f30804b2b520db72"
@@ -6739,7 +6770,7 @@ wcwidth@^1.0.1:
67396770
dependencies:
67406771
defaults "^1.0.3"
67416772

6742-
"web3-react-native@link:../":
6773+
"web3-react-native@link:..":
67436774
version "0.0.0"
67446775
uid ""
67456776

index.js

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,26 @@
11
import { NativeModules } from 'react-native';
2+
import { typeCheck } from 'type-check';
23

34
const { Web3 } = NativeModules;
5+
const { init } = Web3;
46

5-
export default Web3;
7+
const sanitizeKeystore = (k) => {
8+
if (typeCheck("Object", k)) {
9+
return k;
10+
} else if (typeCheck("String", k)) {
11+
return JSON.parse(k);
12+
}
13+
throw new Error(`Expected Object|String keystore, encountered ${keystore}.`);
14+
};
15+
16+
const sanitizePassword = (p) => {
17+
if (typeCheck("String", p) && p.length > 0) {
18+
return p;
19+
}
20+
throw new Error(`Expected non-null non-empty String password, encountered ${typeof p}.`);
21+
};
22+
23+
export default (k, p) => init(
24+
sanitizeKeystore(k),
25+
sanitizePassword(p),
26+
);

0 commit comments

Comments
 (0)