Package com.bitsofproof.supernode.common

Examples of com.bitsofproof.supernode.common.ValidationException


  protected Transaction createSpend (List<TransactionOutput> sources, List<TransactionSink> sinks, long fee) throws ValidationException
  {
    if ( fee < 0 || fee > MAXIMUM_FEE )
    {
      throw new ValidationException ("You unlikely want to do that");
    }
    Transaction transaction = new Transaction ();
    transaction.setInputs (new ArrayList<TransactionInput> ());
    transaction.setOutputs (new ArrayList<TransactionOutput> ());

    long sumOut = 0;
    for ( TransactionSink s : sinks )
    {
      TransactionOutput o = new TransactionOutput ();
      o.setValue (s.getValue ());
      sumOut += s.getValue ();
      o.setScript (s.getAddress ().getAddressScript ());

      transaction.getOutputs ().add (o);
    }

    long sumInput = 0;
    for ( TransactionOutput o : sources )
    {
      TransactionInput i = new TransactionInput ();
      i.setSourceHash (o.getTxHash ());
      i.setIx (o.getIx ());
      sumInput += o.getValue ();

      transaction.getInputs ().add (i);
    }
    if ( sumInput != (sumOut + fee) )
    {
      throw new ValidationException ("Sum of sinks (+fee) does not match sum of sources");
    }

    int j = 0;
    for ( TransactionOutput s : sources )
    {
      TransactionInput i = transaction.getInputs ().get (j);
      ScriptFormat.Writer sw = new ScriptFormat.Writer ();
      if ( ScriptFormat.isPayToAddress (s.getScript ()) )
      {
        Address address = s.getOutputAddress ();
        Key key = getKeyForAddress (address);
        if ( key == null )
        {
          throw new ValidationException ("Have no key to spend this output");
        }
        byte[] sig = key.sign (hashTransaction (transaction, j, ScriptFormat.SIGHASH_ALL, s.getScript ()));
        byte[] sigPlusType = new byte[sig.length + 1];
        System.arraycopy (sig, 0, sigPlusType, 0, sig.length);
        sigPlusType[sigPlusType.length - 1] = (byte) (ScriptFormat.SIGHASH_ALL & 0xff);
View Full Code Here


    return transaction;
  }

  protected void spendNonAddressOutput (int ix, TransactionOutput source, ScriptFormat.Writer writer, Transaction transaction) throws ValidationException
  {
    throw new ValidationException ("Can not spend this output type");
  }
View Full Code Here

      }
      log.trace ("pay " + amount + (senderPaysFee ? " + " + fee : ""));
      List<TransactionOutput> sources = getSufficientSources (amount, senderPaysFee ? fee : 0, null);
      if ( sources == null )
      {
        throw new ValidationException ("Insufficient funds to pay " + amount + (senderPaysFee ? " + " + fee : ""));
      }
      long in = 0;
      for ( TransactionOutput o : sources )
      {
        log.trace ("using input " + o.getTxHash () + "[" + o.getIx () + "] " + o.getValue ());
View Full Code Here

    }
    else if ( (store[0] & 0xff) == 0x01 )
    {
      if ( passphrase == null )
      {
        throw new ValidationException ("Need passphrase");
      }
      return parseBIP38 (store);
    }

    throw new ValidationException ("invalid key");
  }
View Full Code Here

  private ECKeyPair parseBIP38 (byte[] store) throws ValidationException
  {
    if ( store.length != 43 )
    {
      throw new ValidationException ("invalid key length for BIP38");
    }
    boolean ec = false;
    boolean compressed = false;
    boolean hasLot = false;
    if ( (store[1] & 0xff) == 0x42 )
    {
      if ( (store[2] & 0xff) == 0xc0 )
      {
        // non-EC-multiplied keys without compression (prefix 6PR)
      }
      else if ( (store[2] & 0xff) == 0xe0 )
      {
        // non-EC-multiplied keys with compression (prefix 6PY)
        compressed = true;
      }
      else
      {
        throw new ValidationException ("invalid key");
      }
    }
    else if ( (store[1] & 0xff) == 0x43 )
    {
      // EC-multiplied keys without compression (prefix 6Pf)
      // EC-multiplied keys with compression (prefix 6Pn)
      ec = true;
      compressed = (store[2] & 0x20) != 0;
      hasLot = (store[2] & 0x04) != 0;
      if ( (store[2] & 0x24) != store[2] )
      {
        throw new ValidationException ("invalid key");
      }
    }
    else
    {
      throw new ValidationException ("invalid key");
    }

    byte[] checksum = new byte[4];
    System.arraycopy (store, store.length - 4, checksum, 0, 4);
    byte[] ekey = new byte[store.length - 4];
    System.arraycopy (store, 0, ekey, 0, store.length - 4);
    byte[] hash = Hash.hash (ekey);
    for ( int i = 0; i < 4; ++i )
    {
      if ( hash[i] != checksum[i] )
      {
        throw new ValidationException ("checksum mismatch");
      }
    }

    if ( ec == false )
    {
View Full Code Here

      byte[] acs = Hash.hash (new Address (network, kp.getAddress ()).toString ().getBytes ("US-ASCII"));
      byte[] check = new byte[4];
      System.arraycopy (acs, 0, check, 0, 4);
      if ( !Arrays.equals (check, addressHash) )
      {
        throw new ValidationException ("failed to decrpyt");
      }
      return kp;
    }
    catch ( UnsupportedEncodingException e )
    {
      throw new ValidationException (e);
    }
    catch ( NoSuchAlgorithmException e )
    {
      throw new ValidationException (e);
    }
    catch ( NoSuchPaddingException e )
    {
      throw new ValidationException (e);
    }
    catch ( InvalidKeyException e )
    {
      throw new ValidationException (e);
    }
    catch ( IllegalBlockSizeException e )
    {
      throw new ValidationException (e);
    }
    catch ( BadPaddingException e )
    {
      throw new ValidationException (e);
    }
    catch ( NoSuchProviderException e )
    {
      throw new ValidationException (e);
    }
  }
View Full Code Here

      byte[] acs = Hash.hash (new Address (network, kp.getAddress ()).toString ().getBytes ("US-ASCII"));
      byte[] check = new byte[4];
      System.arraycopy (acs, 0, check, 0, 4);
      if ( !Arrays.equals (check, addressHash) )
      {
        throw new ValidationException ("failed to decrpyt");
      }
      return kp;
    }
    catch ( UnsupportedEncodingException e )
    {
      throw new ValidationException (e);
    }
    catch ( NoSuchAlgorithmException e )
    {
      throw new ValidationException (e);
    }
    catch ( NoSuchProviderException e )
    {
      throw new ValidationException (e);
    }
    catch ( NoSuchPaddingException e )
    {
      throw new ValidationException (e);
    }
    catch ( InvalidKeyException e )
    {
      throw new ValidationException (e);
    }
    catch ( IllegalBlockSizeException e )
    {
      throw new ValidationException (e);
    }
    catch ( BadPaddingException e )
    {
      throw new ValidationException (e);
    }
  }
View Full Code Here

  private byte[] bytesBIP38 (Key key) throws ValidationException
  {
    if ( passphrase == null )
    {
      throw new ValidationException ("Must have passphrase to encrypt keys");
    }
    byte[] store = new byte[43];
    store[0] = 0x01;
    store[1] = 0x42;
    store[2] = key.isCompressed () ? (byte) 0xe0 : (byte) 0xc0;
View Full Code Here

  {
    StringTokenizer tokenizer = new StringTokenizer (mnemonic);
    int nt = tokenizer.countTokens ();
    if ( nt % 6 != 0 )
    {
      throw new ValidationException ("invalid mnemonic - word cound not divisible by 6");
    }
    boolean[] bits = new boolean[11 * nt];
    int i = 0;
    while ( tokenizer.hasMoreElements () )
    {
      int c = Arrays.binarySearch (english, tokenizer.nextToken ());
      for ( int j = 0; j < 11; ++j )
      {
        bits[i++] = (c & (1 << (10 - j))) > 0;
      }
    }
    byte[] data = new byte[bits.length / 33 * 4];
    for ( i = 0; i < bits.length / 33 * 32; ++i )
    {
      data[i / 8] |= (bits[i] ? 1 : 0) << (7 - (i % 8));
    }
    byte[] check = Hash.sha256 (data);
    for ( i = bits.length / 33 * 32; i < bits.length; ++i )
    {
      if ( (check[(i - bits.length / 33 * 32) / 8] & (1 << (7 - (i % 8))) ^ (bits[i] ? 1 : 0) << (7 - (i % 8))) != 0 )
      {
        throw new ValidationException ("invalid mnemonic - checksum failed");
      }
    }
    try
    {
      SecretKey seedkey = new SecretKeySpec (("mnemonic" + passphrase).getBytes ("UTF-8"), "Blowfish");
      Cipher cipher = Cipher.getInstance ("BlowFish/ECB/NoPadding", "BC");
      cipher.init (Cipher.DECRYPT_MODE, seedkey);
      for ( i = 0; i < 1000; ++i )
      {
        data = cipher.doFinal (data);
      }
    }
    catch ( UnsupportedEncodingException | NoSuchAlgorithmException |
        NoSuchProviderException | NoSuchPaddingException | InvalidKeyException | IllegalBlockSizeException | BadPaddingException e )
    {
      throw new ValidationException ("can not decrypt mnemonic", e);
    }
    return data;
  }
View Full Code Here

  public static String encode (byte[] data, String passphrase) throws ValidationException
  {
    if ( data.length % 8 != 0 )
    {
      throw new ValidationException ("can nor encode - data length not divisible with 8");
    }
    try
    {
      SecretKey seedkey = new SecretKeySpec (("mnemonic" + passphrase).getBytes ("UTF-8"), "Blowfish");
      Cipher cipher = Cipher.getInstance ("BlowFish/ECB/NoPadding", "BC");
      cipher.init (Cipher.ENCRYPT_MODE, seedkey);
      for ( int i = 0; i < 1000; ++i )
      {
        data = cipher.doFinal (data);
      }
    }
    catch ( UnsupportedEncodingException | NoSuchAlgorithmException |
        NoSuchProviderException | NoSuchPaddingException | InvalidKeyException | IllegalBlockSizeException | BadPaddingException e )
    {
      throw new ValidationException ("can not decrypt mnemonic", e);
    }
    return getMnemonic (data);
  }
View Full Code Here

TOP

Related Classes of com.bitsofproof.supernode.common.ValidationException

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.