public void setCumulativeGas(long cumulativeGas) { this.cumulativeGas = BigIntegers.asUnsignedByteArray(BigInteger.valueOf(cumulativeGas)); rlpEncoded = null; }
/** * @deprecated use {@link Transaction#create(String, BigInteger, BigInteger, BigInteger, BigInteger, Integer)} instead */ public static Transaction create(String to, BigInteger amount, BigInteger nonce, BigInteger gasPrice, BigInteger gasLimit){ return new Transaction(BigIntegers.asUnsignedByteArray(nonce), BigIntegers.asUnsignedByteArray(gasPrice), BigIntegers.asUnsignedByteArray(gasLimit), Hex.decode(to), BigIntegers.asUnsignedByteArray(amount), null); }
@Override public int hashCode() { return Arrays.hashCode(getHash()); } }
public byte[] calcPowValue() { // nonce bytes are expected in Little Endian order, reverting byte[] nonceReverted = Arrays.reverse(nonce); byte[] hashWithoutNonce = HashUtil.sha3(getEncodedWithoutNonce()); byte[] seed = Arrays.concatenate(hashWithoutNonce, nonceReverted); byte[] seedHash = HashUtil.sha512(seed); byte[] concat = Arrays.concatenate(seedHash, mixHash); return HashUtil.sha3(concat); }
private static BlockInfo getBlockInfoForHash(List<BlockInfo> blocks, byte[] hash){ for (BlockInfo blockInfo : blocks) if (areEqual(hash, blockInfo.getHash())) return blockInfo; return null; }
/** * Pack nibbles to binary * * @param nibbles sequence. may have a terminator * @return hex-encoded byte array */ public static byte[] packNibbles(byte[] nibbles) { int terminator = 0; if (nibbles[nibbles.length - 1] == TERMINATOR) { terminator = 1; nibbles = copyOf(nibbles, nibbles.length - 1); } int oddlen = nibbles.length % 2; int flag = 2 * terminator + oddlen; if (oddlen != 0) { byte[] flags = new byte[]{(byte) flag}; nibbles = concatenate(flags, nibbles); } else { byte[] flags = new byte[]{(byte) flag, 0}; nibbles = concatenate(flags, nibbles); } ByteArrayOutputStream buffer = new ByteArrayOutputStream(); for (int i = 0; i < nibbles.length; i += 2) { buffer.write(16 * nibbles[i] + nibbles[i + 1]); } return buffer.toByteArray(); }
public final int[] calcDatasetItem(final int[] cache, final int i) { final int r = params.getHASH_BYTES() / params.getWORD_BYTES(); final int n = cache.length / r; int[] mix = Arrays.copyOfRange(cache, i % n * r, (i % n + 1) * r); mix[0] = i ^ mix[0]; mix = sha512(mix, false); final int dsParents = (int) params.getDATASET_PARENTS(); final int mixLen = mix.length; for (int j = 0; j < dsParents; j++) { int cacheIdx = fnv(i ^ j, mix[j % r]); cacheIdx = remainderUnsigned(cacheIdx, n); int off = cacheIdx * r; for (int k = 0; k < mixLen; k++) { mix[k] = fnv(mix[k], cache[off + k]); } } return sha512(mix, false); }
public static Transaction create(String to, BigInteger amount, BigInteger nonce, BigInteger gasPrice, BigInteger gasLimit, Integer chainId){ return new Transaction(BigIntegers.asUnsignedByteArray(nonce), BigIntegers.asUnsignedByteArray(gasPrice), BigIntegers.asUnsignedByteArray(gasLimit), Hex.decode(to), BigIntegers.asUnsignedByteArray(amount), null, chainId); }
public void setGasUsed(long gasUsed) { this.gasUsed = BigIntegers.asUnsignedByteArray(BigInteger.valueOf(gasUsed)); rlpEncoded = null; }
/** * check if param block is son of this block * * @param block - possible a son of this * @return - true if this block is parent of param block */ public boolean isParentOf(Block block) { return Arrays.areEqual(this.getHash(), block.getParentHash()); }
/** * Integer limitation goes up to 2^31-1 so length can never be bigger than MAX_ITEM_LENGTH */ public static byte[] encodeLength(int length, int offset) { if (length < SIZE_THRESHOLD) { byte firstByte = (byte) (length + offset); return new byte[]{firstByte}; } else if (length < MAX_ITEM_LENGTH) { byte[] binaryLength; if (length > 0xFF) binaryLength = intToBytesNoLeadZeroes(length); else binaryLength = new byte[]{(byte) length}; byte firstByte = (byte) (binaryLength.length + offset + SIZE_THRESHOLD - 1); return concatenate(new byte[]{firstByte}, binaryLength); } else { throw new RuntimeException("Input too long"); } }
public byte[] getPowBoundary() { return BigIntegers.asUnsignedByteArray(32, BigInteger.ONE.shiftLeft(256).divide(getDifficultyBI())); }
public boolean isEqual(Block block) { return Arrays.areEqual(this.getHash(), block.getHash()); }
public static byte[] encodeBigInteger(BigInteger srcBigInteger) { if (srcBigInteger.compareTo(BigInteger.ZERO) < 0) throw new RuntimeException("negative numbers are not allowed"); if (srcBigInteger.equals(BigInteger.ZERO)) return encodeByte((byte) 0); else return encodeElement(asUnsignedByteArray(srcBigInteger)); }
@Override public synchronized BigInteger getTotalDifficultyForHash(byte[] hash){ Block block = this.getBlockByHash(hash); if (block == null) return ZERO; Long level = block.getNumber(); List<BlockInfo> blockInfos = index.get(level.intValue()); for (BlockInfo blockInfo : blockInfos) if (areEqual(blockInfo.getHash(), hash)) { return blockInfo.totalDifficulty; } return ZERO; }
private void validate() { if (getNonce().length > HASH_LENGTH) throw new RuntimeException("Nonce is not valid"); if (receiveAddress != null && receiveAddress.length != 0 && receiveAddress.length != ADDRESS_LENGTH) throw new RuntimeException("Receive address is not valid"); if (gasLimit.length > HASH_LENGTH) throw new RuntimeException("Gas Limit is not valid"); if (gasPrice != null && gasPrice.length > HASH_LENGTH) throw new RuntimeException("Gas Price is not valid"); if (value != null && value.length > HASH_LENGTH) throw new RuntimeException("Value is not valid"); if (getSignature() != null) { if (BigIntegers.asUnsignedByteArray(signature.r).length > HASH_LENGTH) throw new RuntimeException("Signature R is not valid"); if (BigIntegers.asUnsignedByteArray(signature.s).length > HASH_LENGTH) throw new RuntimeException("Signature S is not valid"); if (getSender() != null && getSender().length != ADDRESS_LENGTH) throw new RuntimeException("Sender is not valid"); } }
public byte[] calcDistance(Peer toPeer) { BigInteger aPeer = new BigInteger(getId()); BigInteger bPeer = new BigInteger(toPeer.getId()); BigInteger distance = aPeer.xor(bPeer); return BigIntegers.asUnsignedByteArray(distance); }
private void sign() { flags |= SIGNATURE_FLAG; byte[] forSig = hash(); ECKey.ECDSASignature signature = from.sign(forSig); byte v; if (signature.v == 27) v = 0; else if (signature.v == 28) v = 1; else throw new RuntimeException("Invalid signature: " + signature); this.signature = merge(BigIntegers.asUnsignedByteArray(32, signature.r), BigIntegers.asUnsignedByteArray(32, signature.s), new byte[]{v}); }
@Override public String toString() { byte[] sigBytes = merge(asUnsignedByteArray(signature.r), asUnsignedByteArray(signature.s), new byte[]{EncryptionHandshake.recIdFromSignatureV(signature.v)}); return "AuthInitiateMessage{" + "\n sigBytes=" + toHexString(sigBytes) + "\n publicKey=" + toHexString(publicKey.getEncoded(false)) + "\n nonce=" + toHexString(nonce) + "\n version=" + version + "\n}"; } }
@Override public String toString() { byte[] sigBytes = merge(asUnsignedByteArray(signature.r), asUnsignedByteArray(signature.s), new byte[]{EncryptionHandshake.recIdFromSignatureV(signature.v)}); return "AuthInitiateMessage{" + "\n sigBytes=" + toHexString(sigBytes) + "\n ephemeralPublicHash=" + toHexString(ephemeralPublicHash) + "\n publicKey=" + toHexString(publicKey.getEncoded(false)) + "\n nonce=" + toHexString(nonce) + "\n}"; } }