/**
  * Creates the initial multisig contract and incomplete refund transaction which can be requested
  * at the appropriate time using {@link PaymentChannelClientState#getIncompleteRefundTransaction}
  * and {@link PaymentChannelClientState#getMultisigContract()}. The way the contract is crafted
  * can be adjusted by overriding {@link
  * PaymentChannelClientState#editContractSendRequest(com.google.bitcoin.core.Wallet.SendRequest)}.
  * By default unconfirmed coins are allowed to be used, as for micropayments the risk should be
  * relatively low.
  *
  * @throws ValueOutOfRangeException if the value being used is too small to be accepted by the
  *     network
  * @throws InsufficientMoneyException if the wallet doesn't contain enough balance to initiate
  */
 public synchronized void initiate() throws ValueOutOfRangeException, InsufficientMoneyException {
   final NetworkParameters params = wallet.getParams();
   Transaction template = new Transaction(params);
   // We always place the client key before the server key because, if either side wants some
   // privacy, they can
   // use a fresh key for the the multisig contract and nowhere else
   List<ECKey> keys = Lists.newArrayList(myKey, serverMultisigKey);
   // There is also probably a change output, but we don't bother shuffling them as it's obvious
   // from the
   // format which one is the change. If we start obfuscating the change output better in future
   // this may
   // be worth revisiting.
   TransactionOutput multisigOutput =
       template.addOutput(totalValue, ScriptBuilder.createMultiSigOutputScript(2, keys));
   if (multisigOutput.getMinNonDustValue().compareTo(totalValue) > 0)
     throw new ValueOutOfRangeException("totalValue too small to use");
   Wallet.SendRequest req = Wallet.SendRequest.forTx(template);
   req.coinSelector = AllowUnconfirmedCoinSelector.get();
   editContractSendRequest(req);
   req.shuffleOutputs = false; // TODO: Fix things so shuffling is usable.
   wallet.completeTx(req);
   Coin multisigFee = req.tx.getFee();
   multisigContract = req.tx;
   // Build a refund transaction that protects us in the case of a bad server that's just trying to
   // cause havoc
   // by locking up peoples money (perhaps as a precursor to a ransom attempt). We time lock it so
   // the server
   // has an assurance that we cannot take back our money by claiming a refund before the channel
   // closes - this
   // relies on the fact that since Bitcoin 0.8 time locked transactions are non-final. This will
   // need to change
   // in future as it breaks the intended design of timelocking/tx replacement, but for now it
   // simplifies this
   // specific protocol somewhat.
   refundTx = new Transaction(params);
   refundTx
       .addInput(multisigOutput)
       .setSequenceNumber(0); // Allow replacement when it's eventually reactivated.
   refundTx.setLockTime(expiryTime);
   if (totalValue.compareTo(Coin.CENT) < 0) {
     // Must pay min fee.
     final Coin valueAfterFee = totalValue.subtract(Transaction.REFERENCE_DEFAULT_MIN_TX_FEE);
     if (Transaction.MIN_NONDUST_OUTPUT.compareTo(valueAfterFee) > 0)
       throw new ValueOutOfRangeException("totalValue too small to use");
     refundTx.addOutput(valueAfterFee, myKey.toAddress(params));
     refundFees = multisigFee.add(Transaction.REFERENCE_DEFAULT_MIN_TX_FEE);
   } else {
     refundTx.addOutput(totalValue, myKey.toAddress(params));
     refundFees = multisigFee;
   }
   refundTx.getConfidence().setSource(TransactionConfidence.Source.SELF);
   log.info(
       "initiated channel with multi-sig contract {}, refund {}",
       multisigContract.getHashAsString(),
       refundTx.getHashAsString());
   state = State.INITIATED;
   // Client should now call getIncompleteRefundTransaction() and send it to the server.
 }
 @Before
 public void setUp() throws Exception {
   BriefLogFormatter.initVerbose();
   Context ctx = new Context(params);
   myWatchedKey = new ECKey();
   myWallet = new Wallet(params);
   myKey = new ECKey();
   myKey.setCreationTimeSeconds(123456789L);
   myWallet.importKey(myKey);
   myAddress = myKey.toAddress(params);
   myWallet = new Wallet(params);
   myWallet.importKey(myKey);
   mScriptCreationTime = new Date().getTime() / 1000 - 1234;
   myWallet.addWatchedAddress(myWatchedKey.toAddress(params), mScriptCreationTime);
   myWallet.setDescription(WALLET_DESCRIPTION);
 }
Пример #3
0
 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);
 }
 private synchronized Transaction makeUnsignedChannelContract(Coin valueToMe)
     throws ValueOutOfRangeException {
   Transaction tx = new Transaction(wallet.getParams());
   tx.addInput(multisigContract.getOutput(0));
   // Our output always comes first.
   // TODO: We should drop myKey in favor of output key + multisig key separation
   // (as its always obvious who the client is based on T2 output order)
   tx.addOutput(valueToMe, myKey.toAddress(wallet.getParams()));
   return tx;
 }
 @Test
 public void testKeys() throws Exception {
   for (int i = 0; i < 20; i++) {
     myKey = new ECKey();
     myAddress = myKey.toAddress(params);
     myWallet = new Wallet(params);
     myWallet.importKey(myKey);
     Wallet wallet1 = roundTrip(myWallet);
     assertArrayEquals(
         myKey.getPubKey(), wallet1.findKeyFromPubHash(myKey.getPubKeyHash()).getPubKey());
     assertArrayEquals(
         myKey.getPrivKeyBytes(),
         wallet1.findKeyFromPubHash(myKey.getPubKeyHash()).getPrivKeyBytes());
   }
 }
  public void setUp(BlockStore blockStore) throws Exception {
    BriefLogFormatter.init();

    unitTestParams = UnitTestParams.get();
    Wallet.SendRequest.DEFAULT_FEE_PER_KB = BigInteger.ZERO;
    this.blockStore = blockStore;
    wallet = new Wallet(unitTestParams);
    key = new ECKey();
    address = key.toAddress(unitTestParams);
    wallet.addKey(key);
    blockChain = new BlockChain(unitTestParams, wallet, blockStore);

    startPeerServers();
    if (clientType == ClientType.NIO_CLIENT_MANAGER
        || clientType == ClientType.BLOCKING_CLIENT_MANAGER) {
      channels.startAsync();
      channels.awaitRunning();
    }

    socketAddress = new InetSocketAddress("127.0.0.1", 1111);
  }
 @Test
 public void empty() throws Exception {
   // Check the base case of a wallet with one key and no transactions.
   Wallet wallet1 = roundTrip(myWallet);
   assertEquals(0, wallet1.getTransactions(true).size());
   assertEquals(Coin.ZERO, wallet1.getBalance());
   assertArrayEquals(
       myKey.getPubKey(), wallet1.findKeyFromPubHash(myKey.getPubKeyHash()).getPubKey());
   assertArrayEquals(
       myKey.getPrivKeyBytes(),
       wallet1.findKeyFromPubHash(myKey.getPubKeyHash()).getPrivKeyBytes());
   assertEquals(
       myKey.getCreationTimeSeconds(),
       wallet1.findKeyFromPubHash(myKey.getPubKeyHash()).getCreationTimeSeconds());
   assertEquals(mScriptCreationTime, wallet1.getWatchedScripts().get(0).getCreationTimeSeconds());
   assertEquals(1, wallet1.getWatchedScripts().size());
   assertEquals(
       ScriptBuilder.createOutputScript(myWatchedKey.toAddress(params)),
       wallet1.getWatchedScripts().get(0));
   assertEquals(WALLET_DESCRIPTION, wallet1.getDescription());
 }
Пример #8
0
 public Address genAddress() {
   ECKey key = new ECKey();
   localWallet.addKey(key);
   return key.toAddress(Craftcoinish.network);
 }