|
| 1 | +// VeriBlock Blockchain Project |
| 2 | +// Copyright 2017-2018 VeriBlock, Inc |
| 3 | +// Copyright 2018-2019 Xenios SEZC |
| 4 | +// All rights reserved. |
| 5 | +// https://www.veriblock.org |
| 6 | +// Distributed under the MIT software license, see the accompanying |
| 7 | +// file LICENSE or http://www.opensource.org/licenses/mit-license.php. |
| 8 | + |
| 9 | +package org.veriblock.protoconverters; |
| 10 | + |
| 11 | +import java.util.ArrayList; |
| 12 | +import java.util.List; |
| 13 | + |
| 14 | +import org.veriblock.sdk.BitcoinBlock; |
| 15 | +import org.veriblock.sdk.Sha256Hash; |
| 16 | + |
| 17 | +import com.google.protobuf.ByteString; |
| 18 | + |
| 19 | +import integration.api.grpc.VeriBlockMessages; |
| 20 | + |
| 21 | +public final class BitcoinBlockProtoConverter { |
| 22 | + |
| 23 | + private BitcoinBlockProtoConverter() {} //never |
| 24 | + |
| 25 | + public static BitcoinBlock fromProto(VeriBlockMessages.BitcoinBlock protoData) { |
| 26 | + int version = protoData.getVersion(); |
| 27 | + Sha256Hash previousBlock = Sha256Hash.wrap(protoData.getPreviousBlock().toByteArray(), protoData.getPreviousBlock().size()); |
| 28 | + Sha256Hash merkleRoot = Sha256Hash.wrap(protoData.getMerkleRoot().toByteArray(), protoData.getMerkleRoot().size()); |
| 29 | + int timestamp = protoData.getTimestamp(); |
| 30 | + int bits = protoData.getBits(); |
| 31 | + int nonce = protoData.getNonce(); |
| 32 | + |
| 33 | + BitcoinBlock result = new BitcoinBlock(version, previousBlock, merkleRoot, timestamp, bits, nonce); |
| 34 | + return result; |
| 35 | + } |
| 36 | + |
| 37 | + public static List<BitcoinBlock> fromProto(List<VeriBlockMessages.BitcoinBlock> protoData) { |
| 38 | + List<BitcoinBlock> result = new ArrayList<BitcoinBlock>(); |
| 39 | + for(VeriBlockMessages.BitcoinBlock output : protoData) { |
| 40 | + result.add(fromProto(output)); |
| 41 | + } |
| 42 | + return result; |
| 43 | + } |
| 44 | + |
| 45 | + public static VeriBlockMessages.BitcoinBlock toProto(BitcoinBlock data) { |
| 46 | + int version = data.getVersion(); |
| 47 | + byte[] previousBlock = data.getPreviousBlock().getBytes(); |
| 48 | + byte[] merkleRoot = data.getMerkleRoot().getBytes(); |
| 49 | + int timestamp = data.getTimestamp(); |
| 50 | + int bits = data.getBits(); |
| 51 | + int nonce = data.getNonce(); |
| 52 | + |
| 53 | + VeriBlockMessages.BitcoinBlock.Builder result = VeriBlockMessages.BitcoinBlock.newBuilder(); |
| 54 | + result = result.setVersion(version) |
| 55 | + .setPreviousBlock(ByteString.copyFrom(previousBlock)) |
| 56 | + .setMerkleRoot(ByteString.copyFrom(merkleRoot)) |
| 57 | + .setTimestamp(timestamp) |
| 58 | + .setBits(bits) |
| 59 | + .setNonce(nonce); |
| 60 | + return result.build(); |
| 61 | + } |
| 62 | + |
| 63 | + public static List<VeriBlockMessages.BitcoinBlock> toProto(List<BitcoinBlock> data) { |
| 64 | + List<VeriBlockMessages.BitcoinBlock> result = new ArrayList<>(); |
| 65 | + for(BitcoinBlock output : data) { |
| 66 | + result.add(toProto(output)); |
| 67 | + } |
| 68 | + return result; |
| 69 | + } |
| 70 | +} |
0 commit comments