Skip to content

Commit 2330b29

Browse files
Chintan PatelChintan Patel
authored andcommitted
Fixed merged conflict and changed naming for legacy transaction and legacy message
2 parents 46359ce + 43deb51 commit 2330b29

52 files changed

Lines changed: 4258 additions & 746 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

docs/README.md

Lines changed: 87 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,93 @@
11
# SolanaJ
22

3-
Solana blockchain client, written in pure Java.
4-
Solanaj is an API for integrating with Solana blockchain using the [Solana RPC API](https://docs.solana.com/apps/jsonrpc-api)
3+
[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)
4+
[![Java Version](https://img.shields.io/badge/Java-17%2B-blue)](https://www.oracle.com/java/technologies/javase/jdk17-archive-downloads.html)
5+
[![Maven Central](https://img.shields.io/maven-central/v/com.mmorrell/solanaj.svg)](https://search.maven.org/artifact/com.mmorrell/solanaj)
6+
[![Solana](https://img.shields.io/badge/Solana-Compatible-blueviolet)](https://solana.com/)
7+
[![Java](https://img.shields.io/badge/Pure-Java-orange)](https://www.java.com/)
8+
[![Documentation](https://img.shields.io/badge/API-Documentation-lightgrey)](https://docs.solana.com/apps/jsonrpc-api)
9+
[![Discord](https://img.shields.io/discord/889577356681945098?color=blueviolet)](https://discord.gg/solana)
10+
[![GitHub Stars](https://img.shields.io/github/stars/skynetcap/solanaj?style=social)](https://github.com/skynetcap/solanaj)
11+
12+
Solana blockchain client, written in pure Java. SolanaJ is an API for integrating with Solana blockchain using the [Solana RPC API](https://docs.solana.com/apps/jsonrpc-api).
513

614
This fork includes functionality for multiple Solana programs, including the Serum DEX.
715

8-
# SolanaJ-Programs
9-
For SolanaJ implementations of popular Solana programs such as Serum, please visit: https://github.com/skynetcap/solanaj-programs
16+
## Table of Contents
17+
18+
- [SolanaJ-Programs](#solanaj-programs)
19+
- [Requirements](#%EF%B8%8F-requirements)
20+
- [Dependencies](#-dependencies)
21+
- [Installation](#-installation)
22+
- [Build](#%EF%B8%8F-build)
23+
- [Examples](#-examples)
24+
- [Transfer Lamports](#transfer-lamports)
25+
- [Get Balance](#get-balance)
26+
- [Get Serum Market + Orderbooks](#get-serum-market--orderbooks)
27+
- [Send a Transaction with Memo Program](#send-a-transaction-with-memo-program)
28+
- [Contributing](#-contributing)
29+
- [License](#-license)
30+
31+
## SolanaJ-Programs
32+
33+
For SolanaJ implementations of popular Solana programs such as Serum, please visit: [https://github.com/skynetcap/solanaj-programs](https://github.com/skynetcap/solanaj-programs)
34+
35+
## 🛠️ Requirements
1036

11-
## Requirements
1237
- Java 17+
1338

14-
## Dependencies
39+
## 📚 Dependencies
40+
1541
- bitcoinj
1642
- OkHttp
1743
- Moshi
1844

19-
## Installation
20-
1. Add Maven dependency:
45+
## 📦 Installation
46+
47+
Add the following Maven dependency to your project's `pom.xml`:
2148

2249
```xml
2350
<dependency>
24-
<groupId>com.mmorrell</groupId>
25-
<artifactId>solanaj</artifactId>
26-
<version>1.17.6</version>
51+
<groupId>com.mmorrell</groupId>
52+
<artifactId>solanaj</artifactId>
53+
<version>1.19.2</version>
2754
</dependency>
2855
```
2956

30-
## Build
31-
In pom.xml update the plugin maven-gpg-plugin configuration with your homedir and keyname.
32-
To see if you have a gpg key run `gpg --list-secret-keys`
33-
If nothing is returned create one with `gpg --full-generate-key`
34-
Then run `mvn install` and the build should complete successfully.
57+
## 🏗️ Build
58+
59+
1. In `pom.xml`, update the `maven-gpg-plugin` configuration with your homedir and keyname:
60+
3561
```xml
3662
<configuration>
37-
<homedir>/home/phil/.gnupg/</homedir>
38-
<keyname>AE2D00367F40E980F7C62FF792C4533F3EE03477</keyname>
63+
<homedir>/home/your_username/.gnupg/</homedir>
64+
<keyname>YOUR_GPG_KEY_ID</keyname>
3965
</configuration>
4066
```
4167

42-
## Example
43-
##### Transfer lamports
68+
2. Check if you have a GPG key:
69+
70+
```sh
71+
gpg --list-secret-keys
72+
```
73+
74+
3. If no key is returned, create one:
75+
76+
```sh
77+
gpg --full-generate-key
78+
```
79+
80+
4. Run the Maven install command:
81+
82+
```sh
83+
mvn install
84+
```
85+
86+
The build should complete successfully.
87+
88+
## 🚀 Examples
89+
90+
### Transfer Lamports
4491

4592
```java
4693
RpcClient client = new RpcClient(Cluster.TESTNET);
@@ -57,15 +104,16 @@ legacyTransaction.addInstruction(SystemProgram.transfer(fromPublicKey, toPublick
57104
String signature = client.getApi().sendTransaction(legacyTransaction, signer);
58105
```
59106

60-
##### Get balance
107+
### Get Balance
61108

62109
```java
63110
RpcClient client = new RpcClient(Cluster.TESTNET);
64111

65112
long balance = client.getApi().getBalance(new PublicKey("QqCCvshxtqMAL2CVALqiJB7uEeE5mjSPsseQdDzsRUo"));
66113
```
67114

68-
##### Get Serum market + orderbooks
115+
### Get Serum Market + Orderbooks
116+
69117
```java
70118
final PublicKey solUsdcPublicKey = new PublicKey("7xMDbYTCqQEcK2aM9LbetGtNFJpzKdfXzLL5juaLh4GJ");
71119
final Market solUsdcMarket = new MarketBuilder()
@@ -78,6 +126,7 @@ final OrderBook bids = solUsdcMarket.getBidOrderBook();
78126
```
79127

80128
##### Send a legacyTransaction with call to the "Memo" program
129+
81130
```java
82131
// Create account from private key
83132
final Account feePayer = new Account(Base58.decode(new String(data)));
@@ -88,9 +137,22 @@ legacyTransaction.addInstruction(
88137
MemoProgram.writeUtf8(feePayer.getPublicKey(),"Hello from SolanaJ :)")
89138
);
90139

91-
String response = result = client.getApi().sendTransaction(legacyTransaction, feePayer);
140+
String response = client.getApi().sendTransaction(legacyTransaction, feePayer);
92141
```
93142

94-
## License
143+
## 🤝 Contributing
144+
145+
We welcome contributions to SolanaJ! Here's how you can help:
146+
147+
1. Fork the repository
148+
2. Create a new branch (`git checkout -b feature/your-feature-name`)
149+
3. Make your changes
150+
4. Commit your changes (`git commit -am 'Add some feature'`)
151+
5. Push to the branch (`git push origin feature/your-feature-name`)
152+
6. Create a new Pull Request
153+
154+
Please make sure to update tests as appropriate and adhere to the existing coding style.
155+
156+
## 📄 License
95157

96-
MIT License
158+
SolanaJ is open-source software licensed under the [MIT License](LICENSE). See the LICENSE file for more details.

pom.xml

Lines changed: 42 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,9 @@
3434
</properties>
3535
<dependencies>
3636
<dependency>
37-
<groupId>junit</groupId>
38-
<artifactId>junit</artifactId>
39-
<version>4.13.2</version>
37+
<groupId>org.junit.jupiter</groupId>
38+
<artifactId>junit-jupiter-api</artifactId>
39+
<version>5.11.0</version>
4040
<scope>test</scope>
4141
</dependency>
4242
<dependency>
@@ -120,6 +120,13 @@
120120
<version>2.17.2</version>
121121
</dependency>
122122

123+
<dependency>
124+
<groupId>org.mockito</groupId>
125+
<artifactId>mockito-core</artifactId>
126+
<version>3.12.4</version>
127+
<scope>test</scope>
128+
</dependency>
129+
123130
<dependency>
124131
<groupId>org.apache.httpcomponents.client5</groupId>
125132
<artifactId>httpclient5</artifactId>
@@ -184,31 +191,45 @@
184191
</executions>
185192
<configuration>
186193
<source>17</source>
194+
<doclint>none</doclint>
195+
<failOnError>false</failOnError>
187196
</configuration>
188197
</plugin>
189-
<plugin>
190-
<groupId>org.apache.maven.plugins</groupId>
191-
<artifactId>maven-gpg-plugin</artifactId>
192-
<version>3.2.4</version>
193-
<executions>
194-
<execution>
195-
<id>sign-artifacts</id>
196-
<phase>verify</phase>
197-
<goals>
198-
<goal>sign</goal>
199-
</goals>
200-
<configuration>
201-
<homedir>c:/Users/Michael/.gnupg/</homedir>
202-
<keyname>0x27FAE7D2</keyname>
203-
</configuration>
204-
</execution>
205-
</executions>
206-
</plugin>
207198
<plugin>
208199
<groupId>org.apache.maven.plugins</groupId>
209200
<artifactId>maven-compiler-plugin</artifactId>
210201
<version>3.13.0</version>
211202
</plugin>
203+
<plugin>
204+
<artifactId>maven-surefire-plugin</artifactId>
205+
<version>3.2.5</version>
206+
</plugin>
212207
</plugins>
213208
</build>
209+
<profiles>
210+
<profile>
211+
<id>deploy</id>
212+
<build>
213+
<plugins>
214+
<plugin>
215+
<groupId>org.apache.maven.plugins</groupId>
216+
<artifactId>maven-gpg-plugin</artifactId>
217+
<executions>
218+
<execution>
219+
<id>sign-artifacts</id>
220+
<phase>verify</phase>
221+
<goals>
222+
<goal>sign</goal>
223+
</goals>
224+
<configuration>
225+
<homedir>/Users/chintan_mbp/.gnupg/</homedir>
226+
<keyname>4B9021119F83557FF09AEBAAA4751749EC8210A3</keyname>
227+
</configuration>
228+
</execution>
229+
</executions>
230+
</plugin>
231+
</plugins>
232+
</build>
233+
</profile>
234+
</profiles>
214235
</project>

src/main/java/org/p2p/solanaj/core/Account.java

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import java.util.List;
66

77
import org.bitcoinj.crypto.*;
8+
import org.bitcoinj.core.Base58;
89
import org.p2p.solanaj.utils.TweetNaclFast;
910
import org.p2p.solanaj.utils.bip32.wallet.SolanaBip44;
1011
import org.p2p.solanaj.utils.bip32.wallet.DerivableType;
@@ -116,4 +117,30 @@ private static byte[] convertJsonStringToByteArray(String characters) {
116117

117118
return buffer.array();
118119
}
120+
121+
/**
122+
* Creates an Account from a base58-encoded private key string.
123+
* @param base58PrivateKey The base58-encoded private key
124+
* @return A new Account instance
125+
*/
126+
public static Account fromBase58PrivateKey(String base58PrivateKey) {
127+
byte[] privateKey = Base58.decode(base58PrivateKey);
128+
return new Account(privateKey);
129+
}
130+
131+
/**
132+
* Returns the account's public key as a base58-encoded string.
133+
* @return The base58-encoded public key
134+
*/
135+
public String getPublicKeyBase58() {
136+
return this.getPublicKey().toBase58();
137+
}
138+
139+
/**
140+
* Returns the account's private key as a base58-encoded string.
141+
* @return The base58-encoded private key
142+
*/
143+
public String getPrivateKeyBase58() {
144+
return Base58.encode(this.getSecretKey());
145+
}
119146
}
Lines changed: 9 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,61 +1,38 @@
11
package org.p2p.solanaj.core;
22

3-
import java.util.ArrayList;
4-
import java.util.Collection;
5-
import java.util.Comparator;
6-
import java.util.HashMap;
3+
import java.util.*;
74

85
public class AccountKeysList {
9-
private final HashMap<String, AccountMeta> accounts;
6+
private final Map<String, AccountMeta> accounts;
107

118
public AccountKeysList() {
129
accounts = new HashMap<>();
1310
}
1411

1512
public void add(AccountMeta accountMeta) {
1613
String key = accountMeta.getPublicKey().toString();
17-
18-
if (accounts.containsKey(key)) {
19-
if (!accounts.get(key).isWritable() && accountMeta.isWritable()) {
20-
accounts.put(key, accountMeta);
21-
}
22-
} else {
23-
accounts.put(key, accountMeta);
24-
}
14+
accounts.merge(key, accountMeta, (existing, newMeta) ->
15+
!existing.isWritable() && newMeta.isWritable() ? newMeta : existing);
2516
}
2617

2718
public void addAll(Collection<AccountMeta> metas) {
28-
for (AccountMeta meta : metas) {
29-
add(meta);
30-
}
19+
metas.forEach(this::add);
3120
}
3221

3322
public ArrayList<AccountMeta> getList() {
3423
ArrayList<AccountMeta> accountKeysList = new ArrayList<>(accounts.values());
3524
accountKeysList.sort(metaComparator);
36-
3725
return accountKeysList;
3826
}
3927

40-
private static final Comparator<AccountMeta> metaComparator = (am1, am2) -> {
41-
42-
int cmpSigner = am1.isSigner() == am2.isSigner() ? 0 : am1.isSigner() ? -1 : 1;
43-
if (cmpSigner != 0) {
44-
return cmpSigner;
45-
}
46-
47-
int cmpkWritable = am1.isWritable() == am2.isWritable() ? 0 : am1.isWritable() ? -1 : 1;
48-
if (cmpkWritable != 0) {
49-
return cmpkWritable;
50-
}
51-
52-
return Integer.compare(cmpSigner, cmpkWritable);
53-
};
54-
5528
@Override
5629
public String toString() {
5730
return "AccountKeysList{" +
5831
"accounts=" + accounts +
5932
'}';
6033
}
34+
35+
private static final Comparator<AccountMeta> metaComparator = Comparator
36+
.comparing(AccountMeta::isSigner).reversed()
37+
.thenComparing(AccountMeta::isWritable).reversed();
6138
}

src/main/java/org/p2p/solanaj/core/AccountMeta.java

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,17 @@ public class AccountMeta {
1313

1414
private boolean isWritable;
1515

16+
/**
17+
* Sorting based on isSigner and isWritable cannot fully meet the requirements. This value can be used for custom sorting, because if the order is incorrect during serialization, it may lead to failed method calls.
18+
*/
19+
private int sort = Integer.MAX_VALUE;
20+
21+
public AccountMeta(PublicKey publicKey, boolean isSigner, boolean isWritable) {
22+
this.publicKey = publicKey;
23+
this.isSigner = isSigner;
24+
this.isWritable = isWritable;
25+
}
26+
1627
@Override
1728
public String toString() {
1829
return "AccountMeta{" +
@@ -21,4 +32,4 @@ public String toString() {
2132
", isWritable=" + isWritable +
2233
'}';
2334
}
24-
}
35+
}

0 commit comments

Comments
 (0)