/** * Convert from `Coin` type to BTC `BigDecimal` value . * * @param coin Coin value to convert to BTC * @return Bitcoin amount in BTC units */ public static BigDecimal coinToBTC(final Coin coin) { return satoshiToBtc(coin.value); } }
public RpcClientModule(NetworkParameters netParams) { super("BitcoinJMappingClient", new Version(1, 0, 0, null, null, null)); this.addDeserializer(Address.class, new AddressDeserializer(netParams)) .addDeserializer(Block.class, new BlockHexDeserializer(netParams)) .addDeserializer(Coin.class, new CoinDeserializer()) .addDeserializer(ECKey.class, new ECKeyDeserializer()) .addDeserializer(Sha256Hash.class, new Sha256HashDeserializer()) .addSerializer(Address.class, new AddressSerializer()) .addSerializer(Coin.class, new CoinSerializer()) .addSerializer(ECKey.class, new ECKeySerializer()) .addSerializer(Sha256Hash.class, new Sha256HashSerializer()) .addSerializer(Transaction.class, new TransactionHexSerializer()); } }
public RpcServerModule() { super("BitcoinJMappingServer", new Version(1, 0, 0, null, null, null)); this.addDeserializer(Address.class, new AddressDeserializer(null)) // Null means use default list of netParams .addDeserializer(Coin.class, new CoinDeserializer()) .addDeserializer(ECKey.class, new ECKeyDeserializer()) .addDeserializer(Sha256Hash.class, new Sha256HashDeserializer()) .addSerializer(Address.class, new AddressSerializer()) .addSerializer(Coin.class, new CoinSerializer()) .addSerializer(ECKey.class, new ECKeySerializer()) .addSerializer(Peer.class, new PeerSerializer()) .addSerializer(Sha256Hash.class, new Sha256HashSerializer()) .addSerializer(Transaction.class, new TransactionSerializer()); } }
@Provides ObjectMapper provideObjectMapper(RpcConfig config) { return new ObjectMapper() .registerModule(new Jdk8Module()) .registerModule(new RpcClientModule(config.getNetParams())) .registerModule(new RpcServerModule()); }
/** * Get a "raw" transaction (which we map to a bitcoinj transaction) * @param txid Transaction ID/hash * @return bitcoinj Transaction * @throws JsonRpcStatusException JSON RPC status exception * @throws IOException network error */ public Transaction getRawTransaction(Sha256Hash txid) throws JsonRpcStatusException, IOException { String hexEncoded = send("getrawtransaction", txid); byte[] raw = HexUtil.hexStringToByteArray(hexEncoded); return new Transaction(context.getParams(), raw); }
/** * Construct a BitcoinClient from Network Parameters, URI, user name, and password. * @param netParams Correct Network Parameters for destination server * @param server URI of the Bitcoin RPC server * @param rpcuser Username (if required) * @param rpcpassword Password (if required) */ public BitcoinClient(NetworkParameters netParams, URI server, String rpcuser, String rpcpassword) { super(server, rpcuser, rpcpassword); this.context = new Context(netParams); mapper.registerModule(new RpcClientModule(context.getParams())); }
@Override public void serialize(Transaction value, JsonGenerator gen, SerializerProvider serializers) throws IOException, JsonProcessingException { gen.writeString(bytesToHexString(value.bitcoinSerialize())); }
@Override public void serialize(Coin value, JsonGenerator gen, SerializerProvider serializers) throws IOException, JsonProcessingException { gen.writeNumber(BitcoinMath.coinToBTC(value)); } }
/** * Convert from BTC `BigDecimal` value to `Coin` type. * * @param btc Bitcoin amount in BTC units * @return bitcoinj `Coin` type (uses satoshi unit internally) */ public static Coin btcToCoin(final BigDecimal btc) { return Coin.valueOf(btcToSatoshi(btc)); }
@Override public Coin deserialize(JsonParser p, DeserializationContext ctxt) throws IOException, JsonProcessingException { JsonToken token = p.getCurrentToken(); switch (token) { case VALUE_NUMBER_FLOAT: BigDecimal bd = p.getDecimalValue(); return BitcoinMath.btcToCoin(bd); case VALUE_NUMBER_INT: long val = p.getNumberValue().longValue(); // should be optimal, whatever it is return Coin.valueOf(val); default: return (Coin) ctxt.handleUnexpectedToken(Coin.class, p); } } }
inputs.add(new Outpoint(coinbaseTx, 0)); log.debug("amountGatheredSoFar = {}", BitcoinMath.satoshiToBtc(amountGatheredSoFar));
@Override public Block deserialize(JsonParser p, DeserializationContext ctxt) throws IOException, JsonProcessingException { JsonToken token = p.getCurrentToken(); switch (token) { case VALUE_STRING: try { byte[] payload = HexUtil.hexStringToByteArray(p.getValueAsString()); // convert to hex return context.getParams().getDefaultSerializer().makeBlock(payload); } catch (ProtocolException e) { throw new InvalidFormatException(p, "Invalid Block", p.getValueAsString(), Block.class); } default: return (Block) ctxt.handleUnexpectedToken(Block.class, p); } } }
/** * Construct from a bitcoinj transaction * @param transaction A bitcoinj confirmed or unconfirmed transaction */ public RawTransactionInfo(Transaction transaction) { this.hex = TransactionHexSerializer.bytesToHexString(transaction.bitcoinSerialize()); this.txid = transaction.getHash(); this.version = transaction.getVersion(); this.locktime = transaction.getLockTime(); this.blockhash = null; // For now this.confirmations = transaction.getConfidence().getDepthInBlocks(); this.time = 0; // TODO: block header time of block including transaction this.blocktime = this.time; // same as time (see API doc) vin = new VinList(); for (TransactionInput input : transaction.getInputs()) { vin.add(new Vin(txid, input.getOutpoint().getIndex(), input.getScriptSig().toString(), input.getSequenceNumber())); } vout = new VoutList(); for (TransactionOutput output : transaction.getOutputs()) { vout.add(new Vout(output.getValue(), output.getIndex(), output.getScriptPubKey().toString())); } }