Exemple #1
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 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;
  }