Java源码示例:org.web3j.crypto.WalletFile
示例1
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;
}
示例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);
}
}
});
}
}
}
示例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();
}
}
示例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");
}
});
}
}
}
示例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();
}
}
示例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);
}
}
});
}
}
}
示例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();
}
}
示例8
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);
});
}
示例9
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);
});
}
示例10
/**
* 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);
});
}
示例11
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);
});
}
示例12
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;
});
}
示例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;
}
示例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;
}
示例15
@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"));
}
示例16
@Override
public Request<?, NewAccountIdentifier> parityNewAccountFromWallet(
WalletFile walletFile, String password) {
return new Request<>(
"parity_newAccountFromWallet",
Arrays.asList(walletFile, password),
web3jService,
NewAccountIdentifier.class);
}
示例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
}
示例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();
}
});
}
示例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));
}
}
示例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);
}
示例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);
}
示例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);
}
示例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);
}
示例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]);
}
示例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;
}
示例26
@Override
protected void onPostExecute(WalletFile walletFile) {
currentWallet = walletFile;
if (callback != null) {
callback.onWalletCreated(walletFile);
}
}
示例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]);
}
示例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;
}
示例29
@Override
protected void onPostExecute(WalletFile walletFile) {
currentWallet = walletFile;
if (callback != null) {
callback.onWalletCreated(walletFile);
}
}
示例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");
}
});
}