|
21 | 21 | import java.nio.charset.StandardCharsets; |
22 | 22 | import java.util.Arrays; |
23 | 23 | import java.util.Base64; |
| 24 | +import java.util.List; |
24 | 25 |
|
25 | 26 | import static io.nats.nkey.NKeyConstants.NKEY_PROVIDER_CLASS_SYSTEM_PROPERTY; |
26 | 27 | import static io.nats.nkey.NKeyProvider.getProvider; |
@@ -464,4 +465,74 @@ private static void _testFromPublicKey(String userEncodedSeed, String userEncode |
464 | 465 | assertArrayEquals(userEncodedPubKey.toCharArray(), fromSeed.getPublicKey()); |
465 | 466 | assertArrayEquals(userEncodedPubKey.toCharArray(), fromKey.getPublicKey()); |
466 | 467 | } |
| 468 | + |
| 469 | + static byte[] TO_SIGN = "Synadia".getBytes(StandardCharsets.UTF_8); |
| 470 | + |
| 471 | + @Test |
| 472 | + public void testFromText() { |
| 473 | + List<String> inputs = ResourceUtils.resourceAsLines("test-nkeys.txt"); |
| 474 | + for (int i = 0; i < inputs.size(); ) { |
| 475 | + String name = inputs.get(i++); |
| 476 | + int prefix = Integer.parseInt(inputs.get(i++)); |
| 477 | + char[] seed = inputs.get(i++).toCharArray(); |
| 478 | + char[] publicKey = inputs.get(i++).toCharArray(); |
| 479 | + char[] privateKey = inputs.get(i++).toCharArray(); |
| 480 | + byte[] decoded = toBytes(inputs.get(i++)); |
| 481 | + byte[] signed = toBytes(inputs.get(i++)); |
| 482 | + |
| 483 | + NKeyType type = NKeyType.fromPrefix(prefix); |
| 484 | + assertNotNull(type); |
| 485 | + assertEquals(name, type.name()); |
| 486 | + |
| 487 | + NKey fromSeed = PROVIDER.fromSeed(seed); |
| 488 | + NKey fromPublicKey = PROVIDER.fromPublicKey(publicKey); |
| 489 | + |
| 490 | + assertArrayEquals(seed, fromSeed.getSeed()); |
| 491 | + assertArrayEquals(publicKey, fromSeed.getPublicKey()); |
| 492 | + assertArrayEquals(privateKey, fromSeed.getPrivateKey()); |
| 493 | + assertArrayEquals(publicKey, fromPublicKey.getPublicKey()); |
| 494 | + assertEquals(prefix, fromSeed.getDecodedSeed().prefix); |
| 495 | + assertArrayEquals(decoded, fromSeed.getDecodedSeed().bytes); |
| 496 | + assertArrayEquals(signed, fromSeed.sign(TO_SIGN)); |
| 497 | + } |
| 498 | + } |
| 499 | + |
| 500 | + @Test |
| 501 | + public void testGenerateTestNkeysText() { |
| 502 | + for (int x = 0; x < 10; x++) { |
| 503 | + generateTestNkeysText(PROVIDER.createUser()); |
| 504 | + generateTestNkeysText(PROVIDER.createAccount()); |
| 505 | + generateTestNkeysText(PROVIDER.createOperator()); |
| 506 | + generateTestNkeysText(PROVIDER.createServer()); |
| 507 | + generateTestNkeysText(PROVIDER.createCluster()); |
| 508 | + } |
| 509 | + } |
| 510 | + |
| 511 | + private static void generateTestNkeysText(NKey theKey) { |
| 512 | + char[] seed = theKey.getSeed(); |
| 513 | + char[] publicKey = theKey.getPublicKey(); |
| 514 | + char[] privateKey = theKey.getPrivateKey(); |
| 515 | + System.out.println(theKey.getType()); |
| 516 | + System.out.println(theKey.getType().prefix); |
| 517 | + System.out.println(new String(seed)); |
| 518 | + System.out.println(new String(publicKey)); |
| 519 | + System.out.println(new String(privateKey)); |
| 520 | + byte[] bytes = theKey.getDecodedSeed().bytes; |
| 521 | + System.out.println(toString(bytes)); |
| 522 | + bytes = theKey.sign(TO_SIGN); |
| 523 | + System.out.println(toString(bytes)); |
| 524 | + } |
| 525 | + |
| 526 | + private static String toString(byte[] bytes) { |
| 527 | + return Arrays.toString(bytes).replace("[", "").replace("]", "").replace(" ", ""); |
| 528 | + } |
| 529 | + |
| 530 | + private byte[] toBytes(String s) { |
| 531 | + String[] split = s.split(","); |
| 532 | + byte[] decoded = new byte[split.length]; |
| 533 | + for (int i = 0; i < split.length; i++) { |
| 534 | + decoded[i] = (byte) Integer.parseInt(split[i]); |
| 535 | + } |
| 536 | + return decoded; |
| 537 | + } |
467 | 538 | } |
0 commit comments