@arcblock/did 1.30.26 → 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/did-domain-resolver.d.mts +450 -0
- package/esm/did-domain-resolver.mjs +1163 -0
- package/esm/index.d.mts +7 -5
- package/esm/index.mjs +7 -1
- package/esm/subaccount.d.mts +38 -0
- package/esm/subaccount.mjs +95 -0
- package/lib/did-domain-resolver.cjs +1190 -0
- package/lib/did-domain-resolver.d.cts +450 -0
- package/lib/index.cjs +41 -1
- package/lib/index.d.cts +8 -6
- package/lib/subaccount.cjs +98 -0
- package/lib/subaccount.d.cts +38 -0
- package/package.json +3 -3
|
@@ -0,0 +1,450 @@
|
|
|
1
|
+
import { DIDMethod } from "./method.cjs";
|
|
2
|
+
import { NameRegistry } from "./name-registry.cjs";
|
|
3
|
+
import { BytesType } from "@ocap/util";
|
|
4
|
+
|
|
5
|
+
//#region src/did-domain-resolver.d.ts
|
|
6
|
+
|
|
7
|
+
declare const DEFAULT_DID_DOMAIN_GATEWAY = "did.abtnet.io";
|
|
8
|
+
declare const DNS_NFT_BINDING_DOMAIN_TAG = "arcblock:dns-nft-binding:v1";
|
|
9
|
+
declare const DID_ENDPOINT_DOMAIN_TAG = "arcblock:did-endpoint:v1";
|
|
10
|
+
declare const KEY_ROTATION_DOMAIN_TAG = "arcblock:did-key-rotation:v1";
|
|
11
|
+
declare const PRE_ROTATION_DOMAIN_TAG = "arcblock:did-pre-rotation:v1";
|
|
12
|
+
declare const MAX_ROTATION_CHAIN_LENGTH = 64;
|
|
13
|
+
declare const MAX_CUSTOM_DOMAINS_PER_DID = 32;
|
|
14
|
+
type DnsDidLabelEncoding = 'base36' | 'hex';
|
|
15
|
+
/**
|
|
16
|
+
* Convert a DID (e.g. `did:abt:z1...` or `z1...` or `0x...`) into a DNS-case-safe
|
|
17
|
+
* lowercase subdomain label suitable for `<label>.did.abtnet.io`.
|
|
18
|
+
*
|
|
19
|
+
* Why this is needed:
|
|
20
|
+
* - Standard Base58btc DIDs (`z1nYowHg...`) are case-sensitive.
|
|
21
|
+
* - DNS hostnames (RFC 4343 / browsers) fold all labels to lowercase.
|
|
22
|
+
* - `toDnsDidLabel` encodes the 26-byte binary DID into either:
|
|
23
|
+
* - `'base36'` (default): `k` + 41 lowercase base36 chars (42 chars total, <= 63 DNS limit)
|
|
24
|
+
* - `'hex'`: `f` + 52 lowercase hex chars (53 chars total, <= 63 DNS limit)
|
|
25
|
+
* - For 20-byte Ethereum DIDs (`0x...`): `e` + 31 base36 chars (or `0x` + 40 hex chars).
|
|
26
|
+
*/
|
|
27
|
+
declare function toDnsDidLabel(did: string, encoding?: DnsDidLabelEncoding): string;
|
|
28
|
+
/**
|
|
29
|
+
* Decode a DNS subdomain label (or raw DID string) back into a canonical,
|
|
30
|
+
* checksum-verified DID (`did:abt:z...` or `did:abt:0x...`).
|
|
31
|
+
*
|
|
32
|
+
* Supports:
|
|
33
|
+
* - `k<41-char-base36>` (26-byte ArcBlock binary DID)
|
|
34
|
+
* - `f<52-char-hex>` (26-byte ArcBlock binary DID in hex)
|
|
35
|
+
* - `e<31-char-base36>` or `0x<40-char-hex>` (20-byte Ethereum DID)
|
|
36
|
+
* - Case-preserved raw Base58btc DID (`z...`) when routed via Cloudflare Worker paths/headers
|
|
37
|
+
*/
|
|
38
|
+
declare function fromDnsDidLabel(label: string, method?: DIDMethod): string;
|
|
39
|
+
/**
|
|
40
|
+
* Format the canonical DID Domain hostname (`<label>.did.abtnet.io`).
|
|
41
|
+
*/
|
|
42
|
+
declare function toDidDomainHost(did: string, gatewayDomain?: string, encoding?: DnsDidLabelEncoding): string;
|
|
43
|
+
/**
|
|
44
|
+
* Extract and verify the canonical DID from a `<label>.did.abtnet.io` hostname.
|
|
45
|
+
*/
|
|
46
|
+
declare function extractDidFromDomainHost(hostname: string, gatewayDomain?: string, method?: DIDMethod): string | null;
|
|
47
|
+
/**
|
|
48
|
+
* Construct the canonical `.well-known` resolution URL for a DID via the DID Domain Gateway.
|
|
49
|
+
*/
|
|
50
|
+
declare function toDidWellKnownUrl(did: string, options?: {
|
|
51
|
+
gatewayDomain?: string;
|
|
52
|
+
encoding?: DnsDidLabelEncoding;
|
|
53
|
+
document?: 'did.json' | 'did.jsonl' | 'did-configuration.json';
|
|
54
|
+
protocol?: 'https' | 'http';
|
|
55
|
+
}): string;
|
|
56
|
+
interface KeyRotationEvent {
|
|
57
|
+
domain: typeof KEY_ROTATION_DOMAIN_TAG;
|
|
58
|
+
/** The persistent DID being rotated (e.g. `did:abt:z1...`) */
|
|
59
|
+
did: string;
|
|
60
|
+
/** Monotonic sequence number starting at 1 for the first rotation */
|
|
61
|
+
seq: number;
|
|
62
|
+
/** Previous active public key (hex) */
|
|
63
|
+
previousPublicKey: string;
|
|
64
|
+
/** Newly activated public key (hex) */
|
|
65
|
+
newPublicKey: string;
|
|
66
|
+
/** New key algorithm (`ED25519`, `SECP256K1`, `ETHEREUM`, `PASSKEY`) */
|
|
67
|
+
newKeyType: number;
|
|
68
|
+
/** Optional commitment (`createPreRotationCommitment`) for the *next* future key rotation */
|
|
69
|
+
nextKeyCommitment?: string;
|
|
70
|
+
/** Unix timestamp (seconds) of rotation */
|
|
71
|
+
rotatedAt: number;
|
|
72
|
+
/** Signature by `previousPublicKey` authorizing the rotation */
|
|
73
|
+
previousKeySignature: string;
|
|
74
|
+
/** Signature by `newPublicKey` proving possession of the rotated key */
|
|
75
|
+
newKeySignature: string;
|
|
76
|
+
}
|
|
77
|
+
/**
|
|
78
|
+
* Normalize a public key (`0x` hex, raw hex, `z` Base58btc / Base58 string, or `Uint8Array`)
|
|
79
|
+
* into a canonical lowercase hex string (without `0x` prefix).
|
|
80
|
+
*/
|
|
81
|
+
declare function normalizePublicKeyHex(pk: BytesType): string;
|
|
82
|
+
/**
|
|
83
|
+
* Validate whether an input is a canonical 32-byte (64-hex-char) SHA3-256 pre-rotation commitment.
|
|
84
|
+
*/
|
|
85
|
+
declare function isValidPreRotationCommitment(commitment: unknown): commitment is string;
|
|
86
|
+
/**
|
|
87
|
+
* Normalize a pre-rotation commitment into canonical lowercase `0x<64-hex>`.
|
|
88
|
+
*/
|
|
89
|
+
declare function normalizePreRotationCommitment(commitment: string): string;
|
|
90
|
+
/**
|
|
91
|
+
* Compute a KERI-style pre-rotation commitment (`sha3-256` over domain tag + keyType + normalized publicKey).
|
|
92
|
+
* Even if the current hot key is compromised, an attacker cannot rotate the DID without the pre-committed key.
|
|
93
|
+
*/
|
|
94
|
+
declare function createPreRotationCommitment(nextPublicKey: BytesType, nextKeyType?: number): string;
|
|
95
|
+
/**
|
|
96
|
+
* Verify whether `publicKey` (and `keyType` or target `address` DID type) satisfies an active `commitment`.
|
|
97
|
+
*/
|
|
98
|
+
declare function verifyPreRotationCommitment(params: {
|
|
99
|
+
commitment: string;
|
|
100
|
+
publicKey: BytesType;
|
|
101
|
+
keyType?: number;
|
|
102
|
+
address?: string;
|
|
103
|
+
}): boolean;
|
|
104
|
+
/**
|
|
105
|
+
* Create a dual-signed Key Rotation Event (`previousKey` + `newKey`) for a DID.
|
|
106
|
+
*/
|
|
107
|
+
declare function createKeyRotationEvent(params: {
|
|
108
|
+
did: string;
|
|
109
|
+
seq: number;
|
|
110
|
+
previousPublicKey: string;
|
|
111
|
+
previousSecretKey: BytesType;
|
|
112
|
+
previousKeyType?: number;
|
|
113
|
+
newPublicKey: string;
|
|
114
|
+
newSecretKey: BytesType;
|
|
115
|
+
newKeyType?: number;
|
|
116
|
+
nextKeyCommitment?: string;
|
|
117
|
+
rotatedAt?: number;
|
|
118
|
+
}): KeyRotationEvent;
|
|
119
|
+
/**
|
|
120
|
+
* Verify an offline Key Rotation Event Log (`KeyRotationEvent[]`) for a `did`.
|
|
121
|
+
*
|
|
122
|
+
* Enforces:
|
|
123
|
+
* 1. Genesis anchor: Event #1's `previousPublicKey` MUST derive `did` (`fromPublicKey` match),
|
|
124
|
+
* and if `genesisPreRotationCommitment` is provided, Event #1's `newPublicKey` must match it.
|
|
125
|
+
* 2. Hash/key continuity: Each subsequent event `seq = i` must have `previousPublicKey === event[i-1].newPublicKey`.
|
|
126
|
+
* 3. Pre-rotation commitment continuity: If `event[i-1].nextKeyCommitment` is present,
|
|
127
|
+
* `createPreRotationCommitment(event[i].newPublicKey, event[i].newKeyType)` MUST match it!
|
|
128
|
+
* 4. Dual signatures (`previousKeySignature` and `newKeySignature`) must be cryptographically valid.
|
|
129
|
+
*/
|
|
130
|
+
declare function verifyKeyRotationChain(params: {
|
|
131
|
+
did: string;
|
|
132
|
+
events: KeyRotationEvent[];
|
|
133
|
+
expectedCurrentPublicKey?: string;
|
|
134
|
+
genesisPreRotationCommitment?: string;
|
|
135
|
+
}): {
|
|
136
|
+
valid: boolean;
|
|
137
|
+
activePublicKey: string | null;
|
|
138
|
+
activeKeyType: number | null;
|
|
139
|
+
nextKeyCommitment?: string;
|
|
140
|
+
reason?: string;
|
|
141
|
+
};
|
|
142
|
+
interface AccountPreRotationStateData {
|
|
143
|
+
/** Active KERI pre-rotation commitment (`0x` + 64 hex) for the NEXT key migration */
|
|
144
|
+
nextKeyCommitment?: string;
|
|
145
|
+
/** Consumed pre-rotation commitment satisfied by the most recent migration */
|
|
146
|
+
previousKeyCommitment?: string;
|
|
147
|
+
/** Historical consumed/retired key commitments across the migration chain (bounded by MAX_ROTATION_CHAIN_LENGTH) */
|
|
148
|
+
consumedKeyCommitments?: string[];
|
|
149
|
+
/** Monotonic count of verified pre-rotation migrations completed by this account lineage */
|
|
150
|
+
preRotationSeq?: number;
|
|
151
|
+
/** Optional latest signed KERI KeyRotationEvent anchored during account_migrate */
|
|
152
|
+
lastKeyRotationEvent?: KeyRotationEvent;
|
|
153
|
+
[key: string]: unknown;
|
|
154
|
+
}
|
|
155
|
+
/**
|
|
156
|
+
* Extract the plain `AccountPreRotationStateData` object from `IAccountState.data` or `itx.data`,
|
|
157
|
+
* supporting `{ typeUrl: 'json', value: { ... } }`, `{ type: 'json', value: { ... } }`, or a plain object.
|
|
158
|
+
*/
|
|
159
|
+
declare function extractAccountPreRotationData(rawData: unknown): AccountPreRotationStateData | null;
|
|
160
|
+
/**
|
|
161
|
+
* Construct a canonical `itx.data` payload (`{ type: 'json', value: ... }`) for `AccountMigrateTx`
|
|
162
|
+
* carrying a KERI `nextKeyCommitment` (for the next hop) and/or an inline `KeyRotationEvent`.
|
|
163
|
+
*/
|
|
164
|
+
declare function createAccountMigratePreRotationData(params: {
|
|
165
|
+
nextKeyCommitment?: string | null;
|
|
166
|
+
keyRotationEvent?: KeyRotationEvent;
|
|
167
|
+
purpose?: string;
|
|
168
|
+
extra?: Record<string, unknown>;
|
|
169
|
+
}): {
|
|
170
|
+
type: 'json';
|
|
171
|
+
value: Record<string, unknown>;
|
|
172
|
+
};
|
|
173
|
+
/**
|
|
174
|
+
* Verify a single KERI `KeyRotationEvent` against an on-chain `account_migrate` transition
|
|
175
|
+
* (`senderAddress -> receiverAddress` with `receiverPublicKey` and optional `activePreRotationCommitment`).
|
|
176
|
+
*/
|
|
177
|
+
declare function verifySingleKeyRotationEvent(params: {
|
|
178
|
+
event: KeyRotationEvent;
|
|
179
|
+
senderAddress: string;
|
|
180
|
+
receiverAddress: string;
|
|
181
|
+
receiverPublicKey: BytesType;
|
|
182
|
+
activePreRotationCommitment?: string;
|
|
183
|
+
expectedGenesisDid?: string;
|
|
184
|
+
expectedSeq?: number;
|
|
185
|
+
minRotatedAt?: number;
|
|
186
|
+
}): {
|
|
187
|
+
valid: boolean;
|
|
188
|
+
nextKeyCommitment?: string;
|
|
189
|
+
reason?: string;
|
|
190
|
+
};
|
|
191
|
+
/**
|
|
192
|
+
* Verify that an on-chain OCAP `account_migrate` state chain (`migratedFrom` + `currentAddress` + `currentPublicKey`)
|
|
193
|
+
* is 100% cryptographically consistent with an off-chain KERI `KeyRotationEvent[]` log (`KEL`).
|
|
194
|
+
*
|
|
195
|
+
* In OCAP `account_migrate`, `migratedFrom` on the current terminal account is ordered newest-to-oldest:
|
|
196
|
+
* `[A_{n-1}, ..., A_1, A_0]`, where `A_0` is the genesis DID and `currentAddress` is `A_n`.
|
|
197
|
+
*/
|
|
198
|
+
declare function verifyAccountMigrationChainWithKel(params: {
|
|
199
|
+
genesisDid: string;
|
|
200
|
+
migratedFrom: string[];
|
|
201
|
+
currentAddress: string;
|
|
202
|
+
currentPublicKey: BytesType;
|
|
203
|
+
events: KeyRotationEvent[];
|
|
204
|
+
genesisPreRotationCommitment?: string;
|
|
205
|
+
}): {
|
|
206
|
+
valid: boolean;
|
|
207
|
+
activePublicKey: string | null;
|
|
208
|
+
activeKeyType: number | null;
|
|
209
|
+
nextKeyCommitment?: string;
|
|
210
|
+
reason?: string;
|
|
211
|
+
};
|
|
212
|
+
interface DIDServiceEndpoint {
|
|
213
|
+
id: string;
|
|
214
|
+
type: 'ArcSpacePDS' | 'AfsAgentEndpoint' | 'DIDResolverService' | 'NoiseMeshEndpoint' | string;
|
|
215
|
+
serviceEndpoint: string | Record<string, unknown>;
|
|
216
|
+
description?: string;
|
|
217
|
+
}
|
|
218
|
+
interface W3CDIDVerificationMethod {
|
|
219
|
+
id: string;
|
|
220
|
+
type: string;
|
|
221
|
+
controller: string;
|
|
222
|
+
publicKeyHex?: string;
|
|
223
|
+
arcblockMeta?: {
|
|
224
|
+
role?: string;
|
|
225
|
+
keyType?: string;
|
|
226
|
+
hashType?: string;
|
|
227
|
+
};
|
|
228
|
+
}
|
|
229
|
+
interface W3CDIDDocument {
|
|
230
|
+
'@context': string[];
|
|
231
|
+
id: string;
|
|
232
|
+
alsoKnownAs?: string[];
|
|
233
|
+
controller?: string | string[];
|
|
234
|
+
verificationMethod: W3CDIDVerificationMethod[];
|
|
235
|
+
authentication: string[];
|
|
236
|
+
assertionMethod: string[];
|
|
237
|
+
capabilityInvocation: string[];
|
|
238
|
+
capabilityDelegation: string[];
|
|
239
|
+
keyAgreement?: string[];
|
|
240
|
+
service?: DIDServiceEndpoint[];
|
|
241
|
+
nextKeyCommitment?: string;
|
|
242
|
+
keyRotationHistory?: KeyRotationEvent[];
|
|
243
|
+
}
|
|
244
|
+
interface SynthesizeDidDocumentOptions {
|
|
245
|
+
did: string;
|
|
246
|
+
/** Optional public key (hex) for full verificationMethod hydration */
|
|
247
|
+
publicKey?: string;
|
|
248
|
+
/** Optional X25519 public key (hex) from `@arcblock/did-noise` for E2E encrypted keyAgreement */
|
|
249
|
+
x25519PublicKey?: string;
|
|
250
|
+
/** Bound human-readable names / custom domains (e.g. `['alice.com', 'did:name:alice']`) */
|
|
251
|
+
customDomains?: string[];
|
|
252
|
+
/** Gateway domain (default: `did.abtnet.io`) */
|
|
253
|
+
gatewayDomain?: string;
|
|
254
|
+
/** Optional physical/edge ArcSpace endpoint URL (e.g. `https://space.alice.com` or tunnel URL) */
|
|
255
|
+
arcSpaceEndpoint?: string;
|
|
256
|
+
/** Optional AFS (`/.actions/...`) endpoint URL */
|
|
257
|
+
afsEndpoint?: string;
|
|
258
|
+
/** Optional Noise Mesh P2P endpoint (`noise://...`) */
|
|
259
|
+
noiseMeshEndpoint?: string;
|
|
260
|
+
/** Optional KERI pre-rotation commitment for the next rotation key */
|
|
261
|
+
nextKeyCommitment?: string;
|
|
262
|
+
/** Optional verified Key Rotation Event Log */
|
|
263
|
+
keyRotationEvents?: KeyRotationEvent[];
|
|
264
|
+
}
|
|
265
|
+
/**
|
|
266
|
+
* Synthesize a deterministic W3C DID v1.1 Document for any `did:abt:`, `did:spaces:`, `did:afs:`, or `did:aos:`.
|
|
267
|
+
*/
|
|
268
|
+
declare function synthesizeDidDocument(options: SynthesizeDidDocumentOptions): W3CDIDDocument;
|
|
269
|
+
interface DomainNftAssetSnapshot {
|
|
270
|
+
/** On-chain NFT asset DID (`ROLE_ASSET`) representing the DNS/DID domain */
|
|
271
|
+
address: string;
|
|
272
|
+
/** Current owner DID in `statedb.asset` */
|
|
273
|
+
owner: string;
|
|
274
|
+
/** Whether the NFT has been consumed/burned */
|
|
275
|
+
consumed?: boolean;
|
|
276
|
+
/** Optional moniker or domain name in `data.domain` */
|
|
277
|
+
moniker?: string;
|
|
278
|
+
data?: {
|
|
279
|
+
domain?: string;
|
|
280
|
+
[key: string]: unknown;
|
|
281
|
+
};
|
|
282
|
+
}
|
|
283
|
+
interface DomainNftBindingProof {
|
|
284
|
+
domain: typeof DNS_NFT_BINDING_DOMAIN_TAG;
|
|
285
|
+
/** Human-readable custom domain or `did:name` (e.g., `alice.com` or `alice.arcblock.io`) */
|
|
286
|
+
customDomain: string;
|
|
287
|
+
/** On-chain NFT asset DID (`ROLE_ASSET`) representing ownership of `customDomain` */
|
|
288
|
+
domainNftAssetDid: string;
|
|
289
|
+
/** Owner DID (`did:abt:z1...`) holding the Domain NFT */
|
|
290
|
+
ownerDid: string;
|
|
291
|
+
/** Owner public key (hex) */
|
|
292
|
+
ownerPublicKey: string;
|
|
293
|
+
/**
|
|
294
|
+
* Canonical Tier-2 CNAME target (`<dnsDidLabel>.did.abtnet.io`).
|
|
295
|
+
* MUST always be the owner's DID domain — never a raw IP address!
|
|
296
|
+
*/
|
|
297
|
+
cnameTargetDidDomain: string;
|
|
298
|
+
/** Issued timestamp (seconds) */
|
|
299
|
+
issuedAt: number;
|
|
300
|
+
/** Expiration timestamp (seconds, 0 = until NFT transfer) */
|
|
301
|
+
expiresAt: number;
|
|
302
|
+
/** Owner cryptographic signature authorizing the DID NS / Cloudflare Worker binding */
|
|
303
|
+
ownerSignature: string;
|
|
304
|
+
}
|
|
305
|
+
interface DidEndpointRecord {
|
|
306
|
+
did: string;
|
|
307
|
+
didDomainHost: string;
|
|
308
|
+
/** Physical/edge endpoint URL (e.g. `https://198.51.100.42:8443` or Cloudflare Tunnel URL) */
|
|
309
|
+
endpointUrl: string;
|
|
310
|
+
/** Optional underlying IP or mesh address */
|
|
311
|
+
ipOrMeshAddress?: string;
|
|
312
|
+
/** Optional public key for W3C DID Document synthesis */
|
|
313
|
+
publicKey?: string;
|
|
314
|
+
/** Optional X25519 keyAgreement public key */
|
|
315
|
+
x25519PublicKey?: string;
|
|
316
|
+
/** Optional AFS endpoint */
|
|
317
|
+
afsEndpoint?: string;
|
|
318
|
+
/** Optional KERI pre-rotation commitment */
|
|
319
|
+
nextKeyCommitment?: string;
|
|
320
|
+
/** Optional verified key rotation history */
|
|
321
|
+
keyRotationEvents?: KeyRotationEvent[];
|
|
322
|
+
/** Whether this endpoint registration was cryptographically signed by the DID's active key */
|
|
323
|
+
authenticated?: boolean;
|
|
324
|
+
/** Signature over `DID_ENDPOINT_DOMAIN_TAG` + `did` + `endpointUrl` + `updatedAt` */
|
|
325
|
+
endpointSignature?: string;
|
|
326
|
+
updatedAt: number;
|
|
327
|
+
}
|
|
328
|
+
interface ThreeTierResolutionResult {
|
|
329
|
+
found: boolean;
|
|
330
|
+
/** Query input (`alice.com`, `did:name:alice`, `<label>.did.abtnet.io`, or `did:abt:z1...`) */
|
|
331
|
+
query: string;
|
|
332
|
+
/** Tier 1: Custom domain (if resolved via custom domain or `did:name:`) */
|
|
333
|
+
customDomain?: string;
|
|
334
|
+
/** Bound DNS Domain NFT DID (if resolved via NFT-backed custom domain) */
|
|
335
|
+
domainNftAssetDid?: string;
|
|
336
|
+
/** Tier 2: Canonical CNAME DID Domain (`<label>.did.abtnet.io`) */
|
|
337
|
+
cnameTargetDidDomain: string;
|
|
338
|
+
/** Resolved canonical DID (`did:abt:...`) */
|
|
339
|
+
did: string;
|
|
340
|
+
/** Tier 3: Physical / Tunnel / Edge endpoint record */
|
|
341
|
+
endpoint?: DidEndpointRecord;
|
|
342
|
+
/** Synthesized W3C DID v1.1 Document (`/.well-known/did.json`) */
|
|
343
|
+
didDocument: W3CDIDDocument;
|
|
344
|
+
/** Resolution trace across the 3 tiers */
|
|
345
|
+
trace: string[];
|
|
346
|
+
}
|
|
347
|
+
declare function encodeDidEndpointDigest(params: {
|
|
348
|
+
did: string;
|
|
349
|
+
endpointUrl: string;
|
|
350
|
+
updatedAt: number;
|
|
351
|
+
}): string;
|
|
352
|
+
/**
|
|
353
|
+
* Create a signed Domain-NFT Binding Proof authorizing the DID Name Server / Cloudflare Worker
|
|
354
|
+
* to bind `customDomain -> CNAME <label>.did.abtnet.io`.
|
|
355
|
+
*/
|
|
356
|
+
declare function createDomainNftBindingProof(params: {
|
|
357
|
+
customDomain: string;
|
|
358
|
+
domainNftAssetDid: string;
|
|
359
|
+
ownerDid: string;
|
|
360
|
+
ownerPublicKey: string;
|
|
361
|
+
ownerSecretKey: BytesType;
|
|
362
|
+
gatewayDomain?: string;
|
|
363
|
+
issuedAt?: number;
|
|
364
|
+
expiresAt?: number;
|
|
365
|
+
}): DomainNftBindingProof;
|
|
366
|
+
/**
|
|
367
|
+
* Verify a Domain-NFT Binding Proof against:
|
|
368
|
+
* 1. Canonical 3-Tier CNAME rule (`cnameTargetDidDomain` MUST decode to `ownerDid` under `gatewayDomain`, never a raw IP);
|
|
369
|
+
* 2. `domainNftAssetDid` role (`ROLE_ASSET`) and optional live `nftAssetState` ownership/domain match;
|
|
370
|
+
* 3. Cryptographic signature by `ownerDid` (`ownerPublicKey` or rotated key via `keyRotationEvents`).
|
|
371
|
+
*/
|
|
372
|
+
declare function verifyDomainNftBindingProof(proof: DomainNftBindingProof, options?: {
|
|
373
|
+
gatewayDomain?: string;
|
|
374
|
+
nftAssetState?: DomainNftAssetSnapshot;
|
|
375
|
+
keyRotationEvents?: KeyRotationEvent[];
|
|
376
|
+
now?: number;
|
|
377
|
+
}): {
|
|
378
|
+
valid: boolean;
|
|
379
|
+
reason?: string;
|
|
380
|
+
};
|
|
381
|
+
/**
|
|
382
|
+
* Cloudflare Worker & `ARC` Edge DID Domain Router (`Custom Domain -> CNAME DID Domain -> Endpoint`).
|
|
383
|
+
*
|
|
384
|
+
* Implements `NameRegistry` so it can also act as the backing store for `DIDNameResolver`.
|
|
385
|
+
* Supports wildcard `<label>.did.abtnet.io` resolution with zero pre-registration needed
|
|
386
|
+
* for the DID identity layer, plus NFT-verified custom domain bindings.
|
|
387
|
+
*/
|
|
388
|
+
declare class DIDDomainEdgeRouter implements NameRegistry {
|
|
389
|
+
readonly gatewayDomain: string;
|
|
390
|
+
private bindingsByDomain;
|
|
391
|
+
private nftStateByDomain;
|
|
392
|
+
private endpointsByDidAddr;
|
|
393
|
+
private simpleNameMap;
|
|
394
|
+
constructor(options?: {
|
|
395
|
+
gatewayDomain?: string;
|
|
396
|
+
});
|
|
397
|
+
/**
|
|
398
|
+
* Register or update a Tier-3 physical/tunnel endpoint for a DID (`<label>.did.abtnet.io -> endpoint`).
|
|
399
|
+
* Updating Tier 3 never requires changing Tier 1 (`Custom Domain -> CNAME DID Domain`).
|
|
400
|
+
*
|
|
401
|
+
* Security invariants:
|
|
402
|
+
* - If `keyRotationEvents` or `publicKey` is supplied, it is cryptographically validated against `did` immediately.
|
|
403
|
+
* - Supports cryptographic authentication via `secretKey` or `publicKey + endpointSignature`.
|
|
404
|
+
* - Once an endpoint for a DID is cryptographically authenticated (`authenticated: true`), any subsequent
|
|
405
|
+
* update MUST also be cryptographically authenticated with `updatedAt >= existing.updatedAt`.
|
|
406
|
+
*/
|
|
407
|
+
registerDidEndpoint(input: {
|
|
408
|
+
did: string;
|
|
409
|
+
endpointUrl: string;
|
|
410
|
+
ipOrMeshAddress?: string;
|
|
411
|
+
publicKey?: string;
|
|
412
|
+
secretKey?: BytesType;
|
|
413
|
+
endpointSignature?: string;
|
|
414
|
+
x25519PublicKey?: string;
|
|
415
|
+
afsEndpoint?: string;
|
|
416
|
+
nextKeyCommitment?: string;
|
|
417
|
+
keyRotationEvents?: KeyRotationEvent[];
|
|
418
|
+
updatedAt?: number;
|
|
419
|
+
}): DidEndpointRecord;
|
|
420
|
+
/**
|
|
421
|
+
* Register a Tier-1 -> Tier-2 custom domain binding verified by an on-chain DNS Domain NFT proof.
|
|
422
|
+
*/
|
|
423
|
+
registerDomainNftBinding(proof: DomainNftBindingProof, options?: {
|
|
424
|
+
nftAssetState?: DomainNftAssetSnapshot;
|
|
425
|
+
keyRotationEvents?: KeyRotationEvent[];
|
|
426
|
+
now?: number;
|
|
427
|
+
}): DomainNftBindingProof;
|
|
428
|
+
/**
|
|
429
|
+
* Update the cached on-chain DNS Domain NFT state snapshot for a custom domain
|
|
430
|
+
* (e.g., when an indexer or chain listener observes an NFT transfer or consumption).
|
|
431
|
+
*/
|
|
432
|
+
updateDomainNftState(customDomain: string, nftAssetState: DomainNftAssetSnapshot): void;
|
|
433
|
+
/**
|
|
434
|
+
* Resolve any hostname, `did:name:`, `<label>.did.abtnet.io`, or `did:abt:` across all 3 tiers:
|
|
435
|
+
* `Custom Domain (Tier 1) -> CNAME <label>.did.abtnet.io (Tier 2) -> Endpoint & DID Document (Tier 3)`.
|
|
436
|
+
*/
|
|
437
|
+
resolveThreeTier(queryInput: string, options?: {
|
|
438
|
+
nftAssetState?: DomainNftAssetSnapshot;
|
|
439
|
+
now?: number;
|
|
440
|
+
}): ThreeTierResolutionResult | null;
|
|
441
|
+
private _buildResult;
|
|
442
|
+
resolve(name: string): string | null;
|
|
443
|
+
register(name: string, did: string): void;
|
|
444
|
+
unregister(name: string): void;
|
|
445
|
+
has(name: string): boolean;
|
|
446
|
+
list(): string[];
|
|
447
|
+
clear(): void;
|
|
448
|
+
}
|
|
449
|
+
//#endregion
|
|
450
|
+
export { 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 };
|
package/lib/index.cjs
CHANGED
|
@@ -8,6 +8,8 @@ const require_validate = require('./validate.cjs');
|
|
|
8
8
|
const require_short_form = require('./short-form.cjs');
|
|
9
9
|
const require_name_registry = require('./name-registry.cjs');
|
|
10
10
|
const require_name_resolver = require('./name-resolver.cjs');
|
|
11
|
+
const require_subaccount = require('./subaccount.cjs');
|
|
12
|
+
const require_did_domain_resolver = require('./did-domain-resolver.cjs');
|
|
11
13
|
let _ocap_mcrypto = require("@ocap/mcrypto");
|
|
12
14
|
let _ocap_util = require("@ocap/util");
|
|
13
15
|
|
|
@@ -99,23 +101,47 @@ const isValid = (did) => {
|
|
|
99
101
|
const bytesHex = require_util.toStrictHex(Buffer.from(Uint8Array.from(bytes.slice(0, 22))).toString("hex"));
|
|
100
102
|
return require_util.toStrictHex(Buffer.from(Uint8Array.from(bytes.slice(22, 26))).toString("hex")) === (0, _ocap_util.stripHexPrefix)(hashFn(`0x${bytesHex}`, 1)).slice(0, 8);
|
|
101
103
|
};
|
|
104
|
+
const { deriveSubAccountAddress, isFromSubAccount } = require_subaccount.createSubAccountHelpers({
|
|
105
|
+
fromPublicKeyHash,
|
|
106
|
+
isValid
|
|
107
|
+
});
|
|
102
108
|
|
|
103
109
|
//#endregion
|
|
104
110
|
exports.ALIAS_METHODS = require_method.ALIAS_METHODS;
|
|
105
111
|
exports.ALL_METHODS = require_method.ALL_METHODS;
|
|
106
112
|
exports.CRYPTO_METHODS = require_method.CRYPTO_METHODS;
|
|
113
|
+
exports.DEFAULT_DID_DOMAIN_GATEWAY = require_did_domain_resolver.DEFAULT_DID_DOMAIN_GATEWAY;
|
|
107
114
|
exports.DEFAULT_METHOD = require_method.DEFAULT_METHOD;
|
|
115
|
+
exports.DIDDomainEdgeRouter = require_did_domain_resolver.DIDDomainEdgeRouter;
|
|
108
116
|
exports.DIDNameResolver = require_name_resolver.DIDNameResolver;
|
|
117
|
+
exports.DID_ENDPOINT_DOMAIN_TAG = require_did_domain_resolver.DID_ENDPOINT_DOMAIN_TAG;
|
|
109
118
|
exports.DID_PREFIX = require_util.DID_PREFIX;
|
|
110
119
|
exports.DID_TYPE_ARCBLOCK = require_type.DID_TYPE_ARCBLOCK;
|
|
111
120
|
exports.DID_TYPE_ETHEREUM = require_type.DID_TYPE_ETHEREUM;
|
|
112
121
|
exports.DID_TYPE_PASSKEY = require_type.DID_TYPE_PASSKEY;
|
|
122
|
+
exports.DNS_NFT_BINDING_DOMAIN_TAG = require_did_domain_resolver.DNS_NFT_BINDING_DOMAIN_TAG;
|
|
113
123
|
exports.DidType = require_type.DidType;
|
|
114
124
|
exports.InMemoryNameRegistry = require_name_registry.InMemoryNameRegistry;
|
|
125
|
+
exports.KEY_ROTATION_DOMAIN_TAG = require_did_domain_resolver.KEY_ROTATION_DOMAIN_TAG;
|
|
126
|
+
exports.MAX_CUSTOM_DOMAINS_PER_DID = require_did_domain_resolver.MAX_CUSTOM_DOMAINS_PER_DID;
|
|
127
|
+
exports.MAX_ROTATION_CHAIN_LENGTH = require_did_domain_resolver.MAX_ROTATION_CHAIN_LENGTH;
|
|
128
|
+
exports.PRE_ROTATION_DOMAIN_TAG = require_did_domain_resolver.PRE_ROTATION_DOMAIN_TAG;
|
|
129
|
+
exports.SUBACCOUNT_DOMAIN_TAG = require_subaccount.SUBACCOUNT_DOMAIN_TAG;
|
|
130
|
+
exports.createAccountMigratePreRotationData = require_did_domain_resolver.createAccountMigratePreRotationData;
|
|
131
|
+
exports.createDomainNftBindingProof = require_did_domain_resolver.createDomainNftBindingProof;
|
|
132
|
+
exports.createKeyRotationEvent = require_did_domain_resolver.createKeyRotationEvent;
|
|
133
|
+
exports.createPreRotationCommitment = require_did_domain_resolver.createPreRotationCommitment;
|
|
134
|
+
exports.deriveSubAccountAddress = deriveSubAccountAddress;
|
|
135
|
+
exports.deriveSubAccountSeed = require_subaccount.deriveSubAccountSeed;
|
|
136
|
+
exports.encodeDidEndpointDigest = require_did_domain_resolver.encodeDidEndpointDigest;
|
|
137
|
+
exports.encodeLengthPrefixedTuple = require_subaccount.encodeLengthPrefixedTuple;
|
|
115
138
|
exports.expandShortForm = require_short_form.expandShortForm;
|
|
139
|
+
exports.extractAccountPreRotationData = require_did_domain_resolver.extractAccountPreRotationData;
|
|
140
|
+
exports.extractDidFromDomainHost = require_did_domain_resolver.extractDidFromDomainHost;
|
|
116
141
|
exports.extractIdentifier = require_parse.extractIdentifier;
|
|
117
142
|
exports.extractMethod = require_parse.extractMethod;
|
|
118
143
|
exports.formatDid = require_parse.formatDid;
|
|
144
|
+
exports.fromDnsDidLabel = require_did_domain_resolver.fromDnsDidLabel;
|
|
119
145
|
exports.fromHash = fromHash;
|
|
120
146
|
exports.fromPublicKey = fromPublicKey;
|
|
121
147
|
exports.fromPublicKeyHash = fromPublicKeyHash;
|
|
@@ -128,19 +154,33 @@ exports.isCryptoMethod = require_method.isCryptoMethod;
|
|
|
128
154
|
exports.isEthereumDid = require_type.isEthereumDid;
|
|
129
155
|
exports.isEthereumType = require_type.isEthereumType;
|
|
130
156
|
exports.isFromPublicKey = isFromPublicKey;
|
|
157
|
+
exports.isFromSubAccount = isFromSubAccount;
|
|
131
158
|
exports.isGlobalName = require_name.isGlobalName;
|
|
132
159
|
exports.isKnownDid = require_validate.isKnownDid;
|
|
133
160
|
exports.isKnownMethod = require_method.isKnownMethod;
|
|
134
161
|
exports.isLocalName = require_name.isLocalName;
|
|
135
162
|
exports.isSameEntity = require_entity.isSameEntity;
|
|
136
163
|
exports.isValid = isValid;
|
|
164
|
+
exports.isValidPreRotationCommitment = require_did_domain_resolver.isValidPreRotationCommitment;
|
|
137
165
|
exports.normalizeName = require_name.normalizeName;
|
|
166
|
+
exports.normalizePreRotationCommitment = require_did_domain_resolver.normalizePreRotationCommitment;
|
|
167
|
+
exports.normalizePublicKeyHex = require_did_domain_resolver.normalizePublicKeyHex;
|
|
138
168
|
exports.parse = require_parse.parse;
|
|
139
169
|
exports.parseNameHierarchy = require_name.parseNameHierarchy;
|
|
170
|
+
exports.synthesizeDidDocument = require_did_domain_resolver.synthesizeDidDocument;
|
|
140
171
|
exports.toAddress = _ocap_util.toAddress;
|
|
172
|
+
exports.toChecksumAddress = require_type.toChecksumAddress;
|
|
141
173
|
exports.toDid = _ocap_util.toDid;
|
|
174
|
+
exports.toDidDomainHost = require_did_domain_resolver.toDidDomainHost;
|
|
175
|
+
exports.toDidWellKnownUrl = require_did_domain_resolver.toDidWellKnownUrl;
|
|
176
|
+
exports.toDnsDidLabel = require_did_domain_resolver.toDnsDidLabel;
|
|
142
177
|
exports.toShortForm = require_short_form.toShortForm;
|
|
143
178
|
exports.toStrictHex = require_util.toStrictHex;
|
|
144
179
|
exports.toTypeInfo = require_type.toTypeInfo;
|
|
145
180
|
exports.toTypeInfoStr = require_type.toTypeInfoStr;
|
|
146
|
-
exports.types = _ocap_mcrypto.types;
|
|
181
|
+
exports.types = _ocap_mcrypto.types;
|
|
182
|
+
exports.verifyAccountMigrationChainWithKel = require_did_domain_resolver.verifyAccountMigrationChainWithKel;
|
|
183
|
+
exports.verifyDomainNftBindingProof = require_did_domain_resolver.verifyDomainNftBindingProof;
|
|
184
|
+
exports.verifyKeyRotationChain = require_did_domain_resolver.verifyKeyRotationChain;
|
|
185
|
+
exports.verifyPreRotationCommitment = require_did_domain_resolver.verifyPreRotationCommitment;
|
|
186
|
+
exports.verifySingleKeyRotationEvent = require_did_domain_resolver.verifySingleKeyRotationEvent;
|
package/lib/index.d.cts
CHANGED
|
@@ -1,18 +1,19 @@
|
|
|
1
|
+
import { ALIAS_METHODS, ALL_METHODS, AliasMethod, CRYPTO_METHODS, CryptoMethod, DEFAULT_METHOD, DIDMethod, isAliasMethod, isCryptoMethod, isKnownMethod } from "./method.cjs";
|
|
2
|
+
import { InMemoryNameRegistry, NameRegistry } from "./name-registry.cjs";
|
|
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.cjs";
|
|
1
4
|
import { getEntityId, isSameEntity } from "./entity.cjs";
|
|
2
|
-
import { DIDType, DIDTypeArg, DIDTypeShortcut, DIDTypeStr, DID_TYPE_ARCBLOCK, DID_TYPE_ETHEREUM, DID_TYPE_PASSKEY, DidType, RequiredDIDType, fromTypeInfo, isEthereumDid, isEthereumType, toTypeInfo, toTypeInfoStr } from "./type.cjs";
|
|
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.cjs";
|
|
3
6
|
import { DID_PREFIX, toStrictHex } from "./util.cjs";
|
|
4
|
-
import { ALIAS_METHODS, ALL_METHODS, AliasMethod, CRYPTO_METHODS, CryptoMethod, DEFAULT_METHOD, DIDMethod, isAliasMethod, isCryptoMethod, isKnownMethod } from "./method.cjs";
|
|
5
7
|
import { ParsedDID, extractIdentifier, extractMethod, formatDid, parse } from "./parse.cjs";
|
|
6
8
|
import { isKnownDid } from "./validate.cjs";
|
|
7
9
|
import { expandShortForm, toShortForm } from "./short-form.cjs";
|
|
8
10
|
import { NameHierarchy, ResolveRoute, getResolveRoute, isGlobalName, isLocalName, normalizeName, parseNameHierarchy } from "./name.cjs";
|
|
9
|
-
import { InMemoryNameRegistry, NameRegistry } from "./name-registry.cjs";
|
|
10
11
|
import { DIDNameResolver, DIDNameResolverOptions, ResolveTraceResult } from "./name-resolver.cjs";
|
|
11
|
-
import {
|
|
12
|
+
import { SUBACCOUNT_DOMAIN_TAG, deriveSubAccountSeed, encodeLengthPrefixedTuple } from "./subaccount.cjs";
|
|
12
13
|
import { BytesType, toAddress, toDid } from "@ocap/util";
|
|
14
|
+
import { types } from "@ocap/mcrypto";
|
|
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 };
|
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
const require_type = require('./type.cjs');
|
|
2
|
+
let _ocap_mcrypto = require("@ocap/mcrypto");
|
|
3
|
+
let _ocap_util = require("@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 ((0, _ocap_util.isHexStrict)(trimmed)) {
|
|
52
|
+
const bytes = (0, _ocap_util.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 (0, _ocap_util.toUint8Array)(`0x${trimmed}`);
|
|
57
|
+
return textEncoder.encode((0, _ocap_util.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 (0, _ocap_mcrypto.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 = (0, _ocap_util.toAddress)(subAccountDid);
|
|
86
|
+
return deriveSubAccountAddress(parentIdentity, scope, index, require_type.toTypeInfo(cleanDid)) === cleanDid;
|
|
87
|
+
};
|
|
88
|
+
return {
|
|
89
|
+
deriveSubAccountAddress,
|
|
90
|
+
isFromSubAccount
|
|
91
|
+
};
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
//#endregion
|
|
95
|
+
exports.SUBACCOUNT_DOMAIN_TAG = SUBACCOUNT_DOMAIN_TAG;
|
|
96
|
+
exports.createSubAccountHelpers = createSubAccountHelpers;
|
|
97
|
+
exports.deriveSubAccountSeed = deriveSubAccountSeed;
|
|
98
|
+
exports.encodeLengthPrefixedTuple = encodeLengthPrefixedTuple;
|