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

Roger: updates as per latest documentation and some code clean up. #27

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
22 changes: 12 additions & 10 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,17 @@
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>11</source>
<target>11</target>
</configuration>
</plugin>
</plugins></build>
<properties>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
</properties>
<source>8</source>
<target>8</target>
</configuration>
</plugin>
</plugins>
</build>

<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
<skipTests>false</skipTests> </properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
Expand All @@ -32,7 +34,7 @@
<dependency>
<groupId>org.bitcoinj</groupId>
<artifactId>bitcoinj-core</artifactId>
<version>0.15.8</version>
<version>0.16.1</version>
</dependency>
<dependency>
<groupId>com.squareup.okhttp3</groupId>
Expand Down
12 changes: 4 additions & 8 deletions src/main/java/org/p2p/solanaj/core/Account.java
Original file line number Diff line number Diff line change
@@ -1,16 +1,12 @@
package org.p2p.solanaj.core;

import java.util.List;

import org.bitcoinj.crypto.DeterministicHierarchy;
import org.bitcoinj.crypto.DeterministicKey;
import org.bitcoinj.crypto.HDKeyDerivation;
import org.bitcoinj.crypto.HDUtils;
import org.bitcoinj.crypto.MnemonicCode;
import org.bitcoinj.crypto.*;
import org.p2p.solanaj.utils.TweetNaclFast;

import java.util.List;

public class Account {
private TweetNaclFast.Signature.KeyPair keyPair;
private final TweetNaclFast.Signature.KeyPair keyPair;

public Account() {
this.keyPair = TweetNaclFast.Signature.keyPair();
Expand Down
27 changes: 9 additions & 18 deletions src/main/java/org/p2p/solanaj/core/AccountKeysList.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@
import java.util.List;

public class AccountKeysList {
private List<AccountMeta> accountsList;
private final List<AccountMeta> accountsList;

public AccountKeysList() {
accountsList = new ArrayList<AccountMeta>();
accountsList = new ArrayList<>();
}

public void add(AccountMeta accountMeta) {
Expand All @@ -21,7 +21,7 @@ public void addAll(Collection<AccountMeta> metas) {
}

public List<AccountMeta> getList() {
ArrayList<AccountMeta> uniqueMetas = new ArrayList<AccountMeta>();
ArrayList<AccountMeta> uniqueMetas = new ArrayList<>();

for (AccountMeta accountMeta : accountsList) {
PublicKey pubKey = accountMeta.getPublicKey();
Expand All @@ -41,23 +41,14 @@ public List<AccountMeta> getList() {
return uniqueMetas;
}

private static final Comparator<AccountMeta> metaComparator = new Comparator<AccountMeta>() {
private static final Comparator<AccountMeta> metaComparator = (am1, am2) -> {

@Override
public int compare(AccountMeta am1, AccountMeta am2) {

int cmpSigner = am1.isSigner() == am2.isSigner() ? 0 : am1.isSigner() ? -1 : 1;
if (cmpSigner != 0) {
return cmpSigner;
}

int cmpkWritable = am1.isWritable() == am2.isWritable() ? 0 : am1.isWritable() ? -1 : 1;
if (cmpkWritable != 0) {
return cmpkWritable;
}

return 0;
int cmpSigner = am1.isSigner() == am2.isSigner() ? 0 : am1.isSigner() ? -1 : 1;
if (cmpSigner != 0) {
return cmpSigner;
}

return am1.isWritable() == am2.isWritable() ? 0 : am1.isWritable() ? -1 : 1;
};

}
6 changes: 3 additions & 3 deletions src/main/java/org/p2p/solanaj/core/AccountMeta.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
import java.util.List;

public class AccountMeta {
private PublicKey publicKey;
private boolean isSigner;
private boolean isWritable;
private final PublicKey publicKey;
private final boolean isSigner;
private final boolean isWritable;

public AccountMeta(PublicKey publicKey, boolean isSigner, boolean isWritable) {
this.publicKey = publicKey;
Expand Down
79 changes: 79 additions & 0 deletions src/main/java/org/p2p/solanaj/core/Block.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
package org.p2p.solanaj.core;


import java.util.List;

public class Block {

private long blockHeight;

private long blockTime;

public long getBlockTime() {
return blockTime;
}

public void setBlockTime(long blockTime) {
this.blockTime = blockTime;
}

public String getBlockhash() {
return blockhash;
}

public void setBlockhash(String blockhash) {
this.blockhash = blockhash;
}

public long getParentSlot() {
return parentSlot;
}

public void setParentSlot(long parentSlot) {
this.parentSlot = parentSlot;
}

public String getPreviousBlockhash() {
return previousBlockhash;
}

public void setPreviousBlockhash(String previousBlockhash) {
this.previousBlockhash = previousBlockhash;
}

public List<Transaction> getTransactions() {
return transactions;
}

public void setTransactions(List<Transaction> transactions) {
this.transactions = transactions;
}

public List<Reward> getRewards() {
return rewards;
}

public void setRewards(List<Reward> rewards) {
this.rewards = rewards;
}

private String blockhash;

private long parentSlot;

private String previousBlockhash;

private List<Transaction> transactions;

private List<Reward> rewards;

public long getBlockHeight() {
return blockHeight;
}

public void setBlockHeight(long blockHeight) {
this.blockHeight = blockHeight;
}


}
72 changes: 35 additions & 37 deletions src/main/java/org/p2p/solanaj/core/Message.java
Original file line number Diff line number Diff line change
@@ -1,27 +1,26 @@
package org.p2p.solanaj.core;

import org.bitcoinj.core.Base58;
import org.p2p.solanaj.utils.ShortvecEncoding;

import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.List;

import org.bitcoinj.core.Base58;

import org.p2p.solanaj.utils.ShortvecEncoding;

public class Message {
private class MessageHeader {
private static class MessageHeader {
static final int HEADER_LENGTH = 3;

byte numRequiredSignatures = 0;
byte numReadonlySignedAccounts = 0;
byte numReadonlyUnsignedAccounts = 0;

byte[] toByteArray() {
return new byte[] { numRequiredSignatures, numReadonlySignedAccounts, numReadonlyUnsignedAccounts };
return new byte[]{numRequiredSignatures, numReadonlySignedAccounts, numReadonlyUnsignedAccounts};
}
}

private class CompiledInstruction {
private static class CompiledInstruction {
byte programIdIndex;
byte[] keyIndicesCount;
byte[] keyIndices;
Expand All @@ -38,25 +37,25 @@ int getLength() {

private MessageHeader messageHeader;
private String recentBlockhash;
private AccountKeysList accountKeys;
private List<TransactionInstruction> instructions;
private PublicKey feePayer;
private List<String> programIds;
private final AccountKeysList accountKeys;

public List<TransactionInstruction> getInstructions() {
return instructions;
}

private final List<TransactionInstruction> instructions;
private Account feePayer;

public Message() {
this.programIds = new ArrayList<String>();
this.accountKeys = new AccountKeysList();
this.instructions = new ArrayList<TransactionInstruction>();
this.instructions = new ArrayList<>();
}

public Message addInstruction(TransactionInstruction instruction) {
accountKeys.addAll(instruction.getKeys());
accountKeys.add(new AccountMeta(instruction.getProgramId(), false, false));
instructions.add(instruction);

if (!programIds.contains(instruction.getProgramId().toBase58())) {
programIds.add(instruction.getProgramId().toBase58());
}

return this;
}

Expand All @@ -76,29 +75,24 @@ public byte[] serialize() {

messageHeader = new MessageHeader();

for (String programId : programIds) {
accountKeys.add(new AccountMeta(new PublicKey(programId), false, false));
}
List<AccountMeta> keysList = getAccountKeys();
int accountKeysSize = keysList.size();

byte[] accountAddressesLength = ShortvecEncoding.encodeLength(accountKeysSize);

int compiledInstructionsLength = 0;
List<CompiledInstruction> compiledInstructions = new ArrayList<CompiledInstruction>();
List<CompiledInstruction> compiledInstructions = new ArrayList<>();

for (TransactionInstruction instruction : instructions) {
int keysSize = instruction.getKeys().size();

byte[] keyIndices = new byte[keysSize];
for (int i = 0; i < keysSize; i++) {
keyIndices[i] = (byte) AccountMeta.findAccountIndex(keysList,
instruction.getKeys().get(i).getPublicKey());
keyIndices[i] = (byte) findAccountIndex(keysList, instruction.getKeys().get(i).getPublicKey());
}

CompiledInstruction compiledInstruction = new CompiledInstruction();
compiledInstruction.programIdIndex = (byte) AccountMeta.findAccountIndex(keysList,
instruction.getProgramId());
compiledInstruction.programIdIndex = (byte) findAccountIndex(keysList, instruction.getProgramId());
compiledInstruction.keyIndicesCount = ShortvecEncoding.encodeLength(keysSize);
compiledInstruction.keyIndices = keyIndices;
compiledInstruction.dataLength = ShortvecEncoding.encodeLength(instruction.getData().length);
Expand Down Expand Up @@ -152,26 +146,30 @@ public byte[] serialize() {
return out.array();
}

protected void setFeePayer(PublicKey feePayer) {
protected void setFeePayer(Account feePayer) {
this.feePayer = feePayer;
}

private List<AccountMeta> getAccountKeys() {
List<AccountMeta> keysList = accountKeys.getList();
int feePayerIndex = AccountMeta.findAccountIndex(keysList, feePayer);
int feePayerIndex = findAccountIndex(keysList, feePayer.getPublicKey());

List<AccountMeta> newList = new ArrayList<AccountMeta>();

if (feePayerIndex != -1) {
AccountMeta feePayerMeta = keysList.get(feePayerIndex);
newList.add(new AccountMeta(feePayerMeta.getPublicKey(), true, true));
keysList.remove(feePayerIndex);
} else {
newList.add(new AccountMeta(feePayer, true, true));
}
List<AccountMeta> newList = new ArrayList<>();
AccountMeta feePayerMeta = keysList.get(feePayerIndex);
newList.add(new AccountMeta(feePayerMeta.getPublicKey(), true, true));
keysList.remove(feePayerIndex);
newList.addAll(keysList);

return newList;
}

}
private int findAccountIndex(List<AccountMeta> accountMetaList, PublicKey key) {
for (int i = 0; i < accountMetaList.size(); i++) {
if (accountMetaList.get(i).getPublicKey().equals(key)) {
return i;
}
}

throw new RuntimeException("unable to find account index");
}
}
Loading