org.web3j.crypto.ECKeyPair Java Examples

The following examples show how to use org.web3j.crypto.ECKeyPair. 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: 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 #2
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 #3
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 #4
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 #5
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 #6
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 #7
Source File: ImportWalletActivity.java    From alpha-wallet-android with MIT License 6 votes vote down vote up
@Override
public void onPrivateKey(String privateKey)
{
    try
    {
        BigInteger key = new BigInteger(privateKey, 16);
        if (!WalletUtils.isValidPrivateKey(privateKey)) throw new Exception(getString(R.string.invalid_private_key));
        ECKeyPair keypair = ECKeyPair.create(key);
        String address = Numeric.prependHexPrefix(Keys.getAddress(keypair));

        if (importWalletViewModel.keystoreExists(address))
        {
            queryReplaceWalletPrivateKey(address);
        }
        else
        {
            importWalletViewModel.importPrivateKeyWallet(address, this, this);
        }
    }
    catch (Exception e)
    {
        keyImportError(e.getMessage());
    }
}
 
Example #8
Source File: TestKeycardCommandSet.java    From status-keycard with Apache License 2.0 6 votes vote down vote up
/**
 * Sends a LOAD KEY APDU. The key is sent in TLV format, includes the public key and no chain code, meaning that
 * the card will not be able to do further key derivation. This is needed when the argument is an EC keypair from
 * the web3j package instead of the regular Java ones. Used by the test which actually submits the transaction to
 * the network.
 *
 * @param ecKeyPair a key pair
 * @return the raw card response
 * @throws IOException communication error
 */
public APDUResponse loadKey(ECKeyPair ecKeyPair) throws IOException {
  byte[] publicKey = ecKeyPair.getPublicKey().toByteArray();
  byte[] privateKey = ecKeyPair.getPrivateKey().toByteArray();

  int pubLen = publicKey.length;
  int pubOff = 0;

  if(publicKey[0] == 0x00) {
    pubOff++;
    pubLen--;
  }

  byte[] ansiPublic = new byte[pubLen + 1];
  ansiPublic[0] = 0x04;
  System.arraycopy(publicKey, pubOff, ansiPublic, 1, pubLen);

  return loadKey(ansiPublic, privateKey, null);
}
 
Example #9
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 #10
Source File: GetCredentials.java    From Android-Wallet-Token-ERC20 with Apache License 2.0 5 votes vote down vote up
public Credentials FromSeed(String seedCode,
                            String passwordWallet) throws UnreadableWalletException {
    DeterministicSeed seed = new DeterministicSeed(seedCode, null, passwordWallet, 1409478661L);
    DeterministicKeyChain chain = DeterministicKeyChain.builder().seed(seed).build();
    List<ChildNumber> keyPath = HDUtils.parsePath("M/44H/60H/0H/0/0");
    DeterministicKey key = chain.getKeyByPath(keyPath, true);
    BigInteger privKey = key.getPrivKey();
    return Credentials.create(ECKeyPair.create(privKey));
}
 
Example #11
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 #12
Source File: OwnWalletUtils.java    From Lunary-Ethereum-Wallet with GNU General Public License v3.0 5 votes vote down vote up
public static String generateNewWalletFile(
        String password, File destinationDirectory, boolean useFullScrypt)
        throws CipherException, IOException, InvalidAlgorithmParameterException,
        NoSuchAlgorithmException, NoSuchProviderException {

    ECKeyPair ecKeyPair = Keys.createEcKeyPair();
    return generateWalletFile(password, ecKeyPair, destinationDirectory, useFullScrypt);
}
 
Example #13
Source File: OwnWalletUtils.java    From bcm-android with GNU General Public License v3.0 5 votes vote down vote up
public static String generateNewWalletFile(
        String password, File destinationDirectory, boolean useFullScrypt)
        throws CipherException, IOException, InvalidAlgorithmParameterException,
        NoSuchAlgorithmException, NoSuchProviderException {

    ECKeyPair ecKeyPair = Keys.createEcKeyPair();
    return generateWalletFile(password, ecKeyPair, destinationDirectory, useFullScrypt);
}
 
Example #14
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 #15
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 #16
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 #17
Source File: WalletManager.java    From guarda-android-wallets with GNU General Public License v3.0 5 votes vote down vote up
public void saveJsonLight() {
    try {
        BigInteger b = new BigInteger(Coders.decodeBase64(sharedManager.getLastSyncedBlock()), 16);
        String filename1 = WalletUtils.generateWalletFile("", ECKeyPair.create(b), com.google.common.io.Files.createTempDir(), false);
    } catch (Exception e) {
        e.printStackTrace();
    }
}
 
Example #18
Source File: WalletManager.java    From guarda-android-wallets with GNU General Public License v3.0 5 votes vote down vote up
private void restoreWalletFromFile(String walletPath, String pass, WalletCreationCallback callback) {
    Credentials credentials = null;
    try {
        credentials = WalletUtils.loadCredentials(pass, walletPath);
    } catch (IOException | CipherException e) {
        e.printStackTrace();
    }

    if (credentials != null) {
        ECKeyPair keyPair = credentials.getEcKeyPair();
        WalletCreationTask task = new WalletCreationTask(callback, keyPair);
        task.execute(pass);
    }
}
 
Example #19
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 #20
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 #21
Source File: TransferTest.java    From client-sdk-java with Apache License 2.0 5 votes vote down vote up
@Test
    public void transfer() {

        String fromAddress = Keys.getAddress(ECKeyPair.create(Numeric.toBigIntNoPrefix("a689f0879f53710e9e0c1025af410a530d6381eebb5916773195326e123b822b")));
        String toAddress = Keys.getAddress(ECKeyPair.create(Numeric.toBigIntNoPrefix("6fe419582271a4dcf01c51b89195b77b228377fde4bde6e04ef126a0b4373f79")));
//        String toAddress = Keys.getAddress(ECKeyPair.create(Numeric.toBigIntNoPrefix("0ba4d3bbca0f664fa5230fe6e980927b093dd68892dc55283266962ffe8c03af")));
////
        String hash = sendTransaction("a689f0879f53710e9e0c1025af410a530d6381eebb5916773195326e123b822b", toAddress, new BigDecimal("6000000000000000000000000"), 50000000000000L, 210000L);
//
        try {
            PlatonGetTransactionReceipt platonGetTransactionReceipt =  web3j.platonGetTransactionReceipt(hash).send();
            PlatonGetBalance platonGetBalance = web3j.platonGetBalance("0x" + toAddress, DefaultBlockParameterName.LATEST).send();
            PlatonGetBalance platonGetBalance2 = web3j.platonGetBalance("0x" + fromAddress, DefaultBlockParameterName.LATEST).send();
//
            System.out.println(platonGetBalance.getBalance());
            System.out.println(platonGetBalance2.getBalance());
        } catch (IOException e) {
            e.printStackTrace();
        }
//
//        try {
//            PlatonBlock platonBlock = web3j.platonGetBlockByNumber(DefaultBlockParameterName.LATEST, false).send();
//            System.out.println(platonBlock.getBlock().getNumberRaw());
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
    }
 
Example #22
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 #23
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 #24
Source File: WalletManager.java    From guarda-android-wallets with GNU General Public License v3.0 5 votes vote down vote up
private void restoreWalletFromFile(String walletPath, String pass, WalletCreationCallback callback) {
    Credentials credentials = null;
    try {
        credentials = WalletUtils.loadCredentials(pass, walletPath);
    } catch (IOException | CipherException e) {
        e.printStackTrace();
    }

    if (credentials != null) {
        ECKeyPair keyPair = credentials.getEcKeyPair();
        WalletCreationTask task = new WalletCreationTask(callback, keyPair);
        task.execute(pass);
    }
}
 
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 saveJsonLight() {
    try {
        BigInteger b = new BigInteger(Coders.decodeBase64(sharedManager.getLastSyncedBlock()), 16);
        String filename1 = WalletUtils.generateWalletFile("", ECKeyPair.create(b), com.google.common.io.Files.createTempDir(), false);
    } catch (Exception e) {
        e.printStackTrace();
    }
}
 
Example #26
Source File: WalletManager.java    From guarda-android-wallets with GNU General Public License v3.0 5 votes vote down vote up
private void restoreWalletFromFile(String walletPath, String pass, WalletCreationCallback callback) {
    Credentials credentials = null;
    try {
        credentials = WalletUtils.loadCredentials(pass, walletPath);
    } catch (IOException | CipherException e) {
        e.printStackTrace();
    }

    if (credentials != null) {
        ECKeyPair keyPair = credentials.getEcKeyPair();
        WalletCreationTask task = new WalletCreationTask(callback, keyPair);
        task.execute(pass);
    }
}
 
Example #27
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 #28
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 #29
Source File: WalletManager.java    From guarda-android-wallets with GNU General Public License v3.0 5 votes vote down vote up
public void saveJsonLight() {
    try {
        BigInteger b = new BigInteger(Coders.decodeBase64(sharedManager.getLastSyncedBlock()), 16);
        String filename1 = WalletUtils.generateWalletFile("", ECKeyPair.create(b), com.google.common.io.Files.createTempDir(), false);
    } catch (Exception e) {
        e.printStackTrace();
    }
}
 
Example #30
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;
}