Java Code Examples for org.ethereum.util.RLP#encodeList()

The following examples show how to use org.ethereum.util.RLP#encodeList() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: Transaction.java    From ethereumj with MIT License 6 votes vote down vote up
/**
   *  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);

this.rlpRaw = RLP.encodeList(nonce, gasPrice, gasLimit, receiveAddress,
		value, data);
      return rlpRaw;
  }
 
Example 2
Source File: ContractDetails.java    From ethereumj with MIT License 6 votes vote down vote up
public byte[] getEncoded() {

		if (rlpEncoded == null) {

			int size = storageKeys == null ? 0 : storageKeys.size();

			byte[][] keys = new byte[size][];
			byte[][] values = new byte[size][];

			for (int i = 0; i < size; ++i) {
				DataWord key = storageKeys.get(i);
				keys[i] = RLP.encodeElement(key.getData());
			}
			for (int i = 0; i < size; ++i) {
				DataWord value = storageValues.get(i);
				values[i] = RLP.encodeElement(value.getNoLeadZeroesData());
			}

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

			this.rlpEncoded = RLP.encodeList(rlpKeysList, rlpValuesList, rlpCode);
		}
		return rlpEncoded;
	}
 
Example 3
Source File: Transaction.java    From wkcwallet-java with Apache License 2.0 6 votes vote down vote up
public byte[] getEncodedRaw() {
    if(!this.parsed) {
        this.rlpParse();
    }

    if(this.rlpRaw != null) {
        return this.rlpRaw;
    } else {
        byte[] nonce = null;
        if(this.nonce != null && (this.nonce.length != 1 || this.nonce[0] != 0)) {
            nonce = RLP.encodeElement(this.nonce);
        } else {
            nonce = RLP.encodeElement((byte[])null);
        }

        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);
        this.rlpRaw = RLP.encodeList(new byte[][]{nonce, gasPrice, gasLimit, receiveAddress, value, data});
        return this.rlpRaw;
    }
}
 
Example 4
Source File: GetBlocksMessage.java    From ethereumj with MIT License 5 votes vote down vote up
private void encode() {
	List<byte[]> encodedElements = new ArrayList<>();
	encodedElements.add(RLP.encodeByte(GET_BLOCKS.asByte()));
	for (byte[] hash : blockHashes)
		encodedElements.add(RLP.encodeElement(hash));
	byte[][] encodedElementArray = encodedElements
			.toArray(new byte[encodedElements.size()][]);
	this.encoded = RLP.encodeList(encodedElementArray);
}
 
Example 5
Source File: Block.java    From ethereumj with MIT License 5 votes vote down vote up
public byte[] getEncodedWithoutNonce() {
	if (!parsed) parseRLP();
	byte[] header = this.header.getEncodedWithoutNonce();
       byte[] transactions = RLP.encodeList();
       byte[] uncles = RLP.encodeList();
       
       return RLP.encodeList(header, transactions, uncles);
}
 
Example 6
Source File: PeersMessage.java    From ethereumj with MIT License 5 votes vote down vote up
private void encode() {
	byte[][] encodedByteArrays = new byte[this.peers.size() + 1][];
	encodedByteArrays[0] = RLP.encodeByte(this.getCommand().asByte());
	List<Peer> peerList = new ArrayList<>(this.peers);
	for (int i = 0; i < peerList.size(); i++) {
		encodedByteArrays[i + 1] = peerList.get(i).getEncoded();
	}
	this.encoded = RLP.encodeList(encodedByteArrays);
}
 
Example 7
Source File: TransactionsMessage.java    From ethereumj with MIT License 5 votes vote down vote up
private void encode() {
  	List<byte[]> encodedElements = new ArrayList<>();
  	encodedElements.add(RLP.encodeByte(TRANSACTIONS.asByte()));
  	for (Transaction tx : transactions)
          encodedElements.add(tx.getEncoded());
byte[][] encodedElementArray = encodedElements
		.toArray(new byte[encodedElements.size()][]);
      this.encoded = RLP.encodeList(encodedElementArray);
  }
 
Example 8
Source File: BlocksMessage.java    From ethereumj with MIT License 5 votes vote down vote up
private void encode() {

        List<byte[]> encodedElements = new Vector<>();
        encodedElements.add(RLP.encodeByte(BLOCKS.asByte()));

        for (Block block : blocks){
            encodedElements.add(block.getEncoded());
        }

        byte[][] encodedElementArray = encodedElements
                .toArray(new byte[encodedElements.size()][]);

        this.encoded = RLP.encodeList(encodedElementArray);
    }
 
Example 9
Source File: Transaction.java    From ethereumj with MIT License 5 votes vote down vote up
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(new byte[0]);
        	r = RLP.encodeElement(new byte[0]);
        	s = RLP.encodeElement(new byte[0]);
        }

		this.rlpEncoded = RLP.encodeList(nonce, gasPrice, gasLimit,
				receiveAddress, value, data, v, r, s);
        return rlpEncoded;
    }
 
Example 10
Source File: BlockHashesMessage.java    From ethereumj with MIT License 5 votes vote down vote up
private void encode() {
	List<byte[]> encodedElements = new ArrayList<>();
	encodedElements.add(RLP.encodeByte(BLOCK_HASHES.asByte()));
	for (byte[] blockHash : blockHashes)
		encodedElements.add(RLP.encodeElement(blockHash));
	byte[][] encodedElementArray = encodedElements
			.toArray(new byte[encodedElements.size()][]);
	this.encoded = RLP.encodeList(encodedElementArray);
}
 
Example 11
Source File: StatusMessage.java    From ethereumj with MIT License 5 votes vote down vote up
private void encode() {
    byte[] command			= RLP.encodeByte(STATUS.asByte());
    byte[] protocolVersion	= RLP.encodeByte(this.protocolVersion);
    byte[] networkId		= RLP.encodeByte(this.networkId);
    byte[] totalDifficulty	= RLP.encodeElement(this.totalDifficulty);
    byte[] bestHash			= RLP.encodeElement(this.bestHash);
    byte[] genesisHash		= RLP.encodeElement(this.genesisHash);

    this.encoded = RLP.encodeList(command, protocolVersion, networkId,
            totalDifficulty, bestHash, genesisHash);
}
 
Example 12
Source File: JournalSource.java    From nuls-v2 with MIT License 5 votes vote down vote up
public byte[] serialize() {
    byte[][] insertedBytes = new byte[insertedKeys.size()][];
    for (int i = 0; i < insertedBytes.length; i++) {
        insertedBytes[i] = RLP.encodeElement(insertedKeys.get(i));
    }
    byte[][] deletedBytes = new byte[deletedKeys.size()][];
    for (int i = 0; i < deletedBytes.length; i++) {
        deletedBytes[i] = RLP.encodeElement(deletedKeys.get(i));
    }
    return RLP.encodeList(RLP.encodeElement(updateHash),
            RLP.encodeList(insertedBytes), RLP.encodeList(deletedBytes));
}
 
Example 13
Source File: Block.java    From nuls with MIT License 5 votes vote down vote up
public byte[] getEncoded() {
    if (rlpEncoded == null) {
        byte[] header = this.header.getEncoded();

        List<byte[]> block = getBodyElements();
        block.add(0, header);
        byte[][] elements = block.toArray(new byte[block.size()][]);

        this.rlpEncoded = RLP.encodeList(elements);
    }
    return rlpEncoded;
}
 
Example 14
Source File: JournalSource.java    From nuls with MIT License 5 votes vote down vote up
public byte[] serialize() {
    byte[][] insertedBytes = new byte[insertedKeys.size()][];
    for (int i = 0; i < insertedBytes.length; i++) {
        insertedBytes[i] = RLP.encodeElement(insertedKeys.get(i));
    }
    byte[][] deletedBytes = new byte[deletedKeys.size()][];
    for (int i = 0; i < deletedBytes.length; i++) {
        deletedBytes[i] = RLP.encodeElement(deletedKeys.get(i));
    }
    return RLP.encodeList(RLP.encodeElement(updateHash),
            RLP.encodeList(insertedBytes), RLP.encodeList(deletedBytes));
}
 
Example 15
Source File: Transaction.java    From wkcwallet-java with Apache License 2.0 5 votes vote down vote up
public byte[] getEncoded() {
    if(this.rlpEncoded != null) {
        return this.rlpEncoded;
    } else {
        byte[] nonce = null;
        if(this.nonce != null && (this.nonce.length != 1 || this.nonce[0] != 0)) {
            nonce = RLP.encodeElement(this.nonce);
        } else {
            nonce = RLP.encodeElement((byte[])null);
        }

        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;
        byte[] r;
        byte[] s;
        if(this.signature != null) {
            v = RLP.encodeByte(this.signature.v);
            r = RLP.encodeElement(BigIntegers.asUnsignedByteArray(this.signature.r));
            s = RLP.encodeElement(BigIntegers.asUnsignedByteArray(this.signature.s));
        } else {
            v = RLP.encodeElement(ByteUtil.EMPTY_BYTE_ARRAY);
            r = RLP.encodeElement(ByteUtil.EMPTY_BYTE_ARRAY);
            s = RLP.encodeElement(ByteUtil.EMPTY_BYTE_ARRAY);
        }

        this.rlpEncoded = RLP.encodeList(new byte[][]{nonce, gasPrice, gasLimit, receiveAddress, value, data, v, r, s});
        this.hash = this.getHash();
        return this.rlpEncoded;
    }
}
 
Example 16
Source File: AccountState.java    From nuls-v2 with MIT License 5 votes vote down vote up
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);
        byte[] owner = RLP.encodeElement(this.owner);
        this.rlpEncoded = RLP.encodeList(nonce, balance, stateRoot, codeHash, owner);
    }
    return rlpEncoded;
}
 
Example 17
Source File: BlockHeader.java    From nuls-v2 with MIT License 4 votes vote down vote up
public byte[] getEncoded(boolean withNonce) {
    byte[] parentHash = RLP.encodeElement(this.parentHash);
    byte[] hash = RLP.encodeElement(this.hash);
    byte[] number = RLP.encodeBigInteger(BigInteger.valueOf(this.number));
    return RLP.encodeList(parentHash, hash, number);
}
 
Example 18
Source File: BlockHeader.java    From nuls with MIT License 4 votes vote down vote up
public byte[] getEncoded(boolean withNonce) {
    byte[] parentHash = RLP.encodeElement(this.parentHash);
    byte[] hash = RLP.encodeElement(this.hash);
    byte[] number = RLP.encodeBigInteger(BigInteger.valueOf(this.number));
    return RLP.encodeList(parentHash, hash, number);
}
 
Example 19
Source File: GetBlockHashesMessage.java    From ethereumj with MIT License 4 votes vote down vote up
private void encode() {
	byte[] command = RLP.encodeByte(GET_BLOCK_HASHES.asByte());
	byte[] hash = RLP.encodeElement(this.bestHash);
	byte[] maxBlocks = RLP.encodeInt(this.maxBlocks);
	this.encoded = RLP.encodeList(command, hash, maxBlocks);
}
 
Example 20
Source File: TransactionReceipt.java    From ethereumj with MIT License 3 votes vote down vote up
public byte[] getEncoded() {

        if(rlpEncoded != null) return rlpEncoded;

        byte[] transactionEl   = transaction.getEncoded();
        byte[] postTxStateEl   = RLP.encodeElement(this.postTxState);
        byte[] cumulativeGasEl = RLP.encodeElement(this.cumulativeGas);

        rlpEncoded = RLP.encodeList(transactionEl, postTxStateEl, cumulativeGasEl);

        return rlpEncoded;
    }