Java Code Examples for org.web3j.crypto.WalletFile

The following examples show how to use org.web3j.crypto.WalletFile. These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source Project: bcm-android   Source File: OwnWalletUtils.java    License: GNU General Public License v3.0 6 votes vote down vote up
public static String generateWalletFile(
        String password, ECKeyPair ecKeyPair, File destinationDirectory, boolean useFullScrypt)
        throws CipherException, IOException {

    WalletFile walletFile;
    if (useFullScrypt) {
        walletFile = Wallet.createStandard(password, ecKeyPair);
    } else {
        walletFile = Wallet.createLight(password, ecKeyPair);
    }

    String fileName = getWalletFileName(walletFile);
    File destination = new File(destinationDirectory, fileName);

    ObjectMapper objectMapper = ObjectMapperFactory.getObjectMapper();
    objectMapper.writeValue(destination, walletFile);

    return fileName;
}
 
Example 2
private void checkFromRestore() {
        Bundle args = getArguments();
        if (args != null) {
            String key = args.getString(KEY);
            if (!TextUtils.isEmpty(key)) {
                showProgress(getString(R.string.restoring_wallet));
                walletManager.restoreFromBlock(key, new WalletCreationCallback() {
                    @Override
                    public void onWalletCreated(WalletFile walletFile) {
                        closeProgress();
                        if (isVisible) {
//                            startClockwiseRotation();
                            updateData(true);
                        }
                    }
                });
            }
        }
    }
 
Example 3
private void loadFromJson(Uri uri, String pwd) {
    sharedManager.setJsonExcep("");
    try {
        ObjectMapper objectMapper = ObjectMapperFactory.getObjectMapper();
        objectMapper.configure(JsonParser.Feature.ALLOW_UNQUOTED_FIELD_NAMES, true);
        objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
        InputStream is = context.getContentResolver().openInputStream(uri);
        WalletFile walletFile = objectMapper.readValue(is, WalletFile.class);
        credentials = Credentials.create(Wallet.decrypt(pwd, walletFile));
    } catch (JsonParseException jpe) {
        sharedManager.setJsonExcep("JsonParseException");
        jpe.printStackTrace();
    } catch (CipherException ce) {
        if (ce.getMessage().equals("Invalid password provided")) {
            sharedManager.setJsonExcep("WrongPassword");
        } else {
            sharedManager.setJsonExcep("CipherException");
        }
        ce.printStackTrace();
    } catch (Exception e) {
        e.printStackTrace();
    }
}
 
Example 4
private void checkFromRestore() {
    Log.d("psd", "checkFromRestore start");
    Bundle args = getArguments();
    if (args != null) {
        String key = args.getString(KEY);
        if (!TextUtils.isEmpty(key)) {
            showProgress(getString(R.string.restoring_wallet));
            walletManager.restoreFromBlock(key, new WalletCreationCallback() {
                @Override
                public void onWalletCreated(WalletFile walletFile) {
                    closeProgress();
                    if (isVisible && walletFile != null) {
                        startClockwiseRotation();
                        updateData(true);
                    }
                    Log.d("psd", "checkFromRestore onWalletCreated");
                }
            });
        }
    }
}
 
Example 5
private void loadFromJson(Uri uri, String pwd) {
    sharedManager.setJsonExcep("");
    try {
        ObjectMapper objectMapper = ObjectMapperFactory.getObjectMapper();
        objectMapper.configure(JsonParser.Feature.ALLOW_UNQUOTED_FIELD_NAMES, true);
        objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
        InputStream is = context.getContentResolver().openInputStream(uri);
        WalletFile walletFile = objectMapper.readValue(is, WalletFile.class);
        credentials = Credentials.create(Wallet.decrypt(pwd, walletFile));
    } catch (JsonParseException jpe) {
        sharedManager.setJsonExcep("JsonParseException");
        jpe.printStackTrace();
    } catch (CipherException ce) {
        if (ce.getMessage().equals("Invalid password provided")) {
            sharedManager.setJsonExcep("WrongPassword");
        } else {
            sharedManager.setJsonExcep("CipherException");
        }
        ce.printStackTrace();
    } catch (Exception e) {
        e.printStackTrace();
    }
}
 
Example 6
private void checkFromRestore() {
    Bundle args = getArguments();
    if (args != null) {
        String key = args.getString(KEY);
        if (!TextUtils.isEmpty(key)) {
            showProgress(getString(R.string.restoring_wallet));
            walletManager.restoreFromBlock(key, new WalletCreationCallback() {
                @Override
                public void onWalletCreated(WalletFile walletFile) {
                    closeProgress();
                    if (isVisible) {
                        startClockwiseRotation();
                        updateData(true);
                    }
                }
            });
        }
    }
}
 
Example 7
private void loadFromJson(Uri uri, String pwd) {
    sharedManager.setJsonExcep("");
    try {
        ObjectMapper objectMapper = ObjectMapperFactory.getObjectMapper();
        objectMapper.configure(JsonParser.Feature.ALLOW_UNQUOTED_FIELD_NAMES, true);
        objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
        InputStream is = context.getContentResolver().openInputStream(uri);
        WalletFile walletFile = objectMapper.readValue(is, WalletFile.class);
        credentials = Credentials.create(Wallet.decrypt(pwd, walletFile));
    } catch (JsonParseException jpe) {
        sharedManager.setJsonExcep("JsonParseException");
        jpe.printStackTrace();
    } catch (CipherException ce) {
        if (ce.getMessage().equals("Invalid password provided")){
            sharedManager.setJsonExcep("WrongPassword");
        } else {
            sharedManager.setJsonExcep("CipherException");
        }
        ce.printStackTrace();
    } catch (Exception e) {
        e.printStackTrace();
    }
}
 
Example 8
Source Project: dapp-wallet-demo   Source File: InitWalletManager.java    License: Apache License 2.0 6 votes vote down vote up
public Flowable<HLWallet> importMnemonic(Context context,
                                         String password,
                                         String mnemonics) {
    Flowable<String> flowable = Flowable.just(mnemonics);

    return flowable
            .flatMap(s -> {
                ECKeyPair keyPair = generateKeyPair(s);
                WalletFile walletFile = Wallet.createLight(password, keyPair);
                HLWallet hlWallet = new HLWallet(walletFile);
                if (WalletManager.shared().isWalletExist(hlWallet.getAddress())) {
                    return Flowable.error(new HLError(ReplyCode.walletExisted, new Throwable("Wallet existed!")));
                }
                WalletManager.shared().saveWallet(context, hlWallet);
                return Flowable.just(hlWallet);
            });
}
 
Example 9
Source Project: dapp-wallet-demo   Source File: InitWalletManager.java    License: Apache License 2.0 6 votes vote down vote up
public Flowable<HLWallet> importPrivateKey(Context context, String privateKey, String password) {
    if (privateKey.startsWith(Constant.PREFIX_16)) {
        privateKey = privateKey.substring(Constant.PREFIX_16.length());
    }
    Flowable<String> flowable = Flowable.just(privateKey);
    return flowable.flatMap(s -> {
        byte[] privateBytes = Hex.decode(s);
        ECKeyPair ecKeyPair = ECKeyPair.create(privateBytes);
        WalletFile walletFile = Wallet.createLight(password, ecKeyPair);
        HLWallet hlWallet = new HLWallet(walletFile);
        if (WalletManager.shared().isWalletExist(hlWallet.getAddress())) {
            return Flowable.error(new HLError(ReplyCode.walletExisted, new Throwable("Wallet existed!")));
        }
        WalletManager.shared().saveWallet(context, hlWallet);
        return Flowable.just(hlWallet);
    });
}
 
Example 10
Source Project: dapp-wallet-demo   Source File: InitWalletManager.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * web3j的导入Keystore方式,容易OOM
 */
@Deprecated
public Flowable<HLWallet> importKeystoreViaWeb3j(Context context, String keystore, String password) {
    return Flowable.just(keystore)
            .flatMap(s -> {
                ObjectMapper objectMapper = new ObjectMapper();
                WalletFile walletFile = objectMapper.readValue(keystore, WalletFile.class);
                ECKeyPair keyPair = Wallet.decrypt(password, walletFile);
                HLWallet hlWallet = new HLWallet(walletFile);

                WalletFile generateWalletFile = Wallet.createLight(password, keyPair);
                if (!generateWalletFile.getAddress().equalsIgnoreCase(walletFile.getAddress())) {
                    return Flowable.error(new HLError(ReplyCode.failure, new Throwable("address doesn't match private key")));
                }

                if (WalletManager.shared().isWalletExist(hlWallet.getAddress())) {
                    return Flowable.error(new HLError(ReplyCode.walletExisted, new Throwable("Wallet existed!")));
                }
                WalletManager.shared().saveWallet(context, hlWallet);
                return Flowable.just(hlWallet);
            });
}
 
Example 11
Source Project: dapp-wallet-demo   Source File: InitWalletManager.java    License: Apache License 2.0 6 votes vote down vote up
public Flowable<HLWallet> importKeystore(Context context, String keystore, String password) {
    return Flowable.just(keystore)
            .flatMap(s -> {
                ObjectMapper objectMapper = new ObjectMapper();
                WalletFile walletFile = objectMapper.readValue(keystore, WalletFile.class);
                ECKeyPair keyPair = LWallet.decrypt(password, walletFile);
                HLWallet hlWallet = new HLWallet(walletFile);

                WalletFile generateWalletFile = Wallet.createLight(password, keyPair);
                if (!generateWalletFile.getAddress().equalsIgnoreCase(walletFile.getAddress())) {
                    return Flowable.error(new HLError(ReplyCode.failure, new Throwable("address doesn't match private key")));
                }

                if (WalletManager.shared().isWalletExist(hlWallet.getAddress())) {
                    return Flowable.error(new HLError(ReplyCode.walletExisted, new Throwable("Wallet existed!")));
                }
                WalletManager.shared().saveWallet(context, hlWallet);
                return Flowable.just(hlWallet);
            });
}
 
Example 12
Source Project: alpha-wallet-android   Source File: ImportWalletViewModel.java    License: MIT License 6 votes vote down vote up
public Single<Boolean> checkKeystorePassword(String keystore, String keystoreAddress, String password)
{
    return Single.fromCallable(() -> {
        Boolean isValid = false;
        try
        {
            ObjectMapper objectMapper = new ObjectMapper();
            WalletFile walletFile = objectMapper.readValue(keystore, WalletFile.class);
            ECKeyPair kp = org.web3j.crypto.Wallet.decrypt(password, walletFile);
            String address = Numeric.prependHexPrefix(Keys.getAddress(kp));
            if (address.equalsIgnoreCase(keystoreAddress)) isValid = true;
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        return isValid;
    });
}
 
Example 13
public static String generateWalletFile(
        String password, ECKeyPair ecKeyPair, File destinationDirectory, boolean useFullScrypt)
        throws CipherException, IOException {

    WalletFile walletFile;
    if (useFullScrypt) {
        walletFile = Wallet.createStandard(password, ecKeyPair);
    } else {
        walletFile = Wallet.createLight(password, ecKeyPair);
    }

    String fileName = getWalletFileName(walletFile);
    File destination = new File(destinationDirectory, fileName);

    ObjectMapper objectMapper = ObjectMapperFactory.getObjectMapper();
    objectMapper.writeValue(destination, walletFile);

    return fileName;
}
 
Example 14
/**
 * 导出keystore文件
 *
 * @param walletId
 * @param pwd
 * @return
 */
public static String deriveKeystore(long walletId, String pwd) {
    ETHWallet ethWallet = WalletDaoUtils.ethWalletDao.load(walletId);
    String keystore = null;
    WalletFile walletFile;
    try {
        walletFile = objectMapper.readValue(new File(ethWallet.getKeystorePath()), WalletFile.class);
        keystore = objectMapper.writeValueAsString(walletFile);
    } catch (IOException e) {
        e.printStackTrace();
    }
    return keystore;
}
 
Example 15
Source Project: ETHWallet   Source File: ImportPrivateKey.java    License: GNU General Public License v3.0 5 votes vote down vote up
@Test
public void privateKeyToKeystoreTest() throws UnsupportedEncodingException, CipherException, JsonProcessingException {
    String privateKey = "68adf89afe85baa046919f904f7c1e3a9cb28ca8b3039c2bcb3fa5a980d3a165";
    String passphrase = "x";
    WalletFile w = EtherStoreUtils.convertPrivateKeyToKeystoreFile(privateKey, passphrase);

    assert(w.getAddress().equals("7d788fc8df7165b11a19f201558fcc3590fd8d97"));
}
 
Example 16
@Override
public Request<?, NewAccountIdentifier> parityNewAccountFromWallet(
        WalletFile walletFile, String password) {
    return new Request<>(
            "parity_newAccountFromWallet",
            Arrays.asList(walletFile, password),
            web3jService,
            NewAccountIdentifier.class);
}
 
Example 17
@Test
public void testParityNewAccountFromWallet() throws Exception {
    WalletFile walletFile = new WalletFile();
    walletFile.setAddress("0x...");

    WalletFile.Crypto crypto = new WalletFile.Crypto();
    crypto.setCipher("CIPHER");
    crypto.setCiphertext("CIPHERTEXT");
    walletFile.setCrypto(crypto);

    WalletFile.CipherParams cipherParams = new WalletFile.CipherParams();
    cipherParams.setIv("IV");
    crypto.setCipherparams(cipherParams);

    crypto.setKdf("KDF");
    WalletFile.ScryptKdfParams kdfParams = new WalletFile.ScryptKdfParams();
    kdfParams.setDklen(32);
    kdfParams.setN(1);
    kdfParams.setP(10);
    kdfParams.setR(100);
    kdfParams.setSalt("SALT");
    crypto.setKdfparams(kdfParams);

    crypto.setMac("MAC");
    walletFile.setCrypto(crypto);
    walletFile.setId("cab06c9e-79a9-48ea-afc7-d3bdb3a59526");
    walletFile.setVersion(1);

    web3j.parityNewAccountFromWallet(walletFile, "password").send();

    //CHECKSTYLE:OFF
    verifyResult("{\"jsonrpc\":\"2.0\",\"method\":\"parity_newAccountFromWallet\",\"params\":[{\"address\":\"0x...\",\"id\":\"cab06c9e-79a9-48ea-afc7-d3bdb3a59526\",\"version\":1,\"crypto\":{\"cipher\":\"CIPHER\",\"ciphertext\":\"CIPHERTEXT\",\"cipherparams\":{\"iv\":\"IV\"},\"kdf\":\"KDF\",\"kdfparams\":{\"dklen\":32,\"n\":1,\"p\":10,\"r\":100,\"salt\":\"SALT\"},\"mac\":\"MAC\"}},\"password\"],\"id\":1}");
    //CHECKSTYLE:ON
}
 
Example 18
private void createWallet(String passphrase) {
    if (isAdded()) {
        showProgress(getString(R.string.generating_wallet));
    }
    walletManager.createWallet(passphrase, new WalletCreationCallback() {
        @Override
        public void onWalletCreated(WalletFile walletFile) {
            closeProgress();
            openUserWalletFragment();
        }
    });
}
 
Example 19
private void createWallet(String passphrase) {
    walletManager.createWallet(passphrase, new WalletCreationCallback() {
        @Override
        public void onWalletCreated(WalletFile walletFile) {
            closeProgress();
            showExistingWallet();

        }
    });
    if (isAdded()) {
        showProgress(getString(R.string.generating_wallet));
    }
}
 
Example 20
public void createWallet2(String passphrase, final Runnable callback) {
    ECKeyPair keyPair = null;
    try {
        keyPair = Keys.createEcKeyPair();
    } catch (InvalidAlgorithmParameterException | NoSuchAlgorithmException | NoSuchProviderException e) {
        e.printStackTrace();
    }
    WalletCreationTask2 task = new WalletCreationTask2(new WalletCreationCallback() {
        @Override
        public void onWalletCreated(WalletFile walletFile) {
            callback.run();
        }
    }, keyPair);
    task.execute(passphrase);
}
 
Example 21
public void restoreFromBlock0(String privateKey, final Runnable callback) {
    credentials = Credentials.create(privateKey);
    WalletCreationTask task = new WalletCreationTask(new WalletCreationCallback() {
        @Override
        public void onWalletCreated(WalletFile walletFile) {
            callback.run();
        }
    }, credentials.getEcKeyPair());
    task.execute(BLOCK);
}
 
Example 22
public void restoreFromBlock2(String privateKey, final Runnable callback) {
    credentials = Credentials.create(privateKey);
    WalletCreationTask2 task = new WalletCreationTask2(new WalletCreationCallback() {
        @Override
        public void onWalletCreated(WalletFile walletFile) {
            callback.run();
        }
    }, credentials.getEcKeyPair());
    task.execute(BLOCK);
}
 
Example 23
public void restoreFromBlock2Json(Uri uri, String pwd, final Runnable callback) {
    loadFromJson(uri, pwd);
    if (credentials == null) {
        callback.run();
        return;
    }
    WalletCreationTask2 task = new WalletCreationTask2(new WalletCreationCallback() {
        @Override
        public void onWalletCreated(WalletFile walletFile) {
            callback.run();
        }
    }, credentials.getEcKeyPair());
    task.execute(BLOCK);
}
 
Example 24
@Override
protected WalletFile doInBackground(String... params) {

        /* any key in friendly format*/
    key = keyPair.getPrivateKey().toString(16);
    //sharedManager.setLastSyncedBlock(Coders.encodeBase64(key));
    return generateWallet(keyPair, params[0]);
}
 
Example 25
private WalletFile generateWallet(@NonNull ECKeyPair keyPair, String password) {
    WalletFile wallet = null;
    try {
        wallet = Wallet.createLight(password, keyPair);
        walletFriendlyAddress = context.getString(R.string.hex_marker) + wallet.getAddress();
        walletAddress = wallet.getAddress();
    } catch (CipherException e) {
        e.printStackTrace();
    }

    return wallet;
}
 
Example 26
@Override
protected void onPostExecute(WalletFile walletFile) {
    currentWallet = walletFile;
    if (callback != null) {
        callback.onWalletCreated(walletFile);
    }
}
 
Example 27
@Override
protected WalletFile doInBackground(String... params) {

        /* any key in friendly format*/
    key = keyPair.getPrivateKey().toString(16);
    sharedManager.setLastSyncedBlock(Coders.encodeBase64(key));
    return generateWallet(keyPair, params[0]);
}
 
Example 28
private WalletFile generateWallet(@NonNull ECKeyPair keyPair, String password) {
    WalletFile wallet = null;
    try {
        wallet = Wallet.createLight(password, keyPair);
        walletFriendlyAddress = context.getString(R.string.hex_marker) + wallet.getAddress();
        walletAddress = wallet.getAddress();
    } catch (CipherException e) {
        e.printStackTrace();
    }

    return wallet;
}
 
Example 29
@Override
protected void onPostExecute(WalletFile walletFile) {
    currentWallet = walletFile;
    if (callback != null) {
        callback.onWalletCreated(walletFile);
    }
}
 
Example 30
private void createWallet(String passphrase) {
    Log.d("psd", "createWallet start");
    if (isAdded()) {
        showProgress(getString(R.string.generating_wallet));
    }

    walletManager.createWallet(passphrase, new WalletCreationCallback() {
        @Override
        public void onWalletCreated(WalletFile walletFile) {
            closeProgress();
            openUserWalletFragment();
            Log.d("psd", "createWallet end");
        }
    });
}