Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat:support versioned transaction #33

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
.DS_Store
.vscode
target
build
.project
.classpath
.settings
.idea
*.iml
*.iml
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,16 @@ RpcClient client = new RpcClient(Cluster.TESTNET);
long balance = client.getApi().getBalance(new PublicKey("QqCCvshxtqMAL2CVALqiJB7uEeE5mjSPsseQdDzsRUo"));
```

##### Versioned Transaction decode

```java

String tx = "...";

byte[] txBytes = Base64.getDecoder().decode(tx);
VersionedTransaction versionedTransaction = VersionedTransaction.fromEncodeedTransaction(txBytes);
```

## Contribution

Welcome to contribute, feel free to change and open a PR.
Expand Down
110 changes: 110 additions & 0 deletions src/main/java/org/p2p/solanaj/core/Binary.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
package org.p2p.solanaj.core;

import java.util.Arrays;

public class Binary {

public static byte[] uint32(long value) {
byte[] byteArray = new byte[4];
byteArray[0] = (byte) (0xFFL & value);
byteArray[1] = (byte) (0xFFL & (value >> 8));
byteArray[2] = (byte) (0xFFL & (value >> 16));
byteArray[3] = (byte) (0xFFL & (value >> 24));
return byteArray;
}

public static byte[] int64(long value) {
byte[] byteArray = new byte[8];
byteArray[0] = (byte) (0xFFL & value);
byteArray[1] = (byte) (0xFFL & (value >> 8));
byteArray[2] = (byte) (0xFFL & (value >> 16));
byteArray[3] = (byte) (0xFFL & (value >> 24));
byteArray[4] = (byte) (0xFFL & (value >> 32));
byteArray[5] = (byte) (0xFFL & (value >> 40));
byteArray[6] = (byte) (0xFFL & (value >> 48));
byteArray[7] = (byte) (0xFFL & (value >> 56));
return byteArray;
}

public static byte[] uint16(long value) {
byte[] byteArray = new byte[2];
byteArray[0] = (byte) (0xFFL & value);
byteArray[1] = (byte) (0xFFL & (value >> 8));
return byteArray;
}

public static byte[] encodeLength(int len) {
byte[] out = new byte[10];
int remLen = len;
int cursor = 0;
while (true) {
int elem = remLen & 0x7f;
remLen = remLen >> 7;
if (remLen == 0) {
byte[] uint16 = uint16(elem);
out[cursor] = uint16[0];
out[cursor + 1] = uint16[1];
break;
} else {
elem = elem | 0x80;
byte[] uint16 = uint16(elem);
out[cursor] = uint16[0];
out[cursor + 1] = uint16[1];
cursor += 1;
}
}
byte[] bytes = new byte[cursor + 1];
System.arraycopy(out, 0, bytes, 0, cursor + 1);
return bytes;
}

public static DecodedLength decodeLength(byte[] bytes) {
byte[] newBytes = bytes;
int len = 0;
int size = 0;
while (true) {
int elem = newBytes[0];
newBytes = Arrays.copyOfRange(newBytes, 1, newBytes.length);

len = len | ((elem & 0x7f) << (size * 7));
size += 1;
if ((elem & 0x80) == 0) {
break;
}
}
return new DecodedLength(len, newBytes);
}

public static class DecodedLength {

public int length;
public byte[] bytes;

public DecodedLength(int length, byte[] bytes) {
this.length = length;
this.bytes = bytes;
}

public boolean equals(DecodedLength other) {

if (this == other) {
return true;
}

if (length != other.length) {
return false;
}
if (!Arrays.equals(bytes, other.bytes)) {
return false;
}

return true;
}

public int hashCode() {
int result = length;
result = 31 * result + Arrays.hashCode(bytes);
return result;
}
}
}
Loading