Class NGEPlatform


  • public abstract class NGEPlatform
    extends java.lang.Object
    • Constructor Detail

      • NGEPlatform

        public NGEPlatform()
    • Method Detail

      • set

        public static void set​(NGEPlatform platform)
      • isSet

        public static boolean isSet()
      • setBrowserInterceptor

        public static void setBrowserInterceptor​(BrowserInterceptor interceptor)
      • setStoreInterceptor

        public static void setStoreInterceptor​(VStoreInterceptor interceptor)
      • generatePrivateKey

        public abstract byte[] generatePrivateKey()
      • generatePrivateKeyBuffer

        public java.nio.ByteBuffer generatePrivateKeyBuffer()
        Generates a private key using a buffer-oriented representation.

        The default implementation preserves the existing array implementation. Platforms with a native/shared-buffer boundary should override this method.

      • genPubKey

        public abstract byte[] genPubKey​(byte[] secKey)
      • genPubKey

        public java.nio.ByteBuffer genPubKey​(java.nio.ByteBuffer secKey)
      • toJSON

        public abstract java.lang.String toJSON​(java.util.Collection obj)
        Serializes an owned JSON-compatible tree with JSON.stringify semantics. Java numbers are converted to JavaScript Numbers (binary64); NaN and infinity become null. Ordered maps preserve string-key insertion order, with array-index keys first in numeric order. Use LinkedHashMap when constructing JSON objects; unordered maps cannot recover insertion history.
      • toJSON

        public abstract java.lang.String toJSON​(java.util.Map obj)
      • supportsMinimalJSONEscaping

        public boolean supportsMinimalJSONEscaping()
        Whether the JSON writer escapes only syntax, control characters and unpaired UTF-16 surrogates, without extra HTML or script escaping. Callers must still respect the writer's numeric precision.
      • sha256JSON

        public java.lang.String sha256JSON​(java.util.Collection obj)
        Hashes the UTF-8 JSON representation, allowing native writers to avoid a string round trip.
      • sha256JSONAsync

        public AsyncTask<java.lang.String> sha256JSONAsync​(java.util.Collection obj)
      • fromJSON

        public abstract <T> T fromJSON​(java.lang.String json,
                                       java.lang.Class<T> claz)
        Parses JSON trees; objects returned as maps retain JavaScript property order recursively.
      • parseJsonObject

        public JsonObject parseJsonObject​(java.lang.String json)
        Parses an object with typed reads and owned immutable string rows.
        Throws:
        java.lang.IllegalArgumentException - if the JSON root is not an object
      • secp256k1SharedSecret

        public abstract byte[] secp256k1SharedSecret​(byte[] privKey,
                                                     byte[] pubKey)
      • secp256k1SharedSecret

        public java.nio.ByteBuffer secp256k1SharedSecret​(java.nio.ByteBuffer privKey,
                                                         java.nio.ByteBuffer pubKey)
      • secp256k1PrivateKeyVerify

        public abstract boolean secp256k1PrivateKeyVerify​(byte[] privateKey)
        Verifies whether a secp256k1 private key is valid. A key is valid if it is 32 bytes long and its integer value is in [1, n-1], where n is the secp256k1 curve order.
        Parameters:
        privateKey - raw private key (32 bytes)
        Returns:
        true if valid, otherwise false
      • secp256k1PrivateKeyVerify

        public boolean secp256k1PrivateKeyVerify​(java.nio.ByteBuffer privateKey)
      • secp256k1PublicKeyVerify

        public abstract boolean secp256k1PublicKeyVerify​(byte[] publicKey)
        Verifies whether a secp256k1 public key is valid. It must be an SEC1 compressed key (33 bytes) or uncompressed key (65 bytes), decodable on the curve and not at infinity.
        Parameters:
        publicKey - SEC1 compressed or uncompressed public key
        Returns:
        true if valid, otherwise false
      • secp256k1PublicKeyVerify

        public boolean secp256k1PublicKeyVerify​(java.nio.ByteBuffer publicKey)
      • secp256k1PublicKeyCreate

        public abstract byte[] secp256k1PublicKeyCreate​(byte[] privateKey,
                                                        boolean compressed)
        Derives a secp256k1 public key from a private key.
        Parameters:
        privateKey - raw private key (32 bytes), valid
        compressed - true for compressed output (33 bytes), false for uncompressed output (65 bytes)
        Returns:
        SEC1 public key in the requested format
        Throws:
        java.lang.IllegalArgumentException - if the private key is invalid
      • secp256k1PublicKeyCreate

        public java.nio.ByteBuffer secp256k1PublicKeyCreate​(java.nio.ByteBuffer privateKey,
                                                            boolean compressed)
      • secp256k1SignRecoverable

        public abstract Secp256k1RecoverableSignature secp256k1SignRecoverable​(byte[] hash32,
                                                                               byte[] privateKey)
        Signs a 32-byte hash with ECDSA over secp256k1 and returns a recoverable signature. The signature must be in compact 64-byte format: r(32) || s(32), with low-S normalization (BIP-62 style) for cross-platform stability. recoveryId must be in [0..3].
        Parameters:
        hash32 - message hash (exactly 32 bytes)
        privateKey - raw private key (32 bytes), valid
        Returns:
        recoverable signature containing signature64 and recoveryId
        Throws:
        java.lang.IllegalArgumentException - if inputs are invalid
      • secp256k1SignRecoverable

        public Secp256k1RecoverableSignature secp256k1SignRecoverable​(java.nio.ByteBuffer hash32,
                                                                      java.nio.ByteBuffer privateKey)
      • secp256k1RecoverPublicKey

        public abstract byte[] secp256k1RecoverPublicKey​(byte[] hash32,
                                                         byte[] signature64,
                                                         int recoveryId,
                                                         boolean compressed)
        Recovers a secp256k1 public key from hash + recoverable signature.
        Parameters:
        hash32 - message hash (exactly 32 bytes)
        signature64 - compact 64-byte signature: r||s
        recoveryId - recovery id in [0..3]
        compressed - true for compressed output (33 bytes), false for uncompressed output (65 bytes)
        Returns:
        recovered public key in the requested format
        Throws:
        java.lang.IllegalArgumentException - if inputs are invalid or recovery is impossible
      • secp256k1RecoverPublicKey

        public java.nio.ByteBuffer secp256k1RecoverPublicKey​(java.nio.ByteBuffer hash32,
                                                             java.nio.ByteBuffer signature64,
                                                             int recoveryId,
                                                             boolean compressed)
      • hmac

        public abstract byte[] hmac​(byte[] key,
                                    byte[] data1,
                                    byte[] data2)
      • hmac

        public java.nio.ByteBuffer hmac​(java.nio.ByteBuffer key,
                                        java.nio.ByteBuffer data1,
                                        java.nio.ByteBuffer data2)
      • hkdf_extract

        public abstract byte[] hkdf_extract​(byte[] salt,
                                            byte[] ikm)
      • hkdf_extract

        public java.nio.ByteBuffer hkdf_extract​(java.nio.ByteBuffer salt,
                                                java.nio.ByteBuffer ikm)
      • hkdf_expand

        public abstract byte[] hkdf_expand​(byte[] prk,
                                           byte[] info,
                                           int length)
      • hkdf_expand

        public java.nio.ByteBuffer hkdf_expand​(java.nio.ByteBuffer prk,
                                               java.nio.ByteBuffer info,
                                               int length)
      • base64encode

        public abstract java.lang.String base64encode​(byte[] data)
      • base64encode

        public java.lang.String base64encode​(java.nio.ByteBuffer data)
      • base64decode

        public abstract byte[] base64decode​(java.lang.String data)
      • base64decodeBuffer

        public java.nio.ByteBuffer base64decodeBuffer​(java.lang.String data)
      • chacha20

        public abstract byte[] chacha20​(byte[] key,
                                        byte[] nonce,
                                        byte[] data,
                                        boolean forEncryption)
      • chacha20

        public java.nio.ByteBuffer chacha20​(java.nio.ByteBuffer key,
                                            java.nio.ByteBuffer nonce,
                                            java.nio.ByteBuffer data,
                                            boolean forEncryption)
      • newRTCTransport

        public abstract RTCTransport newRTCTransport​(java.time.Duration p2pAttemptTimeout,
                                                     java.lang.String connId,
                                                     java.util.Collection<java.lang.String> stunServers)
      • sha256

        public abstract java.lang.String sha256​(java.lang.String data)
      • sha256Async

        public AsyncTask<java.lang.String> sha256Async​(java.lang.String data)
      • sha256

        public abstract byte[] sha256​(byte[] data)
      • sha256

        public java.nio.ByteBuffer sha256​(java.nio.ByteBuffer data)
      • createSchnorrSigner

        public SchnorrSigner createSchnorrSigner​(java.util.function.Supplier<java.nio.ByteBuffer> privateKey)
        Creates a signer that may prepare public key state once. The supplier is consulted for every signature, so key destruction remains effective. It must supply the same key throughout the signer's lifetime; no private key snapshot is retained by this factory. Platforms without a prepared implementation preserve their existing signing path.
      • schnorrVerify

        public abstract boolean schnorrVerify​(java.lang.String data,
                                              java.lang.String sign,
                                              byte[] pubKey)
      • schnorrVerify

        public boolean schnorrVerify​(java.lang.String data,
                                     java.lang.String sign,
                                     java.nio.ByteBuffer pubKey)
      • schnorrSignAsync

        public abstract AsyncTask<java.lang.String> schnorrSignAsync​(java.lang.String data,
                                                                     byte[] privKey)
      • schnorrSignAsync

        public AsyncTask<java.lang.String> schnorrSignAsync​(java.lang.String data,
                                                            java.nio.ByteBuffer privKey)
      • schnorrVerifyAsync

        public abstract AsyncTask<java.lang.Boolean> schnorrVerifyAsync​(java.lang.String data,
                                                                        java.lang.String sign,
                                                                        byte[] pubKey)
      • schnorrVerifyAsync

        public AsyncTask<java.lang.Boolean> schnorrVerifyAsync​(java.lang.String data,
                                                               java.lang.String sign,
                                                               java.nio.ByteBuffer pubKey)
      • verify

        @Deprecated
        public boolean verify​(java.lang.String data,
                              java.lang.String sign,
                              byte[] pubKey)
        Deprecated.
        This generically named method is deprecated in favor of schnorrVerify(String, String, byte[]) that does the same
      • signAsync

        @Deprecated
        public AsyncTask<java.lang.String> signAsync​(java.lang.String data,
                                                     byte[] privKey)
        Deprecated.
        This generically named method is deprecated in favor of schnorrSignAsync(String, byte[]) that does the same
      • verifyAsync

        @Deprecated
        public AsyncTask<java.lang.Boolean> verifyAsync​(java.lang.String data,
                                                        java.lang.String sign,
                                                        byte[] pubKey)
        Deprecated.
        This generically named method is deprecated in favor of schnorrVerifyAsync(String, String, byte[]) that does the same
      • randomBytes

        public abstract byte[] randomBytes​(int n)
      • randomBytesBuffer

        public java.nio.ByteBuffer randomBytesBuffer​(int n)
      • newAsyncExecutor

        public final AsyncExecutor newAsyncExecutor()
      • newAsyncExecutor

        public abstract AsyncExecutor newAsyncExecutor​(java.lang.Object hint)
      • promisify

        public abstract <T> AsyncTask<T> promisify​(java.util.function.BiConsumer<java.util.function.Consumer<T>,​java.util.function.Consumer<java.lang.Throwable>> func,
                                                   AsyncExecutor executor)
      • wrapPromise

        public abstract <T> AsyncTask<T> wrapPromise​(java.util.function.BiConsumer<java.util.function.Consumer<T>,​java.util.function.Consumer<java.lang.Throwable>> func)
      • awaitAll

        public <T> AsyncTask<java.util.List<T>> awaitAll​(java.util.List<AsyncTask<T>> promises)
        Waits for all promises to resolve.

        If one of the promises fails, the returned promise is fails with the same error.

        Type Parameters:
        T - the type of the promises
        Parameters:
        promises - the list of promises
        Returns:
        a promise that resolves to a list of results
      • awaitAny

        public <T> AsyncTask<T> awaitAny​(java.util.List<AsyncTask<T>> promises)
        Waits for any promise to resolve.
        Type Parameters:
        T - the type of the promises
        Parameters:
        promises - the list of promises
        Returns:
        a promise that resolves to the result of the first resolved promise

        If all promises fail, the returned promise fails with an exception, otherwise it resolves with the result of the first resolved promise.

      • awaitAny

        public <T> AsyncTask<T> awaitAny​(java.util.List<AsyncTask<T>> promises,
                                         java.util.function.Predicate<T> filter)
        Waits for any promise to resolve and match the filter. Same as awaitAny but with a filter that the result must match. If the result does not match the filter, it is ignored and the next promise is waited for.

        If all promises fail or none match the filter, the returned promise fails with an exception, otherwise it resolves with the result of the first resolved promise that matches the filter.

        Type Parameters:
        T - the type of the promises
        Parameters:
        promises - the list of promises
        filter - the filter to match
        Returns:
        a promise that resolves to the result of the first resolved promise that matches the filter
      • awaitAllSettled

        public <T> AsyncTask<java.util.List<AsyncTask<T>>> awaitAllSettled​(java.util.List<AsyncTask<T>> promises)
        Awaits for all promises to settle (either resolve or reject). The returned promise always resolves with the list of all promises.
        Type Parameters:
        T - the type of the promises
        Parameters:
        promises - the list of promises
        Returns:
        a promise that resolves to the list of all promises
      • getTimestampSeconds

        public abstract long getTimestampSeconds()
      • newConcurrentQueue

        public abstract <T> java.util.Queue<T> newConcurrentQueue​(java.lang.Class<T> claz)
      • httpGet

        public AsyncTask<java.lang.String> httpGet​(java.lang.String url,
                                                   java.time.Duration timeout,
                                                   java.util.Map<java.lang.String,​java.lang.String> headers)
      • httpRequest

        public abstract AsyncTask<NGEHttpResponse> httpRequest​(java.lang.String method,
                                                               java.lang.String inurl,
                                                               byte[] body,
                                                               java.time.Duration timeout,
                                                               java.util.Map<java.lang.String,​java.lang.String> headers)
      • httpRequest

        public AsyncTask<NGEHttpResponse> httpRequest​(java.lang.String method,
                                                      java.lang.String inurl,
                                                      java.time.Duration timeout,
                                                      java.util.Map<java.lang.String,​java.lang.String> headers)
        Issues an HTTP request without a request body.
      • httpRequest

        public AsyncTask<NGEHttpResponse> httpRequest​(java.lang.String method,
                                                      java.lang.String inurl,
                                                      java.nio.ByteBuffer body,
                                                      java.time.Duration timeout,
                                                      java.util.Map<java.lang.String,​java.lang.String> headers)
        Buffer-oriented overload of httpRequest(String, String, byte[], Duration, Map). The default implementation adapts to the established array API; platforms with a direct/shared-buffer HTTP boundary should override it.
      • httpRequestStream

        public abstract AsyncTask<NGEHttpResponseStream> httpRequestStream​(java.lang.String method,
                                                                           java.lang.String inurl,
                                                                           byte[] body,
                                                                           java.time.Duration timeout,
                                                                           java.util.Map<java.lang.String,​java.lang.String> headers)
      • setClipboardContent

        public abstract void setClipboardContent​(java.lang.String data)
      • getClipboardContent

        public abstract AsyncTask<java.lang.String> getClipboardContent()
      • openInWebBrowser

        public abstract void openInWebBrowser​(java.lang.String url)
      • scrypt

        public abstract byte[] scrypt​(byte[] P,
                                      byte[] S,
                                      int N,
                                      int r,
                                      int p,
                                      int dkLen)
      • scrypt

        public java.nio.ByteBuffer scrypt​(java.nio.ByteBuffer password,
                                          java.nio.ByteBuffer salt,
                                          int n,
                                          int r,
                                          int p,
                                          int dkLen)
        Buffer convenience for scrypt. The default adapter is intentionally used by TeaVM as well: scrypt's cost dominates the small password/salt/output transfers, so a separate direct-buffer asynchronous bridge would add complexity without a meaningful performance benefit.
      • xchacha20poly1305

        public abstract byte[] xchacha20poly1305​(byte[] key,
                                                 byte[] nonce,
                                                 byte[] data,
                                                 byte[] associatedData,
                                                 boolean forEncryption)
      • xchacha20poly1305

        public java.nio.ByteBuffer xchacha20poly1305​(java.nio.ByteBuffer key,
                                                     java.nio.ByteBuffer nonce,
                                                     java.nio.ByteBuffer data,
                                                     java.nio.ByteBuffer associatedData,
                                                     boolean forEncryption)
      • nfkc

        public abstract java.lang.String nfkc​(java.lang.String str)
      • getDataStore

        public abstract VStore getDataStore​(java.lang.String appName,
                                            java.lang.String storeName)
      • getCacheStore

        public abstract VStore getCacheStore​(java.lang.String appName,
                                             java.lang.String cacheName)
      • registerFinalizer

        public abstract java.lang.Runnable registerFinalizer​(java.lang.Object obj,
                                                             java.lang.Runnable finalizer)
      • isLoopbackAddress

        public boolean isLoopbackAddress​(java.net.URI uri)
        Checks if the given URI is a loopback address. Polyfill for non jcl compliant platforms. This method should be overriden by a platform specific implementation
        Parameters:
        uri - the URI to check
        Returns:
        true if the URI is a loopback address, false otherwise
      • openResource

        public abstract java.io.InputStream openResource​(java.lang.String resourceName)
                                                  throws java.io.IOException
        Throws:
        java.io.IOException
      • aes256cbc

        public abstract byte[] aes256cbc​(byte[] key,
                                         byte[] iv,
                                         byte[] data,
                                         boolean forEncryption)
        Encrypts or decrypts data using AES-256 in CBC mode with PKCS7 padding.
        Parameters:
        key - A 32-byte key for AES-256
        iv - A 16-byte initialization vector
        data - The data to encrypt or decrypt
        forEncryption - True for encryption, false for decryption
        Returns:
        The encrypted or decrypted data
      • aes256cbc

        public java.nio.ByteBuffer aes256cbc​(java.nio.ByteBuffer key,
                                             java.nio.ByteBuffer iv,
                                             java.nio.ByteBuffer data,
                                             boolean forEncryption)
      • callFunction

        public void callFunction​(java.lang.String function,
                                 java.lang.Object args,
                                 java.util.function.Consumer<java.lang.Object> res,
                                 java.util.function.Consumer<java.lang.Throwable> rej)
        Run platform specific script
      • canCallFunction

        public void canCallFunction​(java.lang.String function,
                                    java.util.function.Consumer<java.lang.Boolean> res)
      • getPlatformName

        public abstract java.lang.String getPlatformName()
      • runInThread

        public void runInThread​(java.lang.Thread thread,
                                java.util.function.Consumer<java.lang.Runnable> enqueue,
                                java.lang.Runnable action)
      • panic

        public void panic​(java.lang.String err)
      • getNativeAllocator

        public abstract NGEAllocator getNativeAllocator()
      • copyRemaining

        protected static byte[] copyRemaining​(java.nio.ByteBuffer input)
      • wrapBinaryResult

        protected static java.nio.ByteBuffer wrapBinaryResult​(byte[] result)