@arcblock/did 1.30.25 → 1.31.0

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/index.d.mts CHANGED
@@ -1,18 +1,19 @@
1
+ import { ALIAS_METHODS, ALL_METHODS, AliasMethod, CRYPTO_METHODS, CryptoMethod, DEFAULT_METHOD, DIDMethod, isAliasMethod, isCryptoMethod, isKnownMethod } from "./method.mjs";
2
+ import { InMemoryNameRegistry, NameRegistry } from "./name-registry.mjs";
3
+ import { AccountPreRotationStateData, DEFAULT_DID_DOMAIN_GATEWAY, DIDDomainEdgeRouter, DIDServiceEndpoint, DID_ENDPOINT_DOMAIN_TAG, DNS_NFT_BINDING_DOMAIN_TAG, DidEndpointRecord, DnsDidLabelEncoding, DomainNftAssetSnapshot, DomainNftBindingProof, KEY_ROTATION_DOMAIN_TAG, KeyRotationEvent, MAX_CUSTOM_DOMAINS_PER_DID, MAX_ROTATION_CHAIN_LENGTH, PRE_ROTATION_DOMAIN_TAG, SynthesizeDidDocumentOptions, ThreeTierResolutionResult, W3CDIDDocument, W3CDIDVerificationMethod, createAccountMigratePreRotationData, createDomainNftBindingProof, createKeyRotationEvent, createPreRotationCommitment, encodeDidEndpointDigest, extractAccountPreRotationData, extractDidFromDomainHost, fromDnsDidLabel, isValidPreRotationCommitment, normalizePreRotationCommitment, normalizePublicKeyHex, synthesizeDidDocument, toDidDomainHost, toDidWellKnownUrl, toDnsDidLabel, verifyAccountMigrationChainWithKel, verifyDomainNftBindingProof, verifyKeyRotationChain, verifyPreRotationCommitment, verifySingleKeyRotationEvent } from "./did-domain-resolver.mjs";
1
4
  import { getEntityId, isSameEntity } from "./entity.mjs";
2
- import { DIDType, DIDTypeArg, DIDTypeShortcut, DIDTypeStr, DID_TYPE_ARCBLOCK, DID_TYPE_ETHEREUM, DID_TYPE_PASSKEY, DidType, RequiredDIDType, fromTypeInfo, isEthereumDid, isEthereumType, toTypeInfo, toTypeInfoStr } from "./type.mjs";
5
+ import { DIDType, DIDTypeArg, DIDTypeShortcut, DIDTypeStr, DID_TYPE_ARCBLOCK, DID_TYPE_ETHEREUM, DID_TYPE_PASSKEY, DidType, RequiredDIDType, fromTypeInfo, isEthereumDid, isEthereumType, toChecksumAddress, toTypeInfo, toTypeInfoStr } from "./type.mjs";
3
6
  import { DID_PREFIX, toStrictHex } from "./util.mjs";
4
- import { ALIAS_METHODS, ALL_METHODS, AliasMethod, CRYPTO_METHODS, CryptoMethod, DEFAULT_METHOD, DIDMethod, isAliasMethod, isCryptoMethod, isKnownMethod } from "./method.mjs";
5
7
  import { ParsedDID, extractIdentifier, extractMethod, formatDid, parse } from "./parse.mjs";
6
8
  import { isKnownDid } from "./validate.mjs";
7
9
  import { expandShortForm, toShortForm } from "./short-form.mjs";
8
10
  import { NameHierarchy, ResolveRoute, getResolveRoute, isGlobalName, isLocalName, normalizeName, parseNameHierarchy } from "./name.mjs";
9
- import { InMemoryNameRegistry, NameRegistry } from "./name-registry.mjs";
10
11
  import { DIDNameResolver, DIDNameResolverOptions, ResolveTraceResult } from "./name-resolver.mjs";
12
+ import { SUBACCOUNT_DOMAIN_TAG, deriveSubAccountSeed, encodeLengthPrefixedTuple } from "./subaccount.mjs";
11
13
  import { types } from "@ocap/mcrypto";
12
14
  import { BytesType, toAddress, toDid } from "@ocap/util";
13
15
 
14
16
  //#region src/index.d.ts
15
-
16
17
  /**
17
18
  * Gen DID from private key and type config
18
19
  *
@@ -65,5 +66,6 @@ declare const isFromPublicKey: (did: string, pk: BytesType) => boolean;
65
66
  * @returns {boolean}
66
67
  */
67
68
  declare const isValid: (did: string) => boolean;
69
+ declare const deriveSubAccountAddress: (parentIdentity: BytesType, scope: string, index?: number, type?: DIDTypeArg) => string, isFromSubAccount: (subAccountDid: string, parentIdentity: BytesType, scope: string, index?: number) => boolean;
68
70
  //#endregion
69
- export { ALIAS_METHODS, ALL_METHODS, type AliasMethod, CRYPTO_METHODS, type CryptoMethod, DEFAULT_METHOD, type DIDMethod, DIDNameResolver, type DIDNameResolverOptions, type DIDType, type DIDTypeArg, type DIDTypeShortcut, type DIDTypeStr, DID_PREFIX, DID_TYPE_ARCBLOCK, DID_TYPE_ETHEREUM, DID_TYPE_PASSKEY, DidType, InMemoryNameRegistry, type NameHierarchy, type NameRegistry, type ParsedDID, type RequiredDIDType, type ResolveRoute, type ResolveTraceResult, expandShortForm, extractIdentifier, extractMethod, formatDid, fromHash, fromPublicKey, fromPublicKeyHash, fromSecretKey, fromTypeInfo, getEntityId, getResolveRoute, isAliasMethod, isCryptoMethod, isEthereumDid, isEthereumType, isFromPublicKey, isGlobalName, isKnownDid, isKnownMethod, isLocalName, isSameEntity, isValid, normalizeName, parse, parseNameHierarchy, toAddress, toDid, toShortForm, toStrictHex, toTypeInfo, toTypeInfoStr, types };
71
+ export { ALIAS_METHODS, ALL_METHODS, type AccountPreRotationStateData, type AliasMethod, CRYPTO_METHODS, type CryptoMethod, DEFAULT_DID_DOMAIN_GATEWAY, DEFAULT_METHOD, DIDDomainEdgeRouter, type DIDMethod, DIDNameResolver, type DIDNameResolverOptions, type DIDServiceEndpoint, type DIDType, type DIDTypeArg, type DIDTypeShortcut, type DIDTypeStr, DID_ENDPOINT_DOMAIN_TAG, DID_PREFIX, DID_TYPE_ARCBLOCK, DID_TYPE_ETHEREUM, DID_TYPE_PASSKEY, DNS_NFT_BINDING_DOMAIN_TAG, type DidEndpointRecord, DidType, type DnsDidLabelEncoding, type DomainNftAssetSnapshot, type DomainNftBindingProof, InMemoryNameRegistry, KEY_ROTATION_DOMAIN_TAG, type KeyRotationEvent, MAX_CUSTOM_DOMAINS_PER_DID, MAX_ROTATION_CHAIN_LENGTH, type NameHierarchy, type NameRegistry, PRE_ROTATION_DOMAIN_TAG, type ParsedDID, type RequiredDIDType, type ResolveRoute, type ResolveTraceResult, SUBACCOUNT_DOMAIN_TAG, type SynthesizeDidDocumentOptions, type ThreeTierResolutionResult, type W3CDIDDocument, type W3CDIDVerificationMethod, createAccountMigratePreRotationData, createDomainNftBindingProof, createKeyRotationEvent, createPreRotationCommitment, deriveSubAccountAddress, deriveSubAccountSeed, encodeDidEndpointDigest, encodeLengthPrefixedTuple, expandShortForm, extractAccountPreRotationData, extractDidFromDomainHost, extractIdentifier, extractMethod, formatDid, fromDnsDidLabel, fromHash, fromPublicKey, fromPublicKeyHash, fromSecretKey, fromTypeInfo, getEntityId, getResolveRoute, isAliasMethod, isCryptoMethod, isEthereumDid, isEthereumType, isFromPublicKey, isFromSubAccount, isGlobalName, isKnownDid, isKnownMethod, isLocalName, isSameEntity, isValid, isValidPreRotationCommitment, normalizeName, normalizePreRotationCommitment, normalizePublicKeyHex, parse, parseNameHierarchy, synthesizeDidDocument, toAddress, toChecksumAddress, toDid, toDidDomainHost, toDidWellKnownUrl, toDnsDidLabel, toShortForm, toStrictHex, toTypeInfo, toTypeInfoStr, types, verifyAccountMigrationChainWithKel, verifyDomainNftBindingProof, verifyKeyRotationChain, verifyPreRotationCommitment, verifySingleKeyRotationEvent };
package/esm/index.mjs CHANGED
@@ -8,6 +8,8 @@ import { isKnownDid } from "./validate.mjs";
8
8
  import { expandShortForm, toShortForm } from "./short-form.mjs";
9
9
  import { InMemoryNameRegistry } from "./name-registry.mjs";
10
10
  import { DIDNameResolver } from "./name-resolver.mjs";
11
+ import { SUBACCOUNT_DOMAIN_TAG, createSubAccountHelpers, deriveSubAccountSeed, encodeLengthPrefixedTuple } from "./subaccount.mjs";
12
+ import { DEFAULT_DID_DOMAIN_GATEWAY, DIDDomainEdgeRouter, DID_ENDPOINT_DOMAIN_TAG, DNS_NFT_BINDING_DOMAIN_TAG, KEY_ROTATION_DOMAIN_TAG, MAX_CUSTOM_DOMAINS_PER_DID, MAX_ROTATION_CHAIN_LENGTH, PRE_ROTATION_DOMAIN_TAG, createAccountMigratePreRotationData, createDomainNftBindingProof, createKeyRotationEvent, createPreRotationCommitment, encodeDidEndpointDigest, extractAccountPreRotationData, extractDidFromDomainHost, fromDnsDidLabel, isValidPreRotationCommitment, normalizePreRotationCommitment, normalizePublicKeyHex, synthesizeDidDocument, toDidDomainHost, toDidWellKnownUrl, toDnsDidLabel, verifyAccountMigrationChainWithKel, verifyDomainNftBindingProof, verifyKeyRotationChain, verifyPreRotationCommitment, verifySingleKeyRotationEvent } from "./did-domain-resolver.mjs";
11
13
  import { getHasher, getSigner, types } from "@ocap/mcrypto";
12
14
  import { stripHexPrefix, toAddress, toBase58, toDid } from "@ocap/util";
13
15
 
@@ -99,6 +101,10 @@ const isValid = (did) => {
99
101
  const bytesHex = toStrictHex(Buffer.from(Uint8Array.from(bytes.slice(0, 22))).toString("hex"));
100
102
  return toStrictHex(Buffer.from(Uint8Array.from(bytes.slice(22, 26))).toString("hex")) === stripHexPrefix(hashFn(`0x${bytesHex}`, 1)).slice(0, 8);
101
103
  };
104
+ const { deriveSubAccountAddress, isFromSubAccount } = createSubAccountHelpers({
105
+ fromPublicKeyHash,
106
+ isValid
107
+ });
102
108
 
103
109
  //#endregion
104
- export { ALIAS_METHODS, ALL_METHODS, CRYPTO_METHODS, DEFAULT_METHOD, DIDNameResolver, DID_PREFIX, DID_TYPE_ARCBLOCK, DID_TYPE_ETHEREUM, DID_TYPE_PASSKEY, DidType, InMemoryNameRegistry, expandShortForm, extractIdentifier, extractMethod, formatDid, fromHash, fromPublicKey, fromPublicKeyHash, fromSecretKey, fromTypeInfo, getEntityId, getResolveRoute, isAliasMethod, isCryptoMethod, isEthereumDid, isEthereumType, isFromPublicKey, isGlobalName, isKnownDid, isKnownMethod, isLocalName, isSameEntity, isValid, normalizeName, parse, parseNameHierarchy, toAddress, toDid, toShortForm, toStrictHex, toTypeInfo, toTypeInfoStr, types };
110
+ export { ALIAS_METHODS, ALL_METHODS, CRYPTO_METHODS, DEFAULT_DID_DOMAIN_GATEWAY, DEFAULT_METHOD, DIDDomainEdgeRouter, DIDNameResolver, DID_ENDPOINT_DOMAIN_TAG, DID_PREFIX, DID_TYPE_ARCBLOCK, DID_TYPE_ETHEREUM, DID_TYPE_PASSKEY, DNS_NFT_BINDING_DOMAIN_TAG, DidType, InMemoryNameRegistry, KEY_ROTATION_DOMAIN_TAG, MAX_CUSTOM_DOMAINS_PER_DID, MAX_ROTATION_CHAIN_LENGTH, PRE_ROTATION_DOMAIN_TAG, SUBACCOUNT_DOMAIN_TAG, createAccountMigratePreRotationData, createDomainNftBindingProof, createKeyRotationEvent, createPreRotationCommitment, deriveSubAccountAddress, deriveSubAccountSeed, encodeDidEndpointDigest, encodeLengthPrefixedTuple, expandShortForm, extractAccountPreRotationData, extractDidFromDomainHost, extractIdentifier, extractMethod, formatDid, fromDnsDidLabel, fromHash, fromPublicKey, fromPublicKeyHash, fromSecretKey, fromTypeInfo, getEntityId, getResolveRoute, isAliasMethod, isCryptoMethod, isEthereumDid, isEthereumType, isFromPublicKey, isFromSubAccount, isGlobalName, isKnownDid, isKnownMethod, isLocalName, isSameEntity, isValid, isValidPreRotationCommitment, normalizeName, normalizePreRotationCommitment, normalizePublicKeyHex, parse, parseNameHierarchy, synthesizeDidDocument, toAddress, toChecksumAddress, toDid, toDidDomainHost, toDidWellKnownUrl, toDnsDidLabel, toShortForm, toStrictHex, toTypeInfo, toTypeInfoStr, types, verifyAccountMigrationChainWithKel, verifyDomainNftBindingProof, verifyKeyRotationChain, verifyPreRotationCommitment, verifySingleKeyRotationEvent };
@@ -0,0 +1,38 @@
1
+ import { DIDTypeArg } from "./type.mjs";
2
+ import { BytesType } from "@ocap/util";
3
+
4
+ //#region src/subaccount.d.ts
5
+ declare const SUBACCOUNT_DOMAIN_TAG = "arcblock:subaccount:v1";
6
+ /**
7
+ * Encode variable-length fields plus a 32-bit unsigned index into a canonical
8
+ * length-prefixed byte tuple:
9
+ * u32be(|f_0|) || f_0 || u32be(|f_1|) || f_1 || ... || u32be(index)
10
+ *
11
+ * Prevents delimiter collision attacks (e.g. scope="a:b", id="c" vs scope="a", id="b:c").
12
+ */
13
+ declare function encodeLengthPrefixedTuple(fields: ReadonlyArray<Uint8Array | string>, index?: number): Uint8Array;
14
+ /**
15
+ * Derive a deterministic 32-byte sub-account seed (`0x` + 64 hex chars) from a parent
16
+ * public key / root DID, scope (such as chain DID or persona namespace), and index:
17
+ *
18
+ * HMAC-SHA256(
19
+ * key = "arcblock:subaccount:v1",
20
+ * msg = u32be(|scope|) || scope || u32be(|parent|) || parent || u32be(index)
21
+ * )
22
+ *
23
+ * Security note (#1167):
24
+ * This is Layer 1 (deterministic sub-account address partitioning & cross-chain wrong-deposit
25
+ * protection). When used directly with a shared hardware Passkey public key on-chain, the
26
+ * public key remains visible in transaction headers; for on-chain explorer unlinkability,
27
+ * use Layer 2 (`fromChainDid` stealth key derivation or delegated per-chain session keys).
28
+ */
29
+ declare function deriveSubAccountSeed(parentIdentity: BytesType, scope: string, index?: number): string;
30
+ declare function createSubAccountHelpers(deps: {
31
+ fromPublicKeyHash: (buffer: string, type: DIDTypeArg) => string;
32
+ isValid: (did: string) => boolean;
33
+ }): {
34
+ deriveSubAccountAddress: (parentIdentity: BytesType, scope: string, index?: number, type?: DIDTypeArg) => string;
35
+ isFromSubAccount: (subAccountDid: string, parentIdentity: BytesType, scope: string, index?: number) => boolean;
36
+ };
37
+ //#endregion
38
+ export { SUBACCOUNT_DOMAIN_TAG, createSubAccountHelpers, deriveSubAccountSeed, encodeLengthPrefixedTuple };
@@ -0,0 +1,95 @@
1
+ import { toTypeInfo } from "./type.mjs";
2
+ import { hmacSha256 } from "@ocap/mcrypto";
3
+ import { isHexStrict, toAddress, toUint8Array } from "@ocap/util";
4
+
5
+ //#region src/subaccount.ts
6
+ const SUBACCOUNT_DOMAIN_TAG = "arcblock:subaccount:v1";
7
+ const textEncoder = new TextEncoder();
8
+ const DOMAIN_KEY_BYTES = textEncoder.encode(SUBACCOUNT_DOMAIN_TAG);
9
+ function writeU32BE(buf, offset, value) {
10
+ const u = value >>> 0;
11
+ buf[offset] = u >>> 24 & 255;
12
+ buf[offset + 1] = u >>> 16 & 255;
13
+ buf[offset + 2] = u >>> 8 & 255;
14
+ buf[offset + 3] = u & 255;
15
+ }
16
+ /**
17
+ * Encode variable-length fields plus a 32-bit unsigned index into a canonical
18
+ * length-prefixed byte tuple:
19
+ * u32be(|f_0|) || f_0 || u32be(|f_1|) || f_1 || ... || u32be(index)
20
+ *
21
+ * Prevents delimiter collision attacks (e.g. scope="a:b", id="c" vs scope="a", id="b:c").
22
+ */
23
+ function encodeLengthPrefixedTuple(fields, index = 0) {
24
+ if (!Number.isInteger(index) || index < 0 || index > 4294967295) throw new Error("Invalid sub-account index: must be a non-negative 32-bit integer");
25
+ const encodedFields = fields.map((field) => {
26
+ if (field instanceof Uint8Array) return field;
27
+ if (typeof field === "string") return textEncoder.encode(field);
28
+ throw new Error("Invalid tuple field: must be string or Uint8Array");
29
+ });
30
+ let totalLength = 4;
31
+ for (const fieldBytes of encodedFields) totalLength += 4 + fieldBytes.byteLength;
32
+ const out = new Uint8Array(totalLength);
33
+ let offset = 0;
34
+ for (const fieldBytes of encodedFields) {
35
+ writeU32BE(out, offset, fieldBytes.byteLength);
36
+ offset += 4;
37
+ out.set(fieldBytes, offset);
38
+ offset += fieldBytes.byteLength;
39
+ }
40
+ writeU32BE(out, offset, index);
41
+ return out;
42
+ }
43
+ function normalizeParentIdentity(parentIdentity) {
44
+ if (parentIdentity instanceof Uint8Array) {
45
+ if (parentIdentity.byteLength === 0) throw new Error("Invalid parentIdentity: must not be empty");
46
+ return parentIdentity;
47
+ }
48
+ if (typeof parentIdentity !== "string") throw new Error("Invalid parentIdentity: must be a non-empty string or Uint8Array");
49
+ const trimmed = parentIdentity.trim();
50
+ if (!trimmed) throw new Error("Invalid parentIdentity: must not be empty");
51
+ if (isHexStrict(trimmed)) {
52
+ const bytes = toUint8Array(trimmed);
53
+ if (bytes.byteLength === 0) throw new Error("Invalid parentIdentity: must not be empty");
54
+ return bytes;
55
+ }
56
+ if (/^[0-9a-fA-F]+$/.test(trimmed) && trimmed.length >= 64 && trimmed.length % 2 === 0) return toUint8Array(`0x${trimmed}`);
57
+ return textEncoder.encode(toAddress(trimmed));
58
+ }
59
+ /**
60
+ * Derive a deterministic 32-byte sub-account seed (`0x` + 64 hex chars) from a parent
61
+ * public key / root DID, scope (such as chain DID or persona namespace), and index:
62
+ *
63
+ * HMAC-SHA256(
64
+ * key = "arcblock:subaccount:v1",
65
+ * msg = u32be(|scope|) || scope || u32be(|parent|) || parent || u32be(index)
66
+ * )
67
+ *
68
+ * Security note (#1167):
69
+ * This is Layer 1 (deterministic sub-account address partitioning & cross-chain wrong-deposit
70
+ * protection). When used directly with a shared hardware Passkey public key on-chain, the
71
+ * public key remains visible in transaction headers; for on-chain explorer unlinkability,
72
+ * use Layer 2 (`fromChainDid` stealth key derivation or delegated per-chain session keys).
73
+ */
74
+ function deriveSubAccountSeed(parentIdentity, scope, index = 0) {
75
+ if (typeof scope !== "string" || scope.length === 0) throw new Error("Invalid scope: must be a non-empty string");
76
+ return hmacSha256(DOMAIN_KEY_BYTES, encodeLengthPrefixedTuple([scope, normalizeParentIdentity(parentIdentity)], index), "hex");
77
+ }
78
+ function createSubAccountHelpers(deps) {
79
+ const deriveSubAccountAddress = (parentIdentity, scope, index = 0, type = "default") => {
80
+ const seed = deriveSubAccountSeed(parentIdentity, scope, index);
81
+ return deps.fromPublicKeyHash(seed, type);
82
+ };
83
+ const isFromSubAccount = (subAccountDid, parentIdentity, scope, index = 0) => {
84
+ if (!deps.isValid(subAccountDid)) return false;
85
+ const cleanDid = toAddress(subAccountDid);
86
+ return deriveSubAccountAddress(parentIdentity, scope, index, toTypeInfo(cleanDid)) === cleanDid;
87
+ };
88
+ return {
89
+ deriveSubAccountAddress,
90
+ isFromSubAccount
91
+ };
92
+ }
93
+
94
+ //#endregion
95
+ export { SUBACCOUNT_DOMAIN_TAG, createSubAccountHelpers, deriveSubAccountSeed, encodeLengthPrefixedTuple };