@twin.org/engine-core 0.0.1-next.5 → 0.0.1-next.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1659,7 +1659,518 @@ class MemoryStateStorage {
1659
1659
  }
1660
1660
  }
1661
1661
 
1662
+ // Copyright 2024 IOTA Stiftung.
1663
+ // SPDX-License-Identifier: Apache-2.0.
1664
+ /**
1665
+ * Build the engine core configuration from environment variables.
1666
+ * @param envVars The environment variables.
1667
+ * @returns The config for the core.
1668
+ */
1669
+ function buildEngineCoreConfiguration(envVars) {
1670
+ envVars.stateFilename ??= "engine-state.json";
1671
+ envVars.storageFileRoot = path.resolve(envVars.storageFileRoot);
1672
+ envVars.stateFilename = path.join(envVars.storageFileRoot, envVars.stateFilename);
1673
+ envVars.attestationAssertionMethodId ??= "attestation-assertion";
1674
+ envVars.immutableProofHashKeyId ??= "immutable-proof-hash";
1675
+ envVars.immutableProofAssertionMethodId ??= "immutable-proof-assertion";
1676
+ const coreConfig = {
1677
+ debug: core.Coerce.boolean(envVars.debug) ?? false,
1678
+ loggingConnector: [],
1679
+ loggingComponent: [{ type: engineModels.LoggingComponentType.Service }],
1680
+ entityStorageConnector: [],
1681
+ blobStorageConnector: [],
1682
+ blobStorageComponent: [{ type: engineModels.BlobStorageComponentType.Service }],
1683
+ backgroundTaskConnector: [],
1684
+ telemetryConnector: [],
1685
+ telemetryComponent: [{ type: engineModels.TelemetryComponentType.Service }],
1686
+ vaultConnector: [],
1687
+ walletConnector: [],
1688
+ faucetConnector: [],
1689
+ immutableStorageConnector: [],
1690
+ nftConnector: [],
1691
+ nftComponent: [{ type: engineModels.NftComponentType.Service }],
1692
+ identityConnector: [],
1693
+ identityComponent: [{ type: engineModels.IdentityComponentType.Service }],
1694
+ identityProfileConnector: [],
1695
+ identityProfileComponent: [{ type: engineModels.IdentityProfileComponentType.Service }],
1696
+ immutableProofComponent: [
1697
+ {
1698
+ type: engineModels.ImmutableProofComponentType.Service,
1699
+ options: {
1700
+ config: {
1701
+ assertionMethodId: envVars.immutableProofAssertionMethodId,
1702
+ proofHashKeyId: envVars.immutableProofHashKeyId
1703
+ }
1704
+ }
1705
+ }
1706
+ ],
1707
+ attestationConnector: [],
1708
+ attestationComponent: [
1709
+ {
1710
+ type: engineModels.AttestationComponentType.Service
1711
+ }
1712
+ ],
1713
+ auditableItemGraphComponent: [{ type: engineModels.AuditableItemGraphComponentType.Service }],
1714
+ auditableItemStreamComponent: [{ type: engineModels.AuditableItemStreamComponentType.Service }]
1715
+ };
1716
+ configureEntityStorageConnectors(coreConfig, envVars);
1717
+ configureBlobStorageConnectors(coreConfig, envVars);
1718
+ configureVaultConnectors(coreConfig, envVars);
1719
+ configureLogging(coreConfig, envVars);
1720
+ configureBackgroundTaskConnectors(coreConfig, envVars);
1721
+ configureTelemetryConnectors(coreConfig, envVars);
1722
+ configureFaucetConnectors(coreConfig, envVars);
1723
+ configureWalletConnectors(coreConfig, envVars);
1724
+ configureNftConnectors(coreConfig, envVars);
1725
+ configureImmutableStorageConnectors(coreConfig, envVars);
1726
+ configureIdentityConnectors(coreConfig, envVars);
1727
+ configureIdentityProfileConnectors(coreConfig, envVars);
1728
+ configureAttestationConnectors(coreConfig, envVars);
1729
+ return coreConfig;
1730
+ }
1731
+ /**
1732
+ * Configures the entity storage connectors for the core.
1733
+ * @param coreConfig The core config.
1734
+ * @param envVars The environment variables.
1735
+ */
1736
+ function configureEntityStorageConnectors(coreConfig, envVars) {
1737
+ coreConfig.entityStorageConnector ??= [];
1738
+ coreConfig.entityStorageConnector.push({
1739
+ type: engineModels.EntityStorageConnectorType.Memory
1740
+ });
1741
+ if (core.Is.stringValue(envVars.storageFileRoot)) {
1742
+ coreConfig.entityStorageConnector.push({
1743
+ type: engineModels.EntityStorageConnectorType.File,
1744
+ options: { config: { directory: envVars.storageFileRoot } }
1745
+ });
1746
+ }
1747
+ if (core.Is.stringValue(envVars.awsDynamodbAccessKeyId)) {
1748
+ coreConfig.entityStorageConnector.push({
1749
+ type: engineModels.EntityStorageConnectorType.AwsDynamoDb,
1750
+ options: {
1751
+ config: {
1752
+ region: envVars.awsDynamodbRegion,
1753
+ accessKeyId: envVars.awsDynamodbAccessKeyId,
1754
+ secretAccessKey: envVars.awsDynamodbSecretAccessKey,
1755
+ endpoint: envVars.awsDynamodbEndpoint
1756
+ },
1757
+ tablePrefix: envVars.entityStorageTablePrefix
1758
+ }
1759
+ });
1760
+ }
1761
+ if (core.Is.stringValue(envVars.azureCosmosdbKey)) {
1762
+ coreConfig.entityStorageConnector.push({
1763
+ type: engineModels.EntityStorageConnectorType.AzureCosmosDb,
1764
+ options: {
1765
+ config: {
1766
+ endpoint: envVars.azureCosmosdbEndpoint,
1767
+ key: envVars.azureCosmosdbKey,
1768
+ databaseId: envVars.azureCosmosdbDatabaseId,
1769
+ containerId: envVars.azureCosmosdbContainerId
1770
+ },
1771
+ tablePrefix: envVars.entityStorageTablePrefix
1772
+ }
1773
+ });
1774
+ }
1775
+ if (core.Is.stringValue(envVars.gcpFirestoreCredentials)) {
1776
+ coreConfig.entityStorageConnector.push({
1777
+ type: engineModels.EntityStorageConnectorType.GcpFirestoreDb,
1778
+ options: {
1779
+ config: {
1780
+ projectId: envVars.gcpFirestoreProjectId,
1781
+ credentials: envVars.gcpFirestoreCredentials,
1782
+ databaseId: envVars.gcpFirestoreDatabaseId,
1783
+ collectionName: envVars.gcpFirestoreCollectionName,
1784
+ endpoint: envVars.gcpFirestoreApiEndpoint
1785
+ },
1786
+ tablePrefix: envVars.entityStorageTablePrefix
1787
+ }
1788
+ });
1789
+ }
1790
+ if (core.Is.stringValue(envVars.scylladbHosts)) {
1791
+ coreConfig.entityStorageConnector.push({
1792
+ type: engineModels.EntityStorageConnectorType.ScyllaDb,
1793
+ options: {
1794
+ config: {
1795
+ hosts: envVars.scylladbHosts.split(","),
1796
+ localDataCenter: envVars.scylladbLocalDataCenter,
1797
+ keyspace: envVars.scylladbKeyspace
1798
+ },
1799
+ tablePrefix: envVars.entityStorageTablePrefix
1800
+ }
1801
+ });
1802
+ }
1803
+ const defaultStorageConnector = envVars.entityStorageConnectorType;
1804
+ if (core.Is.stringValue(defaultStorageConnector)) {
1805
+ for (const config of coreConfig.entityStorageConnector) {
1806
+ if (config.type === defaultStorageConnector) {
1807
+ config.isDefault = true;
1808
+ }
1809
+ }
1810
+ }
1811
+ }
1812
+ /**
1813
+ * Configures the blob storage connectors for the core.
1814
+ * @param coreConfig The core config.
1815
+ * @param envVars The environment variables.
1816
+ */
1817
+ function configureBlobStorageConnectors(coreConfig, envVars) {
1818
+ envVars.blobStorageEnableEncryption ??= "false";
1819
+ envVars.blobStorageEncryptionKey ??= "blob-encryption";
1820
+ coreConfig.blobStorageConnector ??= [];
1821
+ coreConfig.blobStorageConnector.push({
1822
+ type: engineModels.BlobStorageConnectorType.Memory
1823
+ });
1824
+ if (core.Is.stringValue(envVars.storageFileRoot)) {
1825
+ coreConfig.blobStorageConnector.push({
1826
+ type: engineModels.BlobStorageConnectorType.File,
1827
+ options: {
1828
+ config: { directory: envVars.storageFileRoot },
1829
+ storagePrefix: envVars.blobStoragePrefix
1830
+ }
1831
+ });
1832
+ }
1833
+ if (core.Is.stringValue(envVars.ipfsApiUrl)) {
1834
+ coreConfig.blobStorageConnector.push({
1835
+ type: engineModels.BlobStorageConnectorType.Ipfs,
1836
+ options: {
1837
+ config: {
1838
+ apiUrl: envVars.ipfsApiUrl,
1839
+ bearerToken: envVars.ipfsBearerToken
1840
+ }
1841
+ }
1842
+ });
1843
+ }
1844
+ if (core.Is.stringValue(envVars.awsS3AccessKeyId)) {
1845
+ coreConfig.blobStorageConnector.push({
1846
+ type: engineModels.BlobStorageConnectorType.AwsS3,
1847
+ options: {
1848
+ config: {
1849
+ region: envVars.awsS3Region,
1850
+ bucketName: envVars.awsS3BucketName,
1851
+ accessKeyId: envVars.awsS3AccessKeyId,
1852
+ secretAccessKey: envVars.awsS3SecretAccessKey,
1853
+ endpoint: envVars.awsS3Endpoint
1854
+ },
1855
+ storagePrefix: envVars.blobStoragePrefix
1856
+ }
1857
+ });
1858
+ }
1859
+ if (core.Is.stringValue(envVars.azureStorageAccountKey)) {
1860
+ coreConfig.blobStorageConnector.push({
1861
+ type: engineModels.BlobStorageConnectorType.AzureStorage,
1862
+ options: {
1863
+ config: {
1864
+ accountName: envVars.azureStorageAccountName,
1865
+ accountKey: envVars.azureStorageAccountKey,
1866
+ containerName: envVars.azureStorageContainerName,
1867
+ endpoint: envVars.azureStorageEndpoint
1868
+ },
1869
+ storagePrefix: envVars.blobStoragePrefix
1870
+ }
1871
+ });
1872
+ }
1873
+ if (core.Is.stringValue(envVars.gcpStorageCredentials)) {
1874
+ coreConfig.blobStorageConnector.push({
1875
+ type: engineModels.BlobStorageConnectorType.GcpStorage,
1876
+ options: {
1877
+ config: {
1878
+ projectId: envVars.gcpStorageProjectId,
1879
+ credentials: envVars.gcpStorageCredentials,
1880
+ bucketName: envVars.gcpStorageBucketName,
1881
+ apiEndpoint: envVars.gcpFirestoreApiEndpoint
1882
+ },
1883
+ storagePrefix: envVars.blobStoragePrefix
1884
+ }
1885
+ });
1886
+ }
1887
+ const defaultStorageConnectorType = envVars.blobStorageConnectorType;
1888
+ if (core.Is.stringValue(defaultStorageConnectorType)) {
1889
+ for (const config of coreConfig.blobStorageConnector) {
1890
+ if (config.type === defaultStorageConnectorType) {
1891
+ config.isDefault = true;
1892
+ }
1893
+ }
1894
+ }
1895
+ }
1896
+ /**
1897
+ * Configures the logging connectors for the core.
1898
+ * @param coreConfig The core config.
1899
+ * @param envVars The environment variables.
1900
+ */
1901
+ function configureLogging(coreConfig, envVars) {
1902
+ const loggingConnectors = (envVars.loggingConnector ?? "").split(",");
1903
+ for (const loggingConnector of loggingConnectors) {
1904
+ if (loggingConnector === engineModels.LoggingConnectorType.Console) {
1905
+ coreConfig.loggingConnector?.push({
1906
+ type: engineModels.LoggingConnectorType.Console,
1907
+ options: {
1908
+ config: {
1909
+ translateMessages: true,
1910
+ hideGroups: true
1911
+ }
1912
+ }
1913
+ });
1914
+ }
1915
+ else if (loggingConnector === engineModels.LoggingConnectorType.EntityStorage) {
1916
+ coreConfig.loggingConnector?.push({
1917
+ type: engineModels.LoggingConnectorType.EntityStorage
1918
+ });
1919
+ }
1920
+ }
1921
+ if (loggingConnectors.length > 1) {
1922
+ coreConfig.loggingConnector?.push({
1923
+ type: engineModels.LoggingConnectorType.Multi,
1924
+ isDefault: true,
1925
+ options: {
1926
+ loggingConnectorTypes: loggingConnectors
1927
+ }
1928
+ });
1929
+ }
1930
+ }
1931
+ /**
1932
+ * Configures the vault connectors for the core.
1933
+ * @param coreConfig The core config.
1934
+ * @param envVars The environment variables.
1935
+ */
1936
+ function configureVaultConnectors(coreConfig, envVars) {
1937
+ coreConfig.vaultConnector ??= [];
1938
+ if (envVars.vaultConnector === engineModels.VaultConnectorType.EntityStorage) {
1939
+ coreConfig.vaultConnector.push({
1940
+ type: engineModels.VaultConnectorType.EntityStorage
1941
+ });
1942
+ }
1943
+ else if (envVars.vaultConnector === engineModels.VaultConnectorType.Hashicorp) {
1944
+ coreConfig.vaultConnector.push({
1945
+ type: engineModels.VaultConnectorType.Hashicorp,
1946
+ options: {
1947
+ config: { endpoint: envVars.hashicorpVaultEndpoint, token: envVars.hashicorpVaultToken }
1948
+ }
1949
+ });
1950
+ }
1951
+ }
1952
+ /**
1953
+ * Configures the background task connectors for the core.
1954
+ * @param coreConfig The core config.
1955
+ * @param envVars The environment variables.
1956
+ */
1957
+ function configureBackgroundTaskConnectors(coreConfig, envVars) {
1958
+ coreConfig.backgroundTaskConnector ??= [];
1959
+ if (envVars.backgroundTaskConnector === engineModels.BackgroundTaskConnectorType.EntityStorage) {
1960
+ coreConfig.backgroundTaskConnector.push({
1961
+ type: engineModels.BackgroundTaskConnectorType.EntityStorage
1962
+ });
1963
+ }
1964
+ }
1965
+ /**
1966
+ * Configures the telemetry connectors for the core.
1967
+ * @param coreConfig The core config.
1968
+ * @param envVars The environment variables.
1969
+ */
1970
+ function configureTelemetryConnectors(coreConfig, envVars) {
1971
+ coreConfig.telemetryConnector ??= [];
1972
+ if (envVars.telemetryConnector === engineModels.TelemetryConnectorType.EntityStorage) {
1973
+ coreConfig.telemetryConnector.push({
1974
+ type: engineModels.TelemetryConnectorType.EntityStorage
1975
+ });
1976
+ }
1977
+ }
1978
+ /**
1979
+ * Configures the faucet connectors for the core.
1980
+ * @param coreConfig The core config.
1981
+ * @param envVars The environment variables.
1982
+ */
1983
+ function configureFaucetConnectors(coreConfig, envVars) {
1984
+ coreConfig.faucetConnector ??= [];
1985
+ if (envVars.faucetConnector === engineModels.FaucetConnectorType.EntityStorage) {
1986
+ coreConfig.faucetConnector.push({
1987
+ type: engineModels.FaucetConnectorType.EntityStorage
1988
+ });
1989
+ }
1990
+ else if (envVars.faucetConnector === engineModels.FaucetConnectorType.Iota) {
1991
+ coreConfig.faucetConnector.push({
1992
+ type: engineModels.FaucetConnectorType.Iota,
1993
+ options: {
1994
+ config: {
1995
+ endpoint: envVars.iotaFaucetEndpoint,
1996
+ clientOptions: {
1997
+ nodes: [envVars.iotaNodeEndpoint]
1998
+ },
1999
+ bech32Hrp: envVars.iotaBech32Hrp,
2000
+ coinType: core.Coerce.number(envVars.iotaCoinType)
2001
+ }
2002
+ }
2003
+ });
2004
+ }
2005
+ }
2006
+ /**
2007
+ * Configures the wallet connectors for the core.
2008
+ * @param coreConfig The core config.
2009
+ * @param envVars The environment variables.
2010
+ */
2011
+ function configureWalletConnectors(coreConfig, envVars) {
2012
+ coreConfig.walletConnector ??= [];
2013
+ if (envVars.walletConnector === engineModels.WalletConnectorType.EntityStorage) {
2014
+ coreConfig.walletConnector.push({
2015
+ type: engineModels.WalletConnectorType.EntityStorage
2016
+ });
2017
+ }
2018
+ else if (envVars.walletConnector === engineModels.WalletConnectorType.Iota) {
2019
+ coreConfig.walletConnector.push({
2020
+ type: engineModels.WalletConnectorType.Iota,
2021
+ options: {
2022
+ config: {
2023
+ clientOptions: {
2024
+ nodes: [envVars.iotaNodeEndpoint]
2025
+ },
2026
+ bech32Hrp: envVars.iotaBech32Hrp,
2027
+ coinType: core.Coerce.number(envVars.iotaCoinType)
2028
+ }
2029
+ }
2030
+ });
2031
+ }
2032
+ }
2033
+ /**
2034
+ * Configures the NFT connectors for the core.
2035
+ * @param coreConfig The core config.
2036
+ * @param envVars The environment variables.
2037
+ */
2038
+ function configureNftConnectors(coreConfig, envVars) {
2039
+ coreConfig.nftConnector ??= [];
2040
+ if (envVars.nftConnector === engineModels.NftConnectorType.EntityStorage) {
2041
+ coreConfig.nftConnector.push({
2042
+ type: engineModels.NftConnectorType.EntityStorage
2043
+ });
2044
+ }
2045
+ else if (envVars.nftConnector === engineModels.NftConnectorType.Iota) {
2046
+ coreConfig.nftConnector.push({
2047
+ type: engineModels.NftConnectorType.Iota,
2048
+ options: {
2049
+ config: {
2050
+ clientOptions: {
2051
+ nodes: [envVars.iotaNodeEndpoint]
2052
+ },
2053
+ bech32Hrp: envVars.iotaBech32Hrp,
2054
+ coinType: core.Coerce.number(envVars.iotaCoinType)
2055
+ }
2056
+ }
2057
+ });
2058
+ }
2059
+ }
2060
+ /**
2061
+ * Configures the immutable storage connectors for the core.
2062
+ * @param coreConfig The core config.
2063
+ * @param envVars The environment variables.
2064
+ */
2065
+ function configureImmutableStorageConnectors(coreConfig, envVars) {
2066
+ coreConfig.immutableStorageConnector ??= [];
2067
+ if (envVars.immutableStorageConnector === engineModels.ImmutableStorageConnectorType.EntityStorage) {
2068
+ coreConfig.immutableStorageConnector.push({
2069
+ type: engineModels.ImmutableStorageConnectorType.EntityStorage
2070
+ });
2071
+ }
2072
+ else if (envVars.immutableStorageConnector === engineModels.ImmutableStorageConnectorType.Iota) {
2073
+ coreConfig.immutableStorageConnector.push({
2074
+ type: engineModels.ImmutableStorageConnectorType.Iota,
2075
+ options: {
2076
+ config: {
2077
+ clientOptions: {
2078
+ nodes: [envVars.iotaNodeEndpoint]
2079
+ },
2080
+ bech32Hrp: envVars.iotaBech32Hrp,
2081
+ coinType: core.Coerce.number(envVars.iotaCoinType)
2082
+ }
2083
+ }
2084
+ });
2085
+ }
2086
+ }
2087
+ /**
2088
+ * Configures the identity connectors for the core.
2089
+ * @param coreConfig The core config.
2090
+ * @param envVars The environment variables.
2091
+ */
2092
+ function configureIdentityConnectors(coreConfig, envVars) {
2093
+ coreConfig.identityConnector ??= [];
2094
+ if (envVars.identityConnector === engineModels.IdentityConnectorType.EntityStorage) {
2095
+ coreConfig.identityConnector.push({
2096
+ type: engineModels.IdentityConnectorType.EntityStorage
2097
+ });
2098
+ }
2099
+ else if (envVars.identityConnector === engineModels.IdentityConnectorType.Iota) {
2100
+ coreConfig.identityConnector.push({
2101
+ type: engineModels.IdentityConnectorType.Iota,
2102
+ options: {
2103
+ config: {
2104
+ clientOptions: {
2105
+ nodes: [envVars.iotaNodeEndpoint]
2106
+ },
2107
+ bech32Hrp: envVars.iotaBech32Hrp,
2108
+ coinType: core.Coerce.number(envVars.iotaCoinType)
2109
+ }
2110
+ }
2111
+ });
2112
+ }
2113
+ }
2114
+ /**
2115
+ * Configures the identity profile connectors for the core.
2116
+ * @param coreConfig The core config.
2117
+ * @param envVars The environment variables.
2118
+ */
2119
+ function configureIdentityProfileConnectors(coreConfig, envVars) {
2120
+ coreConfig.identityProfileConnector ??= [];
2121
+ if (envVars.identityProfileConnector === engineModels.IdentityConnectorType.EntityStorage) {
2122
+ coreConfig.identityProfileConnector.push({
2123
+ type: engineModels.IdentityProfileConnectorType.EntityStorage
2124
+ });
2125
+ }
2126
+ }
2127
+ /**
2128
+ * Configures the attestation connectors for the core.
2129
+ * @param coreConfig The core config.
2130
+ * @param envVars The environment variables.
2131
+ */
2132
+ function configureAttestationConnectors(coreConfig, envVars) {
2133
+ coreConfig.attestationConnector ??= [];
2134
+ if (envVars.attestationConnector === engineModels.AttestationConnectorType.EntityStorage) {
2135
+ coreConfig.attestationConnector.push({
2136
+ type: engineModels.AttestationConnectorType.EntityStorage
2137
+ });
2138
+ }
2139
+ else if (envVars.attestationConnector === engineModels.AttestationConnectorType.Iota) {
2140
+ coreConfig.attestationConnector.push({
2141
+ type: engineModels.AttestationConnectorType.Iota
2142
+ });
2143
+ }
2144
+ }
2145
+
2146
+ // Copyright 2024 IOTA Stiftung.
2147
+ // SPDX-License-Identifier: Apache-2.0.
2148
+ // SPDX-License-Identifier: Apache-2.0.
2149
+ /**
2150
+ * Environment variable helper.
2151
+ */
2152
+ class EnvHelper {
2153
+ /**
2154
+ * Get the environment variable as an object with camel cased names.
2155
+ * @param envVars The environment variables.
2156
+ * @param prefix The prefix of the environment variables.
2157
+ * @returns The object with camel cased names.
2158
+ */
2159
+ envToJson(envVars, prefix) {
2160
+ const result = {};
2161
+ for (const envVar in envVars) {
2162
+ if (envVar.startsWith(prefix) && core.Is.stringValue(process.env[envVar])) {
2163
+ const camelCaseName = core.StringHelper.camelCase(envVar.replace(prefix, "").toLowerCase());
2164
+ core.ObjectHelper.propertySet(result, camelCaseName, process.env[envVar]);
2165
+ }
2166
+ }
2167
+ return result;
2168
+ }
2169
+ }
2170
+
1662
2171
  exports.EngineCore = EngineCore;
2172
+ exports.EnvHelper = EnvHelper;
1663
2173
  exports.FileStateStorage = FileStateStorage;
1664
2174
  exports.MemoryStateStorage = MemoryStateStorage;
2175
+ exports.buildEngineCoreConfiguration = buildEngineCoreConfiguration;
1665
2176
  exports.initialiseEntityStorageConnector = initialiseEntityStorageConnector;