Skip to content
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
Binary file added .gradle/8.5/checksums/checksums.lock
Binary file not shown.
Binary file not shown.
Binary file added .gradle/8.5/fileChanges/last-build.bin
Binary file not shown.
Binary file added .gradle/8.5/fileHashes/fileHashes.lock
Binary file not shown.
Binary file added .gradle/buildOutputCleanup/buildOutputCleanup.lock
Binary file not shown.
4 changes: 2 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
<connection>scm:git:git://github.com/skynetcap/solanaj.git</connection>
<developerConnection>scm:git:ssh://github.com/skynetcap/solanaj.git</developerConnection>
<url>https://github.com/skynetcap/solanaj/tree/main</url>
</scm>
</scm>
<developers>
<developer>
<name>Michael Morrell</name>
Expand All @@ -42,7 +42,7 @@
<dependency>
<groupId>org.bitcoinj</groupId>
<artifactId>bitcoinj-core</artifactId>
<version>0.16.3</version>
<version>0.17</version>
<exclusions>
<exclusion>
<groupId>com.google.protobuf</groupId>
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/org/p2p/solanaj/core/Account.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import java.util.List;

import org.bitcoinj.crypto.*;
import org.bitcoinj.core.Base58;
import org.bitcoinj.base.Base58;
import org.p2p.solanaj.utils.TweetNaclFast;
import org.p2p.solanaj.utils.bip32.wallet.SolanaBip44;
import org.p2p.solanaj.utils.bip32.wallet.DerivableType;
Expand All @@ -30,7 +30,7 @@ public static Account fromMnemonic(List<String> words, String passphrase) {
byte[] seed = MnemonicCode.toSeed(words, passphrase);
DeterministicKey masterPrivateKey = HDKeyDerivation.createMasterPrivateKey(seed);
DeterministicHierarchy deterministicHierarchy = new DeterministicHierarchy(masterPrivateKey);
DeterministicKey child = deterministicHierarchy.get(HDUtils.parsePath("M/501H/0H/0/0"), true, true);
DeterministicKey child = deterministicHierarchy.get(HDPath.parsePath("M/501H/0H/0/0"), true, true);
TweetNaclFast.Signature.KeyPair keyPair = TweetNaclFast.Signature.keyPair_fromSeed(child.getPrivKeyBytes());
return new Account(keyPair);
}
Expand Down
10 changes: 5 additions & 5 deletions src/main/java/org/p2p/solanaj/core/Message.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package org.p2p.solanaj.core;

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

import java.nio.ByteBuffer;
Expand Down Expand Up @@ -41,7 +41,7 @@ int getLength() {
private String recentBlockhash;
private AccountKeysList accountKeys;
private List<TransactionInstruction> instructions;
private Account feePayer;
private PublicKey feePayerPublicKey;

public Message() {
this.accountKeys = new AccountKeysList();
Expand Down Expand Up @@ -143,8 +143,8 @@ public byte[] serialize() {
return out.array();
}

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

private List<AccountMeta> getAccountKeys() {
Expand All @@ -159,7 +159,7 @@ private List<AccountMeta> getAccountKeys() {
.collect(Collectors.toList());
}

int feePayerIndex = findAccountIndex(keysList, feePayer.getPublicKey());
int feePayerIndex = findAccountIndex(keysList, feePayerPublicKey);
List<AccountMeta> newList = new ArrayList<AccountMeta>();
AccountMeta feePayerMeta = keysList.get(feePayerIndex);
newList.add(new AccountMeta(feePayerMeta.getPublicKey(), true, true));
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/org/p2p/solanaj/core/PublicKey.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
import java.util.List;

import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import org.bitcoinj.core.Base58;
import org.bitcoinj.core.Sha256Hash;
import org.bitcoinj.base.Base58;
import org.bitcoinj.base.Sha256Hash;
import org.p2p.solanaj.utils.ByteUtils;
import org.p2p.solanaj.utils.PublicKeySerializer;
import org.p2p.solanaj.utils.TweetNaclFast;
Expand Down
29 changes: 26 additions & 3 deletions src/main/java/org/p2p/solanaj/core/Transaction.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.function.Function;

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

Expand Down Expand Up @@ -76,8 +77,7 @@ public void sign(List<Account> signers) {
}

Account feePayer = signers.get(0);
message.setFeePayer(feePayer);

message.setFeePayerPublicKey(feePayer.getPublicKey());
serializedMessage = message.serialize();

for (Account signer : signers) {
Expand All @@ -91,6 +91,29 @@ public void sign(List<Account> signers) {
}
}

/**
* Signs the transaction with external signer.
*
* @param feePayerPublicKey - The public key of the signer's account.
* @param externalSigner - Function for external sign.
* @throws IllegalArgumentException if no signers are provided
*/
public void signByExternalSigner(PublicKey feePayerPublicKey, Function<byte[], byte[]> externalSigner) {
if (externalSigner == null) {
throw new IllegalArgumentException("No external signer provided");
}

message.setFeePayerPublicKey(feePayerPublicKey);
serializedMessage = message.serialize();

try {
byte[] signature = externalSigner.apply(message.serialize());
signatures.add(Base58.encode(signature));
} catch (Exception e) {
throw new RuntimeException("Error signing transaction", e); // Improve exception handling
}
}

/**
* Serializes the transaction into a byte array.
*
Expand Down
26 changes: 15 additions & 11 deletions src/main/java/org/p2p/solanaj/programs/AssociatedTokenProgram.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,9 @@ public class AssociatedTokenProgram extends Program {
*/
public static TransactionInstruction create(PublicKey fundingAccount,
PublicKey walletAddress,
PublicKey mint) {
return createInstruction(CREATE_METHOD_ID, fundingAccount, walletAddress, mint);
PublicKey mint,
PublicKey tokenProgram) {
return createInstruction(CREATE_METHOD_ID, fundingAccount, walletAddress, mint, tokenProgram);
}

/**
Expand All @@ -45,8 +46,9 @@ public static TransactionInstruction create(PublicKey fundingAccount,
*/
public static TransactionInstruction createIdempotent(PublicKey fundingAccount,
PublicKey walletAddress,
PublicKey mint) {
return createInstruction(CREATE_IDEMPOTENT_METHOD_ID, fundingAccount, walletAddress, mint);
PublicKey mint,
PublicKey tokenProgram) {
return createInstruction(CREATE_IDEMPOTENT_METHOD_ID, fundingAccount, walletAddress, mint, tokenProgram);
}

/**
Expand All @@ -66,7 +68,8 @@ public static TransactionInstruction recoverNested(PublicKey nestedAccount,
PublicKey destinationAccount,
PublicKey ownerAccount,
PublicKey ownerMint,
PublicKey wallet) {
PublicKey wallet,
PublicKey tokenProgram) {
final List<AccountMeta> keys = new ArrayList<>();

keys.add(new AccountMeta(nestedAccount, false, true));
Expand All @@ -75,7 +78,7 @@ public static TransactionInstruction recoverNested(PublicKey nestedAccount,
keys.add(new AccountMeta(ownerAccount, false, false));
keys.add(new AccountMeta(ownerMint, false, false));
keys.add(new AccountMeta(wallet, true, true));
keys.add(new AccountMeta(TokenProgram.PROGRAM_ID, false, false));
keys.add(new AccountMeta(tokenProgram, false, false));

byte[] transactionData = encodeInstructionData(RECOVER_NESTED_METHOD_ID);

Expand All @@ -85,29 +88,30 @@ public static TransactionInstruction recoverNested(PublicKey nestedAccount,
private static TransactionInstruction createInstruction(int methodId,
PublicKey fundingAccount,
PublicKey walletAddress,
PublicKey mint) {
PublicKey mint,
PublicKey tokenProgram) {
final List<AccountMeta> keys = new ArrayList<>();

PublicKey pda = findAssociatedTokenAddress(walletAddress, mint);
PublicKey pda = findAssociatedTokenAddress(walletAddress, mint, tokenProgram);

keys.add(new AccountMeta(fundingAccount, true, true));
keys.add(new AccountMeta(pda, false, true));
keys.add(new AccountMeta(walletAddress, false, false));
keys.add(new AccountMeta(mint, false, false));
keys.add(new AccountMeta(SystemProgram.PROGRAM_ID, false, false));
keys.add(new AccountMeta(TokenProgram.PROGRAM_ID, false, false));
keys.add(new AccountMeta(tokenProgram, false, false));

byte[] transactionData = encodeInstructionData(methodId);

return createTransactionInstruction(PROGRAM_ID, keys, transactionData);
}

private static PublicKey findAssociatedTokenAddress(PublicKey walletAddress, PublicKey mint) {
private static PublicKey findAssociatedTokenAddress(PublicKey walletAddress, PublicKey mint, PublicKey tokenProgram) {
try {
PublicKey pda = PublicKey.findProgramAddress(
List.of(
walletAddress.toByteArray(),
TokenProgram.PROGRAM_ID.toByteArray(),
tokenProgram.toByteArray(),
mint.toByteArray()
),
PROGRAM_ID
Expand Down
21 changes: 21 additions & 0 deletions src/main/java/org/p2p/solanaj/programs/SystemProgram.java
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,27 @@ public static TransactionInstruction transfer(PublicKey fromPublicKey, PublicKey
return new TransactionInstruction(PROGRAM_ID, keys, data);
}

/** Write 8 bytes to the byte array (starting at the offset) as signed 64-bit integer in little endian format. */
public static void int64ToByteArrayLE(long val, byte[] out, int offset) {
out[offset] = (byte) (0xFF & val);
out[offset + 1] = (byte) (0xFF & (val >> 8));
out[offset + 2] = (byte) (0xFF & (val >> 16));
out[offset + 3] = (byte) (0xFF & (val >> 24));
out[offset + 4] = (byte) (0xFF & (val >> 32));
out[offset + 5] = (byte) (0xFF & (val >> 40));
out[offset + 6] = (byte) (0xFF & (val >> 48));
out[offset + 7] = (byte) (0xFF & (val >> 56));
}


/** Write 4 bytes to the byte array (starting at the offset) as unsigned 32-bit integer in little endian format. */
public static void uint32ToByteArrayLE(long val, byte[] out, int offset) {
out[offset] = (byte) (0xFF & val);
out[offset + 1] = (byte) (0xFF & (val >> 8));
out[offset + 2] = (byte) (0xFF & (val >> 16));
out[offset + 3] = (byte) (0xFF & (val >> 24));
}

/**
* Creates an instruction to create a new account.
*
Expand Down
Loading