@midnightntwrk/midnight-js-bundled-contract-module-provider 0.0.0-stage → 5.0.1

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.
package/README.md CHANGED
@@ -1,3 +1,42 @@
1
- # Temporary Holding Version
1
+ # Bundled Contract Module Provider
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
3
+ Resolves a cross-contract callee to one of the compiled contract modules bundled with your
4
+ application.
5
+
6
+ A deployment puts no code on chain, so nothing on chain says which module implements the contract at
7
+ an address — the application does, by listing the ones it ships. This provider turns that list into
8
+ the `ContractModuleProvider` the Compact runtime consults at each cross-contract call.
9
+
10
+ ## Installation
11
+
12
+ ```bash
13
+ yarn add @midnightntwrk/midnight-js-bundled-contract-module-provider
14
+ ```
15
+
16
+ ## Quick Start
17
+
18
+ ```typescript
19
+ import {
20
+ bundledContractModuleProvider,
21
+ type ModulesByAddress
22
+ } from '@midnightntwrk/midnight-js-bundled-contract-module-provider';
23
+
24
+ // Generated: literal `import()` specifiers, so a bundler splits each implementation into its own
25
+ // chunk and fetches it only if a call reaches that address.
26
+ const MODULES_BY_ADDRESS: ModulesByAddress = new Map([
27
+ ['0200d4...', () => import('./managed/dex/contract/index.js')],
28
+ ['0200a1...', () => import('./managed/token/contract/index.js')]
29
+ ]);
30
+
31
+ const providers = {
32
+ // ...the rest of your providers
33
+ contractModuleProvider: bundledContractModuleProvider(MODULES_BY_ADDRESS)
34
+ };
35
+ ```
36
+
37
+ The table is read once, when the provider is built, so keys are validated then rather than at the
38
+ call: a mistyped address is otherwise indistinguishable from one with nothing deployed at it. Two
39
+ keys differing only in case are an error unless they name the same module.
40
+
41
+ An address the table does not hold resolves to `undefined`, which the runtime reports as an
42
+ unsupported implementation, naming the call it could not bind.
@@ -0,0 +1,31 @@
1
+ import { ModuleThunk, ContractModuleProvider } from '@midnightntwrk/midnight-js-protocol/v9/compact-runtime';
2
+
3
+ /**
4
+ * Which module implements the contract deployed at each address. A deployment puts no code on
5
+ * chain, so this table is the only place the association exists, and only the application has it.
6
+ *
7
+ * Each entry is a thunk over a literal `import()` specifier, so a bundler sees every edge, splits
8
+ * the chunk, and fetches an implementation only if a call actually reaches it. Build tooling
9
+ * generates the table; nothing here executes an entry.
10
+ */
11
+ type ModulesByAddress = ReadonlyMap<string, ModuleThunk>;
12
+ /**
13
+ * Resolves a cross-contract callee to one of the modules bundled with this application, listed in a
14
+ * {@link ModulesByAddress} table. Addresses are matched case-insensitively.
15
+ *
16
+ * The table is read once, here, so the provider is a snapshot: an entry added to the source map
17
+ * afterwards is not resolved. Every key is checked to be a contract address at that point too,
18
+ * because a mistyped one is otherwise indistinguishable at the call from an address with no
19
+ * implementation deployed at it.
20
+ *
21
+ * @param modules The address-to-module table.
22
+ * @returns A provider resolving an address to its thunk, or `undefined` when the table holds no
23
+ * entry for it — which the runtime reports as an unsupported implementation.
24
+ * @throws InvalidArgumentError If a key is not a contract address.
25
+ * @throws InvalidArgumentError If two keys differ only in case and name different modules, since one of them would
26
+ * otherwise be dropped silently.
27
+ */
28
+ declare const bundledContractModuleProvider: (modules: ModulesByAddress) => ContractModuleProvider;
29
+
30
+ export { bundledContractModuleProvider };
31
+ export type { ModulesByAddress };
package/dist/index.js ADDED
@@ -0,0 +1,58 @@
1
+ import { InvalidArgumentError } from '@midnightntwrk/midnight-js-protocol/errors';
2
+ import { assertIsContractAddress } from '@midnightntwrk/midnight-js-utils';
3
+
4
+ /*
5
+ * This file is part of midnight-js.
6
+ * Copyright (C) Midnight Foundation
7
+ * SPDX-License-Identifier: Apache-2.0
8
+ * Licensed under the Apache License, Version 2.0 (the "License");
9
+ * You may not use this file except in compliance with the License.
10
+ * You may obtain a copy of the License at
11
+ * http://www.apache.org/licenses/LICENSE-2.0
12
+ * Unless required by applicable law or agreed to in writing, software
13
+ * distributed under the License is distributed on an "AS IS" BASIS,
14
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
+ * See the License for the specific language governing permissions and
16
+ * limitations under the License.
17
+ */
18
+ /** Addresses are hex, so the table and the chain can only differ in case. */
19
+ const normalizeAddress = (address) => address.toLowerCase();
20
+ /**
21
+ * Resolves a cross-contract callee to one of the modules bundled with this application, listed in a
22
+ * {@link ModulesByAddress} table. Addresses are matched case-insensitively.
23
+ *
24
+ * The table is read once, here, so the provider is a snapshot: an entry added to the source map
25
+ * afterwards is not resolved. Every key is checked to be a contract address at that point too,
26
+ * because a mistyped one is otherwise indistinguishable at the call from an address with no
27
+ * implementation deployed at it.
28
+ *
29
+ * @param modules The address-to-module table.
30
+ * @returns A provider resolving an address to its thunk, or `undefined` when the table holds no
31
+ * entry for it — which the runtime reports as an unsupported implementation.
32
+ * @throws InvalidArgumentError If a key is not a contract address.
33
+ * @throws InvalidArgumentError If two keys differ only in case and name different modules, since one of them would
34
+ * otherwise be dropped silently.
35
+ */
36
+ const bundledContractModuleProvider = (modules) => {
37
+ const byAddress = new Map();
38
+ for (const [address, module] of modules) {
39
+ try {
40
+ assertIsContractAddress(address);
41
+ }
42
+ catch (cause) {
43
+ // Re-thrown to name the key: the underlying message reports the shape that was wrong, not
44
+ // which of a generated table's entries carried it.
45
+ throw new InvalidArgumentError(`Contract module table has a key that is not a contract address: '${address}'`, { cause });
46
+ }
47
+ const key = normalizeAddress(address);
48
+ const collision = byAddress.get(key);
49
+ if (collision !== undefined && collision !== module) {
50
+ throw new InvalidArgumentError(`Two modules are registered for contract address '${key}', differing only in case`);
51
+ }
52
+ byAddress.set(key, module);
53
+ }
54
+ return { resolve: (address) => byAddress.get(normalizeAddress(address)) };
55
+ };
56
+
57
+ export { bundledContractModuleProvider };
58
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sources":["../src/bundled-contract-module-provider.ts"],"sourcesContent":[null],"names":[],"mappings":";;;AAAA;;;;;;;;;;;;;AAaG;AAgBH;AACA,MAAM,gBAAgB,GAAG,CAAC,OAAe,KAAa,OAAO,CAAC,WAAW,EAAE;AAE3E;;;;;;;;;;;;;;;AAeG;AACI,MAAM,6BAA6B,GAAG,CAAC,OAAyB,KAA4B;AACjG,IAAA,MAAM,SAAS,GAAG,IAAI,GAAG,EAAuB;IAChD,KAAK,MAAM,CAAC,OAAO,EAAE,MAAM,CAAC,IAAI,OAAO,EAAE;AACvC,QAAA,IAAI;YACF,uBAAuB,CAAC,OAAO,CAAC;QAClC;QAAE,OAAO,KAAK,EAAE;;;YAGd,MAAM,IAAI,oBAAoB,CAAC,CAAA,iEAAA,EAAoE,OAAO,CAAA,CAAA,CAAG,EAAE,EAAE,KAAK,EAAE,CAAC;QAC3H;AACA,QAAA,MAAM,GAAG,GAAG,gBAAgB,CAAC,OAAO,CAAC;QACrC,MAAM,SAAS,GAAG,SAAS,CAAC,GAAG,CAAC,GAAG,CAAC;QACpC,IAAI,SAAS,KAAK,SAAS,IAAI,SAAS,KAAK,MAAM,EAAE;AACnD,YAAA,MAAM,IAAI,oBAAoB,CAAC,oDAAoD,GAAG,CAAA,yBAAA,CAA2B,CAAC;QACpH;AACA,QAAA,SAAS,CAAC,GAAG,CAAC,GAAG,EAAE,MAAM,CAAC;IAC5B;AACA,IAAA,OAAO,EAAE,OAAO,EAAE,CAAC,OAAO,KAAK,SAAS,CAAC,GAAG,CAAC,gBAAgB,CAAC,OAAO,CAAC,CAAC,EAAE;AAC3E;;"}
package/package.json CHANGED
@@ -1,6 +1,44 @@
1
1
  {
2
2
  "name": "@midnightntwrk/midnight-js-bundled-contract-module-provider",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
3
+ "version": "5.0.1",
4
+ "type": "module",
5
+ "description": "MidnightJS module resolving cross-contract callees to the compiled contract modules bundled with an application",
6
+ "types": "dist/index.d.ts",
7
+ "exports": {
8
+ ".": {
9
+ "types": "./dist/index.d.ts",
10
+ "default": "./dist/index.js"
11
+ }
12
+ },
13
+ "repository": {
14
+ "type": "git",
15
+ "url": "git+https://github.com/midnightntwrk/midnight-js.git",
16
+ "directory": "packages/bundled-contract-module-provider"
17
+ },
18
+ "homepage": "https://github.com/midnightntwrk/midnight-js/tree/main/packages/bundled-contract-module-provider#readme",
19
+ "bugs": {
20
+ "url": "https://github.com/midnightntwrk/midnight-js/issues"
21
+ },
22
+ "author": "Midnight Foundation",
23
+ "packageManager": "yarn@4.14.1",
24
+ "license": "Apache-2.0",
25
+ "scripts": {
26
+ "clean": "rm -rf dist tsconfig.build.tsbuildinfo .rollup.cache reports coverage",
27
+ "build": "rollup -c rollup.config.mjs",
28
+ "test": "vitest run"
29
+ },
30
+ "dependencies": {
31
+ "@midnightntwrk/midnight-js-protocol": "5.0.1",
32
+ "@midnightntwrk/midnight-js-utils": "5.0.1"
33
+ },
34
+ "devDependencies": {
35
+ "vite": "^8.3.4",
36
+ "vitest": "^5.0.4"
37
+ },
38
+ "files": [
39
+ "dist/"
40
+ ],
41
+ "engines": {
42
+ "node": ">=22.12"
43
+ }
6
44
  }