The AdaptEnvironment class provides a set of static methods for interacting with the ADAPT framework.
Important! ADAPT must be initialized using either the Initialize or the InitializeAsync method. Otherwise, any operations such as creating AdaptValue, creating AdaptPacketContext, executing transactions, etc. fail.
Table of contents
Methods
- Check
- CreatePacket
- EmptyPacket
- GetRandomBytes
- Initialize
- InitializeAsync
- ParseTime
- SystemTime
- getStderrContents
- getStdoutContents
Methods
Check
▸ Static
Check(): void
Check whether ADAPT is initialized
Throws
An error if ADAPT is not initialized (see Initialize or InitializeAsync)
Example
AdaptEnvironment.InitializeAsync().then(() => {
AdaptEnvironment.Check(); // ok, no errors
})
Example
// AdaptEnvironment is not yet initialized at this moment
AdaptEnvironment.Check(); // will throw an error
Returns
void
Defined in
CreatePacket
▸ Static
CreatePacket(lt
, unit
, seed_phrase
, entropy
, secure
): AdaptPacketContext
Create an ADAPT packet from a given AdaptEvaluationUnit
Throws
An error if ADAPT is not initialized (see Initialize or InitializeAsync)
Example
Create a packet from the loaded AdaptEvaluationUnit
AdaptEnvironment.InitializeAsync().then(() => {
const lifetime = new AdaptObjectLifetime();
const adaptEvaluationUnitFileName = "./this/is/path/to/adapt/compiled/code.muflo"
const unit = AdaptEvaluationUnit.LoadFromFile(lifetime, adaptevaluationUnitFileName);
const entropyAdaptValue = AdaptEnvironment.GetRandomBytes(lifetime, 256);
const entropyString = entropyAdaptValue.Visualize().substring(2); // substring(2) is used to get rid of '0x' prefix printed by MUFL.
const packet = AdaptEnvironment.CreatePacket(lifetime, unit, "this is seed phrase, you can pass here whatever you want", entropyString, true);
lifetime.Finalize();
})
Parameters
Name | Type | Description |
---|---|---|
lt |
undefined | AdaptObjectLifetime |
The lifetime object to attach the packet to. If undefined is passed, the packet must be destroyed (see Destroy) manually. |
unit |
AdaptEvaluationUnit |
The compiled MUFL application |
seed_phrase |
string |
A seed phrase. Can be any string including an empty string. |
entropy |
string |
256 random bytes of entropy generated using the GetRandomBytes method |
secure |
boolean |
Determines whether packet should be secure. Secure packets forbid calling functions defined in the packet (including primitives). |
Returns
Defined in
EmptyPacket
▸ Static
EmptyPacket(lt
, secure
): AdaptPacketContext
Create a special “empty” type of packet that doesn’t require any MUFL code. It also does not contain any functions or transactions. MUFL primivies could be called in that packet just in case the packet is marked as not secured.
Throws
An error if ADAPT is not initialized (see Initialize or InitializeAsync)
Example
Create a not secure empty packet and call a primitive in the packet
AdaptEnvironment.InitializeAsync().then(() => {
const lifetime = new AdaptObjectLifetime();
const packet = AdaptEnvironment.EmptyPacket(lifetime, false);
const primitiveCallResult = packet.ExecuteFunction("::_strlen", ["hello"]);
console.log(primitiveCallResult.Visualize()); // prints 5 to stdout
lifetime.Finalize(); // to destroy all objects stored in the lifetime object
})
Parameters
Name | Type | Description |
---|---|---|
lt |
undefined | AdaptObjectLifetime |
The lifetime object to attach the packet to. If undefined is passed, the packet must be destroyed (see Destroy) manually. |
secure |
boolean |
Determines whether the packet should be secure. Secure packets forbid calling functions defined in the packet (including primitives). |
Returns
The created packet
Defined in
GetRandomBytes
▸ Static
GetRandomBytes(lt
, length
): AdaptValue
Get cryptographically secure random bytes. Bytes are generated using different methods depending on the current environment. In native and browser environments, the Sodium library is used, whereas the nitro build uses AWS Nitro Enclave’s native NSM library.
Throws
An error if ADAPT is not initialized (see Initialize or InitializeAsync)
Example
AdaptEnvironment.InitializeAsync().then(() => {
const lifetime = new AdaptObjectLifetime();
const bytes = AdaptEnvironment.GetRandomBytes(lifetime, 256);
lifetime.Finalize();
})
Parameters
Name | Type | Description |
---|---|---|
lt |
undefined | AdaptObjectLifetime |
The lifetime object to attach the returned value to. If undefined is passed, the value must be destroyed (see Destroy) manually. |
length |
number |
The length of the required byte sequence |
Returns
A sequence of random bytes represented as an AdaptValue of domain BINARY
Defined in
Initialize
▸ Static
Initialize(test_mode
): boolean
Synchronously initialize ADAPT in either test or release mode depending on the provided parameter.
This function is not available in the Wasm version of the API
Example
Synchronously initializing ADAPT
AdaptEnvironment.Initialize();
// ADAPT environment is initialized and ready to use
Parameters
Name | Type | Description |
---|---|---|
test_mode |
boolean |
Determines whether ADAPT must be initialized in test mode |
Returns
boolean
True if initialization was successful, otherwise false
Defined in
InitializeAsync
▸ Static
InitializeAsync(test_mode
): Promise
<void
>
Asynchronously initialize ADAPT in either test or release mode depending on the provided parameter.
Example
Initialization using async/await syntax
async () => {
await AdaptEnvironment.InitializeAsync();
// ADAPT is initialized now
}
Example
Initialization using JavaScript Promise syntax
AdaptEnvironment.InitializeAsync().then(() => {
// ADAPT is initialized now
})
Parameters
Name | Type | Description |
---|---|---|
test_mode |
boolean |
Determines whether ADAPT must be initialized in test mode |
Returns
Promise
<void
>
Defined in
ParseTime
▸ Static
ParseTime(lt
, time
): AdaptValue
Parse a time represented as string in the YYYY-MM-DD hh:mm:ss[.nnnnnnnnn] (UTC[+x])
format
Throws
An error if either ADAPT is not initialized, or parsing failed.
Example
AdaptEnvironment.InitializeAsync().then(() => {
const lifetime = new AdaptObjectLifetime();
const time = AdaptEnvironment.ParseTime(lifetime, "2022-01-01 12:05:59.123456789 (UTC)");
lifetime.Finalize();
})
Example
AdaptEnvironment.InitializeAsync().then(() => {
const lifetime = new AdaptObjectLifetime();
const time = AdaptEnvironment.ParseTime(lifetime, "2022-01-01 12:05:59 (UTC)");
lifetime.Finalize();
})
Example
AdaptEnvironment.InitializeAsync().then(() => {
const lifetime = new AdaptObjectLifetime();
const time = AdaptEnvironment.ParseTime(lifetime, "2022-01-01 12:05:59 (UTC-2)");
lifetime.Finalize();
})
Example
AdaptEnvironment.InitializeAsync().then(() => {
const lifetime = new AdaptObjectLifetime();
const time = AdaptEnvironment.ParseTime(lifetime, "2022-01-01 12:05:59 (GMT-2)");
lifetime.Finalize();
})
Example
invalid format
AdaptEnvironment.InitializeAsync().then(() => {
const lifetime = new AdaptObjectLifetime();
AdaptEnvironment.ParseTime(lifetime, "2022-01-01 12:05:59"); // ERROR - invalid format
AdaptEnvironment.ParseTime(lifetime, "2022-01-01 12:05:59 (utc)"); // ERROR - invalid format
AdaptEnvironment.ParseTime(lifetime, "2022-01-01 12:05:59 utc"); // ERROR - invalid format
AdaptEnvironment.ParseTime(lifetime, "2022-01-01 12:05:59 (UTC)"); // ERROR - invalid format
AdaptEnvironment.ParseTime(lifetime, "01-01-2022 12:05:59"); // ERROR - invalid format
lifetime.Finalize();
})
Parameters
Name | Type | Description |
---|---|---|
lt |
undefined | AdaptObjectLifetime |
The lifetime object to attach the returned value to. If undefined is passed, the value must be destroyed (see Destroy) manually. |
time |
string |
The current timestamp in the YYYY-MM-DD hh:mm:ss[.nnnnnnnnn] (UTC[+x]) format |
Returns
The parsed time as AdaptValue of domain TIME
Defined in
SystemTime
▸ Static
SystemTime(lt
): AdaptValue
Create an ADAPT value representing the current system time.
Throws
An error if ADAPT is not initialized (see Initialize or InitializeAsync)
Example
AdaptEnvironment.InitializeAsync().then(() => {
const lifetime = new AdaptObjectLifetime();
const time = AdaptEnvironment.SystemTime(lifetime);
lifetime.Finalize();
})
Parameters
Name | Type | Description |
---|---|---|
lt |
undefined | AdaptObjectLifetime |
The lifetime object to attach the returned value to. If undefined is passed, the value must be destroyed (see Destroy) manually. |
Returns
The AdaptValue of domain Time representing current system time.
Defined in
getStderrContents
▸ Static
getStderrContents(): string
Get error output from ADAPT. ADAPT does not print the output directly to the stdout/stderr. Instead, it stores the output in corresponding buffers and returns the content of buffers on request.
Throws
An error if ADAPT is not initialized (see Initialize or InitializeAsync)
Example
AdaptEnvironment.InitializeAsync().then(() => {
const stdout = AdaptEnvironment.getStderrContents();
})
Returns
string
The content of stderr buffer produced by ADAPT
Defined in
getStdoutContents
▸ Static
getStdoutContents(): string
Get standard output from ADAPT. ADAPT does not print the output directly to the stdout/stderr. Instead, it stores the output in corresponding buffers and returns the content of buffers on request.
Throws
An error if ADAPT is not initialized (see Initialize or InitializeAsync)
Example
AdaptEnvironment.InitializeAsync().then(() => {
const stdout = AdaptEnvironment.getStdoutContents();
})
Returns
string
The content of stdout buffer produced by ADAPT