Skip to content

Commit 9e55daa

Browse files
committed
Prefix pubkey constants with an underscore.
1 parent 43d09de commit 9e55daa

File tree

1 file changed

+18
-3
lines changed

1 file changed

+18
-3
lines changed

anchor-src-gen/src/main/java/software/sava/anchor/AnchorPDA.java

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -214,13 +214,18 @@ public String varName(final GenSrcContext genSrcContext) {
214214
if (knownAccountRef != null) {
215215
return knownAccountRef.callReference() + ".toByteArray()";
216216
} else {
217-
return maybeKnownPublicKey.toBase58() + ".toByteArray()";
217+
return pubKeyVarName() + ".toByteArray()";
218218
}
219219
} else {
220220
return "seed" + index;
221221
}
222222
}
223223

224+
private String pubKeyVarName() {
225+
final var base58 = maybeKnownPublicKey.toBase58();
226+
return Character.isDigit(base58.charAt(0)) ? '_' + base58 : base58;
227+
}
228+
224229
@Override
225230
public String fieldName(final GenSrcContext genSrcContext, final Set<String> deDuplicateKnown) {
226231
if (isReadable) {
@@ -237,7 +242,7 @@ public String fieldName(final GenSrcContext genSrcContext, final Set<String> deD
237242
return null;
238243
}
239244
} else {
240-
return "final PublicKey " + maybeKnownPublicKey.toBase58();
245+
return "final PublicKey " + pubKeyVarName();
241246
}
242247
} else {
243248
return "final byte[] unknownSeedConstant" + index;
@@ -309,12 +314,22 @@ private ArgSeed createArgSeed(final int index) {
309314

310315
private ConstSeed createConstSeed(final int index) {
311316
final var str = new String(value);
317+
PublicKey maybePublicKey;
318+
if (value.length == PUBLIC_KEY_LENGTH) {
319+
try {
320+
maybePublicKey = PublicKey.createPubKey(value);
321+
} catch (final RuntimeException e) {
322+
maybePublicKey = null;
323+
}
324+
} else {
325+
maybePublicKey = null;
326+
}
312327
return new ConstSeed(
313328
type,
314329
index,
315330
value, str,
316331
ConstSeed.isReadable(str),
317-
value.length == PUBLIC_KEY_LENGTH ? PublicKey.createPubKey(value) : null
332+
maybePublicKey
318333
);
319334
}
320335

0 commit comments

Comments
 (0)