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

Add send transaction with external sign #76

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
2 changes: 1 addition & 1 deletion 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 Down
8 changes: 4 additions & 4 deletions src/main/java/org/p2p/solanaj/core/Message.java
Original file line number Diff line number Diff line change
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
27 changes: 25 additions & 2 deletions src/main/java/org/p2p/solanaj/core/Transaction.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.function.Function;

import org.bitcoinj.core.Base58;
import org.p2p.solanaj.utils.ShortvecEncoding;
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
32 changes: 32 additions & 0 deletions src/main/java/org/p2p/solanaj/rpc/RpcApi.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import org.p2p.solanaj.ws.listeners.NotificationEventListener;

import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;

public class RpcApi {
Expand Down Expand Up @@ -91,6 +92,37 @@ public String sendTransaction(Transaction transaction, List<Account> signers, St
return client.call("sendTransaction", params, String.class);
}

/**
* Sends a transaction to the RPC server with external signer
*
* @param transaction - The transaction to send.
* @param feePayerPublicKey - The public key of the signer's account.
* @param externalSigner - Function for external sign.
* @param recentBlockHash - The recent block hash. If null, it will be obtained from the RPC server.
* @param rpcSendTransactionConfig - The configuration object for sending transactions via RPC.
* @return The transaction ID as a string.
* @throws RpcException If an error occurs during the RPC call.
*/
public String sendTransaction(Transaction transaction, PublicKey feePayerPublicKey,
Function<byte[], byte[]> externalSigner, String recentBlockHash,
RpcSendTransactionConfig rpcSendTransactionConfig) throws RpcException {
if (recentBlockHash == null) {
recentBlockHash = getLatestBlockhash().getValue().getBlockhash();
}
transaction.setRecentBlockHash(recentBlockHash);
transaction.signByExternalSigner(feePayerPublicKey, externalSigner);
byte[] serializedTransaction = transaction.serialize();

String base64Trx = Base64.getEncoder().encodeToString(serializedTransaction);

List<Object> params = new ArrayList<>();

params.add(base64Trx);
params.add(rpcSendTransactionConfig);

return client.call("sendTransaction", params, String.class);
}

/**
* Sends a transaction to the network for processing.
* A default RpcSendTransactionConfig is used.
Expand Down
2 changes: 1 addition & 1 deletion src/test/java/org/p2p/solanaj/core/MessageTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public void serializeMessage() {
Message message = new Message();
message.addInstruction(SystemProgram.transfer(fromPublicKey, toPublickKey, lamports));
message.setRecentBlockHash("Eit7RCyhUixAe2hGBS8oqnw59QK3kgMMjfLME5bm9wRn");
message.setFeePayer(signer);
message.setFeePayerPublicKey(signer.getPublicKey());

assertArrayEquals(new int[] { 1, 0, 1, 3, 6, 26, 217, 208, 83, 135, 21, 72, 83, 126, 222, 62, 38, 24, 73, 163,
223, 183, 253, 2, 250, 188, 117, 178, 35, 200, 228, 106, 219, 133, 61, 12, 235, 122, 188, 208, 216, 117,
Expand Down