@did-btcr2/method 0.20.0 → 0.22.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.
Files changed (45) hide show
  1. package/README.md +223 -1
  2. package/dist/browser.js +3316 -3292
  3. package/dist/browser.mjs +3316 -3292
  4. package/dist/cjs/core/beacon/singleton.js +6 -9
  5. package/dist/cjs/core/beacon/singleton.js.map +1 -1
  6. package/dist/cjs/core/resolve.js +41 -37
  7. package/dist/cjs/core/resolve.js.map +1 -1
  8. package/dist/cjs/core/update.js +3 -4
  9. package/dist/cjs/core/update.js.map +1 -1
  10. package/dist/cjs/did-btcr2.js +32 -28
  11. package/dist/cjs/did-btcr2.js.map +1 -1
  12. package/dist/cjs/utils/did-document-builder.js +0 -6
  13. package/dist/cjs/utils/did-document-builder.js.map +1 -1
  14. package/dist/cjs/utils/did-document.js +31 -24
  15. package/dist/cjs/utils/did-document.js.map +1 -1
  16. package/dist/esm/core/beacon/singleton.js +6 -9
  17. package/dist/esm/core/beacon/singleton.js.map +1 -1
  18. package/dist/esm/core/resolve.js +41 -37
  19. package/dist/esm/core/resolve.js.map +1 -1
  20. package/dist/esm/core/update.js +3 -4
  21. package/dist/esm/core/update.js.map +1 -1
  22. package/dist/esm/did-btcr2.js +32 -28
  23. package/dist/esm/did-btcr2.js.map +1 -1
  24. package/dist/esm/utils/did-document-builder.js +0 -6
  25. package/dist/esm/utils/did-document-builder.js.map +1 -1
  26. package/dist/esm/utils/did-document.js +31 -24
  27. package/dist/esm/utils/did-document.js.map +1 -1
  28. package/dist/types/core/beacon/singleton.d.ts +1 -1
  29. package/dist/types/core/beacon/singleton.d.ts.map +1 -1
  30. package/dist/types/core/resolve.d.ts +7 -4
  31. package/dist/types/core/resolve.d.ts.map +1 -1
  32. package/dist/types/core/update.d.ts.map +1 -1
  33. package/dist/types/did-btcr2.d.ts +8 -4
  34. package/dist/types/did-btcr2.d.ts.map +1 -1
  35. package/dist/types/utils/did-document-builder.d.ts +0 -1
  36. package/dist/types/utils/did-document-builder.d.ts.map +1 -1
  37. package/dist/types/utils/did-document.d.ts +12 -9
  38. package/dist/types/utils/did-document.d.ts.map +1 -1
  39. package/package.json +7 -6
  40. package/src/core/beacon/singleton.ts +7 -9
  41. package/src/core/resolve.ts +59 -47
  42. package/src/core/update.ts +3 -3
  43. package/src/did-btcr2.ts +34 -32
  44. package/src/utils/did-document-builder.ts +0 -7
  45. package/src/utils/did-document.ts +41 -30
@@ -1,7 +1,9 @@
1
1
  import { getNetwork } from '@did-btcr2/bitcoin';
2
2
  import {
3
3
  BTCR2_DID_DOCUMENT_CONTEXT,
4
+ Canonicalization,
4
5
  DidDocumentError,
6
+ HashBytes,
5
7
  IdentifierTypes,
6
8
  INVALID_DID_DOCUMENT,
7
9
  JSONObject,
@@ -10,11 +12,11 @@ import {
10
12
  } from '@did-btcr2/common';
11
13
  import { CompressedSecp256k1PublicKey } from '@did-btcr2/keypair';
12
14
  import { DidDocument as W3CDidDocument, DidVerificationMethod as W3CDidVerificationMethod } from '@web5/dids';
15
+ import { payments } from 'bitcoinjs-lib';
13
16
  import { BeaconService } from '../core/beacon/interfaces.js';
14
17
  import { BeaconUtils } from '../core/beacon/utils.js';
15
18
  import { Identifier } from '../core/identifier.js';
16
19
  import { Appendix } from './appendix.js';
17
- import { payments } from 'bitcoinjs-lib';
18
20
 
19
21
  export const ID_PLACEHOLDER_VALUE = 'did:btcr2:_';
20
22
  export const BECH32M_CHARS = '';
@@ -36,8 +38,9 @@ export type VerificationRelationships = {
36
38
  capabilityDelegation?: Array<string | DidVerificationMethod>;
37
39
  }
38
40
 
39
- /** Loose type for unvalidated DID document-like input objects. */
41
+ /** Loose type for unvalidated DID / Genesis document-like input objects. */
40
42
  export type DidDocumentLike = Partial<Btcr2DidDocument>;
43
+ export type GenesisDocumentLike = Partial<Btcr2DidDocument>;
41
44
 
42
45
  export interface Btcr2VerificationMethod extends W3CDidVerificationMethod {
43
46
  id: string;
@@ -79,7 +82,6 @@ export class DidVerificationMethod implements Btcr2VerificationMethod {
79
82
  * @type {Btcr2DidDocument}
80
83
  * @extends {W3CDidDocument}
81
84
  * @property {string} id - The identifier of the DID Document.
82
- * @property {Array<string>} [controller] - The controller of the DID Document.
83
85
  * @property {Array<string | JSONObject>} ['@context'] - The context of the DID Document.
84
86
  * @property {Array<DidVerificationMethod>} verificationMethod - The verification methods of the DID Document.
85
87
  * @property {Array<string | DidVerificationMethod>} [authentication] - The authentication methods of the DID Document.
@@ -90,7 +92,6 @@ export class DidVerificationMethod implements Btcr2VerificationMethod {
90
92
  */
91
93
  export interface Btcr2DidDocument extends W3CDidDocument {
92
94
  id: string;
93
- controller?: Array<string>;
94
95
  '@context'?: Array<string | JSONObject>;
95
96
  verificationMethod: Array<DidVerificationMethod>;
96
97
  authentication?: Array<string | DidVerificationMethod>;
@@ -106,7 +107,6 @@ export interface Btcr2DidDocument extends W3CDidDocument {
106
107
  * @type {DidDocument}
107
108
  * @implements {Btcr2DidDocument}
108
109
  * @property {string} id - The identifier of the DID Document.
109
- * @property {Array<string>} [controller] - The controller of the DID Document.
110
110
  * @property {Array<string | JSONObject>} ['@context'] - The context of the DID Document.
111
111
  * @property {Array<DidVerificationMethod>} verificationMethod - The verification methods of the DID Document.
112
112
  * @property {Array<string | DidVerificationMethod>} [authentication] - The authentication methods of the DID Document.
@@ -117,8 +117,10 @@ export interface Btcr2DidDocument extends W3CDidDocument {
117
117
  */
118
118
  export class DidDocument implements Btcr2DidDocument {
119
119
  id: string;
120
- controller?: Array<string>;
121
- '@context'?: Array<string | JSONObject> = BTCR2_DID_DOCUMENT_CONTEXT;
120
+ '@context'?: Array<string | JSONObject> = [
121
+ 'https://www.w3.org/TR/did-1.1',
122
+ 'https://btcr2.dev/context/v1'
123
+ ];
122
124
  verificationMethod: Array<DidVerificationMethod>;
123
125
  authentication?: Array<string | DidVerificationMethod>;
124
126
  assertionMethod?: Array<string | DidVerificationMethod>;
@@ -127,9 +129,14 @@ export class DidDocument implements Btcr2DidDocument {
127
129
  service: Array<BeaconService>;
128
130
  deactivated?: boolean;
129
131
 
130
- constructor(document: Btcr2DidDocument) {
132
+ constructor(document: DidDocumentLike) {
133
+ // Ensure document has an id
134
+ if(!document.id) {
135
+ throw new DidDocumentError('DID Document must have an id', INVALID_DID_DOCUMENT, document);
136
+ }
137
+
131
138
  // Set the ID and ID type
132
- const idType = document.id.includes('k')
139
+ const idType = document.id.includes('k1')
133
140
  ? IdentifierTypes.KEY
134
141
  : IdentifierTypes.EXTERNAL;
135
142
 
@@ -137,16 +144,13 @@ export class DidDocument implements Btcr2DidDocument {
137
144
  const isGenesis = document.id === ID_PLACEHOLDER_VALUE;
138
145
 
139
146
  // Deconstruct the document parts for validation
140
- const { id, controller, verificationMethod: vm, service } = document;
147
+ const { id, verificationMethod: vm, service } = document;
141
148
 
142
149
  // If not genesis, validate core properties
143
150
  if (!isGenesis) {
144
151
  if (!DidDocument.isValidId(id)) {
145
152
  throw new DidDocumentError(`Invalid id: ${id}`, INVALID_DID_DOCUMENT, document);
146
153
  }
147
- if(!DidDocument.isValidController(controller ?? [id])) {
148
- throw new DidDocumentError(`Invalid controller: ${controller}`, INVALID_DID_DOCUMENT, document);
149
- }
150
154
  if (!DidDocument.isValidVerificationMethods(vm)) {
151
155
  throw new DidDocumentError('Invalid verificationMethod: ' + vm, INVALID_DID_DOCUMENT, document);
152
156
  }
@@ -157,10 +161,12 @@ export class DidDocument implements Btcr2DidDocument {
157
161
 
158
162
  // Set core properties
159
163
  this.id = document.id;
160
- this.verificationMethod = document.verificationMethod;
161
- this.service = document.service;
162
- this['@context'] = document['@context'] || BTCR2_DID_DOCUMENT_CONTEXT;
163
- this.controller = document.controller || [this.id];
164
+ this.verificationMethod = document.verificationMethod || [];
165
+ this.service = document.service || [];
166
+ this['@context'] = document['@context'] || [
167
+ 'https://www.w3.org/TR/did-1.1',
168
+ 'https://btcr2.dev/context/v1'
169
+ ];
164
170
 
165
171
  // Relationships logic based on idType
166
172
  if (idType === IdentifierTypes.KEY) {
@@ -180,11 +186,12 @@ export class DidDocument implements Btcr2DidDocument {
180
186
 
181
187
  // Sanitize the DID Document
182
188
  DidDocument.sanitize(this);
189
+
183
190
  // If the DID Document is not an intermediateDocument, validate it
184
- if (!isGenesis) {
185
- DidDocument.validate(this);
186
- } else {
191
+ if (isGenesis) {
187
192
  this.validateGenesis();
193
+ } else {
194
+ DidDocument.validate(this);
188
195
  }
189
196
  }
190
197
 
@@ -192,7 +199,7 @@ export class DidDocument implements Btcr2DidDocument {
192
199
  * Convert the DidDocument to a JSON object.
193
200
  * @returns {DidDocument} The JSON representation of the DidDocument.
194
201
  */
195
- public json(): DidDocument {
202
+ public json(): object {
196
203
  return Object.fromEntries(Object.entries(this)) as DidDocument;
197
204
  }
198
205
 
@@ -386,10 +393,6 @@ export class DidDocument implements Btcr2DidDocument {
386
393
  if(this.id !== ID_PLACEHOLDER_VALUE) {
387
394
  throw new DidDocumentError('Invalid GenesisDocument ID', INVALID_DID_DOCUMENT, this);
388
395
  }
389
- // Validate the controller
390
- if(!this.controller?.every(c => c === ID_PLACEHOLDER_VALUE)) {
391
- throw new DidDocumentError('Invalid GenesisDocument controller', INVALID_DID_DOCUMENT, this);
392
- }
393
396
  // Validate the verificationMethod
394
397
  if(!this.verificationMethod.every(vm => vm.id.includes(ID_PLACEHOLDER_VALUE) && vm.controller === ID_PLACEHOLDER_VALUE)) {
395
398
  throw new DidDocumentError('Invalid GenesisDocument verificationMethod', INVALID_DID_DOCUMENT, this);
@@ -433,8 +436,8 @@ export class Document {
433
436
  * @extends {DidDocument}
434
437
  */
435
438
  export class GenesisDocument extends DidDocument {
436
- constructor(document: object | DidDocument) {
437
- super(document as DidDocument);
439
+ constructor(document: DidDocumentLike) {
440
+ super(document);
438
441
  }
439
442
 
440
443
  /**
@@ -471,8 +474,7 @@ export class GenesisDocument extends DidDocument {
471
474
  relationships: VerificationRelationships,
472
475
  service: Array<BeaconService>
473
476
  ): GenesisDocument {
474
- const id = ID_PLACEHOLDER_VALUE;
475
- return new GenesisDocument({ id, ...relationships, verificationMethod, service, });
477
+ return new GenesisDocument({ id: ID_PLACEHOLDER_VALUE, ...relationships, verificationMethod, service, });
476
478
  }
477
479
 
478
480
  /**
@@ -485,7 +487,7 @@ export class GenesisDocument extends DidDocument {
485
487
  const id = ID_PLACEHOLDER_VALUE;
486
488
  const address = payments.p2pkh({ pubkey: pk.compressed, network: getNetwork(network) })?.address;
487
489
  const services = [{
488
- id : `${id}#key-0`,
490
+ id : `${id}#service-0`,
489
491
  serviceEndpoint : `bitcoin:${address}`,
490
492
  type : 'SingletonBeacon',
491
493
  }];
@@ -516,4 +518,13 @@ export class GenesisDocument extends DidDocument {
516
518
  public static fromJSON(object: object | DidDocument): GenesisDocument {
517
519
  return new GenesisDocument(object as Btcr2DidDocument);
518
520
  }
521
+
522
+ /**
523
+ * Convert a GenesisDocument to genesis bytes.
524
+ * @param {GenesisDocument} genesisDocument The GenesisDocument to convert.
525
+ * @returns {Bytes} The genesis bytes.
526
+ */
527
+ static toGenesisBytes(genesisDocument: GenesisDocumentLike): HashBytes {
528
+ return Canonicalization.andHash(genesisDocument);
529
+ }
519
530
  }