Skip to content

Commit

Permalink
Support parsing key pairs generated by the solana cli.
Browse files Browse the repository at this point in the history
  • Loading branch information
jpe7s committed Jan 19, 2025
1 parent 0f5b703 commit 1c43d85
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 5 deletions.
11 changes: 9 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,13 @@ var signer = PrivateKeyEncoding.fromJsonPrivateKey(ji);

### JSON Configuration

#### Solana CLI Generated Key Pair JSON Array

By default, if only an array is configured, the assumption is that it is a key pair generated by the Solana CLI.

```json
[45,8,240,...,23,243,113]
```
#### JSON Array

```json
Expand All @@ -210,7 +217,7 @@ var signer = PrivateKeyEncoding.fromJsonPrivateKey(ji);
```
#### Key Pair

Encoded 64 bytes private/public key pair. Derived public key will be validated.
Encoded 64 byte private/public key pair. Derived public key will be validated.

* base64KeyPair
* base58KeyPair
Expand All @@ -225,7 +232,7 @@ Encoded 64 bytes private/public key pair. Derived public key will be validated.

#### Private Key Only

Encoded 32 bytes private key.
Encoded 32 byte private key.

* base64PrivateKey
* base58PrivateKey
12 changes: 9 additions & 3 deletions rpc/src/main/java/software/sava/rpc/json/PrivateKeyEncoding.java
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,15 @@ public static Signer fromJsonPrivateKey(final JsonIterator ji, final PrivateKeyE
}

public static Signer fromJsonPrivateKey(final JsonIterator ji) {
final var parser = new Parser();
ji.testObject(parser);
return parser.createSigner(ji);
return switch (ji.whatIsNext()) {
case ARRAY -> fromJsonPrivateKey(ji, jsonKeyPairArray);
case OBJECT -> {
final var parser = new Parser();
ji.testObject(parser);
yield parser.createSigner(ji);
}
default -> throw new IllegalStateException("Must be a JSON object or private key pair array.");
};
}

private static final class Parser implements FieldBufferPredicate {
Expand Down

0 comments on commit 1c43d85

Please sign in to comment.