您好,登錄后才能下訂單哦!
在Android客戶端使用Web3j創建錢包、導入錢包時都可能會產生OOM,相關issue在Github上已經有所提及: https://github.com/web3j/web3j/issues/299 。這個問題在Web3j 3.0版本以前是沒有的,由于新版的Web3j使用spongycastle庫替換了lambdaworks庫,雖然在效率上提升了速度,但存在Android端的兼容性問題。
本項目代碼地址: https://github.com/uncleleonfan/WalletOOM.git
創建錢包OOM解決
在創建錢包時,如果創建一個Full Wallet,則會導致OOM:
public void onCreateFullWallet(View view) { String filePath = Environment.getExternalStorageDirectory().getAbsolutePath() + "/full"; File file = new File(filePath); file.mkdirs(); try { WalletUtils.generateFullNewWalletFile("a12345678", file); } catch (NoSuchAlgorithmException e) { e.printStackTrace(); } catch (NoSuchProviderException e) { e.printStackTrace(); } catch (InvalidAlgorithmParameterException e) { e.printStackTrace(); } catch (CipherException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } }
Log如下:
"Caused by: java.lang.OutOfMemoryError: Failed to allocate a 1036 byte allocation with 16777216 free bytes and 48MB until OOM; failed due to fragmentation (required continguous free 16384 bytes for a new buffer where largest contiguous free 8192 bytes)",
"\tat org.spongycastle.util.Arrays.clone(Arrays.java:602)",
"\tat org.spongycastle.crypto.generators.SCrypt.SMix(SCrypt.java:126)",
"\tat org.spongycastle.crypto.generators.SCrypt.MFcrypt(SCrypt.java:87)",
"\tat org.spongycastle.crypto.generators.SCrypt.generate(SCrypt.java:66)",
"\tat org.web3j.crypto.Wallet.generateDerivedScryptKey(Wallet.java:136)",
"\tat org.web3j.crypto.Wallet.create(Wallet.java:74)",
"\tat org.web3j.crypto.Wallet.createStandard(Wallet.java:93)",
"\tat org.web3j.crypto.WalletUtils.generateWalletFile(WalletUtils.java:61)"
generateFullNewWalletFile里面會調用createStandard創建錢包,使用N_STANDARD,P_STANDARD來配置加密強度,直接影響需使用的內存大小,最終導致OOM的發生。
public static WalletFile createStandard(String password, ECKeyPair ecKeyPair) throws CipherException { return create(password, ecKeyPair, N_STANDARD, P_STANDARD); }
解決方法非常簡單,創建一個Light Wallet即可:
public void onCreateLightWallet(View view) { String filePath = Environment.getExternalStorageDirectory().getAbsolutePath() + "/light"; File file = new File(filePath); file.mkdirs(); try { WalletUtils.generateLightNewWalletFile("a12345678", file); } catch (NoSuchAlgorithmException e) { e.printStackTrace(); } catch (NoSuchProviderException e) { e.printStackTrace(); } catch (InvalidAlgorithmParameterException e) { e.printStackTrace(); } catch (CipherException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } }
generateLightNewWalletFile會調用createLight來創建一個輕錢包,使用N_LIGHT,P_LIGHT,他們在數值上相對較小,所以不會OOM。
public static WalletFile createLight(String password, ECKeyPair ecKeyPair) throws CipherException { return create(password, ecKeyPair, N_LIGHT, P_LIGHT); }
我們可以對比一下N_STANDARD和P_STANDARD, N_LIGHT和P_LIGHT的大小:
private static final int N_LIGHT = 1 << 12; private static final int P_LIGHT = 6; private static final int N_STANDARD = 1 << 18; private static final int P_STANDARD = 1;
導入錢包OOM解決
當我們導入一個輕錢包時,不會產生OOM,但導入不是一個輕錢包時,則有可能產生OOM。例如,我們使用Imtoken創建一個錢包并導出Keystore,Keystore如下:
{"address":"9a2e2419f3af050d4730f80e7a65b9f8deb5e16f","crypto":{"cipher":"aes-128-ctr","cipherparams":{"iv":"eaccea79c27a91e307f24988186ef21a"},"ciphertext":"a163e532edf2d76beaee5c26fd2c2fab071a9cb37627aa185ac89e223e41ab97","kdf":"scrypt","kdfparams":{"dklen":32,"n":65536,"p":1,"r":8,"salt":"6a847392a029553f4152dea7bb0b6fb0ac9eec29f55e572fe94603182f5ed7f1"},"mac":"3fad2a31e18c611b10df84db9ae368ce2e189b5c35e9f111e40ca4b4bfb02491"},"id":"032c47c2-c7b7-46f8-a3f7-f526580f6f09","version":3}
可以看到,其中n為65536,p為1,而輕錢包的n為1<<12,即2的12次方,4096,所以這不是一個輕錢包。
我們將該Keystore作為一個json文件push到SD卡中,然后使用Web3j進行導入:
public void onImportWallet(View view) { try { //需提前將assets目錄下的keystore.json文件推送到手機SD里面 String filePath = Environment.getExternalStorageDirectory().getAbsolutePath() + "/keystore.json"; File file = new File(filePath); WalletUtils.loadCredentials("a12345678", file); } catch (IOException e) { e.printStackTrace(); } catch (CipherException e) { e.printStackTrace(); } }
發現同樣會OOM:
Caused by: java.lang.OutOfMemoryError: Failed to allocate a 1036 byte allocation with 13588800 free bytes and 12MB until OOM; failed due to fragmentation (required continguous free 16384 bytes for a new buffer where largest contiguous free 12288 bytes)
at org.spongycastle.util.Arrays.clone(Arrays.java:602)
at org.spongycastle.crypto.generators.SCrypt.SMix(SCrypt.java:126)
at org.spongycastle.crypto.generators.SCrypt.MFcrypt(SCrypt.java:87)
at org.spongycastle.crypto.generators.SCrypt.generate(SCrypt.java:66)
at org.web3j.crypto.Wallet.generateDerivedScryptKey(Wallet.java:136)
at org.web3j.crypto.Wallet.decrypt(Wallet.java:214)
at org.web3j.crypto.WalletUtils.loadCredentials(WalletUtils.java:112)
通過log可以看出來,這里和創建錢包的OOM是一樣的,都是最后調用generateDerivedScryptKey后導致:
private static byte[] generateDerivedScryptKey( byte[] password, byte[] salt, int n, int r, int p, int dkLen) throws CipherException { return SCrypt.generate(password, salt, n, r, p, dkLen); }
創建錢包可以創建一個輕錢包,導入錢包總不能讓用戶換一個輕錢包來導入吧。這里,我們只能還是換回lambda庫來完成keystore的編解碼, 即我們可以自己寫一個generateDerivedScryptKey方法,將spongycastle的SCrypt換成lambda的SCrypt。我們使用MyWalletUtils和MyWallet共同完成該任務。
public void onImportWallet(View view) { try { //需提前將assets目錄下的keystore.json文件推送到手機SD里面 String filePath = Environment.getExternalStorageDirectory().getAbsolutePath() + "/keystore.json"; File file = new File(filePath); Credentials credentials = MyWalletUtils.loadCredentials("a12345678", file); Log.d(TAG, "address:" + credentials.getAddress()); } catch (IOException e) { e.printStackTrace(); } catch (CipherException e) { e.printStackTrace(); } } public class MyWalletUtils { public static Credentials loadCredentials(String password, File source) throws IOException, CipherException { WalletFile walletFile = objectMapper.readValue(source, WalletFile.class); return Credentials.create(MyWallet.decrypt(password, walletFile)); } } public class MyWallet { private static final int CURRENT_VERSION = 3; private static final String CIPHER = "aes-128-ctr"; static final String AES_128_CTR = "pbkdf2"; static final String SCRYPT = "scrypt"; private static byte[] generateDerivedScryptKey( byte[] password, byte[] salt, int n, int r, int p, int dkLen) { try { return SCrypt.scrypt(password, salt, n, r, p, dkLen); } catch (GeneralSecurityException e) { e.printStackTrace(); } return null; } }
按照以上方法處理之后,就可以解決OOM,但是用戶等待的時間會稍微長一點,另外,最好還是添加一下Android平臺的libscrpt.so庫,大家可在本項目的jniLibs中找到。
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持億速云。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。