2

I'm trying to integrate web3j in an android application. But the library gives an Out Of Memory exception when loading the wallet file:

Credentials credentials = WalletUtils.loadCredentials("password", walletFile)

This is the exception:

java.lang.OutOfMemoryError: Failed to allocate a 268435468 byte allocation with 8050708 free bytes and 244MB until OOM

This seems to be the culprit:

//com.lambdaworks.crypto.SCrypt#scryptJ
byte[] V  = new byte[128 * r * N]; // r: 8, N: 262144, V = byte[268435456]

I know that the credentials can be loaded because another project, Ether Wallet, uses web3j and is able to load the same wallet file. Can someone tell me how to solve this?

  • I got exactly the same error while was trying to create a wallet. In my case, the problem was useFullScrypt which was set to true setting it to false solved the problem. Didn't have time yet to investigate the problem but as I checked the KPF params are different in those cases. – mtfk Feb 5 '18 at 22:35
1

Found a quick hack. Create a jniLibs folder in /src/main and add all the android folders from this scrypt fork into the jniLibs folder. Build. Run

I'll update with a more correct solution if/when I find one.

Note: Requires gradle 0.7.2+ for the jniLibs to be added to the build automatically

0

This is still happening.

decrypt method (org.web3j.crypto) calls generatedDerivedScryptKey (org.web3j.crypto) calls generate in SCrypt (org.spongycastle.crypto.generators)..

it all comes down to the size of the Array, initialized by parameter N:

WalletFile.KdfParams kdfParams = crypto.getKdfparams();
if (kdfParams instanceof WalletFile.ScryptKdfParams) {
    WalletFile.ScryptKdfParams scryptKdfParams =
                (WalletFile.ScryptKdfParams) crypto.getKdfparams();
    int dklen = scryptKdfParams.getDklen();
    int n = scryptKdfParams.getN();
    int p = scryptKdfParams.getP();
    int r = scryptKdfParams.getR();
    byte[] salt = Numeric.hexStringToByteArray(scryptKdfParams.getSalt());
    derivedKey = generateDerivedScryptKey(
                password.getBytes(Charset.forName("UTF-8")), salt, n, r, p, dklen);

N IS THE CORE PROBLEM

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.