Skip to content

Commit 4725473

Browse files
author
cawfree
committed
chore: verify address propagation when allocating wallet
1 parent 092b371 commit 4725473

File tree

1 file changed

+27
-25
lines changed

1 file changed

+27
-25
lines changed

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

Lines changed: 27 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -68,15 +68,13 @@ public Web3Module(final ReactApplicationContext pReactApplicationContext) {
6868
@ReactMethod
6969
public final void loadWallet(final ReadableMap pKeystore, final String pPassword, final Promise pPromise) {
7070
try {
71-
debug("got to this bit");
72-
final String id = this.addWallet(pKeystore, pPassword);
73-
debug("got id "+id);
74-
75-
// TODO: How to delegate? Is this really a wallet?
76-
// TODO: Note, you can also use a file or _create_ a new Wallet.
77-
pPromise.resolve(
78-
Arguments.createMap()
79-
);
71+
// XXX: Attempt to create the Wallet.
72+
final String addr = this.addWallet(pKeystore, pPassword);
73+
final WritableMap args = Arguments.createMap();
74+
// XXX: Propagate useful properties back to the caller.
75+
args.putString("address", addr);
76+
// XXX: Return the allocated wallet instance to the caller.
77+
pPromise.resolve(args);
8078
}
8179
catch (final Exception pException) {
8280
pPromise.reject(pException);
@@ -94,22 +92,26 @@ private void writeFile(final File pFile, final String pContent) throws IOExcepti
9492

9593
/** Adds a Wallet to the in-memory map. */
9694
private final String addWallet(final ReadableMap pKeystore, final String pPassword) throws IOException, CipherException {
97-
final String uuid = UUID.randomUUID().toString();
98-
final File f = File.createTempFile(uuid, "json", this.getReactApplicationContext().getCacheDir());
99-
100-
final HashMap<String, Object> data = pKeystore.toHashMap();
101-
final JSONObject j = new JSONObject(data);
102-
debug("writing "+j.toString());
103-
// TODO: Delete the temporary file once we're done with it.
104-
this.writeFile(f, j.toString());
105-
106-
// TODO: I would like to use loadJsonCredentials here, but it doesn't appear to be available in the latest version of web3-core.
107-
final Credentials creds = WalletUtils.loadCredentials(pPassword, f.getAbsolutePath());
108-
109-
// Here we track the wallet for future reference.
110-
this.getWallets().put(uuid, creds);
111-
112-
return uuid;
95+
final File f = File.createTempFile(
96+
UUID.randomUUID().toString(),
97+
"json",
98+
this.getReactApplicationContext().getCacheDir()
99+
);
100+
// XXX: Write the supplied data to a temporary file.
101+
// TODO: Use loadJsonCredentials.
102+
this.writeFile(f, new JSONObject(pKeystore.toHashMap()).toString());
103+
104+
// Load the Credentials.
105+
final Credentials c = WalletUtils.loadCredentials(pPassword, f.getAbsolutePath());
106+
// Delete the allocated file; it serves no purpose now.
107+
f.delete();
108+
// Fetch the Address of the Wallet.
109+
final String addr = c.getAddress();
110+
// Here we track the wallet for future reference. Any attempts to
111+
// re-create the same wallet will resolve to the same place in memory.
112+
this.getWallets().put(addr, c);
113+
// Return the Wallet's address.
114+
return addr;
113115
}
114116

115117
private final Map<String, Credentials> getWallets() {

0 commit comments

Comments
 (0)