org.web3j.crypto.WalletFile Java Examples

The following examples show how to use org.web3j.crypto.WalletFile. 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 File: TransactionHistoryFragment.java    From guarda-android-wallets with GNU General Public License v3.0 6 votes vote down vote up
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 #2
Source File: WalletManager.java    From guarda-android-wallets with GNU General Public License v3.0 6 votes vote down vote up
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 #3
Source File: TransactionHistoryFragment.java    From guarda-android-wallets with GNU General Public License v3.0 6 votes vote down vote up
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 #4
Source File: WalletManager.java    From guarda-android-wallets with GNU General Public License v3.0 6 votes vote down vote up
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 #5
Source File: TransactionHistoryFragment.java    From guarda-android-wallets with GNU General Public License v3.0 6 votes vote down vote up
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 #6
Source File: InitWalletManager.java    From dapp-wallet-demo with 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 #7
Source File: InitWalletManager.java    From dapp-wallet-demo with 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 #8
Source File: InitWalletManager.java    From dapp-wallet-demo with 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 #9
Source File: InitWalletManager.java    From dapp-wallet-demo with 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 #10
Source File: ImportWalletViewModel.java    From alpha-wallet-android with 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 #11
Source File: WalletManager.java    From guarda-android-wallets with GNU General Public License v3.0 6 votes vote down vote up
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 #12
Source File: OwnWalletUtils.java    From Lunary-Ethereum-Wallet with 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 #13
Source File: OwnWalletUtils.java    From bcm-android with 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 #14
Source File: LWallet.java    From dapp-wallet-demo with Apache License 2.0 5 votes vote down vote up
static void validate(WalletFile walletFile) throws CipherException {
    WalletFile.Crypto crypto = walletFile.getCrypto();

    if (walletFile.getVersion() != CURRENT_VERSION) {
        throw new CipherException("Wallet version is not supported");
    }

    if (!crypto.getCipher().equals(CIPHER)) {
        throw new CipherException("Wallet cipher is not supported");
    }

    if (!crypto.getKdf().equals(AES_128_CTR) && !crypto.getKdf().equals(SCRYPT)) {
        throw new CipherException("KDF type is not supported");
    }
}
 
Example #15
Source File: WalletManager.java    From guarda-android-wallets with GNU General Public License v3.0 5 votes vote down vote up
@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 #16
Source File: GethKeystoreAccountService.java    From trust-wallet-android-source with GNU General Public License v3.0 5 votes vote down vote up
@Override
public Single<Wallet> importPrivateKey(String privateKey, String newPassword) {
    return Single.fromCallable(() -> {
        BigInteger key = new BigInteger(privateKey, PRIVATE_KEY_RADIX);
        ECKeyPair keypair = ECKeyPair.create(key);
        WalletFile walletFile = create(newPassword, keypair, N, P);
        return new ObjectMapper().writeValueAsString(walletFile);
    }).compose(upstream -> importKeystore(upstream.blockingGet(), newPassword, newPassword));
}
 
Example #17
Source File: JsonRpc2_0Parity.java    From web3j with Apache License 2.0 5 votes vote down vote up
@Override
public Request<?, NewAccountIdentifier> parityNewAccountFromWallet(
        WalletFile walletFile, String password) {
    return new Request<>(
            "parity_newAccountFromWallet",
            Arrays.asList(walletFile, password),
            web3jService,
            NewAccountIdentifier.class);
}
 
Example #18
Source File: InitWalletManager.java    From dapp-wallet-demo with Apache License 2.0 5 votes vote down vote up
/**
 * @param context   app context 上下文
 * @param password  the wallet password(not the bip39 password) 钱包密码(而不是BIP39的密码)
 * @param mnemonics 助记词
 * @return wallet 钱包
 */
public Flowable<HLWallet> generateWallet(Context context,
                                         String password,
                                         String mnemonics) {
    Flowable<String> flowable = Flowable.just(mnemonics);

    return flowable
            .map(s -> {
                ECKeyPair keyPair = generateKeyPair(s);
                WalletFile walletFile = Wallet.createLight(password, keyPair);
                HLWallet hlWallet = new HLWallet(walletFile);
                WalletManager.shared().saveWallet(context, hlWallet);
                return hlWallet;
            });
}
 
Example #19
Source File: ETHWalletUtils.java    From Upchain-wallet with GNU Affero General Public License v3.0 5 votes vote down vote up
/**
 * 导出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 #20
Source File: WalletManager.java    From guarda-android-wallets with GNU General Public License v3.0 5 votes vote down vote up
@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 #21
Source File: SendTransaction.java    From BitcoinWallet with MIT License 5 votes vote down vote up
public void sendTransaction(String fromAddress, String toAddress, BigDecimal amount, String password, WalletFile walletfile) throws Exception {
    BigInteger mNonce = getNonce(fromAddress);
    BigInteger gasPrice = getGasPrice();
    BigInteger value = Convert.toWei(amount, Convert.Unit.ETHER).toBigInteger();
    Transaction transaction = Transaction.createEtherTransaction(fromAddress, null,
            null, null, toAddress, value);
    BigInteger gasLimit = getGasLimit(transaction);
    String sign = new TransactionManager().signedEthTransactionData(toAddress, mNonce, gasPrice,
            gasLimit, amount, walletfile, password);
    sendTransaction(sign);

}
 
Example #22
Source File: WalletManager.java    From guarda-android-wallets with GNU General Public License v3.0 5 votes vote down vote up
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 #23
Source File: WalletManager.java    From guarda-android-wallets with GNU General Public License v3.0 5 votes vote down vote up
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 #24
Source File: WalletManager.java    From guarda-android-wallets with GNU General Public License v3.0 5 votes vote down vote up
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 #25
Source File: WalletManager.java    From guarda-android-wallets with GNU General Public License v3.0 5 votes vote down vote up
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 #26
Source File: UserWalletFragment.java    From guarda-android-wallets with GNU General Public License v3.0 5 votes vote down vote up
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 #27
Source File: TransactionHistoryFragment.java    From guarda-android-wallets with GNU General Public License v3.0 5 votes vote down vote up
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 #28
Source File: WalletManager.java    From guarda-android-wallets with GNU General Public License v3.0 5 votes vote down vote up
@Override
protected void onPostExecute(WalletFile walletFile) {
    currentWallet = walletFile;
    if (callback != null) {
        callback.onWalletCreated(walletFile);
    }
}
 
Example #29
Source File: WalletManager.java    From guarda-android-wallets with GNU General Public License v3.0 5 votes vote down vote up
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 #30
Source File: RequestTest.java    From web3j with Apache License 2.0 5 votes vote down vote up
@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();

    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}");
}