1010import lombok .NonNull ;
1111import lombok .Value ;
1212import org .stellar .sdk .exception .UnexpectedException ;
13- import org .stellar .sdk .xdr .Uint256 ;
14- import org .stellar .sdk .xdr .Uint64 ;
15- import org .stellar .sdk .xdr .XdrUnsignedHyperInteger ;
1613
1714/**
1815 * StrKey is a helper class that allows encoding and decoding Stellar keys to/from strings, i.e.
1916 * between their binary and string (i.e. "GABCD...", etc.) representations.
2017 */
2118public class StrKey {
2219
20+ private static final BigInteger UINT64_MAX = new BigInteger ("18446744073709551615" );
2321 private static final byte [] b32Table = decodingTable ();
2422 private static final Base32 base32Codec = Base32Factory .getInstance ();
2523
@@ -584,11 +582,22 @@ private static boolean isInAlphabet(final byte[] arrayOctet) {
584582
585583 @ Value
586584 static class RawMuxedAccountStrKeyParameter {
587- @ NonNull Uint256 ed25519 ;
588- @ NonNull Uint64 id ;
585+ byte @ NonNull [] ed25519 ;
586+ @ NonNull BigInteger id ;
589587 }
590588
591589 static byte [] toRawMuxedAccountStrKey (RawMuxedAccountStrKeyParameter parameter ) {
590+ byte [] ed25519Bytes = parameter .getEd25519 ();
591+ if (ed25519Bytes .length != 32 ) {
592+ throw new IllegalArgumentException (
593+ "Muxed account ed25519 bytes must be 32 bytes long, got " + ed25519Bytes .length );
594+ }
595+ if (parameter .getId ().compareTo (BigInteger .ZERO ) < 0
596+ || parameter .getId ().compareTo (UINT64_MAX ) > 0 ) {
597+ throw new IllegalArgumentException (
598+ "Muxed account ID must be between 0 and 2^64 - 1 inclusive" );
599+ }
600+
592601 // Get the 64-bit ID. This is the critical part of the explanation.
593602 //
594603 // THE KEY INSIGHT: Why using .longValue() is safe for a uint64
@@ -633,8 +642,7 @@ static byte[] toRawMuxedAccountStrKey(RawMuxedAccountStrKeyParameter parameter)
633642 // buffer,
634643 // it correctly serializes the original uint64 value into 8 bytes, regardless of whether Java
635644 // interpreted the intermediate `long` as positive or negative.
636- long idLong = parameter .getId ().getUint64 ().getNumber ().longValue ();
637- byte [] ed25519Bytes = parameter .getEd25519 ().getUint256 ();
645+ long idLong = parameter .getId ().longValue ();
638646 return ByteBuffer .allocate (ed25519Bytes .length + 8 ).put (ed25519Bytes ).putLong (idLong ).array ();
639647 }
640648
@@ -648,9 +656,7 @@ static RawMuxedAccountStrKeyParameter fromRawMuxedAccountStrKey(byte @NonNull []
648656 buffer .get (ed25519Bytes );
649657 byte [] idBytes = new byte [8 ];
650658 buffer .get (idBytes );
651- Uint256 ed25519 = new Uint256 (ed25519Bytes );
652- Uint64 id = new Uint64 (new XdrUnsignedHyperInteger (new BigInteger (1 , idBytes )));
653- return new RawMuxedAccountStrKeyParameter (ed25519 , id );
659+ return new RawMuxedAccountStrKeyParameter (ed25519Bytes , new BigInteger (1 , idBytes ));
654660 }
655661
656662 enum VersionByte {
0 commit comments