Skip to content

Commit

Permalink
Add API documentation for methods
Browse files Browse the repository at this point in the history
* Improved maxClick() and sendClick() error handling
* Edited Grammar
* Updated build.gradle and wrote documentation for the BitcoinKit package. Documented classes included BitcoinKit.kt, MainNet.kt, RegTest.kt, and TestNet.kt.
* Fixed Typo
* Added passphrase to properties
* Dust Calculator documentation
* HashUtils documentation
* Utils documentation and formatting. utils package documentation is completed
* BitcoinInput documentation
* BitcoinInputMarkable documentation
* BitcoinOutput documentation and completion of the IO package
  • Loading branch information
TBCode523 authored Jun 22, 2021
1 parent 3d57571 commit 38611c8
Show file tree
Hide file tree
Showing 12 changed files with 264 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,18 @@ package io.horizontalsystems.bitcoincore
import io.horizontalsystems.bitcoincore.transactions.TransactionSizeCalculator
import io.horizontalsystems.bitcoincore.transactions.scripts.ScriptType

/**
* Calculates the minimum amount of BTC or "dust" required to broadcast a transaction and pay miner fees.
* @param dustRelayTxFee
* @param sizeCalculator
*
*/
class DustCalculator(dustRelayTxFee: Int, val sizeCalculator: TransactionSizeCalculator) {
val minFeeRate = dustRelayTxFee / 1000

/**
*@param type The ScriptType (Ex: P2PKH, P2WPKH, P2SH, etc.)
*@return The minimum amount of satoshis required to make a transaction.
*/
fun dust(type: ScriptType): Int {
// https://github.com/bitcoin/bitcoin/blob/c536dfbcb00fb15963bf5d507b7017c241718bf6/src/policy/policy.cpp#L18

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,9 @@ public int read() throws IOException {

/**
* Read and fill bytes into byte array.
*
* @param b Arbitrary byte array
* @throws IOException
*/
public void readFully(byte b[]) throws IOException {
int off = 0;
Expand All @@ -76,22 +79,36 @@ public void readFully(byte b[]) throws IOException {
}
}

/**
* Read and castes the read value to byte
* @return The converted int
* @throws IOException
*/
public byte readByte() throws IOException {
int ch = in.read();
if (ch < 0) {
throw new EOFException();
}
return (byte) (ch);
}

/**
* Read and return
* @return The read value
* @throws IOException
*/
public int readUnsignedByte() throws IOException {
int ch = in.read();
if (ch < 0) {
throw new EOFException();
}
return ch;
}

/**
* Read and create a short
*
* @return Converted Byte Array
* @throws IOException
*/
public short readShort() throws IOException {
int ch1 = in.read();
int ch2 = in.read();
Expand All @@ -100,7 +117,12 @@ public short readShort() throws IOException {
}
return (short) ((ch2 << 8) + (ch1 << 0));
}

/**
* Read and create an unsigned short
*
* @return Converted Byte Array
* @throws IOException
*/
public int readUnsignedShort() throws IOException {
int ch1 = in.read();
int ch2 = in.read();
Expand All @@ -118,7 +140,12 @@ public char readChar() throws IOException {
}
return (char) ((ch2 << 8) + (ch1 << 0));
}

/**
* Read and create an int
*
* @return Converted Byte Array
* @throws IOException
*/
public int readInt() throws IOException {
int ch1 = in.read();
int ch2 = in.read();
Expand All @@ -129,7 +156,12 @@ public int readInt() throws IOException {
}
return ((ch4 << 24) + (ch3 << 16) + (ch2 << 8) + (ch1 << 0));
}

/**
* Read and create an unsigned int
*
* @return Converted Byte Array
* @throws IOException
*/
public long readUnsignedInt() throws IOException {
int ch1 = in.read();
int ch2 = in.read();
Expand All @@ -141,7 +173,12 @@ public long readUnsignedInt() throws IOException {
long ln4 = ch4 & 0x00000000ffffffffL;
return (ln4 << 24) + (ch3 << 16) + (ch2 << 8) + (ch1 << 0);
}

/**
* Read and create a long
*
* @return Converted Byte Array
* @throws IOException
*/
public long readLong() throws IOException {
readFully(bufferOf8bytes);
return (((long) bufferOf8bytes[7] << 56) + ((long) (bufferOf8bytes[6] & 255) << 48)
Expand All @@ -150,6 +187,12 @@ public long readLong() throws IOException {
+ ((bufferOf8bytes[1] & 255) << 8) + ((bufferOf8bytes[0] & 255) << 0));
}

/**
* Read and create a String
*
* @return Converted Byte Array with Charset UTF-8 format
* @throws IOException
*/
public String readString() throws IOException {
long len = readVarInt();
if (len == 0) {
Expand All @@ -160,6 +203,13 @@ public String readString() throws IOException {
return new String(buffer, StandardCharsets.UTF_8);
}

/**
* Read and put values into a byte array
*
* @param len The desired length of the byte array
* @return A byte array with a length of len
* @throws IOException
*/
public byte[] readBytes(int len) throws IOException {
if (len == 0) {
return EMPTY_BYTES;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,31 @@
import java.io.ByteArrayInputStream;
import java.io.IOException;

/**
* A child class of BitcoinInput and extends its functionality.
* @see BitcoinInput
*/
public final class BitcoinInputMarkable extends BitcoinInput {

//Stores the length of the data byte array.
public int count;

/**
@param data Byte array that is going to by read by the InputStream
*/
public BitcoinInputMarkable(byte[] data) {
super(new ByteArrayInputStream(data));
this.count = data.length;
}

/**
*Marks the InputStream
*/
public void mark() {
// since the readlimit for ByteArrayInputStream has no meaning set it to 0
in.mark(0);
}

/**
*Resets the InputStream
*/
public void reset() throws IOException {
in.reset();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,24 @@
* Output "stream" for bitcoin protocol.
*
* @author Michael Liao
*
*/
public final class BitcoinOutput {

private UnsafeByteArrayOutputStream out;

/**
* Creates an instance of UnsafeByteArrayOutputStream of size 1024.
*/
public BitcoinOutput() {
this.out = new UnsafeByteArrayOutputStream(1024);
}

/**
* Writes a byte array to the OutputStream.
* @param bytes Byte array that's to be written.
* @return The newly written OutputStream.
*/
public BitcoinOutput write(byte[] bytes) {
try {
out.write(bytes);
Expand All @@ -25,26 +34,42 @@ public BitcoinOutput write(byte[] bytes) {
}
return this;
}

/**
* Writes a byte to the OutputStream.
* @param v The byte to be written.
* @return The newly written OutputStream.
*/
public BitcoinOutput writeByte(int v) {
out.write(v);
return this;
}

/**
* Writes a short to the OutputStream.
* @param v The short to be written.
* @return The newly written OutputStream.
*/
public BitcoinOutput writeShort(short v) {
out.write(0xff & v);
out.write(0xff & (v >> 8));
return this;
}

/**
* Writes an int to the OutputStream.
* @param v Int value.
* @return The newly written OutputStream.
*/
public BitcoinOutput writeInt(int v) {
out.write(0xff & v);
out.write(0xff & (v >> 8));
out.write(0xff & (v >> 16));
out.write(0xff & (v >> 24));
return this;
}

/**
* Writes a 32-bit int to the OutputStream.
* @param v Long value to be converted.
* @return The newly written OutputStream.
*/
public BitcoinOutput writeInt32(long v) {
out.write((int)(0xff & v));
out.write((int)(0xff & (v >> 8)));
Expand All @@ -53,6 +78,11 @@ public BitcoinOutput writeInt32(long v) {
return this;
}

/**
* Writes a Long to the OutputStream.
* @param v Long value to be converted.
* @return The newly written OutputStream.
*/
public BitcoinOutput writeLong(long v) {
out.write((int) (0xff & v));
out.write((int) (0xff & (v >> 8)));
Expand All @@ -65,6 +95,11 @@ public BitcoinOutput writeLong(long v) {
return this;
}

/**
* Writes a var int to the OutputStream.
* @param n Long value to be converted.
* @return The newly written OutputStream.
*/
public BitcoinOutput writeVarInt(long n) {
if (n < 0xfd) {
writeByte((int) n);
Expand All @@ -82,18 +117,33 @@ public BitcoinOutput writeVarInt(long n) {
return this;
}

/**
* Writes an unsigned int to the OutputStream.
* @param ln Long value to be converted.
* @return The newly written OutputStream.
*/
public BitcoinOutput writeUnsignedInt(long ln) {
int n = (int) (0xffffffff & ln);
writeInt(n);
return this;
}

/**
* Writes an unsigned short to the OutputStream.
* @param i integer value to be casted as an unsigned short.
* @return The newly written OutputStream.
*/
public BitcoinOutput writeUnsignedShort(int i) {
short n = (short) (0xffff & i);
writeShort(n);
return this;
}

/**
* Writes a String to the OutputStream.
* @param str String value to be written.
* @return The newly written OutputStream.
*/
public BitcoinOutput writeString(String str) {
byte[] bs = str.getBytes(StandardCharsets.UTF_8);
writeVarInt(bs.length);
Expand Down
Loading

0 comments on commit 38611c8

Please sign in to comment.