/* [receipt, blockHash, index] */
  public byte[] getEncoded() {

    byte[] receiptRLP = this.receipt.getEncoded();
    byte[] blockHashRLP = RLP.encodeElement(blockHash);
    byte[] indexRLP = RLP.encodeInt(index);

    byte[] rlpEncoded = RLP.encodeList(receiptRLP, blockHashRLP, indexRLP);

    return rlpEncoded;
  }
 public byte[] getEncoded() {
   if (rlpEncoded == null) {
     byte[] nonce = RLP.encodeBigInteger(this.nonce);
     byte[] balance = RLP.encodeBigInteger(this.balance);
     byte[] stateRoot = RLP.encodeElement(this.stateRoot);
     byte[] codeHash = RLP.encodeElement(this.codeHash);
     this.rlpEncoded = RLP.encodeList(nonce, balance, stateRoot, codeHash);
   }
   return rlpEncoded;
 }
  public byte[] encode() {

    byte[] publicKey = new byte[64];
    System.arraycopy(ephemeralPublicKey.getEncoded(false), 1, publicKey, 0, publicKey.length);

    byte[] publicBytes = RLP.encode(publicKey);
    byte[] nonceBytes = RLP.encode(nonce);
    byte[] versionBytes = RLP.encodeInt(version);

    return RLP.encodeList(publicBytes, nonceBytes, versionBytes);
  }
Exemple #4
0
  @Override
  public void parse(byte[] data) {
    RLPList list = (RLPList) RLP.decode2OneItem(data, 0);

    this.token = list.get(0).getRLPData();
    RLPItem expires = (RLPItem) list.get(1);
    this.expires = ByteUtil.byteArrayToLong(expires.getRLPData());
  }
Exemple #5
0
  static PongMessage create(byte[] token, ECKey privKey, long expiration) {

    /* RLP Encode data */
    byte[] rlpToken = RLP.encodeElement(token);
    byte[] rlpExp = RLP.encodeElement(ByteUtil.longToBytes(expiration));

    byte[] type = new byte[] {2};
    byte[] data = RLP.encodeList(rlpToken, rlpExp);

    PongMessage pong = new PongMessage();
    pong.encode(type, data, privKey);

    pong.token = token;
    pong.expires = expiration;

    return pong;
  }
Exemple #6
0
 public static byte[] rlpEncodeList(Object... elems) {
   byte[][] encodedElems = new byte[elems.length][];
   for (int i = 0; i < elems.length; i++) {
     if (elems[i] instanceof Byte) {
       encodedElems[i] = RLP.encodeByte((Byte) elems[i]);
     } else if (elems[i] instanceof Integer) {
       encodedElems[i] = RLP.encodeInt((Integer) elems[i]);
     } else if (elems[i] instanceof Long) {
       encodedElems[i] = rlpEncodeLong((Long) elems[i]);
     } else if (elems[i] instanceof String) {
       encodedElems[i] = RLP.encodeString((String) elems[i]);
     } else if (elems[i] instanceof byte[]) {
       encodedElems[i] = ((byte[]) elems[i]);
     } else {
       throw new RuntimeException("Unsupported object: " + elems[i]);
     }
   }
   return RLP.encodeList(encodedElems);
 }
  public TransactionInfo(byte[] rlp) {
    RLPList params = RLP.decode2(rlp);
    RLPList txInfo = (RLPList) params.get(0);
    RLPList receiptRLP = (RLPList) txInfo.get(0);
    RLPItem blockHashRLP = (RLPItem) txInfo.get(1);
    RLPItem indexRLP = (RLPItem) txInfo.get(2);

    receipt = new TransactionReceipt(receiptRLP.getRLPData());
    blockHash = blockHashRLP.getRLPData();
    if (indexRLP.getRLPData() == null) index = 0;
    else index = new BigInteger(1, indexRLP.getRLPData()).intValue();
  }
Exemple #8
0
  public static PongMessage create(byte[] token, String host, int port, ECKey privKey) {

    long expiration = 90 * 60 + System.currentTimeMillis() / 1000;

    byte[] rlpIp = RLP.encodeElement(host.getBytes());

    byte[] tmpPort = longToBytes(port);
    byte[] rlpPort = RLP.encodeElement(stripLeadingZeroes(tmpPort));
    byte[] rlpToList = RLP.encodeList(rlpIp, rlpPort, rlpPort);

    /* RLP Encode data */
    byte[] rlpToken = RLP.encodeElement(token);
    byte[] tmpExp = longToBytes(expiration);
    byte[] rlpExp = RLP.encodeElement(stripLeadingZeroes(tmpExp));

    byte[] type = new byte[] {2};
    byte[] data = RLP.encodeList(rlpToList, rlpToken, rlpExp);

    PongMessage pong = new PongMessage();
    pong.encode(type, data, privKey);

    pong.token = token;
    pong.expires = expiration;

    return pong;
  }
  @Override
  public byte[] getStorageHash() {

    SecureTrie storageTrie = new SecureTrie(null);

    for (DataWord key : storage.keySet()) {

      DataWord value = storage.get(key);

      storageTrie.update(key.getData(), RLP.encodeElement(value.getNoLeadZeroesData()));
    }

    return storageTrie.getRootHash();
  }
  public AccountState(byte[] rlpData) {
    this.rlpEncoded = rlpData;

    RLPList items = (RLPList) RLP.decode2(rlpEncoded).get(0);
    this.nonce =
        items.get(0).getRLPData() == null
            ? BigInteger.ZERO
            : new BigInteger(1, items.get(0).getRLPData());
    this.balance =
        items.get(1).getRLPData() == null
            ? BigInteger.ZERO
            : new BigInteger(1, items.get(1).getRLPData());
    this.stateRoot = items.get(2).getRLPData();
    this.codeHash = items.get(3).getRLPData();
  }
Exemple #11
0
  /** For signatures you have to keep also RLP of the transaction without any signature data */
  public byte[] getEncodedRaw() {

    if (!parsed) rlpParse();
    if (rlpRaw != null) return rlpRaw;

    // parse null as 0 for nonce
    byte[] nonce = null;
    if (this.nonce == null || this.nonce.length == 1 && this.nonce[0] == 0) {
      nonce = RLP.encodeElement(null);
    } else {
      nonce = RLP.encodeElement(this.nonce);
    }
    byte[] gasPrice = RLP.encodeElement(this.gasPrice);
    byte[] gasLimit = RLP.encodeElement(this.gasLimit);
    byte[] receiveAddress = RLP.encodeElement(this.receiveAddress);
    byte[] value = RLP.encodeElement(this.value);
    byte[] data = RLP.encodeElement(this.data);

    rlpRaw = RLP.encodeList(nonce, gasPrice, gasLimit, receiveAddress, value, data);
    return rlpRaw;
  }
  @Override
  public void decode(byte[] rlpCode) {
    RLPList data = RLP.decode2(rlpCode);
    RLPList rlpList = (RLPList) data.get(0);

    RLPList keys = (RLPList) rlpList.get(0);
    RLPList values = (RLPList) rlpList.get(1);
    RLPElement code = rlpList.get(2);

    for (int i = 0; i < keys.size(); ++i) {

      RLPItem key = (RLPItem) keys.get(i);
      RLPItem value = (RLPItem) values.get(i);

      storage.put(new DataWord(key.getRLPData()), new DataWord(value.getRLPData()));
    }

    this.code = (code.getRLPData() == null) ? ByteUtil.EMPTY_BYTE_ARRAY : code.getRLPData();
  }
  static AuthResponseMessageV4 decode(byte[] wire) {

    AuthResponseMessageV4 message = new AuthResponseMessageV4();

    RLPList params = (RLPList) RLP.decode2OneItem(wire, 0);

    byte[] pubKeyBytes = params.get(0).getRLPData();

    byte[] bytes = new byte[65];
    System.arraycopy(pubKeyBytes, 0, bytes, 1, 64);
    bytes[0] = 0x04; // uncompressed
    message.ephemeralPublicKey = ECKey.CURVE.getCurve().decodePoint(bytes);

    message.nonce = params.get(1).getRLPData();

    byte[] versionBytes = params.get(2).getRLPData();
    message.version = ByteUtil.byteArrayToInt(versionBytes);

    return message;
  }
Exemple #14
0
  public void rlpParse() {

    RLPList decodedTxList = RLP.decode2(rlpEncoded);
    RLPList transaction = (RLPList) decodedTxList.get(0);

    this.nonce = transaction.get(0).getRLPData();
    this.gasPrice = transaction.get(1).getRLPData();
    this.gasLimit = transaction.get(2).getRLPData();
    this.receiveAddress = transaction.get(3).getRLPData();
    this.value = transaction.get(4).getRLPData();
    this.data = transaction.get(5).getRLPData();
    // only parse signature in case tx is signed
    if (transaction.get(6).getRLPData() != null) {
      byte v = transaction.get(6).getRLPData()[0];
      byte[] r = transaction.get(7).getRLPData();
      byte[] s = transaction.get(8).getRLPData();
      this.signature = ECDSASignature.fromComponents(r, s, v);
    } else {
      logger.debug("RLP encoded tx is not signed!");
    }
    this.parsed = true;
    this.hash = getHash();
  }
  @Override
  public byte[] getEncoded() {

    byte[][] keys = new byte[storage.size()][];
    byte[][] values = new byte[storage.size()][];

    int i = 0;
    for (DataWord key : storage.keySet()) {

      DataWord value = storage.get(key);

      keys[i] = RLP.encodeElement(key.getData());
      values[i] = RLP.encodeElement(value.getNoLeadZeroesData());

      ++i;
    }

    byte[] rlpKeysList = RLP.encodeList(keys);
    byte[] rlpValuesList = RLP.encodeList(values);
    byte[] rlpCode = RLP.encodeElement(code);

    return RLP.encodeList(rlpKeysList, rlpValuesList, rlpCode);
  }
Exemple #16
0
  public byte[] getEncoded() {

    if (rlpEncoded != null) return rlpEncoded;

    // parse null as 0 for nonce
    byte[] nonce = null;
    if (this.nonce == null || this.nonce.length == 1 && this.nonce[0] == 0) {
      nonce = RLP.encodeElement(null);
    } else {
      nonce = RLP.encodeElement(this.nonce);
    }
    byte[] gasPrice = RLP.encodeElement(this.gasPrice);
    byte[] gasLimit = RLP.encodeElement(this.gasLimit);
    byte[] receiveAddress = RLP.encodeElement(this.receiveAddress);
    byte[] value = RLP.encodeElement(this.value);
    byte[] data = RLP.encodeElement(this.data);

    byte[] v, r, s;

    if (signature != null) {
      v = RLP.encodeByte(signature.v);
      r = RLP.encodeElement(BigIntegers.asUnsignedByteArray(signature.r));
      s = RLP.encodeElement(BigIntegers.asUnsignedByteArray(signature.s));
    } else {
      v = RLP.encodeElement(EMPTY_BYTE_ARRAY);
      r = RLP.encodeElement(EMPTY_BYTE_ARRAY);
      s = RLP.encodeElement(EMPTY_BYTE_ARRAY);
    }

    this.rlpEncoded =
        RLP.encodeList(nonce, gasPrice, gasLimit, receiveAddress, value, data, v, r, s);

    this.hash = this.getHash();

    return rlpEncoded;
  }
Exemple #17
0
 public static byte[] rlpEncodeLong(long n) {
   // TODO for now leaving int cast
   return RLP.encodeInt((int) n);
 }