ether-camp / ethereum-harmony

DEPRECATED! Ethereum Independent Peer
GNU General Public License v3.0
254 stars 89 forks source link

FileSystemKeystore does wrong assumption about wallet file name #44

Open Neurone opened 6 years ago

Neurone commented 6 years ago

Once the node is started, I got a bunch of errors like this:

2017-10-22 15:33:53,763 ERROR [MessageBroker-7] harmony - Error in making wallet address 5167e245-ea52-2b07-b853-29764b21c0f8
org.spongycastle.util.encoders.DecoderException: exception decoding Hex string: invalid characters encountered in Hex string
    at org.spongycastle.util.encoders.Hex.decode(Hex.java:132)
    at com.ethercamp.harmony.service.WalletService.lambda$getWalletInfo$104(WalletService.java:258)
    at java.util.stream.ReferencePipeline$7$1.accept(Unknown Source)
    at java.util.concurrent.ConcurrentHashMap$EntrySpliterator.forEachRemaining(Unknown Source)
    at java.util.stream.AbstractPipeline.copyInto(Unknown Source)
    at java.util.stream.AbstractPipeline.wrapAndCopyInto(Unknown Source)
    at java.util.stream.ReduceOps$ReduceOp.evaluateSequential(Unknown Source)
    at java.util.stream.AbstractPipeline.evaluate(Unknown Source)
    at java.util.stream.ReferencePipeline.collect(Unknown Source)
    at com.ethercamp.harmony.service.WalletService.getWalletInfo(WalletService.java:274)
    at com.ethercamp.harmony.service.WalletService.doSendWalletInfo(WalletService.java:237)
    at sun.reflect.GeneratedMethodAccessor141.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at org.springframework.scheduling.support.ScheduledMethodRunnable.run(ScheduledMethodRunnable.java:65)
    at org.springframework.scheduling.support.DelegatingErrorHandlingRunnable.run(DelegatingErrorHandlingRunnable.java:54)
    at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source)
    at java.util.concurrent.FutureTask.runAndReset(Unknown Source)
    at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$301(Unknown Source)
    at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
    at java.lang.Thread.run(Unknown Source)
Caused by: java.io.IOException: invalid characters encountered in Hex string
    at org.spongycastle.util.encoders.HexEncoder.decode(HexEncoder.java:180)
    at org.spongycastle.util.encoders.Hex.decode(Hex.java:128)
    ... 22 common frames omitted

I think the main cause of these errors is the method listStoredKeys() in the class com.ethercamp.harmony.keystore.FileSystemKeystore

    /**
     * @return array of addresses in format "0x123abc..."
     */
    @Override
    public String[] listStoredKeys() {
        return getFiles().stream()
                .filter(f -> !f.isDirectory())
                .map(f -> f.getName().split("--"))
                .filter(n -> n != null && n.length == 3)
                .map(a -> "0x" + a[2])
                .toArray(size -> new String[size]);
    }

The method does the wrong assumption that if a wallet has -- inside the file name, the right side should be the address of the wallet. This is not true, indeed it usually is the UUID. This should be also expected because the go-ethereum best practices are linked in the head of the same FileSystemKeystore.java file:

/**
 * Key store manager working in user file system. Can store and load keys.
 * Comply to go-ethereum key store format.
 * https://github.com/ethereum/wiki/wiki/Web3-Secret-Storage-Definition
**/

I think the class should be able to read wallet with any file name and it should read the address inside the JSON file, at the "address" attribute.