Exemplo n.º 1
0
  public Collection<String> getAllAddresses(Transaction tx, boolean confirmed) {
    HashSet<String> lst = new HashSet<String>();

    for (TransactionInput in : tx.getInputs()) {
      Address a = getAddressForInput(in, confirmed);
      if (a != null) lst.add(a.toString());
    }

    for (TransactionOutput out : tx.getOutputs()) {
      Address a = getAddressForOutput(out);
      if (a != null) lst.add(a.toString());
    }

    return lst;
  }
Exemplo n.º 2
0
 @Test
 public void testScriptSig() throws Exception {
   byte[] sigProgBytes = Hex.decode(sigProg);
   Script script = new Script(sigProgBytes);
   // Test we can extract the from address.
   byte[] hash160 = Utils.sha256hash160(script.getPubKey());
   Address a = new Address(params, hash160);
   assertEquals("mkFQohBpy2HDXrCwyMrYL5RtfrmeiuuPY2", a.toString());
 }
Exemplo n.º 3
0
 @Test
 public void testScriptPubKey() throws Exception {
   // Check we can extract the to address
   byte[] pubkeyBytes = Hex.decode(pubkeyProg);
   Script pubkey = new Script(pubkeyBytes);
   assertEquals(
       "DUP HASH160 [33e81a941e64cda12c6a299ed322ddbdd03f8d0e] EQUALVERIFY CHECKSIG",
       pubkey.toString());
   Address toAddr = new Address(params, pubkey.getPubKeyHash());
   assertEquals("mkFQohBpy2HDXrCwyMrYL5RtfrmeiuuPY2", toAddr.toString());
 }
Exemplo n.º 4
0
  public void sweepKey(ECKey key, long fee, int accountId, JSONArray outputs) {
    mLogger.info("sweepKey starting");

    mLogger.info("key addr " + key.toAddress(mParams).toString());

    Transaction tx = new Transaction(mParams);

    long balance = 0;
    ArrayList<Script> scripts = new ArrayList<Script>();
    try {
      for (int ii = 0; ii < outputs.length(); ++ii) {
        JSONObject output;
        output = outputs.getJSONObject(ii);

        String tx_hash = output.getString("tx_hash");
        int tx_output_n = output.getInt("tx_output_n");
        String script = output.getString("script");

        // Reverse byte order, create hash.
        Sha256Hash hash = new Sha256Hash(WalletUtil.msgHexToBytes(tx_hash));

        tx.addInput(
            new TransactionInput(
                mParams, tx, new byte[] {}, new TransactionOutPoint(mParams, tx_output_n, hash)));

        scripts.add(new Script(Hex.decode(script)));

        balance += output.getLong("value");
      }
    } catch (JSONException e) {
      e.printStackTrace();
      throw new RuntimeException("trouble parsing unspent outputs");
    }

    // Compute balance - fee.
    long amount = balance - fee;
    mLogger.info(String.format("sweeping %d", amount));

    // Figure out the destination address.
    Address to = mHDWallet.nextReceiveAddress(accountId);
    mLogger.info("sweeping to " + to.toString());

    // Add output.
    tx.addOutput(BigInteger.valueOf(amount), to);

    WalletUtil.signTransactionInputs(tx, Transaction.SigHash.ALL, key, scripts);

    mLogger.info("tx bytes: " + new String(Hex.encode(tx.bitcoinSerialize())));
    // mKit.peerGroup().broadcastTransaction(tx);
    broadcastTransaction(mKit.peerGroup(), tx);

    mLogger.info("sweepKey finished");
  }
Exemplo n.º 5
0
  public static void main(String[] args) throws AddressFormatException {

    Node lenode = Node.getInstance();
    NodeWallet lewallet = lenode.getWallet();
    BigInteger leinteger = BigInteger.valueOf(20000000);
    System.out.print(leinteger);
    NetworkParameters netParams = NetworkParameters.testNet();
    // leaddress.equals("mipcBbFg9gMiCh81Kj8tqqdgoZub1ZJRfn");
    Address targetAddress = new Address(netParams, "mipcBbFg9gMiCh81Kj8tqqdgoZub1ZJRfn");
    // Address.getParametersFromAddress("mipcBbFg9gMiCh81Kj8tqqdgoZub1ZJRfn");
    lewallet.setBalance(targetAddress, leinteger);
    // BigInteger result = lewallet.getBalance(leaddress);
    System.out.print(targetAddress.toString());
  }
Exemplo n.º 6
0
  public Address determineSelectedAddress() {
    final String selectedAddress = prefs.getString(Constants.PREFS_KEY_SELECTED_ADDRESS, null);

    Address firstAddress = null;
    for (final ECKey key : wallet.getKeys()) {
      if (!wallet.isKeyRotating(key)) {
        final Address address = key.toAddress(Constants.NETWORK_PARAMETERS);

        if (address.toString().equals(selectedAddress)) return address;

        if (firstAddress == null) firstAddress = address;
      }
    }

    return firstAddress;
  }
Exemplo n.º 7
0
  public boolean onCommand(CommandSender arg0, Command arg1, String arg2, String[] arg3) {
    if (!arg0.hasPermission("satoshis.withdraw")) {
      error("You do not have permission for this command!", arg0);
      return true;
    }

    if (arg0 instanceof Player) {
      Player player = (Player) arg0;

      // Withdraw exact amount
      if (arg3.length == 2) {
        try {
          Address withdrawTo = new Address(Satoshis.network, arg3[0]);
          double withdraw = Double.parseDouble(arg3[1]);
          if (!Satoshis.econ.hasMoney(player.getName(), Satoshis.minWithdraw)) {
            error(
                "Oops! You must have "
                    + Satoshis.econ.formatValue(Satoshis.minWithdraw, false)
                    + " to withdraw!",
                arg0);
            return true;
          }
          if (!Satoshis.econ.hasMoney(player.getName(), withdraw)) {
            error("Oops! You cannot withdraw more money than you have!", arg0);
            return true;
          }
          if (!Satoshis.salesTax && !player.getName().equalsIgnoreCase(Satoshis.owner)) {
            Satoshis.bapi.localSendCoins(
                withdrawTo, withdraw - Satoshis.econ.priceOfTax(withdraw) - BitcoinAPI.minBitFee);
          } else {
            Satoshis.bapi.localSendCoins(withdrawTo, withdraw - BitcoinAPI.minBitFee);
          }
          action(
              "Sending "
                  + Satoshis.econ.formatValue(withdraw - BitcoinAPI.minBitFee, false)
                  + " to address "
                  + withdrawTo.toString()
                  + " sucessfully!",
              arg0);
          Satoshis.econ.subFunds(player.getName(), withdraw + BitcoinAPI.minBitFee);
        } catch (WrongNetworkException e) {
          error("Oops! That address was for the TestNet!", arg0);
        } catch (AddressFormatException e) {
          error("Oops! Is that the correct address?", arg0);
        } catch (NumberFormatException e) {
          error("Syntax: /withdraw <address> [amount]", arg0);
          error("Amount must be a number!", arg0);
        }
      } else if (arg3.length == 1) {
        try {
          Address withdrawTo = new Address(Satoshis.network, arg3[0]);
          double withdraw = Satoshis.econ.getMoney(player.getName());
          if (withdraw == 0 + BitcoinAPI.minBitFee) {
            error("Oops! You have no money in your account!", arg0);
            return true;
          }
          if (!Satoshis.salesTax && !player.getName().equalsIgnoreCase(Satoshis.owner)) {
            Satoshis.bapi.localSendCoins(
                withdrawTo, withdraw - Satoshis.econ.priceOfTax(withdraw) - BitcoinAPI.minBitFee);
          } else {
            Satoshis.bapi.localSendCoins(withdrawTo, withdraw - BitcoinAPI.minBitFee);
          }
          action(
              "Sending "
                  + Satoshis.econ.formatValue(withdraw - BitcoinAPI.minBitFee, false)
                  + " to address "
                  + withdrawTo.toString()
                  + " sucessfully!",
              arg0);

          Satoshis.econ.subFunds(player.getName(), withdraw);
        } catch (WrongNetworkException e) {
          error("Oops! That address was for the TestNet!", arg0);
        } catch (AddressFormatException e) {
          error("Oops! Is that the correct address?", arg0);
        } catch (NumberFormatException e) {
          error("Syntax: /withdraw <address> [amount]", arg0);
          error("Amount must be a number!", arg0);
        }
      } else {
        error("Syntax: /withdraw <address> [amount]", arg0);
      }
    }

    return true;
  }