Skip to content

Commit

Permalink
Adding createWithSeed (create_with_seed) method from the Solana Rust SDK
Browse files Browse the repository at this point in the history
  • Loading branch information
angusscott committed Nov 4, 2024
1 parent 89f49ee commit 5d3c686
Showing 1 changed file with 19 additions and 0 deletions.
19 changes: 19 additions & 0 deletions src/main/java/org/p2p/solanaj/core/PublicKey.java
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,23 @@ public static PublicKey createProgramAddress(List<byte[]> seeds, PublicKey progr
}
}

public static PublicKey createWithSeed(PublicKey fromPublicKey, String seed, PublicKey programId) {
try (ByteArrayOutputStream buffer = new ByteArrayOutputStream()) {
if (seed.length() > 32) {
throw new IllegalArgumentException("Max seed length exceeded: " + seed.length());
}

buffer.write(fromPublicKey.toByteArray());
buffer.write(seed.getBytes());
buffer.write(programId.toByteArray());

byte[] hash = Sha256Hash.hash(buffer.toByteArray());
return new PublicKey(hash);
} catch (IOException e) {
throw new RuntimeException("Error creating program address", e);
}
}

public static class ProgramDerivedAddress {
private PublicKey address;
private int nonce;
Expand Down Expand Up @@ -124,6 +141,8 @@ public static ProgramDerivedAddress findProgramAddress(List<byte[]> seeds, Publi
throw new IllegalStateException("Unable to find a viable program address nonce");
}



public static PublicKey valueOf(String publicKey) {
return new PublicKey(publicKey);
}
Expand Down

0 comments on commit 5d3c686

Please sign in to comment.