private static void deleteKey() { String pubkey = (String) options.valueOf("pubkey"); String addr = (String) options.valueOf("addr"); if (pubkey == null && addr == null) { System.err.println("One of --pubkey or --addr must be specified."); return; } ECKey key = null; if (pubkey != null) { key = wallet.findKeyFromPubKey(Hex.decode(pubkey)); } else { try { Address address = new Address(wallet.getParams(), addr); key = wallet.findKeyFromPubHash(address.getHash160()); } catch (AddressFormatException e) { System.err.println( addr + " does not parse as a Bitcoin address of the right network parameters."); return; } } if (key == null) { System.err.println("Wallet does not seem to contain that key."); return; } wallet.removeKey(key); }
private static void addKey() { ECKey key; long creationTimeSeconds = getCreationTimeSeconds(); if (options.has("privkey")) { String data = (String) options.valueOf("privkey"); if (data.startsWith("5J") || data.startsWith("5H") || data.startsWith("5K")) { DumpedPrivateKey dpk; try { dpk = new DumpedPrivateKey(params, data); } catch (AddressFormatException e) { System.err.println("Could not parse dumped private key " + data); return; } key = dpk.getKey(); } else { byte[] decode = Utils.parseAsHexOrBase58(data); if (decode == null) { System.err.println("Could not understand --privkey as either hex or base58: " + data); return; } key = new ECKey(new BigInteger(1, decode)); } if (options.has("pubkey")) { // Give the user a hint. System.out.println("You don't have to specify --pubkey when a private key is supplied."); } key.setCreationTimeSeconds(creationTimeSeconds); } else if (options.has("pubkey")) { byte[] pubkey = Utils.parseAsHexOrBase58((String) options.valueOf("pubkey")); key = new ECKey(null, pubkey); key.setCreationTimeSeconds(creationTimeSeconds); } else { // Freshly generated key. key = new ECKey(); if (creationTimeSeconds > 0) key.setCreationTimeSeconds(creationTimeSeconds); } if (wallet.findKeyFromPubKey(key.getPubKey()) != null) { System.err.println("That key already exists in this wallet."); return; } try { if (wallet.isEncrypted()) { if (password == null || !wallet.checkPassword(password)) { System.err.println("The password is incorrect."); return; } key = key.encrypt(wallet.getKeyCrypter(), wallet.getKeyCrypter().deriveKey(password)); } wallet.addKey(key); } catch (KeyCrypterException kce) { System.err.println( "There was an encryption related error when adding the key. The error was '" + kce.getMessage() + "'."); } System.out.println(key.toAddress(params) + " " + key); }