@arcblock/did-util 1.30.3 → 1.30.4

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/esm/cbor.d.mts ADDED
@@ -0,0 +1,12 @@
1
+ import { toDelegateAddress, toStakeAddress } from "./shared.mjs";
2
+ import { TCreateAssetTx, TCreateFactoryTx, TCreateRollupTx, TCreateTokenFactoryTx, TCreateTokenTx } from "@ocap/types";
3
+
4
+ //#region src/cbor.d.ts
5
+ declare function toItxAddress(itx: Record<string, unknown>, type: string, role: number): string;
6
+ declare const toAssetAddress: (itx: Partial<TCreateAssetTx>) => string;
7
+ declare const toFactoryAddress: (itx: Partial<TCreateFactoryTx>) => string;
8
+ declare const toTokenAddress: (itx: Partial<TCreateTokenTx>) => string;
9
+ declare const toTokenFactoryAddress: (itx: Partial<TCreateTokenFactoryTx>) => string;
10
+ declare const toRollupAddress: (itx: Partial<TCreateRollupTx>) => string;
11
+ //#endregion
12
+ export { toAssetAddress, toDelegateAddress, toFactoryAddress, toItxAddress, toRollupAddress, toStakeAddress, toTokenAddress, toTokenFactoryAddress };
package/esm/cbor.mjs ADDED
@@ -0,0 +1,36 @@
1
+ import { makeCurriedAddress, toDelegateAddress, toStakeAddress } from "./shared.mjs";
2
+ import { fromHash } from "@arcblock/did";
3
+ import { Hasher, types } from "@ocap/mcrypto";
4
+ import { canonicalBytes } from "@ocap/message/cbor";
5
+ import { transactions } from "@ocap/proto/schema";
6
+
7
+ //#region src/cbor.ts
8
+ /**
9
+ * `@arcblock/did-util/cbor` — CBOR-only address derivation.
10
+ *
11
+ * Imports only `@ocap/proto/schema` and `@ocap/message/cbor`, so new Client
12
+ * SDK bundles (and any Worker / Kernel build) that stick to CBOR end-to-end
13
+ * never drag the protobuf runtime (`*_pb.js`, `google-protobuf`) in.
14
+ *
15
+ * The curried helpers match the legacy main-entry signatures (`toAssetAddress(itx)`)
16
+ * minus the `encoding` parameter — the subpath IS the encoding selector, and
17
+ * wiring in an encoding argument here would defeat the tree-shake goal by
18
+ * forcing the protobuf path into the bundle.
19
+ *
20
+ * For dual-encoding callers (chain side) keep using the main entry, whose
21
+ * `toItxAddress(itx, type, role, encoding)` dispatches per request.
22
+ */
23
+ function toItxAddress(itx, type, role) {
24
+ if (transactions.indexOf(type) === -1) throw new Error(`Unsupported itx type ${type}`);
25
+ const itxBytes = canonicalBytes(type, itx);
26
+ return fromHash(Hasher.SHA3.hash256(itxBytes), role);
27
+ }
28
+ const curriedAddress = makeCurriedAddress(toItxAddress);
29
+ const toAssetAddress = curriedAddress("CreateAssetTx", types.RoleType.ROLE_ASSET);
30
+ const toFactoryAddress = curriedAddress("CreateFactoryTx", types.RoleType.ROLE_FACTORY);
31
+ const toTokenAddress = curriedAddress("CreateTokenTx", types.RoleType.ROLE_TOKEN);
32
+ const toTokenFactoryAddress = curriedAddress("CreateTokenFactoryTx", types.RoleType.ROLE_TOKEN_FACTORY);
33
+ const toRollupAddress = curriedAddress("CreateRollupTx", types.RoleType.ROLE_ROLLUP);
34
+
35
+ //#endregion
36
+ export { toAssetAddress, toDelegateAddress, toFactoryAddress, toItxAddress, toRollupAddress, toStakeAddress, toTokenAddress, toTokenFactoryAddress };
package/esm/index.d.mts CHANGED
@@ -1,38 +1,23 @@
1
+ import { toDelegateAddress, toStakeAddress } from "./shared.mjs";
1
2
  import { TCreateAssetTx, TCreateFactoryTx, TCreateRollupTx, TCreateTokenFactoryTx, TCreateTokenTx } from "@ocap/types";
2
3
 
3
4
  //#region src/index.d.ts
4
-
5
- /**
6
- * Create an itx address
7
- */
8
- declare function toItxAddress(itx: Record<string, unknown>, type: string, role?: number): string;
9
- /**
10
- * Create an asset address
11
- */
12
- declare function toAssetAddress(itx: Partial<TCreateAssetTx>): string;
13
- /**
14
- * Create an asset factory address
15
- */
16
- declare function toFactoryAddress(itx: Partial<TCreateFactoryTx>): string;
17
- /**
18
- * Create an token address
19
- */
20
- declare function toTokenAddress(itx: Partial<TCreateTokenTx>): string;
21
- /**
22
- * Create a token factory address
23
- */
24
- declare function toTokenFactoryAddress(itx: Partial<TCreateTokenFactoryTx>): string;
25
- /**
26
- * Create an rollup address
27
- */
28
- declare function toRollupAddress(itx: Partial<TCreateRollupTx>): string;
29
- /**
30
- * Generate an stake address, eg: the did of the stake
31
- */
32
- declare function toStakeAddress(sender: string, receiver: string, nonce?: string): string;
33
- /**
34
- * Generate an delegate address, eg: the did of the delegation
35
- */
36
- declare function toDelegateAddress(delegator: string, delegatee: string): string;
5
+ type ItxEncoding = 'protobuf' | 'cbor';
6
+ /**
7
+ * Create an itx address.
8
+ *
9
+ * ⚠️ `encoding` is mandatory (no default). Phase 3 servers compute addresses
10
+ * from `context.txEncoding` — letting this default silently pick `protobuf`
11
+ * would re-introduce the dual-encoding footgun the tasks.md §4.3 warns about.
12
+ *
13
+ * The byte payload differs between `'cbor'` and `'protobuf'` even for the
14
+ * same `itx`, and therefore so does the resulting address.
15
+ */
16
+ declare function toItxAddress(itx: Record<string, unknown>, type: string, role: number, encoding: ItxEncoding): string;
17
+ declare const toAssetAddress: (itx: Partial<TCreateAssetTx>, encoding: ItxEncoding) => string;
18
+ declare const toFactoryAddress: (itx: Partial<TCreateFactoryTx>, encoding: ItxEncoding) => string;
19
+ declare const toTokenAddress: (itx: Partial<TCreateTokenTx>, encoding: ItxEncoding) => string;
20
+ declare const toTokenFactoryAddress: (itx: Partial<TCreateTokenFactoryTx>, encoding: ItxEncoding) => string;
21
+ declare const toRollupAddress: (itx: Partial<TCreateRollupTx>, encoding: ItxEncoding) => string;
37
22
  //#endregion
38
- export { toAssetAddress, toDelegateAddress, toFactoryAddress, toItxAddress, toRollupAddress, toStakeAddress, toTokenAddress, toTokenFactoryAddress };
23
+ export { ItxEncoding, toAssetAddress, toDelegateAddress, toFactoryAddress, toItxAddress, toRollupAddress, toStakeAddress, toTokenAddress, toTokenFactoryAddress };
package/esm/index.mjs CHANGED
@@ -1,66 +1,40 @@
1
- import { fromHash } from "@arcblock/did";
2
- import { Hasher, types } from "@ocap/mcrypto";
3
- import { createMessage } from "@ocap/message";
4
- import ocapProto from "@ocap/proto";
1
+ import { toDelegateAddress, toStakeAddress } from "./shared.mjs";
2
+ import { toItxAddress as toItxAddress$1 } from "./cbor.mjs";
3
+ import { toItxAddress as toItxAddress$2 } from "./protobuf.mjs";
4
+ import { types } from "@ocap/mcrypto";
5
5
 
6
6
  //#region src/index.ts
7
- const { transactions } = ocapProto;
8
7
  /**
9
- * Create an itx address
10
- */
11
- function toItxAddress(itx, type, role = types.RoleType.ROLE_TX) {
12
- if (transactions.indexOf(type) === -1) throw new Error(`Unsupported itx type ${type}`);
13
- const itxBytes = createMessage(type, itx).serializeBinary();
14
- return fromHash(Hasher.SHA3.hash256(itxBytes), role);
15
- }
16
- /**
17
- * Create an asset address
18
- */
19
- function toAssetAddress(itx) {
20
- return toItxAddress(itx, "CreateAssetTx", types.RoleType.ROLE_ASSET);
21
- }
22
- /**
23
- * Create an asset factory address
24
- */
25
- function toFactoryAddress(itx) {
26
- return toItxAddress(itx, "CreateFactoryTx", types.RoleType.ROLE_FACTORY);
27
- }
28
- /**
29
- * Create an token address
30
- */
31
- function toTokenAddress(itx) {
32
- return toItxAddress(itx, "CreateTokenTx", types.RoleType.ROLE_TOKEN);
33
- }
34
- /**
35
- * Create a token factory address
36
- */
37
- function toTokenFactoryAddress(itx) {
38
- return toItxAddress(itx, "CreateTokenFactoryTx", types.RoleType.ROLE_TOKEN_FACTORY);
39
- }
40
- /**
41
- * Create an rollup address
42
- */
43
- function toRollupAddress(itx) {
44
- return toItxAddress(itx, "CreateRollupTx", types.RoleType.ROLE_ROLLUP);
45
- }
46
- /**
47
- * Generate an stake address, eg: the did of the stake
48
- */
49
- function toStakeAddress(sender, receiver, nonce) {
50
- const buffer = Buffer.concat([
51
- sender,
52
- receiver,
53
- nonce
54
- ].filter((x) => Boolean(x)).map((x) => Uint8Array.from(Buffer.from(x))));
55
- return fromHash(Hasher.SHA3.hash256(buffer), types.RoleType.ROLE_STAKE);
56
- }
57
- /**
58
- * Generate an delegate address, eg: the did of the delegation
59
- */
60
- function toDelegateAddress(delegator, delegatee) {
61
- const buffer = Buffer.concat([Uint8Array.from(Buffer.from(delegator)), Uint8Array.from(Buffer.from(delegatee))]);
62
- return fromHash(Hasher.SHA3.hash256(buffer), types.RoleType.ROLE_DELEGATION);
63
- }
8
+ * `@arcblock/did-util` aggregate entry — chain-side / dual-encoding caller.
9
+ *
10
+ * The main entry pulls in BOTH the CBOR and protobuf paths so the chain side
11
+ * can pick the right one off `context.txEncoding`. Client SDK callers
12
+ * (tree-shake-sensitive) must import the `/cbor` or `/protobuf` subpath.
13
+ *
14
+ * Curried helpers (`toAssetAddress`, `toFactoryAddress`, …) keep their
15
+ * `(itx, encoding)` signature on the main entry for backwards compatibility.
16
+ */
17
+ /**
18
+ * Create an itx address.
19
+ *
20
+ * ⚠️ `encoding` is mandatory (no default). Phase 3 servers compute addresses
21
+ * from `context.txEncoding` — letting this default silently pick `protobuf`
22
+ * would re-introduce the dual-encoding footgun the tasks.md §4.3 warns about.
23
+ *
24
+ * The byte payload differs between `'cbor'` and `'protobuf'` even for the
25
+ * same `itx`, and therefore so does the resulting address.
26
+ */
27
+ function toItxAddress(itx, type, role, encoding) {
28
+ if (encoding === "cbor") return toItxAddress$1(itx, type, role);
29
+ if (encoding === "protobuf") return toItxAddress$2(itx, type, role);
30
+ throw new Error(`Unsupported itx encoding: ${encoding}`);
31
+ }
32
+ const curriedAddress = (type, role) => (itx, encoding) => toItxAddress(itx, type, role, encoding);
33
+ const toAssetAddress = curriedAddress("CreateAssetTx", types.RoleType.ROLE_ASSET);
34
+ const toFactoryAddress = curriedAddress("CreateFactoryTx", types.RoleType.ROLE_FACTORY);
35
+ const toTokenAddress = curriedAddress("CreateTokenTx", types.RoleType.ROLE_TOKEN);
36
+ const toTokenFactoryAddress = curriedAddress("CreateTokenFactoryTx", types.RoleType.ROLE_TOKEN_FACTORY);
37
+ const toRollupAddress = curriedAddress("CreateRollupTx", types.RoleType.ROLE_ROLLUP);
64
38
 
65
39
  //#endregion
66
40
  export { toAssetAddress, toDelegateAddress, toFactoryAddress, toItxAddress, toRollupAddress, toStakeAddress, toTokenAddress, toTokenFactoryAddress };
@@ -0,0 +1,12 @@
1
+ import { toDelegateAddress, toStakeAddress } from "./shared.mjs";
2
+ import { TCreateAssetTx, TCreateFactoryTx, TCreateRollupTx, TCreateTokenFactoryTx, TCreateTokenTx } from "@ocap/types";
3
+
4
+ //#region src/protobuf.d.ts
5
+ declare function toItxAddress(itx: Record<string, unknown>, type: string, role: number): string;
6
+ declare const toAssetAddress: (itx: Partial<TCreateAssetTx>) => string;
7
+ declare const toFactoryAddress: (itx: Partial<TCreateFactoryTx>) => string;
8
+ declare const toTokenAddress: (itx: Partial<TCreateTokenTx>) => string;
9
+ declare const toTokenFactoryAddress: (itx: Partial<TCreateTokenFactoryTx>) => string;
10
+ declare const toRollupAddress: (itx: Partial<TCreateRollupTx>) => string;
11
+ //#endregion
12
+ export { toAssetAddress, toDelegateAddress, toFactoryAddress, toItxAddress, toRollupAddress, toStakeAddress, toTokenAddress, toTokenFactoryAddress };
@@ -0,0 +1,28 @@
1
+ import { makeCurriedAddress, toDelegateAddress, toStakeAddress } from "./shared.mjs";
2
+ import { fromHash } from "@arcblock/did";
3
+ import { Hasher, types } from "@ocap/mcrypto";
4
+ import { createMessage } from "@ocap/message";
5
+ import { transactions } from "@ocap/proto/runtime";
6
+
7
+ //#region src/protobuf.ts
8
+ /**
9
+ * `@arcblock/did-util/protobuf` — protobuf-only address derivation.
10
+ *
11
+ * Imports `@ocap/proto/runtime` + `@ocap/message/protobuf`; use this subpath
12
+ * only from code paths that already carry the protobuf runtime. CBOR-capable
13
+ * callers should use `/cbor` instead.
14
+ */
15
+ function toItxAddress(itx, type, role) {
16
+ if (transactions.indexOf(type) === -1) throw new Error(`Unsupported itx type ${type}`);
17
+ const itxBytes = createMessage(type, itx).serializeBinary();
18
+ return fromHash(Hasher.SHA3.hash256(itxBytes), role);
19
+ }
20
+ const curriedAddress = makeCurriedAddress(toItxAddress);
21
+ const toAssetAddress = curriedAddress("CreateAssetTx", types.RoleType.ROLE_ASSET);
22
+ const toFactoryAddress = curriedAddress("CreateFactoryTx", types.RoleType.ROLE_FACTORY);
23
+ const toTokenAddress = curriedAddress("CreateTokenTx", types.RoleType.ROLE_TOKEN);
24
+ const toTokenFactoryAddress = curriedAddress("CreateTokenFactoryTx", types.RoleType.ROLE_TOKEN_FACTORY);
25
+ const toRollupAddress = curriedAddress("CreateRollupTx", types.RoleType.ROLE_ROLLUP);
26
+
27
+ //#endregion
28
+ export { toAssetAddress, toDelegateAddress, toFactoryAddress, toItxAddress, toRollupAddress, toStakeAddress, toTokenAddress, toTokenFactoryAddress };
@@ -0,0 +1,7 @@
1
+ //#region src/shared.d.ts
2
+ type ToItxAddressFn = (itx: Record<string, unknown>, type: string, role: number) => string;
3
+ declare function makeCurriedAddress(toItxAddress: ToItxAddressFn): (type: string, role: number) => (itx: Record<string, unknown>) => string;
4
+ declare function toStakeAddress(sender: string, receiver: string, nonce?: string): string;
5
+ declare function toDelegateAddress(delegator: string, delegatee: string): string;
6
+ //#endregion
7
+ export { ToItxAddressFn, makeCurriedAddress, toDelegateAddress, toStakeAddress };
package/esm/shared.mjs ADDED
@@ -0,0 +1,33 @@
1
+ import { fromHash } from "@arcblock/did";
2
+ import { Hasher, types } from "@ocap/mcrypto";
3
+
4
+ //#region src/shared.ts
5
+ /**
6
+ * Encoding-agnostic address helpers + curry factory shared by the three
7
+ * `@arcblock/did-util` entries (`./cbor`, `./protobuf`, `./index`).
8
+ *
9
+ * `toStakeAddress` / `toDelegateAddress` hash raw UTF-8 strings — no CBOR or
10
+ * protobuf involved — so all three entries exposed identical copies before.
11
+ * They live here once.
12
+ *
13
+ * `makeCurriedAddress` takes a `toItxAddress` implementation and produces a
14
+ * curry. Each entry injects its own encoding-specific `toItxAddress`.
15
+ */
16
+ function makeCurriedAddress(toItxAddress) {
17
+ return (type, role) => (itx) => toItxAddress(itx, type, role);
18
+ }
19
+ function toStakeAddress(sender, receiver, nonce) {
20
+ const buffer = Buffer.concat([
21
+ sender,
22
+ receiver,
23
+ nonce
24
+ ].filter((x) => Boolean(x)).map((x) => Uint8Array.from(Buffer.from(x))));
25
+ return fromHash(Hasher.SHA3.hash256(buffer), types.RoleType.ROLE_STAKE);
26
+ }
27
+ function toDelegateAddress(delegator, delegatee) {
28
+ const buffer = Buffer.concat([Uint8Array.from(Buffer.from(delegator)), Uint8Array.from(Buffer.from(delegatee))]);
29
+ return fromHash(Hasher.SHA3.hash256(buffer), types.RoleType.ROLE_DELEGATION);
30
+ }
31
+
32
+ //#endregion
33
+ export { makeCurriedAddress, toDelegateAddress, toStakeAddress };
package/lib/cbor.cjs ADDED
@@ -0,0 +1,43 @@
1
+ const require_shared = require('./shared.cjs');
2
+ let _arcblock_did = require("@arcblock/did");
3
+ let _ocap_mcrypto = require("@ocap/mcrypto");
4
+ let _ocap_message_cbor = require("@ocap/message/cbor");
5
+ let _ocap_proto_schema = require("@ocap/proto/schema");
6
+
7
+ //#region src/cbor.ts
8
+ /**
9
+ * `@arcblock/did-util/cbor` — CBOR-only address derivation.
10
+ *
11
+ * Imports only `@ocap/proto/schema` and `@ocap/message/cbor`, so new Client
12
+ * SDK bundles (and any Worker / Kernel build) that stick to CBOR end-to-end
13
+ * never drag the protobuf runtime (`*_pb.js`, `google-protobuf`) in.
14
+ *
15
+ * The curried helpers match the legacy main-entry signatures (`toAssetAddress(itx)`)
16
+ * minus the `encoding` parameter — the subpath IS the encoding selector, and
17
+ * wiring in an encoding argument here would defeat the tree-shake goal by
18
+ * forcing the protobuf path into the bundle.
19
+ *
20
+ * For dual-encoding callers (chain side) keep using the main entry, whose
21
+ * `toItxAddress(itx, type, role, encoding)` dispatches per request.
22
+ */
23
+ function toItxAddress(itx, type, role) {
24
+ if (_ocap_proto_schema.transactions.indexOf(type) === -1) throw new Error(`Unsupported itx type ${type}`);
25
+ const itxBytes = (0, _ocap_message_cbor.canonicalBytes)(type, itx);
26
+ return (0, _arcblock_did.fromHash)(_ocap_mcrypto.Hasher.SHA3.hash256(itxBytes), role);
27
+ }
28
+ const curriedAddress = require_shared.makeCurriedAddress(toItxAddress);
29
+ const toAssetAddress = curriedAddress("CreateAssetTx", _ocap_mcrypto.types.RoleType.ROLE_ASSET);
30
+ const toFactoryAddress = curriedAddress("CreateFactoryTx", _ocap_mcrypto.types.RoleType.ROLE_FACTORY);
31
+ const toTokenAddress = curriedAddress("CreateTokenTx", _ocap_mcrypto.types.RoleType.ROLE_TOKEN);
32
+ const toTokenFactoryAddress = curriedAddress("CreateTokenFactoryTx", _ocap_mcrypto.types.RoleType.ROLE_TOKEN_FACTORY);
33
+ const toRollupAddress = curriedAddress("CreateRollupTx", _ocap_mcrypto.types.RoleType.ROLE_ROLLUP);
34
+
35
+ //#endregion
36
+ exports.toAssetAddress = toAssetAddress;
37
+ exports.toDelegateAddress = require_shared.toDelegateAddress;
38
+ exports.toFactoryAddress = toFactoryAddress;
39
+ exports.toItxAddress = toItxAddress;
40
+ exports.toRollupAddress = toRollupAddress;
41
+ exports.toStakeAddress = require_shared.toStakeAddress;
42
+ exports.toTokenAddress = toTokenAddress;
43
+ exports.toTokenFactoryAddress = toTokenFactoryAddress;
package/lib/cbor.d.cts ADDED
@@ -0,0 +1,12 @@
1
+ import { toDelegateAddress, toStakeAddress } from "./shared.cjs";
2
+ import { TCreateAssetTx, TCreateFactoryTx, TCreateRollupTx, TCreateTokenFactoryTx, TCreateTokenTx } from "@ocap/types";
3
+
4
+ //#region src/cbor.d.ts
5
+ declare function toItxAddress(itx: Record<string, unknown>, type: string, role: number): string;
6
+ declare const toAssetAddress: (itx: Partial<TCreateAssetTx>) => string;
7
+ declare const toFactoryAddress: (itx: Partial<TCreateFactoryTx>) => string;
8
+ declare const toTokenAddress: (itx: Partial<TCreateTokenTx>) => string;
9
+ declare const toTokenFactoryAddress: (itx: Partial<TCreateTokenFactoryTx>) => string;
10
+ declare const toRollupAddress: (itx: Partial<TCreateRollupTx>) => string;
11
+ //#endregion
12
+ export { toAssetAddress, toDelegateAddress, toFactoryAddress, toItxAddress, toRollupAddress, toStakeAddress, toTokenAddress, toTokenFactoryAddress };
package/lib/index.cjs CHANGED
@@ -1,75 +1,47 @@
1
- const require_rolldown_runtime = require('./_virtual/rolldown_runtime.cjs');
2
- let _arcblock_did = require("@arcblock/did");
1
+ const require_shared = require('./shared.cjs');
2
+ const require_cbor = require('./cbor.cjs');
3
+ const require_protobuf = require('./protobuf.cjs');
3
4
  let _ocap_mcrypto = require("@ocap/mcrypto");
4
- let _ocap_message = require("@ocap/message");
5
- let _ocap_proto = require("@ocap/proto");
6
- _ocap_proto = require_rolldown_runtime.__toESM(_ocap_proto);
7
5
 
8
6
  //#region src/index.ts
9
- const { transactions } = _ocap_proto.default;
10
7
  /**
11
- * Create an itx address
12
- */
13
- function toItxAddress(itx, type, role = _ocap_mcrypto.types.RoleType.ROLE_TX) {
14
- if (transactions.indexOf(type) === -1) throw new Error(`Unsupported itx type ${type}`);
15
- const itxBytes = (0, _ocap_message.createMessage)(type, itx).serializeBinary();
16
- return (0, _arcblock_did.fromHash)(_ocap_mcrypto.Hasher.SHA3.hash256(itxBytes), role);
17
- }
18
- /**
19
- * Create an asset address
20
- */
21
- function toAssetAddress(itx) {
22
- return toItxAddress(itx, "CreateAssetTx", _ocap_mcrypto.types.RoleType.ROLE_ASSET);
23
- }
24
- /**
25
- * Create an asset factory address
26
- */
27
- function toFactoryAddress(itx) {
28
- return toItxAddress(itx, "CreateFactoryTx", _ocap_mcrypto.types.RoleType.ROLE_FACTORY);
29
- }
30
- /**
31
- * Create an token address
32
- */
33
- function toTokenAddress(itx) {
34
- return toItxAddress(itx, "CreateTokenTx", _ocap_mcrypto.types.RoleType.ROLE_TOKEN);
35
- }
36
- /**
37
- * Create a token factory address
38
- */
39
- function toTokenFactoryAddress(itx) {
40
- return toItxAddress(itx, "CreateTokenFactoryTx", _ocap_mcrypto.types.RoleType.ROLE_TOKEN_FACTORY);
41
- }
42
- /**
43
- * Create an rollup address
44
- */
45
- function toRollupAddress(itx) {
46
- return toItxAddress(itx, "CreateRollupTx", _ocap_mcrypto.types.RoleType.ROLE_ROLLUP);
47
- }
48
- /**
49
- * Generate an stake address, eg: the did of the stake
50
- */
51
- function toStakeAddress(sender, receiver, nonce) {
52
- const buffer = Buffer.concat([
53
- sender,
54
- receiver,
55
- nonce
56
- ].filter((x) => Boolean(x)).map((x) => Uint8Array.from(Buffer.from(x))));
57
- return (0, _arcblock_did.fromHash)(_ocap_mcrypto.Hasher.SHA3.hash256(buffer), _ocap_mcrypto.types.RoleType.ROLE_STAKE);
58
- }
59
- /**
60
- * Generate an delegate address, eg: the did of the delegation
61
- */
62
- function toDelegateAddress(delegator, delegatee) {
63
- const buffer = Buffer.concat([Uint8Array.from(Buffer.from(delegator)), Uint8Array.from(Buffer.from(delegatee))]);
64
- return (0, _arcblock_did.fromHash)(_ocap_mcrypto.Hasher.SHA3.hash256(buffer), _ocap_mcrypto.types.RoleType.ROLE_DELEGATION);
65
- }
8
+ * `@arcblock/did-util` aggregate entry — chain-side / dual-encoding caller.
9
+ *
10
+ * The main entry pulls in BOTH the CBOR and protobuf paths so the chain side
11
+ * can pick the right one off `context.txEncoding`. Client SDK callers
12
+ * (tree-shake-sensitive) must import the `/cbor` or `/protobuf` subpath.
13
+ *
14
+ * Curried helpers (`toAssetAddress`, `toFactoryAddress`, …) keep their
15
+ * `(itx, encoding)` signature on the main entry for backwards compatibility.
16
+ */
17
+ /**
18
+ * Create an itx address.
19
+ *
20
+ * ⚠️ `encoding` is mandatory (no default). Phase 3 servers compute addresses
21
+ * from `context.txEncoding` — letting this default silently pick `protobuf`
22
+ * would re-introduce the dual-encoding footgun the tasks.md §4.3 warns about.
23
+ *
24
+ * The byte payload differs between `'cbor'` and `'protobuf'` even for the
25
+ * same `itx`, and therefore so does the resulting address.
26
+ */
27
+ function toItxAddress(itx, type, role, encoding) {
28
+ if (encoding === "cbor") return require_cbor.toItxAddress(itx, type, role);
29
+ if (encoding === "protobuf") return require_protobuf.toItxAddress(itx, type, role);
30
+ throw new Error(`Unsupported itx encoding: ${encoding}`);
31
+ }
32
+ const curriedAddress = (type, role) => (itx, encoding) => toItxAddress(itx, type, role, encoding);
33
+ const toAssetAddress = curriedAddress("CreateAssetTx", _ocap_mcrypto.types.RoleType.ROLE_ASSET);
34
+ const toFactoryAddress = curriedAddress("CreateFactoryTx", _ocap_mcrypto.types.RoleType.ROLE_FACTORY);
35
+ const toTokenAddress = curriedAddress("CreateTokenTx", _ocap_mcrypto.types.RoleType.ROLE_TOKEN);
36
+ const toTokenFactoryAddress = curriedAddress("CreateTokenFactoryTx", _ocap_mcrypto.types.RoleType.ROLE_TOKEN_FACTORY);
37
+ const toRollupAddress = curriedAddress("CreateRollupTx", _ocap_mcrypto.types.RoleType.ROLE_ROLLUP);
66
38
 
67
39
  //#endregion
68
40
  exports.toAssetAddress = toAssetAddress;
69
- exports.toDelegateAddress = toDelegateAddress;
41
+ exports.toDelegateAddress = require_shared.toDelegateAddress;
70
42
  exports.toFactoryAddress = toFactoryAddress;
71
43
  exports.toItxAddress = toItxAddress;
72
44
  exports.toRollupAddress = toRollupAddress;
73
- exports.toStakeAddress = toStakeAddress;
45
+ exports.toStakeAddress = require_shared.toStakeAddress;
74
46
  exports.toTokenAddress = toTokenAddress;
75
47
  exports.toTokenFactoryAddress = toTokenFactoryAddress;
package/lib/index.d.cts CHANGED
@@ -1,38 +1,23 @@
1
+ import { toDelegateAddress, toStakeAddress } from "./shared.cjs";
1
2
  import { TCreateAssetTx, TCreateFactoryTx, TCreateRollupTx, TCreateTokenFactoryTx, TCreateTokenTx } from "@ocap/types";
2
3
 
3
4
  //#region src/index.d.ts
4
-
5
- /**
6
- * Create an itx address
7
- */
8
- declare function toItxAddress(itx: Record<string, unknown>, type: string, role?: number): string;
9
- /**
10
- * Create an asset address
11
- */
12
- declare function toAssetAddress(itx: Partial<TCreateAssetTx>): string;
13
- /**
14
- * Create an asset factory address
15
- */
16
- declare function toFactoryAddress(itx: Partial<TCreateFactoryTx>): string;
17
- /**
18
- * Create an token address
19
- */
20
- declare function toTokenAddress(itx: Partial<TCreateTokenTx>): string;
21
- /**
22
- * Create a token factory address
23
- */
24
- declare function toTokenFactoryAddress(itx: Partial<TCreateTokenFactoryTx>): string;
25
- /**
26
- * Create an rollup address
27
- */
28
- declare function toRollupAddress(itx: Partial<TCreateRollupTx>): string;
29
- /**
30
- * Generate an stake address, eg: the did of the stake
31
- */
32
- declare function toStakeAddress(sender: string, receiver: string, nonce?: string): string;
33
- /**
34
- * Generate an delegate address, eg: the did of the delegation
35
- */
36
- declare function toDelegateAddress(delegator: string, delegatee: string): string;
5
+ type ItxEncoding = 'protobuf' | 'cbor';
6
+ /**
7
+ * Create an itx address.
8
+ *
9
+ * ⚠️ `encoding` is mandatory (no default). Phase 3 servers compute addresses
10
+ * from `context.txEncoding` — letting this default silently pick `protobuf`
11
+ * would re-introduce the dual-encoding footgun the tasks.md §4.3 warns about.
12
+ *
13
+ * The byte payload differs between `'cbor'` and `'protobuf'` even for the
14
+ * same `itx`, and therefore so does the resulting address.
15
+ */
16
+ declare function toItxAddress(itx: Record<string, unknown>, type: string, role: number, encoding: ItxEncoding): string;
17
+ declare const toAssetAddress: (itx: Partial<TCreateAssetTx>, encoding: ItxEncoding) => string;
18
+ declare const toFactoryAddress: (itx: Partial<TCreateFactoryTx>, encoding: ItxEncoding) => string;
19
+ declare const toTokenAddress: (itx: Partial<TCreateTokenTx>, encoding: ItxEncoding) => string;
20
+ declare const toTokenFactoryAddress: (itx: Partial<TCreateTokenFactoryTx>, encoding: ItxEncoding) => string;
21
+ declare const toRollupAddress: (itx: Partial<TCreateRollupTx>, encoding: ItxEncoding) => string;
37
22
  //#endregion
38
- export { toAssetAddress, toDelegateAddress, toFactoryAddress, toItxAddress, toRollupAddress, toStakeAddress, toTokenAddress, toTokenFactoryAddress };
23
+ export { ItxEncoding, toAssetAddress, toDelegateAddress, toFactoryAddress, toItxAddress, toRollupAddress, toStakeAddress, toTokenAddress, toTokenFactoryAddress };
@@ -0,0 +1,35 @@
1
+ const require_shared = require('./shared.cjs');
2
+ let _arcblock_did = require("@arcblock/did");
3
+ let _ocap_mcrypto = require("@ocap/mcrypto");
4
+ let _ocap_message = require("@ocap/message");
5
+ let _ocap_proto_runtime = require("@ocap/proto/runtime");
6
+
7
+ //#region src/protobuf.ts
8
+ /**
9
+ * `@arcblock/did-util/protobuf` — protobuf-only address derivation.
10
+ *
11
+ * Imports `@ocap/proto/runtime` + `@ocap/message/protobuf`; use this subpath
12
+ * only from code paths that already carry the protobuf runtime. CBOR-capable
13
+ * callers should use `/cbor` instead.
14
+ */
15
+ function toItxAddress(itx, type, role) {
16
+ if (_ocap_proto_runtime.transactions.indexOf(type) === -1) throw new Error(`Unsupported itx type ${type}`);
17
+ const itxBytes = (0, _ocap_message.createMessage)(type, itx).serializeBinary();
18
+ return (0, _arcblock_did.fromHash)(_ocap_mcrypto.Hasher.SHA3.hash256(itxBytes), role);
19
+ }
20
+ const curriedAddress = require_shared.makeCurriedAddress(toItxAddress);
21
+ const toAssetAddress = curriedAddress("CreateAssetTx", _ocap_mcrypto.types.RoleType.ROLE_ASSET);
22
+ const toFactoryAddress = curriedAddress("CreateFactoryTx", _ocap_mcrypto.types.RoleType.ROLE_FACTORY);
23
+ const toTokenAddress = curriedAddress("CreateTokenTx", _ocap_mcrypto.types.RoleType.ROLE_TOKEN);
24
+ const toTokenFactoryAddress = curriedAddress("CreateTokenFactoryTx", _ocap_mcrypto.types.RoleType.ROLE_TOKEN_FACTORY);
25
+ const toRollupAddress = curriedAddress("CreateRollupTx", _ocap_mcrypto.types.RoleType.ROLE_ROLLUP);
26
+
27
+ //#endregion
28
+ exports.toAssetAddress = toAssetAddress;
29
+ exports.toDelegateAddress = require_shared.toDelegateAddress;
30
+ exports.toFactoryAddress = toFactoryAddress;
31
+ exports.toItxAddress = toItxAddress;
32
+ exports.toRollupAddress = toRollupAddress;
33
+ exports.toStakeAddress = require_shared.toStakeAddress;
34
+ exports.toTokenAddress = toTokenAddress;
35
+ exports.toTokenFactoryAddress = toTokenFactoryAddress;
@@ -0,0 +1,12 @@
1
+ import { toDelegateAddress, toStakeAddress } from "./shared.cjs";
2
+ import { TCreateAssetTx, TCreateFactoryTx, TCreateRollupTx, TCreateTokenFactoryTx, TCreateTokenTx } from "@ocap/types";
3
+
4
+ //#region src/protobuf.d.ts
5
+ declare function toItxAddress(itx: Record<string, unknown>, type: string, role: number): string;
6
+ declare const toAssetAddress: (itx: Partial<TCreateAssetTx>) => string;
7
+ declare const toFactoryAddress: (itx: Partial<TCreateFactoryTx>) => string;
8
+ declare const toTokenAddress: (itx: Partial<TCreateTokenTx>) => string;
9
+ declare const toTokenFactoryAddress: (itx: Partial<TCreateTokenFactoryTx>) => string;
10
+ declare const toRollupAddress: (itx: Partial<TCreateRollupTx>) => string;
11
+ //#endregion
12
+ export { toAssetAddress, toDelegateAddress, toFactoryAddress, toItxAddress, toRollupAddress, toStakeAddress, toTokenAddress, toTokenFactoryAddress };
package/lib/shared.cjs ADDED
@@ -0,0 +1,35 @@
1
+ let _arcblock_did = require("@arcblock/did");
2
+ let _ocap_mcrypto = require("@ocap/mcrypto");
3
+
4
+ //#region src/shared.ts
5
+ /**
6
+ * Encoding-agnostic address helpers + curry factory shared by the three
7
+ * `@arcblock/did-util` entries (`./cbor`, `./protobuf`, `./index`).
8
+ *
9
+ * `toStakeAddress` / `toDelegateAddress` hash raw UTF-8 strings — no CBOR or
10
+ * protobuf involved — so all three entries exposed identical copies before.
11
+ * They live here once.
12
+ *
13
+ * `makeCurriedAddress` takes a `toItxAddress` implementation and produces a
14
+ * curry. Each entry injects its own encoding-specific `toItxAddress`.
15
+ */
16
+ function makeCurriedAddress(toItxAddress) {
17
+ return (type, role) => (itx) => toItxAddress(itx, type, role);
18
+ }
19
+ function toStakeAddress(sender, receiver, nonce) {
20
+ const buffer = Buffer.concat([
21
+ sender,
22
+ receiver,
23
+ nonce
24
+ ].filter((x) => Boolean(x)).map((x) => Uint8Array.from(Buffer.from(x))));
25
+ return (0, _arcblock_did.fromHash)(_ocap_mcrypto.Hasher.SHA3.hash256(buffer), _ocap_mcrypto.types.RoleType.ROLE_STAKE);
26
+ }
27
+ function toDelegateAddress(delegator, delegatee) {
28
+ const buffer = Buffer.concat([Uint8Array.from(Buffer.from(delegator)), Uint8Array.from(Buffer.from(delegatee))]);
29
+ return (0, _arcblock_did.fromHash)(_ocap_mcrypto.Hasher.SHA3.hash256(buffer), _ocap_mcrypto.types.RoleType.ROLE_DELEGATION);
30
+ }
31
+
32
+ //#endregion
33
+ exports.makeCurriedAddress = makeCurriedAddress;
34
+ exports.toDelegateAddress = toDelegateAddress;
35
+ exports.toStakeAddress = toStakeAddress;
@@ -0,0 +1,7 @@
1
+ //#region src/shared.d.ts
2
+ type ToItxAddressFn = (itx: Record<string, unknown>, type: string, role: number) => string;
3
+ declare function makeCurriedAddress(toItxAddress: ToItxAddressFn): (type: string, role: number) => (itx: Record<string, unknown>) => string;
4
+ declare function toStakeAddress(sender: string, receiver: string, nonce?: string): string;
5
+ declare function toDelegateAddress(delegator: string, delegatee: string): string;
6
+ //#endregion
7
+ export { ToItxAddressFn, makeCurriedAddress, toDelegateAddress, toStakeAddress };
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@arcblock/did-util",
3
3
  "description": "Helper function to calculate did",
4
- "version": "1.30.3",
4
+ "version": "1.30.4",
5
5
  "type": "module",
6
6
  "author": {
7
7
  "name": "wangshijun",
@@ -19,19 +19,29 @@
19
19
  "wangshijun <shijun@arcblock.io> (https://github.com/wangshijun)"
20
20
  ],
21
21
  "dependencies": {
22
- "@arcblock/did": "1.30.3",
23
- "@ocap/mcrypto": "1.30.3",
24
- "@ocap/message": "1.30.3",
25
- "@ocap/proto": "1.30.3",
26
- "@ocap/types": "1.30.3",
27
- "@ocap/util": "1.30.3",
28
- "@ocap/wallet": "1.30.3"
22
+ "@arcblock/did": "1.30.4",
23
+ "@ocap/mcrypto": "1.30.4",
24
+ "@ocap/message": "1.30.4",
25
+ "@ocap/proto": "1.30.4",
26
+ "@ocap/types": "1.30.4",
27
+ "@ocap/util": "1.30.4",
28
+ "@ocap/wallet": "1.30.4"
29
29
  },
30
30
  "sideEffects": false,
31
31
  "main": "./lib/index.cjs",
32
32
  "module": "./esm/index.mjs",
33
33
  "types": "./esm/index.d.mts",
34
34
  "exports": {
35
+ "./cbor": {
36
+ "types": "./esm/cbor.d.mts",
37
+ "import": "./esm/cbor.mjs",
38
+ "default": "./lib/cbor.cjs"
39
+ },
40
+ "./protobuf": {
41
+ "types": "./esm/protobuf.d.mts",
42
+ "import": "./esm/protobuf.mjs",
43
+ "default": "./lib/protobuf.cjs"
44
+ },
35
45
  ".": {
36
46
  "types": "./esm/index.d.mts",
37
47
  "import": "./esm/index.mjs",
@@ -1,29 +0,0 @@
1
- //#region rolldown:runtime
2
- var __create = Object.create;
3
- var __defProp = Object.defineProperty;
4
- var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
- var __getOwnPropNames = Object.getOwnPropertyNames;
6
- var __getProtoOf = Object.getPrototypeOf;
7
- var __hasOwnProp = Object.prototype.hasOwnProperty;
8
- var __copyProps = (to, from, except, desc) => {
9
- if (from && typeof from === "object" || typeof from === "function") {
10
- for (var keys = __getOwnPropNames(from), i = 0, n = keys.length, key; i < n; i++) {
11
- key = keys[i];
12
- if (!__hasOwnProp.call(to, key) && key !== except) {
13
- __defProp(to, key, {
14
- get: ((k) => from[k]).bind(null, key),
15
- enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
16
- });
17
- }
18
- }
19
- }
20
- return to;
21
- };
22
- var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", {
23
- value: mod,
24
- enumerable: true
25
- }) : target, mod));
26
-
27
- //#endregion
28
-
29
- exports.__toESM = __toESM;